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
use std::{fmt, mem::transmute, ops::*};

/// Mono [`Frame`] type
pub type Mono = f64;

/// Convert a [`Frame`] to mono
pub fn mono(frame: impl AsRef<[f64]>) -> Mono {
    frame.as_ref().iter().sum::<f64>() / frame.as_ref().len() as f64
}

/// Convert a mono [`Frame`] to stereo
pub fn stereo(frame: Mono) -> Stereo {
    Stereo::both(frame)
}

/// A single multi-channel frame in an audio source
pub trait Frame: Clone + Send + 'static {
    /// The number of audio channels
    const CHANNELS: usize;
    /// Create a frame with a uniform amplitude across all channels
    fn uniform(amplitude: f64) -> Self;
    /// Get the amplitude of a channel
    fn get_channel(&self, index: usize) -> f64;
    /// Set the amplitude of a channel
    fn set_channel(&mut self, index: usize, amplitude: f64);
    /// Apply a function to each channel
    fn map(self, f: impl Fn(f64) -> f64) -> Self;
    /// Combine two frames by applying a function
    fn merge(&mut self, other: Self, f: impl Fn(f64, f64) -> f64);
    /// Get the average amplitude
    fn avg(&self) -> f64 {
        (0..Self::CHANNELS)
            .map(|i| self.get_channel(i))
            .sum::<f64>()
            / Self::CHANNELS as f64
    }
    /// Add two frames
    fn add(mut self, other: Self) -> Self {
        self.merge(other, Add::add);
        self
    }
    /// Write the frame to a channel slice
    ///
    /// The channel counts of the frame and slice need not match.
    fn write_slice(self, slice: &mut [f64]) {
        match (Self::CHANNELS, slice.len()) {
            (1, _) => slice.fill(self.get_channel(0)),
            (_, 1) => slice[0] = self.avg(),
            (a, b) => {
                for i in 0..a.min(b) {
                    slice[i] = self.get_channel(i);
                }
            }
        }
    }
}

impl Frame for f64 {
    const CHANNELS: usize = 1;
    fn uniform(amplitude: f64) -> Self {
        amplitude
    }
    fn get_channel(&self, _index: usize) -> f64 {
        *self
    }
    fn set_channel(&mut self, _index: usize, amplitude: f64) {
        *self = amplitude;
    }
    fn map(self, f: impl Fn(f64) -> f64) -> Self {
        f(self)
    }
    fn merge(&mut self, other: Self, f: impl Fn(f64, f64) -> f64) {
        *self = f(*self, other);
    }
    fn avg(&self) -> f64 {
        *self
    }
    fn add(self, other: Self) -> Self {
        self + other
    }
}

impl Frame for f32 {
    const CHANNELS: usize = 1;
    fn uniform(amplitude: f64) -> Self {
        amplitude as f32
    }
    fn get_channel(&self, _index: usize) -> f64 {
        *self as f64
    }
    fn set_channel(&mut self, _index: usize, amplitude: f64) {
        *self = amplitude as f32;
    }
    fn map(self, f: impl Fn(f64) -> f64) -> Self {
        f(self as f64) as f32
    }
    fn merge(&mut self, other: Self, f: impl Fn(f64, f64) -> f64) {
        *self = f(*self as f64, other as f64) as f32;
    }
    fn avg(&self) -> f64 {
        *self as f64
    }
    fn add(self, other: Self) -> Self {
        self + other
    }
}

impl<const N: usize> Frame for [f64; N]
where
    Self: Default,
{
    const CHANNELS: usize = N;
    fn uniform(amplitude: f64) -> Self {
        [amplitude; N]
    }
    fn get_channel(&self, index: usize) -> f64 {
        self[index]
    }
    fn set_channel(&mut self, index: usize, amplitude: f64) {
        self[index] = amplitude;
    }
    fn map(self, f: impl Fn(f64) -> f64) -> Self {
        self.map(f)
    }
    fn merge(&mut self, other: Self, f: impl Fn(f64, f64) -> f64) {
        for (a, b) in self.iter_mut().zip(other) {
            *a = f(*a, b);
        }
    }
}

impl<const N: usize> Frame for [f32; N]
where
    Self: Default,
{
    const CHANNELS: usize = N;
    fn uniform(amplitude: f64) -> Self {
        [amplitude as f32; N]
    }
    fn get_channel(&self, index: usize) -> f64 {
        self[index] as f64
    }
    fn set_channel(&mut self, index: usize, amplitude: f64) {
        self[index] = amplitude as f32;
    }
    fn map(self, f: impl Fn(f64) -> f64) -> Self {
        self.map(|v| f(v as f64) as f32)
    }
    fn merge(&mut self, other: Self, f: impl Fn(f64, f64) -> f64) {
        for (a, b) in self.iter_mut().zip(other) {
            *a = f(*a as f64, b as f64) as f32;
        }
    }
}

/// Stereo [`Frame`] type
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Stereo<T = f64> {
    /// The left channel
    pub left: T,
    /// The right channel
    pub right: T,
}

impl<T> Stereo<T> {
    #[inline]
    /// Create a new stereo frame
    pub const fn new(left: T, right: T) -> Self {
        Self { left, right }
    }
    /// Map the channels of the frame
    pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Stereo<U> {
        Stereo::new(f(self.left), f(self.right))
    }
    /// Combine two frames by applying a function
    pub fn with<U, V>(self, other: Stereo<U>, mut f: impl FnMut(T, U) -> V) -> Stereo<V> {
        Stereo::new(f(self.left, other.left), f(self.right, other.right))
    }
    /// Combine two frames
    pub fn zip<U>(self, other: Stereo<U>) -> Stereo<(T, U)> {
        self.with(other, |a, b| (a, b))
    }
    /// Call a function on the channels of the frame
    pub fn reduce(self, mut f: impl FnMut(T, T) -> T) -> T {
        f(self.left, self.right)
    }
}

impl<T: fmt::Display + PartialEq + Default> fmt::Display for Stereo<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.left == self.right {
            write!(f, "{}", self.left)
        } else {
            write!(f, "({} {})", self.left, self.right)
        }
    }
}

impl<T: Clone> Stereo<T> {
    #[inline]
    /// Create a new stereo frame with the same value for both channels
    pub fn both(v: T) -> Self {
        Self {
            left: v.clone(),
            right: v,
        }
    }
}

impl<T: Clone> From<T> for Stereo<T> {
    fn from(v: T) -> Self {
        Self::both(v)
    }
}

impl Stereo {
    /// `[0.0, 0.0]`
    pub const ZERO: Self = Self::new(0.0, 0.0);
    /// `[1.0, 0.0]`
    pub const LEFT: Self = Self::new(1.0, 0.0);
    /// `[0.0, 1.0]`
    pub const RIGHT: Self = Self::new(0.0, 1.0);
    /// Create a new stereo frame with a panned value
    pub fn pan(value: f64, pan: f64) -> Self {
        let left = value * (-pan + 1.0).min(1.0);
        let right = value * (pan + 1.0).min(1.0);
        Self::new(left, right)
    }
    /// Get the average of the channels
    pub fn average(self) -> f64 {
        (self.left + self.right) / 2.0
    }
}

macro_rules! bin_op {
    ($trait:ident, $method:ident, $assign_trait:ident, $assign_method:ident) => {
        impl $trait for Stereo {
            type Output = Self;
            fn $method(mut self, other: Self) -> Self {
                self.$assign_method(other);
                self
            }
        }

        impl $assign_trait for Stereo {
            fn $assign_method(&mut self, other: Self) {
                self.left.$assign_method(other.left);
                self.right.$assign_method(other.right);
            }
        }

        impl $trait<f64> for Stereo {
            type Output = Self;
            fn $method(mut self, other: f64) -> Self {
                self.$assign_method(Self::both(other));
                self
            }
        }

        impl $trait<Stereo> for f64 {
            type Output = Stereo;
            fn $method(self, other: Stereo) -> Stereo {
                other.map(|v| $trait::$method(self, v))
            }
        }

        impl $assign_trait<f64> for Stereo {
            fn $assign_method(&mut self, other: f64) {
                self.$assign_method(Self::both(other));
            }
        }
    };
}

bin_op!(Add, add, AddAssign, add_assign);
bin_op!(Sub, sub, SubAssign, sub_assign);
bin_op!(Mul, mul, MulAssign, mul_assign);
bin_op!(Div, div, DivAssign, div_assign);
bin_op!(Rem, rem, RemAssign, rem_assign);

impl Neg for Stereo {
    type Output = Self;
    fn neg(self) -> Self {
        Self::new(-self.left, -self.right)
    }
}

impl Frame for Stereo {
    const CHANNELS: usize = 2;
    fn uniform(amplitude: f64) -> Self {
        Self::both(amplitude)
    }
    fn get_channel(&self, index: usize) -> f64 {
        unsafe { transmute::<&Self, &[f64; 2]>(self)[index] }
    }
    fn set_channel(&mut self, index: usize, amplitude: f64) {
        unsafe { transmute::<&mut Self, &mut [f64; 2]>(self)[index] = amplitude }
    }
    fn map(self, f: impl Fn(f64) -> f64) -> Self {
        Self::map(self, f)
    }
    fn merge(&mut self, other: Self, f: impl Fn(f64, f64) -> f64) {
        *self = self.with(other, f);
    }
}

impl Frame for Stereo<f32> {
    const CHANNELS: usize = 2;
    fn uniform(amplitude: f64) -> Self {
        Self::both(amplitude as f32)
    }
    fn get_channel(&self, index: usize) -> f64 {
        unsafe { transmute::<&Self, &[f32; 2]>(self)[index] as f64 }
    }
    fn set_channel(&mut self, index: usize, amplitude: f64) {
        unsafe { transmute::<&mut Self, &mut [f32; 2]>(self)[index] = amplitude as f32 }
    }
    fn map(self, f: impl Fn(f64) -> f64) -> Self {
        Self::map(self, |v| f(v as f64) as f32)
    }
    fn merge(&mut self, other: Self, f: impl Fn(f64, f64) -> f64) {
        *self = self.with(other, |a, b| f(a as f64, b as f64) as f32);
    }
}