Skip to main content

qualia_audio/fx/stereo/
width.rs

1//! Mid/side stereo width control.
2//!
3//! Delegated to [`etalon_signal::reverb::stereo_width_interleaved_into`].
4
5use crate::types::AudioError;
6
7/// Apply mid/side width to interleaved stereo `in_stereo` → `out_stereo`.
8///
9/// Processes `n = min(in_stereo.len(), out_stereo.len()) / 2` frames and returns
10/// the frame count. Errors with [`AudioError::InvalidParameter`] if `width` is
11/// not finite.
12pub fn width(in_stereo: &[f32], out_stereo: &mut [f32], width: f32) -> Result<usize, AudioError> {
13    if !width.is_finite() {
14        return Err(AudioError::InvalidParameter);
15    }
16    Ok(etalon_signal::reverb::stereo_width_interleaved_into(
17        in_stereo, out_stereo, width,
18    ))
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24
25    #[test]
26    fn width_zero_collapses_to_mono() {
27        let inp = [1.0f32, -1.0, 0.5, 0.1, -0.3, 0.9];
28        let mut out = [0.0f32; 6];
29        let n = width(&inp, &mut out, 0.0).unwrap();
30        assert_eq!(n, 3);
31        for i in 0..3 {
32            let l = out[i * 2];
33            let r = out[i * 2 + 1];
34            assert!((l - r).abs() < 1e-9, "mono: L {} == R {}", l, r);
35            let mid = 0.5 * (inp[i * 2] + inp[i * 2 + 1]);
36            assert!((l - mid).abs() < 1e-9);
37        }
38    }
39
40    #[test]
41    fn width_one_is_identity() {
42        let inp: Vec<f32> = (0..64).map(|i| (i as f32 * 0.07).sin()).collect();
43        let mut out = vec![0.0f32; 64];
44        let n = width(&inp, &mut out, 1.0).unwrap();
45        assert_eq!(n, 32);
46        for i in 0..64 {
47            assert!((out[i] - inp[i]).abs() < 1e-6, "identity at {}", i);
48        }
49    }
50
51    #[test]
52    fn width_two_widens_side() {
53        let inp = [1.0f32, -1.0];
54        let mut out = [0.0f32; 2];
55        width(&inp, &mut out, 2.0).unwrap();
56        assert!((out[0] - 2.0).abs() < 1e-9);
57        assert!((out[1] + 2.0).abs() < 1e-9);
58    }
59
60    #[test]
61    fn non_finite_width_errors() {
62        let inp = [1.0f32, -1.0];
63        let mut out = [0.0f32; 2];
64        assert_eq!(
65            width(&inp, &mut out, f32::NAN),
66            Err(AudioError::InvalidParameter)
67        );
68    }
69}