plughost-core 0.2.2

Shared types and offline render rules for plughost
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
use std::collections::VecDeque;

use super::*;
use crate::{Event, FailureKind};

const RATE: f64 = 48_000.0;

#[test]
fn default_tail_policy_uses_one_second_of_silence_with_a_thirty_second_cap() {
    let options = RenderOptions::default();
    assert_eq!(
        options.tail,
        TailPolicy::UntilSilence {
            threshold: 1.585e-5,
            hold_seconds: 1.0,
        }
    );
    assert_eq!(options.max_tail_seconds, 30.0);
}

/// A stereo processor whose output is the input delayed by `latency`, plus an echo of it
/// `echo_delay` samples later at half level.
struct Echo {
    latency: u32,
    echo_delay: usize,
    reported_tail: Tail,
    history: Vec<VecDeque<f32>>,
    latency_after_blocks: Option<(usize, u32)>,
    tail_after_blocks: Option<(usize, Tail)>,
    blocks: usize,
    /// The events each block received, by block number.
    events: Vec<(usize, Events)>,
    max_block_size: usize,
}

impl Echo {
    fn new(latency: u32, echo_delay: usize, reported_tail: Tail) -> Echo {
        let depth = latency as usize + echo_delay + 1;
        Echo {
            latency,
            echo_delay,
            reported_tail,
            history: vec![VecDeque::from(vec![0.0; depth]); 2],
            latency_after_blocks: None,
            tail_after_blocks: None,
            blocks: 0,
            events: Vec::new(),
            max_block_size: 256,
        }
    }
}

impl Process<f32> for Echo {
    type Error = RenderError;

    fn sample_rate(&self) -> f64 {
        RATE
    }

    fn max_block_size(&self) -> usize {
        self.max_block_size
    }

    fn input_channels(&self) -> usize {
        2
    }

    fn output_channels(&self) -> usize {
        2
    }

    fn latency(&self) -> Result<u32, RenderError> {
        Ok(match self.latency_after_blocks {
            Some((blocks, latency)) if self.blocks >= blocks => latency,
            _ => self.latency,
        })
    }

    fn tail(&self) -> Result<Tail, RenderError> {
        Ok(match self.tail_after_blocks {
            Some((blocks, tail)) if self.blocks >= blocks => tail,
            _ => self.reported_tail,
        })
    }

    fn process(
        &mut self,
        _context: &crate::BlockContext,
        input: &[&[f32]],
        output: &mut [&mut [f32]],
        _automation: &[crate::AutomationEvent],
        events: &Events,
        produced: &mut Events,
    ) -> Result<(), RenderError> {
        self.events.push((self.blocks, events.clone()));
        // The events come back out on port 1, one sample later where the block allows.
        produced.clear();
        for event in events {
            let offset = (event.offset + 1).min(input[0].len() - 1);
            produced.push(event.at(offset).on_port(1)).unwrap();
        }
        let direct = self.latency as usize;
        let echo = direct + self.echo_delay;
        for ((history, source), destination) in self.history.iter_mut().zip(input).zip(output) {
            for (sample, out) in source.iter().zip(destination.iter_mut()) {
                history.push_front(*sample);
                history.pop_back();
                let echoed = if self.echo_delay > 0 {
                    0.5 * history[echo]
                } else {
                    0.0
                };
                *out = history[direct] + echoed;
            }
        }
        self.blocks += 1;
        Ok(())
    }
}

fn signal(frames: usize) -> Vec<Vec<f32>> {
    (0..2)
        .map(|ch| {
            (0..frames)
                .map(|i| ((i * (ch + 3)) % 17) as f32 / 17.0 + 0.1)
                .collect()
        })
        .collect()
}

fn run(
    processor: &mut Echo,
    input: &[Vec<f32>],
    options: RenderOptions,
) -> Result<Rendered<f32>, RenderError> {
    let slices: Vec<&[f32]> = input.iter().map(Vec::as_slice).collect();
    render(processor, &slices, input[0].len(), &Events::new(), &options)
}

fn reported(max_tail_seconds: f64) -> RenderOptions {
    RenderOptions {
        tail: TailPolicy::Reported,
        max_tail_seconds,
    }
}

#[test]
fn latency_is_removed_so_output_lines_up_with_input() {
    let input = signal(1000);
    let rendered = run(
        &mut Echo::new(300, 0, Tail::Samples(0)),
        &input,
        reported(1.0),
    )
    .unwrap();
    assert_eq!(rendered.latency, 300);
    assert_eq!(rendered.channels, input);
}

#[test]
fn reported_tail_is_rendered_after_the_input() {
    let input = signal(1000);
    let rendered = run(
        &mut Echo::new(64, 300, Tail::Samples(300)),
        &input,
        reported(1.0),
    )
    .unwrap();
    assert_eq!(rendered.tail, 300);
    for (out, source) in rendered.channels.iter().zip(&input) {
        assert_eq!(out.len(), 1300);
        for i in 1000..1300 {
            assert_eq!(out[i], 0.5 * source[i - 300]);
        }
    }
}

#[test]
fn infinite_tail_stops_at_the_maximum() {
    let input = signal(1000);
    let rendered = run(&mut Echo::new(0, 0, Tail::Infinite), &input, reported(0.01)).unwrap();
    assert_eq!(rendered.tail, 480);
    assert_eq!(rendered.channels[0].len(), 1480);
    // Only a tail waiting for silence ends at a limit; a reported one is planned to it.
    assert_eq!(rendered.status, RenderStatus::Complete);
}

#[test]
fn until_silence_waits_through_gaps_shorter_than_the_hold() {
    let input = signal(1000);
    let options = RenderOptions {
        tail: TailPolicy::UntilSilence {
            threshold: 1e-6,
            hold_seconds: 0.1,
        },
        max_tail_seconds: 1.0,
    };
    // The echo starts 2000 samples after the input ends: a silent gap shorter than the hold.
    let rendered = run(&mut Echo::new(10, 3000, Tail::Samples(0)), &input, options).unwrap();
    assert_eq!(rendered.tail, 3000);
    assert_eq!(rendered.channels[1][3999], 0.5 * input[1][999]);
    assert_eq!(rendered.status, RenderStatus::Complete);
}

#[test]
fn a_silence_hold_near_the_largest_length_does_not_overflow() {
    // The largest hold below 2^64 samples at this rate, with the tail's first quiet sample past
    // index 4096 of a block.
    let options = RenderOptions {
        tail: TailPolicy::UntilSilence {
            threshold: 1e-6,
            hold_seconds: 384_307_168_202_282.25,
        },
        max_tail_seconds: 0.1,
    };
    let mut processor = Echo::new(0, 0, Tail::Samples(0));
    processor.max_block_size = 16_384;
    let input = signal(5000);
    let rendered = run(&mut processor, &input, options).unwrap();
    assert_eq!(rendered.channels[0][..5000], input[0]);
}

#[test]
fn until_silence_stops_at_the_maximum_tail() {
    let input = signal(1000);
    let options = RenderOptions {
        tail: TailPolicy::UntilSilence {
            threshold: 1e-6,
            hold_seconds: 0.1,
        },
        max_tail_seconds: 0.01,
    };
    let rendered = run(&mut Echo::new(0, 3000, Tail::Samples(0)), &input, options).unwrap();
    assert_eq!(rendered.tail, 0);
    assert_eq!(rendered.channels[0].len(), 1000);
    assert_eq!(rendered.status, RenderStatus::TailLimit);
}

#[test]
fn latency_change_during_rendering_is_an_error() {
    let input = signal(1000);
    let mut processor = Echo::new(10, 0, Tail::Samples(0));
    processor.latency_after_blocks = Some((2, 20));
    let error = run(&mut processor, &input, reported(1.0)).unwrap_err();
    assert_eq!(
        error,
        RenderError::LatencyChanged {
            before: 10,
            after: 20
        }
    );
}

#[test]
fn a_reported_tail_is_planned_when_the_render_starts() {
    let input = signal(1000);
    let changing = || {
        let mut processor = Echo::new(0, 0, Tail::Samples(2400));
        processor.tail_after_blocks = Some((1, Tail::Samples(4800)));
        processor
    };
    let rendered = run(&mut changing(), &input, RenderOptions::default()).unwrap();
    assert_eq!(rendered.channels, input);
    // The tail the processor reports after the first block does not change the plan.
    let rendered = run(&mut changing(), &input, reported(1.0)).unwrap();
    assert_eq!(rendered.tail, 2400);
    let rendered = run(&mut changing(), &input, reported(0.01)).unwrap();
    assert_eq!(rendered.tail, 480);
}

#[test]
fn input_must_match_the_processor() {
    let mut processor = Echo::new(0, 0, Tail::Samples(0));
    let mono = [vec![0.0f32; 10]];
    assert_eq!(
        run(&mut processor, &mono, reported(1.0)).unwrap_err(),
        RenderError::Input(crate::InputError::AudioBuffers)
    );
    let uneven: Vec<&[f32]> = vec![&[0.0; 10], &[0.0; 9]];
    assert_eq!(
        render(&mut processor, &uneven, 10, &Events::new(), &reported(1.0)).unwrap_err(),
        RenderError::Input(crate::InputError::AudioBuffers)
    );
}

#[test]
fn events_reach_the_block_they_fall_in_with_block_offsets() {
    let input = signal(1000);
    let slices: Vec<&[f32]> = input.iter().map(Vec::as_slice).collect();
    let mut processor = Echo::new(0, 0, Tail::Samples(0));
    let on = Event::note_on(10, 0, 60, 100);
    let off = Event::note_off(300, 0, 60, 0);
    render(
        &mut processor,
        &slices,
        1000,
        &Events::from_iter([on, off]),
        &reported(1.0),
    )
    .unwrap();
    let received: Vec<(usize, Events)> = processor
        .events
        .into_iter()
        .filter(|(_, events)| !events.is_empty())
        .collect();
    assert_eq!(
        received,
        [
            (0, Events::from_iter([on])),
            (1, Events::from_iter([off.at(44)]))
        ]
    );
    // Output events count from the start of the input, whichever block produced them.
    let rendered = render(
        &mut Echo::new(0, 0, Tail::Samples(0)),
        &slices,
        1000,
        &Events::from_iter([on, off]),
        &reported(1.0),
    )
    .unwrap();
    assert_eq!(
        rendered.events,
        [on, off].map(|event| event.on_port(1).at(event.offset + 1))
    );
    let unordered = render(
        &mut Echo::new(0, 0, Tail::Samples(0)),
        &slices,
        1000,
        &Events::from_iter([off, on]),
        &reported(1.0),
    );
    assert_eq!(
        unordered.unwrap_err(),
        RenderError::Input(crate::InputError::Events)
    );
}

#[test]
fn output_events_keep_processing_offsets_when_audio_latency_is_trimmed() {
    let input = signal(1000);
    let slices: Vec<_> = input.iter().map(Vec::as_slice).collect();
    // Echo emits an event one frame later, clamped to its 256-frame block. Audio loses its
    // first 300 processing frames; events retain the documented processing timeline, including
    // events in that trimmed prefix and the event at the end of the aligned audio.
    let events: Events = [0, 298, 299, 300, 511, 512, 999]
        .into_iter()
        .enumerate()
        .map(|(id, frame)| Event::control_change(frame, 0, id as u8, 64))
        .collect();
    let rendered = render(
        &mut Echo::new(300, 0, Tail::Samples(0)),
        &slices,
        1000,
        &events,
        &reported(0.0),
    )
    .unwrap();
    assert_eq!(rendered.channels, input);
    assert_eq!(
        rendered.events,
        [
            Event::control_change(1, 0, 0, 64).on_port(1),
            Event::control_change(299, 0, 1, 64).on_port(1),
            Event::control_change(300, 0, 2, 64).on_port(1),
            Event::control_change(301, 0, 3, 64).on_port(1),
            Event::control_change(511, 0, 4, 64).on_port(1),
            Event::control_change(513, 0, 5, 64).on_port(1),
            Event::control_change(1000, 0, 6, 64).on_port(1),
        ]
    );
}

/// A stereo instrument whose output is NaN during its latency, then silent except for one
/// infinite sample on the right at `infinite_at` and NaN on the left from `nan_from` on, both
/// counted in processed frames.
struct NonFinite {
    latency: u32,
    infinite_at: usize,
    nan_from: usize,
    position: usize,
}

impl Process<f32> for NonFinite {
    type Error = RenderError;

    fn sample_rate(&self) -> f64 {
        RATE
    }

    fn max_block_size(&self) -> usize {
        64
    }

    fn input_channels(&self) -> usize {
        0
    }

    fn output_channels(&self) -> usize {
        2
    }

    fn latency(&self) -> Result<u32, RenderError> {
        Ok(self.latency)
    }

    fn tail(&self) -> Result<Tail, RenderError> {
        Ok(Tail::Samples(0))
    }

    fn process(
        &mut self,
        context: &crate::BlockContext,
        _input: &[&[f32]],
        output: &mut [&mut [f32]],
        _automation: &[crate::AutomationEvent],
        _events: &Events,
        produced: &mut Events,
    ) -> Result<(), RenderError> {
        produced.clear();
        let (left, right) = output.split_at_mut(1);
        for (frame, (left, right)) in left[0].iter_mut().zip(right[0].iter_mut()).enumerate() {
            let at = self.position + frame;
            let latency = at < self.latency as usize;
            *left = if latency || at >= self.nan_from {
                f32::NAN
            } else {
                0.0
            };
            *right = if latency {
                f32::NAN
            } else if at == self.infinite_at {
                f32::INFINITY
            } else {
                0.0
            };
        }
        self.position += context.frames;
        Ok(())
    }
}

#[test]
fn output_after_the_silence_hold_is_not_checked() {
    // Silence from the start ends the tail 5 frames after the input, in the block whose later
    // frames are NaN; those are never output.
    let options = RenderOptions {
        tail: TailPolicy::UntilSilence {
            threshold: 1e-6,
            hold_seconds: 0.0001,
        },
        max_tail_seconds: 1.0,
    };
    let mut processor = NonFinite {
        latency: 0,
        infinite_at: usize::MAX,
        nan_from: 30,
        position: 0,
    };
    let rendered = render::<f32, _>(&mut processor, &[], 10, &Events::new(), &options).unwrap();
    assert_eq!(rendered.status, RenderStatus::Complete);
    assert_eq!(rendered.channels[0], [0.0; 10]);
}

#[test]
fn a_tail_still_sounding_at_the_maximum_ends_at_the_limit() {
    let input = signal(1000);
    let options = RenderOptions {
        tail: TailPolicy::UntilSilence {
            threshold: 1e-6,
            hold_seconds: 0.0005,
        },
        max_tail_seconds: 0.001,
    };
    // The echo sounds for 300 frames after the input, past the 48-frame maximum.
    let rendered = run(&mut Echo::new(0, 300, Tail::Samples(0)), &input, options).unwrap();
    assert_eq!(rendered.tail, 48);
    assert_eq!(rendered.status, RenderStatus::TailLimit);
}

#[test]
fn a_session_of_segments_shorter_than_the_latency_renders_like_one_render() {
    let input = signal(1000);
    let processor = || Echo::new(300, 50, Tail::Samples(100));
    let rendered = run(&mut processor(), &input, reported(1.0)).unwrap();

    let mut source = processor();
    let mut session = RenderSession::new(&mut source).unwrap();
    let mut channels = vec![Vec::new(); 2];
    let mut consume = |delivery: Delivery<'_, f32>| {
        for (channel, samples) in channels.iter_mut().zip(delivery.audio) {
            channel.extend_from_slice(samples);
        }
    };
    let mut output_frames = 0;
    for start in (0..1000).step_by(64) {
        let end = (start + 64).min(1000);
        let segment: Vec<&[f32]> = input.iter().map(|channel| &channel[start..end]).collect();
        let progress = session
            .push(
                RenderInput {
                    audio: &segment,
                    frames: end - start,
                    events: &Events::new(),
                    schedule: RenderSchedule::default(),
                },
                &mut consume,
                || false,
            )
            .unwrap();
        output_frames += progress.output_frames;
    }
    let flushed = session
        .finish(&reported(1.0), &mut consume, || false)
        .unwrap();
    output_frames += flushed.output_frames;
    assert_eq!(flushed.tail, rendered.tail);
    assert_eq!(output_frames, rendered.channels[0].len());
    assert_eq!(channels, rendered.channels);
}

#[test]
fn the_first_non_finite_output_sample_fails_the_render_at_its_frame() {
    let processor = || NonFinite {
        latency: 100,
        infinite_at: 110,
        nan_from: 1100,
        position: 0,
    };
    // The NaN in the removed latency is not output; the infinity 10 frames into the output is,
    // whatever the tail policy.
    for options in [reported(0.01), RenderOptions::default()] {
        let error =
            render::<f32, _>(&mut processor(), &[], 1000, &Events::new(), &options).unwrap_err();
        assert_eq!(error, RenderError::NonFiniteOutput { frame: 10 });
        assert_eq!(error.kind(), FailureKind::Processing);
    }

    // A NaN in the tail fails a render whose tail waits for silence, which it would keep from.
    let in_tail = || NonFinite {
        infinite_at: usize::MAX,
        ..processor()
    };
    let error = render::<f32, _>(
        &mut in_tail(),
        &[],
        1000,
        &Events::new(),
        &RenderOptions::default(),
    )
    .unwrap_err();
    assert_eq!(error, RenderError::NonFiniteOutput { frame: 1000 });

    // A session trims the latency once across segments shorter than it, and counts the frame
    // in the output of the call that reaches it: the third, after 20 more frames of latency.
    let mut source = processor();
    let mut session = RenderSession::new(&mut source).unwrap();
    let segment = RenderInput {
        audio: &[],
        frames: 40,
        events: &Events::new(),
        schedule: RenderSchedule::default(),
    };
    for _ in 0..2 {
        session.push(segment, |_| {}, || false).unwrap();
    }
    assert_eq!(
        session.push(segment, |_| {}, || false).unwrap_err(),
        RenderError::NonFiniteOutput { frame: 10 }
    );
    assert_eq!(session.state(), SessionState::Failed);
}