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
//! Defines the basic types and traits for the crate.

use crate::SAMPLE_RATE;
use std::ops::*;

/// Rescales a value from `-1.0` to `1.0`, into a value from `0.0` to `1.0`.
pub fn to_pos(x: f64) -> f64 {
    (x + 1.0) / 2.0
}

/// Rescales a value from `0.0` to `1.0`, into a value from `-1.0` to `1.0`.
pub fn to_sgn(x: f64) -> f64 {
    2.0 * x - 1.0
}

/// Clamps a signal between `-1.0` and `1.0`.
pub fn clip(x: f64) -> f64 {
    x.clamp(-1.0, 1.0)
}

/// A wrapper for a Rust function which converts it into a [`Map`] or
/// [`MapMut`].
///
/// It may be necessary to explicitly write down the types of the arguments to
/// the function.
#[derive(Clone, Copy, Debug)]
pub struct FnWrapper<F>(pub F);

/// An abstract trait for a structure representing a function `X → Y`.
///
/// Due to orphan rules, this trait can't be implemented for Rust functions. In
/// order to use it in this case, wrap your function in [`FnWrapper`].
pub trait Map<X, Y> {
    /// Evaluates the function.
    fn eval(&self, x: X) -> Y;
}

impl<X, Y, F: Fn(X) -> Y> Map<X, Y> for FnWrapper<F> {
    fn eval(&self, x: X) -> Y {
        self.0(x)
    }
}

/// An abstract trait for a structure representing a function taking `&mut X`
/// and `Y`.
///
/// Due to orphan rules, this trait can't be implemented for Rust functions. In
/// order to use it in this case, wrap your function in [`FnWrapper`].
pub trait MapMut<X, Y> {
    fn modify(&mut self, x: &mut X, y: Y);
}

impl<X, Y, F: FnMut(&mut X, Y)> MapMut<X, Y> for FnWrapper<F> {
    fn modify(&mut self, x: &mut X, y: Y) {
        self.0(x, y);
    }
}

/// Represents the gain of some signal.
#[derive(Clone, Copy, Debug)]
pub struct Vol {
    /// Gain factor.
    pub gain: f64,
}

impl Vol {
    /// Initializes a new volume variable.
    pub const fn new(gain: f64) -> Self {
        Self { gain }
    }

    /// Gain measured in decibels.
    pub fn new_db(db: f64) -> Self {
        Self::new(10f64.powf(db / 20.0))
    }

    /// The gain in decibels.
    pub fn db(&self) -> f64 {
        20.0 * self.gain.log10()
    }

    /// Silence.
    pub const fn zero() -> Self {
        Self::new(0.0)
    }
}

impl Default for Vol {
    fn default() -> Self {
        Self::new(1.0)
    }
}

impl Map<f64, f64> for Vol {
    fn eval(&self, x: f64) -> f64 {
        x * self.gain
    }
}

/// Represents a frequency.
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Freq {
    /// The frequency in hertz.
    pub hz: f64,
}

/// Converts a letter to a numeric note.
fn letter_to_note(letter: char) -> i16 {
    match letter {
        'C' => 0,
        'D' => 2,
        'E' => 4,
        'F' => 5,
        'G' => 7,
        'A' => 9,
        'B' => 11,
        _ => panic!("Invalid letter"),
    }
}

impl Freq {
    /// Initializes a given frequency.
    pub const fn new(hz: f64) -> Self {
        Self { hz }
    }

    /// The frequency in Hertz.
    pub const fn hz(&self) -> f64 {
        self.hz
    }

    /// The period, which equals the reciprocal of the frequency.
    pub fn period(&self) -> Time {
        Time::new(1.0 / self.hz())
    }

    /// Initializes a frequency in a given `edo` (equal division of the octave)
    /// a certain amount of `note`s above or below a `base` pitch (usually
    /// [`A4`](crate::A4)).
    pub fn new_edo(base: Freq, edo: u16, note: i16) -> Self {
        2f64.powf(note as f64 / edo as f64) * base
    }

    /// Initializes a frequency from a MIDI note.
    pub fn new_midi(note: i16) -> Self {
        Self::new_edo(crate::A4, 12, note - 69)
    }

    /// Initializes a pitch from its name (e.g. `"A4"` or `"G#5"`).
    pub fn new_name(name: &str) -> Self {
        let mut chars = name.chars();
        let mut note = letter_to_note(chars.next().unwrap());

        let idx = match chars.next().unwrap() {
            '#' => {
                note += 1;
                2
            }
            'b' => {
                note -= 1;
                2
            }
            _ => 1,
        };

        note += 12 * (name[idx..].parse::<i16>().unwrap() + 1);
        Self::new_midi(note)
    }
}

impl From<Freq> for Time {
    fn from(value: Freq) -> Self {
        value.period()
    }
}

impl Mul<f64> for Freq {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self::Output {
        rhs * self
    }
}

impl MulAssign<f64> for Freq {
    fn mul_assign(&mut self, rhs: f64) {
        self.hz *= rhs;
    }
}

impl Div<f64> for Freq {
    type Output = Self;

    fn div(self, rhs: f64) -> Self {
        Self::new(self.hz / rhs)
    }
}

impl DivAssign<f64> for Freq {
    fn div_assign(&mut self, rhs: f64) {
        self.hz /= rhs;
    }
}

/// Represents an amount of time.
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Time {
    /// Number of seconds.
    pub seconds: f64,
}

impl Time {
    /// Initializes a time variable for the number of seconds.
    pub const fn new(seconds: f64) -> Self {
        Self { seconds }
    }

    /// Initializes a time variable for the number of frames.
    pub fn new_frames(frames: f64) -> Self {
        Self::new(frames / SAMPLE_RATE as f64)
    }

    /// The time for a single beat at a given BPM.
    pub fn new_beat(bpm: f64) -> Self {
        Self::new(60.0 / bpm)
    }

    /// Time to frequency.
    pub fn freq(&self) -> Freq {
        Freq::new(1.0 / self.seconds())
    }

    /// Zero seconds.
    pub const fn zero() -> Self {
        Self::new(0.0)
    }

    /// The time in seconds.
    pub const fn seconds(&self) -> f64 {
        self.seconds
    }

    /// The time in frames.
    pub fn frames(&self) -> f64 {
        self.seconds() * SAMPLE_RATE as f64
    }

    /// Advances the time by one frame.
    pub fn advance(&mut self) {
        self.seconds += 1.0 / SAMPLE_RATE as f64;
    }
}

impl From<Time> for Freq {
    fn from(value: Time) -> Self {
        value.freq()
    }
}

impl Mul<Freq> for f64 {
    type Output = Freq;

    fn mul(self, rhs: Freq) -> Freq {
        Freq::new(self * rhs.hz)
    }
}

impl Add for Time {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Self::new(self.seconds + rhs.seconds)
    }
}

impl AddAssign for Time {
    fn add_assign(&mut self, rhs: Self) {
        self.seconds += rhs.seconds;
    }
}

impl Sub for Time {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        Self::new(self.seconds - rhs.seconds)
    }
}

impl SubAssign for Time {
    fn sub_assign(&mut self, rhs: Self) {
        self.seconds -= rhs.seconds;
    }
}

impl Mul<Time> for f64 {
    type Output = Time;

    fn mul(self, rhs: Time) -> Time {
        Time::new(self * rhs.seconds)
    }
}

impl Mul<f64> for Time {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self::Output {
        rhs * self
    }
}

impl MulAssign<f64> for Time {
    fn mul_assign(&mut self, rhs: f64) {
        self.seconds *= rhs;
    }
}

impl Div<f64> for Time {
    type Output = Self;

    fn div(self, rhs: f64) -> Self {
        Self::new(self.seconds / rhs)
    }
}

impl DivAssign<f64> for Time {
    fn div_assign(&mut self, rhs: f64) {
        self.seconds /= rhs;
    }
}

impl Rem for Time {
    type Output = Self;

    fn rem(self, rhs: Self) -> Self::Output {
        Self::new(self.seconds % rhs.seconds)
    }
}

impl RemAssign for Time {
    fn rem_assign(&mut self, rhs: Self) {
        self.seconds %= rhs.seconds;
    }
}