dhvani 1.0.0

Core audio engine — buffers, DSP, resampling, mixing, analysis, and capture for Rust
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
//! Biquad filter primitives — coefficients, per-channel state, processing.
//!
//! Implements the standard Robert Bristow-Johnson Audio EQ Cookbook formulas
//! for all common filter types. Coefficients computed in f64 for precision.

use serde::{Deserialize, Serialize};

use crate::buffer::AudioBuffer;
use abaco::dsp::{angular_frequency, db_gain_factor};

/// Filter type with associated parameters.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum FilterType {
    /// Second-order low-pass filter.
    LowPass,
    /// Second-order high-pass filter.
    HighPass,
    /// Band-pass filter (constant skirt gain).
    BandPass,
    /// Notch (band-reject) filter.
    Notch,
    /// All-pass filter (phase shift only).
    AllPass,
    /// Peaking EQ filter.
    Peaking {
        /// Boost/cut in decibels.
        gain_db: f32,
    },
    /// Low-shelf filter.
    LowShelf {
        /// Boost/cut in decibels.
        gain_db: f32,
    },
    /// High-shelf filter.
    HighShelf {
        /// Boost/cut in decibels.
        gain_db: f32,
    },
}

/// Biquad filter coefficients (Direct Form II Transposed).
#[must_use]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct BiquadCoeffs {
    /// Feedforward coefficient b0.
    pub b0: f64,
    /// Feedforward coefficient b1.
    pub b1: f64,
    /// Feedforward coefficient b2.
    pub b2: f64,
    /// Feedback coefficient a1 (negated/normalized).
    pub a1: f64,
    /// Feedback coefficient a2 (negated/normalized).
    pub a2: f64,
}

impl BiquadCoeffs {
    /// Design filter coefficients using Bristow-Johnson Audio EQ Cookbook.
    pub fn design(filter_type: FilterType, freq_hz: f32, q: f32, sample_rate: u32) -> Self {
        let sr = sample_rate as f64;
        let f0 = (freq_hz as f64).clamp(1.0, sr * 0.499);
        let q = (q as f64).max(0.01);
        let w0 = angular_frequency(f0, sr);
        let cos_w0 = w0.cos();
        let sin_w0 = w0.sin();
        let alpha = sin_w0 / (2.0 * q);

        let (b0, b1, b2, a0, a1, a2) = match filter_type {
            FilterType::LowPass => {
                let b1 = 1.0 - cos_w0;
                let b0 = b1 / 2.0;
                let b2 = b0;
                (b0, b1, b2, 1.0 + alpha, -2.0 * cos_w0, 1.0 - alpha)
            }
            FilterType::HighPass => {
                let b1 = -(1.0 + cos_w0);
                let b0 = (1.0 + cos_w0) / 2.0;
                let b2 = b0;
                (b0, b1, b2, 1.0 + alpha, -2.0 * cos_w0, 1.0 - alpha)
            }
            FilterType::BandPass => {
                let b0 = alpha;
                let b1 = 0.0;
                let b2 = -alpha;
                (b0, b1, b2, 1.0 + alpha, -2.0 * cos_w0, 1.0 - alpha)
            }
            FilterType::Notch => {
                let b0 = 1.0;
                let b1 = -2.0 * cos_w0;
                let b2 = 1.0;
                (b0, b1, b2, 1.0 + alpha, -2.0 * cos_w0, 1.0 - alpha)
            }
            FilterType::AllPass => {
                let b0 = 1.0 - alpha;
                let b1 = -2.0 * cos_w0;
                let b2 = 1.0 + alpha;
                (b0, b1, b2, 1.0 + alpha, -2.0 * cos_w0, 1.0 - alpha)
            }
            FilterType::Peaking { gain_db } => {
                let a = db_gain_factor(gain_db as f64);
                let b0 = 1.0 + alpha * a;
                let b1 = -2.0 * cos_w0;
                let b2 = 1.0 - alpha * a;
                (b0, b1, b2, 1.0 + alpha / a, -2.0 * cos_w0, 1.0 - alpha / a)
            }
            FilterType::LowShelf { gain_db } => {
                let a = db_gain_factor(gain_db as f64);
                let two_sqrt_a_alpha = 2.0 * a.sqrt() * alpha;
                let b0 = a * ((a + 1.0) - (a - 1.0) * cos_w0 + two_sqrt_a_alpha);
                let b1 = 2.0 * a * ((a - 1.0) - (a + 1.0) * cos_w0);
                let b2 = a * ((a + 1.0) - (a - 1.0) * cos_w0 - two_sqrt_a_alpha);
                let a0 = (a + 1.0) + (a - 1.0) * cos_w0 + two_sqrt_a_alpha;
                let a1 = -2.0 * ((a - 1.0) + (a + 1.0) * cos_w0);
                let a2 = (a + 1.0) + (a - 1.0) * cos_w0 - two_sqrt_a_alpha;
                (b0, b1, b2, a0, a1, a2)
            }
            FilterType::HighShelf { gain_db } => {
                let a = db_gain_factor(gain_db as f64);
                let two_sqrt_a_alpha = 2.0 * a.sqrt() * alpha;
                let b0 = a * ((a + 1.0) + (a - 1.0) * cos_w0 + two_sqrt_a_alpha);
                let b1 = -2.0 * a * ((a - 1.0) + (a + 1.0) * cos_w0);
                let b2 = a * ((a + 1.0) + (a - 1.0) * cos_w0 - two_sqrt_a_alpha);
                let a0 = (a + 1.0) - (a - 1.0) * cos_w0 + two_sqrt_a_alpha;
                let a1 = 2.0 * ((a - 1.0) - (a + 1.0) * cos_w0);
                let a2 = (a + 1.0) - (a - 1.0) * cos_w0 - two_sqrt_a_alpha;
                (b0, b1, b2, a0, a1, a2)
            }
        };

        // Normalize by a0
        Self {
            b0: b0 / a0,
            b1: b1 / a0,
            b2: b2 / a0,
            a1: a1 / a0,
            a2: a2 / a0,
        }
    }

    /// Unity (pass-through) coefficients.
    pub fn unity() -> Self {
        Self {
            b0: 1.0,
            b1: 0.0,
            b2: 0.0,
            a1: 0.0,
            a2: 0.0,
        }
    }
}

/// Per-channel biquad state (Direct Form II Transposed).
#[derive(Debug, Clone, Default)]
struct BiquadState {
    z1: f64,
    z2: f64,
}

impl BiquadState {
    #[inline]
    fn process(&mut self, input: f64, c: &BiquadCoeffs) -> f64 {
        let out = c.b0 * input + self.z1;
        self.z1 = c.b1 * input - c.a1 * out + self.z2;
        self.z2 = c.b2 * input - c.a2 * out;
        out
    }
}

/// A biquad filter with per-channel state.
#[must_use]
#[derive(Debug, Clone)]
pub struct BiquadFilter {
    coeffs: BiquadCoeffs,
    states: Vec<BiquadState>,
    filter_type: FilterType,
    freq_hz: f32,
    q: f32,
    sample_rate: u32,
    bypassed: bool,
    /// Dry/wet mix (0.0 = fully dry, 1.0 = fully wet).
    mix: f32,
}

impl BiquadFilter {
    /// Create a new biquad filter.
    pub fn new(
        filter_type: FilterType,
        freq_hz: f32,
        q: f32,
        sample_rate: u32,
        channels: u32,
    ) -> Self {
        tracing::debug!(
            ?filter_type,
            freq_hz,
            q,
            sample_rate,
            channels,
            "BiquadFilter::new"
        );
        Self {
            coeffs: BiquadCoeffs::design(filter_type, freq_hz, q, sample_rate),
            states: vec![BiquadState::default(); channels as usize],
            filter_type,
            freq_hz,
            q,
            sample_rate,
            bypassed: false,
            mix: 1.0,
        }
    }

    /// Set whether this filter is bypassed.
    pub fn set_bypass(&mut self, bypassed: bool) {
        self.bypassed = bypassed;
    }

    /// Returns `true` if this filter is currently bypassed.
    pub fn is_bypassed(&self) -> bool {
        self.bypassed
    }

    /// Process an entire audio buffer in-place.
    #[inline]
    pub fn process(&mut self, buf: &mut AudioBuffer) {
        if self.bypassed {
            return;
        }
        let ch = buf.channels as usize;
        let mix = self.mix;

        // Fast path: stereo, full wet, SIMD available — use cross-channel SIMD biquad
        #[cfg(feature = "simd")]
        if ch == 2 && (mix - 1.0).abs() < f32::EPSILON {
            let coeffs = [
                self.coeffs.b0,
                self.coeffs.b1,
                self.coeffs.b2,
                self.coeffs.a1,
                self.coeffs.a2,
            ];
            let mut state = [
                self.states[0].z1,
                self.states[0].z2,
                self.states[1].z1,
                self.states[1].z2,
            ];
            crate::simd::biquad_stereo(&mut buf.samples, &coeffs, &mut state);
            self.states[0].z1 = state[0];
            self.states[0].z2 = state[1];
            self.states[1].z1 = state[2];
            self.states[1].z2 = state[3];
            return;
        }

        let dry = 1.0 - mix;
        for frame in 0..buf.frames {
            for c in 0..ch {
                let idx = frame * ch + c;
                let input = buf.samples[idx] as f64;
                let wet = self.states[c].process(input, &self.coeffs) as f32;
                buf.samples[idx] = buf.samples[idx] * dry + wet * mix;
            }
        }
    }

    /// Process a single sample for a given channel.
    #[inline]
    #[must_use]
    pub fn process_sample(&mut self, sample: f32, channel: usize) -> f32 {
        if channel < self.states.len() {
            self.states[channel].process(sample as f64, &self.coeffs) as f32
        } else {
            sample
        }
    }

    /// Reset all filter state (e.g., on seek).
    pub fn reset(&mut self) {
        for s in &mut self.states {
            s.z1 = 0.0;
            s.z2 = 0.0;
        }
    }

    /// Update filter parameters without resetting state (for smooth automation).
    pub fn set_params(&mut self, filter_type: FilterType, freq_hz: f32, q: f32) {
        self.filter_type = filter_type;
        self.freq_hz = freq_hz;
        self.q = q;
        self.coeffs = BiquadCoeffs::design(filter_type, freq_hz, q, self.sample_rate);
    }

    /// Current filter type.
    pub fn filter_type(&self) -> FilterType {
        self.filter_type
    }

    /// Current center frequency in Hz.
    pub fn freq_hz(&self) -> f32 {
        self.freq_hz
    }

    /// Current Q factor.
    pub fn q(&self) -> f32 {
        self.q
    }

    /// Set the dry/wet mix (0.0 = fully dry, 1.0 = fully wet).
    pub fn set_mix(&mut self, mix: f32) {
        self.mix = mix.clamp(0.0, 1.0);
    }

    /// Current dry/wet mix.
    pub fn mix(&self) -> f32 {
        self.mix
    }

    /// Update the sample rate and recompute coefficients.
    pub fn set_sample_rate(&mut self, sample_rate: u32) {
        self.sample_rate = sample_rate;
        self.coeffs = BiquadCoeffs::design(self.filter_type, self.freq_hz, self.q, sample_rate);
    }
}

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

    fn make_sine(freq: f32, sample_rate: u32, frames: usize) -> AudioBuffer {
        let samples: Vec<f32> = (0..frames)
            .map(|i| (2.0 * std::f32::consts::PI * freq * i as f32 / sample_rate as f32).sin())
            .collect();
        AudioBuffer::from_interleaved(samples, 1, sample_rate).unwrap()
    }

    #[test]
    fn silence_passthrough() {
        let mut buf = AudioBuffer::silence(2, 1024, 44100);
        let mut filt = BiquadFilter::new(FilterType::LowPass, 1000.0, 0.707, 44100, 2);
        filt.process(&mut buf);
        assert!(buf.peak() < f32::EPSILON);
    }

    #[test]
    fn unity_coeffs_passthrough() {
        let coeffs = BiquadCoeffs::unity();
        let mut state = BiquadState::default();
        let out = state.process(0.5, &coeffs);
        assert!((out - 0.5).abs() < 1e-10);
    }

    #[test]
    fn lowpass_attenuates_high_frequencies() {
        // 10kHz sine through 500Hz low-pass should be heavily attenuated
        let mut buf = make_sine(10000.0, 44100, 4096);
        let original_rms = buf.rms();
        let mut filt = BiquadFilter::new(FilterType::LowPass, 500.0, 0.707, 44100, 1);
        filt.process(&mut buf);
        assert!(buf.rms() < original_rms * 0.1, "LP should attenuate 10kHz");
    }

    #[test]
    fn highpass_attenuates_low_frequencies() {
        // 100Hz sine through 5kHz high-pass should be heavily attenuated
        let mut buf = make_sine(100.0, 44100, 4096);
        let original_rms = buf.rms();
        let mut filt = BiquadFilter::new(FilterType::HighPass, 5000.0, 0.707, 44100, 1);
        filt.process(&mut buf);
        assert!(buf.rms() < original_rms * 0.1, "HP should attenuate 100Hz");
    }

    #[test]
    fn peaking_boosts_target_frequency() {
        let mut buf = make_sine(1000.0, 44100, 4096);
        let original_rms = buf.rms();
        let mut filt =
            BiquadFilter::new(FilterType::Peaking { gain_db: 12.0 }, 1000.0, 1.0, 44100, 1);
        filt.process(&mut buf);
        // After transient settles, RMS should be higher
        assert!(buf.rms() > original_rms * 1.5, "Peaking should boost 1kHz");
    }

    #[test]
    fn notch_attenuates_target_frequency() {
        let mut buf = make_sine(1000.0, 44100, 4096);
        let original_rms = buf.rms();
        let mut filt = BiquadFilter::new(FilterType::Notch, 1000.0, 10.0, 44100, 1);
        filt.process(&mut buf);
        assert!(
            buf.rms() < original_rms * 0.2,
            "Notch should attenuate 1kHz"
        );
    }

    #[test]
    fn reset_clears_state() {
        let mut filt = BiquadFilter::new(FilterType::LowPass, 1000.0, 0.707, 44100, 2);
        let mut buf = make_sine(440.0, 44100, 256);
        filt.process(&mut buf);
        filt.reset();
        // After reset, process_sample should behave as fresh
        let out = filt.process_sample(0.0, 0);
        assert!(out.abs() < f32::EPSILON);
    }

    #[test]
    fn set_params_updates_coefficients() {
        let mut filt = BiquadFilter::new(FilterType::LowPass, 1000.0, 0.707, 44100, 1);
        filt.set_params(FilterType::HighPass, 5000.0, 1.0);
        assert_eq!(filt.filter_type(), FilterType::HighPass);
        assert!((filt.freq_hz() - 5000.0).abs() < f32::EPSILON);
    }

    #[test]
    fn stereo_channels_independent() {
        let samples = vec![1.0, 0.0, 0.5, 0.0, 0.25, 0.0, 0.0, 0.0];
        let mut buf = AudioBuffer::from_interleaved(samples, 2, 44100).unwrap();
        let mut filt = BiquadFilter::new(FilterType::LowPass, 10000.0, 0.707, 44100, 2);
        filt.process(&mut buf);
        // Right channel was all zeros, should remain near zero
        for frame in 0..buf.frames {
            assert!(
                buf.samples[frame * 2 + 1].abs() < 0.01,
                "Right channel should stay near zero"
            );
        }
    }

    #[test]
    fn extreme_q_does_not_panic() {
        let mut buf = make_sine(440.0, 44100, 256);
        let mut filt = BiquadFilter::new(FilterType::BandPass, 20000.0, 100.0, 44100, 1);
        filt.process(&mut buf);
        assert!(buf.samples.iter().all(|s| s.is_finite()));
    }

    #[test]
    fn allpass_preserves_magnitude() {
        let mut buf = make_sine(1000.0, 44100, 4096);
        let original_rms = buf.rms();
        let mut filt = BiquadFilter::new(FilterType::AllPass, 1000.0, 0.707, 44100, 1);
        filt.process(&mut buf);
        // AllPass should preserve magnitude (only change phase)
        assert!(
            (buf.rms() - original_rms).abs() < original_rms * 0.05,
            "AllPass should preserve RMS: {} vs {}",
            buf.rms(),
            original_rms
        );
    }

    #[test]
    fn low_shelf_boosts_lows() {
        let mut buf = make_sine(200.0, 44100, 4096);
        let original_rms = buf.rms();
        let mut filt = BiquadFilter::new(
            FilterType::LowShelf { gain_db: 12.0 },
            500.0,
            0.707,
            44100,
            1,
        );
        filt.process(&mut buf);
        assert!(
            buf.rms() > original_rms * 1.5,
            "Low shelf should boost 200Hz"
        );
    }

    #[test]
    fn high_shelf_boosts_highs() {
        let mut buf = make_sine(8000.0, 44100, 4096);
        let original_rms = buf.rms();
        let mut filt = BiquadFilter::new(
            FilterType::HighShelf { gain_db: 12.0 },
            5000.0,
            0.707,
            44100,
            1,
        );
        filt.process(&mut buf);
        assert!(
            buf.rms() > original_rms * 1.5,
            "High shelf should boost 8kHz"
        );
    }

    #[test]
    fn low_shelf_cuts_lows() {
        let mut buf = make_sine(200.0, 44100, 4096);
        let original_rms = buf.rms();
        let mut filt = BiquadFilter::new(
            FilterType::LowShelf { gain_db: -12.0 },
            500.0,
            0.707,
            44100,
            1,
        );
        filt.process(&mut buf);
        assert!(buf.rms() < original_rms * 0.5, "Low shelf should cut 200Hz");
    }

    #[test]
    fn process_sample_out_of_range_channel() {
        let mut filt = BiquadFilter::new(FilterType::LowPass, 1000.0, 0.707, 44100, 1);
        // Channel 5 doesn't exist — should return input unchanged
        let out = filt.process_sample(0.75, 5);
        assert!((out - 0.75).abs() < f32::EPSILON);
    }
}