SpeedChangingAudioProcessor: fix outputTime calculation

minor fix for arithmetic error in calculated added in f4c60c52b9 (speed is outputTime (change in y) divided by inputTime (change in x), this calculation is inverted in code).

Changed test to cover the case.

PiperOrigin-RevId: 613966027
This commit is contained in:
tofunmi 2024-03-08 09:58:47 -08:00 committed by Copybara-Service
parent 4bca1ac47b
commit a975c75748
2 changed files with 21 additions and 10 deletions

View File

@ -17,6 +17,7 @@
package androidx.media3.common.audio; package androidx.media3.common.audio;
import static java.lang.Math.min; import static java.lang.Math.min;
import static java.lang.Math.round;
import androidx.media3.common.C; import androidx.media3.common.C;
import androidx.media3.common.util.LongArray; import androidx.media3.common.util.LongArray;
@ -210,17 +211,26 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor {
floorIndex--; floorIndex--;
} }
long lastSegmentInputDuration = inputTimeUs - inputSegmentStartTimesUs.get(floorIndex); long lastSegmentInputDuration = inputTimeUs - inputSegmentStartTimesUs.get(floorIndex);
long lastSegmentOutputDuration = long lastSegmentOutputDuration;
floorIndex == inputSegmentStartTimesUs.size() - 1 if (floorIndex == inputSegmentStartTimesUs.size() - 1) {
? getPlayoutDurationAtCurrentSpeed(lastSegmentInputDuration) lastSegmentOutputDuration = getPlayoutDurationAtCurrentSpeed(lastSegmentInputDuration);
: lastSegmentInputDuration } else {
* (inputSegmentStartTimesUs.get(floorIndex + 1) lastSegmentOutputDuration =
- inputSegmentStartTimesUs.get(floorIndex)) round(
/ (outputSegmentStartTimesUs.get(floorIndex + 1) lastSegmentInputDuration
- outputSegmentStartTimesUs.get(floorIndex)); * divide(
outputSegmentStartTimesUs.get(floorIndex + 1)
- outputSegmentStartTimesUs.get(floorIndex),
inputSegmentStartTimesUs.get(floorIndex + 1)
- inputSegmentStartTimesUs.get(floorIndex)));
}
return outputSegmentStartTimesUs.get(floorIndex) + lastSegmentOutputDuration; return outputSegmentStartTimesUs.get(floorIndex) + lastSegmentOutputDuration;
} }
private static double divide(long dividend, long divisor) {
return ((double) dividend) / divisor;
}
private void processPendingCallbacks() { private void processPendingCallbacks() {
synchronized (pendingCallbacksLock) { synchronized (pendingCallbacksLock) {
while (!pendingCallbacks.isEmpty() while (!pendingCallbacks.isEmpty()

View File

@ -357,7 +357,7 @@ public class SpeedChangingAudioProcessorTest {
// The speed change is at 113Us (5*MICROS_PER_SECOND/sampleRate). // The speed change is at 113Us (5*MICROS_PER_SECOND/sampleRate).
SpeedProvider speedProvider = SpeedProvider speedProvider =
TestSpeedProvider.createWithFrameCounts( TestSpeedProvider.createWithFrameCounts(
AUDIO_FORMAT, /* frameCounts= */ new int[] {5, 5}, /* speeds= */ new float[] {1, 2}); AUDIO_FORMAT, /* frameCounts= */ new int[] {5, 5}, /* speeds= */ new float[] {2, 1});
SpeedChangingAudioProcessor speedChangingAudioProcessor = SpeedChangingAudioProcessor speedChangingAudioProcessor =
getConfiguredSpeedChangingAudioProcessor(speedProvider); getConfiguredSpeedChangingAudioProcessor(speedProvider);
ByteBuffer inputBuffer = getInputBuffer(/* frameCount= */ 5); ByteBuffer inputBuffer = getInputBuffer(/* frameCount= */ 5);
@ -374,7 +374,8 @@ public class SpeedChangingAudioProcessorTest {
speedChangingAudioProcessor.getSpeedAdjustedTimeAsync( speedChangingAudioProcessor.getSpeedAdjustedTimeAsync(
/* inputTimeUs= */ 150L, outputTimesUs::add); /* inputTimeUs= */ 150L, outputTimesUs::add);
assertThat(outputTimesUs).containsExactly(50L, 100L, 131L); // 150 is after the speed change so floor(113 / 2 + (150 - 113)*1) -> 93
assertThat(outputTimesUs).containsExactly(25L, 50L, 93L);
} }
private static SpeedChangingAudioProcessor getConfiguredSpeedChangingAudioProcessor( private static SpeedChangingAudioProcessor getConfiguredSpeedChangingAudioProcessor(