mirror of
https://github.com/RustAudio/rodio
synced 2024-12-13 13:42:34 +00:00
Group the sink controls into one struct
This commit is contained in:
parent
a77acb73ed
commit
8b0a20f42e
1 changed files with 23 additions and 19 deletions
42
src/sink.rs
42
src/sink.rs
|
@ -21,14 +21,18 @@ pub struct Sink {
|
||||||
queue_tx: Arc<queue::SourcesQueueInput<f32>>,
|
queue_tx: Arc<queue::SourcesQueueInput<f32>>,
|
||||||
sleep_until_end: Mutex<Option<Receiver<()>>>,
|
sleep_until_end: Mutex<Option<Receiver<()>>>,
|
||||||
|
|
||||||
pause: Arc<AtomicBool>,
|
controls: Arc<Controls>,
|
||||||
volume: Arc<Mutex<f32>>,
|
|
||||||
stopped: Arc<AtomicBool>,
|
|
||||||
sound_count: Arc<AtomicUsize>,
|
sound_count: Arc<AtomicUsize>,
|
||||||
|
|
||||||
detached: bool,
|
detached: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Controls {
|
||||||
|
pause: AtomicBool,
|
||||||
|
volume: Mutex<f32>,
|
||||||
|
stopped: AtomicBool,
|
||||||
|
}
|
||||||
|
|
||||||
impl Sink {
|
impl Sink {
|
||||||
/// Builds a new `Sink`.
|
/// Builds a new `Sink`.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -39,9 +43,11 @@ impl Sink {
|
||||||
Sink {
|
Sink {
|
||||||
queue_tx: queue_tx,
|
queue_tx: queue_tx,
|
||||||
sleep_until_end: Mutex::new(None),
|
sleep_until_end: Mutex::new(None),
|
||||||
pause: Arc::new(AtomicBool::new(false)),
|
controls: Arc::new(Controls {
|
||||||
volume: Arc::new(Mutex::new(1.0)),
|
pause: AtomicBool::new(false),
|
||||||
stopped: Arc::new(AtomicBool::new(false)),
|
volume: Mutex::new(1.0),
|
||||||
|
stopped: AtomicBool::new(false),
|
||||||
|
}),
|
||||||
sound_count: Arc::new(AtomicUsize::new(0)),
|
sound_count: Arc::new(AtomicUsize::new(0)),
|
||||||
detached: false,
|
detached: false,
|
||||||
}
|
}
|
||||||
|
@ -54,22 +60,20 @@ impl Sink {
|
||||||
S::Item: Sample,
|
S::Item: Sample,
|
||||||
S::Item: Send
|
S::Item: Send
|
||||||
{
|
{
|
||||||
let volume = self.volume.clone();
|
let controls = self.controls.clone();
|
||||||
let pause = self.pause.clone();
|
|
||||||
let stopped = self.stopped.clone();
|
|
||||||
|
|
||||||
let source = source
|
let source = source
|
||||||
.pausable(false)
|
.pausable(false)
|
||||||
.amplify(1.0)
|
.amplify(1.0)
|
||||||
.stoppable()
|
.stoppable()
|
||||||
.periodic_access(Duration::from_millis(5), move |src| {
|
.periodic_access(Duration::from_millis(5), move |src| {
|
||||||
if stopped.load(Ordering::SeqCst) {
|
if controls.stopped.load(Ordering::SeqCst) {
|
||||||
src.stop();
|
src.stop();
|
||||||
} else {
|
} else {
|
||||||
src.inner_mut().set_factor(*volume.lock().unwrap());
|
src.inner_mut().set_factor(*controls.volume.lock().unwrap());
|
||||||
src.inner_mut()
|
src.inner_mut()
|
||||||
.inner_mut()
|
.inner_mut()
|
||||||
.set_paused(pause.load(Ordering::SeqCst));
|
.set_paused(controls.pause.load(Ordering::SeqCst));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.convert_samples();
|
.convert_samples();
|
||||||
|
@ -84,7 +88,7 @@ impl Sink {
|
||||||
/// multiply each sample by this value.
|
/// multiply each sample by this value.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn volume(&self) -> f32 {
|
pub fn volume(&self) -> f32 {
|
||||||
*self.volume.lock().unwrap()
|
*self.controls.volume.lock().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Changes the volume of the sound.
|
/// Changes the volume of the sound.
|
||||||
|
@ -93,7 +97,7 @@ impl Sink {
|
||||||
/// multiply each sample by this value.
|
/// multiply each sample by this value.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set_volume(&mut self, value: f32) {
|
pub fn set_volume(&mut self, value: f32) {
|
||||||
*self.volume.lock().unwrap() = value;
|
*self.controls.volume.lock().unwrap() = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resumes playback of a paused sound.
|
/// Resumes playback of a paused sound.
|
||||||
|
@ -101,7 +105,7 @@ impl Sink {
|
||||||
/// No effect if not paused.
|
/// No effect if not paused.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn play(&self) {
|
pub fn play(&self) {
|
||||||
self.pause.store(false, Ordering::SeqCst);
|
self.controls.pause.store(false, Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pauses playback of this sink.
|
/// Pauses playback of this sink.
|
||||||
|
@ -110,20 +114,20 @@ impl Sink {
|
||||||
///
|
///
|
||||||
/// A paused sound can be resumed with `play()`.
|
/// A paused sound can be resumed with `play()`.
|
||||||
pub fn pause(&self) {
|
pub fn pause(&self) {
|
||||||
self.pause.store(true, Ordering::SeqCst);
|
self.controls.pause.store(true, Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets if a sound is paused
|
/// Gets if a sound is paused
|
||||||
///
|
///
|
||||||
/// Sounds can be paused and resumed using pause() and play(). This gets if a sound is paused.
|
/// Sounds can be paused and resumed using pause() and play(). This gets if a sound is paused.
|
||||||
pub fn is_paused(&self) -> bool {
|
pub fn is_paused(&self) -> bool {
|
||||||
self.pause.load(Ordering::SeqCst)
|
self.controls.pause.load(Ordering::SeqCst)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stops the sink by emptying the queue.
|
/// Stops the sink by emptying the queue.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn stop(&self) {
|
pub fn stop(&self) {
|
||||||
self.stopped.store(true, Ordering::SeqCst);
|
self.controls.stopped.store(true, Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Destroys the sink without stopping the sounds that are still playing.
|
/// Destroys the sink without stopping the sounds that are still playing.
|
||||||
|
@ -153,7 +157,7 @@ impl Drop for Sink {
|
||||||
self.queue_tx.set_keep_alive_if_empty(false);
|
self.queue_tx.set_keep_alive_if_empty(false);
|
||||||
|
|
||||||
if !self.detached {
|
if !self.detached {
|
||||||
self.stopped.store(true, Ordering::Relaxed);
|
self.controls.stopped.store(true, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue