mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Take format for SilentAudioGenerator
This simplifies the caller slightly. PiperOrigin-RevId: 495234339
This commit is contained in:
parent
b1425aa6f4
commit
3060b97180
3 changed files with 30 additions and 10 deletions
|
|
@ -69,11 +69,7 @@ import org.checkerframework.dataflow.qual.Pure;
|
||||||
super(inputFormat, streamStartPositionUs, muxerWrapper);
|
super(inputFormat, streamStartPositionUs, muxerWrapper);
|
||||||
|
|
||||||
if (forceSilentAudioDurationUs != C.TIME_UNSET) {
|
if (forceSilentAudioDurationUs != C.TIME_UNSET) {
|
||||||
silentAudioGenerator =
|
silentAudioGenerator = new SilentAudioGenerator(inputFormat, forceSilentAudioDurationUs);
|
||||||
new SilentAudioGenerator(
|
|
||||||
forceSilentAudioDurationUs,
|
|
||||||
inputFormat.sampleRate,
|
|
||||||
Util.getPcmFrameSize(C.ENCODING_PCM_16BIT, inputFormat.channelCount));
|
|
||||||
} else {
|
} else {
|
||||||
silentAudioGenerator = null;
|
silentAudioGenerator = null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@
|
||||||
package androidx.media3.transformer;
|
package androidx.media3.transformer;
|
||||||
|
|
||||||
import androidx.media3.common.C;
|
import androidx.media3.common.C;
|
||||||
|
import androidx.media3.common.Format;
|
||||||
|
import androidx.media3.common.util.Util;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
|
|
||||||
|
|
@ -27,8 +29,13 @@ import java.nio.ByteOrder;
|
||||||
|
|
||||||
private long remainingBytesToOutput;
|
private long remainingBytesToOutput;
|
||||||
|
|
||||||
public SilentAudioGenerator(long totalDurationUs, long sampleRate, int frameSize) {
|
public SilentAudioGenerator(Format format, long totalDurationUs) {
|
||||||
remainingBytesToOutput = frameSize * ((sampleRate * totalDurationUs) / C.MICROS_PER_SECOND);
|
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 =
|
internalBuffer =
|
||||||
ByteBuffer.allocate(DEFAULT_BUFFER_SIZE_FRAMES * frameSize).order(ByteOrder.nativeOrder());
|
ByteBuffer.allocate(DEFAULT_BUFFER_SIZE_FRAMES * frameSize).order(ByteOrder.nativeOrder());
|
||||||
internalBuffer.flip();
|
internalBuffer.flip();
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ package androidx.media3.transformer;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
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 androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -30,7 +32,12 @@ public class SilentAudioGeneratorTest {
|
||||||
public void numberOfBytesProduced_isCorrect() {
|
public void numberOfBytesProduced_isCorrect() {
|
||||||
SilentAudioGenerator generator =
|
SilentAudioGenerator generator =
|
||||||
new SilentAudioGenerator(
|
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;
|
int bytesOutput = 0;
|
||||||
while (!generator.isEnded()) {
|
while (!generator.isEnded()) {
|
||||||
ByteBuffer output = generator.getBuffer();
|
ByteBuffer output = generator.getBuffer();
|
||||||
|
|
@ -47,7 +54,12 @@ public class SilentAudioGeneratorTest {
|
||||||
public void lastBufferProduced_isCorrectSize() {
|
public void lastBufferProduced_isCorrectSize() {
|
||||||
SilentAudioGenerator generator =
|
SilentAudioGenerator generator =
|
||||||
new SilentAudioGenerator(
|
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;
|
int currentBufferSize = 0;
|
||||||
while (!generator.isEnded()) {
|
while (!generator.isEnded()) {
|
||||||
|
|
@ -66,7 +78,12 @@ public class SilentAudioGeneratorTest {
|
||||||
public void totalBytesLowerThanDefaultBufferSize_smallBufferProduced() {
|
public void totalBytesLowerThanDefaultBufferSize_smallBufferProduced() {
|
||||||
SilentAudioGenerator generator =
|
SilentAudioGenerator generator =
|
||||||
new SilentAudioGenerator(
|
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
|
// 5_000 * 48_000 * 4 / 1_000_000 = 960
|
||||||
assertThat(generator.getBuffer().remaining()).isEqualTo(960);
|
assertThat(generator.getBuffer().remaining()).isEqualTo(960);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue