Take format for SilentAudioGenerator

This simplifies the caller slightly.

PiperOrigin-RevId: 495234339
This commit is contained in:
andrewlewis 2022-12-14 08:24:47 +00:00 committed by Ian Baker
parent b1425aa6f4
commit 3060b97180
3 changed files with 30 additions and 10 deletions

View file

@ -69,11 +69,7 @@ import org.checkerframework.dataflow.qual.Pure;
super(inputFormat, streamStartPositionUs, muxerWrapper);
if (forceSilentAudioDurationUs != C.TIME_UNSET) {
silentAudioGenerator =
new SilentAudioGenerator(
forceSilentAudioDurationUs,
inputFormat.sampleRate,
Util.getPcmFrameSize(C.ENCODING_PCM_16BIT, inputFormat.channelCount));
silentAudioGenerator = new SilentAudioGenerator(inputFormat, forceSilentAudioDurationUs);
} else {
silentAudioGenerator = null;
}

View file

@ -17,6 +17,8 @@
package androidx.media3.transformer;
import androidx.media3.common.C;
import androidx.media3.common.Format;
import androidx.media3.common.util.Util;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -27,8 +29,13 @@ import java.nio.ByteOrder;
private long remainingBytesToOutput;
public SilentAudioGenerator(long totalDurationUs, long sampleRate, int frameSize) {
remainingBytesToOutput = frameSize * ((sampleRate * totalDurationUs) / C.MICROS_PER_SECOND);
public SilentAudioGenerator(Format format, long totalDurationUs) {
int frameSize =
Util.getPcmFrameSize(
format.pcmEncoding == Format.NO_VALUE ? C.ENCODING_PCM_16BIT : format.pcmEncoding,
format.channelCount);
long outputFrameCount = (format.sampleRate * totalDurationUs) / C.MICROS_PER_SECOND;
remainingBytesToOutput = frameSize * outputFrameCount;
internalBuffer =
ByteBuffer.allocate(DEFAULT_BUFFER_SIZE_FRAMES * frameSize).order(ByteOrder.nativeOrder());
internalBuffer.flip();

View file

@ -17,6 +17,8 @@ package androidx.media3.transformer;
import static com.google.common.truth.Truth.assertThat;
import androidx.media3.common.C;
import androidx.media3.common.Format;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.nio.ByteBuffer;
import org.junit.Test;
@ -30,7 +32,12 @@ public class SilentAudioGeneratorTest {
public void numberOfBytesProduced_isCorrect() {
SilentAudioGenerator generator =
new SilentAudioGenerator(
/* totalDurationUs= */ 3_000_000, /* sampleRate= */ 88_200, /* frameSize= */ 12);
new Format.Builder()
.setSampleRate(88_200)
.setPcmEncoding(C.ENCODING_PCM_16BIT)
.setChannelCount(6)
.build(),
/* totalDurationUs= */ 3_000_000);
int bytesOutput = 0;
while (!generator.isEnded()) {
ByteBuffer output = generator.getBuffer();
@ -47,7 +54,12 @@ public class SilentAudioGeneratorTest {
public void lastBufferProduced_isCorrectSize() {
SilentAudioGenerator generator =
new SilentAudioGenerator(
/* totalDurationUs= */ 1_000_000, /* sampleRate= */ 44_100, /* frameSize= */ 4);
new Format.Builder()
.setSampleRate(44_100)
.setPcmEncoding(C.ENCODING_PCM_16BIT)
.setChannelCount(2)
.build(),
/* totalDurationUs= */ 1_000_000);
int currentBufferSize = 0;
while (!generator.isEnded()) {
@ -66,7 +78,12 @@ public class SilentAudioGeneratorTest {
public void totalBytesLowerThanDefaultBufferSize_smallBufferProduced() {
SilentAudioGenerator generator =
new SilentAudioGenerator(
/* totalDurationUs= */ 5_000, /* sampleRate= */ 48_000, /* frameSize= */ 4);
new Format.Builder()
.setSampleRate(48_000)
.setPcmEncoding(C.ENCODING_PCM_16BIT)
.setChannelCount(2)
.build(),
/* totalDurationUs= */ 5_000);
// 5_000 * 48_000 * 4 / 1_000_000 = 960
assertThat(generator.getBuffer().remaining()).isEqualTo(960);
}