Add skip silence option to PlaybackParameters

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=190917894
This commit is contained in:
andrewlewis 2018-03-29 05:47:47 -07:00 committed by Oliver Woodman
parent 017c95ffdd
commit ba8bbd8941
4 changed files with 35 additions and 17 deletions

View file

@ -37,6 +37,8 @@
* Fix an issue where the playback position would pause just after playback * Fix an issue where the playback position would pause just after playback
begins, and poll the audio timestamp less frequently once it starts begins, and poll the audio timestamp less frequently once it starts
advancing ([#3841](https://github.com/google/ExoPlayer/issues/3841)). advancing ([#3841](https://github.com/google/ExoPlayer/issues/3841)).
* Add an option to skip silent audio in `PlaybackParameters`
((#2635)[https://github.com/google/ExoPlayer/issues/2635]).
* Caching: * Caching:
* Add release method to Cache interface. * Add release method to Cache interface.
* Prevent multiple instances of SimpleCache in the same folder. * Prevent multiple instances of SimpleCache in the same folder.

View file

@ -22,21 +22,19 @@ import com.google.android.exoplayer2.util.Assertions;
*/ */
public final class PlaybackParameters { public final class PlaybackParameters {
/** /** The default playback parameters: real-time playback with no pitch modification. */
* The default playback parameters: real-time playback with no pitch modification. public static final PlaybackParameters DEFAULT =
*/ new PlaybackParameters(/* speed= */ 1f, /* pitch= */ 1f, /* skipSilence= */ false);
public static final PlaybackParameters DEFAULT = new PlaybackParameters(1f, 1f);
/** /** The factor by which playback will be sped up. */
* The factor by which playback will be sped up.
*/
public final float speed; public final float speed;
/** /** The factor by which the audio pitch will be scaled. */
* The factor by which the audio pitch will be scaled.
*/
public final float pitch; public final float pitch;
/** Whether to skip silence in the input. */
public final boolean skipSilence;
private final int scaledUsPerMs; private final int scaledUsPerMs;
/** /**
@ -46,10 +44,22 @@ public final class PlaybackParameters {
* @param pitch The factor by which the audio pitch will be scaled. Must be greater than zero. * @param pitch The factor by which the audio pitch will be scaled. Must be greater than zero.
*/ */
public PlaybackParameters(float speed, float pitch) { public PlaybackParameters(float speed, float pitch) {
this(speed, pitch, /* skipSilence= */ false);
}
/**
* Creates new playback parameters.
*
* @param speed The factor by which playback will be sped up. Must be greater than zero.
* @param pitch The factor by which the audio pitch will be scaled. Must be greater than zero.
* @param skipSilence Whether to skip silences in the audio stream.
*/
public PlaybackParameters(float speed, float pitch, boolean skipSilence) {
Assertions.checkArgument(speed > 0); Assertions.checkArgument(speed > 0);
Assertions.checkArgument(pitch > 0); Assertions.checkArgument(pitch > 0);
this.speed = speed; this.speed = speed;
this.pitch = pitch; this.pitch = pitch;
this.skipSilence = skipSilence;
scaledUsPerMs = Math.round(speed * 1000f); scaledUsPerMs = Math.round(speed * 1000f);
} }
@ -73,7 +83,9 @@ public final class PlaybackParameters {
return false; return false;
} }
PlaybackParameters other = (PlaybackParameters) obj; PlaybackParameters other = (PlaybackParameters) obj;
return this.speed == other.speed && this.pitch == other.pitch; return this.speed == other.speed
&& this.pitch == other.pitch
&& this.skipSilence == other.skipSilence;
} }
@Override @Override
@ -81,6 +93,7 @@ public final class PlaybackParameters {
int result = 17; int result = 17;
result = 31 * result + Float.floatToRawIntBits(speed); result = 31 * result + Float.floatToRawIntBits(speed);
result = 31 * result + Float.floatToRawIntBits(pitch); result = 31 * result + Float.floatToRawIntBits(pitch);
result = 31 * result + (skipSilence ? 1 : 0);
return result; return result;
} }

View file

@ -738,9 +738,12 @@ public final class DefaultAudioSink implements AudioSink {
this.playbackParameters = PlaybackParameters.DEFAULT; this.playbackParameters = PlaybackParameters.DEFAULT;
return this.playbackParameters; return this.playbackParameters;
} }
playbackParameters = new PlaybackParameters( playbackParameters =
new PlaybackParameters(
sonicAudioProcessor.setSpeed(playbackParameters.speed), sonicAudioProcessor.setSpeed(playbackParameters.speed),
sonicAudioProcessor.setPitch(playbackParameters.pitch)); sonicAudioProcessor.setPitch(playbackParameters.pitch),
playbackParameters.skipSilence);
silenceSkippingAudioProcessor.setEnabled(playbackParameters.skipSilence);
PlaybackParameters lastSetPlaybackParameters = PlaybackParameters lastSetPlaybackParameters =
drainingPlaybackParameters != null ? drainingPlaybackParameters drainingPlaybackParameters != null ? drainingPlaybackParameters
: !playbackParametersCheckpoints.isEmpty() : !playbackParametersCheckpoints.isEmpty()

View file

@ -115,8 +115,8 @@ public class EventLogger
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) { public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
logd( logd(
Util.formatInvariant( Util.formatInvariant(
"playbackParameters [speed=%.2f, pitch=%.2f]", "playbackParameters [speed=%.2f, pitch=%.2f, skipSilence=%s]",
playbackParameters.speed, playbackParameters.pitch)); playbackParameters.speed, playbackParameters.pitch, playbackParameters.skipSilence));
} }
@Override @Override