From 49cc46560481f9d8b5247f98428f13e3f2b12f51 Mon Sep 17 00:00:00 2001 From: David M Date: Mon, 24 Jan 2022 19:57:24 -0500 Subject: [PATCH] Change Sink playback speed (#398) * Add speed to Sink Controls * Add Sink::set_speed * Add Sink::speed getter * Add speed and set_speed for SpatialSink * Fix whitespace from previous commit --- src/sink.rs | 25 +++++++++++++++++++++++++ src/source/speed.rs | 6 ++++++ src/spatial_sink.rs | 18 ++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/src/sink.rs b/src/sink.rs index 5faaa9d..ab03dec 100644 --- a/src/sink.rs +++ b/src/sink.rs @@ -24,6 +24,7 @@ struct Controls { pause: AtomicBool, volume: Mutex, stopped: AtomicBool, + speed: Mutex, } impl Sink { @@ -47,6 +48,7 @@ impl Sink { pause: AtomicBool::new(false), volume: Mutex::new(1.0), stopped: AtomicBool::new(false), + speed: Mutex::new(1.0), }), sound_count: Arc::new(AtomicUsize::new(0)), detached: false, @@ -65,6 +67,7 @@ impl Sink { let controls = self.controls.clone(); let source = source + .speed(1.0) .pausable(false) .amplify(1.0) .stoppable() @@ -76,6 +79,10 @@ impl Sink { src.inner_mut() .inner_mut() .set_paused(controls.pause.load(Ordering::SeqCst)); + src.inner_mut() + .inner_mut() + .inner_mut() + .set_factor(*controls.speed.lock().unwrap()); } }) .convert_samples(); @@ -102,6 +109,24 @@ impl Sink { *self.controls.volume.lock().unwrap() = value; } + /// Gets the speed of the sound. + /// + /// The value `1.0` is the "normal" speed (unfiltered input). Any value other than `1.0` will + /// change the play speed of the sound. + #[inline] + pub fn speed(&self) -> f32 { + *self.controls.speed.lock().unwrap() + } + + /// Changes the speed of the sound. + /// + /// The value `1.0` is the "normal" speed (unfiltered input). Any value other than `1.0` will + /// change the play speed of the sound. + #[inline] + pub fn set_speed(&self, value: f32) { + *self.controls.speed.lock().unwrap() = value; + } + /// Resumes playback of a paused sink. /// /// No effect if not paused. diff --git a/src/source/speed.rs b/src/source/speed.rs index 4a60ee5..5598be9 100644 --- a/src/source/speed.rs +++ b/src/source/speed.rs @@ -19,6 +19,12 @@ where I: Source, I::Item: Sample, { + /// Modifies the speed factor. + #[inline] + pub fn set_factor(&mut self, factor: f32) { + self.factor = factor; + } + /// Returns a reference to the inner source. #[inline] pub fn inner(&self) -> &I { diff --git a/src/spatial_sink.rs b/src/spatial_sink.rs index 3be5c00..71ef38c 100644 --- a/src/spatial_sink.rs +++ b/src/spatial_sink.rs @@ -91,6 +91,24 @@ impl SpatialSink { self.sink.set_volume(value); } + /// Gets the speed of the sound. + /// + /// The value `1.0` is the "normal" speed (unfiltered input). Any value other than `1.0` will + /// change the play speed of the sound. + #[inline] + pub fn speed(&self) -> f32 { + self.sink.speed() + } + + /// Changes the speed of the sound. + /// + /// The value `1.0` is the "normal" speed (unfiltered input). Any value other than `1.0` will + /// change the play speed of the sound. + #[inline] + pub fn set_speed(&self, value: f32) { + self.sink.set_speed(value) + } + /// Resumes playback of a paused sound. /// /// No effect if not paused.