package androidx.media3.common.audio; import androidx.media3.common.audio.AudioProcessor; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Util; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.ShortBuffer; import okhttp3.internal.ws.RealWebSocket; /* loaded from: classes2.dex */ public class SonicAudioProcessor implements AudioProcessor { private static final float CLOSE_THRESHOLD = 1.0E-4f; private static final int MIN_BYTES_FOR_DURATION_SCALING_CALCULATION = 1024; public static final int SAMPLE_RATE_NO_CHANGE = -1; private ByteBuffer buffer; private long inputBytes; private boolean inputEnded; private ByteBuffer outputBuffer; private long outputBytes; private int pendingOutputSampleRate; private boolean pendingSonicRecreation; private ShortBuffer shortBuffer; private Sonic sonic; private float speed = 1.0f; private float pitch = 1.0f; private AudioProcessor.AudioFormat pendingInputAudioFormat = AudioProcessor.AudioFormat.NOT_SET; private AudioProcessor.AudioFormat pendingOutputAudioFormat = AudioProcessor.AudioFormat.NOT_SET; private AudioProcessor.AudioFormat inputAudioFormat = AudioProcessor.AudioFormat.NOT_SET; private AudioProcessor.AudioFormat outputAudioFormat = AudioProcessor.AudioFormat.NOT_SET; public final void setOutputSampleRateHz(int i) { this.pendingOutputSampleRate = i; } public final void setPitch(float f) { if (this.pitch != f) { this.pitch = f; this.pendingSonicRecreation = true; } } public final void setSpeed(float f) { if (this.speed != f) { this.speed = f; this.pendingSonicRecreation = true; } } public SonicAudioProcessor() { ByteBuffer byteBuffer = EMPTY_BUFFER; this.buffer = byteBuffer; this.shortBuffer = byteBuffer.asShortBuffer(); this.outputBuffer = EMPTY_BUFFER; this.pendingOutputSampleRate = -1; } public final long getMediaDuration(long j) { if (this.outputBytes < RealWebSocket.DEFAULT_MINIMUM_DEFLATE_SIZE) { return (long) (this.speed * j); } long pendingInputBytes = this.inputBytes - ((Sonic) Assertions.checkNotNull(this.sonic)).getPendingInputBytes(); if (this.outputAudioFormat.sampleRate == this.inputAudioFormat.sampleRate) { return Util.scaleLargeTimestamp(j, pendingInputBytes, this.outputBytes); } return Util.scaleLargeTimestamp(j, pendingInputBytes * this.outputAudioFormat.sampleRate, this.outputBytes * this.inputAudioFormat.sampleRate); } @Override // androidx.media3.common.audio.AudioProcessor public final AudioProcessor.AudioFormat configure(AudioProcessor.AudioFormat audioFormat) throws AudioProcessor.UnhandledAudioFormatException { if (audioFormat.encoding != 2) { throw new AudioProcessor.UnhandledAudioFormatException(audioFormat); } int i = this.pendingOutputSampleRate; if (i == -1) { i = audioFormat.sampleRate; } this.pendingInputAudioFormat = audioFormat; AudioProcessor.AudioFormat audioFormat2 = new AudioProcessor.AudioFormat(i, audioFormat.channelCount, 2); this.pendingOutputAudioFormat = audioFormat2; this.pendingSonicRecreation = true; return audioFormat2; } @Override // androidx.media3.common.audio.AudioProcessor public final boolean isActive() { return this.pendingOutputAudioFormat.sampleRate != -1 && (Math.abs(this.speed - 1.0f) >= 1.0E-4f || Math.abs(this.pitch - 1.0f) >= 1.0E-4f || this.pendingOutputAudioFormat.sampleRate != this.pendingInputAudioFormat.sampleRate); } @Override // androidx.media3.common.audio.AudioProcessor public final void queueInput(ByteBuffer byteBuffer) { if (byteBuffer.hasRemaining()) { Sonic sonic = (Sonic) Assertions.checkNotNull(this.sonic); ShortBuffer asShortBuffer = byteBuffer.asShortBuffer(); int remaining = byteBuffer.remaining(); this.inputBytes += remaining; sonic.queueInput(asShortBuffer); byteBuffer.position(byteBuffer.position() + remaining); } } @Override // androidx.media3.common.audio.AudioProcessor public final void queueEndOfStream() { Sonic sonic = this.sonic; if (sonic != null) { sonic.queueEndOfStream(); } this.inputEnded = true; } @Override // androidx.media3.common.audio.AudioProcessor public final ByteBuffer getOutput() { int outputSize; Sonic sonic = this.sonic; if (sonic != null && (outputSize = sonic.getOutputSize()) > 0) { if (this.buffer.capacity() < outputSize) { ByteBuffer order = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder()); this.buffer = order; this.shortBuffer = order.asShortBuffer(); } else { this.buffer.clear(); this.shortBuffer.clear(); } sonic.getOutput(this.shortBuffer); this.outputBytes += outputSize; this.buffer.limit(outputSize); this.outputBuffer = this.buffer; } ByteBuffer byteBuffer = this.outputBuffer; this.outputBuffer = EMPTY_BUFFER; return byteBuffer; } @Override // androidx.media3.common.audio.AudioProcessor public final boolean isEnded() { Sonic sonic; return this.inputEnded && ((sonic = this.sonic) == null || sonic.getOutputSize() == 0); } @Override // androidx.media3.common.audio.AudioProcessor public final void flush() { if (isActive()) { this.inputAudioFormat = this.pendingInputAudioFormat; this.outputAudioFormat = this.pendingOutputAudioFormat; if (this.pendingSonicRecreation) { this.sonic = new Sonic(this.inputAudioFormat.sampleRate, this.inputAudioFormat.channelCount, this.speed, this.pitch, this.outputAudioFormat.sampleRate); } else { Sonic sonic = this.sonic; if (sonic != null) { sonic.flush(); } } } this.outputBuffer = EMPTY_BUFFER; this.inputBytes = 0L; this.outputBytes = 0L; this.inputEnded = false; } @Override // androidx.media3.common.audio.AudioProcessor public final void reset() { this.speed = 1.0f; this.pitch = 1.0f; this.pendingInputAudioFormat = AudioProcessor.AudioFormat.NOT_SET; this.pendingOutputAudioFormat = AudioProcessor.AudioFormat.NOT_SET; this.inputAudioFormat = AudioProcessor.AudioFormat.NOT_SET; this.outputAudioFormat = AudioProcessor.AudioFormat.NOT_SET; ByteBuffer byteBuffer = EMPTY_BUFFER; this.buffer = byteBuffer; this.shortBuffer = byteBuffer.asShortBuffer(); this.outputBuffer = EMPTY_BUFFER; this.pendingOutputSampleRate = -1; this.pendingSonicRecreation = false; this.sonic = null; this.inputBytes = 0L; this.outputBytes = 0L; this.inputEnded = false; } }