1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::Sound;

use super::{SetSpeed, SetVolume};

/// A Sound which can be paused.
pub trait SetPaused {
    /// Pause or unpause the sound.
    fn set_paused(&mut self, paused: bool);
}

/// A wrapper to make a Sound pausable.
pub struct Pausable<S: Sound> {
    inner: S,
    paused: bool,
}

impl<S> Pausable<S>
where
    S: Sound,
{
    /// Wrap `inner` and allow it to be paused via
    /// [set_paused][SetPaused::set_paused].
    pub fn new(inner: S) -> Self {
        Pausable {
            inner,
            paused: false,
        }
    }

    /// Get a reference to the wrapped inner Sound.
    pub fn inner(&self) -> &S {
        &self.inner
    }

    /// Get a mutable reference to the wrapped inner Sound.
    pub fn inner_mut(&mut self) -> &mut S {
        &mut self.inner
    }

    /// Unwrap and return the previously wrapped Sound.
    pub fn into_inner(self) -> S {
        self.inner
    }
}

impl<S> Sound for Pausable<S>
where
    S: Sound,
{
    fn channel_count(&self) -> u16 {
        self.inner.channel_count()
    }

    fn sample_rate(&self) -> u32 {
        self.inner.sample_rate()
    }

    fn next_sample(&mut self) -> Result<crate::NextSample, crate::Error> {
        if self.paused {
            return Ok(crate::NextSample::Paused);
        }
        self.inner.next_sample()
    }

    fn on_start_of_batch(&mut self) {
        self.inner.on_start_of_batch()
    }
}

impl<S> Pausable<S>
where
    S: Sound,
{
    /// Return if the Sound is currently being paused.
    pub fn paused(&self) -> bool {
        self.paused
    }
}

impl<S> SetPaused for Pausable<S>
where
    S: Sound,
{
    fn set_paused(&mut self, paused: bool) {
        self.paused = paused;
    }
}

impl<S> SetVolume for Pausable<S>
where
    S: Sound + SetVolume,
{
    fn set_volume(&mut self, multiplier: f32) {
        self.inner.set_volume(multiplier)
    }
}

impl<S> SetSpeed for Pausable<S>
where
    S: Sound + SetSpeed,
{
    fn set_speed(&mut self, multiplier: f32) {
        self.inner.set_speed(multiplier)
    }
}

#[cfg(test)]
#[path = "./tests/pausable.rs"]
mod tests;