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
use crate::Sound;

use super::{SetPaused, SetVolume};

/// A sound that can have the playback speed adjusted.
///
/// This is [pitch control](https://en.wikipedia.org/wiki/Pitch_control) affecting both the pitch and speed and not [time stretching](https://en.wikipedia.org/wiki/Audio_time_stretching_and_pitch_scaling) which would only affect playback speed.
pub trait SetSpeed {
    /// Change the playback speed.
    ///
    /// 1.0 is the normal playback speed. 2.0 would be twice as fast, 0.5 would
    /// be half has fast.
    fn set_speed(&mut self, multiplier: f32);
}

/// A wrapper that adjusts the speed of the inner sound.
pub struct AdjustableSpeed<S: Sound> {
    inner: S,
    speed_adjustment: f32,
    speed_changed: bool,
}

impl<S> AdjustableSpeed<S>
where
    S: Sound,
{
    /// Wrap `inner` such that its speed can be adjusted.
    pub fn new(inner: S) -> Self {
        Self::new_with_speed(inner, 1.0)
    }

    /// Wrap `inner` such that its speed can be adjusted and set an initial
    /// adjustment.
    pub fn new_with_speed(inner: S, speed_adjustment: f32) -> Self {
        AdjustableSpeed {
            inner,
            speed_adjustment,
            speed_changed: 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 AdjustableSpeed<S>
where
    S: Sound,
{
    fn channel_count(&self) -> u16 {
        self.inner.channel_count()
    }

    fn sample_rate(&self) -> u32 {
        let new_rate = (self.inner.sample_rate() as f32 * self.speed_adjustment).round() as u32;
        // Do not let the new rate be 0 which would cause issues
        u32::max(1, new_rate)
    }

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

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

impl<S> SetSpeed for AdjustableSpeed<S>
where
    S: Sound,
{
    fn set_speed(&mut self, new: f32) {
        self.speed_changed = true;
        self.speed_adjustment = new;
    }
}

impl<S> AdjustableSpeed<S>
where
    S: Sound,
{
    /// Return the current speed multiplier. 1.0 is the default speed.
    pub fn speed(&self) -> f32 {
        self.speed_adjustment
    }
}

impl<S> SetPaused for AdjustableSpeed<S>
where
    S: Sound + SetPaused,
{
    fn set_paused(&mut self, paused: bool) {
        self.inner.set_paused(paused)
    }
}

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

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