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
use crate::SAMPLE_RATE;
use std::ops::*;
pub fn to_pos(x: f64) -> f64 {
(x + 1.0) / 2.0
}
pub fn to_sgn(x: f64) -> f64 {
2.0 * x - 1.0
}
pub fn clip(x: f64) -> f64 {
x.clamp(-1.0, 1.0)
}
#[derive(Clone, Copy, Debug)]
pub struct FnWrapper<F>(pub F);
pub trait Map<X, Y> {
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)
}
}
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);
}
}
#[derive(Clone, Copy, Debug)]
pub struct Vol {
pub gain: f64,
}
impl Vol {
pub const fn new(gain: f64) -> Self {
Self { gain }
}
pub fn new_db(db: f64) -> Self {
Self::new(10f64.powf(db / 20.0))
}
pub fn db(&self) -> f64 {
20.0 * self.gain.log10()
}
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
}
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Freq {
pub hz: f64,
}
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 {
pub const fn new(hz: f64) -> Self {
Self { hz }
}
pub const fn hz(&self) -> f64 {
self.hz
}
pub fn period(&self) -> Time {
Time::new(1.0 / self.hz())
}
pub fn new_edo(base: Freq, edo: u16, note: i16) -> Self {
2f64.powf(note as f64 / edo as f64) * base
}
pub fn new_midi(note: i16) -> Self {
Self::new_edo(crate::A4, 12, note - 69)
}
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;
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Time {
pub seconds: f64,
}
impl Time {
pub const fn new(seconds: f64) -> Self {
Self { seconds }
}
pub fn new_frames(frames: f64) -> Self {
Self::new(frames / SAMPLE_RATE as f64)
}
pub fn new_beat(bpm: f64) -> Self {
Self::new(60.0 / bpm)
}
pub fn freq(&self) -> Freq {
Freq::new(1.0 / self.seconds())
}
pub const fn zero() -> Self {
Self::new(0.0)
}
pub const fn seconds(&self) -> f64 {
self.seconds
}
pub fn frames(&self) -> f64 {
self.seconds() * SAMPLE_RATE as f64
}
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;
}
}