Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class NormalizedFrameIterator(
init {
require(frameSize > 0) { "Frame size must be larger than zero." }
require(shiftAmount > 0) { "Shift size must be larger than zero." }
require(shiftAmount <= frameSize) { "Shift size cannot exceed frame size." }
}

override fun hasNext(): Boolean {
Expand Down Expand Up @@ -51,7 +52,11 @@ class NormalizedFrameIterator(
DoubleVector(data)
} else {
val previous = currentFrame!!.data.clone()
System.arraycopy(data, 0, previous, previous.size - shiftAmount, shiftAmount)
val preserved = previous.size - shiftAmount
if (preserved > 0) {
System.arraycopy(previous, shiftAmount, previous, 0, preserved)
}
System.arraycopy(data, 0, previous, preserved, data.size)
DoubleVector(previous)
}
frameCounter++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NormalizedFrameIteratorTest {
assertEquals(2, frames.size)
val normalization = 0x7fff.toDouble()
val expectedFirst = doubleArrayOf(0.0, 1000 / normalization, 2000 / normalization, 3000 / normalization)
val expectedSecond = doubleArrayOf(0.0, 1000 / normalization, 4000 / normalization, 5000 / normalization)
val expectedSecond = doubleArrayOf(2000 / normalization, 3000 / normalization, 4000 / normalization, 5000 / normalization)
frames[0].forEachIndexed { index, value ->
assertEquals(expectedFirst[index], value, 1e-3)
}
Expand All @@ -51,4 +51,12 @@ class NormalizedFrameIteratorTest {
assertEquals(3000 / 0x7fff.toDouble(), first[2], 1e-3)
assertEquals(0.0, first[3], 1e-6)
}

@Test(expected = IllegalArgumentException::class)
fun `iterator rejects shift larger than frame size`() {
val format = PcmAudioFormat.mono16BitSignedLittleEndian(8000)
val stream = PcmMonoInputStream(format, ByteArrayInputStream(ByteArray(0)))

NormalizedFrameIterator(stream, frameSize = 2, shiftAmount = 3, applyPadding = false)
}
}
Loading