alpha2-audio 0.4.3

Platform-independent audio DSP core for alpha2.
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
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss
)]

use crate::biquad::flush_denormal;
use crate::{ProcessCtx, Processor, StateBlob, StateError};

/// Allocation-free prepared Schroeder reverb with four combs and two allpasses.
#[derive(Clone, Debug)]
pub struct SchroederReverb {
    room_size: f32,
    /// The authored damping: the combs' one-pole coefficient at the
    /// 44.1 kHz their lengths are given at.
    damping: f32,
    /// [`Self::damping`] at the prepared rate, so the filter keeps its time
    /// constant in seconds as the lines keep their lengths.
    damping_coefficient: f32,
    wet: f32,
    combs: Vec<DelayLine>,
    allpasses: Vec<DelayLine>,
    bypass: bool,
}

/// The loudest mono sample the lines take, far past any audio and far
/// enough under `f32::MAX` that no comb or allpass state can overflow.
const MAX_LINE_INPUT: f32 = 1.0e30;

/// The rate the line lengths and the damping are given at.
const REFERENCE_RATE: f64 = 44_100.0;
/// Comb lengths at [`REFERENCE_RATE`], shortest first.
const COMB_LENGTHS: [usize; 4] = [1116, 1188, 1277, 1356];
/// Allpass lengths at [`REFERENCE_RATE`].
const ALLPASS_LENGTHS: [usize; 2] = [556, 441];

/// The rate the lines are sized for: `rate` itself, bounded by the highest
/// rate any host runs the DSP at (the `AudioWorklet` binding takes every
/// `AudioContext` rate up to 768 kHz), so a direct caller cannot size them
/// from an absurd rate. Every rate in that range keeps the lines' lengths in
/// seconds, so the echoes come at the same times; at 768 kHz the lines hold
/// about 103 000 samples (413 KB), four times what they hold at 192 kHz.
fn line_rate(rate: u32) -> u32 {
    rate.clamp(1, crate::MAX_RESAMPLE_RATE)
}

/// A line `length` samples long at [`REFERENCE_RATE`], at `rate`.
fn line_length(length: usize, rate: u32) -> usize {
    (length as f32 * (rate as f32 / REFERENCE_RATE as f32)).round() as usize
}

/// The one-pole coefficient that damps at `rate` as `damping` does at
/// [`REFERENCE_RATE`]: a coefficient d decays by d each sample, so
/// d^(44100 / rate) decays as much each 1/44100 s.
fn damping_at(damping: f32, rate: u32) -> f32 {
    if damping > 0.0 && damping < 1.0 {
        libm::pow(f64::from(damping), REFERENCE_RATE / f64::from(rate)) as f32
    } else {
        damping
    }
}

#[derive(Clone, Debug)]
struct DelayLine {
    samples: Vec<f32>,
    cursor: usize,
    filter: f32,
}

impl DelayLine {
    fn new(length: usize) -> Self {
        Self {
            samples: vec![0.0; length.max(1)],
            cursor: 0,
            filter: 0.0,
        }
    }
    fn comb(&mut self, input: f32, feedback: f32, damping: f32) -> f32 {
        let delayed = self.samples[self.cursor];
        self.filter = flush_denormal(delayed * (1.0 - damping) + self.filter * damping);
        self.samples[self.cursor] = flush_denormal(input + self.filter * feedback);
        self.cursor = (self.cursor + 1) % self.samples.len();
        delayed
    }
    fn allpass(&mut self, input: f32) -> f32 {
        let delayed = self.samples[self.cursor];
        let output = delayed - input;
        self.samples[self.cursor] = flush_denormal(input + delayed * 0.5);
        self.cursor = (self.cursor + 1) % self.samples.len();
        output
    }
}

impl SchroederReverb {
    /// Creates a reverb with room, damping, and wet controls clamped to the unit interval.
    /// Non-finite values fall back to 0.7, 0.3, and 0.25.
    #[must_use]
    pub fn new(room_size: f32, damping: f32, wet: f32) -> Self {
        Self {
            room_size: unit(room_size, 0.7),
            damping: unit(damping, 0.3),
            damping_coefficient: unit(damping, 0.3),
            wet: unit(wet, 0.25),
            combs: Vec::new(),
            allpasses: Vec::new(),
            bypass: false,
        }
    }
    /// Enables transparent bypass while the tail continues evolving.
    pub fn set_bypass(&mut self, bypass: bool) {
        self.bypass = bypass;
    }
    /// How long the reverb rings after its input stops, in samples at
    /// `sample_rate`, prepared or not: until its slowest frequencies (the
    /// lowest, which the damping filter passes) have fallen 60 dB through
    /// the longest comb, plus the allpasses' own 60 dB. A bound for these
    /// room and damping values, not a fixed length: a room of 1 rings about
    /// four seconds.
    #[must_use]
    pub fn tail_samples(&self, sample_rate: u32) -> u64 {
        let safe_rate = line_rate(sample_rate);
        let longest = line_length(COMB_LENGTHS[COMB_LENGTHS.len() - 1], safe_rate) as f64;
        let damping = f64::from(damping_at(self.damping, safe_rate));
        let comb = if damping >= 1.0 {
            // The filter stays at zero, so a comb only repeats its input once.
            longest
        } else {
            let feedback = f64::from(self.feedback());
            // Each cycle loses ln(feedback) at low frequencies and takes the line plus the filter's
            // group delay there.
            let cycles = libm::log(1_000.0) / -libm::log(feedback);
            longest + cycles * (longest + damping / (1.0 - damping))
        };
        // An allpass's feedback of 0.5 loses 60 dB in ten trips.
        let allpasses = ALLPASS_LENGTHS
            .into_iter()
            .map(|length| line_length(length, safe_rate) as f64 * 10.0)
            .sum::<f64>();
        libm::ceil(comb + allpasses) as u64
    }
    /// The combs' feedback gain.
    fn feedback(&self) -> f32 {
        0.65 + self.room_size * 0.3
    }
}

impl Processor for SchroederReverb {
    fn history_samples(&self) -> u32 {
        let delay = self
            .combs
            .iter()
            .map(|line| line.samples.len())
            .max()
            .unwrap_or(0);
        let delay = u32::try_from(delay).unwrap_or(u32::MAX);
        let comb = if self.damping_coefficient >= 1.0 {
            // The zero-initialized feedback filter never changes in this case.
            f64::from(delay)
        } else {
            let damping = f64::from(self.damping_coefficient);
            let feedback = f64::from(self.feedback());
            (f64::from(delay) + 1.0 / (1.0 - damping)) * 32.0 / (1.0 - feedback)
        };
        let comb = libm::ceil(comb) as u32;
        self.allpasses.iter().fold(comb, |total, line| {
            total.saturating_add(
                u32::try_from(line.samples.len())
                    .unwrap_or(u32::MAX)
                    .saturating_mul(crate::processor::settling_samples(0.5)),
            )
        })
    }

    fn prepare(&mut self, sample_rate: u32, _max_block: usize) {
        let safe_rate = line_rate(sample_rate);
        self.combs = COMB_LENGTHS
            .into_iter()
            .map(|length| DelayLine::new(line_length(length, safe_rate)))
            .collect();
        self.allpasses = ALLPASS_LENGTHS
            .into_iter()
            .map(|length| DelayLine::new(line_length(length, safe_rate)))
            .collect();
        self.damping_coefficient = damping_at(self.damping, safe_rate);
    }
    fn process(&mut self, _ctx: &ProcessCtx, inputs: &[&[f32]], outputs: &mut [&mut [f32]]) {
        let channels = inputs.len().min(outputs.len()).min(2);
        let frames = inputs
            .iter()
            .take(channels)
            .map(|v| v.len())
            .chain(outputs.iter().take(channels).map(|v| v.len()))
            .min()
            .unwrap_or(0);
        if self.combs.is_empty() {
            return;
        }
        for frame in 0..frames {
            let mono = inputs[..channels]
                .iter()
                .map(|channel| channel[frame])
                .sum::<f32>()
                / channels.max(1) as f32;
            // The lines feed back on themselves, so a value they cannot hold would stay (as NaN)
            // until a reset. A non-finite sum is silence, and a finite one is held where no line
            // can overflow: a comb's gain is under 1 / (1 - 0.95) = 20 and an allpass's under 2.
            let mono = if mono.is_finite() {
                mono.clamp(-MAX_LINE_INPUT, MAX_LINE_INPUT)
            } else {
                0.0
            };
            let (feedback, damping) = (self.feedback(), self.damping_coefficient);
            let mut reverberant = self
                .combs
                .iter_mut()
                .map(|comb| comb.comb(mono, feedback, damping))
                .sum::<f32>()
                / self.combs.len() as f32;
            for allpass in &mut self.allpasses {
                reverberant = allpass.allpass(reverberant);
            }
            for channel in 0..channels {
                let input = finite_or_zero(inputs[channel][frame]);
                outputs[channel][frame] = if self.bypass {
                    input
                } else {
                    input * (1.0 - self.wet) + reverberant * self.wet
                };
            }
        }
    }
    fn reset(&mut self) {
        for delay in self.combs.iter_mut().chain(&mut self.allpasses) {
            delay.samples.fill(0.0);
            delay.cursor = 0;
            delay.filter = 0.0;
        }
    }
    fn save_state(&self) -> StateBlob {
        let mut bytes = Vec::new();
        for delay in self.combs.iter().chain(&self.allpasses) {
            bytes.extend_from_slice(&(u64::try_from(delay.cursor).unwrap_or(0)).to_le_bytes());
            bytes.extend_from_slice(&delay.filter.to_le_bytes());
            for sample in &delay.samples {
                bytes.extend_from_slice(&sample.to_le_bytes());
            }
        }
        StateBlob::new(bytes)
    }
    fn load_state(&mut self, state: &StateBlob) -> Result<(), StateError> {
        let bytes = state.as_bytes();
        // Every line's part is its cursor, its filter, then its samples: check them all before
        // taking any.
        let expected = self
            .combs
            .iter()
            .chain(&self.allpasses)
            .map(|delay| 12 + delay.samples.len() * 4)
            .sum::<usize>();
        if bytes.len() != expected {
            return Err(StateError::Mismatch);
        }
        let mut offset = 0;
        for delay in self.combs.iter().chain(&self.allpasses) {
            let cursor = usize::try_from(u64::from_le_bytes(
                bytes[offset..offset + 8].try_into().expect("fixed range"),
            ))
            .ok();
            let values = bytes[offset + 8..offset + 12 + delay.samples.len() * 4]
                .as_chunks::<4>()
                .0
                .iter()
                .map(|word| f32::from_le_bytes(*word));
            if cursor.is_none_or(|value| value >= delay.samples.len())
                || values.clone().any(|value| !value.is_finite())
            {
                return Err(StateError::Invalid);
            }
            offset += 12 + delay.samples.len() * 4;
        }
        let mut offset = 0;
        for delay in self.combs.iter_mut().chain(&mut self.allpasses) {
            let mut values = bytes[offset + 8..offset + 12 + delay.samples.len() * 4]
                .as_chunks::<4>()
                .0
                .iter()
                .map(|word| f32::from_le_bytes(*word));
            delay.cursor = usize::try_from(u64::from_le_bytes(
                bytes[offset..offset + 8].try_into().expect("fixed range"),
            ))
            .unwrap_or(0);
            delay.filter = values.next().unwrap_or(0.0);
            for (target, value) in delay.samples.iter_mut().zip(values) {
                *target = value;
            }
            offset += 12 + delay.samples.len() * 4;
        }
        Ok(())
    }
}

fn unit(value: f32, fallback: f32) -> f32 {
    if value.is_finite() {
        value.clamp(0.0, 1.0)
    } else {
        fallback
    }
}

fn finite_or_zero(value: f32) -> f32 {
    if value.is_finite() { value } else { 0.0 }
}

#[cfg(test)]
mod tests {
    use super::SchroederReverb;
    use crate::{ProcessCtx, Processor, TransportState};
    use alpha2_time::MediaTime;

    /// The impulse response of a fully wet reverb, `seconds` long.
    fn impulse_response(room_size: f32, damping: f32, sample_rate: u32, seconds: f64) -> Vec<f32> {
        let mut reverb = SchroederReverb::new(room_size, damping, 1.0);
        reverb.prepare(sample_rate, 512);
        let ctx = ProcessCtx {
            position: MediaTime::ZERO,
            sample_position: 0,
            sample_rate,
            transport: TransportState::Playing,
        };
        let frames = (f64::from(sample_rate) * seconds) as usize;
        let mut input = vec![0.0; frames];
        input[0] = 1.0;
        let mut output = vec![0.0; frames];
        for (input, output) in input.chunks(512).zip(output.chunks_mut(512)) {
            reverb.process(&ctx, &[input], &mut [output]);
        }
        output
    }

    /// How fast `response` decays around `frequency`, in dB a second:
    /// the energy through a fourth-order band-pass (two RBJ sections,
    /// Q 1.414) over 0.2 to 0.4 s against 0.6 to 0.8 s.
    fn band_decay_db_per_second(response: &[f32], sample_rate: u32, frequency: f64) -> f64 {
        let omega = core::f64::consts::TAU * frequency / f64::from(sample_rate);
        let alpha = libm::sin(omega) / (2.0 * 1.414);
        let a0 = 1.0 + alpha;
        let (b0, b2, a1, a2) = (
            alpha / a0,
            -alpha / a0,
            -2.0 * libm::cos(omega) / a0,
            (1.0 - alpha) / a0,
        );
        let mut signal = response
            .iter()
            .map(|&sample| f64::from(sample))
            .collect::<Vec<_>>();
        for _ in 0..2 {
            let (mut x1, mut x2, mut y1, mut y2) = (0.0, 0.0, 0.0, 0.0);
            for sample in &mut signal {
                let y = b0 * *sample + b2 * x2 - a1 * y1 - a2 * y2;
                (x2, x1, y2, y1) = (x1, *sample, y1, y);
                *sample = y;
            }
        }
        let energy = |from: f64, to: f64| {
            let rate = f64::from(sample_rate);
            signal[(from * rate) as usize..(to * rate) as usize]
                .iter()
                .map(|sample| sample * sample)
                .sum::<f64>()
        };
        10.0 * libm::log10(energy(0.2, 0.4) / energy(0.6, 0.8)) / 0.4
    }

    #[test]
    fn a_room_decays_alike_in_every_band_at_any_project_rate() {
        for (room_size, damping) in [(0.7, 0.3), (0.7, 0.5), (0.9, 0.2)] {
            for frequency in [500.0, 5_000.0, 8_000.0] {
                let reference = band_decay_db_per_second(
                    &impulse_response(room_size, damping, 48_000, 1.0),
                    48_000,
                    frequency,
                );
                for sample_rate in [44_100, 96_000] {
                    let decay = band_decay_db_per_second(
                        &impulse_response(room_size, damping, sample_rate, 1.0),
                        sample_rate,
                        frequency,
                    );
                    let difference = (decay / reference - 1.0).abs();
                    assert!(
                        difference < 0.08,
                        "room {room_size} damping {damping} at {frequency} Hz: {decay:.1} dB/s at {sample_rate} Hz, {reference:.1} at 48 kHz"
                    );
                }
            }
        }
    }

    #[test]
    fn the_tail_bound_outlasts_every_room_without_overstating_it() {
        for sample_rate in [44_100, 48_000, 96_000] {
            for room_size in [1.0, 0.95, 0.9, 0.5, 0.0] {
                for damping in [0.0, 0.3, 0.7, 1.0] {
                    let response = impulse_response(room_size, damping, sample_rate, 5.5);
                    let peak = response
                        .iter()
                        .fold(0.0_f32, |peak, sample| peak.max(sample.abs()));
                    // The last sample within 60 dB of the peak.
                    let rings = response
                        .iter()
                        .rposition(|sample| sample.abs() >= peak * 1.0e-3)
                        .expect("a response") as u64;
                    assert!(
                        rings < response.len() as u64 - u64::from(sample_rate) / 4,
                        "the response is long enough to measure"
                    );
                    let mut reverb = SchroederReverb::new(room_size, damping, 1.0);
                    let tail = reverb.tail_samples(sample_rate);
                    reverb.prepare(sample_rate, 512);
                    assert_eq!(reverb.tail_samples(sample_rate), tail, "prepared or not");
                    assert!(
                        rings <= tail,
                        "room {room_size} damping {damping} at {sample_rate} Hz rings {rings} samples, past {tail}"
                    );
                    assert!(
                        tail <= rings * 3,
                        "room {room_size} damping {damping} at {sample_rate} Hz rings {rings} samples, far under {tail}"
                    );
                }
            }
        }
    }

    #[test]
    fn a_decaying_tail_never_leaves_subnormal_state() {
        // A small, bright room empties its combs within seconds, after which the allpasses only
        // halve what they hold.
        let mut reverb = SchroederReverb::new(0.0, 0.0, 1.0);
        reverb.prepare(48_000, 128);
        let ctx = ProcessCtx {
            position: MediaTime::ZERO,
            sample_position: 0,
            sample_rate: 48_000,
            transport: TransportState::Playing,
        };
        let burst = [0.5_f32; 128];
        let silence = [0.0_f32; 128];
        let mut output = [0.0_f32; 128];
        reverb.process(&ctx, &[&burst], &mut [&mut output]);
        for block in 0..3_750 {
            reverb.process(&ctx, &[&silence], &mut [&mut output]);
            let subnormal = reverb
                .combs
                .iter()
                .chain(&reverb.allpasses)
                .flat_map(|line| line.samples.iter().chain([&line.filter]))
                .find(|value| value.is_subnormal());
            assert_eq!(subnormal, None, "block {block}");
        }
    }
}