mirror of
https://github.com/androidx/media.git
synced 2025-04-29 22:36:54 +08:00
Move util methods in SpeedChangingAudioProcessor
to Util
This CL is prework for implementing `RandomParameterizedSpeedChangingAudioProcessorTest`, which will build on logic present in `RandomParameterizedSonicTest`. This is a non-functional change. PiperOrigin-RevId: 684838017
This commit is contained in:
parent
984b0bb31a
commit
337e59e733
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package androidx.media3.common.audio;
|
||||
|
||||
import static androidx.media3.test.utils.TestUtil.generateFloatInRange;
|
||||
import static androidx.media3.test.utils.TestUtil.roundToDecimalPlaces;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static java.lang.Math.max;
|
||||
|
||||
@ -97,8 +99,10 @@ public final class RandomParameterizedSonicTest {
|
||||
ImmutableSet.Builder<Float> speedsBuilder = new ImmutableSet.Builder<>();
|
||||
|
||||
for (int i = 0; i < PARAM_COUNT; i++) {
|
||||
Range<Float> r = SPEED_RANGES.get(i % SPEED_RANGES.size());
|
||||
speedsBuilder.add(round(generateFloatInRange(r)));
|
||||
Range<Float> range = SPEED_RANGES.get(i % SPEED_RANGES.size());
|
||||
speedsBuilder.add(
|
||||
(float)
|
||||
roundToDecimalPlaces(generateFloatInRange(random, range), SPEED_DECIMAL_PRECISION));
|
||||
}
|
||||
ImmutableSet<Float> speeds = speedsBuilder.build();
|
||||
|
||||
@ -235,13 +239,4 @@ public final class RandomParameterizedSonicTest {
|
||||
|
||||
assertThat(readSampleCount).isWithin(tolerance).of(expectedSampleCount.longValueExact());
|
||||
}
|
||||
|
||||
private static float round(float num) {
|
||||
BigDecimal bigDecimal = new BigDecimal(Float.toString(num));
|
||||
return bigDecimal.setScale(SPEED_DECIMAL_PRECISION, RoundingMode.HALF_EVEN).floatValue();
|
||||
}
|
||||
|
||||
private static float generateFloatInRange(Range<Float> r) {
|
||||
return r.lowerEndpoint() + random.nextFloat() * (r.upperEndpoint() - r.lowerEndpoint());
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +51,9 @@ import androidx.media3.extractor.ExtractorInput;
|
||||
import androidx.media3.extractor.PositionHolder;
|
||||
import androidx.media3.extractor.SeekMap;
|
||||
import androidx.media3.extractor.metadata.MetadataInputBuffer;
|
||||
import com.google.common.collect.BoundType;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.primitives.Bytes;
|
||||
import com.google.common.primitives.UnsignedBytes;
|
||||
@ -63,6 +65,8 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
@ -663,6 +667,37 @@ public class TestUtil {
|
||||
return checkNotNull(parcel.readBundle(throwingClassLoader));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a randomly generated float within the specified range, using {@code random} as random
|
||||
* number generator.
|
||||
*
|
||||
* <p>{@code range} must be a bounded range.
|
||||
*/
|
||||
public static float generateFloatInRange(Random random, Range<Float> range) {
|
||||
float bottom =
|
||||
range.lowerBoundType() == BoundType.OPEN
|
||||
? Math.nextUp(range.lowerEndpoint())
|
||||
: range.lowerEndpoint();
|
||||
float top =
|
||||
range.upperBoundType() == BoundType.OPEN
|
||||
? Math.nextDown(range.upperEndpoint())
|
||||
: range.upperEndpoint();
|
||||
|
||||
return bottom + random.nextFloat() * (top - bottom);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rounds {@code num} to {@code decimalPlaces}, using {@link RoundingMode#HALF_EVEN}.
|
||||
*
|
||||
* <p>The number is converted first into a {@link BigDecimal} on which the scale is set to {@code
|
||||
* decimalPlaces}. Finally, the method returns the {@link BigDecimal#doubleValue()} of the scaled
|
||||
* big decimal.
|
||||
*/
|
||||
public static double roundToDecimalPlaces(double num, int decimalPlaces) {
|
||||
BigDecimal bigDecimal = new BigDecimal(Double.toString(num));
|
||||
return bigDecimal.setScale(decimalPlaces, RoundingMode.HALF_EVEN).doubleValue();
|
||||
}
|
||||
|
||||
private static final class NoUidOrShufflingTimeline extends Timeline {
|
||||
|
||||
private final Timeline delegate;
|
||||
|
Loading…
x
Reference in New Issue
Block a user