oximedia-audio 0.1.2

Audio codec implementations for OxiMedia
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Biquad filter implementation.
//!
//! This module provides a generic biquad filter that can be configured
//! for various filter types (low-pass, high-pass, band-pass, etc.).

#![forbid(unsafe_code)]
#![allow(clippy::cast_lossless)]

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

/// Type of biquad filter.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BiquadType {
    /// Low shelf filter - boost/cut below frequency.
    LowShelf,
    /// High shelf filter - boost/cut above frequency.
    HighShelf,
    /// Peaking filter - boost/cut around center frequency.
    Peaking,
    /// Low pass filter - attenuate above frequency.
    LowPass,
    /// High pass filter - attenuate below frequency.
    HighPass,
    /// Band pass filter - pass around center frequency.
    BandPass,
    /// Notch filter - attenuate around center frequency.
    Notch,
    /// All pass filter - phase shift only.
    AllPass,
}

/// Biquad filter coefficients.
///
/// These coefficients define a biquad (second-order IIR) filter in the form:
/// `y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] - a1*y[n-1] - a2*y[n-2]`
#[derive(Clone, Debug)]
pub struct BiquadCoefficients {
    /// Feed-forward coefficient 0.
    pub b0: f64,
    /// Feed-forward coefficient 1.
    pub b1: f64,
    /// Feed-forward coefficient 2.
    pub b2: f64,
    /// Feed-back coefficient 1 (normalized).
    pub a1: f64,
    /// Feed-back coefficient 2 (normalized).
    pub a2: f64,
}

impl Default for BiquadCoefficients {
    fn default() -> Self {
        Self::identity()
    }
}

impl BiquadCoefficients {
    /// Create identity (pass-through) coefficients.
    #[must_use]
    pub fn identity() -> Self {
        Self {
            b0: 1.0,
            b1: 0.0,
            b2: 0.0,
            a1: 0.0,
            a2: 0.0,
        }
    }

    /// Calculate coefficients for a specific filter type.
    ///
    /// # Arguments
    ///
    /// * `filter_type` - Type of filter to create
    /// * `sample_rate` - Sample rate in Hz
    /// * `frequency` - Center/cutoff frequency in Hz
    /// * `q` - Q factor (resonance/bandwidth control)
    /// * `gain_db` - Gain in dB (for peaking and shelf filters)
    #[must_use]
    pub fn calculate(
        filter_type: BiquadType,
        sample_rate: f64,
        frequency: f64,
        q: f64,
        gain_db: f64,
    ) -> Self {
        let w0 = 2.0 * PI * frequency / sample_rate;
        let cos_w0 = w0.cos();
        let sin_w0 = w0.sin();
        let alpha = sin_w0 / (2.0 * q);
        let a = 10.0_f64.powf(gain_db / 40.0);

        let (b0, b1, b2, a0, a1, a2) = match filter_type {
            BiquadType::LowShelf => {
                let two_sqrt_a_alpha = 2.0 * a.sqrt() * alpha;
                let a_plus_1 = a + 1.0;
                let a_minus_1 = a - 1.0;

                let b0 = a * (a_plus_1 - a_minus_1 * cos_w0 + two_sqrt_a_alpha);
                let b1 = 2.0 * a * (a_minus_1 - a_plus_1 * cos_w0);
                let b2 = a * (a_plus_1 - a_minus_1 * cos_w0 - two_sqrt_a_alpha);
                let a0 = a_plus_1 + a_minus_1 * cos_w0 + two_sqrt_a_alpha;
                let a1 = -2.0 * (a_minus_1 + a_plus_1 * cos_w0);
                let a2 = a_plus_1 + a_minus_1 * cos_w0 - two_sqrt_a_alpha;

                (b0, b1, b2, a0, a1, a2)
            }
            BiquadType::HighShelf => {
                let two_sqrt_a_alpha = 2.0 * a.sqrt() * alpha;
                let a_plus_1 = a + 1.0;
                let a_minus_1 = a - 1.0;

                let b0 = a * (a_plus_1 + a_minus_1 * cos_w0 + two_sqrt_a_alpha);
                let b1 = -2.0 * a * (a_minus_1 + a_plus_1 * cos_w0);
                let b2 = a * (a_plus_1 + a_minus_1 * cos_w0 - two_sqrt_a_alpha);
                let a0 = a_plus_1 - a_minus_1 * cos_w0 + two_sqrt_a_alpha;
                let a1 = 2.0 * (a_minus_1 - a_plus_1 * cos_w0);
                let a2 = a_plus_1 - a_minus_1 * cos_w0 - two_sqrt_a_alpha;

                (b0, b1, b2, a0, a1, a2)
            }
            BiquadType::Peaking => {
                let alpha_a = alpha * a;
                let alpha_over_a = alpha / a;

                let b0 = 1.0 + alpha_a;
                let b1 = -2.0 * cos_w0;
                let b2 = 1.0 - alpha_a;
                let a0 = 1.0 + alpha_over_a;
                let a1 = -2.0 * cos_w0;
                let a2 = 1.0 - alpha_over_a;

                (b0, b1, b2, a0, a1, a2)
            }
            BiquadType::LowPass => {
                let b0 = (1.0 - cos_w0) / 2.0;
                let b1 = 1.0 - cos_w0;
                let b2 = (1.0 - cos_w0) / 2.0;
                let a0 = 1.0 + alpha;
                let a1 = -2.0 * cos_w0;
                let a2 = 1.0 - alpha;

                (b0, b1, b2, a0, a1, a2)
            }
            BiquadType::HighPass => {
                let b0 = (1.0 + cos_w0) / 2.0;
                let b1 = -(1.0 + cos_w0);
                let b2 = (1.0 + cos_w0) / 2.0;
                let a0 = 1.0 + alpha;
                let a1 = -2.0 * cos_w0;
                let a2 = 1.0 - alpha;

                (b0, b1, b2, a0, a1, a2)
            }
            BiquadType::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)
            }
            BiquadType::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)
            }
            BiquadType::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)
            }
        };

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

    /// Create a low-pass filter.
    #[must_use]
    pub fn low_pass(sample_rate: f64, cutoff_hz: f64, q: f64) -> Self {
        Self::calculate(BiquadType::LowPass, sample_rate, cutoff_hz, q, 0.0)
    }

    /// Create a high-pass filter.
    #[must_use]
    pub fn high_pass(sample_rate: f64, cutoff_hz: f64, q: f64) -> Self {
        Self::calculate(BiquadType::HighPass, sample_rate, cutoff_hz, q, 0.0)
    }

    /// Create a band-pass filter.
    #[must_use]
    pub fn band_pass(sample_rate: f64, center_hz: f64, q: f64) -> Self {
        Self::calculate(BiquadType::BandPass, sample_rate, center_hz, q, 0.0)
    }

    /// Create a notch filter.
    #[must_use]
    pub fn notch(sample_rate: f64, center_hz: f64, q: f64) -> Self {
        Self::calculate(BiquadType::Notch, sample_rate, center_hz, q, 0.0)
    }

    /// Create an all-pass filter.
    #[must_use]
    pub fn all_pass(sample_rate: f64, center_hz: f64, q: f64) -> Self {
        Self::calculate(BiquadType::AllPass, sample_rate, center_hz, q, 0.0)
    }

    /// Create a peaking EQ filter.
    #[must_use]
    pub fn peaking(sample_rate: f64, center_hz: f64, q: f64, gain_db: f64) -> Self {
        Self::calculate(BiquadType::Peaking, sample_rate, center_hz, q, gain_db)
    }

    /// Create a low-shelf filter.
    #[must_use]
    pub fn low_shelf(sample_rate: f64, cutoff_hz: f64, gain_db: f64) -> Self {
        Self::calculate(BiquadType::LowShelf, sample_rate, cutoff_hz, 0.707, gain_db)
    }

    /// Create a high-shelf filter.
    #[must_use]
    pub fn high_shelf(sample_rate: f64, cutoff_hz: f64, gain_db: f64) -> Self {
        Self::calculate(
            BiquadType::HighShelf,
            sample_rate,
            cutoff_hz,
            0.707,
            gain_db,
        )
    }
}

/// Biquad filter state for processing audio.
///
/// This maintains the filter history (previous inputs and outputs)
/// needed for IIR filtering.
#[derive(Clone, Debug, Default)]
pub struct BiquadState {
    /// Previous input sample 1.
    x1: f64,
    /// Previous input sample 2.
    x2: f64,
    /// Previous output sample 1.
    y1: f64,
    /// Previous output sample 2.
    y2: f64,
}

impl BiquadState {
    /// Create a new biquad state with zero history.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Process a single sample through the biquad filter.
    ///
    /// # Arguments
    ///
    /// * `input` - Input sample
    /// * `coeffs` - Biquad coefficients
    ///
    /// # Returns
    ///
    /// Filtered output sample
    pub fn process(&mut self, input: f64, coeffs: &BiquadCoefficients) -> f64 {
        let output = coeffs.b0 * input + coeffs.b1 * self.x1 + coeffs.b2 * self.x2
            - coeffs.a1 * self.y1
            - coeffs.a2 * self.y2;

        self.x2 = self.x1;
        self.x1 = input;
        self.y2 = self.y1;
        self.y1 = output;

        output
    }

    /// Process multiple samples in-place.
    ///
    /// # Arguments
    ///
    /// * `samples` - Input/output sample buffer
    /// * `coeffs` - Biquad coefficients
    pub fn process_block(&mut self, samples: &mut [f64], coeffs: &BiquadCoefficients) {
        for sample in samples.iter_mut() {
            *sample = self.process(*sample, coeffs);
        }
    }

    /// Reset filter state to zero.
    pub fn reset(&mut self) {
        self.x1 = 0.0;
        self.x2 = 0.0;
        self.y1 = 0.0;
        self.y2 = 0.0;
    }
}

/// A cascaded biquad filter consisting of multiple biquad sections in series.
#[derive(Clone, Debug)]
pub struct CascadedBiquad {
    /// Coefficients for each stage.
    coefficients: Vec<BiquadCoefficients>,
    /// State for each stage.
    states: Vec<BiquadState>,
}

impl CascadedBiquad {
    /// Create a new cascaded biquad filter.
    ///
    /// # Arguments
    ///
    /// * `coefficients` - Vector of biquad coefficients (one per stage)
    #[must_use]
    pub fn new(coefficients: Vec<BiquadCoefficients>) -> Self {
        let num_stages = coefficients.len();
        Self {
            coefficients,
            states: vec![BiquadState::new(); num_stages],
        }
    }

    /// Process a single sample through all cascade stages.
    pub fn process(&mut self, mut input: f64) -> f64 {
        for (state, coeffs) in self.states.iter_mut().zip(&self.coefficients) {
            input = state.process(input, coeffs);
        }
        input
    }

    /// Process multiple samples in-place through all cascade stages.
    pub fn process_block(&mut self, samples: &mut [f64]) {
        for sample in samples.iter_mut() {
            *sample = self.process(*sample);
        }
    }

    /// Reset all filter states.
    pub fn reset(&mut self) {
        for state in &mut self.states {
            state.reset();
        }
    }

    /// Update coefficients for a specific stage.
    pub fn set_stage_coefficients(&mut self, stage: usize, coeffs: BiquadCoefficients) {
        if stage < self.coefficients.len() {
            self.coefficients[stage] = coeffs;
        }
    }

    /// Get the number of stages.
    #[must_use]
    pub fn num_stages(&self) -> usize {
        self.coefficients.len()
    }
}

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

    const SAMPLE_RATE: f64 = 48000.0;

    #[test]
    fn test_identity_coefficients() {
        let coeffs = BiquadCoefficients::identity();
        assert_eq!(coeffs.b0, 1.0);
        assert_eq!(coeffs.b1, 0.0);
        assert_eq!(coeffs.b2, 0.0);
        assert_eq!(coeffs.a1, 0.0);
        assert_eq!(coeffs.a2, 0.0);
    }

    #[test]
    fn test_identity_passthrough() {
        let coeffs = BiquadCoefficients::identity();
        let mut state = BiquadState::new();
        let input = 0.5;
        let output = state.process(input, &coeffs);
        assert!((output - input).abs() < 1e-10);
    }

    #[test]
    fn test_biquad_state_reset() {
        let coeffs = BiquadCoefficients::low_pass(SAMPLE_RATE, 1000.0, 0.707);
        let mut state = BiquadState::new();
        // Feed some samples
        for _ in 0..10 {
            state.process(1.0, &coeffs);
        }
        state.reset();
        // After reset, state should be zero - first sample output equals b0*input
        let out = state.process(0.0, &coeffs);
        assert_eq!(out, 0.0);
    }

    #[test]
    fn test_low_pass_attenuates_high_freq() {
        // Low-pass at 1 kHz should attenuate 10 kHz
        let coeffs = BiquadCoefficients::low_pass(SAMPLE_RATE, 1000.0, 0.707);
        let mut state = BiquadState::new();
        // Steady-state DC (0 Hz) should pass through
        let mut dc_out = 0.0;
        for _ in 0..1000 {
            dc_out = state.process(1.0, &coeffs);
        }
        assert!(
            dc_out > 0.9,
            "DC should pass through low-pass, got {dc_out}"
        );
    }

    #[test]
    fn test_high_pass_attenuates_dc() {
        // High-pass should block DC
        let coeffs = BiquadCoefficients::high_pass(SAMPLE_RATE, 1000.0, 0.707);
        let mut state = BiquadState::new();
        // Feed DC; output should settle near zero
        let mut dc_out = 0.0;
        for _ in 0..1000 {
            dc_out = state.process(1.0, &coeffs);
        }
        assert!(
            dc_out.abs() < 0.01,
            "DC should be blocked by high-pass, got {dc_out}"
        );
    }

    #[test]
    fn test_peaking_filter_no_gain() {
        // Peaking with 0 dB gain should be identity
        let coeffs = BiquadCoefficients::peaking(SAMPLE_RATE, 1000.0, 1.0, 0.0);
        let mut state = BiquadState::new();
        // After enough samples, DC should pass through unchanged
        let mut out = 0.0;
        for _ in 0..200 {
            out = state.process(1.0, &coeffs);
        }
        assert!(
            (out - 1.0).abs() < 0.01,
            "Peaking with 0 dB should be identity, got {out}"
        );
    }

    #[test]
    fn test_notch_filter_coefficients() {
        let coeffs = BiquadCoefficients::notch(SAMPLE_RATE, 1000.0, 1.0);
        // Notch: b0 and b2 should be equal, b1 == a1
        assert!((coeffs.b0 - coeffs.b2).abs() < 1e-10);
        assert!((coeffs.b1 - coeffs.a1).abs() < 1e-10);
    }

    #[test]
    fn test_low_shelf_boost_dc() {
        // Low shelf boost should amplify DC
        let coeffs = BiquadCoefficients::low_shelf(SAMPLE_RATE, 500.0, 6.0);
        let mut state = BiquadState::new();
        let mut out = 0.0;
        for _ in 0..500 {
            out = state.process(1.0, &coeffs);
        }
        // 6 dB gain at DC means output ~2.0
        assert!(out > 1.5, "Low shelf boost should amplify DC, got {out}");
    }

    #[test]
    fn test_high_shelf_boost_passes_high_freq() {
        // High shelf boost at 1 kHz: input at Nyquist (~24 kHz effective)
        // We just verify the coefficients compute without panicking
        let coeffs = BiquadCoefficients::high_shelf(SAMPLE_RATE, 1000.0, 6.0);
        // b0 should be positive and valid
        assert!(coeffs.b0.is_finite());
        assert!(coeffs.b1.is_finite());
        assert!(coeffs.b2.is_finite());
    }

    #[test]
    fn test_band_pass_coefficients() {
        let coeffs = BiquadCoefficients::band_pass(SAMPLE_RATE, 1000.0, 1.0);
        // For band-pass: b1 = 0 and b0 = -b2
        assert!((coeffs.b1).abs() < 1e-10);
        assert!((coeffs.b0 + coeffs.b2).abs() < 1e-10);
    }

    #[test]
    fn test_process_block_matches_per_sample() {
        let coeffs = BiquadCoefficients::low_pass(SAMPLE_RATE, 2000.0, 0.707);
        let input: Vec<f64> = (0..64).map(|i| (i as f64 * 0.1).sin()).collect();

        let mut state1 = BiquadState::new();
        let output_per_sample: Vec<f64> =
            input.iter().map(|&s| state1.process(s, &coeffs)).collect();

        let mut state2 = BiquadState::new();
        let mut block = input.clone();
        state2.process_block(&mut block, &coeffs);

        for (a, b) in output_per_sample.iter().zip(block.iter()) {
            assert!(
                (a - b).abs() < 1e-15,
                "process_block should match per-sample processing"
            );
        }
    }

    #[test]
    fn test_cascaded_biquad_num_stages() {
        let c1 = BiquadCoefficients::low_pass(SAMPLE_RATE, 1000.0, 0.707);
        let c2 = BiquadCoefficients::high_pass(SAMPLE_RATE, 100.0, 0.707);
        let cascaded = CascadedBiquad::new(vec![c1, c2]);
        assert_eq!(cascaded.num_stages(), 2);
    }

    #[test]
    fn test_cascaded_biquad_reset() {
        let c1 = BiquadCoefficients::low_pass(SAMPLE_RATE, 1000.0, 0.707);
        let mut cascaded = CascadedBiquad::new(vec![c1]);
        for _ in 0..20 {
            cascaded.process(1.0);
        }
        cascaded.reset();
        let out = cascaded.process(0.0);
        assert_eq!(out, 0.0);
    }

    #[test]
    fn test_cascaded_biquad_process_block() {
        let c1 = BiquadCoefficients::low_pass(SAMPLE_RATE, 2000.0, 0.707);
        let mut cascaded = CascadedBiquad::new(vec![c1]);
        let mut samples = vec![1.0_f64; 64];
        cascaded.process_block(&mut samples);
        // All outputs should be finite
        for s in &samples {
            assert!(s.is_finite());
        }
    }

    #[test]
    fn test_cascaded_biquad_set_stage_coefficients() {
        let c1 = BiquadCoefficients::identity();
        let c2 = BiquadCoefficients::low_pass(SAMPLE_RATE, 1000.0, 0.707);
        let mut cascaded = CascadedBiquad::new(vec![c1]);
        cascaded.set_stage_coefficients(0, c2);
        // After update, should process without panic
        let _ = cascaded.process(1.0);
    }

    #[test]
    fn test_all_pass_filter_unity_magnitude() {
        // All-pass filter should not change magnitude at DC (steady state)
        let coeffs = BiquadCoefficients::all_pass(SAMPLE_RATE, 1000.0, 0.707);
        let mut state = BiquadState::new();
        let mut out = 0.0;
        for _ in 0..1000 {
            out = state.process(1.0, &coeffs);
        }
        // Magnitude should be approximately 1.0 at DC
        assert!(
            (out.abs() - 1.0).abs() < 0.1,
            "All-pass should preserve magnitude, got {out}"
        );
    }
}