oximedia-effects 0.1.6

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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
//! Dynamics compression effects.
//!
//! Provides professional compressor, limiter, and expander with industry-standard
//! gain computer and level detector designs. Includes sidechain input support
//! for frequency-conscious compression and ducking applications.

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

/// Compressor configuration parameters.
#[derive(Debug, Clone)]
pub struct CompressorConfig {
    /// Threshold in dB above which compression begins.
    pub threshold_db: f32,
    /// Compression ratio (e.g. 4.0 = 4:1).
    pub ratio: f32,
    /// Attack time in milliseconds.
    pub attack_ms: f32,
    /// Release time in milliseconds.
    pub release_ms: f32,
    /// Knee width in dB (0 = hard knee).
    pub knee_db: f32,
    /// Makeup gain in dB applied after compression.
    pub makeup_gain_db: f32,
}

impl CompressorConfig {
    /// Standard general-purpose compressor (4:1 ratio, moderate attack/release).
    #[must_use]
    pub fn standard() -> Self {
        Self {
            threshold_db: -18.0,
            ratio: 4.0,
            attack_ms: 10.0,
            release_ms: 100.0,
            knee_db: 6.0,
            makeup_gain_db: 3.0,
        }
    }

    /// Limiting configuration (100:1 ratio, very fast attack).
    #[must_use]
    pub fn limiting() -> Self {
        Self {
            threshold_db: -3.0,
            ratio: 100.0,
            attack_ms: 0.1,
            release_ms: 50.0,
            knee_db: 0.0,
            makeup_gain_db: 0.0,
        }
    }

    /// Vocal compressor preset (gentle 3:1 ratio).
    #[must_use]
    pub fn vocal() -> Self {
        Self {
            threshold_db: -20.0,
            ratio: 3.0,
            attack_ms: 5.0,
            release_ms: 80.0,
            knee_db: 8.0,
            makeup_gain_db: 4.0,
        }
    }
}

impl Default for CompressorConfig {
    fn default() -> Self {
        Self::standard()
    }
}

/// Peak level detector with attack/release envelopes.
pub struct LevelDetector {
    /// Current peak level.
    pub peak_level: f32,
}

impl LevelDetector {
    /// Create a new level detector.
    #[must_use]
    pub fn new() -> Self {
        Self { peak_level: 0.0 }
    }

    /// Process a single sample and return the envelope level.
    ///
    /// # Arguments
    ///
    /// * `x` - Input sample (absolute value used)
    /// * `attack` - Attack coefficient (0..1), computed as `1 - exp(-2.2 / (attack_ms * sr / 1000))`
    /// * `release` - Release coefficient (0..1)
    pub fn process(&mut self, x: f32, attack: f32, release: f32) -> f32 {
        let input_level = x.abs();
        if input_level > self.peak_level {
            self.peak_level += attack * (input_level - self.peak_level);
        } else {
            self.peak_level += release * (input_level - self.peak_level);
        }
        self.peak_level
    }

    /// Reset the detector state.
    pub fn reset(&mut self) {
        self.peak_level = 0.0;
    }
}

impl Default for LevelDetector {
    fn default() -> Self {
        Self::new()
    }
}

/// Gain computer state that implements the compression curve.
pub struct GainComputerState {
    /// Last computed gain reduction in dB.
    pub last_gain_reduction_db: f32,
}

impl GainComputerState {
    /// Create a new gain computer.
    #[must_use]
    pub fn new() -> Self {
        Self {
            last_gain_reduction_db: 0.0,
        }
    }

    /// Compute gain reduction in dB for the given input level.
    ///
    /// Implements soft-knee compression curve from AES guidelines.
    pub fn compute_gain(&mut self, input_db: f32, config: &CompressorConfig) -> f32 {
        let threshold = config.threshold_db;
        let ratio = config.ratio;
        let knee = config.knee_db;
        let half_knee = knee / 2.0;

        let gain_reduction_db =
            if knee > 0.0 && input_db >= threshold - half_knee && input_db <= threshold + half_knee
            {
                // Soft knee region: smooth transition
                let knee_input = input_db - threshold + half_knee;
                let knee_factor = knee_input / knee;
                // Soft knee formula: gain_reduction = (1/R - 1) * (input - threshold + knee/2)^2 / (2*knee)
                (1.0 / ratio - 1.0) * (knee_factor * knee_input) / 2.0
            } else if input_db > threshold + half_knee {
                // Above threshold: apply ratio
                (input_db - threshold) * (1.0 / ratio - 1.0)
            } else {
                // Below threshold: no gain reduction
                0.0
            };

        self.last_gain_reduction_db = gain_reduction_db;
        gain_reduction_db
    }
}

impl Default for GainComputerState {
    fn default() -> Self {
        Self::new()
    }
}

/// Gain reduction tracking for metering.
#[derive(Debug, Clone, Default)]
pub struct GainReduction {
    /// Peak gain reduction observed in dB (positive = reduction).
    pub peak_db: f32,
    /// RMS gain reduction in dB over a measurement window.
    pub rms_db: f32,
    /// Accumulator for RMS computation.
    accumulator: f32,
    /// Sample count for RMS window.
    sample_count: u32,
    /// Window size for RMS.
    window_size: u32,
}

impl GainReduction {
    /// Create a new gain reduction tracker.
    #[must_use]
    pub fn new(window_size: u32) -> Self {
        Self {
            window_size,
            ..Default::default()
        }
    }

    /// Update with a new gain reduction value (in dB, positive = reduction).
    pub fn update(&mut self, reduction_db: f32) {
        let abs_reduction = reduction_db.abs();
        if abs_reduction > self.peak_db {
            self.peak_db = abs_reduction;
        }
        self.accumulator += abs_reduction * abs_reduction;
        self.sample_count += 1;
        if self.sample_count >= self.window_size {
            self.rms_db = (self.accumulator / self.window_size as f32).sqrt();
            self.accumulator = 0.0;
            self.sample_count = 0;
        }
    }

    /// Reset peak reading.
    pub fn reset_peak(&mut self) {
        self.peak_db = 0.0;
    }
}

/// Sidechain filter type for frequency-conscious compression.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SidechainFilter {
    /// No filtering on sidechain signal.
    None,
    /// High-pass filter at given frequency (Hz) for de-essing / sibilance detection.
    HighPass(f32),
    /// Low-pass filter at given frequency (Hz) for bass-focused compression.
    LowPass(f32),
    /// Band-pass filter centered at given frequency (Hz) with Q factor.
    BandPass(f32, f32),
}

/// One-pole filter state for sidechain filtering.
#[derive(Debug, Clone)]
struct OnePoleFilter {
    /// Previous output sample.
    prev: f32,
    /// Filter coefficient.
    coeff: f32,
    /// Filter mode.
    is_highpass: bool,
}

impl OnePoleFilter {
    fn new_highpass(cutoff_hz: f32, sample_rate: f32) -> Self {
        let rc = 1.0 / (2.0 * std::f32::consts::PI * cutoff_hz);
        let dt = 1.0 / sample_rate;
        let coeff = rc / (rc + dt);
        Self {
            prev: 0.0,
            coeff,
            is_highpass: true,
        }
    }

    fn new_lowpass(cutoff_hz: f32, sample_rate: f32) -> Self {
        let rc = 1.0 / (2.0 * std::f32::consts::PI * cutoff_hz);
        let dt = 1.0 / sample_rate;
        let coeff = dt / (rc + dt);
        Self {
            prev: 0.0,
            coeff,
            is_highpass: false,
        }
    }

    fn process(&mut self, input: f32) -> f32 {
        if self.is_highpass {
            let _output = self.coeff * (self.prev + input - self.prev);
            // Simple high-pass: output = coeff * (prev_output + input - prev_input)
            // Use a simplified version:
            let hp_output = self.coeff * self.prev + self.coeff * (input - self.prev);
            self.prev = input;
            hp_output
        } else {
            self.prev += self.coeff * (input - self.prev);
            self.prev
        }
    }

    fn reset(&mut self) {
        self.prev = 0.0;
    }
}

/// Professional dynamics compressor.
pub struct Compressor {
    config: CompressorConfig,
    detector: LevelDetector,
    gain_computer: GainComputerState,
    /// Current gain reduction in linear.
    gain_reduction_linear: f32,
    /// Gain reduction metering.
    pub gain_reduction: GainReduction,
    /// Smoothed gain reduction in dB (for smooth ballistics).
    smoothed_gr_db: f32,
    /// Optional sidechain filter.
    sidechain_filter_type: SidechainFilter,
    /// Sidechain filter state.
    sc_filter: Option<OnePoleFilter>,
}

impl Compressor {
    /// Create a new compressor with the given configuration and sample rate.
    #[must_use]
    pub fn new(config: CompressorConfig, _sample_rate: u32) -> Self {
        Self {
            config,
            detector: LevelDetector::new(),
            gain_computer: GainComputerState::new(),
            gain_reduction_linear: 1.0,
            gain_reduction: GainReduction::new(4800),
            smoothed_gr_db: 0.0,
            sidechain_filter_type: SidechainFilter::None,
            sc_filter: None,
        }
    }

    /// Set a sidechain filter for frequency-conscious compression.
    ///
    /// When set, the filter is applied to the sidechain signal before
    /// level detection. This enables:
    /// - **De-essing**: `SidechainFilter::HighPass(4000.0)` to detect sibilance
    /// - **Bass compression**: `SidechainFilter::LowPass(200.0)` for bass-only triggering
    pub fn set_sidechain_filter(&mut self, filter: SidechainFilter, sample_rate: u32) {
        self.sidechain_filter_type = filter;
        #[allow(clippy::cast_precision_loss)]
        let sr = sample_rate as f32;
        self.sc_filter = match filter {
            SidechainFilter::None => None,
            SidechainFilter::HighPass(freq) => Some(OnePoleFilter::new_highpass(freq, sr)),
            SidechainFilter::LowPass(freq) => Some(OnePoleFilter::new_lowpass(freq, sr)),
            SidechainFilter::BandPass(freq, _q) => {
                // Approximate band-pass as cascaded HP + LP
                // For a proper implementation, use a biquad; this is a reasonable approximation
                Some(OnePoleFilter::new_highpass(freq * 0.7, sr))
            }
        };
    }

    /// Get the current sidechain filter type.
    #[must_use]
    pub fn sidechain_filter(&self) -> SidechainFilter {
        self.sidechain_filter_type
    }

    fn db_to_linear(db: f32) -> f32 {
        10.0_f32.powf(db / 20.0)
    }

    fn linear_to_db(linear: f32) -> f32 {
        20.0 * linear.max(1e-10_f32).log10()
    }

    fn attack_coeff(attack_ms: f32, sample_rate: u32) -> f32 {
        let attack_samples = attack_ms * sample_rate as f32 / 1000.0;
        if attack_samples > 0.0 {
            1.0 - (-2.2_f32 / attack_samples).exp()
        } else {
            1.0
        }
    }

    fn release_coeff(release_ms: f32, sample_rate: u32) -> f32 {
        let release_samples = release_ms * sample_rate as f32 / 1000.0;
        if release_samples > 0.0 {
            1.0 - (-2.2_f32 / release_samples).exp()
        } else {
            1.0
        }
    }

    /// Process a buffer of samples and return compressed output.
    #[must_use]
    pub fn process(&mut self, samples: &[f32], sample_rate: u32) -> Vec<f32> {
        let attack = Self::attack_coeff(self.config.attack_ms, sample_rate);
        let release = Self::release_coeff(self.config.release_ms, sample_rate);
        let makeup = Self::db_to_linear(self.config.makeup_gain_db);

        // Ballistics: smooth the gain reduction itself
        let gr_attack = Self::attack_coeff(self.config.attack_ms, sample_rate);
        let gr_release = Self::release_coeff(self.config.release_ms, sample_rate);

        samples
            .iter()
            .map(|&x| {
                // Detect level
                let level = self.detector.process(x, attack, release);
                let level_db = Self::linear_to_db(level);

                // Compute gain reduction
                let gr_db = self.gain_computer.compute_gain(level_db, &self.config);

                // Smooth gain reduction (ballistics on gain signal)
                if gr_db < self.smoothed_gr_db {
                    // Attack: gain goes down (more reduction)
                    self.smoothed_gr_db += gr_attack * (gr_db - self.smoothed_gr_db);
                } else {
                    // Release: gain comes back up
                    self.smoothed_gr_db += gr_release * (gr_db - self.smoothed_gr_db);
                }

                self.gain_reduction_linear = Self::db_to_linear(self.smoothed_gr_db);
                self.gain_reduction.update(self.smoothed_gr_db);

                x * self.gain_reduction_linear * makeup
            })
            .collect()
    }

    /// Process with external sidechain input.
    ///
    /// The compressor uses `sidechain` for level detection but applies
    /// gain reduction to the `input` signal. This is useful for:
    /// - De-essing (sidechain is EQ'd high-frequency band)
    /// - Ducking (sidechain is voiceover, input is music)
    /// - Frequency-conscious compression (sidechain is filtered version)
    ///
    /// Both buffers must be the same length.
    #[must_use]
    pub fn process_sidechain(
        &mut self,
        input: &[f32],
        sidechain: &[f32],
        sample_rate: u32,
    ) -> Vec<f32> {
        let len = input.len().min(sidechain.len());
        let attack = Self::attack_coeff(self.config.attack_ms, sample_rate);
        let release = Self::release_coeff(self.config.release_ms, sample_rate);
        let makeup = Self::db_to_linear(self.config.makeup_gain_db);
        let gr_attack = Self::attack_coeff(self.config.attack_ms, sample_rate);
        let gr_release = Self::release_coeff(self.config.release_ms, sample_rate);

        let mut output = Vec::with_capacity(len);

        for i in 0..len {
            // Optionally filter the sidechain signal
            let sc_sample = if let Some(ref mut filter) = self.sc_filter {
                filter.process(sidechain[i])
            } else {
                sidechain[i]
            };

            // Detect level from SIDECHAIN signal
            let level = self.detector.process(sc_sample, attack, release);
            let level_db = Self::linear_to_db(level);

            // Compute gain reduction based on sidechain level
            let gr_db = self.gain_computer.compute_gain(level_db, &self.config);

            // Smooth gain reduction
            if gr_db < self.smoothed_gr_db {
                self.smoothed_gr_db += gr_attack * (gr_db - self.smoothed_gr_db);
            } else {
                self.smoothed_gr_db += gr_release * (gr_db - self.smoothed_gr_db);
            }

            self.gain_reduction_linear = Self::db_to_linear(self.smoothed_gr_db);
            self.gain_reduction.update(self.smoothed_gr_db);

            // Apply gain reduction to INPUT signal
            output.push(input[i] * self.gain_reduction_linear * makeup);
        }

        output
    }

    /// Reset compressor state.
    pub fn reset(&mut self) {
        self.detector.reset();
        self.gain_reduction_linear = 1.0;
        self.smoothed_gr_db = 0.0;
        if let Some(ref mut filter) = self.sc_filter {
            filter.reset();
        }
    }

    /// Get current gain reduction in dB.
    #[must_use]
    pub fn current_gain_reduction_db(&self) -> f32 {
        -self.smoothed_gr_db
    }
}

/// Below-threshold expander / gate-like processor.
///
/// Reduces signal level when below the threshold, acting as a soft gate.
pub struct Expander {
    /// Threshold below which expansion is applied (dB).
    pub threshold_db: f32,
    /// Expansion ratio (> 1 = expand).
    pub ratio: f32,
    /// Attack time in milliseconds.
    pub attack_ms: f32,
    /// Release time in milliseconds.
    pub release_ms: f32,
    /// Knee width in dB.
    pub knee_db: f32,
    detector: LevelDetector,
    smoothed_gain_db: f32,
}

impl Expander {
    /// Create a new expander.
    #[must_use]
    pub fn new(
        threshold_db: f32,
        ratio: f32,
        attack_ms: f32,
        release_ms: f32,
        knee_db: f32,
    ) -> Self {
        Self {
            threshold_db,
            ratio,
            attack_ms,
            release_ms,
            knee_db,
            detector: LevelDetector::new(),
            smoothed_gain_db: 0.0,
        }
    }

    /// Default gate-like expander preset.
    #[must_use]
    pub fn gate() -> Self {
        Self::new(-40.0, 10.0, 1.0, 50.0, 4.0)
    }

    fn db_to_linear(db: f32) -> f32 {
        10.0_f32.powf(db / 20.0)
    }

    fn linear_to_db(linear: f32) -> f32 {
        20.0 * linear.max(1e-10_f32).log10()
    }

    fn attack_coeff(attack_ms: f32, sample_rate: u32) -> f32 {
        let s = attack_ms * sample_rate as f32 / 1000.0;
        if s > 0.0 {
            1.0 - (-2.2_f32 / s).exp()
        } else {
            1.0
        }
    }

    fn release_coeff(release_ms: f32, sample_rate: u32) -> f32 {
        let s = release_ms * sample_rate as f32 / 1000.0;
        if s > 0.0 {
            1.0 - (-2.2_f32 / s).exp()
        } else {
            1.0
        }
    }

    fn compute_expansion_gain(&self, input_db: f32) -> f32 {
        let threshold = self.threshold_db;
        let ratio = self.ratio;
        let half_knee = self.knee_db / 2.0;

        if input_db < threshold - half_knee {
            // Below threshold: expand downward
            (threshold - input_db) * (1.0 - ratio)
        } else if input_db <= threshold + half_knee && self.knee_db > 0.0 {
            // Soft knee region
            let knee_input = input_db - threshold + half_knee;
            (1.0 - ratio) * (knee_input - self.knee_db) * (knee_input - self.knee_db)
                / (2.0 * self.knee_db)
        } else {
            0.0
        }
    }

    /// Process a buffer of samples.
    #[must_use]
    pub fn process(&mut self, samples: &[f32], sample_rate: u32) -> Vec<f32> {
        let attack = Self::attack_coeff(self.attack_ms, sample_rate);
        let release = Self::release_coeff(self.release_ms, sample_rate);

        samples
            .iter()
            .map(|&x| {
                let level = self.detector.process(x, attack, release);
                let level_db = Self::linear_to_db(level);
                let gain_db = self.compute_expansion_gain(level_db);

                // Smooth the gain
                if gain_db < self.smoothed_gain_db {
                    self.smoothed_gain_db += attack * (gain_db - self.smoothed_gain_db);
                } else {
                    self.smoothed_gain_db += release * (gain_db - self.smoothed_gain_db);
                }

                x * Self::db_to_linear(self.smoothed_gain_db)
            })
            .collect()
    }

    /// Reset expander state.
    pub fn reset(&mut self) {
        self.detector.reset();
        self.smoothed_gain_db = 0.0;
    }
}

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

    #[test]
    fn test_compressor_config_standard() {
        let config = CompressorConfig::standard();
        assert_eq!(config.ratio, 4.0);
        assert!(config.threshold_db < 0.0);
    }

    #[test]
    fn test_compressor_config_limiting() {
        let config = CompressorConfig::limiting();
        assert_eq!(config.ratio, 100.0);
        assert!(config.attack_ms < 1.0);
    }

    #[test]
    fn test_compressor_config_vocal() {
        let config = CompressorConfig::vocal();
        assert_eq!(config.ratio, 3.0);
    }

    #[test]
    fn test_level_detector_attack() {
        let mut det = LevelDetector::new();
        // After processing a loud signal, level should be greater than 0
        for _ in 0..100 {
            det.process(1.0, 0.1, 0.01);
        }
        assert!(det.peak_level > 0.0);
    }

    #[test]
    fn test_level_detector_release() {
        let mut det = LevelDetector::new();
        det.peak_level = 1.0;
        // After processing silence, level should decrease
        for _ in 0..100 {
            det.process(0.0, 0.1, 0.1);
        }
        assert!(det.peak_level < 0.5);
    }

    #[test]
    fn test_gain_computer_below_threshold() {
        let config = CompressorConfig {
            threshold_db: -10.0,
            knee_db: 0.0,
            ..CompressorConfig::standard()
        };
        let mut computer = GainComputerState::new();
        // Signal well below threshold: no gain reduction
        let gr = computer.compute_gain(-20.0, &config);
        assert!(
            gr >= -0.001,
            "Expected no reduction below threshold, got {gr}"
        );
    }

    #[test]
    fn test_gain_computer_above_threshold() {
        let config = CompressorConfig {
            threshold_db: -10.0,
            ratio: 4.0,
            knee_db: 0.0,
            ..CompressorConfig::standard()
        };
        let mut computer = GainComputerState::new();
        // 10dB above threshold with 4:1 ratio
        let gr = computer.compute_gain(0.0, &config);
        // Gain reduction should be negative (attenuation)
        assert!(
            gr < 0.0,
            "Expected gain reduction above threshold, got {gr}"
        );
    }

    #[test]
    fn test_compressor_output_finite() {
        let config = CompressorConfig::standard();
        let mut comp = Compressor::new(config, 48000);
        let input: Vec<f32> = (0..512).map(|i| (i as f32 * 0.01).sin()).collect();
        let output = comp.process(&input, 48000);
        assert_eq!(output.len(), 512);
        assert!(output.iter().all(|&s| s.is_finite()));
    }

    #[test]
    fn test_compressor_reduces_loud_signal() {
        let config = CompressorConfig {
            threshold_db: -6.0,
            ratio: 10.0,
            attack_ms: 1.0,
            release_ms: 50.0,
            knee_db: 0.0,
            makeup_gain_db: 0.0,
        };
        let mut comp = Compressor::new(config, 48000);
        // Loud constant signal
        let input = vec![0.9f32; 1024];
        let output = comp.process(&input, 48000);

        // After settling, output should be lower than input for a loud signal
        let in_rms: f32 = (input.iter().map(|&s| s * s).sum::<f32>() / input.len() as f32).sqrt();
        let out_rms: f32 =
            (output.iter().map(|&s| s * s).sum::<f32>() / output.len() as f32).sqrt();
        assert!(out_rms < in_rms, "Compressor should reduce loud signal");
    }

    #[test]
    fn test_compressor_limiter() {
        let config = CompressorConfig::limiting();
        let mut comp = Compressor::new(config, 48000);
        // Very loud signal
        let input = vec![0.99f32; 2048];
        let output = comp.process(&input, 48000);
        assert!(output.iter().all(|&s| s.is_finite()));
    }

    #[test]
    fn test_gain_reduction_tracking() {
        let mut gr = GainReduction::new(100);
        gr.update(3.0);
        gr.update(6.0);
        assert!(gr.peak_db >= 6.0);
        gr.reset_peak();
        assert_eq!(gr.peak_db, 0.0);
    }

    #[test]
    fn test_expander_output_finite() {
        let mut exp = Expander::gate();
        let input: Vec<f32> = (0..512).map(|i| (i as f32 * 0.01).sin() * 0.1).collect();
        let output = exp.process(&input, 48000);
        assert_eq!(output.len(), 512);
        assert!(output.iter().all(|&s| s.is_finite()));
    }

    #[test]
    fn test_expander_attenuates_below_threshold() {
        let mut exp = Expander::new(-10.0, 5.0, 1.0, 50.0, 0.0);
        // Quiet signal below threshold
        let input = vec![0.001f32; 1024];
        let output = exp.process(&input, 48000);
        let in_rms: f32 = (input.iter().map(|&s| s * s).sum::<f32>() / input.len() as f32).sqrt();
        let out_rms: f32 =
            (output.iter().map(|&s| s * s).sum::<f32>() / output.len() as f32).sqrt();
        assert!(
            out_rms <= in_rms + 1e-6,
            "Expander should attenuate or not increase quiet signals"
        );
    }

    #[test]
    fn test_compressor_reset() {
        let config = CompressorConfig::standard();
        let mut comp = Compressor::new(config, 48000);
        let _ = comp.process(&vec![0.9f32; 512], 48000);
        comp.reset();
        assert_eq!(comp.smoothed_gr_db, 0.0);
    }

    // --- Sidechain compressor tests ---

    #[test]
    fn test_sidechain_compressor_output_finite() {
        let config = CompressorConfig::standard();
        let mut comp = Compressor::new(config, 48000);
        let input: Vec<f32> = (0..512).map(|i| (i as f32 * 0.01).sin()).collect();
        let sidechain: Vec<f32> = vec![0.9; 512]; // loud sidechain
        let output = comp.process_sidechain(&input, &sidechain, 48000);
        assert_eq!(output.len(), 512);
        assert!(output.iter().all(|&s| s.is_finite()));
    }

    #[test]
    fn test_sidechain_compressor_applies_reduction_from_sidechain() {
        let config = CompressorConfig {
            threshold_db: -6.0,
            ratio: 10.0,
            attack_ms: 1.0,
            release_ms: 50.0,
            knee_db: 0.0,
            makeup_gain_db: 0.0,
        };
        let mut comp = Compressor::new(config, 48000);

        // Input is quiet but sidechain is loud: should still compress
        let input = vec![0.5f32; 2048];
        let sidechain = vec![0.9f32; 2048];
        let output = comp.process_sidechain(&input, &sidechain, 48000);

        // Output RMS should be less than input RMS (gain reduction from sidechain)
        let in_rms: f32 = (input.iter().map(|&s| s * s).sum::<f32>() / input.len() as f32).sqrt();
        let out_rms: f32 =
            (output.iter().map(|&s| s * s).sum::<f32>() / output.len() as f32).sqrt();
        assert!(
            out_rms < in_rms,
            "Sidechain compression should reduce input: in={in_rms}, out={out_rms}"
        );
    }

    #[test]
    fn test_sidechain_silent_no_compression() {
        let config = CompressorConfig {
            threshold_db: -20.0,
            ratio: 10.0,
            attack_ms: 1.0,
            release_ms: 50.0,
            knee_db: 0.0,
            makeup_gain_db: 0.0,
        };
        let mut comp = Compressor::new(config, 48000);

        // Input is loud but sidechain is silent: should not compress
        let input = vec![0.5f32; 2048];
        let sidechain = vec![0.0f32; 2048];
        let output = comp.process_sidechain(&input, &sidechain, 48000);

        // Output should be approximately same as input (no sidechain trigger)
        for (&inp, &out) in input.iter().zip(output.iter()) {
            assert!(
                (out - inp).abs() < 0.01,
                "Silent sidechain should not compress: in={inp}, out={out}"
            );
        }
    }

    #[test]
    fn test_sidechain_different_from_normal() {
        let config = CompressorConfig {
            threshold_db: -10.0,
            ratio: 8.0,
            attack_ms: 1.0,
            release_ms: 50.0,
            knee_db: 0.0,
            makeup_gain_db: 0.0,
        };

        // Normal compression on quiet signal
        let mut comp1 = Compressor::new(config.clone(), 48000);
        let quiet_input = vec![0.05f32; 1024];
        let normal_output = comp1.process(&quiet_input, 48000);

        // Sidechain with loud trigger on same quiet signal
        let mut comp2 = Compressor::new(config, 48000);
        let loud_sidechain = vec![0.9f32; 1024];
        let sc_output = comp2.process_sidechain(&quiet_input, &loud_sidechain, 48000);

        // Sidechain output should be quieter due to loud sidechain trigger
        let normal_rms: f32 =
            (normal_output.iter().map(|&s| s * s).sum::<f32>() / normal_output.len() as f32).sqrt();
        let sc_rms: f32 =
            (sc_output.iter().map(|&s| s * s).sum::<f32>() / sc_output.len() as f32).sqrt();
        assert!(
            sc_rms < normal_rms,
            "Sidechain with loud trigger should produce lower output: normal={normal_rms}, sc={sc_rms}"
        );
    }

    // --- Sidechain filter tests ---

    #[test]
    fn test_sidechain_filter_highpass() {
        let config = CompressorConfig {
            threshold_db: -10.0,
            ratio: 8.0,
            attack_ms: 1.0,
            release_ms: 50.0,
            knee_db: 0.0,
            makeup_gain_db: 0.0,
        };
        let mut comp = Compressor::new(config, 48000);
        comp.set_sidechain_filter(SidechainFilter::HighPass(4000.0), 48000);
        assert_eq!(comp.sidechain_filter(), SidechainFilter::HighPass(4000.0));

        // Low-frequency sidechain should be filtered out, reducing compression
        let input = vec![0.5f32; 2048];
        // 100 Hz sidechain (below 4kHz HPF) — should be attenuated
        let sidechain: Vec<f32> = (0..2048)
            .map(|i| (i as f32 * 2.0 * std::f32::consts::PI * 100.0 / 48000.0).sin() * 0.9)
            .collect();
        let output = comp.process_sidechain(&input, &sidechain, 48000);
        assert!(output.iter().all(|&s| s.is_finite()));
    }

    #[test]
    fn test_sidechain_filter_lowpass() {
        let config = CompressorConfig::standard();
        let mut comp = Compressor::new(config, 48000);
        comp.set_sidechain_filter(SidechainFilter::LowPass(200.0), 48000);
        assert_eq!(comp.sidechain_filter(), SidechainFilter::LowPass(200.0));

        let input = vec![0.5f32; 1024];
        let sidechain = vec![0.9f32; 1024];
        let output = comp.process_sidechain(&input, &sidechain, 48000);
        assert!(output.iter().all(|&s| s.is_finite()));
    }

    #[test]
    fn test_sidechain_filter_bandpass() {
        let config = CompressorConfig::standard();
        let mut comp = Compressor::new(config, 48000);
        comp.set_sidechain_filter(SidechainFilter::BandPass(1000.0, 1.0), 48000);

        let input = vec![0.5f32; 1024];
        let sidechain = vec![0.9f32; 1024];
        let output = comp.process_sidechain(&input, &sidechain, 48000);
        assert!(output.iter().all(|&s| s.is_finite()));
    }

    #[test]
    fn test_sidechain_filter_none() {
        let config = CompressorConfig::standard();
        let mut comp = Compressor::new(config, 48000);
        comp.set_sidechain_filter(SidechainFilter::None, 48000);
        assert_eq!(comp.sidechain_filter(), SidechainFilter::None);
        assert!(comp.sc_filter.is_none());
    }

    #[test]
    fn test_sidechain_filter_reset() {
        let config = CompressorConfig::standard();
        let mut comp = Compressor::new(config, 48000);
        comp.set_sidechain_filter(SidechainFilter::HighPass(2000.0), 48000);

        // Process some samples
        let input = vec![0.5f32; 512];
        let sidechain = vec![0.9f32; 512];
        let _ = comp.process_sidechain(&input, &sidechain, 48000);

        // Reset should clear filter state
        comp.reset();
        assert_eq!(comp.smoothed_gr_db, 0.0);
    }

    #[test]
    fn test_sidechain_hpf_attenuates_low_freq_trigger() {
        // With HPF at 4kHz, a low-frequency sidechain should trigger LESS compression
        // than the same sidechain without filtering
        let config = CompressorConfig {
            threshold_db: -10.0,
            ratio: 10.0,
            attack_ms: 1.0,
            release_ms: 50.0,
            knee_db: 0.0,
            makeup_gain_db: 0.0,
        };

        // Without filter
        let mut comp_no_filter = Compressor::new(config.clone(), 48000);

        // With HPF
        let mut comp_hpf = Compressor::new(config, 48000);
        comp_hpf.set_sidechain_filter(SidechainFilter::HighPass(4000.0), 48000);

        let input = vec![0.5f32; 4096];
        // Low-frequency sidechain (100Hz)
        let sidechain: Vec<f32> = (0..4096)
            .map(|i| (i as f32 * 2.0 * std::f32::consts::PI * 100.0 / 48000.0).sin() * 0.9)
            .collect();

        let out_no_filter = comp_no_filter.process_sidechain(&input, &sidechain, 48000);
        let out_hpf = comp_hpf.process_sidechain(&input, &sidechain, 48000);

        let rms_no_filter: f32 =
            (out_no_filter.iter().map(|&s| s * s).sum::<f32>() / out_no_filter.len() as f32).sqrt();
        let rms_hpf: f32 =
            (out_hpf.iter().map(|&s| s * s).sum::<f32>() / out_hpf.len() as f32).sqrt();

        // HPF should pass through more signal (less compression from filtered-out bass)
        assert!(
            rms_hpf >= rms_no_filter - 0.01,
            "HPF should reduce compression from bass sidechain: no_filter={rms_no_filter}, hpf={rms_hpf}"
        );
    }
}