Skip to content

Commit

Permalink
word-align audio data, so it's size is never an odd number
Browse files Browse the repository at this point in the history
  • Loading branch information
mXaln committed Jun 6, 2024
1 parent e6acf93 commit 79a124c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ open class CueChunk : RiffChunk {
.sum()
}

private fun getWordAlignedLength(length: Int) =
if (length % DWORD_SIZE != 0) length + DWORD_SIZE - (length % DWORD_SIZE) else length

private fun wordAlignedLabel(cue: AudioCue): ByteArray {
val label = cue.label
val alignedLength = getWordAlignedLength(cue.label.length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class WavFile private constructor() : AudioFormatStrategy {
val metadataSize = totalDataLength - nonMetadataSize
val bytes = ByteArray(metadataSize)
file.inputStream().use {
val metadataStart = headerSize + getWordAlignedLength(totalAudioLength)
val metadataStart = headerSize + wordAlign(totalAudioLength)
it.skip(metadataStart.toLong())
it.read(bytes)
}
Expand Down Expand Up @@ -197,6 +197,3 @@ class WavFile private constructor() : AudioFormatStrategy {
return WavOutputStream(this, append, buffered)
}
}

internal fun getWordAlignedLength(length: Int) =
if (length % WORD_SIZE != 0) length + WORD_SIZE - (length % WORD_SIZE) else length
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class CueChunkTest {
}
val os = WavOutputStream(wav)
os.use {
os.write(ByteArray(writeSize))
os.write(ByteArray(wordAlign(writeSize)))
}
val validator = WavFile(file, WavMetadata(listOf(CueChunk())))
val resultMetadata = validator.metadata
Expand Down

0 comments on commit 79a124c

Please sign in to comment.