Commit 88a97fdb by andrewlewis Committed by christosts

Fix audio generation in silence skipping test

Tests in `SilenceSkippingAudioProcessorTest` used half as many short integers as needed for channel values when generating alternating silence/noise input. Fix this by passing left and right channel input.

PiperOrigin-RevId: 509188074
parent 117ce33a
...@@ -15,13 +15,14 @@ ...@@ -15,13 +15,14 @@
*/ */
package com.google.android.exoplayer2.audio; package com.google.android.exoplayer2.audio;
import static com.google.android.exoplayer2.util.Assertions.checkState;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static java.lang.Math.min; import static java.lang.Math.min;
import static java.lang.Short.MAX_VALUE;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.audio.AudioProcessor.AudioFormat; import com.google.android.exoplayer2.audio.AudioProcessor.AudioFormat;
import com.google.android.exoplayer2.util.Assertions;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.ShortBuffer; import java.nio.ShortBuffer;
...@@ -144,9 +145,10 @@ public final class SilenceSkippingAudioProcessorTest { ...@@ -144,9 +145,10 @@ public final class SilenceSkippingAudioProcessorTest {
long totalOutputFrames = long totalOutputFrames =
process(silenceSkippingAudioProcessor, inputBufferProvider, INPUT_BUFFER_SIZE); process(silenceSkippingAudioProcessor, inputBufferProvider, INPUT_BUFFER_SIZE);
// The right number of frames are skipped/output. // The output consists of 50000 frames of noise, plus 20 frames of padding at the start and 99 *
assertThat(totalOutputFrames).isEqualTo(57980); // 40 frames of padding after that.
assertThat(silenceSkippingAudioProcessor.getSkippedFrames()).isEqualTo(42020); assertThat(totalOutputFrames).isEqualTo(50000 + (20 + 99 * 40));
assertThat(silenceSkippingAudioProcessor.getSkippedFrames()).isEqualTo(50000 - (20 + 99 * 40));
} }
@Test @Test
...@@ -169,9 +171,10 @@ public final class SilenceSkippingAudioProcessorTest { ...@@ -169,9 +171,10 @@ public final class SilenceSkippingAudioProcessorTest {
long totalOutputFrames = long totalOutputFrames =
process(silenceSkippingAudioProcessor, inputBufferProvider, /* inputBufferSize= */ 80); process(silenceSkippingAudioProcessor, inputBufferProvider, /* inputBufferSize= */ 80);
// The right number of frames are skipped/output. // The output consists of 50000 frames of noise, plus 20 frames of padding at the start and 99 *
assertThat(totalOutputFrames).isEqualTo(57980); // 40 frames of padding after that.
assertThat(silenceSkippingAudioProcessor.getSkippedFrames()).isEqualTo(42020); assertThat(totalOutputFrames).isEqualTo(50000 + (20 + 99 * 40));
assertThat(silenceSkippingAudioProcessor.getSkippedFrames()).isEqualTo(50000 - (20 + 99 * 40));
} }
@Test @Test
...@@ -194,9 +197,10 @@ public final class SilenceSkippingAudioProcessorTest { ...@@ -194,9 +197,10 @@ public final class SilenceSkippingAudioProcessorTest {
long totalOutputFrames = long totalOutputFrames =
process(silenceSkippingAudioProcessor, inputBufferProvider, /* inputBufferSize= */ 120); process(silenceSkippingAudioProcessor, inputBufferProvider, /* inputBufferSize= */ 120);
// The right number of frames are skipped/output. // The output consists of 50000 frames of noise, plus 20 frames of padding at the start and 99 *
assertThat(totalOutputFrames).isEqualTo(57980); // 40 frames of padding after that.
assertThat(silenceSkippingAudioProcessor.getSkippedFrames()).isEqualTo(42020); assertThat(totalOutputFrames).isEqualTo(50000 + (20 + 99 * 40));
assertThat(silenceSkippingAudioProcessor.getSkippedFrames()).isEqualTo(50000 - (20 + 99 * 40));
} }
@Test @Test
...@@ -221,9 +225,10 @@ public final class SilenceSkippingAudioProcessorTest { ...@@ -221,9 +225,10 @@ public final class SilenceSkippingAudioProcessorTest {
long totalOutputFrames = long totalOutputFrames =
process(silenceSkippingAudioProcessor, inputBufferProvider, /* inputBufferSize= */ 120); process(silenceSkippingAudioProcessor, inputBufferProvider, /* inputBufferSize= */ 120);
// The right number of frames are skipped/output. // The output consists of 50000 frames of noise, plus 21 frames of padding at the start and 99 *
assertThat(totalOutputFrames).isEqualTo(58379); // 42 frames of padding after that.
assertThat(silenceSkippingAudioProcessor.getSkippedFrames()).isEqualTo(41621); assertThat(totalOutputFrames).isEqualTo(50000 + (21 + 99 * 42));
assertThat(silenceSkippingAudioProcessor.getSkippedFrames()).isEqualTo(50000 - (21 + 99 * 42));
} }
@Test @Test
...@@ -289,11 +294,13 @@ public final class SilenceSkippingAudioProcessorTest { ...@@ -289,11 +294,13 @@ public final class SilenceSkippingAudioProcessorTest {
Pcm16BitAudioBuilder audioBuilder = new Pcm16BitAudioBuilder(channelCount, totalFrameCount); Pcm16BitAudioBuilder audioBuilder = new Pcm16BitAudioBuilder(channelCount, totalFrameCount);
while (!audioBuilder.isFull()) { while (!audioBuilder.isFull()) {
int silenceDurationFrames = (silenceDurationMs * sampleRate) / 1000; int silenceDurationFrames = (silenceDurationMs * sampleRate) / 1000;
// Append stereo silence.
audioBuilder.appendFrames( audioBuilder.appendFrames(
/* count= */ silenceDurationFrames, /* channelLevels...= */ (short) 0); /* count= */ silenceDurationFrames, /* channelLevels...= */ (short) 0, (short) 0);
int noiseDurationFrames = (noiseDurationMs * sampleRate) / 1000; int noiseDurationFrames = (noiseDurationMs * sampleRate) / 1000;
// Append stereo noise.
audioBuilder.appendFrames( audioBuilder.appendFrames(
/* count= */ noiseDurationFrames, /* channelLevels...= */ Short.MAX_VALUE); /* count= */ noiseDurationFrames, /* channelLevels...= */ MAX_VALUE, MAX_VALUE);
} }
return new InputBufferProvider(audioBuilder.build()); return new InputBufferProvider(audioBuilder.build());
} }
...@@ -345,7 +352,8 @@ public final class SilenceSkippingAudioProcessorTest { ...@@ -345,7 +352,8 @@ public final class SilenceSkippingAudioProcessorTest {
* Appends {@code count} audio frames, using the specified {@code channelLevels} in each frame. * Appends {@code count} audio frames, using the specified {@code channelLevels} in each frame.
*/ */
public void appendFrames(int count, short... channelLevels) { public void appendFrames(int count, short... channelLevels) {
Assertions.checkState(!built); checkState(!built);
checkState(channelLevels.length == channelCount);
for (int i = 0; i < count; i += channelCount) { for (int i = 0; i < count; i += channelCount) {
for (short channelLevel : channelLevels) { for (short channelLevel : channelLevels) {
buffer.put(channelLevel); buffer.put(channelLevel);
...@@ -355,13 +363,13 @@ public final class SilenceSkippingAudioProcessorTest { ...@@ -355,13 +363,13 @@ public final class SilenceSkippingAudioProcessorTest {
/** Returns whether the buffer is full. */ /** Returns whether the buffer is full. */
public boolean isFull() { public boolean isFull() {
Assertions.checkState(!built); checkState(!built);
return !buffer.hasRemaining(); return !buffer.hasRemaining();
} }
/** Returns the built buffer. After calling this method the builder should not be reused. */ /** Returns the built buffer. After calling this method the builder should not be reused. */
public ShortBuffer build() { public ShortBuffer build() {
Assertions.checkState(!built); checkState(!built);
built = true; built = true;
buffer.flip(); buffer.flip();
return buffer; return buffer;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment