oximedia-effects 0.1.8

Professional audio effects suite 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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! Parametric equaliser using cascaded biquad filters with f64 internal precision.
//!
//! This module provides a [`ParametricEq`] that implements the [`AudioEffect`]
//! trait, enabling drop-in use within OxiMedia effect chains.  Each band is a
//! second-order IIR biquad filter whose coefficients follow the Robert
//! Bristow-Johnson *Audio EQ Cookbook* formulas.
//!
//! Internal processing uses `f64` to maintain numerical stability when many
//! bands are cascaded.
//!
//! # Supported band types
//!
//! | Type | Description |
//! |------|-------------|
//! | [`BandType::LowShelf`]  | Boost/cut below frequency |
//! | [`BandType::HighShelf`] | Boost/cut above frequency |
//! | [`BandType::Peaking`]   | Bell-curve boost/cut around frequency |
//! | [`BandType::Notch`]     | Narrow band-reject |
//! | [`BandType::LowPass`]   | Second-order Butterworth low-pass |
//! | [`BandType::HighPass`]  | Second-order Butterworth high-pass |
//! | [`BandType::BandPass`]  | Constant-skirt-gain band-pass |
//! | [`BandType::AllPass`]   | Unity magnitude, phase-shift only |
//!
//! # Example
//!
//! ```
//! use oximedia_effects::parametric_eq::{ParametricEq, EqBand, BandType};
//! use oximedia_effects::AudioEffect;
//!
//! let mut eq = ParametricEq::new(48_000.0);
//! eq.add_band(EqBand {
//!     band_type: BandType::Peaking,
//!     frequency: 1000.0,
//!     gain_db: 6.0,
//!     q: 1.0,
//!     enabled: true,
//! });
//!
//! let out = eq.process_sample(0.5);
//! assert!(out.is_finite());
//! ```

#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_possible_truncation)]

use crate::AudioEffect;
use std::f64::consts::PI;

// ═══════════════════════════════════════════════════════════ BandType ══════

/// Band type for parametric EQ.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BandType {
    /// Boost or cut below the cutoff frequency.
    LowShelf,
    /// Boost or cut above the cutoff frequency.
    HighShelf,
    /// Bell-curve boost or cut around a centre frequency.
    Peaking,
    /// Narrow band-reject filter.
    Notch,
    /// Second-order Butterworth low-pass filter.
    LowPass,
    /// Second-order Butterworth high-pass filter.
    HighPass,
    /// Constant-skirt-gain band-pass filter.
    BandPass,
    /// Unity-gain all-pass filter (phase shift only).
    AllPass,
}

// ═══════════════════════════════════════════════════════════ EqBand ════════

/// A single EQ band configuration.
#[derive(Debug, Clone)]
pub struct EqBand {
    /// Filter shape / type.
    pub band_type: BandType,
    /// Centre or cutoff frequency in Hz.
    pub frequency: f32,
    /// Gain in dB (positive = boost, negative = cut).
    /// Only meaningful for [`BandType::Peaking`], [`BandType::LowShelf`],
    /// and [`BandType::HighShelf`].
    pub gain_db: f32,
    /// Quality factor controlling bandwidth.
    pub q: f32,
    /// Whether this band is active. A disabled band passes the signal
    /// unchanged without consuming CPU on coefficient computation.
    pub enabled: bool,
}

impl EqBand {
    /// Create a peaking (bell) EQ band.
    #[must_use]
    pub fn peaking(frequency: f32, gain_db: f32, q: f32) -> Self {
        Self {
            band_type: BandType::Peaking,
            frequency,
            gain_db,
            q,
            enabled: true,
        }
    }

    /// Create a low-shelf band with Butterworth Q.
    #[must_use]
    pub fn low_shelf(frequency: f32, gain_db: f32) -> Self {
        Self {
            band_type: BandType::LowShelf,
            frequency,
            gain_db,
            q: 0.707,
            enabled: true,
        }
    }

    /// Create a high-shelf band with Butterworth Q.
    #[must_use]
    pub fn high_shelf(frequency: f32, gain_db: f32) -> Self {
        Self {
            band_type: BandType::HighShelf,
            frequency,
            gain_db,
            q: 0.707,
            enabled: true,
        }
    }

    /// Create a notch (band-reject) band.
    #[must_use]
    pub fn notch(frequency: f32, q: f32) -> Self {
        Self {
            band_type: BandType::Notch,
            frequency,
            gain_db: 0.0,
            q,
            enabled: true,
        }
    }

    /// Create a low-pass filter band.
    #[must_use]
    pub fn low_pass(frequency: f32, q: f32) -> Self {
        Self {
            band_type: BandType::LowPass,
            frequency,
            gain_db: 0.0,
            q,
            enabled: true,
        }
    }

    /// Create a high-pass filter band.
    #[must_use]
    pub fn high_pass(frequency: f32, q: f32) -> Self {
        Self {
            band_type: BandType::HighPass,
            frequency,
            gain_db: 0.0,
            q,
            enabled: true,
        }
    }

    /// Create a band-pass filter band.
    #[must_use]
    pub fn band_pass(frequency: f32, q: f32) -> Self {
        Self {
            band_type: BandType::BandPass,
            frequency,
            gain_db: 0.0,
            q,
            enabled: true,
        }
    }

    /// Create an all-pass filter band.
    #[must_use]
    pub fn all_pass(frequency: f32, q: f32) -> Self {
        Self {
            band_type: BandType::AllPass,
            frequency,
            gain_db: 0.0,
            q,
            enabled: true,
        }
    }
}

// ═══════════════════════════════════════════════════════ BiquadCoeffs ═════

/// Biquad filter coefficients (normalised by a0).
///
/// Transfer function:
/// `H(z) = (b0 + b1*z^-1 + b2*z^-2) / (1 + a1*z^-1 + a2*z^-2)`
#[derive(Debug, Clone)]
pub struct BiquadCoeffs {
    /// Feed-forward coefficient b0.
    pub b0: f64,
    /// Feed-forward coefficient b1.
    pub b1: f64,
    /// Feed-forward coefficient b2.
    pub b2: f64,
    /// Feedback coefficient a1.
    pub a1: f64,
    /// Feedback coefficient a2.
    pub a2: f64,
}

impl Default for BiquadCoeffs {
    /// Identity (pass-through) coefficients.
    fn default() -> Self {
        Self {
            b0: 1.0,
            b1: 0.0,
            b2: 0.0,
            a1: 0.0,
            a2: 0.0,
        }
    }
}

// ═══════════════════════════════════════════════════════ BiquadState ══════

/// Biquad filter state (Direct Form II Transposed).
///
/// Uses two delay elements `z1` and `z2`.
#[derive(Debug, Clone)]
pub struct BiquadState {
    /// First delay element.
    pub z1: f64,
    /// Second delay element.
    pub z2: f64,
}

impl Default for BiquadState {
    fn default() -> Self {
        Self { z1: 0.0, z2: 0.0 }
    }
}

// ═══════════════════════════════════════════════════════ ParametricEq ═════

/// Parametric equaliser with configurable bands and f64 internal precision.
///
/// Implements [`AudioEffect`] so it can be used directly in effect chains,
/// wrapped with [`MixEffect`](crate::mix::MixEffect), or composed with other
/// effects.
pub struct ParametricEq {
    bands: Vec<EqBand>,
    coeffs: Vec<BiquadCoeffs>,
    states: Vec<BiquadState>,
    sample_rate: f32,
}

impl ParametricEq {
    /// Create a new empty parametric EQ at the given sample rate.
    #[must_use]
    pub fn new(sample_rate: f32) -> Self {
        Self {
            bands: Vec::new(),
            coeffs: Vec::new(),
            states: Vec::new(),
            sample_rate,
        }
    }

    /// Add a band and compute its biquad coefficients.
    pub fn add_band(&mut self, band: EqBand) {
        let c = Self::compute_coefficients(&band, self.sample_rate);
        self.coeffs.push(c);
        self.states.push(BiquadState::default());
        self.bands.push(band);
    }

    /// Replace a band at `index` and recompute its coefficients.
    ///
    /// Returns `Err` if `index` is out of range.
    pub fn set_band(&mut self, index: usize, band: EqBand) -> Result<(), String> {
        if index >= self.bands.len() {
            return Err(format!(
                "Band index {index} out of range (have {} bands)",
                self.bands.len()
            ));
        }
        let c = Self::compute_coefficients(&band, self.sample_rate);
        self.coeffs[index] = c;
        self.states[index] = BiquadState::default();
        self.bands[index] = band;
        Ok(())
    }

    /// Remove a band by index.
    ///
    /// Returns `Err` if `index` is out of range.
    pub fn remove_band(&mut self, index: usize) -> Result<EqBand, String> {
        if index >= self.bands.len() {
            return Err(format!(
                "Band index {index} out of range (have {} bands)",
                self.bands.len()
            ));
        }
        self.coeffs.remove(index);
        self.states.remove(index);
        Ok(self.bands.remove(index))
    }

    /// Return the number of bands.
    #[must_use]
    pub fn band_count(&self) -> usize {
        self.bands.len()
    }

    /// Get an immutable reference to a band by index.
    #[must_use]
    pub fn band(&self, index: usize) -> Option<&EqBand> {
        self.bands.get(index)
    }

    /// Process a buffer of samples through all enabled bands (in-place).
    pub fn process_samples(&mut self, samples: &mut [f32]) {
        for sample in samples.iter_mut() {
            let mut s = f64::from(*sample);
            for (i, band) in self.bands.iter().enumerate() {
                if band.enabled {
                    s = Self::process_biquad(s, &self.coeffs[i], &mut self.states[i]);
                }
            }
            *sample = s as f32;
        }
    }

    /// Reset all filter states (for seek / discontinuity).
    pub fn reset_states(&mut self) {
        for state in &mut self.states {
            state.z1 = 0.0;
            state.z2 = 0.0;
        }
    }

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

    /// Builder helper — append a band and return `self`.
    #[must_use]
    pub fn with_band(mut self, band: EqBand) -> Self {
        self.add_band(band);
        self
    }

    // ────────────────────────────────────── coefficient computation ──────

    /// Compute normalised biquad coefficients for a band.
    ///
    /// Uses the Robert Bristow-Johnson *Audio EQ Cookbook* formulas.
    fn compute_coefficients(band: &EqBand, sample_rate: f32) -> BiquadCoeffs {
        let sr = f64::from(sample_rate);
        let freq = f64::from(band.frequency);
        let gain = f64::from(band.gain_db);
        let q = f64::from(band.q).max(f64::EPSILON);

        let w0 = 2.0 * PI * freq / 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 band.band_type {
            BandType::Peaking => {
                let a = 10.0_f64.powf(gain / 40.0);
                (
                    1.0 + alpha * a,
                    -2.0 * cos_w0,
                    1.0 - alpha * a,
                    1.0 + alpha / a,
                    -2.0 * cos_w0,
                    1.0 - alpha / a,
                )
            }
            BandType::LowShelf => {
                let a = 10.0_f64.powf(gain / 40.0);
                let sqrt_a = a.sqrt();
                (
                    a * ((a + 1.0) - (a - 1.0) * cos_w0 + 2.0 * sqrt_a * alpha),
                    2.0 * a * ((a - 1.0) - (a + 1.0) * cos_w0),
                    a * ((a + 1.0) - (a - 1.0) * cos_w0 - 2.0 * sqrt_a * alpha),
                    (a + 1.0) + (a - 1.0) * cos_w0 + 2.0 * sqrt_a * alpha,
                    -2.0 * ((a - 1.0) + (a + 1.0) * cos_w0),
                    (a + 1.0) + (a - 1.0) * cos_w0 - 2.0 * sqrt_a * alpha,
                )
            }
            BandType::HighShelf => {
                let a = 10.0_f64.powf(gain / 40.0);
                let sqrt_a = a.sqrt();
                (
                    a * ((a + 1.0) + (a - 1.0) * cos_w0 + 2.0 * sqrt_a * alpha),
                    -2.0 * a * ((a - 1.0) + (a + 1.0) * cos_w0),
                    a * ((a + 1.0) + (a - 1.0) * cos_w0 - 2.0 * sqrt_a * alpha),
                    (a + 1.0) - (a - 1.0) * cos_w0 + 2.0 * sqrt_a * alpha,
                    2.0 * ((a - 1.0) - (a + 1.0) * cos_w0),
                    (a + 1.0) - (a - 1.0) * cos_w0 - 2.0 * sqrt_a * alpha,
                )
            }
            BandType::LowPass => (
                (1.0 - cos_w0) / 2.0,
                1.0 - cos_w0,
                (1.0 - cos_w0) / 2.0,
                1.0 + alpha,
                -2.0 * cos_w0,
                1.0 - alpha,
            ),
            BandType::HighPass => (
                (1.0 + cos_w0) / 2.0,
                -(1.0 + cos_w0),
                (1.0 + cos_w0) / 2.0,
                1.0 + alpha,
                -2.0 * cos_w0,
                1.0 - alpha,
            ),
            BandType::Notch => (
                1.0,
                -2.0 * cos_w0,
                1.0,
                1.0 + alpha,
                -2.0 * cos_w0,
                1.0 - alpha,
            ),
            BandType::BandPass => (alpha, 0.0, -alpha, 1.0 + alpha, -2.0 * cos_w0, 1.0 - alpha),
            BandType::AllPass => (
                1.0 - alpha,
                -2.0 * cos_w0,
                1.0 + alpha,
                1.0 + alpha,
                -2.0 * cos_w0,
                1.0 - alpha,
            ),
        };

        // Normalise by a0
        let inv_a0 = if a0.abs() < f64::EPSILON {
            1.0
        } else {
            1.0 / a0
        };

        BiquadCoeffs {
            b0: b0 * inv_a0,
            b1: b1 * inv_a0,
            b2: b2 * inv_a0,
            a1: a1 * inv_a0,
            a2: a2 * inv_a0,
        }
    }

    /// Process a single sample through one biquad stage (Direct Form II Transposed).
    #[inline]
    fn process_biquad(sample: f64, coeffs: &BiquadCoeffs, state: &mut BiquadState) -> f64 {
        let output = coeffs.b0 * sample + state.z1;
        state.z1 = coeffs.b1 * sample - coeffs.a1 * output + state.z2;
        state.z2 = coeffs.b2 * sample - coeffs.a2 * output;
        output
    }
}

// ═══════════════════════════════════════════════════ AudioEffect impl ═════

impl AudioEffect for ParametricEq {
    const EFFECT_ID: &'static str = "parametric_eq";

    fn process_sample(&mut self, input: f32) -> f32 {
        let mut s = f64::from(input);
        for (i, band) in self.bands.iter().enumerate() {
            if band.enabled {
                s = Self::process_biquad(s, &self.coeffs[i], &mut self.states[i]);
            }
        }
        s as f32
    }

    fn process(&mut self, buffer: &mut [f32]) {
        self.process_samples(buffer);
    }

    fn reset(&mut self) {
        self.reset_states();
    }

    fn latency_samples(&self) -> usize {
        0
    }

    fn set_sample_rate(&mut self, sample_rate: f32) {
        self.sample_rate = sample_rate;
        // Recompute all coefficients for the new sample rate
        for (i, band) in self.bands.iter().enumerate() {
            self.coeffs[i] = Self::compute_coefficients(band, sample_rate);
        }
        self.reset_states();
    }
}

// ═══════════════════════════════════════════════════════════ tests ════════

#[cfg(test)]
mod tests {
    use super::*;
    use std::f32::consts::PI as PI32;

    const SR: f32 = 48_000.0;

    fn make_sine(freq: f32, sample_rate: f32, n: usize) -> Vec<f32> {
        (0..n)
            .map(|i| (2.0 * PI32 * freq * i as f32 / sample_rate).sin())
            .collect()
    }

    fn rms(buf: &[f32]) -> f32 {
        if buf.is_empty() {
            return 0.0;
        }
        (buf.iter().map(|&s| s * s).sum::<f32>() / buf.len() as f32).sqrt()
    }

    // ── basic structure ──────────────────────────────────────────────────

    #[test]
    fn test_eq_new_no_bands() {
        let eq = ParametricEq::new(SR);
        assert_eq!(eq.band_count(), 0);
    }

    #[test]
    fn test_eq_add_band() {
        let mut eq = ParametricEq::new(SR);
        eq.add_band(EqBand::peaking(1000.0, 6.0, 1.0));
        assert_eq!(eq.band_count(), 1);
        eq.add_band(EqBand::low_shelf(200.0, 3.0));
        assert_eq!(eq.band_count(), 2);
    }

    // ── passthrough / bypass ─────────────────────────────────────────────

    #[test]
    fn test_eq_flat_passthrough() {
        // A peaking band with gain = 0 dB should not modify the signal.
        let mut eq = ParametricEq::new(SR).with_band(EqBand::peaking(1000.0, 0.0, 1.0));
        let input = make_sine(1000.0, SR, 512);
        let output: Vec<f32> = input.iter().map(|&s| eq.process_sample(s)).collect();
        for (i, (&a, &b)) in input.iter().zip(output.iter()).enumerate() {
            assert!(
                (a - b).abs() < 1e-5,
                "Flat EQ should pass through unchanged, sample {i}: in={a}, out={b}"
            );
        }
    }

    // ── low-pass attenuates high ─────────────────────────────────────────

    #[test]
    fn test_eq_low_pass_attenuates_high() {
        let mut eq = ParametricEq::new(SR).with_band(EqBand::low_pass(1000.0, 0.707));
        // Settle the filter
        let settle = make_sine(10_000.0, SR, 4096);
        for &s in &settle {
            eq.process_sample(s);
        }
        let input = make_sine(10_000.0, SR, 1024);
        let output: Vec<f32> = input.iter().map(|&s| eq.process_sample(s)).collect();
        let in_rms = rms(&input);
        let out_rms = rms(&output);
        assert!(
            out_rms < in_rms * 0.5,
            "LPF at 1 kHz should attenuate 10 kHz: in_rms={in_rms:.4}, out_rms={out_rms:.4}"
        );
    }

    // ── high-pass attenuates low ─────────────────────────────────────────

    #[test]
    fn test_eq_high_pass_attenuates_low() {
        let mut eq = ParametricEq::new(SR).with_band(EqBand::high_pass(1000.0, 0.707));
        // Settle
        let settle = make_sine(100.0, SR, 4096);
        for &s in &settle {
            eq.process_sample(s);
        }
        let input = make_sine(100.0, SR, 1024);
        let output: Vec<f32> = input.iter().map(|&s| eq.process_sample(s)).collect();
        let in_rms = rms(&input);
        let out_rms = rms(&output);
        assert!(
            out_rms < in_rms * 0.5,
            "HPF at 1 kHz should attenuate 100 Hz: in_rms={in_rms:.4}, out_rms={out_rms:.4}"
        );
    }

    // ── peaking boost ────────────────────────────────────────────────────

    #[test]
    fn test_eq_peaking_boost_at_center() {
        let mut eq = ParametricEq::new(SR).with_band(EqBand::peaking(1000.0, 12.0, 1.0));
        // Settle
        let settle = make_sine(1000.0, SR, 4096);
        for &s in &settle {
            eq.process_sample(s);
        }
        let input = make_sine(1000.0, SR, 1024);
        let output: Vec<f32> = input.iter().map(|&s| eq.process_sample(s)).collect();
        assert!(
            rms(&output) > rms(&input),
            "Peaking +12 dB at 1 kHz should boost: in={:.4}, out={:.4}",
            rms(&input),
            rms(&output)
        );
    }

    // ── notch attenuates ─────────────────────────────────────────────────

    #[test]
    fn test_eq_notch_attenuates_center() {
        let mut eq = ParametricEq::new(SR).with_band(EqBand::notch(1000.0, 1.0));
        // Settle with wider Q for deeper attenuation
        let settle = make_sine(1000.0, SR, 8192);
        for &s in &settle {
            eq.process_sample(s);
        }
        let input = make_sine(1000.0, SR, 1024);
        let output: Vec<f32> = input.iter().map(|&s| eq.process_sample(s)).collect();
        assert!(
            rms(&output) < rms(&input) * 0.8,
            "Notch at 1 kHz should reduce RMS: in={:.4}, out={:.4}",
            rms(&input),
            rms(&output)
        );
    }

    // ── reset ────────────────────────────────────────────────────────────

    #[test]
    fn test_eq_reset_clears_state() {
        let mut eq = ParametricEq::new(SR).with_band(EqBand::peaking(1000.0, 6.0, 1.0));
        // Prime with non-zero signal
        for &s in &make_sine(1000.0, SR, 256) {
            eq.process_sample(s);
        }
        eq.reset();
        // After reset, processing silence should give silence
        let out = eq.process_sample(0.0);
        assert!(
            out.abs() < 1e-10,
            "After reset, zero input should produce zero output, got {out}"
        );
    }

    // ── remove band ──────────────────────────────────────────────────────

    #[test]
    fn test_eq_remove_band() {
        let mut eq = ParametricEq::new(SR);
        eq.add_band(EqBand::peaking(500.0, 3.0, 1.0));
        eq.add_band(EqBand::notch(2000.0, 5.0));
        assert_eq!(eq.band_count(), 2);
        let removed = eq.remove_band(0);
        assert!(removed.is_ok());
        assert_eq!(eq.band_count(), 1);
        // Remaining band should be the notch
        assert_eq!(eq.band(0).map(|b| b.band_type), Some(BandType::Notch));
    }

    // ── disabled band bypassed ───────────────────────────────────────────

    #[test]
    fn test_eq_disabled_band_bypassed() {
        let band = EqBand {
            band_type: BandType::Peaking,
            frequency: 1000.0,
            gain_db: 20.0,
            q: 1.0,
            enabled: false,
        };
        let mut eq = ParametricEq::new(SR).with_band(band);
        let input = make_sine(1000.0, SR, 256);
        let output: Vec<f32> = input.iter().map(|&s| eq.process_sample(s)).collect();
        for (i, (&a, &b)) in input.iter().zip(output.iter()).enumerate() {
            assert!(
                (a - b).abs() < 1e-6,
                "Disabled band should pass through, sample {i}: in={a}, out={b}"
            );
        }
    }
}