kessel-audio 0.2.0

A real-time voice audio pipeline in pure Rust: devices, echo cancellation, noise suppression and gain control, Opus, jitter buffers with loss concealment, mixing and levels.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Metric tests mirroring the baseline measurement (sonora vs the C++ WebRTC
//! library): far-end speech is encoded, received, decoded and played; the
//! playback goes through a synthetic laptop echo path to the microphone;
//! capture runs echo cancellation (and NS / AGC where stated), Opus
//! encode, and is decoded again by a remote decoder. Every bar compares
//! metrics (ERLE, levels, times, CPU), never samples.
//!
//! The baseline's sonora means are quoted next to each bar. The bars sit below them:
//! the material here is synthetic speech (no corpus download), and the
//! measured path includes the Opus codec.

use super::*;
use crate::apm::NoiseSuppressionStrength;
use crate::testkit::*;

pub(super) const SYS_DELAY: usize = 60 * 48; // 60 ms
pub(super) const ECHO_DB: f64 = -24.0;
pub(super) const NEAR_DB: f64 = -26.0;
pub(super) const FLOOR_DB: f64 = -72.0;

pub(super) struct Case {
    pub(super) rendered: Vec<f32>,
    pub(super) sched: Vec<(u64, Packet)>,
}

/// Far-end talker over `far_range` seconds of a `total`-second call,
/// received with a fixed 30 ms network delay; returns what gets played.
pub(super) fn far_case(cfg: &CoreConfig, seed: u64, total: f64, far_range: (f64, f64)) -> Case {
    let mut rng = Rng::new(seed);
    let n = secs(total);
    let mut far = vec![0.0f32; n];
    let t = speech_track(FAR_POOL, &mut rng, secs(far_range.1 - far_range.0));
    far[secs(far_range.0)..secs(far_range.0) + t.len()].copy_from_slice(&t);
    let packets = encode_remote(&far, EncoderCfg::default());
    let sched = schedule(
        &packets,
        Net {
            base_ms: 30.0,
            ..Net::default()
        },
    );
    let run = run_core(cfg, &sched, n, None);
    Case {
        rendered: run.rendered,
        sched,
    }
}

pub(super) fn mic_from(parts: &[&[f32]], rng: &mut Rng) -> Vec<f32> {
    let n = parts[0].len();
    let mut m = floor_noise(rng, n, FLOOR_DB);
    for p in parts {
        add_into(&mut m, p);
    }
    m
}

/// Run the capture side against a fixed playback; checks the playback is
/// reproduced exactly (the pipeline is deterministic).
fn duplex(cfg: &CoreConfig, case: &Case, mic: &[f32]) -> CallRun {
    let run = run_core(cfg, &case.sched, mic.len(), Some(mic));
    assert_eq!(
        run.rendered, case.rendered,
        "playback differs between runs: the pipeline must be deterministic"
    );
    run
}

pub(super) fn report(name: &str, values: &[(String, f64)]) {
    let line: Vec<String> = values.iter().map(|(k, v)| format!("{k}={v:.2}")).collect();
    eprintln!("[metrics] {name}: {}", line.join(" "));
}

/// S1 (baseline criterion 1): far-end speech only. Baseline, sonora (AEC3 + HPF):
/// steady ERLE 50.4 dB mean, 40.0 worst; first 2 s 33.5 dB.
#[test]
fn s1_far_end_only_erle() {
    let _cpu = crate::tests::shared_cpu();
    let cfg = core_cfg(aec_only());
    let mut steady = Vec::new();
    for (ri, room) in ROOMS.iter().enumerate() {
        let seed = 1000 + ri as u64 * 100;
        let case = far_case(&cfg, seed, 30.0, (0.0, 30.0));
        let mut rng = Rng::new(seed + 1);
        let pose = laptop_pose(room, &mut rng);
        let path = echo_path(room, &pose, &mut rng);
        let echo = make_echo(&case.rendered, &path, SYS_DELAY, ECHO_DB);
        let mic = mic_from(&[echo.as_slice()], &mut rng);
        let run = duplex(&cfg, &case, &mic);
        let far = active_frames(&echo);
        let e_steady = erle(&mic, &run.out, &far, 500, 3000).expect("enough far-end frames");
        let e_first = erle(&mic, &run.out, &far, 0, 200).expect("enough far-end frames");
        let t20 = convergence_s(&mic, &run.out, &far, 0, 3000, 20.0);
        report(
            &format!("S1 {}", room.name),
            &[
                ("erle_5_30s_db".into(), e_steady),
                ("erle_first_2s_db".into(), e_first),
                ("t_to_20db_s".into(), t20.unwrap_or(f64::NAN)),
            ],
        );
        let aec_delay = run
            .stats
            .aec_delay_ms
            .expect("the echo canceller reports its delay");
        report(
            &format!("S1 {}", room.name),
            &[("aec_delay_ms".into(), aec_delay as f64)],
        );
        // The echo path's delay is 60 ms (the render reference is fed in the
        // same 10 ms block it is played).
        assert!(
            (45..=75).contains(&aec_delay),
            "{}: AEC delay {aec_delay} ms",
            room.name
        );
        assert!(
            e_steady >= 38.0,
            "{}: steady ERLE {e_steady:.1} dB < 38",
            room.name
        );
        let t20 = t20.unwrap_or_else(|| panic!("{}: never reached 20 dB ERLE", room.name));
        assert!(t20 <= 0.5, "{}: {t20:.2} s to 20 dB ERLE", room.name);
        steady.push(e_steady);
    }
    let m = mean(&steady);
    report("S1 mean", &[("erle_5_30s_db".into(), m)]);
    assert!(m >= 42.0, "mean steady ERLE {m:.1} dB < 42");
}

struct DoubleTalk {
    near_gain_db: f64,
    gap_erle_db: f64,
    after_far_db: f64,
}

/// Far 0-30 s, near 10-40 s (baseline S2). Compares against the same near-end
/// alone through the same chain.
fn double_talk(processing: ProcessingConfig, ri: usize, seed: u64) -> DoubleTalk {
    let cfg = core_cfg(processing);
    let room = &ROOMS[ri];
    let case = far_case(&cfg, seed, 40.0, (0.0, 30.0));
    let mut rng = Rng::new(seed + 7);
    let pose = laptop_pose(room, &mut rng);
    let path = echo_path(room, &pose, &mut rng);
    let echo = make_echo(&case.rendered, &path, SYS_DELAY, ECHO_DB);
    let n = echo.len();
    let mut near_dry = vec![0.0f32; n];
    let nt = speech_track(NEAR_POOL, &mut rng, n - secs(10.0));
    near_dry[secs(10.0)..secs(10.0) + nt.len()].copy_from_slice(&nt);
    let near = near_at_mic(&near_dry, room, &pose, NEAR_DB);
    let floor = floor_noise(&mut rng, n, FLOOR_DB);
    let mut mic = floor.clone();
    add_into(&mut mic, &echo);
    add_into(&mut mic, &near);
    let mut mic_ref = floor;
    add_into(&mut mic_ref, &near);

    let run = duplex(&cfg, &case, &mic);
    let silent = Case {
        rendered: vec![0.0; case.rendered.len()],
        sched: Vec::new(),
    };
    let reference = duplex(&cfg, &silent, &mic_ref);

    let near_m = active_frames(&near);
    let far_m = active_frames(&echo);
    let dt = mask_and(&near_m, &far_m);
    let near_gain_db =
        level_on(&run.out, &dt, 1200, 3000) - level_on(&reference.out, &dt, 1200, 3000);
    let gaps = mask_and_not(&far_m, &near_m);
    let gap_erle_db = erle(&mic, &run.out, &gaps, 1200, 3000).expect("far-only gaps");
    let after_far_db =
        level_on(&run.out, &near_m, 3100, 4000) - level_on(&reference.out, &near_m, 3100, 4000);
    DoubleTalk {
        near_gain_db,
        gap_erle_db,
        after_far_db,
    }
}

/// S2 (baseline criterion 2): double talk, AEC3 + HPF. Baseline, sonora: near-end gain
/// in double talk -4.0 dB (worst -5.8); ERLE in far-only gaps 42.1 dB
/// (worst 30.2).
#[test]
fn s2_double_talk_keeps_the_near_end() {
    let _cpu = crate::tests::shared_cpu();
    for (ri, seed) in [(0usize, 2000u64), (2, 2100)] {
        let r = double_talk(aec_only(), ri, seed);
        report(
            &format!("S2 aec {}", ROOMS[ri].name),
            &[
                ("near_gain_dt_db".into(), r.near_gain_db),
                ("gap_erle_db".into(), r.gap_erle_db),
                ("near_after_far_db".into(), r.after_far_db),
            ],
        );
        assert!(
            r.near_gain_db >= -7.0,
            "near end ducked {:.1} dB in double talk",
            r.near_gain_db
        );
        assert!(
            r.gap_erle_db >= 32.0,
            "ERLE in far-only gaps {:.1} dB",
            r.gap_erle_db
        );
        assert!(
            r.after_far_db.abs() <= 1.0,
            "near end after far stops off by {:.1} dB",
            r.after_far_db
        );
    }
}

/// The default chain (AEC3 + NS + AGC2). Baseline, sonora: far-only ERLE 33.6 dB,
/// gaps 46.6 dB.
#[test]
fn s2_double_talk_full_chain() {
    let _cpu = crate::tests::shared_cpu();
    let r = double_talk(ProcessingConfig::default(), 1, 2200);
    report(
        &format!("S2 full {}", ROOMS[1].name),
        &[
            ("near_gain_dt_db".into(), r.near_gain_db),
            ("gap_erle_db".into(), r.gap_erle_db),
            ("near_after_far_db".into(), r.after_far_db),
        ],
    );
    assert!(
        r.gap_erle_db >= 35.0,
        "ERLE in far-only gaps {:.1} dB",
        r.gap_erle_db
    );
    assert!(
        r.near_gain_db >= -7.0,
        "near end ducked {:.1} dB",
        r.near_gain_db
    );
    assert!(
        r.after_far_db.abs() <= 1.5,
        "near end after far stops off by {:.1} dB",
        r.after_far_db
    );
}

/// S3 (baseline criterion 3): the echo path changes at 20 s. (a) the laptop
/// moves and the speaker gets 3 dB louder; (b) the system delay jumps by
/// 40 ms. Baseline, sonora: time to 45 dB ERLE 0.01 s (a) and 0.32 s (b).
#[test]
fn s3_reconverges_after_an_echo_path_change() {
    let _cpu = crate::tests::shared_cpu();
    let cfg = core_cfg(aec_only());
    let room = &ROOMS[1];
    let seed = 3000;
    let case = far_case(&cfg, seed, 40.0, (0.0, 40.0));
    let mut rng = Rng::new(seed + 1);
    let pose_a = laptop_pose(room, &mut rng);
    let path_a = echo_path(room, &pose_a, &mut rng);
    let pose_b = laptop_pose(room, &mut rng);
    let path_b = echo_path(room, &pose_b, &mut rng);
    let change = secs(20.0);
    let echo_a = make_echo(&case.rendered, &path_a, SYS_DELAY, ECHO_DB);
    for (label, echo_b) in [
        (
            "moved+3dB",
            make_echo(&case.rendered, &path_b, SYS_DELAY, ECHO_DB + 3.0),
        ),
        (
            "delay+40ms",
            make_echo(&case.rendered, &path_a, SYS_DELAY + 40 * 48, ECHO_DB),
        ),
    ] {
        let mut echo = echo_a.clone();
        echo[change..].copy_from_slice(&echo_b[change..]);
        let mic = mic_from(&[echo.as_slice()], &mut rng);
        let run = duplex(&cfg, &case, &mic);
        let far = active_frames(&echo);
        let before = erle(&mic, &run.out, &far, 1000, 2000).expect("frames");
        let after_2s = erle(&mic, &run.out, &far, 2000, 2200).expect("frames");
        let settled = erle(&mic, &run.out, &far, 3000, 4000).expect("frames");
        let t30 = convergence_s(&mic, &run.out, &far, 2000, 4000, 30.0);
        report(
            &format!("S3 {label}"),
            &[
                ("erle_before_db".into(), before),
                ("erle_first_2s_after_db".into(), after_2s),
                ("erle_settled_db".into(), settled),
                ("t_to_30db_s".into(), t30.unwrap_or(f64::NAN)),
            ],
        );
        let t30 = t30.unwrap_or_else(|| panic!("{label}: never got back to 30 dB ERLE"));
        assert!(t30 <= 0.5, "{label}: {t30:.2} s to get back to 30 dB ERLE");
        assert!(settled >= 40.0, "{label}: settled ERLE {settled:.1} dB");
        assert!(
            after_2s >= 30.0,
            "{label}: ERLE in the first 2 s after {after_2s:.1} dB"
        );
    }
}

/// S4: noise suppression, pink noise at 5 dB SNR. Baseline, sonora
/// (moderate): 11.9 dB noise cut in pauses, measured against the raw input
/// (so it includes the high-pass filter's share).
#[test]
fn s4_noise_suppression_cuts_noise_in_pauses() {
    let _cpu = crate::tests::shared_cpu();
    let processing = ProcessingConfig {
        echo_cancellation: false,
        noise_suppression: Some(NoiseSuppressionStrength::Moderate),
        gain_control: false,
        high_pass: true,
        capture_stage: None,
        stage_fallback_noise_suppression: None,
    };
    let cfg = core_cfg(processing);
    let mut rng = Rng::new(4000);
    let n = secs(20.0);
    let speech = speech_track(NEAR_POOL, &mut rng, n);
    let mut noise = pink(&mut rng, n);
    let sl = active_level_db(&speech);
    let nl = db(energy(&noise) / n as f64);
    scale(&mut noise, 10f64.powf((sl - 5.0 - nl) / 20.0));
    let mut mic = speech.clone();
    add_into(&mut mic, &noise);
    let silent = Case {
        rendered: vec![0.0; n],
        sched: Vec::new(),
    };
    let run = duplex(&cfg, &silent, &mic);
    // Reference: the same input with NS off, through the same codec.
    let plain = duplex(
        &core_cfg(crate::apm::ApmSettings {
            noise_suppression: None,
            ..cfg.processing
        }),
        &silent,
        &mic,
    );
    let speech_m = active_frames(&speech);
    let pauses = mask_not(&speech_m);
    let vs_input = level_on(&mic, &pauses, 300, 2000) - level_on(&run.out, &pauses, 300, 2000);
    let ns_alone =
        level_on(&plain.out, &pauses, 300, 2000) - level_on(&run.out, &pauses, 300, 2000);
    let speech_change =
        level_on(&run.out, &speech_m, 300, 2000) - level_on(&plain.out, &speech_m, 300, 2000);
    report(
        "S4 pink 5 dB",
        &[
            ("noise_cut_vs_input_db".into(), vs_input),
            ("noise_cut_ns_alone_db".into(), ns_alone),
            ("speech_change_db".into(), speech_change),
        ],
    );
    assert!(
        vs_input >= 12.0,
        "noise cut in pauses {vs_input:.1} dB < 12"
    );
    assert!(
        ns_alone >= 8.0,
        "noise suppression alone cut {ns_alone:.1} dB < 8"
    );
    assert!(
        speech_change >= -2.5,
        "speech level changed {speech_change:.1} dB"
    );
}

/// The C++ WebRTC library's AGC2 output levels in the baseline (input -50,
/// -40, -30, -20 dBFS).
const CPP_AGC2_LEVELS: [f64; 4] = [-27.3, -23.1, -23.1, -20.6];

/// S6: AGC2 levelling, near-end only at -50 to -20 dBFS, through the tuned
/// AGC2. Baseline (sonora untuned / C++): -27.0/-23.8/-24.0/-20.7 and
/// -27.3/-23.1/-23.1/-20.6 dBFS; spread 6.3 / 6.8 dB.
#[test]
fn s6_agc2_levels_speech() {
    let _cpu = crate::tests::shared_cpu();
    let processing = ProcessingConfig {
        echo_cancellation: false,
        noise_suppression: None,
        gain_control: true,
        high_pass: true,
        capture_stage: None,
        stage_fallback_noise_suppression: None,
    };
    let cfg = core_cfg(processing);
    let room = &ROOMS[0];
    let mut rng = Rng::new(6000);
    let pose = laptop_pose(room, &mut rng);
    let n = secs(30.0);
    let near_dry = speech_track(NEAR_POOL, &mut rng, n);
    let silent = Case {
        rendered: vec![0.0; n],
        sched: Vec::new(),
    };
    let mut levels = Vec::new();
    for lvl in [-50.0f64, -40.0, -30.0, -20.0] {
        let near = near_at_mic(&near_dry, room, &pose, lvl);
        let mask = active_frames(&near);
        let mic = mic_from(&[near.as_slice()], &mut rng);
        let run = duplex(&cfg, &silent, &mic);
        let l = level_on(&run.out, &mask, 1500, 3000);
        report(&format!("S6 input {lvl}"), &[("output_dbfs".into(), l)]);
        levels.push(l);
    }
    let spread = max(&levels) - min(&levels);
    report("S6", &[("spread_db".into(), spread)]);
    // Within 1.5 dB of the C++ library's levels on the same kind of input.
    for (i, (l, c)) in levels.iter().zip(CPP_AGC2_LEVELS).enumerate() {
        assert!(
            (l - c).abs() <= 1.5,
            "input {} dBFS came out at {l:.1} dBFS (C++ library: {c:.1})",
            -50 + 10 * i as i32
        );
    }
    assert!(
        spread <= 7.5,
        "spread {spread:.1} dB over a 30 dB input range"
    );
}

/// Baseline criterion 5: CPU per 10 ms block. The baseline's bar was for
/// the echo canceller chain alone (AEC3 + NS + AGC2): under 5% of one core
/// (it measured 1.55%, p50 150.6 µs). That bar holds here, timed alone.
/// The whole duplex path adds the jitter buffer, Opus encode (complexity 9)
/// and decode, and the mix; its bar is 10% of a core.
///
/// Only where the DSP crates are optimised: a release build, or the `cpu-bars`
/// feature when the root manifest optimises them in debug builds. Unoptimised
/// the echo canceller alone takes about 3 ms of each 10 ms block, and the
/// figure says nothing about real use.
#[cfg(any(not(debug_assertions), feature = "cpu-bars"))]
#[test]
fn cpu_per_block_within_bars() {
    let inputs = super::shared_cpu();
    let cfg = core_cfg(ProcessingConfig::default());
    let case = far_case(&cfg, 5000, 20.0, (0.0, 20.0));
    let mut rng = Rng::new(5001);
    let room = &ROOMS[1];
    let pose = laptop_pose(room, &mut rng);
    let path = echo_path(room, &pose, &mut rng);
    let echo = make_echo(&case.rendered, &path, SYS_DELAY, ECHO_DB);
    let mic = mic_from(&[echo.as_slice()], &mut rng);
    drop(inputs);
    let (run, apm_ns) = {
        let _alone = super::exclusive_cpu();
        let run = run_core_inner(&cfg, &case.sched, mic.len(), Some(&mic));
        let mut apm = crate::apm::Apm::new(cfg.processing);
        let mut out = [0.0f32; BLOCK_SAMPLES];
        let mut apm_ns = Vec::new();
        for (r, m) in case
            .rendered
            .as_chunks::<BLOCK_SAMPLES>()
            .0
            .iter()
            .zip(mic.as_chunks::<BLOCK_SAMPLES>().0.iter())
        {
            let t = Instant::now();
            apm.render(r);
            apm.capture(m, &mut out);
            apm_ns.push(t.elapsed().as_nanos() as u64);
        }
        (run, apm_ns)
    };
    assert_eq!(
        run.rendered, case.rendered,
        "the pipeline must be deterministic"
    );
    let stats = |v: &[u64]| -> (f64, f64, f64) {
        let mut ns = v.to_vec();
        ns.sort_unstable();
        let p50 = ns[ns.len() / 2] as f64 / 1000.0;
        let p99 = ns[ns.len() * 99 / 100] as f64 / 1000.0;
        let mean = ns.iter().sum::<u64>() as f64 / ns.len() as f64 / 1000.0;
        (p50, p99, mean)
    };
    let (d50, d99, dmean) = stats(&run.block_ns);
    let (a50, a99, amean) = stats(&apm_ns);
    report(
        "CPU",
        &[
            ("apm_p50_us".into(), a50),
            ("apm_p99_us".into(), a99),
            ("apm_pct_core".into(), amean / 100.0),
            ("duplex_p50_us".into(), d50),
            ("duplex_p99_us".into(), d99),
            ("duplex_pct_core".into(), dmean / 100.0),
            ("debug_build".into(), cfg!(debug_assertions) as u8 as f64),
        ],
    );
    assert!(
        a50 <= 500.0,
        "echo canceller chain: median block {a50:.0} µs (> 5% of a core)"
    );
    assert!(
        d50 <= 1000.0,
        "duplex path: median block {d50:.0} µs (> 10% of a core)"
    );
}

/// The AGC2 tuning holds: with the tuned headroom, speech at -40 and
/// -30 dBFS comes out louder than with WebRTC's default headroom by about
/// the 1 dB the C++ comparison asked for.
#[test]
fn s6_agc2_tuning_lifts_mid_levels_by_about_a_db() {
    let _cpu = crate::tests::shared_cpu();
    use crate::apm::{Apm, AGC2_HEADROOM_DB};
    let processing = ProcessingConfig {
        echo_cancellation: false,
        noise_suppression: None,
        gain_control: true,
        high_pass: true,
        capture_stage: None,
        stage_fallback_noise_suppression: None,
    };
    let room = &ROOMS[0];
    let mut rng = Rng::new(6100);
    let pose = laptop_pose(room, &mut rng);
    let n = secs(30.0);
    let near_dry = speech_track(NEAR_POOL, &mut rng, n);
    for lvl in [-40.0f64, -30.0] {
        let near = near_at_mic(&near_dry, room, &pose, lvl);
        let mask = active_frames(&near);
        let mic = mic_from(&[near.as_slice()], &mut rng);
        let run = |headroom: f32| -> f64 {
            let mut apm = Apm::with_agc_headroom(&processing, headroom);
            let zeros = [0.0f32; BLOCK_SAMPLES];
            let mut out = vec![0.0f32; mic.len()];
            for (i, b) in mic.as_chunks::<BLOCK_SAMPLES>().0.iter().enumerate() {
                apm.render(&zeros);
                apm.capture(b, &mut out[i * BLOCK_SAMPLES..(i + 1) * BLOCK_SAMPLES]);
            }
            level_on(&out, &mask, 1500, 3000)
        };
        let default = run(5.0);
        let tuned = run(AGC2_HEADROOM_DB);
        report(
            &format!("S6 tuning input {lvl}"),
            &[
                ("default_dbfs".into(), default),
                ("tuned_dbfs".into(), tuned),
            ],
        );
        let lift = tuned - default;
        assert!(
            (0.6..=1.4).contains(&lift),
            "input {lvl}: tuning lifted the level {lift:.2} dB"
        );
    }
}