naad 1.2.5

naad — Audio synthesis primitives: oscillators, filters, envelopes, modulation, wavetables, effects
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! Biquad and state variable filters.
//!
//! Implements the Audio EQ Cookbook (Robert Bristow-Johnson) formulas
//! for biquad filter coefficient computation, and a state variable filter
//! with simultaneous LP/HP/BP/Notch outputs.

use serde::{Deserialize, Serialize};
use tracing::debug;

use crate::error::{self, NaadError, Result};

/// Filter type for biquad coefficient calculation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum FilterType {
    /// Low-pass filter.
    LowPass,
    /// High-pass filter.
    HighPass,
    /// Band-pass filter (constant skirt gain).
    BandPass,
    /// Notch (band-reject) filter.
    Notch,
    /// All-pass filter.
    AllPass,
    /// Low shelf filter.
    LowShelf,
    /// High shelf filter.
    HighShelf,
    /// Peaking EQ filter.
    Peak,
}

/// Biquad filter coefficients and state.
///
/// Uses Direct Form II Transposed implementation for better
/// numerical behavior with floating point.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BiquadFilter {
    // Coefficients (normalized by a0)
    b0: f32,
    b1: f32,
    b2: f32,
    a1: f32,
    a2: f32,
    // State variables (Direct Form II Transposed)
    #[serde(skip)]
    z1: f32,
    #[serde(skip)]
    z2: f32,
    // Parameters for recalculation
    filter_type: FilterType,
    sample_rate: f32,
    frequency: f32,
    q: f32,
    gain_db: f32,
}

impl BiquadFilter {
    /// Create a new biquad filter.
    ///
    /// # Arguments
    ///
    /// * `filter_type` - Type of filter
    /// * `sample_rate` - Sample rate in Hz
    /// * `frequency` - Cutoff/center frequency in Hz
    /// * `q` - Q factor (resonance), must be > 0
    ///
    /// # Errors
    ///
    /// Returns error if frequency or sample_rate is invalid, or q <= 0.
    pub fn new(filter_type: FilterType, sample_rate: f32, frequency: f32, q: f32) -> Result<Self> {
        Self::with_gain(filter_type, sample_rate, frequency, q, 0.0)
    }

    /// Create a new biquad filter with gain (for shelf and peak filter types).
    ///
    /// # Arguments
    ///
    /// * `filter_type` - Type of filter
    /// * `sample_rate` - Sample rate in Hz
    /// * `frequency` - Cutoff/center frequency in Hz
    /// * `q` - Q factor (resonance), must be > 0
    /// * `gain_db` - Gain in dB (used by LowShelf, HighShelf, Peak)
    ///
    /// # Errors
    ///
    /// Returns error if frequency or sample_rate is invalid, or q <= 0.
    pub fn with_gain(
        filter_type: FilterType,
        sample_rate: f32,
        frequency: f32,
        q: f32,
        gain_db: f32,
    ) -> Result<Self> {
        if let Some(e) = error::validate_sample_rate(sample_rate) {
            return Err(e);
        }
        if let Some(e) = error::validate_frequency(frequency, sample_rate) {
            return Err(e);
        }
        if q <= 0.0 || !q.is_finite() {
            return Err(NaadError::InvalidParameter {
                name: "q".to_string(),
                reason: "must be > 0".to_string(),
            });
        }

        debug!(?filter_type, frequency, q, gain_db, "biquad filter created");
        let mut filter = Self {
            b0: 1.0,
            b1: 0.0,
            b2: 0.0,
            a1: 0.0,
            a2: 0.0,
            z1: 0.0,
            z2: 0.0,
            filter_type,
            sample_rate,
            frequency,
            q,
            gain_db,
        };
        filter.compute_coefficients();
        Ok(filter)
    }

    /// Compute biquad coefficients from the Audio EQ Cookbook.
    fn compute_coefficients(&mut self) {
        let w0 = std::f32::consts::TAU * self.frequency / self.sample_rate;
        let cos_w0 = w0.cos();
        let sin_w0 = w0.sin();
        let alpha = sin_w0 / (2.0 * self.q);

        let (b0, b1, b2, a0, a1, a2) = match self.filter_type {
            FilterType::LowPass => {
                let b1 = 1.0 - cos_w0;
                let b0 = b1 / 2.0;
                let b2 = b0;
                let a0 = 1.0 + alpha;
                let a1 = -2.0 * cos_w0;
                let a2 = 1.0 - alpha;
                (b0, b1, b2, a0, a1, a2)
            }
            FilterType::HighPass => {
                let b1_raw = 1.0 + cos_w0;
                let b0 = b1_raw / 2.0;
                let b1 = -(1.0 + cos_w0);
                let b2 = b0;
                let a0 = 1.0 + alpha;
                let a1 = -2.0 * cos_w0;
                let a2 = 1.0 - alpha;
                (b0, b1, b2, a0, a1, a2)
            }
            FilterType::BandPass => {
                let b0 = alpha;
                let b1 = 0.0;
                let b2 = -alpha;
                let a0 = 1.0 + alpha;
                let a1 = -2.0 * cos_w0;
                let a2 = 1.0 - alpha;
                (b0, b1, b2, a0, a1, a2)
            }
            FilterType::Notch => {
                let b0 = 1.0;
                let b1 = -2.0 * cos_w0;
                let b2 = 1.0;
                let a0 = 1.0 + alpha;
                let a1 = -2.0 * cos_w0;
                let a2 = 1.0 - alpha;
                (b0, b1, b2, a0, a1, a2)
            }
            FilterType::AllPass => {
                let b0 = 1.0 - alpha;
                let b1 = -2.0 * cos_w0;
                let b2 = 1.0 + alpha;
                let a0 = 1.0 + alpha;
                let a1 = -2.0 * cos_w0;
                let a2 = 1.0 - alpha;
                (b0, b1, b2, a0, a1, a2)
            }
            FilterType::LowShelf => {
                let a_gain = 10.0f32.powf(self.gain_db / 40.0);
                let two_sqrt_a_alpha = 2.0 * a_gain.sqrt() * alpha;
                let b0 = a_gain * ((a_gain + 1.0) - (a_gain - 1.0) * cos_w0 + two_sqrt_a_alpha);
                let b1 = 2.0 * a_gain * ((a_gain - 1.0) - (a_gain + 1.0) * cos_w0);
                let b2 = a_gain * ((a_gain + 1.0) - (a_gain - 1.0) * cos_w0 - two_sqrt_a_alpha);
                let a0 = (a_gain + 1.0) + (a_gain - 1.0) * cos_w0 + two_sqrt_a_alpha;
                let a1 = -2.0 * ((a_gain - 1.0) + (a_gain + 1.0) * cos_w0);
                let a2 = (a_gain + 1.0) + (a_gain - 1.0) * cos_w0 - two_sqrt_a_alpha;
                (b0, b1, b2, a0, a1, a2)
            }
            FilterType::HighShelf => {
                let a_gain = 10.0f32.powf(self.gain_db / 40.0);
                let two_sqrt_a_alpha = 2.0 * a_gain.sqrt() * alpha;
                let b0 = a_gain * ((a_gain + 1.0) + (a_gain - 1.0) * cos_w0 + two_sqrt_a_alpha);
                let b1 = -2.0 * a_gain * ((a_gain - 1.0) + (a_gain + 1.0) * cos_w0);
                let b2 = a_gain * ((a_gain + 1.0) + (a_gain - 1.0) * cos_w0 - two_sqrt_a_alpha);
                let a0 = (a_gain + 1.0) - (a_gain - 1.0) * cos_w0 + two_sqrt_a_alpha;
                let a1 = 2.0 * ((a_gain - 1.0) - (a_gain + 1.0) * cos_w0);
                let a2 = (a_gain + 1.0) - (a_gain - 1.0) * cos_w0 - two_sqrt_a_alpha;
                (b0, b1, b2, a0, a1, a2)
            }
            FilterType::Peak => {
                let a_gain = 10.0f32.powf(self.gain_db / 40.0);
                let b0 = 1.0 + alpha * a_gain;
                let b1 = -2.0 * cos_w0;
                let b2 = 1.0 - alpha * a_gain;
                let a0 = 1.0 + alpha / a_gain;
                let a1 = -2.0 * cos_w0;
                let a2 = 1.0 - alpha / a_gain;
                (b0, b1, b2, a0, a1, a2)
            }
        };

        // Normalize by a0
        let inv_a0 = 1.0 / a0;
        self.b0 = b0 * inv_a0;
        self.b1 = b1 * inv_a0;
        self.b2 = b2 * inv_a0;
        self.a1 = a1 * inv_a0;
        self.a2 = a2 * inv_a0;
    }

    /// Process a single sample through the filter.
    ///
    /// Uses Direct Form II Transposed for numerical stability.
    /// State variables are flushed to prevent denormal slowdowns.
    #[inline]
    #[must_use]
    pub fn process_sample(&mut self, input: f32) -> f32 {
        let output = self.b0 * input + self.z1;
        self.z1 = crate::flush_denormal(self.b1 * input - self.a1 * output + self.z2);
        self.z2 = crate::flush_denormal(self.b2 * input - self.a2 * output);
        output
    }

    /// Update filter parameters and recalculate coefficients.
    ///
    /// # Errors
    ///
    /// Returns error if frequency or q is invalid.
    pub fn set_params(&mut self, frequency: f32, q: f32, gain_db: f32) -> Result<()> {
        if let Some(e) = error::validate_frequency(frequency, self.sample_rate) {
            return Err(e);
        }
        if q <= 0.0 || !q.is_finite() {
            return Err(NaadError::InvalidParameter {
                name: "q".to_string(),
                reason: "must be > 0".to_string(),
            });
        }
        self.frequency = frequency;
        self.q = q;
        self.gain_db = gain_db;
        self.compute_coefficients();
        Ok(())
    }

    /// Process a buffer of samples in place.
    #[inline]
    pub fn process_buffer(&mut self, buffer: &mut [f32]) {
        for sample in buffer.iter_mut() {
            *sample = self.process_sample(*sample);
        }
    }

    /// Reset filter state (clear delay line).
    pub fn reset(&mut self) {
        self.z1 = 0.0;
        self.z2 = 0.0;
    }
}

/// Output from a state variable filter (simultaneous outputs).
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct SvfOutput {
    /// Low-pass output.
    pub low_pass: f32,
    /// High-pass output.
    pub high_pass: f32,
    /// Band-pass output.
    pub band_pass: f32,
    /// Notch output.
    pub notch: f32,
}

/// State variable filter with simultaneous LP/HP/BP/Notch outputs.
///
/// Uses the Cytomic/Simper SVF topology for numerical stability at high
/// resonance and high frequencies. Coefficients are cached and only
/// recomputed when parameters change.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateVariableFilter {
    frequency: f32,
    q: f32,
    sample_rate: f32,
    // Cached coefficients
    g: f32,
    k: f32,
    a1: f32,
    a2: f32,
    a3: f32,
    // Internal state
    #[serde(skip)]
    ic1eq: f32,
    #[serde(skip)]
    ic2eq: f32,
}

impl StateVariableFilter {
    /// Create a new state variable filter.
    ///
    /// # Errors
    ///
    /// Returns error if frequency, sample_rate, or q is invalid.
    pub fn new(frequency: f32, q: f32, sample_rate: f32) -> Result<Self> {
        if let Some(e) = error::validate_sample_rate(sample_rate) {
            return Err(e);
        }
        if let Some(e) = error::validate_frequency(frequency, sample_rate) {
            return Err(e);
        }
        if q <= 0.0 || !q.is_finite() {
            return Err(NaadError::InvalidParameter {
                name: "q".to_string(),
                reason: "must be > 0".to_string(),
            });
        }

        let g = (std::f32::consts::PI * frequency / sample_rate).tan();
        let k = 1.0 / q;
        let a1 = 1.0 / (1.0 + g * (g + k));
        let a2 = g * a1;
        let a3 = g * a2;

        Ok(Self {
            frequency,
            q,
            sample_rate,
            g,
            k,
            a1,
            a2,
            a3,
            ic1eq: 0.0,
            ic2eq: 0.0,
        })
    }

    /// Returns the current cutoff frequency in Hz.
    #[inline]
    #[must_use]
    pub fn frequency(&self) -> f32 {
        self.frequency
    }

    /// Returns the current Q factor.
    #[inline]
    #[must_use]
    pub fn q(&self) -> f32 {
        self.q
    }

    /// Returns the sample rate in Hz.
    #[inline]
    #[must_use]
    pub fn sample_rate(&self) -> f32 {
        self.sample_rate
    }

    /// Update filter parameters. Coefficients are recalculated only when called.
    ///
    /// # Errors
    ///
    /// Returns error if frequency or q is invalid.
    pub fn set_params(&mut self, frequency: f32, q: f32) -> Result<()> {
        if let Some(e) = error::validate_frequency(frequency, self.sample_rate) {
            return Err(e);
        }
        if q <= 0.0 || !q.is_finite() {
            return Err(NaadError::InvalidParameter {
                name: "q".to_string(),
                reason: "must be > 0".to_string(),
            });
        }

        self.frequency = frequency;
        self.q = q;
        self.g = (std::f32::consts::PI * frequency / self.sample_rate).tan();
        self.k = 1.0 / q;
        self.a1 = 1.0 / (1.0 + self.g * (self.g + self.k));
        self.a2 = self.g * self.a1;
        self.a3 = self.g * self.a2;
        Ok(())
    }

    /// Process a sample and return all four filter outputs simultaneously.
    ///
    /// State variables are flushed to prevent denormal slowdowns.
    #[inline]
    #[must_use]
    pub fn process_sample(&mut self, input: f32) -> SvfOutput {
        let v3 = input - self.ic2eq;
        let v1 = self.a1 * self.ic1eq + self.a2 * v3;
        let v2 = self.ic2eq + self.a2 * self.ic1eq + self.a3 * v3;

        self.ic1eq = crate::flush_denormal(2.0 * v1 - self.ic1eq);
        self.ic2eq = crate::flush_denormal(2.0 * v2 - self.ic2eq);

        let low_pass = v2;
        let band_pass = v1;
        let high_pass = input - self.k * v1 - v2;
        let notch = low_pass + high_pass;

        SvfOutput {
            low_pass,
            high_pass,
            band_pass,
            notch,
        }
    }

    /// Process a single sample returning only the low-pass output.
    ///
    /// Convenience method — internally computes all outputs.
    #[inline]
    #[must_use]
    pub fn process_sample_lowpass(&mut self, input: f32) -> f32 {
        self.process_sample(input).low_pass
    }

    /// Process a buffer of samples, writing low-pass output in-place.
    #[inline]
    pub fn process_buffer_lowpass(&mut self, buffer: &mut [f32]) {
        for sample in buffer.iter_mut() {
            *sample = self.process_sample(*sample).low_pass;
        }
    }

    /// Reset filter state.
    pub fn reset(&mut self) {
        self.ic1eq = 0.0;
        self.ic2eq = 0.0;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_lowpass_basic() {
        let mut filter = BiquadFilter::new(FilterType::LowPass, 44100.0, 1000.0, 0.707).unwrap();
        // DC should pass through
        let mut output = 0.0;
        for _ in 0..1000 {
            output = filter.process_sample(1.0);
        }
        assert!(
            (output - 1.0).abs() < 0.01,
            "DC should pass through LP, got {output}"
        );
    }

    #[test]
    fn test_highpass_blocks_dc() {
        let mut filter = BiquadFilter::new(FilterType::HighPass, 44100.0, 1000.0, 0.707).unwrap();
        let mut output = 0.0;
        for _ in 0..1000 {
            output = filter.process_sample(1.0);
        }
        assert!(output.abs() < 0.01, "HP should block DC, got {output}");
    }

    #[test]
    fn test_invalid_params() {
        assert!(BiquadFilter::new(FilterType::LowPass, 0.0, 1000.0, 0.7).is_err());
        assert!(BiquadFilter::new(FilterType::LowPass, 44100.0, 0.0, 0.7).is_err());
        assert!(BiquadFilter::new(FilterType::LowPass, 44100.0, 1000.0, 0.0).is_err());
        assert!(BiquadFilter::new(FilterType::LowPass, 44100.0, 30000.0, 0.7).is_err());
    }

    #[test]
    fn test_set_params() {
        let mut filter = BiquadFilter::new(FilterType::LowPass, 44100.0, 1000.0, 0.707).unwrap();
        assert!(filter.set_params(2000.0, 1.0, 0.0).is_ok());
        assert!(filter.set_params(0.0, 1.0, 0.0).is_err());
    }

    #[test]
    fn test_svf_basic() {
        let mut svf = StateVariableFilter::new(1000.0, 0.707, 44100.0).unwrap();
        let out = svf.process_sample(1.0);
        assert!(out.low_pass.is_finite());
        assert!(out.high_pass.is_finite());
        assert!(out.band_pass.is_finite());
        assert!(out.notch.is_finite());
    }

    #[test]
    fn test_serde_roundtrip() {
        let filter = BiquadFilter::new(FilterType::BandPass, 44100.0, 1000.0, 2.0).unwrap();
        let json = serde_json::to_string(&filter).unwrap();
        let back: BiquadFilter = serde_json::from_str(&json).unwrap();
        assert!((filter.frequency - back.frequency).abs() < f32::EPSILON);
    }

    #[test]
    fn test_process_buffer() {
        let mut filter = BiquadFilter::new(FilterType::LowPass, 44100.0, 1000.0, 0.707).unwrap();
        let mut buf = [1.0f32; 256];
        filter.process_buffer(&mut buf);
        assert!(buf.iter().all(|s| s.is_finite()));
    }
}