manguebyte 0.2.0

The Manguebyte Audio Framework. Modern, real-time-safe audio for Rust: buffers, DSP, MIDI, audio files and device I/O in one place.
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
//! Offline rendering is deterministic and does not depend on block size.
//!
//! A host may hand a plugin blocks of any length, and a different length on
//! every call. The sound must not depend on it: notes, automation and effect
//! tails have to land on the same frames however the timeline is cut up.
//! This renders the example song several ways and requires the results to be
//! identical, bit for bit, and does the same with denser material: more
//! notes at once than the synth has voices, rendered again after a reset
//! and played again once it has ended. The song also goes through the
//! processors that keep state of their own across blocks (partitioned
//! convolution, FIR filters, an expander and a wavetable oscillator), with
//! the same requirement.

#[path = "../examples/common/mod.rs"]
mod common;

use std::sync::Arc;

use common::RiffPlayer;
use manguebyte::prelude::*;

const SAMPLE_RATE: f64 = 48_000.0;

/// Renders the whole song, asking for blocks of the lengths in `pattern`,
/// repeated for as long as it takes.
fn render(max_block_size: usize, pattern: &[usize]) -> AudioBuffer<f32> {
    let mut player = RiffPlayer::new(SAMPLE_RATE, 2, max_block_size);
    let frames = player.total_frames();
    let mut song = AudioBuffer::<f32>::new(2, frames);
    let mut position = 0;
    for &length in pattern.iter().cycle() {
        if position == frames {
            break;
        }
        let length = length.min(frames - position);
        song.windowed(position..position + length, |block| player.render(block));
        position += length;
    }
    assert!(player.is_finished());
    song
}

#[test]
fn the_song_is_the_same_whatever_the_block_size() {
    let reference = render(256, &[256]);
    assert!(reference.peak() > 0.1, "the reference render is silent");

    for (max_block_size, pattern) in [
        (64, &[64][..]),
        (1024, &[1024]),
        // Odd lengths that never line up with a note boundary.
        (997, &[997]),
        // A host that changes its mind on every call.
        (512, &[1, 511, 7, 300, 64, 2, 129]),
        // Blocks larger than the player's own, which it must split itself.
        (48, &[5_000]),
    ] {
        let other = render(max_block_size, pattern);
        assert!(
            other == reference,
            "blocks of {pattern:?} (max {max_block_size}) changed the output"
        );
    }
}

#[test]
fn two_renders_are_identical() {
    assert!(render(256, &[256]) == render(256, &[256]));
}

/// The processors whose state runs across blocks in ways of their own: a
/// partitioned convolution with no latency and one with a partition of
/// latency, FIR filters in both of their modes, an expander's envelope, and
/// a wavetable oscillator whose phase the song modulates. Each block is
/// fed to them in pieces of at most the size they were prepared for.
struct Effects {
    expander: Expander<f32>,
    direct: FirFilter<f32>,
    long: FirFilter<f32>,
    convolution: Convolution<f32>,
    _loader: ConvolutionLoader<f32>,
    latent: ConvolutionEngine<f32>,
    wavetable: WavetableOscillator,
    modulator: AudioBuffer<f32>,
    max_block_size: usize,
}

impl Effects {
    fn new(max_block_size: usize) -> Self {
        let spec = ProcessSpec {
            sample_rate: SAMPLE_RATE,
            max_block_size,
            num_channels: 2,
        };
        // A decaying noise, a different one for each channel.
        let response = |seed: u64| {
            let mut state = seed;
            (0..3_000)
                .map(|n| {
                    state ^= state << 13;
                    state ^= state >> 7;
                    state ^= state << 17;
                    let noise = (state >> 40) as f32 / (1 << 24) as f32 - 0.5;
                    noise * (-(n as f32) / 600.0).exp() * 0.1
                })
                .collect::<Vec<f32>>()
        };
        let stereo = [
            response(0x2545_F491_4F6C_DD1D),
            response(0x9E37_79B9_7F4A_7C15),
        ];
        let engine = ConvolutionEngine::new(
            &stereo,
            2,
            ConvolutionRouting::Parallel,
            ConvolutionSettings::new().with_partition_size(64),
        )
        .expect("a valid response");
        let (convolution, loader) = Convolution::from_engine(engine);
        let latent = ConvolutionEngine::new(
            &stereo[..1],
            2,
            ConvolutionRouting::Parallel,
            ConvolutionSettings::new()
                .with_partition_size(128)
                .with_zero_latency(false),
        )
        .expect("a valid response");
        let direct = FirFilter::new(&manguebyte::dsp::fir::lowpass(31, 0.2, 60.0)).unwrap();
        let long = FirFilter::new(&manguebyte::dsp::fir::lowpass(255, 0.15, 80.0)).unwrap();
        assert_eq!(direct.mode(), FirMode::Direct);
        assert_eq!(long.mode(), FirMode::Fft);
        let saw: Vec<f32> = (0..2_048).map(|i| i as f32 / 1_024.0 - 1.0).collect();
        let table = Arc::new(Wavetable::new(&[saw]).expect("a valid frame"));
        let mut wavetable = WavetableOscillator::new(table, 110.0);
        wavetable.set_phase_modulation(0.5);
        let mut effects = Self {
            expander: Expander::new().with_threshold_db(-30.0).with_ratio(3.0),
            direct,
            long,
            convolution,
            _loader: loader,
            latent,
            wavetable,
            modulator: AudioBuffer::new(2, max_block_size),
            max_block_size,
        };
        effects.expander.prepare(&spec);
        effects.direct.prepare(&spec);
        effects.long.prepare(&spec);
        effects.convolution.prepare(&spec);
        effects.latent.prepare(&spec);
        Processor::<f32>::prepare(&mut effects.wavetable, &spec);
        effects
    }

    fn process(&mut self, block: &mut AudioBuffer<f32>) {
        let frames = block.num_frames();
        let mut done = 0;
        while done < frames {
            let length = (frames - done).min(self.max_block_size);
            block.windowed(done..done + length, |piece| self.piece(piece));
            done += length;
        }
    }

    fn piece(&mut self, piece: &mut AudioBuffer<f32>) {
        self.modulator.set_num_frames(piece.num_frames());
        for channel in 0..2 {
            self.modulator
                .channel_mut(channel)
                .copy_from_slice(piece.channel(channel));
        }
        self.wavetable.process(&mut self.modulator);
        self.expander.process(piece);
        self.direct.process(piece);
        self.long.process(piece);
        self.convolution.process(piece);
        self.latent.process(piece);
        for channel in 0..2 {
            let tone = self.modulator.channel(channel);
            for (out, &tone) in piece.channel_mut(channel).iter_mut().zip(tone) {
                *out += 0.25 * tone;
            }
        }
    }
}

/// The first two and a half seconds of the song through [`Effects`],
/// asking for blocks of the lengths in `pattern`. (The whole song, in a
/// debug build, takes over a minute for the five renders.)
fn render_through_effects(max_block_size: usize, pattern: &[usize]) -> AudioBuffer<f32> {
    let mut player = RiffPlayer::new(SAMPLE_RATE, 2, max_block_size);
    let mut effects = Effects::new(max_block_size);
    let frames = player.total_frames().min(120_000);
    let mut song = AudioBuffer::<f32>::new(2, frames);
    let mut position = 0;
    for &length in pattern.iter().cycle() {
        if position == frames {
            break;
        }
        let length = length.min(frames - position);
        song.windowed(position..position + length, |block| {
            player.render(block);
            effects.process(block);
        });
        position += length;
    }
    song
}

#[test]
fn convolution_fir_expander_and_wavetable_are_the_same_whatever_the_block_size() {
    let reference = render_through_effects(256, &[256]);
    assert!(reference.peak() > 0.05, "the reference render is silent");
    let plain = render(256, &[256]);
    assert!(
        reference.channel(0) != &plain.channel(0)[..reference.num_frames()],
        "the effects changed nothing"
    );
    for (max_block_size, pattern) in [
        (64, &[64][..]),
        (997, &[997]),
        (512, &[1, 511, 7, 300, 64, 2, 129]),
        (48, &[5_000]),
    ] {
        let other = render_through_effects(max_block_size, pattern);
        assert_eq!(
            parting(&other, &reference),
            None,
            "blocks of {pattern:?} (max {max_block_size})"
        );
    }
}

#[test]
fn the_song_contains_the_notes_that_were_asked_for() {
    use manguebyte::dsp::fft::{Complex, RealFft};
    use manguebyte::dsp::window::{Symmetry, Window};

    let song = render(256, &[256]);

    // The first half second holds an A minor chord over a low A.
    let len = 16_384;
    let mut slice: Vec<f32> = song.channel(0)[2_000..2_000 + len].to_vec();
    Window::Hann.apply(&mut slice, Symmetry::Periodic);
    let mut fft = RealFft::<f32>::new(len);
    let mut spectrum = vec![Complex::ZERO; fft.spectrum_len()];
    fft.forward(&slice, &mut spectrum);

    let level_at = |hz: f64| {
        let bin = (hz * len as f64 / SAMPLE_RATE).round() as usize;
        (bin - 1..=bin + 1)
            .map(|index| spectrum[index].norm())
            .fold(0.0f32, f32::max)
    };
    // Something no note in the song produces: between A2 and its octave.
    let floor = level_at(160.0);
    for key in [45.0, 57.0, 60.0, 64.0] {
        let level = level_at(note_to_hz(key));
        assert!(
            level > 8.0 * floor,
            "key {key}: level {level} against a floor of {floor}"
        );
    }
}

// --- Dense material (TECH_DEBT 42) ----------------------------------------
//
// In REAPER, two offline renders of one project with many overlapping notes
// differed by a unit in the last place. The song above has few voices at a
// time; this material has more notes sounding than there are voices, all
// at full velocity, with every voice's samples far from whole numbers, so
// that the order in which the synth adds its voices up shows in the last
// bit of the output.

/// One note of an instrument like the example `plugins/pluck`: a sawtooth
/// through a resonant low-pass that closes as the note fades.
struct Saw {
    oscillator: Oscillator,
    filter: StateVariableFilter<f32>,
    envelope: Adsr,
    velocity: f32,
}

impl Saw {
    fn new() -> Self {
        Self {
            oscillator: Oscillator::new(Waveform::Saw, 220.0),
            filter: StateVariableFilter::new(SvfMode::Lowpass, 9_000.0, 0.9),
            envelope: Adsr::new(AdsrParams {
                attack: 0.003,
                decay: 1.5,
                sustain: 0.4,
                release: 1.2,
            }),
            velocity: 0.0,
        }
    }
}

impl Voice for Saw {
    fn prepare(&mut self, spec: &ProcessSpec) {
        self.oscillator.set_sample_rate(spec.sample_rate);
        self.envelope.set_sample_rate(spec.sample_rate);
        self.filter.prepare(&ProcessSpec {
            num_channels: 1,
            ..*spec
        });
    }

    fn note_on(&mut self, note: NoteRef, velocity: f32) {
        self.oscillator
            .set_frequency(note_to_hz(f64::from(note.key)));
        self.velocity = velocity;
        self.envelope.note_on();
    }

    fn note_off(&mut self, _velocity: f32) {
        self.envelope.note_off();
    }

    fn choke(&mut self) {
        self.envelope.silence();
        self.filter.reset();
        self.oscillator.reset_phase();
    }

    fn is_active(&self) -> bool {
        self.envelope.is_active()
    }

    fn render(&mut self, buffer: &mut AudioBuffer<f32>) {
        for frame in 0..buffer.num_frames() {
            let level = self.envelope.next_sample();
            self.filter.set_frequency(9_000.0 * (0.25 + 0.75 * level));
            let raw = self.oscillator.next_sample() as f32;
            let sample = self.filter.process_sample(0, raw) * level as f32 * self.velocity;
            for channel in buffer.iter_channels_mut() {
                channel[frame] += sample;
            }
        }
    }
}

struct DenseParams {
    reverb: FloatParam,
}
param_set!(DenseParams { reverb });

/// Sixteen of those voices and a reverb, as a plugin, so that the harness
/// can play it as a host does.
struct Dense {
    params: Arc<DenseParams>,
    synth: Synth<Saw>,
    reverb: Reverb<f32>,
}

impl Plugin for Dense {
    const INFO: PluginInfo = PluginInfo {
        id: "org.manguebyte.test.dense",
        name: "Dense",
        vendor: "Manguebyte",
        version: "1",
        description: "Many voices at once",
        kind: PluginKind::Instrument,
    };
    const LAYOUT: AudioLayout = AudioLayout::STEREO_INSTRUMENT;
    type Params = DenseParams;

    fn new() -> Self {
        Self {
            params: Arc::new(DenseParams {
                reverb: FloatParam::new("reverb", "Reverb", FloatRange::linear(0.0, 1.0), 0.5),
            }),
            synth: Synth::with_voices(16, Saw::new),
            reverb: Reverb::new().with_decay_seconds(2.0),
        }
    }

    fn params(&self) -> Arc<DenseParams> {
        Arc::clone(&self.params)
    }

    fn prepare(&mut self, spec: &ProcessSpec) {
        self.synth.prepare(spec);
        self.reverb.prepare(spec);
    }

    fn process(&mut self, buffer: &mut AudioBuffer<f32>, context: &ProcessContext<'_, f32>) {
        self.synth.process_with(buffer, context);
        // Held still in these tests, so reading it once per block is exact.
        self.reverb.set_mix(f64::from(self.params.reverb.value()));
        self.reverb.process(buffer);
    }

    fn reset(&mut self) {
        self.synth.reset();
        self.reverb.reset();
    }
}

/// Seconds of dense material; renders run on for the release.
const DENSE_SECONDS: f64 = 10.0;

fn dense_frames() -> usize {
    ((DENSE_SECONDS + 1.5) * SAMPLE_RATE) as usize
}

/// 37 notes at full velocity in ten seconds, keys from 36 to 96, chords
/// struck on one frame and notes up to three seconds long: often more than
/// sixteen at once, so voices are stolen too.
fn schedule_dense(harness: &mut Harness<Dense>, from: u64) {
    let mut state = 0x9E37_79B9_7F4A_7C15_u64;
    let mut below = move |n: u64| {
        state ^= state << 13;
        state ^= state >> 7;
        state ^= state << 17;
        state % n
    };
    let span = (DENSE_SECONDS * SAMPLE_RATE) as u64;
    let mut start = 0;
    for index in 0..37_u64 {
        if index % 4 != 3 {
            start = index * span / 38 + below(4_800);
        }
        let length = (12_000 + below(132_000)).min(span - start);
        harness.note(from + start, length, 36 + below(61) as u8, 1.0);
    }
}

fn render_dense(max_block_size: usize, pattern: &[usize]) -> AudioBuffer<f32> {
    let mut harness =
        Harness::<Dense>::new(SAMPLE_RATE, max_block_size).with_block_pattern(pattern);
    schedule_dense(&mut harness, 0);
    harness.render(dense_frames())
}

/// Where two renders part, if they do, and by how much against the
/// loudest sample, in decibels.
fn parting(a: &AudioBuffer<f32>, b: &AudioBuffer<f32>) -> Option<String> {
    let mut first = None;
    let mut largest = 0.0_f32;
    for channel in 0..a.num_channels() {
        for (frame, (x, y)) in a
            .channel(channel)
            .iter()
            .zip(b.channel(channel))
            .enumerate()
        {
            if x.to_bits() != y.to_bits() {
                first = Some(first.map_or(frame, |at: usize| at.min(frame)));
                largest = largest.max((x - y).abs());
            }
        }
    }
    first.map(|frame| {
        let below = 20.0 * (largest / a.peak().max(b.peak())).log10();
        format!("from frame {frame}, by up to {largest:e}, {below:.0} dB")
    })
}

#[test]
fn dense_material_is_the_same_whatever_the_block_size() {
    let reference = render_dense(256, &[256]);
    assert!(reference.peak() > 1.0, "too few notes overlap");
    for (max_block_size, pattern) in [
        (64, &[64][..]),
        (997, &[997]),
        (512, &[1, 511, 7, 300, 64, 2, 129]),
    ] {
        let other = render_dense(max_block_size, pattern);
        assert_eq!(parting(&other, &reference), None, "blocks of {pattern:?}");
    }
}

#[test]
fn dense_material_after_a_reset_is_the_same_as_the_first_time() {
    let first = render_dense(256, &[256]);
    let mut harness = Harness::<Dense>::new(SAMPLE_RATE, 256).with_block_pattern(&[256]);
    schedule_dense(&mut harness, 0);
    harness.render(dense_frames());
    harness.reset();
    let from = harness.position();
    schedule_dense(&mut harness, from);
    let again = harness.render(dense_frames());
    assert_eq!(parting(&again, &first), None);
}

#[test]
fn dense_material_played_again_after_it_ends_is_the_same_without_a_reset() {
    // No reverb, whose tail would rightly carry over: the synth alone.
    let mut harness = Harness::<Dense>::new(SAMPLE_RATE, 256);
    harness.params().reverb.set(0.0);
    schedule_dense(&mut harness, 0);
    let first = harness.render(dense_frames());
    assert!(first.peak() > 1.0);
    // Every note has ended: the material ends with a second and a half of
    // room for the release, which is 1.2 s.
    let quiet = harness.render(4_800);
    assert_eq!(quiet.peak(), 0.0);
    let from = harness.position();
    schedule_dense(&mut harness, from);
    let again = harness.render(dense_frames());
    assert_eq!(parting(&again, &first), None);
}