From 2a9963424b4a951de2a315a8f31573b8a4f11cc9 Mon Sep 17 00:00:00 2001 From: ivanbuper Date: Wed, 30 Oct 2024 09:18:52 -0700 Subject: [PATCH] Refactor `readBytes` to `readFrames` in SpeedChangingAudioProcessor Using frames instead of bytes helps simplify the processing logic for the component and will help with the move towards sample-based `SpeedProvider`s. By using frames, we also abstract away the complexity related to sample encoding. This is a non-functional refactor. PiperOrigin-RevId: 691444106 --- .../audio/SpeedChangingAudioProcessor.java | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/libraries/common/src/main/java/androidx/media3/common/audio/SpeedChangingAudioProcessor.java b/libraries/common/src/main/java/androidx/media3/common/audio/SpeedChangingAudioProcessor.java index 0550aba0b8..d2a0e68766 100644 --- a/libraries/common/src/main/java/androidx/media3/common/audio/SpeedChangingAudioProcessor.java +++ b/libraries/common/src/main/java/androidx/media3/common/audio/SpeedChangingAudioProcessor.java @@ -17,6 +17,7 @@ package androidx.media3.common.audio; import static androidx.media3.common.util.Assertions.checkArgument; +import static androidx.media3.common.util.Util.sampleCountToDurationUs; import static java.lang.Math.min; import static java.lang.Math.round; @@ -87,7 +88,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor { @GuardedBy("lock") private float currentSpeed; - private long bytesRead; + private long framesRead; private boolean endOfStreamQueuedToSonic; @@ -114,11 +115,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor { @Override public void queueInput(ByteBuffer inputBuffer) { - long currentTimeUs = - Util.scaleLargeTimestamp( - /* timestamp= */ bytesRead, - /* multiplier= */ C.MICROS_PER_SECOND, - /* divisor= */ (long) inputAudioFormat.sampleRate * inputAudioFormat.bytesPerFrame); + long currentTimeUs = sampleCountToDurationUs(framesRead, inputAudioFormat.sampleRate); float newSpeed = speedProvider.getSpeed(currentTimeUs); long nextSpeedChangeTimeUs = speedProvider.getNextSpeedChangeTimeUs(currentTimeUs); long sampleRateAlignedNextSpeedChangeTimeUs = @@ -129,7 +126,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor { // one or more mid-sample speed changes. if (sampleRateAlignedNextSpeedChangeTimeUs == currentTimeUs) { long sampleDuration = - Util.sampleCountToDurationUs(/* sampleCount= */ 1, inputAudioFormat.sampleRate); + sampleCountToDurationUs(/* sampleCount= */ 1, inputAudioFormat.sampleRate); newSpeed = speedProvider.getSpeed(currentTimeUs + sampleDuration); nextSpeedChangeTimeUs = speedProvider.getNextSpeedChangeTimeUs(currentTimeUs + sampleDuration); @@ -173,7 +170,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor { } buffer.flip(); } - bytesRead += inputBuffer.position() - startPosition; + framesRead += (inputBuffer.position() - startPosition) / inputAudioFormat.bytesPerFrame; updateLastProcessedInputTime(); inputBuffer.limit(inputBufferLimit); } @@ -385,11 +382,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor { inputSegmentStartTimesUs.get(inputSegmentStartTimesUs.size() - 1) + currentProcessedInputDurationUs; } else { - lastProcessedInputTimeUs = - Util.scaleLargeTimestamp( - /* timestamp= */ bytesRead, - /* multiplier= */ C.MICROS_PER_SECOND, - /* divisor= */ (long) inputAudioFormat.sampleRate * inputAudioFormat.bytesPerFrame); + lastProcessedInputTimeUs = sampleCountToDurationUs(framesRead, inputAudioFormat.sampleRate); } } } @@ -414,7 +407,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor { currentSpeed = 1f; } - bytesRead = 0; + framesRead = 0; endOfStreamQueuedToSonic = false; // TODO: b/339842724 - This should ideally also reset speedAdjustedTimeAsyncInputTimeUs and // clear pendingCallbacks and pendingCallbacksInputTimes. We can't do this at the moment