mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
audio-dsp: Mastering EQ changes ramp, Binauralizer reports its tail
- Mastering: a change to an EQ gain or the peak frequency replaced the coefficients at once, under the old filter memories, which gave a transient. The four EQ settings now ramp over 20 ms, the coefficients designed again for each sample while they ramp; reset goes straight to the target. - Binauralizer: tail_samples was 0, so an offline render stopped before the far ear's delayed sound. It now covers the longer of the current and target delay and the head-shadow filters' decay. - The neutral identity test fails on a sample that is not finite.
This commit is contained in:
1 parent
518d94bdf5
commit
e000dc5f76
2 files changed
+139
-15
No files matched your search
@@ -103,7 +103,9 @@ fn neutral_settings_are_delayed_identity() {
|
||||
let (a, b) = run(e.as_mut(), &l, &r);
|
||||
let mut worst = 0.0f32;
|
||||
for i in lat..n {
|
||||
worst = worst.max((a[i] - l[i - lat]).abs()).max((b[i] - r[i - lat]).abs());
|
||||
let (el, er) = ((a[i] - l[i - lat]).abs(), (b[i] - r[i - lat]).abs());
|
||||
assert!(el.is_finite() && er.is_finite(), "{id}: neutral output at {i} is not finite");
|
||||
worst = worst.max(el).max(er);
|
||||
}
|
||||
assert!(worst < 2e-4, "{id}: neutral output differs by {worst}");
|
||||
checked += 1;
|
||||
@@ -541,6 +543,78 @@ fn mastering_maximizer_ramps_out() {
|
||||
assert!((z[z.len() - 1] - 0.01).abs() < 1e-4, "{}", z[z.len() - 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mastering_eq_gain_ramps_and_reset_snaps() {
|
||||
let n = SR as usize / 10;
|
||||
let (pre, post) = (noise(41, n, 0.25), noise(42, n, 0.25));
|
||||
for (id, gain) in [("eq_high", 12.0), ("eq_low", -12.0), ("eq_mid", 12.0)] {
|
||||
let (mut a, mut b) = (fx("mastering", &[]), fx("mastering", &[]));
|
||||
run(a.as_mut(), &pre, &pre);
|
||||
run(b.as_mut(), &pre, &pre);
|
||||
assert!(a.set_param(id, gain));
|
||||
let (ya, _) = run(a.as_mut(), &post, &post);
|
||||
let (yb, _) = run(b.as_mut(), &post, &post);
|
||||
// The first samples after the change still sound like the old setting.
|
||||
let step = ya[..8].iter().zip(&yb[..8]).fold(0.0f32, |m, (x, y)| m.max((x - y).abs()));
|
||||
assert!(step < 0.03, "{id}: the output jumped by {step} at the change");
|
||||
// After the ramp the output is what the new setting gives.
|
||||
let mut c = fx("mastering", &[(id, gain)]);
|
||||
run(c.as_mut(), &pre, &pre);
|
||||
let (yc, _) = run(c.as_mut(), &post, &post);
|
||||
let late = ya[n / 2..].iter().zip(&yc[n / 2..]).fold(0.0f32, |m, (x, y)| m.max((x - y).abs()));
|
||||
assert!(late < 1e-4, "{id}: after the ramp the output differs by {late}");
|
||||
// Reset goes straight to the new setting.
|
||||
assert!(b.set_param(id, gain));
|
||||
b.reset();
|
||||
c.reset();
|
||||
assert_eq!(run(b.as_mut(), &post, &post), run(c.as_mut(), &post, &post), "{id}: reset did not snap");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mastering_eq_ramp_does_not_depend_on_the_block_size() {
|
||||
let n = SR as usize / 20;
|
||||
let x = noise(43, n, 0.25);
|
||||
let ramp = |block: usize| {
|
||||
let mut e = fx("mastering", &[]);
|
||||
run(e.as_mut(), &x[..1000], &x[..1000]);
|
||||
assert!(e.set_param("eq_high", 12.0));
|
||||
let mut out = vec![];
|
||||
for part in x.chunks(block) {
|
||||
out.extend(run(e.as_mut(), part, part).0);
|
||||
}
|
||||
out
|
||||
};
|
||||
let whole = ramp(n);
|
||||
for block in [1, 7, 100] {
|
||||
let worst = whole.iter().zip(&ramp(block)).fold(0.0f32, |m, (a, b)| m.max((a - b).abs()));
|
||||
assert!(worst <= 1e-6, "blocks of {block} differ by {worst}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn binauralizer_tail_covers_the_far_ear() {
|
||||
let n = SR as usize / 10;
|
||||
for angle in [30.0, 90.0] {
|
||||
let mut e = fx("binauralizer", &[("angle", angle), ("head_size", 24.0)]);
|
||||
// A left-only impulse in the last frame of the input.
|
||||
let mut l = vec![0.0f32; n];
|
||||
l[n - 1] = 1.0;
|
||||
run(e.as_mut(), &l, &vec![0.0; n]);
|
||||
let tail = e.tail_samples();
|
||||
let silence = vec![0.0f32; SR as usize / 10];
|
||||
let (a, b) = run(e.as_mut(), &silence, &silence);
|
||||
let last = |s: &[f32]| s.iter().rposition(|v| v.abs() > 1e-6).map_or(0, |i| i + 1);
|
||||
assert!(last(&b) > 10, "{angle}: the right ear's delayed sound ends at {}", last(&b));
|
||||
assert!(last(&a).max(last(&b)) <= tail, "{angle}: sound until {} after a tail of {tail}", last(&a).max(last(&b)));
|
||||
}
|
||||
// A delay that ramps longer is covered from the start of the ramp.
|
||||
let mut e = fx("binauralizer", &[("angle", 0.0)]);
|
||||
assert!(e.set_param("angle", 90.0));
|
||||
let itd = 0.0875 / 343.0 * (std::f32::consts::FRAC_PI_2 + 1.0) * SR;
|
||||
assert!(e.tail_samples() as f32 >= itd, "{} below the target delay {itd}", e.tail_samples());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dynamics_effects_report_gain_reduction() {
|
||||
let n = SR as usize / 2;
|
||||
|
||||
@@ -352,6 +352,9 @@ pub struct Mastering {
|
||||
pv: ParamValues,
|
||||
sr: f32,
|
||||
eq: [Coeffs; 3],
|
||||
/// The EQ settings the coefficients ramp through: low shelf gain, peak frequency (log2 Hz),
|
||||
/// peak gain and high shelf gain.
|
||||
eq_set: [Smoothed; 4],
|
||||
st: Vec<[Biquad; 3]>,
|
||||
rev: Reverb,
|
||||
scratch: [[f32; CHUNK]; 2],
|
||||
@@ -394,6 +397,7 @@ impl Mastering {
|
||||
pv: ParamValues::new(Self::PARAMS),
|
||||
sr: sample_rate,
|
||||
eq: [Coeffs::IDENTITY; 3],
|
||||
eq_set: [sm(0.0, sample_rate), sm(1000f32.log2(), sample_rate), sm(0.0, sample_rate), sm(0.0, sample_rate)],
|
||||
st: vec![[Biquad::new(); 3]; ch],
|
||||
rev,
|
||||
scratch: [[0.0; CHUNK]; 2],
|
||||
@@ -411,14 +415,28 @@ impl Mastering {
|
||||
s
|
||||
}
|
||||
|
||||
fn apply_params(&mut self, snap: bool) {
|
||||
/// The EQ's coefficients at the ramps' current values, or at their targets.
|
||||
fn eq_coeffs(&self, target: bool) -> [Coeffs; 3] {
|
||||
let sr = self.sr as f64;
|
||||
let q = std::f64::consts::FRAC_1_SQRT_2;
|
||||
self.eq = [
|
||||
Coeffs::design(FilterType::LowShelf, sr, 100.0, q, self.pv.v("eq_low") as f64),
|
||||
Coeffs::design(FilterType::Peaking, sr, self.pv.v("eq_mid_freq") as f64, 1.0, self.pv.v("eq_mid") as f64),
|
||||
Coeffs::design(FilterType::HighShelf, sr, 8000.0, q, self.pv.v("eq_high") as f64),
|
||||
];
|
||||
let [low, freq, mid, high] = self.eq_set.map(|s| f64::from(if target { s.target() } else { s.value() }));
|
||||
[
|
||||
Coeffs::design(FilterType::LowShelf, sr, 100.0, q, low),
|
||||
Coeffs::design(FilterType::Peaking, sr, 2f64.powf(freq), 1.0, mid),
|
||||
Coeffs::design(FilterType::HighShelf, sr, 8000.0, q, high),
|
||||
]
|
||||
}
|
||||
|
||||
fn apply_params(&mut self, snap: bool) {
|
||||
let [low, freq, mid, high] = &mut self.eq_set;
|
||||
low.set(self.pv.v("eq_low"));
|
||||
freq.set(self.pv.v("eq_mid_freq").max(1.0).log2());
|
||||
mid.set(self.pv.v("eq_mid"));
|
||||
high.set(self.pv.v("eq_high"));
|
||||
if snap {
|
||||
self.eq_set.iter_mut().for_each(Smoothed::snap);
|
||||
}
|
||||
self.eq = self.eq_coeffs(false);
|
||||
self.rev_amt.set(0.4 * self.pv.v("reverb") / 100.0);
|
||||
self.exc.set(0.6 * self.pv.v("exciter") / 100.0);
|
||||
self.width.set(self.pv.v("widener") / 100.0);
|
||||
@@ -440,9 +458,11 @@ impl AudioEffect for Mastering {
|
||||
if self.pv.v("reverb") > 0.0 || self.rev_amt.is_smoothing() { 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)
|
||||
Some(self.eq_coeffs(true).iter().map(|c| c.magnitude_db(freq, self.sr as f64)).sum::<f64>() + self.pv.v("output") as f64)
|
||||
}
|
||||
fn reset(&mut self) {
|
||||
self.eq_set.iter_mut().for_each(Smoothed::snap);
|
||||
self.eq = self.eq_coeffs(false);
|
||||
self.st.iter_mut().for_each(|s| s.iter_mut().for_each(Biquad::reset));
|
||||
self.rev.reset();
|
||||
self.hp.iter_mut().for_each(OnePole::reset);
|
||||
@@ -469,15 +489,28 @@ impl AudioEffect for Mastering {
|
||||
// The reverb runs until its amount has faded to 0.
|
||||
let use_rev = rev_param || self.rev_amt.is_smoothing();
|
||||
self.rev_on = use_rev;
|
||||
// EQ in place.
|
||||
for ch in 0..nch {
|
||||
for v in channels[ch][start..start + len].iter_mut() {
|
||||
let mut y = *v as f64;
|
||||
for (k, c) in self.eq.iter().enumerate() {
|
||||
y = self.st[ch][k].tick(c, y);
|
||||
// EQ in place. While a setting ramps, the coefficients are designed again for every
|
||||
// sample; once the ramps end, the rest of the chunk runs on fixed coefficients.
|
||||
let mut i = 0;
|
||||
while i < len {
|
||||
let ramping = self.eq_set.iter().any(Smoothed::is_smoothing);
|
||||
if ramping {
|
||||
for s in &mut self.eq_set {
|
||||
s.tick();
|
||||
}
|
||||
*v = y as f32;
|
||||
self.eq = self.eq_coeffs(false);
|
||||
}
|
||||
let step = if ramping { 1 } else { len - i };
|
||||
for ch in 0..nch {
|
||||
for v in channels[ch][start + i..start + i + step].iter_mut() {
|
||||
let mut y = *v as f64;
|
||||
for (k, c) in self.eq.iter().enumerate() {
|
||||
y = self.st[ch][k].tick(c, y);
|
||||
}
|
||||
*v = y as f32;
|
||||
}
|
||||
}
|
||||
i += step;
|
||||
}
|
||||
if use_rev {
|
||||
for i in 0..len {
|
||||
@@ -783,6 +816,23 @@ impl Binauralizer {
|
||||
|
||||
impl AudioEffect for Binauralizer {
|
||||
param_plumbing!("binauralizer");
|
||||
/// The far ear's delay (the longer of the current and target delay while it ramps) and the
|
||||
/// head-shadow filters' decay to a millionth.
|
||||
fn tail_samples(&self) -> usize {
|
||||
if self.mix.target() <= 0.0 && !self.mix.is_smoothing() {
|
||||
return 0;
|
||||
}
|
||||
let delay = self.itd.value().max(self.itd.target()).max(0.0).ceil() as usize + 2;
|
||||
let pole = self.sh.iter().fold(0.0f32, |p, s| p.max(s.a1.abs()));
|
||||
let decay = if pole <= 0.0 {
|
||||
1
|
||||
} else if pole < 1.0 {
|
||||
(1e-6f32.ln() / pole.ln()).ceil() as usize + 1
|
||||
} else {
|
||||
super::util::tail_samples(f32::INFINITY, self.sr)
|
||||
};
|
||||
delay + decay
|
||||
}
|
||||
fn reset(&mut self) {
|
||||
self.lines.iter_mut().for_each(DelayLine::reset);
|
||||
for s in &mut self.sh {
|
||||
|
||||
Reference in new issue
Block a user