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
This commit is contained in:
David M 2022-01-24 19:57:24 -05:00 committed by GitHub
parent b4f4dacb82
commit 49cc465604
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 0 deletions

View file

@ -24,6 +24,7 @@ struct Controls {
pause: AtomicBool,
volume: Mutex<f32>,
stopped: AtomicBool,
speed: Mutex<f32>,
}
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.

View file

@ -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 {

View file

@ -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.