bs2b 0.1.2

Bauer stereophonic-to-binaural (bs2b) DSP library in Rust
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
//! Bauer stereophonic-to-binaural (bs2b) crossfeed DSP.
//!
//! This crate implements the classic bs2b algorithm with an ergonomic Rust API.
#![cfg_attr(feature = "no_std", no_std)]
#![forbid(unsafe_code)]

#[cfg(all(feature = "std", feature = "no_std"))]
compile_error!(
    "features `std` and `no_std` are mutually exclusive; disable default features to use `no_std`"
);

#[cfg(feature = "streaming")]
pub mod streaming;

use core::f64::consts::{LN_10, PI};
use core::fmt;

#[cfg(test)]
extern crate std;

/// Minimum supported sample rate in Hz.
pub const MIN_SAMPLE_RATE: u32 = 2_000;
/// Maximum supported sample rate in Hz.
pub const MAX_SAMPLE_RATE: u32 = 384_000;
/// Default sample rate in Hz used by the original library.
pub const DEFAULT_SAMPLE_RATE: u32 = 44_100;

/// Minimum crossfeed cut frequency in Hz.
pub const MIN_CUT_FREQUENCY: u32 = 300;
/// Maximum crossfeed cut frequency in Hz.
pub const MAX_CUT_FREQUENCY: u32 = 2_000;

/// Minimum feed level in dB * 10.
pub const MIN_FEED_DB_TENTHS: u32 = 10;
/// Maximum feed level in dB * 10.
pub const MAX_FEED_DB_TENTHS: u32 = 150;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Crossfeed level definition.
pub struct Level {
    /// Low-pass crossover in Hz.
    cut_frequency_hz: u32,
    /// Crossfeed level at low frequencies in dB * 10.
    feed_db_tenths: u32,
}

impl Level {
    /// Original bs2b default profile.
    pub const DEFAULT: Self = Self {
        cut_frequency_hz: 700,
        feed_db_tenths: 45,
    };

    /// Chu Moy profile.
    pub const CMOY: Self = Self {
        cut_frequency_hz: 700,
        feed_db_tenths: 60,
    };

    /// Jan Meier profile.
    pub const JMEIER: Self = Self {
        cut_frequency_hz: 650,
        feed_db_tenths: 95,
    };

    /// Creates a validated level.
    pub fn new(cut_frequency_hz: u32, feed_db_tenths: u32) -> Result<Self, Bs2bError> {
        if !(MIN_CUT_FREQUENCY..=MAX_CUT_FREQUENCY).contains(&cut_frequency_hz) {
            return Err(Bs2bError::InvalidCutFrequency(cut_frequency_hz));
        }
        if !(MIN_FEED_DB_TENTHS..=MAX_FEED_DB_TENTHS).contains(&feed_db_tenths) {
            return Err(Bs2bError::InvalidFeedLevel(feed_db_tenths));
        }

        Ok(Self {
            cut_frequency_hz,
            feed_db_tenths,
        })
    }

    /// Packs the level into the original C format.
    pub const fn packed(self) -> u32 {
        self.cut_frequency_hz | (self.feed_db_tenths << 16)
    }

    /// Returns low-pass crossover in Hz.
    pub const fn cut_frequency_hz(self) -> u32 {
        self.cut_frequency_hz
    }

    /// Returns crossfeed level at low frequencies in dB * 10.
    pub const fn feed_db_tenths(self) -> u32 {
        self.feed_db_tenths
    }

    /// Unpacks the original C level representation and validates it.
    pub fn from_packed(value: u32) -> Result<Self, Bs2bError> {
        let cut_frequency_hz = value & 0xffff;
        let feed_db_tenths = value >> 16;
        Self::new(cut_frequency_hz, feed_db_tenths)
    }

    /// Delay at low frequencies, in microseconds.
    pub const fn delay_microseconds(self) -> u32 {
        (18_700 / self.cut_frequency_hz) * 10
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Configuration and processing errors.
pub enum Bs2bError {
    InvalidSampleRate(u32),

    InvalidCutFrequency(u32),

    InvalidFeedLevel(u32),

    OddInterleavedSamples(usize),

    MismatchedPlanarLengths { left: usize, right: usize },
}

impl fmt::Display for Bs2bError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidSampleRate(sample_rate) => write!(
                f,
                "sample rate {sample_rate} is out of range [{MIN_SAMPLE_RATE}, {MAX_SAMPLE_RATE}]"
            ),
            Self::InvalidCutFrequency(cut_frequency_hz) => write!(
                f,
                "cut frequency {cut_frequency_hz} Hz is out of range [{MIN_CUT_FREQUENCY}, {MAX_CUT_FREQUENCY}]"
            ),
            Self::InvalidFeedLevel(feed_db_tenths) => write!(
                f,
                "feed level {feed_db_tenths} (dB*10) is out of range [{MIN_FEED_DB_TENTHS}, {MAX_FEED_DB_TENTHS}]"
            ),
            Self::OddInterleavedSamples(sample_count) => write!(
                f,
                "interleaved stereo buffer must have an even number of samples, got {sample_count}"
            ),
            Self::MismatchedPlanarLengths { left, right } => write!(
                f,
                "left/right planar buffers must have equal length, got {left} and {right}"
            ),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Bs2bError {}

#[derive(Debug, Clone, Copy)]
struct Coefficients {
    a0_lo: f64,
    b1_lo: f64,
    a0_hi: f64,
    a1_hi: f64,
    b1_hi: f64,
    gain: f64,
}

impl Coefficients {
    fn from_level(level: Level, sample_rate: u32) -> Self {
        let fc_lo = level.cut_frequency_hz as f64;
        let level_db = level.feed_db_tenths as f64 / 10.0;

        let gb_lo = level_db * -5.0 / 6.0 - 3.0;
        let gb_hi = level_db / 6.0 - 3.0;

        let g_lo = powf(10.0, gb_lo / 20.0);
        let g_hi = 1.0 - powf(10.0, gb_hi / 20.0);

        let fc_hi = fc_lo * powf(2.0, (gb_lo - 20.0 * log10(g_hi)) / 12.0);

        let x_lo = exp(-2.0 * PI * fc_lo / sample_rate as f64);
        let b1_lo = x_lo;
        let a0_lo = g_lo * (1.0 - x_lo);

        let x_hi = exp(-2.0 * PI * fc_hi / sample_rate as f64);
        let b1_hi = x_hi;
        let a0_hi = 1.0 - g_hi * (1.0 - x_hi);
        let a1_hi = -x_hi;

        let gain = 1.0 / (1.0 - g_hi + g_lo);

        Self {
            a0_lo,
            b1_lo,
            a0_hi,
            a1_hi,
            b1_hi,
            gain,
        }
    }
}

#[derive(Debug, Clone, Copy, Default)]
struct FilterState {
    asis: [f64; 2],
    lo: [f64; 2],
    hi: [f64; 2],
}

/// Stateful bs2b DSP processor.
#[derive(Debug, Clone)]
pub struct Bs2b {
    sample_rate: u32,
    level: Level,
    coefficients: Coefficients,
    state: FilterState,
}

impl Default for Bs2b {
    fn default() -> Self {
        Self::new(DEFAULT_SAMPLE_RATE, Level::DEFAULT)
            .expect("default level and sample rate are valid")
    }
}

impl Bs2b {
    /// Creates a new processor.
    pub fn new(sample_rate: u32, level: Level) -> Result<Self, Bs2bError> {
        validate_sample_rate(sample_rate)?;

        Ok(Self {
            sample_rate,
            level,
            coefficients: Coefficients::from_level(level, sample_rate),
            state: FilterState::default(),
        })
    }

    /// Creates a processor from the packed C level representation.
    pub fn from_packed_level(sample_rate: u32, level: u32) -> Result<Self, Bs2bError> {
        let level = Level::from_packed(level)?;
        Self::new(sample_rate, level)
    }

    /// Returns the current sample rate in Hz.
    pub const fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    /// Returns the current level.
    pub const fn level(&self) -> Level {
        self.level
    }

    /// Returns the current packed level representation.
    pub const fn packed_level(&self) -> u32 {
        self.level.packed()
    }

    /// Returns the low-frequency delay for the current level in microseconds.
    pub const fn level_delay_microseconds(&self) -> u32 {
        self.level.delay_microseconds()
    }

    /// Updates the sample rate and clears the filter history.
    pub fn set_sample_rate(&mut self, sample_rate: u32) -> Result<(), Bs2bError> {
        validate_sample_rate(sample_rate)?;

        if self.sample_rate == sample_rate {
            return Ok(());
        }

        self.sample_rate = sample_rate;
        self.coefficients = Coefficients::from_level(self.level, sample_rate);
        self.clear();
        Ok(())
    }

    /// Updates the crossfeed level while preserving filter history.
    pub fn set_level(&mut self, level: Level) {
        if self.level == level {
            return;
        }

        self.level = level;
        self.coefficients = Coefficients::from_level(level, self.sample_rate);
    }

    /// Clears filter history.
    pub fn clear(&mut self) {
        self.state = FilterState::default();
    }

    /// Returns true if filter history is fully cleared.
    pub fn is_clear(&self) -> bool {
        self.state.asis.iter().all(|v| *v == 0.0)
            && self.state.lo.iter().all(|v| *v == 0.0)
            && self.state.hi.iter().all(|v| *v == 0.0)
    }

    /// Processes one stereo frame and returns the transformed frame.
    pub fn process_frame<T: Sample>(&mut self, left: T, right: T) -> (T, T) {
        let (left, right) = self.process_frame_f64(left.to_f64(), right.to_f64());
        (
            T::from_f64(left.clamp(T::MIN_VALUE, T::MAX_VALUE)),
            T::from_f64(right.clamp(T::MIN_VALUE, T::MAX_VALUE)),
        )
    }

    /// Processes an interleaved stereo buffer in-place.
    pub fn process_interleaved<T: Sample>(&mut self, samples: &mut [T]) -> Result<(), Bs2bError> {
        if !samples.len().is_multiple_of(2) {
            return Err(Bs2bError::OddInterleavedSamples(samples.len()));
        }

        for frame in samples.chunks_exact_mut(2) {
            let (left, right) = self.process_frame(frame[0], frame[1]);
            frame[0] = left;
            frame[1] = right;
        }

        Ok(())
    }

    /// Processes left/right planar buffers in-place.
    pub fn process_planar<T: Sample>(
        &mut self,
        left: &mut [T],
        right: &mut [T],
    ) -> Result<(), Bs2bError> {
        if left.len() != right.len() {
            return Err(Bs2bError::MismatchedPlanarLengths {
                left: left.len(),
                right: right.len(),
            });
        }

        for (l, r) in left.iter_mut().zip(right.iter_mut()) {
            (*l, *r) = self.process_frame(*l, *r);
        }

        Ok(())
    }

    fn process_frame_f64(&mut self, left: f64, right: f64) -> (f64, f64) {
        self.state.lo[0] =
            self.coefficients.a0_lo * left + self.coefficients.b1_lo * self.state.lo[0];
        self.state.lo[1] =
            self.coefficients.a0_lo * right + self.coefficients.b1_lo * self.state.lo[1];

        self.state.hi[0] = self.coefficients.a0_hi * left
            + self.coefficients.a1_hi * self.state.asis[0]
            + self.coefficients.b1_hi * self.state.hi[0];
        self.state.hi[1] = self.coefficients.a0_hi * right
            + self.coefficients.a1_hi * self.state.asis[1]
            + self.coefficients.b1_hi * self.state.hi[1];

        self.state.asis[0] = left;
        self.state.asis[1] = right;

        let left = (self.state.hi[0] + self.state.lo[1]) * self.coefficients.gain;
        let right = (self.state.hi[1] + self.state.lo[0]) * self.coefficients.gain;

        (left, right)
    }
}

fn validate_sample_rate(sample_rate: u32) -> Result<(), Bs2bError> {
    if (MIN_SAMPLE_RATE..=MAX_SAMPLE_RATE).contains(&sample_rate) {
        Ok(())
    } else {
        Err(Bs2bError::InvalidSampleRate(sample_rate))
    }
}

fn log10(value: f64) -> f64 {
    ln(value) / LN_10
}

#[cfg(feature = "no_std")]
fn powf(value: f64, power: f64) -> f64 {
    libm::pow(value, power)
}

#[cfg(not(feature = "no_std"))]
fn powf(value: f64, power: f64) -> f64 {
    value.powf(power)
}

#[cfg(feature = "no_std")]
fn exp(value: f64) -> f64 {
    libm::exp(value)
}

#[cfg(not(feature = "no_std"))]
fn exp(value: f64) -> f64 {
    value.exp()
}

#[cfg(feature = "no_std")]
fn ln(value: f64) -> f64 {
    libm::log(value)
}

#[cfg(not(feature = "no_std"))]
fn ln(value: f64) -> f64 {
    value.ln()
}

mod private {
    pub trait Sealed {}
}

/// Sample type that can be processed by the bs2b processor.
pub trait Sample: private::Sealed + Copy {
    const MIN_VALUE: f64;
    const MAX_VALUE: f64;

    fn to_f64(self) -> f64;
    fn from_f64(value: f64) -> Self;
}

impl private::Sealed for f64 {}
impl Sample for f64 {
    const MIN_VALUE: f64 = -1.0;
    const MAX_VALUE: f64 = 1.0;

    #[inline]
    fn to_f64(self) -> f64 {
        self
    }

    #[inline]
    fn from_f64(value: f64) -> Self {
        value
    }
}

impl private::Sealed for f32 {}
impl Sample for f32 {
    const MIN_VALUE: f64 = -1.0;
    const MAX_VALUE: f64 = 1.0;

    #[inline]
    fn to_f64(self) -> f64 {
        self as f64
    }

    #[inline]
    fn from_f64(value: f64) -> Self {
        value as f32
    }
}

impl private::Sealed for i32 {}
impl Sample for i32 {
    const MIN_VALUE: f64 = i32::MIN as f64;
    const MAX_VALUE: f64 = i32::MAX as f64;

    #[inline]
    fn to_f64(self) -> f64 {
        self as f64
    }

    #[inline]
    fn from_f64(value: f64) -> Self {
        value as i32
    }
}

impl private::Sealed for u32 {}
impl Sample for u32 {
    const MIN_VALUE: f64 = i32::MIN as f64;
    const MAX_VALUE: f64 = i32::MAX as f64;

    #[inline]
    fn to_f64(self) -> f64 {
        ((self ^ 0x8000_0000) as i32) as f64
    }

    #[inline]
    fn from_f64(value: f64) -> Self {
        (value as i32 as u32) ^ 0x8000_0000
    }
}

impl private::Sealed for i16 {}
impl Sample for i16 {
    const MIN_VALUE: f64 = i16::MIN as f64;
    const MAX_VALUE: f64 = i16::MAX as f64;

    #[inline]
    fn to_f64(self) -> f64 {
        self as f64
    }

    #[inline]
    fn from_f64(value: f64) -> Self {
        value as i16
    }
}

impl private::Sealed for u16 {}
impl Sample for u16 {
    const MIN_VALUE: f64 = i16::MIN as f64;
    const MAX_VALUE: f64 = i16::MAX as f64;

    #[inline]
    fn to_f64(self) -> f64 {
        ((self ^ 0x8000) as i16) as f64
    }

    #[inline]
    fn from_f64(value: f64) -> Self {
        (value as i16 as u16) ^ 0x8000
    }
}

impl private::Sealed for i8 {}
impl Sample for i8 {
    const MIN_VALUE: f64 = i8::MIN as f64;
    const MAX_VALUE: f64 = i8::MAX as f64;

    #[inline]
    fn to_f64(self) -> f64 {
        self as f64
    }

    #[inline]
    fn from_f64(value: f64) -> Self {
        value as i8
    }
}

impl private::Sealed for u8 {}
impl Sample for u8 {
    const MIN_VALUE: f64 = i8::MIN as f64;
    const MAX_VALUE: f64 = i8::MAX as f64;

    #[inline]
    fn to_f64(self) -> f64 {
        ((self ^ 0x80) as i8) as f64
    }

    #[inline]
    fn from_f64(value: f64) -> Self {
        (value as i8 as u8) ^ 0x80
    }
}

#[cfg(test)]
mod tests {
    use approx::assert_abs_diff_eq;
    use rand::rngs::StdRng;
    use rand::{RngExt, SeedableRng};
    use std::vec::Vec;

    use super::*;

    #[test]
    fn level_validation_checks_bounds() {
        assert!(Level::new(700, 45).is_ok());
        assert!(matches!(
            Level::new(299, 45),
            Err(Bs2bError::InvalidCutFrequency(299))
        ));
        assert!(matches!(
            Level::new(700, 151),
            Err(Bs2bError::InvalidFeedLevel(151))
        ));
    }

    #[test]
    fn sample_rate_validation_checks_bounds() {
        assert!(Bs2b::new(MIN_SAMPLE_RATE, Level::DEFAULT).is_ok());
        assert!(matches!(
            Bs2b::new(MIN_SAMPLE_RATE - 1, Level::DEFAULT),
            Err(Bs2bError::InvalidSampleRate(1_999))
        ));
    }

    #[test]
    fn delay_microseconds_matches_reference_formula() {
        let level = Level::DEFAULT;
        assert_eq!(level.delay_microseconds(), (18_700 / 700) * 10);
    }

    #[test]
    fn clear_and_is_clear_roundtrip() {
        let mut bs2b = Bs2b::default();
        let _ = bs2b.process_frame(0.5_f32, -0.25_f32);
        assert!(!bs2b.is_clear());

        bs2b.clear();
        assert!(bs2b.is_clear());
    }

    #[test]
    fn set_sample_rate_clears_state() {
        let mut bs2b = Bs2b::default();
        let _ = bs2b.process_frame(0.2_f32, -0.1_f32);
        assert!(!bs2b.is_clear());

        bs2b.set_sample_rate(48_000)
            .expect("48 kHz should be accepted");
        assert!(bs2b.is_clear());
    }

    #[test]
    fn interleaved_rejects_odd_length() {
        let mut bs2b = Bs2b::default();
        let mut data = std::vec![0.1_f32, 0.2, 0.3];
        assert!(matches!(
            bs2b.process_interleaved(&mut data),
            Err(Bs2bError::OddInterleavedSamples(3))
        ));
    }

    #[test]
    fn planar_rejects_mismatched_lengths() {
        let mut bs2b = Bs2b::default();
        let mut left = std::vec![0.1_f32, 0.2];
        let mut right = std::vec![0.3_f32];

        assert!(matches!(
            bs2b.process_planar(&mut left, &mut right),
            Err(Bs2bError::MismatchedPlanarLengths { left: 2, right: 1 })
        ));
    }

    #[test]
    fn planar_and_interleaved_match() {
        let mut interleaved = std::vec![0.4_f32, -0.2, 0.1, 0.9, -0.8, 0.3, 0.05, -0.4];
        let mut left = std::vec![0.4_f32, 0.1, -0.8, 0.05];
        let mut right = std::vec![-0.2_f32, 0.9, 0.3, -0.4];

        let mut a = Bs2b::default();
        let mut b = Bs2b::default();

        a.process_interleaved(&mut interleaved)
            .expect("interleaved buffer should be valid");
        b.process_planar(&mut left, &mut right)
            .expect("planar buffers should be valid");

        for (idx, frame) in interleaved.chunks_exact(2).enumerate() {
            assert_abs_diff_eq!(frame[0], left[idx], epsilon = 1.0e-7);
            assert_abs_diff_eq!(frame[1], right[idx], epsilon = 1.0e-7);
        }
    }

    #[test]
    fn clips_float_output_to_unit_range() {
        let mut bs2b = Bs2b::default();
        let mut data = std::vec![10.0_f32, -10.0, 10.0, -10.0, 10.0, -10.0];
        bs2b.process_interleaved(&mut data)
            .expect("buffer should be valid");

        assert!(data.iter().all(|sample| (-1.0..=1.0).contains(sample)));
    }

    #[test]
    fn unsigned_and_signed_16_bit_paths_match() {
        let mut signed = std::vec![1000_i16, -2000, 8000, -100, -32000, 30000];
        let mut unsigned: Vec<u16> = signed.iter().map(|v| (*v as u16) ^ 0x8000).collect();

        let mut a = Bs2b::default();
        let mut b = Bs2b::default();

        a.process_interleaved(&mut signed)
            .expect("buffer should be valid");
        b.process_interleaved(&mut unsigned)
            .expect("buffer should be valid");

        let decoded: Vec<i16> = unsigned.into_iter().map(|v| (v ^ 0x8000) as i16).collect();

        assert_eq!(signed, decoded);
    }

    #[test]
    fn deterministic_against_reference_path() {
        let mut rng = StdRng::seed_from_u64(0x5eed_ba11);
        let mut input = Vec::with_capacity(512);
        for _ in 0..256 {
            input.push(rng.random_range(-1.0_f64..=1.0));
            input.push(rng.random_range(-1.0_f64..=1.0));
        }

        let mut a = Bs2b::default();
        let mut b = Bs2b::default();

        let mut output = input.clone();
        a.process_interleaved(&mut output)
            .expect("buffer should be valid");

        let mut expected = Vec::with_capacity(input.len());
        for frame in input.chunks_exact(2) {
            let (l, r) = reference_step(&mut b, frame[0], frame[1]);
            expected.push(l.clamp(-1.0, 1.0));
            expected.push(r.clamp(-1.0, 1.0));
        }

        for (lhs, rhs) in output.iter().zip(expected.iter()) {
            assert_abs_diff_eq!(lhs, rhs, epsilon = 1.0e-12);
        }
    }

    fn reference_step(bs2b: &mut Bs2b, left: f64, right: f64) -> (f64, f64) {
        let c = bs2b.coefficients;

        let lo_left = c.a0_lo * left + c.b1_lo * bs2b.state.lo[0];
        let lo_right = c.a0_lo * right + c.b1_lo * bs2b.state.lo[1];

        let hi_left = c.a0_hi * left + c.a1_hi * bs2b.state.asis[0] + c.b1_hi * bs2b.state.hi[0];
        let hi_right = c.a0_hi * right + c.a1_hi * bs2b.state.asis[1] + c.b1_hi * bs2b.state.hi[1];

        bs2b.state.asis = [left, right];
        bs2b.state.lo = [lo_left, lo_right];
        bs2b.state.hi = [hi_left, hi_right];

        ((hi_left + lo_right) * c.gain, (hi_right + lo_left) * c.gain)
    }
}