meadow-dsp-mit 0.1.0

Liberally-licensed DSP library used in the Meadowlark DAW project
Documentation
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
//! An implementation of Andrew Simper's SVF (state variable filter) model (f64 version).
//! https://cytomic.com/files/dsp/SvfLinearTrapOptimised2.pdf

use std::f64::consts::PI;

use super::f32::SvfCoeff as SvfCoeffF32;

pub const Q_BUTTERWORTH_ORD2: f64 = 0.70710678118654752440;
pub const Q_BUTTERWORTH_ORD4: [f64; 2] = [0.54119610014619698440, 1.3065629648763765279];
pub const Q_BUTTERWORTH_ORD6: [f64; 3] = [
    0.51763809020504152470,
    0.70710678118654752440,
    1.9318516525781365735,
];
pub const Q_BUTTERWORTH_ORD8: [f64; 4] = [
    0.50979557910415916894,
    0.60134488693504528054,
    0.89997622313641570464,
    2.5629154477415061788,
];

pub const ORD4_Q_SCALE: f64 = 0.35;
pub const ORD6_Q_SCALE: f64 = 0.2;
pub const ORD8_Q_SCALE: f64 = 0.14;

/// The coefficients for an SVF (state variable filter) model.
#[derive(Default, Clone, Copy)]
pub struct SvfCoeff {
    pub a1: f64,
    pub a2: f64,
    pub a3: f64,

    pub m0: f64,
    pub m1: f64,
    pub m2: f64,
}

impl SvfCoeff {
    pub const NO_OP: Self = Self {
        a1: 0.0,
        a2: 0.0,
        a3: 0.0,
        m0: 1.0,
        m1: 0.0,
        m2: 0.0,
    };

    pub fn lowpass_ord2(cutoff_hz: f64, q: f64, sample_rate_recip: f64) -> Self {
        let g = g(cutoff_hz, sample_rate_recip);
        let k = 1.0 / q;

        Self::from_g_and_k(g, k, 0.0, 0.0, 1.0)
    }

    pub fn lowpass_ord4(cutoff_hz: f64, q: f64, sample_rate_recip: f64) -> [Self; 2] {
        let g = g(cutoff_hz, sample_rate_recip);
        let q_norm = scale_q_norm_for_order(q_norm(q), ORD4_Q_SCALE);

        std::array::from_fn(|i| {
            let q = q_norm * Q_BUTTERWORTH_ORD4[i];
            let k = 1.0 / q;

            Self::from_g_and_k(g, k, 0.0, 0.0, 1.0)
        })
    }

    pub fn lowpass_ord6(cutoff_hz: f64, q: f64, sample_rate_recip: f64) -> [Self; 3] {
        let g = g(cutoff_hz, sample_rate_recip);
        let q_norm = scale_q_norm_for_order(q_norm(q), ORD6_Q_SCALE);

        std::array::from_fn(|i| {
            let q = q_norm * Q_BUTTERWORTH_ORD6[i];
            let k = 1.0 / q;

            Self::from_g_and_k(g, k, 0.0, 0.0, 1.0)
        })
    }

    pub fn lowpass_ord8(cutoff_hz: f64, q: f64, sample_rate_recip: f64) -> [Self; 4] {
        let g = g(cutoff_hz, sample_rate_recip);
        let q_norm = scale_q_norm_for_order(q_norm(q), ORD8_Q_SCALE);

        std::array::from_fn(|i| {
            let q = q_norm * Q_BUTTERWORTH_ORD8[i];
            let k = 1.0 / q;

            Self::from_g_and_k(g, k, 0.0, 0.0, 1.0)
        })
    }

    pub fn highpass_ord2(cutoff_hz: f64, q: f64, sample_rate_recip: f64) -> Self {
        let g = g(cutoff_hz, sample_rate_recip);
        let k = 1.0 / q;

        Self::from_g_and_k(g, k, 1.0, -k, -1.0)
    }

    pub fn highpass_ord4(cutoff_hz: f64, q: f64, sample_rate_recip: f64) -> [Self; 2] {
        let g = g(cutoff_hz, sample_rate_recip);
        let q_norm = scale_q_norm_for_order(q_norm(q), ORD4_Q_SCALE);

        std::array::from_fn(|i| {
            let q = q_norm * Q_BUTTERWORTH_ORD4[i];
            let k = 1.0 / q;

            Self::from_g_and_k(g, k, 1.0, -k, -1.0)
        })
    }

    pub fn highpass_ord6(cutoff_hz: f64, q: f64, sample_rate_recip: f64) -> [Self; 3] {
        let g = g(cutoff_hz, sample_rate_recip);
        let q_norm = scale_q_norm_for_order(q_norm(q), ORD6_Q_SCALE);

        std::array::from_fn(|i| {
            let q = q_norm * Q_BUTTERWORTH_ORD6[i];
            let k = 1.0 / q;

            Self::from_g_and_k(g, k, 1.0, -k, -1.0)
        })
    }

    pub fn highpass_ord8(cutoff_hz: f64, q: f64, sample_rate_recip: f64) -> [Self; 4] {
        let g = g(cutoff_hz, sample_rate_recip);
        let q_norm = scale_q_norm_for_order(q_norm(q), ORD8_Q_SCALE);

        std::array::from_fn(|i| {
            let q = q_norm * Q_BUTTERWORTH_ORD8[i];
            let k = 1.0 / q;

            Self::from_g_and_k(g, k, 1.0, -k, -1.0)
        })
    }

    pub fn notch(cutoff_hz: f64, q: f64, sample_rate_recip: f64) -> Self {
        let g = g(cutoff_hz, sample_rate_recip);
        let k = 1.0 / q;

        Self::from_g_and_k(g, k, 1.0, -k, 0.0)
    }

    pub fn bell(cutoff_hz: f64, q: f64, gain_db: f64, sample_rate_recip: f64) -> Self {
        let a = gain_db_to_a(gain_db);

        let g = g(cutoff_hz, sample_rate_recip);
        let k = 1.0 / (q * a);

        Self::from_g_and_k(g, k, 1.0, k * (a * a - 1.0), 0.0)
    }

    pub fn low_shelf(cutoff_hz: f64, q: f64, gain_db: f64, sample_rate_recip: f64) -> Self {
        let a = gain_db_to_a(gain_db);

        let g = (PI * cutoff_hz * sample_rate_recip).tan() / a.sqrt();
        let k = 1.0 / q;

        Self::from_g_and_k(g, k, 1.0, k * (a - 1.0), a * a - 1.0)
    }

    pub fn high_shelf(cutoff_hz: f64, q: f64, gain_db: f64, sample_rate_recip: f64) -> Self {
        let a = gain_db_to_a(gain_db);

        let g = (PI * cutoff_hz * sample_rate_recip).tan() / a.sqrt();
        let k = 1.0 / q;

        Self::from_g_and_k(g, k, a * a, k * (1.0 - a) * a, 1.0 - a * a)
    }

    pub fn allpass(cutoff_hz: f64, q: f64, sample_rate_recip: f64) -> Self {
        let g = g(cutoff_hz, sample_rate_recip);
        let k = 1.0 / q;

        Self::from_g_and_k(g, k, 1.0, -2.0 * k, 0.0)
    }

    pub fn from_g_and_k(g: f64, k: f64, m0: f64, m1: f64, m2: f64) -> Self {
        let a1 = 1.0 / (1.0 + g * (g + k));
        let a2 = g * a1;
        let a3 = g * a2;

        Self {
            a1,
            a2,
            a3,
            m0,
            m1,
            m2,
        }
    }

    pub fn to_f32(self) -> SvfCoeffF32 {
        SvfCoeffF32 {
            a1: self.a1 as f32,
            a2: self.a2 as f32,
            a3: self.a3 as f32,
            m0: self.m0 as f32,
            m1: self.m1 as f32,
            m2: self.m2 as f32,
        }
    }
}

/// The state of an SVF (state variable filter) model.
#[derive(Default, Clone, Copy)]
pub struct SvfState {
    pub ic1eq: f64,
    pub ic2eq: f64,
}

impl SvfState {
    #[inline(always)]
    pub fn tick(&mut self, input: f64, coeff: &SvfCoeff) -> f64 {
        let v3 = input - self.ic2eq;
        let v1 = coeff.a1 * self.ic1eq + coeff.a2 * v3;
        let v2 = self.ic2eq + coeff.a2 * self.ic1eq + coeff.a3 * v3;
        self.ic1eq = 2.0 * v1 - self.ic1eq;
        self.ic2eq = 2.0 * v2 - self.ic2eq;

        coeff.m0 * input + coeff.m1 * v1 + coeff.m2 * v2
    }

    #[inline(always)]
    pub fn reset(&mut self) {
        self.ic1eq = 0.0;
        self.ic2eq = 0.0;
    }
}

fn g(cutoff_hz: f64, sample_rate_recip: f64) -> f64 {
    (PI * cutoff_hz * sample_rate_recip).tan()
}

fn q_norm(q: f64) -> f64 {
    q * (1.0 / Q_BUTTERWORTH_ORD2)
}

fn gain_db_to_a(gain_db: f64) -> f64 {
    10.0f64.powf(gain_db * (1.0 / 40.0))
}

fn scale_q_norm_for_order(q_norm: f64, scale: f64) -> f64 {
    if q_norm > 1.0 {
        1.0 + ((q_norm - 1.0) * scale)
    } else {
        q_norm
    }
}

#[cfg(feature = "portable-simd")]
pub mod simd {
    use std::{
        array,
        simd::{f64x2, f64x4},
    };

    use super::{SvfCoeff, SvfState};

    /// The coefficients of two SVF (state variable filter) models packed
    /// into an SIMD vector.
    pub struct SvfCoeffx2 {
        pub a1: f64x2,
        pub a2: f64x2,
        pub a3: f64x2,

        pub m0: f64x2,
        pub m1: f64x2,
        pub m2: f64x2,
    }

    impl SvfCoeffx2 {
        pub const fn splat(coeffs: SvfCoeff) -> Self {
            Self {
                a1: f64x2::splat(coeffs.a1),
                a2: f64x2::splat(coeffs.a2),
                a3: f64x2::splat(coeffs.a3),
                m0: f64x2::splat(coeffs.m0),
                m1: f64x2::splat(coeffs.m1),
                m2: f64x2::splat(coeffs.m2),
            }
        }

        pub fn load(coeffs: &[SvfCoeff; 2]) -> Self {
            Self {
                a1: f64x2::from_array(array::from_fn(|i| coeffs[i].a1)),
                a2: f64x2::from_array(array::from_fn(|i| coeffs[i].a2)),
                a3: f64x2::from_array(array::from_fn(|i| coeffs[i].a3)),
                m0: f64x2::from_array(array::from_fn(|i| coeffs[i].m0)),
                m1: f64x2::from_array(array::from_fn(|i| coeffs[i].m1)),
                m2: f64x2::from_array(array::from_fn(|i| coeffs[i].m2)),
            }
        }
    }

    /// The coefficients of four SVF (state variable filter) models packed
    /// into an SIMD vector.
    pub struct SvfCoeffx4 {
        pub a1: f64x4,
        pub a2: f64x4,
        pub a3: f64x4,

        pub m0: f64x4,
        pub m1: f64x4,
        pub m2: f64x4,
    }

    impl SvfCoeffx4 {
        pub const fn splat(coeffs: SvfCoeff) -> Self {
            Self {
                a1: f64x4::splat(coeffs.a1),
                a2: f64x4::splat(coeffs.a2),
                a3: f64x4::splat(coeffs.a3),
                m0: f64x4::splat(coeffs.m0),
                m1: f64x4::splat(coeffs.m1),
                m2: f64x4::splat(coeffs.m2),
            }
        }

        pub fn load(coeffs: &[SvfCoeff; 4]) -> Self {
            Self {
                a1: f64x4::from_array(array::from_fn(|i| coeffs[i].a1)),
                a2: f64x4::from_array(array::from_fn(|i| coeffs[i].a2)),
                a3: f64x4::from_array(array::from_fn(|i| coeffs[i].a3)),
                m0: f64x4::from_array(array::from_fn(|i| coeffs[i].m0)),
                m1: f64x4::from_array(array::from_fn(|i| coeffs[i].m1)),
                m2: f64x4::from_array(array::from_fn(|i| coeffs[i].m2)),
            }
        }
    }

    /// The state of two SVF (state variable filter) models packed into an
    /// SIMD vector.
    #[derive(Default, Clone, Copy)]
    pub struct SvfStatex2 {
        pub ic1eq: f64x2,
        pub ic2eq: f64x2,
    }

    impl SvfStatex2 {
        pub const fn splat(state: SvfState) -> Self {
            Self {
                ic1eq: f64x2::splat(state.ic1eq),
                ic2eq: f64x2::splat(state.ic2eq),
            }
        }

        pub fn load(&mut self, states: &[SvfState; 2]) -> Self {
            Self {
                ic1eq: f64x2::from_array(array::from_fn(|i| states[i].ic1eq)),
                ic2eq: f64x2::from_array(array::from_fn(|i| states[i].ic2eq)),
            }
        }

        pub fn store(&self, states: &mut [SvfState; 2]) {
            let ic1eq = self.ic1eq.to_array();
            let ic2eq = self.ic2eq.to_array();

            for (i, s) in states.iter_mut().enumerate() {
                s.ic1eq = ic1eq[i];
                s.ic2eq = ic2eq[i];
            }
        }

        #[inline(always)]
        pub fn tick(&mut self, input: f64x2, coeff: &SvfCoeffx2) -> f64x2 {
            const V_2: f64x2 = f64x2::from_array([2.0; 2]);

            let v3 = input - self.ic2eq;
            let v1 = coeff.a1 * self.ic1eq + coeff.a2 * v3;
            let v2 = self.ic2eq + coeff.a2 * self.ic1eq + coeff.a3 * v3;
            self.ic1eq = V_2 * v1 - self.ic1eq;
            self.ic2eq = V_2 * v2 - self.ic2eq;

            coeff.m0 * input + coeff.m1 * v1 + coeff.m2 * v2
        }

        #[inline(always)]
        pub fn reset(&mut self) {
            self.ic1eq = f64x2::splat(0.0);
            self.ic2eq = f64x2::splat(0.0);
        }
    }

    /// The state of four SVF (state variable filter) models packed into an
    /// SIMD vector.
    #[derive(Default, Clone, Copy)]
    pub struct SvfStatex4 {
        pub ic1eq: f64x4,
        pub ic2eq: f64x4,
    }

    impl SvfStatex4 {
        pub const fn splat(state: SvfState) -> Self {
            Self {
                ic1eq: f64x4::splat(state.ic1eq),
                ic2eq: f64x4::splat(state.ic2eq),
            }
        }

        pub fn load(&mut self, states: &[SvfState; 4]) -> Self {
            Self {
                ic1eq: f64x4::from_array(array::from_fn(|i| states[i].ic1eq)),
                ic2eq: f64x4::from_array(array::from_fn(|i| states[i].ic2eq)),
            }
        }

        pub fn store(&self, states: &mut [SvfState; 4]) {
            let ic1eq = self.ic1eq.to_array();
            let ic2eq = self.ic2eq.to_array();

            for (i, s) in states.iter_mut().enumerate() {
                s.ic1eq = ic1eq[i];
                s.ic2eq = ic2eq[i];
            }
        }

        #[inline(always)]
        pub fn tick(&mut self, input: f64x4, coeff: &SvfCoeffx4) -> f64x4 {
            const V_2: f64x4 = f64x4::from_array([2.0; 4]);

            let v3 = input - self.ic2eq;
            let v1 = coeff.a1 * self.ic1eq + coeff.a2 * v3;
            let v2 = self.ic2eq + coeff.a2 * self.ic1eq + coeff.a3 * v3;
            self.ic1eq = V_2 * v1 - self.ic1eq;
            self.ic2eq = V_2 * v2 - self.ic2eq;

            coeff.m0 * input + coeff.m1 * v1 + coeff.m2 * v2
        }

        #[inline(always)]
        pub fn reset(&mut self) {
            self.ic1eq = f64x4::splat(0.0);
            self.ic2eq = f64x4::splat(0.0);
        }
    }
}