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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use crate::{converter, SampleRate, SoundId, SoundSource};
use std::{
    collections::HashMap,
    hash::Hash,
    sync::atomic::{AtomicU64, Ordering},
};

fn next_id() -> SoundId {
    static GLOBAL_COUNT: AtomicU64 = AtomicU64::new(0);
    GLOBAL_COUNT.fetch_add(1, Ordering::Relaxed)
}

struct SoundInner<G = ()> {
    id: SoundId,
    data: Box<dyn SoundSource + Send>,
    volume: f32,
    group: G,
    looping: bool,
    drop: bool,
}
impl<G> SoundInner<G> {
    fn new(group: G, data: Box<dyn SoundSource + Send>) -> Self {
        Self {
            id: next_id(),
            data,
            volume: 1.0,
            group,
            looping: false,
            drop: true,
        }
    }
}

/// Keep track of each Sound, and mix they output together.
pub struct Mixer<G: Eq + Hash + Send + 'static = ()> {
    sounds: Vec<SoundInner<G>>,
    playing: usize,
    channels: u16,
    sample_rate: SampleRate,
    group_volumes: HashMap<G, f32>,
}

impl<G: Eq + Hash + Send + 'static> Mixer<G> {
    /// Create a new Mixer.
    ///
    /// The created Mixer output samples with given sample rate and number of channels. This
    /// configuration can be changed by calling [`set_config`](Self::set_config).
    pub fn new(channels: u16, sample_rate: SampleRate) -> Self {
        Self {
            sounds: vec![],
            playing: 0,
            channels,
            sample_rate,
            group_volumes: HashMap::new(),
        }
    }

    /// Change the number of channels and the sample rate.
    ///
    /// This keep also keep all currently playing sounds, and convert them to the new config, if
    /// necessary.
    pub fn set_config(&mut self, channels: u16, sample_rate: SampleRate) {
        struct Nop;
        #[rustfmt::skip]
        impl SoundSource for Nop {
            fn channels(&self) -> u16 { 0 }
            fn sample_rate(&self) -> u32 { 0 }
            fn reset(&mut self) { }
            fn write_samples(&mut self, _: &mut [i16]) -> usize { 0 }
        }

        let not_chaged = self.channels == channels && self.sample_rate == sample_rate;
        if not_chaged {
            return;
        }
        if !self.sounds.is_empty() {
            for sound in self.sounds.iter_mut() {
                // FIXME: if the config change multiple times, this will nest multiple converts,
                // increasing processing and loosing quality.
                // Maybe I should create something like a tree of converters, and always keep the
                // convertes Concrete.
                if sound.data.channels() != channels {
                    let inner = std::mem::replace(&mut sound.data, Box::new(Nop));
                    sound.data = Box::new(converter::ChannelConverter::new(inner, channels));
                }
                if sound.data.sample_rate() != sample_rate.0 {
                    let inner = std::mem::replace(&mut sound.data, Box::new(Nop));
                    sound.data =
                        Box::new(converter::SampleRateConverter::new(inner, sample_rate.0));
                }
            }
        }
        self.channels = channels;
        self.sample_rate = sample_rate;
    }

    /// Add new sound to the Mixer.
    ///
    /// Return the SoundId associated with that sound. This Id is globally unique.
    ///
    /// The added sound is started in stopped state, and [`play`](Self::play) must be called to start playing
    /// it. [`mark_to_remove`](Self::mark_to_remove) is true by default.
    pub fn add_sound(&mut self, group: G, sound: Box<dyn SoundSource + Send>) -> SoundId {
        let sound_inner = SoundInner::new(group, sound);
        let id = sound_inner.id;
        self.sounds.push(sound_inner);
        id
    }

    /// Start playing the sound associated with the given id.
    ///
    /// If the sound was paused or stop, it will start playing again.
    /// Otherwise, does nothing.
    pub fn play(&mut self, id: SoundId) {
        for i in (self.playing..self.sounds.len()).rev() {
            if self.sounds[i].id == id {
                self.sounds.swap(self.playing, i);
                self.playing += 1;
                break;
            }
        }
    }

    /// Pause the sound associated with the given id.
    ///
    /// If the sound is playing, it will pause. If play is called,
    /// this sound will continue from where it was when paused.
    /// If the sound is not playing, does nothing.
    pub fn pause(&mut self, id: SoundId) {
        for i in (0..self.playing).rev() {
            if self.sounds[i].id == id {
                self.playing -= 1;
                self.sounds.swap(self.playing, i);
                break;
            }
        }
    }

    /// Stop the sound associated with the given id.
    ///
    /// If the sound is playing, it will pause and reset the song. When play is called,
    /// this sound will start from the begging.
    ///
    /// Even if the sound is not playing, it will reset the sound to the start. If the sound is
    /// [marked to be removed](Self::mark_to_remove), this sound will be removed from the Mixer.
    pub fn stop(&mut self, id: SoundId) {
        for i in (0..self.sounds.len()).rev() {
            if self.sounds[i].id == id {
                if self.sounds[i].drop {
                    self.sounds.swap_remove(i);
                } else {
                    self.sounds[i].data.reset();
                }
                if i < self.playing && self.playing < self.sounds.len() {
                    self.playing -= 1;
                    self.sounds.swap(self.playing, i);
                }
                break;
            }
        }
    }

    /// Reset the sound associated with the given id.
    ///
    /// This reset the sound to the start, the sound being playing or not.
    pub fn reset(&mut self, id: SoundId) {
        for i in (0..self.sounds.len()).rev() {
            if self.sounds[i].id == id {
                self.sounds[i].data.reset();
                break;
            }
        }
    }

    /// Set if the sound associated with the given id will loop.
    ///
    /// If true, ever time the sound reachs its end, it will reset, and continue to play in a loop.
    ///
    /// This also set [`mark_to_remove`](Self::mark_to_remove) to false.
    pub fn set_loop(&mut self, id: SoundId, looping: bool) {
        for i in (0..self.sounds.len()).rev() {
            if self.sounds[i].id == id {
                self.sounds[i].looping = looping;
                break;
            }
        }
    }

    /// Set the volume of the sound associated with the given id.
    ///
    /// The output samples of the SoundSource assicociated with the given id will be multiplied by
    /// this volume.
    pub fn set_volume(&mut self, id: SoundId, volume: f32) {
        for i in (0..self.sounds.len()).rev() {
            if self.sounds[i].id == id {
                self.sounds[i].volume = volume;
                break;
            }
        }
    }

    /// Set the volume of the given group.
    ///
    /// The volume of all sounds associated with this group is multiplied by this volume.
    pub fn set_group_volume(&mut self, group: G, volume: f32) {
        self.group_volumes.insert(group, volume);
    }

    /// Mark if the sound will be removed after it reachs its end.
    ///
    /// If false, it will be possible to reset the sound and play it again after it has already
    /// reached its end. Otherwise, the sound will be removed when it reachs its end, even if it is
    /// marked to loop.
    pub fn mark_to_remove(&mut self, id: SoundId, drop: bool) {
        for i in (0..self.sounds.len()).rev() {
            if self.sounds[i].id == id {
                self.sounds[i].drop = drop;
                break;
            }
        }
    }

    /// The number of sounds in the mixer.
    ///
    /// This include the sounds that are currently stopped.
    pub fn sound_count(&self) -> usize {
        self.sounds.len()
    }

    /// The number of sounds being played currently.
    ///
    /// Does not include the sounds that are currently stopped.
    pub fn playing_count(&self) -> usize {
        self.playing
    }
}

impl<G: Eq + Hash + Send + 'static> SoundSource for Mixer<G> {
    fn channels(&self) -> u16 {
        self.channels
    }

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

    fn reset(&mut self) {}

    fn write_samples(&mut self, buffer: &mut [i16]) -> usize {
        if self.playing == 0 {
            for b in buffer.iter_mut() {
                *b = 0;
            }
            return buffer.len();
        }

        let mut buf = vec![0; buffer.len()];
        let mut s = 0;
        while s < self.playing {
            let mut len = 0;
            loop {
                len += self.sounds[s].data.write_samples(&mut buf[len..]);
                if len < buffer.len() {
                    self.sounds[s].data.reset();
                    if self.sounds[s].looping {
                        continue;
                    }
                }
                break;
            }

            let group_volume = *self
                .group_volumes
                .get(&self.sounds[s].group)
                .unwrap_or(&1.0);
            let volume = self.sounds[s].volume * group_volume;

            if (volume - 1.0).abs() < 1.0 / i16::max_value() as f32 {
                for i in 0..len {
                    buffer[i] = buffer[i].saturating_add(buf[i]);
                }
            } else {
                for i in 0..len {
                    buffer[i] = buffer[i].saturating_add((buf[i] as f32 * volume) as i16);
                }
            }

            if len < buffer.len() {
                if self.sounds[s].drop {
                    let _ = self.sounds.swap_remove(s);
                }
                self.playing -= 1;
                if self.playing > 0 && self.playing < self.sounds.len() {
                    self.sounds.swap(s, self.playing);
                }
            } else {
                s += 1;
            }
        }

        buffer.len()
    }
}

#[cfg(test)]
mod test {
    use crate::SoundSource;

    use super::Mixer;

    struct DebugSource {
        i: usize,
        v: i16,
        len: usize,
    }
    impl DebugSource {
        fn new(v: i16, len: usize) -> Self {
            Self { i: 0, v, len }
        }
    }
    impl SoundSource for DebugSource {
        fn channels(&self) -> u16 {
            1
        }

        fn sample_rate(&self) -> u32 {
            1
        }

        fn reset(&mut self) {
            self.i = 0;
        }

        fn write_samples(&mut self, buffer: &mut [i16]) -> usize {
            for (i, o) in buffer.iter_mut().enumerate() {
                *o = self.v;
                self.i += 1;
                if self.i > self.len {
                    return i;
                }
            }
            buffer.len()
        }
    }

    #[test]
    fn start_stopped() {
        let mut mixer = Mixer::new(1, crate::SampleRate(1));

        // create new sound
        let id = mixer.add_sound((), Box::new(DebugSource::new(2, 5)));

        // sound start in stopped state
        let mut buffer = [0; 10];
        assert_eq!(mixer.write_samples(&mut buffer), 10);
        assert_eq!(buffer, [0; 10]);

        // play the sound
        mixer.play(id);
        assert_eq!(mixer.write_samples(&mut buffer), 10);
        assert_eq!(buffer, [2, 2, 2, 2, 2, 0, 0, 0, 0, 0]);

        // mark to remove is true, sound was removed
        mixer.reset(id);
        mixer.play(id);
        assert_eq!(mixer.write_samples(&mut buffer), 10);
        assert_eq!(buffer, [0; 10]);

        // create new sound
        let id = mixer.add_sound((), Box::new(DebugSource::new(2, 5)));

        mixer.stop(id);
    }

    #[test]
    fn mark_to_remove_true() {
        let mut mixer = Mixer::new(1, crate::SampleRate(1));

        // create new sound
        let id = mixer.add_sound((), Box::new(DebugSource::new(2, 5)));

        // stop the sound will remove the sound if marked to remove is true
        mixer.stop(id);

        // mark to remove is true, sound was removed
        mixer.reset(id);
        mixer.play(id);
        let mut buffer = [0; 10];
        assert_eq!(mixer.write_samples(&mut buffer), 10);
        assert_eq!(buffer, [0; 10]);
    }

    #[test]
    fn mark_to_remove_false() {
        let mut mixer = Mixer::new(1, crate::SampleRate(1));

        let id = mixer.add_sound((), Box::new(DebugSource::new(2, 5)));
        mixer.mark_to_remove(id, false);

        // stop the sound will not remove the sound if marked to remove is false
        mixer.stop(id);

        // sound start in stopped state
        let mut buffer = [0; 10];
        assert_eq!(mixer.write_samples(&mut buffer), 10);
        assert_eq!(buffer, [0; 10]);

        // play the sound
        mixer.play(id);
        assert_eq!(mixer.playing_count(), 1);
        assert_eq!(mixer.write_samples(&mut buffer), 10);
        assert_eq!(buffer, [2, 2, 2, 2, 2, 0, 0, 0, 0, 0]);

        // mark to remove is false
        mixer.play(id);
        buffer = [0; 10];
        assert_eq!(mixer.playing_count(), 1);
        assert_eq!(mixer.write_samples(&mut buffer), 10);
        assert_eq!(buffer, [2, 2, 2, 2, 2, 0, 0, 0, 0, 0]);
    }

    #[test]
    fn volume() {
        let mut mixer = Mixer::new(1, crate::SampleRate(1));

        let id0 = mixer.add_sound((), Box::new(DebugSource::new(10, 2)));
        let id1 = mixer.add_sound((), Box::new(DebugSource::new(10, 4)));
        let id2 = mixer.add_sound((), Box::new(DebugSource::new(10, 6)));

        mixer.set_volume(id0, 0.2);
        mixer.set_volume(id1, 0.4);
        mixer.set_volume(id2, 0.8);

        mixer.play(id0);
        mixer.play(id1);
        mixer.play(id2);

        let mut buffer = [0; 10];
        assert_eq!(mixer.write_samples(&mut buffer), 10);
        assert_eq!(buffer, [14, 14, 12, 12, 8, 8, 0, 0, 0, 0]);
    }

    #[test]
    fn group_volume() {
        #[derive(Eq, Hash, PartialEq)]
        enum Group {
            A,
            B,
        }

        let mut mixer = Mixer::new(1, crate::SampleRate(1));

        let a0 = mixer.add_sound(Group::A, Box::new(DebugSource::new(10, 2)));
        let a1 = mixer.add_sound(Group::A, Box::new(DebugSource::new(10, 4)));
        let a2 = mixer.add_sound(Group::A, Box::new(DebugSource::new(10, 6)));

        let b0 = mixer.add_sound(Group::B, Box::new(DebugSource::new(10, 8)));
        let b1 = mixer.add_sound(Group::B, Box::new(DebugSource::new(10, 10)));
        let b2 = mixer.add_sound(Group::B, Box::new(DebugSource::new(10, 12)));

        mixer.set_volume(a0, 0.2);
        mixer.set_volume(a1, 0.4);
        mixer.set_volume(a2, 0.8);

        mixer.set_volume(b0, 0.2);
        mixer.set_volume(b1, 0.4);
        mixer.set_volume(b2, 0.8);

        mixer.set_group_volume(Group::A, 2.0);
        mixer.set_group_volume(Group::B, 4.0);

        assert_eq!(mixer.sound_count(), 6);
        assert_eq!(mixer.playing_count(), 0);

        mixer.play(a0);
        mixer.play(a1);
        mixer.play(a2);

        mixer.play(b0);
        mixer.play(b1);
        mixer.play(b2);

        assert_eq!(mixer.sound_count(), 6);
        assert_eq!(mixer.playing_count(), 6);

        let mut buffer = [0; 10];
        assert_eq!(mixer.write_samples(&mut buffer), 10);
        // 28 + 56
        assert_eq!(buffer, [84, 84, 80, 80, 72, 72, 56, 56, 48, 48]);
        buffer = [0; 10];
        assert_eq!(mixer.write_samples(&mut buffer), 10);
        // 28 + 56
        assert_eq!(buffer, [32, 32, 0, 0, 0, 0, 0, 0, 0, 0]);

        assert_eq!(mixer.sound_count(), 0);
        assert_eq!(mixer.playing_count(), 0);
    }
}