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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use crate::sounds::wrappers::SetPaused;
use crate::sounds::wrappers::SetVolume;
use crate::Sound;
use std::sync::mpsc;

use super::AddSound;
use super::ClearSounds;
use super::SetSpeed;
use super::Wrapper;

/// Wrap a Sound so that it can be controlled via a [Controller] even after it
/// has been added to the Manager and/or started playing.
///
/// Since new sounds can be added after playing has started, if the inner sound
/// returns Finished it will be converted to Paused by Controllable. Only after
/// the controller has dropped and all sounds have played will Finished be
/// returned;
pub struct Controllable<S: Sound> {
    inner: S,
    command_receiver: mpsc::Receiver<Command<S>>,
    finished: bool,
}

impl<S> Controllable<S>
where
    S: Sound,
{
    /// Wrap `inner` so it can be controlled.
    pub fn new(inner: S) -> (Self, Controller<S>) {
        let (command_sender, command_receiver) = mpsc::channel::<Command<S>>();
        let controllable = Controllable {
            inner,
            command_receiver,
            finished: false,
        };
        let controller = Controller { command_sender };

        (controllable, controller)
    }
}

impl<S> Sound for Controllable<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> {
        let next = self.inner.next_sample()?;
        match next {
            crate::NextSample::Sample(_)
            | crate::NextSample::MetadataChanged
            | crate::NextSample::Paused => Ok(next),
            // Since this is controllable we might add another sound later.
            // Ideally we would do this only if the inner sound can have sounds
            // added to it but I don't think we can branch on S: AddSound here.
            // We could add a Sound::is_addable but lets avoid that until we see
            // a reason why it is necessary.
            crate::NextSample::Finished => {
                if self.finished {
                    Ok(crate::NextSample::Finished)
                } else {
                    Ok(crate::NextSample::Paused)
                }
            }
        }
    }

    fn on_start_of_batch(&mut self) {
        loop {
            match self.command_receiver.try_recv() {
                Ok(command) => command(&mut self.inner),
                Err(mpsc::TryRecvError::Empty) => break,
                Err(mpsc::TryRecvError::Disconnected) => {
                    self.finished = true;
                    break;
                }
            }
        }
        self.inner.on_start_of_batch();
    }
}

impl<S> Wrapper for Controllable<S>
where
    S: Sound,
{
    type Inner = S;

    fn inner(&self) -> &S {
        &self.inner
    }

    fn inner_mut(&mut self) -> &mut Self::Inner {
        &mut self.inner
    }

    fn into_inner(self) -> S {
        self.inner
    }
}

// TODO consider using Small Box for perf
type Command<S> = Box<dyn FnOnce(&mut S) + Send>;

/// The remote Controller for a Sound wrapped in a Controllable.
pub struct Controller<S: Sound> {
    command_sender: mpsc::Sender<Command<S>>,
}

impl<S> Clone for Controller<S>
where
    S: Sound,
{
    fn clone(&self) -> Self {
        Self {
            command_sender: self.command_sender.clone(),
        }
    }
}

impl<S> Controller<S>
where
    S: Sound,
{
    /// Send a custom command to the associated Controllable.
    ///
    /// A command is a closure where you are given mutable access to the inner
    /// sound of the controllable. Many but not all actions are available as
    /// trait functions on `Controller` but sending a command allows for full
    /// control.
    pub fn send_command(&mut self, command: Command<S>) {
        // Ignore the error since it only happens if the receiver
        // has been dropped which is not expected after it has been
        // sent to the manager.
        let _ = self.command_sender.send(command);
    }
}

impl<S> Controller<S>
where
    S: Sound + AddSound,
{
    /// Add `sound` to the sound container.
    pub fn add(&mut self, sound: Box<dyn Sound>) {
        self.send_command(Box::new(|s: &mut S| s.add(sound)));
    }
}

impl<S> Controller<S>
where
    S: Sound + ClearSounds,
{
    /// Clear all sounds currently playing or scheduled to play.
    pub fn clear(&mut self) {
        self.send_command(Box::new(|s: &mut S| s.clear()));
    }
}

impl<S> Controller<S>
where
    S: Sound + SetPaused,
{
    /// Pause or unpause the controllable sound.
    pub fn set_paused(&mut self, paused: bool) {
        self.send_command(Box::new(move |s: &mut S| s.set_paused(paused)));
    }
}

impl<S> Controller<S>
where
    S: Sound + SetSpeed,
{
    /// Set the playback speed of the controllable sound.
    pub fn set_speed(&mut self, speed: f32) {
        self.send_command(Box::new(move |s: &mut S| s.set_speed(speed)));
    }
}

impl<S> Controller<S>
where
    S: Sound + SetVolume,
{
    /// Set the volume of the controllable sound.
    pub fn set_volume(&mut self, volume: f32) {
        self.send_command(Box::new(move |s: &mut S| s.set_volume(volume)));
    }
}