Skip to main content

math_audio_dsp/
auto_makeup.rs

1// ============================================================================
2// Measured Auto-Makeup Gain — Based on actual gain reduction, not heuristics
3// ============================================================================
4
5/// Measured auto-makeup gain that tracks the actual average gain reduction
6/// and compensates for it, rather than using a fixed heuristic like
7/// `threshold × 0.5 × slope`.
8///
9/// Uses an exponential moving average (EMA) of the applied gain reduction
10/// to determine how much makeup gain to apply.
11#[derive(Debug, Clone, Copy)]
12pub struct MeasuredMakeup {
13    /// Smoothed average gain reduction in dB (positive = reduction).
14    avg_gr_db: f32,
15    /// EMA coefficient for smoothing.
16    coeff: f32,
17}
18
19impl MeasuredMakeup {
20    /// Create a new measured auto-makeup.
21    ///
22    /// * `smoothing_ms` — EMA time constant (e.g., 1000ms for slow tracking)
23    /// * `sample_rate` — audio sample rate
24    pub fn new(smoothing_ms: f32, sample_rate: u32) -> Self {
25        let coeff = if smoothing_ms <= 0.0 {
26            0.0
27        } else {
28            (-1.0 / (smoothing_ms * 0.001 * sample_rate as f32)).exp()
29        };
30        Self {
31            avg_gr_db: 0.0,
32            coeff,
33        }
34    }
35
36    /// Update with the current gain reduction in dB (positive = reduction).
37    ///
38    /// Call this once per sample (or once per frame with the frame's GR).
39    #[inline]
40    pub fn update(&mut self, gain_reduction_db: f32) {
41        let gr = gain_reduction_db.max(0.0);
42        self.avg_gr_db = gr + self.coeff * (self.avg_gr_db - gr);
43    }
44
45    /// Get the makeup gain in dB that compensates for average reduction.
46    #[inline]
47    pub fn makeup_db(&self) -> f32 {
48        self.avg_gr_db
49    }
50
51    /// Get the makeup gain as a linear multiplier.
52    #[inline]
53    pub fn makeup_linear(&self) -> f32 {
54        // 10^(avg_gr_db / 20)
55        fast_pow10(self.avg_gr_db / 20.0)
56    }
57
58    pub fn reset(&mut self) {
59        self.avg_gr_db = 0.0;
60    }
61
62    /// Update smoothing time (e.g., when sample rate changes).
63    pub fn set_smoothing(&mut self, smoothing_ms: f32, sample_rate: u32) {
64        self.coeff = if smoothing_ms <= 0.0 {
65            0.0
66        } else {
67            (-1.0 / (smoothing_ms * 0.001 * sample_rate as f32)).exp()
68        };
69    }
70}
71
72/// Fast 10^x approximation for real-time audio.
73#[inline]
74fn fast_pow10(x: f32) -> f32 {
75    // 10^x = e^(x * ln10)
76    (x * std::f32::consts::LN_10).exp()
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_no_reduction() {
85        let mut m = MeasuredMakeup::new(100.0, 48000);
86        for _ in 0..48000 {
87            m.update(0.0);
88        }
89        assert!(m.makeup_db().abs() < 0.01);
90        assert!((m.makeup_linear() - 1.0).abs() < 0.01);
91    }
92
93    #[test]
94    fn test_steady_reduction() {
95        let mut m = MeasuredMakeup::new(100.0, 48000);
96        // Feed 6dB reduction for a long time
97        for _ in 0..480000 {
98            m.update(6.0);
99        }
100        // Should converge to ~6 dB makeup
101        assert!((m.makeup_db() - 6.0).abs() < 0.1);
102        // Linear: 10^(6/20) ≈ 1.995
103        assert!((m.makeup_linear() - 1.995).abs() < 0.05);
104    }
105
106    #[test]
107    fn test_reset() {
108        let mut m = MeasuredMakeup::new(100.0, 48000);
109        for _ in 0..48000 {
110            m.update(10.0);
111        }
112        m.reset();
113        assert!(m.makeup_db().abs() < 0.01);
114    }
115}