forge
Explore
Log in

Don't you dare go Hollow

Clamp a band's gain, the one field Coeffs::new left open

Browse files at this commit

1b0fbe8ca5d15ae68f2aeb5cabf0627282c1a549Ahmet Kaan GümüşParent: 70ba4eb
  • fix
    • clamp a band's gain, the one field Coeffs::new left open
Show full message
fix(engine): clamp a band's gain, the one field Coeffs::new left open

`Band`'s doc states the invariant for the whole struct: out-of-range
values are clamped when coefficients are built, so a deserializer can
hand this over unchecked. `Coeffs::new` clamped three of the four
fields -- `fs`, `f0`, `q` -- and computed `a = 10^(gain_db/40)` from the
fourth with nothing at all.

That is not a magnitude, it is a pole placement. For a peaking section
`|pole| = sqrt(a2/a0)` walks toward 1 as `a` grows: at 200 dB the radius
is 0.9999993 and the section rings for ~32 seconds, and past a few
hundred dB the f32 rounding puts the poles *exactly* on the unit circle.
Nothing upstream stops it -- `edith.rs`'s `band()` takes any parseable
field, its `float()` refuses only inf/NaN, `Project::set_eq` refuses
only `!finite`, and the UI's 12 dB is a card decision rather than a
project guarantee. The device clamp and the export's own [-1, 1] then
turn the result into a hard-clipped full-scale square for the whole
clip, in monitoring and in the exported file alike, and with the limiter
on the overflow is folded into its gain and colours every later sample.

Clamped at the same site as the other three, so the documented invariant
is true. `MAX_BAND_GAIN_DB`/`MIN_BAND_GAIN_DB` are +/-40 dB: far past
any card (deliberately -- a project may be more extreme than the control
that set it) while keeping `a` a linear gain of 100, comfortably inside
f32. Clamped rather than refused, because the struct's contract is that
a deserializer may hand this over unchecked: a project saying +400 dB
gets +40 dB, not a project that will not open.

Witness (arithmetic, not an audibility claim -- nothing here says what
the filter sounds like, only that it is stable): the poles of
`z^2 + a1 z + a2` solved off the normalized coefficients, asserted
strictly inside the unit circle, plus a full-scale 1 kHz burst whose
peak stays at the ceiling's own gain. Pre-fix, +10000 dB produced
`b0: inf` and +100 dB sent a full-scale burst to 62773.
crates/engine/src/eq.rsmodified+109 -3
@@ -91,16 +91,46 @@ struct Coeffs {
91 a2: f32,91 a2: f32,
92 }92 }
93 93
94+/// How far a band's gain may go either side of flat, in dB: the bound
95+/// [`Coeffs::new`] holds the one field [`Band`]'s doc says is clamped for it.
96+///
97+/// Deliberately far past anything a card offers -- the UI's own limit is a
98+/// card decision, not a project one, and a project is allowed to be more
99+/// extreme than the control that set it -- but bounded all the same, because
100+/// the gain is not a magnitude anywhere else in this struct: it enters as
101+/// `a = 10^(gain_db/40)`, and `a` drives the peak section's pole radius toward
102+/// the unit circle. A few hundred dB puts the poles *on* it, and a section
103+/// whose poles are on the unit circle does not decay, it rings -- growing the
104+/// samples without limit until the device clamp and the export's own [-1, 1]
105+/// turn the whole clip into a full-scale square. Past 10_000 dB `a` is not even
106+/// finite any more and the coefficients come out `inf`.
107+///
108+/// 40 dB is the ceiling because it is where the arithmetic stays comfortably
109+/// inside f32 (a linear gain of 100) while being louder than any boost a
110+/// mixing console offers. Clamped, not refused: the struct's contract is that
111+/// a deserializer may hand this over unchecked, and a project that says
112+/// +400 dB gets +40 dB rather than a project that will not open.
113+pub const MAX_BAND_GAIN_DB: f32 = 40.0;
114+/// ...and the floor, the same distance the other way. Symmetric because a
115+/// wide cut and a wide boost are the same filter run at two gains.
116+pub const MIN_BAND_GAIN_DB: f32 = -40.0;
117+
94 impl Coeffs {118 impl Coeffs {
95 /// Cookbook peaking/shelving section for `band` at `sample_rate`.119 /// Cookbook peaking/shelving section for `band` at `sample_rate`.
96 fn new(band: &Band, sample_rate: u32) -> Self {120 fn new(band: &Band, sample_rate: u32) -> Self {
97 // A frequency at or past Nyquist has no cookbook answer (sin(w0) folds),121 // A frequency at or past Nyquist has no cookbook answer (sin(w0) folds),
98- // and Q of zero divides by zero: clamp rather than reject, the caller's122+ // Q of zero divides by zero, and a gain of a few hundred dB walks the
99- // value came out of a text file.123+ // peak's poles onto the unit circle: clamp rather than reject, the
124+ // caller's value came out of a text file. The gain is the fourth of
125+ // these four clamps, and the one the struct's own doc promised.
100 let fs = f64::from(sample_rate.max(1));126 let fs = f64::from(sample_rate.max(1));
101 let f0 = f64::from(band.freq_hz).clamp(1.0, fs * 0.49);127 let f0 = f64::from(band.freq_hz).clamp(1.0, fs * 0.49);
102 let q = f64::from(band.q).clamp(0.05, 40.0);128 let q = f64::from(band.q).clamp(0.05, 40.0);
103- let a = 10f64.powf(f64::from(band.gain_db) / 40.0);129+ let gain = f64::from(band.gain_db).clamp(
130+ f64::from(MIN_BAND_GAIN_DB),
131+ f64::from(MAX_BAND_GAIN_DB),
132+ );
133+ let a = 10f64.powf(gain / 40.0);
104 let w0 = 2.0 * PI * f0 / fs;134 let w0 = 2.0 * PI * f0 / fs;
105 let (sin_w0, cos_w0) = (w0.sin(), w0.cos());135 let (sin_w0, cos_w0) = (w0.sin(), w0.cos());
106 let alpha = sin_w0 / (2.0 * q);136 let alpha = sin_w0 / (2.0 * q);
@@ -428,4 +458,80 @@ mod tests {
428 );458 );
429 }459 }
430 }460 }
461+
462+ /// A gain a `.edith` line can name, and the pole radius that comes of it.
463+ ///
464+ /// The invariant [`Band`] documents is that out-of-range values are clamped
465+ /// when the coefficients are built, so a deserializer may hand one over
466+ /// unchecked -- and three of the four fields were clamped at
467+ /// [`Coeffs::new`] while `gain_db` was not. `a = 10^(gain_db/40)` feeds the
468+ /// peak's `sqrt(a2/a0)` pole radius straight to 1 as the gain grows, and at
469+ /// a few hundred dB the f32 rounding puts the poles *exactly* on the unit
470+ /// circle: a marginally-stable section that rings for seconds, blowing up
471+ /// the samples until the device clamp turns the whole clip into a
472+ /// full-scale square (in monitoring and in the export alike). This is an
473+ /// arithmetic claim about the pole radius, not an audibility claim: nothing
474+ /// here says what the filter sounds like, only that it is stable.
475+ ///
476+ /// The poles are solved off the normalized coefficients, so this is the
477+ /// filter the sample loop actually runs rather than a restatement of the
478+ /// formula that built it.
479+ #[test]
480+ fn a_gain_a_file_can_name_leaves_the_poles_strictly_inside_the_unit_circle() {
481+ for gain in [12.0f32, 40.0, 100.0, 200.0, 400.0, 10_000.0, -200.0, -10_000.0] {
482+ let c = super::Coeffs::new(
483+ &Band {
484+ freq_hz: 1000.0,
485+ gain_db: gain,
486+ q: 1.0,
487+ kind: BandKind::Peak,
488+ },
489+ FS,
490+ );
491+ // The poles of `z^2 + a1 z + a2`, solved: the number the sample
492+ // loop's homogeneous solution is actually made of, and the only
493+ // place a section's stability is decided. `radius` is the larger
494+ // of the two, so a section is stable exactly when it is `< 1`.
495+ let (a1, a2) = (c.a1 as f64, c.a2 as f64);
496+ let disc = (a1 * a1 - 4.0 * a2).max(0.0).sqrt();
497+ let radius = [(-a1 + disc) / 2.0, (-a1 - disc) / 2.0]
498+ .into_iter()
499+ .fold(0.0f64, |m, r| m.max(r.abs()));
500+ assert!(
501+ radius < 0.999,
502+ "gain {gain} dB gives a pole radius of {radius:.9} -- \
503+ the section rings instead of decaying"
504+ );
505+ assert!(
506+ c.b0.is_finite() && c.b1.is_finite() && c.b2.is_finite(),
507+ "gain {gain} dB produced non-finite coefficients {c:?}"
508+ );
509+ }
510+ }
511+
512+ /// The same claim about the samples rather than the coefficients: a
513+ /// full-scale burst through a band whose gain a file named, with the gain
514+ /// far past the card's own 12 dB limit, has to come out at the gain the
515+ /// clamp ceiling *is*. +40 dB is a linear gain of 100, so a full-scale
516+ /// burst peaks at ~100 and that is the answer, not a fault; anything past
517+ /// it is the section ringing rather than a band being loud. Pre-fix, 200 dB
518+ /// and 400 dB are past it by orders of magnitude.
519+ #[test]
520+ fn a_gain_a_file_can_name_does_not_blow_the_samples_up() {
521+ for gain in [40.0f32, 100.0, 200.0, 400.0, 10_000.0] {
522+ let mut out = vec![0.0f32; 48_000 * 2];
523+ // A full-scale 1 kHz burst for a tenth of a second, then silence:
524+ // whatever the section rings into is what shows up after it.
525+ for i in 0..4_800 * 2 {
526+ out[i] = (TAU * 1000.0 * (i / 2) as f32 / FS as f32).sin();
527+ }
528+ EqState::new(&peak(1000.0, gain, 1.0), FS, 2).process(&mut out);
529+ let peak = out.iter().fold(0.0f32, |m, v| m.max(v.abs()));
530+ assert!(
531+ peak.is_finite() && peak < 200.0,
532+ "gain {gain} dB sent a full-scale burst to {peak}: the clamp ceiling is \
533+ +40 dB, a linear gain of 100, and 200x that is a section ringing, not a band"
534+ );
535+ }
536+ }
431 }537 }