diff --git a/crates/audio-dsp/src/effects/behavior_tests.rs b/crates/audio-dsp/src/effects/behavior_tests.rs index a4e574583..31c5314c9 100644 --- a/crates/audio-dsp/src/effects/behavior_tests.rs +++ b/crates/audio-dsp/src/effects/behavior_tests.rs @@ -245,6 +245,23 @@ fn dynamics_rack_sections() { assert!(ys.iter().all(|v| v.abs() <= db_to_gain(-6.0) + 1e-6)); } +#[test] +fn dynamics_rack_applies_makeup_before_the_limiter() { + let params = [("comp_ratio", 1.0), ("comp_makeup", 6.0), ("lim_on", 1.0), ("lim_threshold", -1.0), ("output", 0.0)]; + let mut e = fx("dynamics_rack", ¶ms); + assert!((e.transfer_db(0, 0.0).unwrap() + 1.0).abs() < 1e-4); + let n = SR as usize / 10; + let x = vec![1.0f32; n]; + let (y, _) = run(e.as_mut(), &x, &x); + assert!(y.iter().all(|v| v.abs() <= db_to_gain(-1.0) + 1e-6), "peak {}", gain_db(y.iter().fold(0.0f32, |a, v| a.max(v.abs())))); + assert!((gain_db(y[n - 1]) + 1.0).abs() < 0.01, "{}", gain_db(y[n - 1])); + // The output gain still follows the limiter. + let mut o = fx("dynamics_rack", &[("comp_ratio", 1.0), ("comp_makeup", 6.0), ("lim_on", 1.0), ("lim_threshold", -1.0), ("output", -6.0)]); + assert!((o.transfer_db(0, 0.0).unwrap() + 7.0).abs() < 1e-4); + let (yo, _) = run(o.as_mut(), &x, &x); + assert!((gain_db(yo[n - 1]) + 7.0).abs() < 0.01, "{}", gain_db(yo[n - 1])); +} + #[test] fn multiband_crossovers_sum_flat_and_bands_compress_independently() { let mut e = fx("multiband_compressor", &[("b1_ratio", 1.0), ("b2_ratio", 1.0), ("b3_ratio", 1.0), ("b4_ratio", 1.0)]); @@ -484,6 +501,31 @@ fn mastering_and_vocal_enhancer() { assert!((tone_db(music.as_mut(), 1500.0, 0.3) - music.response_db(1500.0).unwrap()).abs() < 0.1); } +#[test] +fn mastering_output_and_maximizer_reach_every_channel() { + let six = |params: &[(&str, f32)]| { + let mut e = create_effect("mastering", SR, 6).unwrap_or_else(|| panic!("no effect mastering")); + for (p, v) in params { + assert!(e.set_param(p, *v), "mastering.{p}"); + } + e.reset(); + let n = SR as usize / 10; + let mut bufs = vec![vec![0.25f32; n]; 6]; + let mut refs: Vec<&mut [f32]> = bufs.iter_mut().map(|b| b.as_mut_slice()).collect(); + e.process(&mut refs); + bufs.iter().map(|b| b[n - 1]).collect::>() + }; + let out = six(&[("output", -6.0)]); + for (ch, v) in out.iter().enumerate() { + assert!((gain_db(*v) - (gain_db(0.25) - 6.0)).abs() < 0.01, "channel {ch}: {} dB", gain_db(*v)); + } + let loud = six(&[("loudness", 100.0)]); + for (ch, v) in loud.iter().enumerate() { + assert!(v.abs() <= db_to_gain(-0.3) + 1e-6, "channel {ch}: {v}"); + assert!((v - loud[0]).abs() < 1e-6, "channel {ch}: {v}, channel 0: {}", loud[0]); + } +} + #[test] fn binaural_and_ambisonic_rendering() { let n = SR as usize; diff --git a/crates/audio-dsp/src/effects/multiband.rs b/crates/audio-dsp/src/effects/multiband.rs index a27de3c3d..7f4a207a7 100644 --- a/crates/audio-dsp/src/effects/multiband.rs +++ b/crates/audio-dsp/src/effects/multiband.rs @@ -40,7 +40,8 @@ pub fn soft_clip(x: f32, t: f32) -> f32 { // --------------------------------------------------------------------------------- dynamics /// Dynamics rack: Auto Gate, Compressor, Expander and Limiter sections in series -/// (stereo-linked peak detection), plus output gain. +/// (stereo-linked peak detection), plus output gain. The compressor's make-up gain is applied +/// before the limiter, and the output gain after it. pub struct DynamicsRack { pv: ParamValues, sr: f32, @@ -49,6 +50,8 @@ pub struct DynamicsRack { exp_env: f32, comp_env: f32, lim_env: f32, + /// Compressor make-up gain (dB). + makeup: Smoothed, out: Smoothed, /// Cached coefficients. c: [f32; 8], @@ -89,6 +92,7 @@ impl DynamicsRack { exp_env: 0.0, comp_env: 0.0, lim_env: 0.0, + makeup: Smoothed::with_ms(0.0, sample_rate, 20.0), out: Smoothed::with_ms(1.0, sample_rate, 20.0), c: [0.0; 8], hold: 0, @@ -111,8 +115,10 @@ impl DynamicsRack { coef(p.v("lim_release"), sr), ]; self.hold = (p.v("gate_hold") * 0.001 * sr) as u32; - self.out.set(db_to_gain(p.v("output") + self.makeup_db())); + self.makeup.set(self.makeup_db()); + self.out.set(db_to_gain(p.v("output"))); if snap { + self.makeup.snap(); self.out.snap(); } } @@ -131,7 +137,8 @@ impl DynamicsRack { p.v("comp_makeup") + auto } - /// Static gain (dB) of the enabled sections for a detector level, before make-up/output. + /// Static gain (dB) of the enabled sections for a detector level, including the compressor's + /// make-up gain and before the output gain. fn static_gain(&self, level: f32) -> f32 { let p = &self.pv; let mut l = level; @@ -142,7 +149,7 @@ impl DynamicsRack { l = expander_curve(l, p.v("exp_threshold"), p.v("exp_ratio"), EXP_RANGE); } if p.on("comp_on") { - l = compressor_curve(l, p.v("comp_threshold"), p.v("comp_ratio"), 0.0); + l = compressor_curve(l, p.v("comp_threshold"), p.v("comp_ratio"), 0.0) + self.makeup_db(); } if p.on("lim_on") { l = l.min(p.v("lim_threshold")); @@ -154,7 +161,7 @@ impl DynamicsRack { impl AudioEffect for DynamicsRack { param_plumbing!("dynamics_rack"); fn transfer_db(&self, band: usize, input_db: f32) -> Option { - (band == 0).then(|| input_db + self.static_gain(input_db) + self.pv.v("output") + self.makeup_db()) + (band == 0).then(|| input_db + self.static_gain(input_db) + self.pv.v("output")) } fn reset(&mut self) { self.gate_env = 0.0; @@ -162,6 +169,7 @@ impl AudioEffect for DynamicsRack { self.exp_env = 0.0; self.comp_env = 0.0; self.lim_env = 0.0; + self.makeup.snap(); self.out.snap(); } fn process(&mut self, channels: &mut [&mut [f32]]) { @@ -203,6 +211,10 @@ impl AudioEffect for DynamicsRack { g += self.comp_env; l += self.comp_env; } + // Make-up gain (0 dB with the compressor off) comes before the limiter. + let makeup = self.makeup.tick(); + g += makeup; + l += makeup; if lim_on { let target = (lt - l).min(0.0); if target < self.lim_env { diff --git a/crates/audio-dsp/src/effects/special.rs b/crates/audio-dsp/src/effects/special.rs index 5ae18c762..73676ce15 100644 --- a/crates/audio-dsp/src/effects/special.rs +++ b/crates/audio-dsp/src/effects/special.rs @@ -345,7 +345,9 @@ impl AudioEffect for GuitarSuite { pub const EXCITER_MODES: &[&str] = &["Retro", "Tape", "Tube"]; /// Mastering chain: three-band EQ → reverb → exciter → widener → loudness maximiser → output. -/// At its defaults it is transparent. +/// At its defaults it is transparent. Reverb and the widener work on the first two channels; +/// the other stages work on every channel, and the loudness maximizer takes its peak across all +/// of them. pub struct Mastering { pv: ParamValues, sr: f32, @@ -473,10 +475,10 @@ impl AudioEffect for Mastering { } for i in 0..len { let (ra, ex, w, boost, out) = (self.rev_amt.tick(), self.exc.tick(), self.width.tick(), self.boost.tick(), self.out.tick()); - let mut y = [0.0f32; 2]; - for ch in 0..nch.min(2) { - let mut v = channels[ch][start + i]; - if use_rev { + let j = start + i; + for ch in 0..nch { + let mut v = channels[ch][j]; + if use_rev && ch < 2 { v += ra * self.scratch[ch][i]; } if ex > 0.0 { @@ -488,17 +490,19 @@ impl AudioEffect for Mastering { }; v += ex * (s - h * 0.5) * 2.0; } - y[ch] = v; + channels[ch][j] = v; } if nch >= 2 { - let (m, s) = (0.5 * (y[0] + y[1]), 0.5 * (y[0] - y[1]) * w); - y = [m + s, m - s]; + let (l, r) = (channels[0][j], channels[1][j]); + let (m, s) = (0.5 * (l + r), 0.5 * (l - r) * w); + channels[0][j] = m + s; + channels[1][j] = m - s; } if loud { - for v in y.iter_mut().take(nch.min(2)) { - *v *= boost; + for c in channels[..nch].iter_mut() { + c[j] *= boost; } - let peak = y[..nch.min(2)].iter().fold(0.0f32, |a, v| a.max(v.abs())); + let peak = channels[..nch].iter().fold(0.0f32, |a, c| a.max(c[j].abs())); let target = (lin_db(ceiling) - lin_db(peak)).min(0.0); if target < self.lim_env { // An inf input sample gives -inf, which the release would never leave. @@ -507,12 +511,12 @@ impl AudioEffect for Mastering { self.lim_env = rel * self.lim_env + (1.0 - rel) * target; } let g = db_to_gain(self.lim_env); - for v in y.iter_mut().take(nch.min(2)) { - *v = soft_clip(*v * g, ceiling); + for c in channels[..nch].iter_mut() { + c[j] = soft_clip(c[j] * g, ceiling); } } - for ch in 0..nch.min(2) { - channels[ch][start + i] = y[ch] * out; + for c in channels[..nch].iter_mut() { + c[j] *= out; } } start += len;