Skip to content

Commit

Permalink
Word-align wav total audio length to correctly parse metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mXaln committed Jun 5, 2024
1 parent e7958ef commit e6acf93
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion common/audio/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = 'org.wycliffeassociates.otter.common'
version = '0.4.2'
version = '0.4.3'

dependencies {
testImplementation "junit:junit:$junitVer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ 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 @@ -24,6 +24,7 @@ internal const val RIFF_HEADER_SIZE = 12
internal const val CHUNK_HEADER_SIZE = 8
internal const val CHUNK_LABEL_SIZE = 4
internal const val DWORD_SIZE = 4
internal const val WORD_SIZE = 2

// chunk data must be word aligned but the size might not account for padding
// therefore, if odd, the size we read must add one to include the padding
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 + totalAudioLength
val metadataStart = headerSize + getWordAlignedLength(totalAudioLength)
it.skip(metadataStart.toLong())
it.read(bytes)
}
Expand Down Expand Up @@ -197,3 +197,6 @@ 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

0 comments on commit e6acf93

Please sign in to comment.