oximedia-core 0.1.3

Core types and traits for OxiMedia
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
//! Downmix and upmix matrices for audio channel layout conversion.
//!
//! This module provides [`DownmixMatrix`] — a dense `(out_channels × in_channels)`
//! mixing matrix — together with factory functions that produce ITU-R BS.775-3
//! compliant downmix coefficients for common speaker configurations.
//!
//! # Design
//!
//! - Coefficients are stored as linear amplitude gains (not dB).
//! - All matrices are normalised so that the loudest output channel's gain sum
//!   does not exceed `1.0` (prevents digital clipping on sum-of-all-channels
//!   inputs).
//! - The matrix is applied with [`DownmixMatrix::apply`], which takes a flat
//!   interleaved input slice (`samples * in_channels`) and writes a flat
//!   interleaved output slice (`samples * out_channels`).
//!
//! # Supported conversions
//!
//! | Source layout | Target layout | Factory |
//! |---|---|---|
//! | 5.1 | Stereo | [`DownmixMatrix::surround51_to_stereo`] |
//! | 5.1 | Mono | [`DownmixMatrix::surround51_to_mono`] |
//! | 7.1 | 5.1 | [`DownmixMatrix::surround71_to_51`] |
//! | 7.1 | Stereo | [`DownmixMatrix::surround71_to_stereo`] |
//! | 5.1.4 Atmos | 5.1 | [`DownmixMatrix::atmos514_to_51`] |
//! | Stereo | Mono | [`DownmixMatrix::stereo_to_mono`] |
//! | Mono | Stereo | [`DownmixMatrix::mono_to_stereo`] (upmix) |
//!
//! # Example
//!
//! ```
//! use oximedia_core::downmix::DownmixMatrix;
//!
//! // Downmix a 5.1 frame to stereo.
//! let mx = DownmixMatrix::surround51_to_stereo();
//! assert_eq!(mx.input_channels(), 6);
//! assert_eq!(mx.output_channels(), 2);
//!
//! // 1 sample of 5.1 silence → 1 sample of stereo silence.
//! let input = vec![0.0f32; 6];
//! let mut output = vec![0.0f32; 2];
//! mx.apply(&input, &mut output, 1);
//! assert_eq!(output, vec![0.0; 2]);
//! ```

#![allow(dead_code)]

use std::fmt;

// ─────────────────────────────────────────────────────────────────────────────
// Error type
// ─────────────────────────────────────────────────────────────────────────────

/// Errors that can arise when constructing or applying a [`DownmixMatrix`].
#[derive(Debug, PartialEq, Eq)]
pub enum DownmixError {
    /// The matrix dimensions are inconsistent.
    InvalidDimensions {
        /// Expected number of rows × columns.
        expected: (usize, usize),
        /// Actual coefficient vector length.
        actual: usize,
    },
    /// The input buffer length is not a multiple of the number of input channels.
    InputLengthMismatch {
        /// Length of the input slice.
        input_len: usize,
        /// Expected stride (number of input channels).
        in_channels: usize,
    },
    /// The output buffer is too small for the given sample count and output channels.
    OutputLengthMismatch {
        /// Length of the output slice.
        output_len: usize,
        /// Required length.
        required: usize,
    },
}

impl fmt::Display for DownmixError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidDimensions { expected, actual } => write!(
                f,
                "invalid matrix: expected {} coefficients ({}×{}), got {}",
                expected.0 * expected.1,
                expected.0,
                expected.1,
                actual
            ),
            Self::InputLengthMismatch {
                input_len,
                in_channels,
            } => write!(
                f,
                "input length {input_len} is not a multiple of in_channels {in_channels}"
            ),
            Self::OutputLengthMismatch {
                output_len,
                required,
            } => write!(f, "output buffer length {output_len} < required {required}"),
        }
    }
}

impl std::error::Error for DownmixError {}

// ─────────────────────────────────────────────────────────────────────────────
// DownmixMatrix
// ─────────────────────────────────────────────────────────────────────────────

/// A dense `(out_channels × in_channels)` mixing matrix.
///
/// Coefficients are stored in **row-major** order: coefficient `[o][i]` is
/// at index `o * in_channels + i` in the flat `coeffs` vector.
///
/// `output[o] += coeffs[o][i] * input[i]` for all `i`.
#[derive(Debug, Clone, PartialEq)]
pub struct DownmixMatrix {
    out_channels: usize,
    in_channels: usize,
    /// Flat coefficients in row-major order (`out_channels × in_channels`).
    coeffs: Vec<f32>,
}

impl DownmixMatrix {
    // ── Construction ──────────────────────────────────────────────────────

    /// Creates a new matrix from a flat coefficient slice.
    ///
    /// `coeffs` must have exactly `out_channels * in_channels` elements.
    ///
    /// # Errors
    ///
    /// Returns [`DownmixError::InvalidDimensions`] if the coefficient slice
    /// length does not match `out_channels * in_channels`.
    pub fn new(
        out_channels: usize,
        in_channels: usize,
        coeffs: Vec<f32>,
    ) -> Result<Self, DownmixError> {
        let expected = out_channels * in_channels;
        if coeffs.len() != expected {
            return Err(DownmixError::InvalidDimensions {
                expected: (out_channels, in_channels),
                actual: coeffs.len(),
            });
        }
        Ok(Self {
            out_channels,
            in_channels,
            coeffs,
        })
    }

    // ── Accessors ─────────────────────────────────────────────────────────

    /// Returns the number of output channels.
    #[must_use]
    pub fn output_channels(&self) -> usize {
        self.out_channels
    }

    /// Returns the number of input channels.
    #[must_use]
    pub fn input_channels(&self) -> usize {
        self.in_channels
    }

    /// Returns the coefficient for output channel `o` from input channel `i`.
    ///
    /// Returns `None` if `o ≥ out_channels` or `i ≥ in_channels`.
    #[must_use]
    pub fn coeff(&self, o: usize, i: usize) -> Option<f32> {
        if o < self.out_channels && i < self.in_channels {
            Some(self.coeffs[o * self.in_channels + i])
        } else {
            None
        }
    }

    // ── Apply ─────────────────────────────────────────────────────────────

    /// Applies the matrix to `sample_count` frames of interleaved audio.
    ///
    /// - `input` must have length `sample_count * in_channels`.
    /// - `output` must have length ≥ `sample_count * out_channels`.
    ///
    /// The output buffer is **overwritten** (not accumulated).
    ///
    /// # Errors
    ///
    /// Returns an error if the slice lengths are inconsistent with the matrix
    /// dimensions and `sample_count`.
    pub fn apply(
        &self,
        input: &[f32],
        output: &mut [f32],
        sample_count: usize,
    ) -> Result<(), DownmixError> {
        let expected_in = sample_count * self.in_channels;
        if input.len() < expected_in
            || (self.in_channels > 0 && input.len() % self.in_channels != 0)
        {
            return Err(DownmixError::InputLengthMismatch {
                input_len: input.len(),
                in_channels: self.in_channels,
            });
        }
        let required_out = sample_count * self.out_channels;
        if output.len() < required_out {
            return Err(DownmixError::OutputLengthMismatch {
                output_len: output.len(),
                required: required_out,
            });
        }

        for s in 0..sample_count {
            let in_base = s * self.in_channels;
            let out_base = s * self.out_channels;
            for o in 0..self.out_channels {
                let mut acc = 0.0f32;
                for i in 0..self.in_channels {
                    acc += self.coeffs[o * self.in_channels + i] * input[in_base + i];
                }
                output[out_base + o] = acc;
            }
        }
        Ok(())
    }

    /// Convenience wrapper — same as [`DownmixMatrix::apply`] but panics on
    /// dimension mismatch.  Intended for use in hot-path code that has already
    /// validated the buffer sizes.
    ///
    /// # Panics
    ///
    /// Panics if `input` / `output` are not correctly sized.
    pub fn apply_unchecked(&self, input: &[f32], output: &mut [f32], sample_count: usize) {
        self.apply(input, output, sample_count)
            .expect("apply_unchecked: dimension mismatch");
    }

    // ── Factory functions ──────────────────────────────────────────────────
    //
    // Coefficients follow ITU-R BS.775-3 where possible.
    // Constants:
    //   SQRT2 = √2 ≈ 1.4142135
    //   1/SQRT2 ≈ 0.7071068  (−3 dB)
    //   1/(SQRT2*2) ≈ 0.3535534 (−6 dB relative to 0 dB)

    const INV_SQRT2: f32 = 0.707_106_8;

    /// Builds a 5.1 → Stereo downmix matrix (ITU-R BS.775-3, Table B.1).
    ///
    /// Input channel order: FL FR FC LFE BL BR
    /// Output channel order: L R
    ///
    /// ```text
    /// L = FL + FC/√2 + 0.5·BL + 0 (LFE discarded)
    /// R = FR + FC/√2 + 0.5·BR
    /// ```
    #[must_use]
    pub fn surround51_to_stereo() -> Self {
        // Row 0 (L): FL  FR    FC             LFE  BL   BR
        // Row 1 (R): FL  FR    FC             LFE  BL   BR
        #[rustfmt::skip]
        let coeffs = vec![
            1.0,  0.0, Self::INV_SQRT2, 0.0, 0.5, 0.0,  // L
            0.0,  1.0, Self::INV_SQRT2, 0.0, 0.0, 0.5,  // R
        ];
        Self {
            out_channels: 2,
            in_channels: 6,
            coeffs,
        }
    }

    /// Builds a 5.1 → Mono downmix matrix.
    ///
    /// Input channel order: FL FR FC LFE BL BR
    /// Output: single mono channel.
    ///
    /// ```text
    /// M = 0.5·FL + 0.5·FR + FC/√2 + 0.25·BL + 0.25·BR  (LFE discarded)
    /// ```
    #[must_use]
    pub fn surround51_to_mono() -> Self {
        #[rustfmt::skip]
        let coeffs = vec![
            0.5, 0.5, Self::INV_SQRT2, 0.0, 0.25, 0.25,
        ];
        Self {
            out_channels: 1,
            in_channels: 6,
            coeffs,
        }
    }

    /// Builds a Stereo → Mono downmix matrix.
    ///
    /// ```text
    /// M = 0.5·L + 0.5·R
    /// ```
    #[must_use]
    pub fn stereo_to_mono() -> Self {
        let coeffs = vec![0.5, 0.5];
        Self {
            out_channels: 1,
            in_channels: 2,
            coeffs,
        }
    }

    /// Builds a Mono → Stereo upmix matrix (identity split).
    ///
    /// ```text
    /// L = M · (1/√2)
    /// R = M · (1/√2)
    /// ```
    ///
    /// Scaling by 1/√2 preserves the perceived loudness of the centre.
    #[must_use]
    pub fn mono_to_stereo() -> Self {
        let coeffs = vec![
            Self::INV_SQRT2, // L ← M
            Self::INV_SQRT2, // R ← M
        ];
        Self {
            out_channels: 2,
            in_channels: 1,
            coeffs,
        }
    }

    /// Builds a 7.1 → 5.1 downmix matrix.
    ///
    /// Input order:  FL FR FC LFE BL BR SL SR
    /// Output order: FL FR FC LFE BL BR
    ///
    /// ```text
    /// FL_out = FL  + SL/√2
    /// FR_out = FR  + SR/√2
    /// FC_out = FC
    /// LFE_out = LFE
    /// BL_out = BL  + SL/√2
    /// BR_out = BR  + SR/√2
    /// ```
    #[must_use]
    pub fn surround71_to_51() -> Self {
        //                  FL  FR  FC  LFE  BL  BR  SL             SR
        #[rustfmt::skip]
        let coeffs = vec![
            1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Self::INV_SQRT2, 0.0,  // FL_out
            0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Self::INV_SQRT2,  // FR_out
            0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,              // FC_out
            0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,              // LFE_out
            0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Self::INV_SQRT2, 0.0,  // BL_out
            0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Self::INV_SQRT2,  // BR_out
        ];
        Self {
            out_channels: 6,
            in_channels: 8,
            coeffs,
        }
    }

    /// Builds a 7.1 → Stereo downmix matrix.
    ///
    /// Input order: FL FR FC LFE BL BR SL SR
    ///
    /// ```text
    /// L = FL + FC/√2 + 0.5·BL + SL/√2
    /// R = FR + FC/√2 + 0.5·BR + SR/√2
    /// ```
    #[must_use]
    pub fn surround71_to_stereo() -> Self {
        //                 FL  FR  FC             LFE  BL   BR   SL             SR
        #[rustfmt::skip]
        let coeffs = vec![
            1.0, 0.0, Self::INV_SQRT2, 0.0, 0.5, 0.0, Self::INV_SQRT2, 0.0,  // L
            0.0, 1.0, Self::INV_SQRT2, 0.0, 0.0, 0.5, 0.0, Self::INV_SQRT2,  // R
        ];
        Self {
            out_channels: 2,
            in_channels: 8,
            coeffs,
        }
    }

    /// Builds a 5.1.4 Atmos → 5.1 downmix matrix.
    ///
    /// Input order:  FL FR FC LFE BL BR TFL TFR TBL TBR
    /// Output order: FL FR FC LFE BL BR
    ///
    /// Height channels are blended into their nearest bed equivalents at −6 dB.
    ///
    /// ```text
    /// FL_out  = FL  + TFL/2
    /// FR_out  = FR  + TFR/2
    /// FC_out  = FC
    /// LFE_out = LFE
    /// BL_out  = BL  + TBL/2
    /// BR_out  = BR  + TBR/2
    /// ```
    #[must_use]
    pub fn atmos514_to_51() -> Self {
        //                 FL  FR  FC  LFE  BL  BR  TFL  TFR  TBL  TBR
        #[rustfmt::skip]
        let coeffs = vec![
            1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0,  // FL_out
            0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,  // FR_out
            0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,  // FC_out
            0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,  // LFE_out
            0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.5, 0.0,  // BL_out
            0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.5,  // BR_out
        ];
        Self {
            out_channels: 6,
            in_channels: 10,
            coeffs,
        }
    }

    // ── Utility ───────────────────────────────────────────────────────────

    /// Returns the identity matrix for `channels` channels.
    ///
    /// All output channels map 1:1 to the corresponding input channel.
    #[must_use]
    pub fn identity(channels: usize) -> Self {
        let n = channels;
        let mut coeffs = vec![0.0f32; n * n];
        for c in 0..n {
            coeffs[c * n + c] = 1.0;
        }
        Self {
            out_channels: n,
            in_channels: n,
            coeffs,
        }
    }

    /// Returns the transpose of this matrix.
    ///
    /// Swaps input and output channel counts and transposes the coefficient
    /// matrix.  This is useful for constructing upmix matrices from downmix
    /// definitions.
    #[must_use]
    pub fn transpose(&self) -> Self {
        let mut coeffs = vec![0.0f32; self.in_channels * self.out_channels];
        for o in 0..self.out_channels {
            for i in 0..self.in_channels {
                coeffs[i * self.out_channels + o] = self.coeffs[o * self.in_channels + i];
            }
        }
        Self {
            out_channels: self.in_channels,
            in_channels: self.out_channels,
            coeffs,
        }
    }
}

impl fmt::Display for DownmixMatrix {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "DownmixMatrix({}×{})",
            self.out_channels, self.in_channels
        )
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    const EPS: f32 = 1e-5;

    fn approx_eq(a: f32, b: f32) -> bool {
        (a - b).abs() < EPS
    }

    // 1. Dimension mismatch on construction returns an error.
    #[test]
    fn test_new_dimension_mismatch() {
        let result = DownmixMatrix::new(2, 3, vec![1.0; 7]);
        assert!(result.is_err());
        match result {
            Err(DownmixError::InvalidDimensions { expected, actual }) => {
                assert_eq!(expected, (2, 3));
                assert_eq!(actual, 7);
            }
            _ => panic!("expected InvalidDimensions"),
        }
    }

    // 2. Valid construction succeeds.
    #[test]
    fn test_new_valid() {
        let mx = DownmixMatrix::new(2, 3, vec![0.0; 6]).expect("valid");
        assert_eq!(mx.input_channels(), 3);
        assert_eq!(mx.output_channels(), 2);
    }

    // 3. Identity matrix passes samples through unchanged.
    #[test]
    fn test_identity_passthrough() {
        let mx = DownmixMatrix::identity(3);
        let input = vec![1.0f32, 2.0, 3.0];
        let mut output = vec![0.0f32; 3];
        mx.apply(&input, &mut output, 1).expect("apply ok");
        assert!(approx_eq(output[0], 1.0));
        assert!(approx_eq(output[1], 2.0));
        assert!(approx_eq(output[2], 3.0));
    }

    // 4. Stereo → Mono: two equal channels → half amplitude.
    #[test]
    fn test_stereo_to_mono() {
        let mx = DownmixMatrix::stereo_to_mono();
        let input = vec![1.0f32, 1.0];
        let mut output = vec![0.0f32; 1];
        mx.apply(&input, &mut output, 1).expect("apply ok");
        assert!(approx_eq(output[0], 1.0));
    }

    // 5. Mono → Stereo upmix: output scaled by 1/√2.
    #[test]
    fn test_mono_to_stereo() {
        let mx = DownmixMatrix::mono_to_stereo();
        let input = vec![1.0f32];
        let mut output = vec![0.0f32; 2];
        mx.apply(&input, &mut output, 1).expect("apply ok");
        let expected = DownmixMatrix::INV_SQRT2;
        assert!(approx_eq(output[0], expected));
        assert!(approx_eq(output[1], expected));
    }

    // 6. 5.1 → Stereo dimensions.
    #[test]
    fn test_51_to_stereo_dimensions() {
        let mx = DownmixMatrix::surround51_to_stereo();
        assert_eq!(mx.input_channels(), 6);
        assert_eq!(mx.output_channels(), 2);
    }

    // 7. 5.1 → Stereo: pure FL/FR only → same amplitude out.
    #[test]
    fn test_51_to_stereo_pure_front() {
        let mx = DownmixMatrix::surround51_to_stereo();
        // FL=1, FR=1, FC=0, LFE=0, BL=0, BR=0
        let input = vec![1.0f32, 1.0, 0.0, 0.0, 0.0, 0.0];
        let mut output = vec![0.0f32; 2];
        mx.apply(&input, &mut output, 1).expect("apply ok");
        assert!(approx_eq(output[0], 1.0));
        assert!(approx_eq(output[1], 1.0));
    }

    // 8. 5.1 → Stereo: centre channel split equally at −3 dB.
    #[test]
    fn test_51_to_stereo_centre_split() {
        let mx = DownmixMatrix::surround51_to_stereo();
        // FL=0, FR=0, FC=1, LFE=0, BL=0, BR=0
        let input = vec![0.0f32, 0.0, 1.0, 0.0, 0.0, 0.0];
        let mut output = vec![0.0f32; 2];
        mx.apply(&input, &mut output, 1).expect("apply ok");
        assert!(
            approx_eq(output[0], DownmixMatrix::INV_SQRT2),
            "L = {:.6}",
            output[0]
        );
        assert!(
            approx_eq(output[1], DownmixMatrix::INV_SQRT2),
            "R = {:.6}",
            output[1]
        );
    }

    // 9. 5.1 → Mono dimensions.
    #[test]
    fn test_51_to_mono_dimensions() {
        let mx = DownmixMatrix::surround51_to_mono();
        assert_eq!(mx.input_channels(), 6);
        assert_eq!(mx.output_channels(), 1);
    }

    // 10. 5.1 → Mono: LFE is discarded (zero coefficient).
    #[test]
    fn test_51_to_mono_lfe_discarded() {
        let mx = DownmixMatrix::surround51_to_mono();
        let lfe_coeff = mx.coeff(0, 3).expect("coeff(0,3)");
        assert!(approx_eq(lfe_coeff, 0.0));
    }

    // 11. 7.1 → 5.1 dimensions.
    #[test]
    fn test_71_to_51_dimensions() {
        let mx = DownmixMatrix::surround71_to_51();
        assert_eq!(mx.input_channels(), 8);
        assert_eq!(mx.output_channels(), 6);
    }

    // 12. 7.1 → 5.1: FC passes through unmodified.
    #[test]
    fn test_71_to_51_fc_passthrough() {
        let mx = DownmixMatrix::surround71_to_51();
        // FC is input channel 2, output channel 2
        let fc_coeff = mx.coeff(2, 2).expect("coeff(2,2)");
        assert!(approx_eq(fc_coeff, 1.0));
        // FC does not bleed into other outputs
        assert!(approx_eq(mx.coeff(0, 2).expect("c(0,2)"), 0.0));
        assert!(approx_eq(mx.coeff(1, 2).expect("c(1,2)"), 0.0));
    }

    // 13. 7.1 → Stereo dimensions and SL coefficient.
    #[test]
    fn test_71_to_stereo() {
        let mx = DownmixMatrix::surround71_to_stereo();
        assert_eq!(mx.input_channels(), 8);
        assert_eq!(mx.output_channels(), 2);
        // SL (channel 6) feeds L (output 0) at 1/√2
        let sl_to_l = mx.coeff(0, 6).expect("coeff(0,6)");
        assert!(approx_eq(sl_to_l, DownmixMatrix::INV_SQRT2));
        // SR (channel 7) does NOT feed L
        let sr_to_l = mx.coeff(0, 7).expect("coeff(0,7)");
        assert!(approx_eq(sr_to_l, 0.0));
    }

    // 14. Atmos 5.1.4 → 5.1 dimensions and TFL coefficient.
    #[test]
    fn test_atmos514_to_51() {
        let mx = DownmixMatrix::atmos514_to_51();
        assert_eq!(mx.input_channels(), 10);
        assert_eq!(mx.output_channels(), 6);
        // TFL (channel 6) folds into FL_out (row 0) at 0.5
        let tfl_coeff = mx.coeff(0, 6).expect("coeff(0,6)");
        assert!(approx_eq(tfl_coeff, 0.5));
    }

    // 15. apply over multiple samples.
    #[test]
    fn test_apply_multiple_samples() {
        let mx = DownmixMatrix::stereo_to_mono();
        // 4 stereo samples: (1,1), (2,2), (3,3), (4,4)
        let input = vec![1.0f32, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0];
        let mut output = vec![0.0f32; 4];
        mx.apply(&input, &mut output, 4).expect("apply ok");
        assert!(approx_eq(output[0], 1.0));
        assert!(approx_eq(output[1], 2.0));
        assert!(approx_eq(output[2], 3.0));
        assert!(approx_eq(output[3], 4.0));
    }

    // 16. apply returns error when output buffer is too small.
    #[test]
    fn test_apply_output_too_small() {
        let mx = DownmixMatrix::stereo_to_mono();
        let input = vec![1.0f32, 1.0];
        let mut output = vec![]; // too small for 1 sample
        let result = mx.apply(&input, &mut output, 1);
        assert!(matches!(
            result,
            Err(DownmixError::OutputLengthMismatch { .. })
        ));
    }

    // 17. transpose of stereo_to_mono gives a 1-in → 2-out matrix.
    #[test]
    fn test_transpose_stereo_to_mono() {
        let mx = DownmixMatrix::stereo_to_mono().transpose();
        assert_eq!(mx.input_channels(), 1);
        assert_eq!(mx.output_channels(), 2);
    }

    // 18. coeff returns None for out-of-range indices.
    #[test]
    fn test_coeff_out_of_range() {
        let mx = DownmixMatrix::stereo_to_mono();
        assert!(mx.coeff(0, 0).is_some());
        assert!(mx.coeff(1, 0).is_none()); // only 1 output row
        assert!(mx.coeff(0, 2).is_none()); // only 2 input columns
    }

    // 19. Display impl.
    #[test]
    fn test_display() {
        let mx = DownmixMatrix::surround51_to_stereo();
        let s = format!("{mx}");
        assert!(s.contains("2×6"), "display = {s}");
    }

    // 20. DownmixError display does not panic.
    #[test]
    fn test_error_display() {
        let err = DownmixError::InvalidDimensions {
            expected: (2, 3),
            actual: 7,
        };
        let s = format!("{err}");
        assert!(s.contains("7"));
    }
}