Skip to main content

qualia_audio/fx/reverb/
allpass.rs

1//! Schroeder allpass filter (Freeverb variant).
2//!
3//! `y[n] = -x[n] + buf[n]`, `buf'[n] = x[n] + feedback · buf[n]`. Delegated to
4//! [`etalon_signal::reverb::allpass_step`].
5
6use crate::types::AudioError;
7
8/// Schroeder allpass diffuser — fixed delay + feedback.
9#[derive(Debug, Clone)]
10pub struct Allpass {
11    buffer: Vec<f32>,
12    index: usize,
13    feedback: f32,
14}
15
16impl Allpass {
17    /// Construct with delay `delay_samples` (≥ 1) and `feedback` (typ. 0.5).
18    pub fn new(delay_samples: usize, feedback: f32) -> Result<Self, AudioError> {
19        if !feedback.is_finite() {
20            return Err(AudioError::InvalidParameter);
21        }
22        Ok(Self {
23            buffer: vec![0.0; delay_samples.max(1)],
24            index: 0,
25            feedback: feedback.clamp(-0.999, 0.999),
26        })
27    }
28
29    /// Delay length in samples.
30    #[inline]
31    pub fn delay(&self) -> usize {
32        self.buffer.len()
33    }
34
35    /// Zero internal state. No allocation.
36    pub fn reset(&mut self) {
37        for s in self.buffer.iter_mut() {
38            *s = 0.0;
39        }
40        self.index = 0;
41    }
42
43    /// Process one sample. **Zero-alloc, no locks.**
44    #[inline]
45    pub fn process_sample(&mut self, input: f32) -> f32 {
46        etalon_signal::reverb::allpass_step(&mut self.buffer, &mut self.index, self.feedback, input)
47    }
48
49    /// Block process. **Zero-alloc.** Errors if `output` is shorter than `input`.
50    pub fn process(&mut self, input: &[f32], output: &mut [f32]) -> Result<(), AudioError> {
51        if output.len() < input.len() {
52            return Err(AudioError::OutputBufferTooSmall);
53        }
54        for (o, &x) in output.iter_mut().zip(input.iter()) {
55            *o = self.process_sample(x);
56        }
57        Ok(())
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn impulse_response_is_finite_and_diffuses() {
67        let d = 9usize;
68        let mut a = Allpass::new(d, 0.5).unwrap();
69        let n = 6 * d;
70        let mut input = vec![0.0f32; n];
71        input[0] = 1.0;
72        let mut out = vec![0.0f32; n];
73        a.process(&input, &mut out).unwrap();
74
75        assert!(
76            (out[0] + 1.0).abs() < 1e-6,
77            "y[0] should be -1, got {}",
78            out[0]
79        );
80        assert!(
81            out[d].abs() > 1e-6,
82            "expected energy at delay tap, got {}",
83            out[d]
84        );
85        assert!(out.iter().all(|x| x.is_finite()));
86    }
87
88    #[test]
89    fn energy_preserving_ish_and_decays() {
90        let mut a = Allpass::new(13, 0.5).unwrap();
91        let n = 2000;
92        let mut input = vec![0.0f32; n];
93        input[0] = 1.0;
94        let mut out = vec![0.0f32; n];
95        a.process(&input, &mut out).unwrap();
96        let early: f32 = out[..500].iter().map(|x| x * x).sum();
97        let late: f32 = out[1500..].iter().map(|x| x * x).sum();
98        assert!(
99            late < early,
100            "late energy {} should be < early {}",
101            late,
102            early
103        );
104        assert!(out.iter().all(|x| x.abs() < 10.0));
105    }
106
107    #[test]
108    fn output_too_small_errors() {
109        let mut a = Allpass::new(4, 0.5).unwrap();
110        let input = [0.0f32; 8];
111        let mut out = [0.0f32; 4];
112        assert_eq!(
113            a.process(&input, &mut out),
114            Err(AudioError::OutputBufferTooSmall)
115        );
116    }
117}