Engaging mid-stream costs a permanent shift, and here it is measured
Browse files at this commit41560939042715362328a43786f2b6214bc6e7cbParent: 8b784e6- docs
- engaging mid-stream costs a permanent shift, and here it is measured
1 changed file +98 -6
- crates/engine/src/limiter.rs +98 -6
crates/engine/src/limiter.rsmodified+98 -6
| @@ -140,14 +140,24 @@ impl LimiterState { | ||
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | /// One switched in **while the mix is already running**: the same limiter, |
| 143 | - /// except that it swallows nothing to fill its delay line -- what comes out | |
| 144 | - /// first is the line's own [`LOOKAHEAD_MS`] of silence and the mix behind | |
| 145 | - /// it, where a fresh state would have eaten that much of the mix instead. | |
| 143 | + /// except that it swallows nothing to fill its delay line -- the line is | |
| 144 | + /// zeroed and emitting from the first frame, where [`new`] would have eaten | |
| 145 | + /// that much of the mix at the front instead. | |
| 146 | 146 | /// |
| 147 | 147 | /// Which matters because the count *is* the clock: a mixer that emitted |
| 148 | 148 | /// fewer samples than the timeline asked for would slide the picture off |
| 149 | - /// the sound by the lookahead, once per switch-on. A hair of silence at the | |
| 150 | - /// moment somebody engages a limiter is the honest price. | |
| 149 | + /// the sound by the lookahead, once per switch-on. | |
| 150 | + /// | |
| 151 | + /// The price, stated as it measures rather than as it was hoped: not a | |
| 152 | + /// momentary hair of silence the stream grows back from, but a **permanent** | |
| 153 | + /// shift of [`LOOKAHEAD_MS`] for the rest of the stream -- the output is | |
| 154 | + /// the input delayed by the whole line, every block, because nothing ever | |
| 155 | + /// re-aligns it. That is at most 2 ms, which is under the perceptual | |
| 156 | + /// threshold, and it is paid once per stream rather than growing; it is | |
| 157 | + /// pinned by | |
| 158 | + /// `tests::an_engaged_limiter_shifts_the_stream_by_the_lookahead_for_good`. | |
| 159 | + /// Changing it is a design call about the export's A/V alignment, not a | |
| 160 | + /// bug fix. | |
| 151 | 161 | pub fn engaged(params: &Limiter, sample_rate: u32, channels: usize) -> Self { |
| 152 | 162 | Self { |
| 153 | 163 | warm: 0, |
| @@ -314,7 +324,10 @@ mod tests { | ||
| 314 | 324 | state.process(&mut buf); |
| 315 | 325 | assert_eq!(buf.len(), block.len(), "a retune ate samples"); |
| 316 | 326 | let quiet = buf.iter().fold(0.0f32, |m, s| m.max(s.abs())); |
| 317 | - assert!(quiet <= db_to_linear(-12.0) + 1e-6, "peak {quiet} passed -12 dB"); | |
| 327 | + assert!( | |
| 328 | + quiet <= db_to_linear(-12.0) + 1e-6, | |
| 329 | + "peak {quiet} passed -12 dB" | |
| 330 | + ); | |
| 318 | 331 | assert!(quiet < hot, "the ceiling did not move: {hot} -> {quiet}"); |
| 319 | 332 | |
| 320 | 333 | // Switched out: the line still delays, so the count still holds, and |
| @@ -330,6 +343,85 @@ mod tests { | ||
| 330 | 343 | assert!(raw > 1.0, "bypassed, the mix is untouched: peak {raw}"); |
| 331 | 344 | } |
| 332 | 345 | |
| 346 | + /// What engaging a limiter mid-stream actually costs, measured rather than | |
| 347 | + /// described: not a momentary hair of silence that the stream then grows | |
| 348 | + /// back from, but a **permanent** shift. `engaged` starts its delay line | |
| 349 | + /// empty and swallows nothing, so from the first block onward the output is | |
| 350 | + /// the input delayed by the whole lookahead -- for the rest of the stream. | |
| 351 | + /// `new` is head-aligned instead: it swallows the lookahead once, at the | |
| 352 | + /// front, and is a block shorter for it. | |
| 353 | + /// | |
| 354 | + /// The two are measured against each other because the difference is the | |
| 355 | + /// whole claim: same tone, same settings, one index offset and one block | |
| 356 | + /// length apart. A permanent offset of at most [`LOOKAHEAD_MS`] is below | |
| 357 | + /// the perceptual threshold, so this pins the *measured* behaviour the doc | |
| 358 | + /// comment on [`LimiterState::engaged`] describes rather than ruling on | |
| 359 | + /// whether it should be changed -- that is a design call, not a bug fix. | |
| 360 | + #[test] | |
| 361 | + fn an_engaged_limiter_shifts_the_stream_by_the_lookahead_for_good() { | |
| 362 | + let rate = 48_000; | |
| 363 | + let params = Limiter { | |
| 364 | + ceiling_db: -3.0, | |
| 365 | + on: true, | |
| 366 | + }; | |
| 367 | + let lookahead = ((LOOKAHEAD_MS / 1000.0 * rate as f32) as usize).max(1); | |
| 368 | + // Under the knee throughout, so what comes out is the input and the | |
| 369 | + // only thing being measured is *when* each frame appears. | |
| 370 | + let tone: Vec<f32> = (0..4_000) | |
| 371 | + .flat_map(|i| { | |
| 372 | + let s = 0.1 * (i as f32 / rate as f32 * 440.0 * std::f32::consts::TAU).sin(); | |
| 373 | + [s, s] | |
| 374 | + }) | |
| 375 | + .collect(); | |
| 376 | + | |
| 377 | + // `new`: head-aligned, and a block shorter by exactly the lookahead. | |
| 378 | + let mut fresh = LimiterState::new(¶ms, rate, 2); | |
| 379 | + let mut head = tone.clone(); | |
| 380 | + fresh.process(&mut head); | |
| 381 | + assert_eq!( | |
| 382 | + head.len(), | |
| 383 | + tone.len() - lookahead * 2, | |
| 384 | + "a fresh limiter is not head-aligned by exactly the lookahead" | |
| 385 | + ); | |
| 386 | + | |
| 387 | + // `engaged`: the same count, the same samples -- one lookahead later. | |
| 388 | + let mut live = LimiterState::engaged(¶ms, rate, 2); | |
| 389 | + let mut shifted = tone.clone(); | |
| 390 | + live.process(&mut shifted); | |
| 391 | + assert_eq!( | |
| 392 | + shifted.len(), | |
| 393 | + tone.len(), | |
| 394 | + "an engaged limiter ate samples, which is the thing it exists not to do" | |
| 395 | + ); | |
| 396 | + assert_eq!( | |
| 397 | + &shifted[lookahead * 2..], | |
| 398 | + &head[..], | |
| 399 | + "an engaged limiter is not the fresh one delayed by exactly the \ | |
| 400 | + lookahead, so what it costs mid-stream is not what the doc claims" | |
| 401 | + ); | |
| 402 | + // And the shift is the *stream's*, not one block's. A second block of a | |
| 403 | + // marker the first did not contain comes out *still* delayed by the | |
| 404 | + // lookahead: the head of it is still the tail of the tone, and the | |
| 405 | + // marker itself only appears from the lookahead on. Nothing re-aligns | |
| 406 | + // the line, which is what makes the cost permanent rather than a one-off | |
| 407 | + // at the moment of engaging. | |
| 408 | + let marker = vec![0.5; tone.len()]; | |
| 409 | + let mut next = marker.clone(); | |
| 410 | + live.process(&mut next); | |
| 411 | + assert!( | |
| 412 | + next[..lookahead * 2].iter().any(|s| *s != 0.5), | |
| 413 | + "the second block is aligned where the first was not, so the cost is \ | |
| 414 | + a one-off rather than a permanent shift" | |
| 415 | + ); | |
| 416 | + assert!( | |
| 417 | + next[lookahead * 2..] | |
| 418 | + .iter() | |
| 419 | + .all(|s| (*s - 0.5).abs() < 1e-6), | |
| 420 | + "the marker is not behind by exactly the lookahead: the stream is not \ | |
| 421 | + delayed by a fixed amount from here on" | |
| 422 | + ); | |
| 423 | + } | |
| 424 | + | |
| 333 | 425 | /// Flat settings are a passthrough, which is what the export invariant |
| 334 | 426 | /// rests on: 0 dB is exactly 1.0, never 0.9999999. |
| 335 | 427 | #[test] |