oximedia-audio 0.1.0

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
//! Biquad filter implementation (IIR) using f32 arithmetic.
//!
//! This module provides a lightweight, self-contained second-order IIR filter
//! designed for real-time audio processing.  It complements the higher-precision
//! f64 implementation found in [`crate::dsp`] by offering faster throughput at
//! the cost of slightly reduced numerical precision – appropriate for most
//! sample-processing pipelines.
//!
//! # Filter designs
//!
//! | Constructor | Type |
//! |---|---|
//! | [`BiquadCoeffs::lowpass`] | Second-order Butterworth low-pass |
//! | [`BiquadCoeffs::highpass`] | Second-order Butterworth high-pass |
//! | [`BiquadCoeffs::bandpass`] | Constant-skirt band-pass |
//! | [`BiquadCoeffs::peaking_eq`] | Peaking parametric EQ |
//!
//! # Example
//!
//! ```
//! use oximedia_audio::biquad::{BiquadCoeffs, BiquadFilter};
//!
//! let coeffs = BiquadCoeffs::lowpass(1000.0, 0.707, 48000.0);
//! let mut filter = BiquadFilter::new(coeffs);
//! let output = filter.process(0.5);
//! assert!(output.is_finite());
//! ```

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

use std::f32::consts::PI;

/// Biquad filter coefficients in direct form I.
///
/// The filter equation is:
/// `y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] - a1*y[n-1] - a2*y[n-2]`
///
/// All `a` coefficients are stored **normalised** (divided by a0).
#[derive(Clone, Debug, PartialEq)]
#[allow(dead_code)]
pub struct BiquadCoeffs {
    /// Feed-forward coefficient 0.
    pub b0: f32,
    /// Feed-forward coefficient 1.
    pub b1: f32,
    /// Feed-forward coefficient 2.
    pub b2: f32,
    /// Feedback coefficient 1 (normalised).
    pub a1: f32,
    /// Feedback coefficient 2 (normalised).
    pub a2: f32,
}

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

    /// Design a second-order Butterworth **low-pass** filter.
    ///
    /// # Arguments
    ///
    /// * `freq_hz` – Cutoff frequency in Hz.
    /// * `q` – Q factor (0.707 ≈ Butterworth; higher → resonant peak).
    /// * `sample_rate` – Sample rate in Hz.
    #[must_use]
    pub fn lowpass(freq_hz: f32, q: f32, sample_rate: f32) -> Self {
        let w0 = 2.0 * PI * freq_hz / sample_rate;
        let cos_w0 = w0.cos();
        let sin_w0 = w0.sin();
        let alpha = sin_w0 / (2.0 * q);

        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;

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

    /// Design a second-order Butterworth **high-pass** filter.
    ///
    /// # Arguments
    ///
    /// * `freq_hz` – Cutoff frequency in Hz.
    /// * `q` – Q factor.
    /// * `sample_rate` – Sample rate in Hz.
    #[must_use]
    pub fn highpass(freq_hz: f32, q: f32, sample_rate: f32) -> Self {
        let w0 = 2.0 * PI * freq_hz / sample_rate;
        let cos_w0 = w0.cos();
        let sin_w0 = w0.sin();
        let alpha = sin_w0 / (2.0 * q);

        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;

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

    /// Design a constant-skirt **band-pass** filter.
    ///
    /// The bandwidth `bw_hz` is the –3 dB bandwidth in Hz.
    ///
    /// # Arguments
    ///
    /// * `freq_hz` – Centre frequency in Hz.
    /// * `bw_hz` – Bandwidth in Hz.
    /// * `sample_rate` – Sample rate in Hz.
    #[must_use]
    pub fn bandpass(freq_hz: f32, bw_hz: f32, sample_rate: f32) -> Self {
        let w0 = 2.0 * PI * freq_hz / sample_rate;
        let cos_w0 = w0.cos();
        let sin_w0 = w0.sin();
        // Convert bandwidth in Hz to Q
        let q = freq_hz / bw_hz.max(1e-6);
        let alpha = sin_w0 / (2.0 * q);

        let b0 = alpha;
        let b1 = 0.0_f32;
        let b2 = -alpha;
        let a0 = 1.0 + alpha;
        let a1 = -2.0 * cos_w0;
        let a2 = 1.0 - alpha;

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

    /// Design a **peaking EQ** filter.
    ///
    /// # Arguments
    ///
    /// * `freq_hz` – Centre frequency in Hz.
    /// * `gain_db` – Gain at centre frequency in dB (positive = boost, negative = cut).
    /// * `q` – Q factor (bandwidth control).
    /// * `sample_rate` – Sample rate in Hz.
    #[must_use]
    pub fn peaking_eq(freq_hz: f32, gain_db: f32, q: f32, sample_rate: f32) -> Self {
        let w0 = 2.0 * PI * freq_hz / sample_rate;
        let cos_w0 = w0.cos();
        let sin_w0 = w0.sin();
        let alpha = sin_w0 / (2.0 * q);
        let a = 10.0_f32.powf(gain_db / 40.0);

        let b0 = 1.0 + alpha * a;
        let b1 = -2.0 * cos_w0;
        let b2 = 1.0 - alpha * a;
        let a0 = 1.0 + alpha / a;
        let a1 = -2.0 * cos_w0;
        let a2 = 1.0 - alpha / a;

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

/// Filter state (delay elements).
///
/// This struct stores the two previous input samples and two previous output
/// samples required by a direct-form-I biquad filter.
#[derive(Clone, Debug, Default)]
#[allow(dead_code)]
pub struct BiquadState {
    /// Previous input sample (x[n-1]).
    pub x1: f32,
    /// Input sample two steps back (x[n-2]).
    pub x2: f32,
    /// Previous output sample (y[n-1]).
    pub y1: f32,
    /// Output sample two steps back (y[n-2]).
    pub y2: f32,
}

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

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

/// A complete biquad filter: coefficients + state.
///
/// `BiquadFilter` combines [`BiquadCoeffs`] and [`BiquadState`] into a
/// single entity for convenient per-sample or block processing.
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub struct BiquadFilter {
    /// Filter coefficients.
    pub coeffs: BiquadCoeffs,
    /// Filter state (delay-line memory).
    pub state: BiquadState,
}

impl BiquadFilter {
    /// Create a new filter with the given coefficients and zeroed state.
    #[must_use]
    pub fn new(coeffs: BiquadCoeffs) -> Self {
        Self {
            coeffs,
            state: BiquadState::new(),
        }
    }

    /// Process a single sample and return the filtered output.
    pub fn process(&mut self, sample: f32) -> f32 {
        let output = self.coeffs.b0 * sample
            + self.coeffs.b1 * self.state.x1
            + self.coeffs.b2 * self.state.x2
            - self.coeffs.a1 * self.state.y1
            - self.coeffs.a2 * self.state.y2;

        self.state.x2 = self.state.x1;
        self.state.x1 = sample;
        self.state.y2 = self.state.y1;
        self.state.y1 = output;

        output
    }

    /// Process a block of samples, returning a new `Vec<f32>` with the filtered output.
    #[must_use]
    pub fn process_block(&mut self, samples: &[f32]) -> Vec<f32> {
        samples.iter().map(|&s| self.process(s)).collect()
    }

    /// Reset the filter state (delay-line memory) to zero.
    pub fn reset(&mut self) {
        self.state.reset();
    }

    /// Replace the filter coefficients (state is preserved).
    pub fn set_coeffs(&mut self, coeffs: BiquadCoeffs) {
        self.coeffs = coeffs;
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Unit tests
// ─────────────────────────────────────────────────────────────────────────────

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

    const SR: f32 = 48_000.0;

    // ── coefficient sanity ────────────────────────────────────────────────────

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

    #[test]
    fn test_identity_is_passthrough() {
        let mut f = BiquadFilter::new(BiquadCoeffs::identity());
        let sample = 0.75_f32;
        assert!((f.process(sample) - sample).abs() < 1e-7);
    }

    #[test]
    fn test_lowpass_coeffs_are_finite() {
        let c = BiquadCoeffs::lowpass(1000.0, 0.707, SR);
        assert!(c.b0.is_finite() && c.b1.is_finite() && c.b2.is_finite());
        assert!(c.a1.is_finite() && c.a2.is_finite());
    }

    #[test]
    fn test_highpass_coeffs_are_finite() {
        let c = BiquadCoeffs::highpass(1000.0, 0.707, SR);
        assert!(c.b0.is_finite() && c.b1.is_finite() && c.b2.is_finite());
        assert!(c.a1.is_finite() && c.a2.is_finite());
    }

    #[test]
    fn test_bandpass_b1_is_zero() {
        // Band-pass constant-skirt: b1 is always zero, b0 = -b2
        let c = BiquadCoeffs::bandpass(1000.0, 200.0, SR);
        assert!(c.b1.abs() < 1e-7, "b1 should be zero, got {}", c.b1);
        assert!(
            (c.b0 + c.b2).abs() < 1e-6,
            "b0 should equal -b2, b0={} b2={}",
            c.b0,
            c.b2
        );
    }

    #[test]
    fn test_peaking_eq_zero_gain_is_nearly_identity() {
        // A peaking EQ with 0 dB gain should be a unity-gain filter at DC
        let c = BiquadCoeffs::peaking_eq(1000.0, 0.0, 1.0, SR);
        let mut f = BiquadFilter::new(c);
        // Feed DC and wait for transient to settle
        let mut out = 0.0_f32;
        for _ in 0..500 {
            out = f.process(1.0);
        }
        assert!(
            (out - 1.0).abs() < 0.02,
            "0 dB peaking should be near unity at DC, got {out}"
        );
    }

    // ── state management ──────────────────────────────────────────────────────

    #[test]
    fn test_state_reset_clears_memory() {
        let mut f = BiquadFilter::new(BiquadCoeffs::lowpass(500.0, 0.707, SR));
        for _ in 0..100 {
            f.process(1.0);
        }
        f.reset();
        // After reset, feeding silence should give silence
        let out = f.process(0.0);
        assert_eq!(out, 0.0, "Output after reset+silence should be 0");
    }

    #[test]
    fn test_biquad_state_new_is_zeroed() {
        let s = BiquadState::new();
        assert_eq!(s.x1, 0.0);
        assert_eq!(s.x2, 0.0);
        assert_eq!(s.y1, 0.0);
        assert_eq!(s.y2, 0.0);
    }

    // ── frequency-domain behaviour ────────────────────────────────────────────

    #[test]
    fn test_lowpass_passes_dc() {
        let mut f = BiquadFilter::new(BiquadCoeffs::lowpass(4000.0, 0.707, SR));
        let mut out = 0.0_f32;
        for _ in 0..2000 {
            out = f.process(1.0);
        }
        assert!(out > 0.9, "Low-pass should pass DC; got {out}");
    }

    #[test]
    fn test_highpass_blocks_dc() {
        let mut f = BiquadFilter::new(BiquadCoeffs::highpass(1000.0, 0.707, SR));
        let mut out = 0.0_f32;
        for _ in 0..2000 {
            out = f.process(1.0);
        }
        assert!(out.abs() < 0.01, "High-pass should block DC; got {out}");
    }

    #[test]
    fn test_peaking_boost_amplifies_at_center() {
        // Peaking EQ +12 dB at 1 kHz: drive the filter with a 1 kHz sine at
        // 48 kHz sample-rate and confirm the output envelope is louder than input.
        let c = BiquadCoeffs::peaking_eq(1000.0, 12.0, 1.0, SR);
        let mut f = BiquadFilter::new(c);
        let freq = 1000.0_f32;
        let mut peak_in = 0.0_f32;
        let mut peak_out = 0.0_f32;
        for i in 0..4800_usize {
            let s = (2.0 * PI * freq * i as f32 / SR).sin();
            peak_in = peak_in.max(s.abs());
            let y = f.process(s);
            if i > 960 {
                // Skip transient
                peak_out = peak_out.max(y.abs());
            }
        }
        assert!(
            peak_out > peak_in * 1.5,
            "Peaking boost should increase amplitude; in={peak_in} out={peak_out}"
        );
    }

    #[test]
    fn test_peaking_cut_attenuates_at_center() {
        let c = BiquadCoeffs::peaking_eq(1000.0, -12.0, 1.0, SR);
        let mut f = BiquadFilter::new(c);
        let freq = 1000.0_f32;
        let mut peak_out = 0.0_f32;
        for i in 0..4800_usize {
            let s = (2.0 * PI * freq * i as f32 / SR).sin();
            let y = f.process(s);
            if i > 960 {
                peak_out = peak_out.max(y.abs());
            }
        }
        assert!(
            peak_out < 0.5,
            "Peaking cut should attenuate; peak_out={peak_out}"
        );
    }

    // ── block processing ──────────────────────────────────────────────────────

    #[test]
    fn test_process_block_matches_per_sample() {
        let c = BiquadCoeffs::lowpass(2000.0, 0.707, SR);
        let input: Vec<f32> = (0..128).map(|i| (i as f32 * 0.05).sin()).collect();

        let mut f1 = BiquadFilter::new(c.clone());
        let expected: Vec<f32> = input.iter().map(|&s| f1.process(s)).collect();

        let mut f2 = BiquadFilter::new(c);
        let got = f2.process_block(&input);

        for (e, g) in expected.iter().zip(got.iter()) {
            assert!((e - g).abs() < 1e-6, "Mismatch: expected {e} got {g}");
        }
    }

    #[test]
    fn test_process_block_output_length_matches_input() {
        let mut f = BiquadFilter::new(BiquadCoeffs::highpass(500.0, 0.707, SR));
        let input = vec![0.1_f32; 256];
        let output = f.process_block(&input);
        assert_eq!(output.len(), input.len());
    }

    #[test]
    fn test_process_block_all_finite() {
        let mut f = BiquadFilter::new(BiquadCoeffs::bandpass(1000.0, 200.0, SR));
        let input: Vec<f32> = (0..256).map(|i| (i as f32).sin()).collect();
        let output = f.process_block(&input);
        assert!(
            output.iter().all(|x| x.is_finite()),
            "All outputs must be finite"
        );
    }

    // ── set_coeffs ────────────────────────────────────────────────────────────

    #[test]
    fn test_set_coeffs_updates_filter() {
        let mut f = BiquadFilter::new(BiquadCoeffs::identity());
        // The identity filter should pass DC unchanged
        let out_before = f.process(1.0);
        assert!((out_before - 1.0).abs() < 1e-7);

        // Now switch to a high-pass that blocks DC
        f.set_coeffs(BiquadCoeffs::highpass(4000.0, 0.707, SR));
        f.reset();
        let mut out = 0.0_f32;
        for _ in 0..2000 {
            out = f.process(1.0);
        }
        assert!(
            out.abs() < 0.05,
            "HP should block DC after set_coeffs; got {out}"
        );
    }
}