audio-dsp: report Mastering and flanger tails, band-limit the triangle, bound Sections

- Mastering reports its reverb's tail while the reverb is on, Chorus/Flanger
  the echoes of its longest voice delay, and Flanger those of the longer
  sweep delay, so an offline render no longer cuts their tails off.
- The triangle oscillator rounds its corners with PolyBLAMP: a 10 kHz
  triangle at 48 kHz no longer has the naive -18.7 dB alias at 18 kHz.
- Sections::as_slice and magnitude_db read a count above MAX_SECTIONS as
  every section instead of panicking.
This commit is contained in:
dotCooCoo
2026-11-17 06:11:34 -08:00
parent e3a4c0b2bd
commit e8b366dbf1
5 changed files with 74 additions and 5 deletions
+13 -2
View File
@@ -159,7 +159,7 @@ pub enum Band {
BandStop,
}
/// A cascade of second-order sections.
/// A cascade of second-order sections: the first `n` of `c` (at most [`MAX_SECTIONS`]).
#[derive(Clone, Copy, Debug)]
pub struct Sections {
pub c: [Coeffs; MAX_SECTIONS],
@@ -167,8 +167,9 @@ pub struct Sections {
}
impl Sections {
/// The sections in use. A count above [`MAX_SECTIONS`] reads as all of them.
pub fn as_slice(&self) -> &[Coeffs] {
&self.c[..self.n]
&self.c[..self.n.min(MAX_SECTIONS)]
}
/// Magnitude of the cascade in dB.
pub fn magnitude_db(&self, freq: f64, sample_rate: f64) -> f64 {
@@ -770,4 +771,14 @@ mod tests {
}
}
}
#[test]
fn an_oversized_section_count_reads_as_every_section() {
let mut s = design(&spec(Family::Butterworth, Band::LowPass, 4, 1000.0, 0.0), 48_000.0);
s.n = MAX_SECTIONS + 5;
assert_eq!(s.as_slice().len(), MAX_SECTIONS);
assert!(s.magnitude_db(100.0, 48_000.0).is_finite());
s.n = usize::MAX;
assert_eq!(s.as_slice().len(), MAX_SECTIONS);
}
}
@@ -608,6 +608,10 @@ fn delays_and_reverbs_report_their_tails() {
("reverb", &[("mix", 100.0)][..]),
("convolution_reverb", &[("mix", 100.0)][..]),
("surround_reverb", &[][..]),
("mastering", &[("reverb", 100.0)][..]),
("chorus_flanger", &[("mode", 1.0), ("width", 100.0), ("intensity", 100.0), ("mix", 100.0)][..]),
("chorus_flanger", &[("width", 100.0), ("intensity", 100.0), ("mix", 100.0)][..]),
("flanger", &[("final_delay", 20.0), ("feedback", 100.0), ("mix", 100.0)][..]),
] {
let mut e = fx(id, params);
let tail = e.tail_samples();
@@ -70,6 +70,12 @@ impl ChorusFlanger {
impl AudioEffect for ChorusFlanger {
param_plumbing!("chorus_flanger");
/// Echoes of the longest voice delay at the current width, falling by the feedback each pass.
fn tail_samples(&self) -> usize {
let (w, inten) = (self.pv.v("width") / 100.0, self.pv.v("intensity") / 100.0);
let (delay, g) = if self.pv.idx("mode") == 1 { (0.3 + 4.7 * w, 0.95 * inten) } else { (15.0 + 20.0 * w, 0.6 * inten) };
super::util::echo_tail(delay, g, self.sr)
}
fn reset(&mut self) {
self.lines.iter_mut().for_each(DelayLine::reset);
self.lfo.reset();
@@ -190,6 +196,11 @@ impl Flanger {
impl AudioEffect for Flanger {
param_plumbing!("flanger");
/// Echoes of the longer of the two sweep delays, falling by the feedback each pass.
fn tail_samples(&self) -> usize {
let delay = self.pv.v("initial_delay").max(self.pv.v("final_delay"));
super::util::echo_tail(delay, 0.95 * self.pv.v("feedback").abs() / 100.0, self.sr)
}
fn reset(&mut self) {
self.lines.iter_mut().for_each(DelayLine::reset);
self.lfo.reset();
+4
View File
@@ -428,6 +428,10 @@ impl Mastering {
impl AudioEffect for Mastering {
param_plumbing!("mastering");
/// The reverb's tail while the reverb is on.
fn tail_samples(&self) -> usize {
if self.pv.v("reverb") > 0.0 { self.rev.tail_samples() } else { 0 }
}
fn response_db(&self, freq: f64) -> Option<f64> {
Some(self.eq.iter().map(|c| c.magnitude_db(freq, self.sr as f64)).sum::<f64>() + self.pv.v("output") as f64)
}
+42 -3
View File
@@ -1,5 +1,5 @@
//! Test signals: band-limited (PolyBLEP) sine, square, saw and triangle oscillators, and white
//! and pink noise.
//! Test signals: band-limited sine, square and saw (PolyBLEP) and triangle (PolyBLAMP)
//! oscillators, and white and pink noise.
//!
//! [`Oscillator`] produces one sample per call and never allocates, for real-time use (the
//! `signal_generator` effect). [`signal`] renders a whole buffer offline.
@@ -47,6 +47,22 @@ fn poly_blep(t: f32, dt: f32) -> f32 {
}
}
/// PolyBLAMP residual of a unit change of slope per sample at phase 0: `t` is the phase since the
/// corner (0..1), `dt` the phase step per sample.
#[inline]
fn poly_blamp(t: f32, dt: f32) -> f32 {
let cube = |x: f32| x * x * x / 6.0;
if dt <= 0.0 {
0.0
} else if t < dt {
cube(1.0 - t / dt)
} else if t > 1.0 - dt {
cube(1.0 - (1.0 - t) / dt)
} else {
0.0
}
}
/// Xorshift32 noise source: deterministic for a seed, and it does not allocate.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Noise(u32);
@@ -135,7 +151,10 @@ impl Oscillator {
// Starts at 0 and rises, like the sine.
let u = t + 0.25;
let u = if u >= 1.0 { u - 1.0 } else { u };
1.0 - 4.0 * (u - 0.5).abs()
// The slope changes by -8·dt per sample at the peak (u = 0.5) and by +8·dt at
// the trough (u = 0); PolyBLAMP rounds both corners.
let since_peak = if u >= 0.5 { u - 0.5 } else { u + 0.5 };
1.0 - 4.0 * (u - 0.5).abs() + 8.0 * dt * (poly_blamp(u, dt) - poly_blamp(since_peak, dt))
}
Waveform::WhiteNoise => self.noise.bipolar(),
Waveform::PinkNoise => {
@@ -212,6 +231,26 @@ mod tests {
assert!(alias(&blep) < alias(&naive) / 4.0, "alias {} dB, naive {} dB", db(alias(&blep)), db(alias(&naive)));
}
#[test]
fn the_triangle_is_band_limited() {
// A 10 kHz triangle's 3rd harmonic (30 kHz) folds to 18 kHz. PolyBLAMP keeps that alias
// well below the one a naive triangle has.
let blamp = signal(Waveform::Triangle, 10_000.0, 0.0, 48_000.0, 48_000, 1).remove(0);
let mut phase = 0.25f32;
let naive: Vec<f32> = (0..48_000)
.map(|_| {
let v = 1.0 - 4.0 * (phase - 0.5).abs();
phase += 10_000.0 / 48_000.0;
if phase >= 1.0 {
phase -= 1.0;
}
v
})
.collect();
let alias = |x: &[f32]| tone_amplitude(x, 18_000.0, 48_000.0) / tone_amplitude(x, 10_000.0, 48_000.0);
assert!(alias(&blamp) < alias(&naive) / 4.0, "alias {} dB, naive {} dB", db(alias(&blamp)), db(alias(&naive)));
}
#[test]
fn pink_noise_falls_3_db_per_octave() {
let x = signal(Waveform::PinkNoise, 0.0, 0.0, 48_000.0, 1 << 17, 1).remove(0);