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
begins, and poll the audio timestamp less frequently once it starts
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:
* Add release method to Cache interface.
* 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 {
/**
* The default playback parameters: real-time playback with no pitch modification.
*/
public static final PlaybackParameters DEFAULT = new PlaybackParameters(1f, 1f);
/** The default playback parameters: real-time playback with no pitch modification. */
public static final PlaybackParameters DEFAULT =
new PlaybackParameters(/* speed= */ 1f, /* pitch= */ 1f, /* skipSilence= */ false);
/**
* The factor by which playback will be sped up.
*/
/** The factor by which playback will be sped up. */
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;
/** Whether to skip silence in the input. */
public final boolean skipSilence;
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.
*/
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(pitch > 0);
this.speed = speed;
this.pitch = pitch;
this.skipSilence = skipSilence;
scaledUsPerMs = Math.round(speed * 1000f);
}
@ -73,14 +83,17 @@ public final class PlaybackParameters {
return false;
}
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
public int hashCode() {
int result = 17;
result = 31 * result + Float.floatToRawIntBits(speed);
result = 31 * result + Float.floatToRawIntBits(pitch);
result = 31 * result + (skipSilence ? 1 : 0);
return result;
}

View file

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

View file

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