diff --git a/crates/audio-dsp/src/effects/behavior_tests.rs b/crates/audio-dsp/src/effects/behavior_tests.rs index d5ae9b8d5..af6b240a2 100644 --- a/crates/audio-dsp/src/effects/behavior_tests.rs +++ b/crates/audio-dsp/src/effects/behavior_tests.rs @@ -453,6 +453,56 @@ fn analog_delay_tail_covers_the_spread_channel() { assert!(last <= e.latency() + tail, "echo at {last} after a tail of {tail}"); } +#[test] +fn analog_delay_tail_covers_a_shortening_ramp() { + let mut e = fx("analog_delay", &[("delay", 500.0), ("feedback", 0.0), ("dry", 0.0), ("wet", 100.0)]); + let x = noise(31, SR as usize / 10, 0.5); + run(e.as_mut(), &x, &x); + // The delay ramps down from 500 ms while the noise is still in the line. + assert!(e.set_param("delay", 5.0)); + let tail = e.tail_samples(); + let silence = vec![0.0f32; SR as usize]; + let (a, b) = run(e.as_mut(), &silence, &silence); + let last = |s: &[f32]| s.iter().rposition(|v| v.abs() > 1e-6).unwrap_or(0); + let last = last(&a).max(last(&b)); + assert!(last > SR as usize / 100, "the ramped echo at {last}"); + assert!(last <= e.latency() + tail, "echo at {last} after a tail of {tail}"); +} + +#[test] +fn mastering_maximizer_switches_without_a_step() { + let mut e = fx("mastering", &[]); + let x = vec![0.9f32; SR as usize / 10]; + let (y, _) = run(e.as_mut(), &x, &x); + let mut prev = y[y.len() - 1]; + let mut steps = |z: &[f32]| { + let mut worst = 0.0f32; + for &v in z { + worst = worst.max((v - prev).abs()); + prev = v; + } + worst + }; + assert!(e.set_param("loudness", 1.0)); + let (on, _) = run(e.as_mut(), &x, &x); + let on_step = steps(&on); + assert!(on_step < 0.005, "switching on stepped by {on_step}"); + assert!(e.set_param("loudness", 0.0)); + let (off, _) = run(e.as_mut(), &x, &x); + let off_step = steps(&off); + assert!(off_step < 0.005, "switching off stepped by {off_step}"); + assert!((off[off.len() - 1] - y[y.len() - 1]).abs() < 1e-6, "{} {}", off[off.len() - 1], y[y.len() - 1]); + // Input above the ceiling is held to it from the first sample of the fade. + let mut e = fx("mastering", &[]); + let x = vec![1.0f32; SR as usize / 10]; + run(e.as_mut(), &x, &x); + assert!(e.set_param("loudness", 1.0)); + let (on, _) = run(e.as_mut(), &x, &x); + let ceiling = crate::db_to_gain(-0.3); + let peak = on.iter().fold(0.0f32, |a, v| a.max(v.abs())); + assert!(peak <= ceiling + 1e-6, "{peak} over the ceiling {ceiling}"); +} + #[test] fn mastering_reverb_fades_out_and_restarts_from_silence() { let mut e = fx("mastering", &[("reverb", 100.0)]); diff --git a/crates/audio-dsp/src/effects/modulation.rs b/crates/audio-dsp/src/effects/modulation.rs index d9bcac5db..b7b93e9c9 100644 --- a/crates/audio-dsp/src/effects/modulation.rs +++ b/crates/audio-dsp/src/effects/modulation.rs @@ -420,9 +420,13 @@ impl AnalogDelay { impl AudioEffect for AnalogDelay { param_plumbing!("analog_delay"); /// Echoes of the longest channel delay: Spread lengthens the second channel's by up to half. + /// While a ramp runs, each of delay, spread and feedback counts at the larger of its current + /// and target values. fn tail_samples(&self) -> usize { - let delay = self.pv.v("delay") * (1.0 + 0.25 * self.pv.v("spread") / 100.0); - super::util::echo_tail(delay, self.pv.v("feedback") / 100.0, self.sr) + let delay_ms = self.time.value().max(self.time.target()) * 1000.0 / self.sr; + let spread = self.spread.value().max(self.spread.target()); + let feedback = self.fb.value().max(self.fb.target()); + super::util::echo_tail(delay_ms * (1.0 + 0.25 * spread), feedback, self.sr) } fn reset(&mut self) { self.lines.iter_mut().for_each(DelayLine::reset); diff --git a/crates/audio-dsp/src/effects/special.rs b/crates/audio-dsp/src/effects/special.rs index 67c3b8458..2e3bb3813 100644 --- a/crates/audio-dsp/src/effects/special.rs +++ b/crates/audio-dsp/src/effects/special.rs @@ -363,6 +363,8 @@ pub struct Mastering { exc: Smoothed, width: Smoothed, boost: Smoothed, + /// Crossfade from the bypassed signal (0) to the maximized one (1). + loud_mix: Smoothed, out: Smoothed, } @@ -402,6 +404,7 @@ impl Mastering { exc: sm(0.0, sample_rate), width: sm(1.0, sample_rate), boost: sm(1.0, sample_rate), + loud_mix: sm(0.0, sample_rate), out: sm(1.0, sample_rate), }; s.apply_params(true); @@ -420,9 +423,10 @@ impl Mastering { self.exc.set(0.6 * self.pv.v("exciter") / 100.0); self.width.set(self.pv.v("widener") / 100.0); self.boost.set(db_to_gain(12.0 * self.pv.v("loudness") / 100.0)); + self.loud_mix.set(if self.pv.v("loudness") > 0.0 { 1.0 } else { 0.0 }); self.out.set(db_to_gain(self.pv.v("output"))); if snap { - for s in [&mut self.rev_amt, &mut self.exc, &mut self.width, &mut self.boost, &mut self.out] { + for s in [&mut self.rev_amt, &mut self.exc, &mut self.width, &mut self.boost, &mut self.loud_mix, &mut self.out] { s.snap(); } } @@ -444,7 +448,7 @@ impl AudioEffect for Mastering { self.hp.iter_mut().for_each(OnePole::reset); self.lim_env = 0.0; self.rev_on = false; - for s in [&mut self.rev_amt, &mut self.exc, &mut self.width, &mut self.boost, &mut self.out] { + for s in [&mut self.rev_amt, &mut self.exc, &mut self.width, &mut self.boost, &mut self.loud_mix, &mut self.out] { s.snap(); } } @@ -454,7 +458,6 @@ impl AudioEffect for Mastering { if nch == 0 { return; } - let loud_on = self.pv.v("loudness") > 0.0; let rev_param = self.pv.v("reverb") > 0.0; let mode = self.pv.idx("exciter_mode"); let ahp = OnePole::alpha(3000.0, self.sr); @@ -485,9 +488,9 @@ impl AudioEffect for Mastering { self.rev.process(&mut [&mut a[..len], &mut b[..len]]); } for i in 0..len { - // The maximizer stays in while its boost ramps back to 0 dB. - let loud = loud_on || self.boost.is_smoothing(); let (ra, ex, w, boost, out) = (self.rev_amt.tick(), self.exc.tick(), self.width.tick(), self.boost.tick(), self.out.tick()); + // The maximizer crossfades in and out over the smoothing ramp. + let mix = self.loud_mix.tick(); let j = start + i; for ch in 0..nch { let mut v = channels[ch][j]; @@ -511,11 +514,8 @@ impl AudioEffect for Mastering { channels[0][j] = m + s; channels[1][j] = m - s; } - if loud { - for c in channels[..nch].iter_mut() { - c[j] *= boost; - } - let peak = channels[..nch].iter().fold(0.0f32, |a, c| a.max(c[j].abs())); + if mix > 0.0 { + let peak = channels[..nch].iter().fold(0.0f32, |a, c| a.max((c[j] * boost).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. @@ -525,7 +525,10 @@ impl AudioEffect for Mastering { } let g = db_to_gain(self.lim_env); for c in channels[..nch].iter_mut() { - c[j] = soft_clip(c[j] * g, ceiling); + let x = c[j]; + let y = soft_clip(x * boost * g, ceiling); + // The blend stays within the ceiling, which the maximized signal holds. + c[j] = if mix < 1.0 { (x + mix * (y - x)).clamp(-ceiling, ceiling) } else { y }; } } else { self.lim_env = 0.0;