From ba8bbd8941afca62454ef69608cc2a6be90fbdc6 Mon Sep 17 00:00:00 2001 From: andrewlewis Date: Thu, 29 Mar 2018 05:47:47 -0700 Subject: [PATCH] Add skip silence option to PlaybackParameters ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=190917894 --- RELEASENOTES.md | 2 + .../exoplayer2/PlaybackParameters.java | 37 +++++++++++++------ .../exoplayer2/audio/DefaultAudioSink.java | 9 +++-- .../android/exoplayer2/util/EventLogger.java | 4 +- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 6b16e265ea..810fc0ee3f 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -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. diff --git a/library/core/src/main/java/com/google/android/exoplayer2/PlaybackParameters.java b/library/core/src/main/java/com/google/android/exoplayer2/PlaybackParameters.java index 47d5bc88b9..0ad425e12f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/PlaybackParameters.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/PlaybackParameters.java @@ -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; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/audio/DefaultAudioSink.java b/library/core/src/main/java/com/google/android/exoplayer2/audio/DefaultAudioSink.java index 7270336a08..0fa7257f16 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/audio/DefaultAudioSink.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/audio/DefaultAudioSink.java @@ -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() diff --git a/library/core/src/main/java/com/google/android/exoplayer2/util/EventLogger.java b/library/core/src/main/java/com/google/android/exoplayer2/util/EventLogger.java index 2a3b36c96b..010e60830a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/util/EventLogger.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/util/EventLogger.java @@ -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