broadcast-loudness 0.2.0

EBU R 128 / ITU-R BS.1770-5 loudness measurement: K-weighting, integrated/short-term/momentary loudness (LUFS), loudness range (LRA), and true-peak (dBTP). no_std + alloc.
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
//! Loudness meter implementing ITU-R BS.1770‑5 and EBU R 128 / Tech 3341.
//!
//! Provides momentary (400 ms), short‑term (3 s), and integrated (gated)
//! loudness in LUFS, plus Loudness Range (LRA) per EBU Tech 3342.

use alloc::vec::Vec;

use crate::channel_layout::ChannelLayout;
use crate::filter::{BiquadCoeffs, BiquadState, apply_biquad, k_weighting_coeffs};

/// —70 LUFS absolute gating threshold (ITU‑R BS.1770‑5 §Annex 1, eq. 6).
const ABSOLUTE_GATE: f64 = -70.0;

/// —10 LU relative gating threshold (ITU‑R BS.1770‑5 §Annex 1, eq. 6).
const RELATIVE_GATE: f64 = -10.0;

/// Gating block duration in seconds (ITU‑R BS.1770‑5 §Annex 1).
const GATING_BLOCK_S: f64 = 0.4;

/// Overlap fraction (75%) of gating blocks (ITU‑R BS.1770‑5 §Annex 1).
const GATING_OVERLAP: f64 = 0.75;

/// Momentary window duration in seconds (EBU Tech 3341 §2.2.1).
const MOMENTARY_S: f64 = 0.4;

/// Short‑term window duration in seconds (EBU Tech 3341 §2.2.2).
const SHORT_TERM_S: f64 = 3.0;

/// The constant —0.691 in BS.1770‑5 eq. (2), cancelling the K‑weighting
/// gain for a 997 Hz tone.
const LOUDNESS_OFFSET: f64 = -0.691;

/// BS.1770‑5 Annex 1 eq. (2): convert mean‑square to LKFS.
///
/// `mean_sq` is the K‑weighted, channel‑weighted mean square.
#[inline]
fn mean_sq_to_lkfs(mean_sq: f64) -> f64 {
    if mean_sq <= 0.0 {
        f64::NEG_INFINITY
    } else {
        LOUDNESS_OFFSET + 10.0 * libm::log10(mean_sq)
    }
}

/// BS.1770‑5 Annex 1 inverse: LKFS → mean square.
#[inline]
fn lkfs_to_mean_sq(lkfs: f64) -> f64 {
    libm::pow(10.0, (lkfs - LOUDNESS_OFFSET) / 10.0)
}

/// Per‑channel K‑weighting filter state.
#[derive(Debug, Clone)]
struct ChannelFilter {
    stage1: BiquadState,
    stage2: BiquadState,
    coeffs: BiquadCoeffsPair,
}

/// The two K‑weighting biquad coefficient sets (shelving + high‑pass),
/// shared by all channels.
#[derive(Debug, Clone, Copy)]
struct BiquadCoeffsPair {
    stage1: BiquadCoeffs,
    stage2: BiquadCoeffs,
}

impl ChannelFilter {
    fn new(stage1: BiquadCoeffs, stage2: BiquadCoeffs) -> Self {
        Self {
            stage1: BiquadState::new(),
            stage2: BiquadState::new(),
            coeffs: BiquadCoeffsPair { stage1, stage2 },
        }
    }

    /// Apply the cascaded K‑weighting to one sample.
    #[inline]
    fn process(&mut self, sample: f64) -> f64 {
        let y1 = apply_biquad(sample, &self.coeffs.stage1, &mut self.stage1);
        apply_biquad(y1, &self.coeffs.stage2, &mut self.stage2)
    }
}

/// A pre‑computed gating block loudness value (for the integrated measurement).
#[derive(Debug, Clone, Copy)]
struct GatingBlock {
    /// Loudness in LKFS of this 400 ms block.
    lkfs: f64,
}

/// EBU R 128 / ITU‑R BS.1770‑5 loudness meter.
///
/// Feed planar or interleaved PCM samples, then query momentary, short‑term,
/// integrated loudness (LUFS), loudness range (LU), and per‑channel true‑peak.
///
/// ## Measurement flow
///
/// ```text
/// input samples → K‑weighting → channel weighting → mean square
///   → gating blocks (400 ms, 75% overlap)
///   → momentary (sliding 400 ms), short‑term (sliding 3 s)
///   → integrated (gated: absolute then relative)
/// ```
#[derive(Debug, Clone)]
pub struct LoudnessMeter {
    sample_rate: u32,
    layout: ChannelLayout,
    channel_count: usize,

    /// Per‑channel K‑weighting filter state.
    filters: Vec<ChannelFilter>,

    /// The K‑weighting biquad coefficients (derived for `sample_rate`).
    coeffs: BiquadCoeffsPair,

    /// Buffer of K‑weighted, channel‑weighted sample energies.
    /// Each entry is the sum-of-squares (weighted) for one sample frame.
    weighted_power: Vec<f64>,

    /// Gating block loudness values (computed on `finish()`).
    gating_blocks: Vec<GatingBlock>,

    /// Integrated loudness result (computed on `finish()`).
    integrated: f64,

    /// Loudness range result (computed on `finish()`).
    lra: f64,

    /// Maximum momentary loudness.
    max_momentary: f64,

    /// Maximum short‑term loudness.
    max_short_term: f64,

    /// Whether `finish()` has been called.
    finished: bool,

    /// Number of sample frames pushed.
    frame_count: usize,
}

impl LoudnessMeter {
    /// Create a new loudness meter.
    ///
    /// `sample_rate` — input sample rate in Hz. Any rate greater than zero is
    /// accepted (e.g. 44100, 48000, 96000, 192000). The K‑weighting filter
    /// coefficients are derived for the given rate by a bilinear transform of
    /// the analog prototype filters with frequency pre‑warping (the same
    /// derivation libebur128 uses); at 48 kHz they match the ITU‑R BS.1770‑5
    /// Annex 1 tabulated coefficients to within floating‑point epsilon.
    ///
    /// Returns `Error::InvalidSampleRate` if `sample_rate == 0`.
    ///
    /// `layout` — channel configuration with per‑channel weights.
    pub fn new(sample_rate: u32, layout: ChannelLayout) -> Result<Self, crate::Error> {
        if sample_rate == 0 {
            return Err(crate::Error::InvalidSampleRate { got: sample_rate });
        }
        let (stage1, stage2) = k_weighting_coeffs(sample_rate);
        let coeffs = BiquadCoeffsPair { stage1, stage2 };
        let channel_count = layout.channel_count();
        Ok(Self {
            sample_rate,
            layout,
            channel_count,
            filters: (0..channel_count)
                .map(|_| ChannelFilter::new(stage1, stage2))
                .collect(),
            coeffs,
            weighted_power: Vec::new(),
            gating_blocks: Vec::new(),
            integrated: f64::NEG_INFINITY,
            lra: 0.0,
            max_momentary: f64::NEG_INFINITY,
            max_short_term: f64::NEG_INFINITY,
            finished: false,
            frame_count: 0,
        })
    }

    /// Reset the meter for a new measurement.
    pub fn reset(&mut self) {
        for f in &mut self.filters {
            *f = ChannelFilter::new(self.coeffs.stage1, self.coeffs.stage2);
        }
        self.weighted_power.clear();
        self.gating_blocks.clear();
        self.integrated = f64::NEG_INFINITY;
        self.lra = 0.0;
        self.max_momentary = f64::NEG_INFINITY;
        self.max_short_term = f64::NEG_INFINITY;
        self.finished = false;
        self.frame_count = 0;
    }

    /// Push one frame of planar f32 samples.
    ///
    /// `channels` must have length equal to `self.channel_count`.
    /// Each entry is the sample for one channel at this time instant.
    pub fn push_f32(&mut self, channels: &[f32]) -> Result<(), crate::Error> {
        if self.finished {
            return Err(crate::Error::Finished);
        }
        if channels.len() != self.channel_count {
            return Err(crate::Error::ChannelMismatch {
                expected: self.channel_count,
                got: channels.len(),
            });
        }
        let mut sum_sq = 0.0f64;
        for (i, &sample) in channels.iter().enumerate() {
            let sample_f64 = f64::from(sample);
            if !sample_f64.is_finite() {
                return Err(crate::Error::NonFiniteSample {
                    index: self.frame_count,
                    channel: i,
                    value: sample_f64,
                });
            }
            let weight = self.layout.weight(i);
            if weight == 0.0 {
                continue;
            }
            let filtered = self.filters[i].process(sample_f64);
            sum_sq += weight * filtered * filtered;
        }
        self.weighted_power.push(sum_sq);
        self.frame_count += 1;
        Ok(())
    }

    /// Push one frame of planar f64 samples.
    pub fn push_f64(&mut self, channels: &[f64]) -> Result<(), crate::Error> {
        if self.finished {
            return Err(crate::Error::Finished);
        }
        if channels.len() != self.channel_count {
            return Err(crate::Error::ChannelMismatch {
                expected: self.channel_count,
                got: channels.len(),
            });
        }
        let mut sum_sq = 0.0f64;
        for (i, &sample) in channels.iter().enumerate() {
            if !sample.is_finite() {
                return Err(crate::Error::NonFiniteSample {
                    index: self.frame_count,
                    channel: i,
                    value: sample,
                });
            }
            let weight = self.layout.weight(i);
            if weight == 0.0 {
                continue;
            }
            let filtered = self.filters[i].process(sample);
            sum_sq += weight * filtered * filtered;
        }
        self.weighted_power.push(sum_sq);
        self.frame_count += 1;
        Ok(())
    }

    /// Push interleaved stereo f32 samples.
    ///
    /// `left` and `right` must have equal length.
    pub fn push_interleaved_f32(
        &mut self,
        left: &[f32],
        right: &[f32],
    ) -> Result<(), crate::Error> {
        if self.finished {
            return Err(crate::Error::Finished);
        }
        if left.len() != right.len() {
            return Err(crate::Error::ChannelMismatch {
                expected: left.len(),
                got: right.len(),
            });
        }
        for (frame_idx, (&l, &r)) in left.iter().zip(right.iter()).enumerate() {
            let l_f64 = f64::from(l);
            let r_f64 = f64::from(r);
            if !l_f64.is_finite() {
                return Err(crate::Error::NonFiniteSample {
                    index: self.frame_count + frame_idx,
                    channel: 0,
                    value: l_f64,
                });
            }
            if !r_f64.is_finite() {
                return Err(crate::Error::NonFiniteSample {
                    index: self.frame_count + frame_idx,
                    channel: 1,
                    value: r_f64,
                });
            }
            let weight_l = self.layout.weight(0);
            let weight_r = self.layout.weight(1);
            let mut sum_sq = 0.0;
            if weight_l != 0.0 {
                let f = self.filters[0].process(l_f64);
                sum_sq += weight_l * f * f;
            }
            if weight_r != 0.0 {
                let f = self.filters[1].process(r_f64);
                sum_sq += weight_r * f * f;
            }
            self.weighted_power.push(sum_sq);
        }
        self.frame_count += left.len();
        Ok(())
    }

    /// Finish measurement and compute integrated loudness + LRA.
    ///
    /// After calling this, no more samples are accepted. Query results via
    /// `integrated_lufs()`, `loudness_range()`, `max_momentary_lufs()`,
    /// and `max_short_term_lufs()`.
    pub fn finish(&mut self) {
        if self.finished {
            return;
        }
        self.finished = true;

        // --- Compute gating blocks ---
        let gating_block_samples = ((GATING_BLOCK_S * self.sample_rate as f64) as usize).max(1);
        let step_samples = ((gating_block_samples as f64 * (1.0 - GATING_OVERLAP)) as usize).max(1);

        let mut block_idx = 0usize;
        loop {
            let start = block_idx * step_samples;
            let end = (start + gating_block_samples).min(self.weighted_power.len());
            if end - start < gating_block_samples / 2 {
                break;
            }
            let n = end - start;
            if n == 0 {
                break;
            }
            let mean_sq: f64 = self.weighted_power[start..end].iter().sum::<f64>() / n as f64;
            let lkfs = mean_sq_to_lkfs(mean_sq);
            self.gating_blocks.push(GatingBlock { lkfs });
            block_idx += 1;
        }

        // --- Integrated loudness (two‑stage gating) ---
        // Stage 1: absolute gate at —70 LKFS
        let abs_gated: Vec<f64> = self
            .gating_blocks
            .iter()
            .filter(|b| b.lkfs > ABSOLUTE_GATE)
            .map(|b| b.lkfs)
            .collect();

        let integrated = if abs_gated.is_empty() {
            f64::NEG_INFINITY
        } else {
            let abs_gated_loudness = mean_of_lkfs(&abs_gated);
            let rel_threshold = abs_gated_loudness + RELATIVE_GATE;

            // Stage 2: relative gate
            let rel_gated: Vec<f64> = abs_gated
                .iter()
                .filter(|&&l| l > rel_threshold)
                .copied()
                .collect();

            if rel_gated.is_empty() {
                f64::NEG_INFINITY
            } else {
                mean_of_lkfs(&rel_gated)
            }
        };
        self.integrated = integrated;

        // --- LRA (EBU Tech 3342) ---
        self.lra = compute_lra(&self.gating_blocks);

        // --- Max momentary and max short‑term ---
        self.max_momentary = self.compute_max_sliding(MOMENTARY_S);
        self.max_short_term = self.compute_max_sliding(SHORT_TERM_S);
    }

    /// Compute the maximum loudness over a sliding window of `window_s` seconds.
    /// Uses an incremental running mean (O(N) complexity).
    fn compute_max_sliding(&self, window_s: f64) -> f64 {
        let window_samples = ((window_s * self.sample_rate as f64) as usize).max(1);
        let n = self.weighted_power.len();
        if n == 0 || n < window_samples {
            return f64::NEG_INFINITY;
        }
        // Initial window sum
        let mut window_sum: f64 = self.weighted_power[..window_samples].iter().sum();
        let mut max_lkfs = mean_sq_to_lkfs(window_sum / window_samples as f64);
        // Slide the window
        for i in 1..=(n - window_samples) {
            window_sum -= self.weighted_power[i - 1];
            window_sum += self.weighted_power[i + window_samples - 1];
            let lkfs = mean_sq_to_lkfs(window_sum / window_samples as f64);
            if lkfs > max_lkfs {
                max_lkfs = lkfs;
            }
        }
        max_lkfs
    }

    // ---- Query methods ----

    /// Integrated loudness in LUFS (gated, two‑stage, per BS.1770‑5).
    ///
    /// Returns `f64::NEG_INFINITY` if the measurement has no valid blocks.
    #[must_use]
    pub fn integrated_lufs(&self) -> f64 {
        self.integrated
    }

    /// Integrated loudness relative to —23 LUFS target level, in LU.
    #[must_use]
    pub fn integrated_lu(&self) -> f64 {
        if self.integrated.is_finite() {
            self.integrated + 23.0
        } else {
            f64::NEG_INFINITY
        }
    }

    /// Maximum momentary loudness (400 ms window) in LUFS.
    #[must_use]
    pub fn max_momentary_lufs(&self) -> f64 {
        self.max_momentary
    }

    /// Maximum short‑term loudness (3 s window) in LUFS.
    #[must_use]
    pub fn max_short_term_lufs(&self) -> f64 {
        self.max_short_term
    }

    /// Loudness Range in LU (EBU Tech 3342).
    #[must_use]
    pub fn loudness_range(&self) -> f64 {
        self.lra
    }

    /// Number of sample frames processed.
    #[must_use]
    pub fn frame_count(&self) -> usize {
        self.frame_count
    }

    /// Duration in seconds of the measurement so far.
    #[must_use]
    pub fn duration_seconds(&self) -> f64 {
        self.frame_count as f64 / self.sample_rate as f64
    }
}

/// Compute the mean loudness from a list of LKFS values.
fn mean_of_lkfs(values: &[f64]) -> f64 {
    if values.is_empty() {
        return f64::NEG_INFINITY;
    }
    let n = values.len() as f64;
    let sum_power: f64 = values.iter().map(|&l| lkfs_to_mean_sq(l)).sum();
    mean_sq_to_lkfs(sum_power / n)
}

/// Compute Loudness Range per EBU Tech 3342.
///
/// Input: gating‑block loudness values (from short‑term 3 s windows per the spec).
/// The algorithm:
/// 1. Absolute gate: keep blocks ≥ —70 LUFS.
/// 2. Compute absolute‑gated integrated loudness.
/// 3. Relative gate: keep blocks ≥ (integrated —20 LU).
/// 4. Compute 10th and 95th percentiles of the distribution.
/// 5. LRA = 95th percentile — 10th percentile.
fn compute_lra(blocks: &[GatingBlock]) -> f64 {
    // Collect short‑term equivalent: we use the gating blocks themselves.
    // Per Tech 3342, the input is 3 s sliding‑window loudness levels.
    // Our gating blocks are 400 ms — for now we use them directly,
    // which is more granular than the spec requires. This is equivalent
    // to the spec's reference implementation when block rate ≥ 10 Hz.

    // Absolute gate
    let abs_gated: Vec<f64> = blocks
        .iter()
        .filter(|b| b.lkfs >= ABSOLUTE_GATE)
        .map(|b| b.lkfs)
        .collect();

    if abs_gated.is_empty() {
        return 0.0;
    }

    let abs_integrated = mean_of_lkfs(&abs_gated);
    let rel_threshold = abs_integrated - 20.0; // —20 LU relative gate

    // Relative gate
    let mut rel_gated: Vec<f64> = abs_gated
        .iter()
        .filter(|&&l| l >= rel_threshold)
        .copied()
        .collect();

    if rel_gated.is_empty() {
        return 0.0;
    }

    // Sort for percentile computation
    rel_gated.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal));

    let n = rel_gated.len();
    // Tech 3342 MATLAB: round((n-1)*PRC/100 + 1) — 1‑based indexing → 0‑based
    let low_idx = (libm::round((n as f64 - 1.0) * 10.0 / 100.0 + 1.0) as usize).saturating_sub(1);
    let high_idx = (libm::round((n as f64 - 1.0) * 95.0 / 100.0 + 1.0) as usize).saturating_sub(1);
    let low_idx = low_idx.min(n - 1);
    let high_idx = high_idx.min(n - 1);

    let perc_low = rel_gated[low_idx];
    let perc_high = rel_gated[high_idx];

    perc_high - perc_low
}

#[cfg(test)]
mod tests {
    use super::LoudnessMeter;
    use crate::channel_layout::ChannelLayout;

    #[test]
    fn stereo_1khz_minus_23_lufs_is_minus_23() {
        let sample_rate = 48_000;
        let duration = 2.0;
        let n = (duration * sample_rate as f64) as usize;
        let amplitude = 10.0f64.powf(-23.0 / 20.0) as f32;
        let mut left = alloc::vec![0.0f32; n];
        let mut right = alloc::vec![0.0f32; n];
        for i in 0..n {
            let t = i as f64 / sample_rate as f64;
            let val = (amplitude as f64 * (2.0 * core::f64::consts::PI * 1000.0 * t).sin()) as f32;
            left[i] = val;
            right[i] = val;
        }
        let mut meter = LoudnessMeter::new(sample_rate, ChannelLayout::Stereo).unwrap();
        meter.push_interleaved_f32(&left, &right).unwrap();
        meter.finish();
        let lufs = meter.integrated_lufs();
        assert!((lufs - (-23.0)).abs() < 0.2, "got {lufs}, expected -23.0");
    }

    #[test]
    fn absolute_gate_excludes_silence() {
        // 2 s of silence, then 3 s at —23 LUFS.
        let sample_rate = 48_000;
        let silence_s = 2.0;
        let tone_s = 3.0;
        let n_silence = (silence_s * sample_rate as f64) as usize;
        let n_tone = (tone_s * sample_rate as f64) as usize;
        let amplitude = 10.0f64.powf(-23.0 / 20.0) as f32;

        let mut left = alloc::vec![0.0f32; n_silence + n_tone];
        let mut right = alloc::vec![0.0f32; n_silence + n_tone];
        for i in n_silence..(n_silence + n_tone) {
            let t = (i - n_silence) as f64 / sample_rate as f64;
            let val = (amplitude as f64 * (2.0 * core::f64::consts::PI * 1000.0 * t).sin()) as f32;
            left[i] = val;
            right[i] = val;
        }
        let mut meter = LoudnessMeter::new(sample_rate, ChannelLayout::Stereo).unwrap();
        meter.push_interleaved_f32(&left, &right).unwrap();
        meter.finish();
        let lufs = meter.integrated_lufs();
        assert!(
            (lufs - (-23.0)).abs() < 0.5,
            "got {lufs}, expected ~-23.0 (gating should exclude silence)"
        );
    }

    #[test]
    fn low_signal_below_absolute_gate_does_not_drag_integrated() {
        // 2 s at —80 LUFS, then 3 s at —23 LUFS, then 2 s at —80.
        let sample_rate = 48_000;
        let tone_amplitude = 10.0f64.powf(-23.0 / 20.0) as f32;
        let low_amplitude = 10.0f64.powf(-80.0 / 20.0) as f32;

        let seg_low1_s = 2.0;
        let seg_tone_s = 3.0;
        let seg_low2_s = 2.0;
        let total = (seg_low1_s + seg_tone_s + seg_low2_s) * sample_rate as f64;
        let n = total as usize;
        let mut left = alloc::vec![0.0f32; n];
        let mut right = alloc::vec![0.0f32; n];

        let n_low1 = (seg_low1_s * sample_rate as f64) as usize;
        let n_tone = (seg_tone_s * sample_rate as f64) as usize;

        for i in 0..n_low1 {
            let t = i as f64 / sample_rate as f64;
            let val =
                (low_amplitude as f64 * (2.0 * core::f64::consts::PI * 1000.0 * t).sin()) as f32;
            left[i] = val;
            right[i] = val;
        }
        for i in 0..n_tone {
            let t = i as f64 / sample_rate as f64;
            let val =
                (tone_amplitude as f64 * (2.0 * core::f64::consts::PI * 1000.0 * t).sin()) as f32;
            left[n_low1 + i] = val;
            right[n_low1 + i] = val;
        }
        let offset = n_low1 + n_tone;
        for i in 0..(n - offset) {
            let t = i as f64 / sample_rate as f64;
            let val =
                (low_amplitude as f64 * (2.0 * core::f64::consts::PI * 1000.0 * t).sin()) as f32;
            left[offset + i] = val;
            right[offset + i] = val;
        }

        let mut meter = LoudnessMeter::new(sample_rate, ChannelLayout::Stereo).unwrap();
        meter.push_interleaved_f32(&left, &right).unwrap();
        meter.finish();
        let lufs = meter.integrated_lufs();
        assert!(
            (lufs - (-23.0)).abs() < 0.5,
            "got {lufs}, expected ~-23.0 (low segments should be gated out)"
        );
    }

    #[test]
    fn accepts_441_khz() {
        // 44100 Hz is now accepted; coefficients are derived via bilinear
        // transform rather than restricted to 48 kHz.
        assert!(LoudnessMeter::new(44_100, ChannelLayout::Stereo).is_ok());
    }

    #[test]
    fn rejects_zero_rate() {
        let err = LoudnessMeter::new(0, ChannelLayout::Stereo).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("sample rate") && msg.contains("0"),
            "expected invalid sample rate error, got: {msg}"
        );
    }

    #[test]
    fn coeffs_at_48k_match_tabulated() {
        // The bilinear transform at 48 kHz must reproduce the BS.1770-5 Annex 1
        // tabulated coefficients to within floating-point epsilon.
        let (stage1, stage2) = crate::filter::k_weighting_coeffs(48_000);

        let shelf_ref = crate::filter::BiquadCoeffs {
            b0: 1.535_124_859_586_97,
            b1: -2.691_696_189_406_38,
            b2: 1.198_392_810_852_85,
            a1: -1.690_659_293_182_41,
            a2: 0.732_480_774_215_85,
        };
        let hp_ref = crate::filter::BiquadCoeffs {
            b0: 1.0,
            b1: -2.0,
            b2: 1.0,
            a1: -1.990_047_454_833_98,
            a2: 0.990_072_250_366_21,
        };

        for (got, want) in [
            (stage1.b0, shelf_ref.b0),
            (stage1.b1, shelf_ref.b1),
            (stage1.b2, shelf_ref.b2),
            (stage1.a1, shelf_ref.a1),
            (stage1.a2, shelf_ref.a2),
            (stage2.b0, hp_ref.b0),
            (stage2.b1, hp_ref.b1),
            (stage2.b2, hp_ref.b2),
            (stage2.a1, hp_ref.a1),
            (stage2.a2, hp_ref.a2),
        ] {
            assert!(
                (got - want).abs() < 1e-12,
                "expected {want}, got {got} (diff {})",
                (got - want).abs()
            );
        }
    }

    #[test]
    fn rejects_nan_in_planar_f32() {
        let mut meter = LoudnessMeter::new(48_000, ChannelLayout::Stereo).unwrap();
        let err = meter.push_f32(&[f32::NAN, 0.5]).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("non-finite"), "got: {msg}");
    }

    #[test]
    fn rejects_inf_in_planar_f32() {
        let mut meter = LoudnessMeter::new(48_000, ChannelLayout::Stereo).unwrap();
        let err = meter.push_f32(&[0.5, f32::INFINITY]).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("non-finite"), "got: {msg}");
    }

    #[test]
    fn rejects_non_finite_in_planar_f64() {
        let mut meter = LoudnessMeter::new(48_000, ChannelLayout::Stereo).unwrap();
        let err = meter.push_f64(&[f64::NEG_INFINITY, 0.5]).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("non-finite"), "got: {msg}");
    }

    #[test]
    fn rejects_non_finite_in_interleaved_f32() {
        let mut meter = LoudnessMeter::new(48_000, ChannelLayout::Stereo).unwrap();
        let err = meter.push_interleaved_f32(&[0.5], &[f32::NAN]).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("non-finite"), "got: {msg}");
    }

    #[test]
    fn meter_not_poisoned_after_non_finite_rejection() {
        let mut meter = LoudnessMeter::new(48_000, ChannelLayout::Stereo).unwrap();

        meter.push_f32(&[0.5, 0.5]).unwrap();

        let _ = meter.push_f32(&[f32::NAN, 0.5]);

        for _ in 0..192_000 {
            meter.push_f32(&[0.1, 0.1]).unwrap();
        }
        meter.finish();
        let lufs = meter.integrated_lufs();
        assert!(lufs.is_finite(), "meter was poisoned: got {lufs}");
        assert!(lufs < -10.0, "unexpectedly loud: {lufs}");
    }

    #[test]
    fn non_finite_sample_error_carries_metadata() {
        let mut meter = LoudnessMeter::new(48_000, ChannelLayout::Stereo).unwrap();
        meter.push_f32(&[1.0, 1.0]).unwrap();
        let err = meter.push_f32(&[f32::NEG_INFINITY, 0.5]).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("channel 0"), "got: {msg}");
        assert!(msg.contains("index 1"), "got: {msg}");
    }
}