firewheel-nodes 0.13.0

Official factory nodes for the Firewheel audio engine
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
use bevy_platform::sync::{Arc, Mutex, MutexGuard};
use core::num::{NonZeroU32, NonZeroUsize};
use firewheel_core::node::NodeError;
use firewheel_core::{
    StreamInfo,
    channel_config::{ChannelConfig, ChannelCount, NonZeroChannelCount},
    diff::{Diff, EventQueue, Patch, PatchError, PathBuilder},
    dsp::buffer::SequentialBuffer,
    event::{ParamData, ProcEvents},
    node::{
        AudioNode, AudioNodeInfo, AudioNodeProcessor, ConstructProcessorContext, ProcBuffers,
        ProcExtra, ProcInfo, ProcStreamCtx, ProcessStatus,
    },
};

#[cfg(not(feature = "std"))]
use num_traits::Float;

/// The configuration of a [`TripleBufferNode`]
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy", derive(bevy_ecs::prelude::Component))]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TripleBufferConfig {
    /// The number of channels
    pub channels: NonZeroChannelCount,
    /// The maximum window size that can be used
    pub max_window_size: WindowSize,
}

impl Default for TripleBufferConfig {
    fn default() -> Self {
        Self {
            channels: NonZeroChannelCount::STEREO,
            max_window_size: WindowSize::default(),
        }
    }
}

/// The window size for a [`TripleBufferNode`]
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy", derive(bevy_ecs::prelude::Component))]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum WindowSize {
    /// Use the capacity in units of samples (of a single channel
    /// of audio)
    Samples(u32),
    /// Use the capacity in units of seconds
    Seconds(f64),
}

impl WindowSize {
    pub fn as_frames(&self, sample_rate: NonZeroU32) -> u32 {
        match self {
            Self::Samples(samples) => *samples,
            Self::Seconds(seconds) => (seconds * (sample_rate.get() as f64)).round() as u32,
        }
    }
}

impl Default for WindowSize {
    fn default() -> Self {
        Self::Samples(2048)
    }
}

impl Diff for WindowSize {
    fn diff<E: EventQueue>(&self, baseline: &Self, path: PathBuilder, event_queue: &mut E) {
        if self != baseline {
            match self {
                WindowSize::Samples(samples) => event_queue.push_param(*samples, path),
                WindowSize::Seconds(seconds) => event_queue.push_param(*seconds, path),
            }
        }
    }
}

impl Patch for WindowSize {
    type Patch = Self;

    fn patch(data: &ParamData, _: &[u32]) -> Result<Self::Patch, PatchError> {
        match data {
            ParamData::U32(samples) => Ok(Self::Samples(*samples)),
            ParamData::F64(seconds) => Ok(Self::Seconds(*seconds)),
            _ => Err(PatchError::InvalidData),
        }
    }

    fn apply(&mut self, value: Self::Patch) {
        *self = value;
    }
}

/// A node that sends raw audio data from the audio graph to another
/// thread. Useful for cases where you only care about the latest data
/// in the buffer, such as for creating visualizers.
#[derive(Default, Diff, Patch, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy", derive(bevy_ecs::prelude::Component))]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TripleBufferNode {
    /// The window size (the number of frames in each channel in the output buffer)
    pub window_size: WindowSize,
}

#[derive(Clone)]
pub struct TripleBufferState {
    num_channels: NonZeroChannelCount,
    active_state: Arc<Mutex<Option<ActiveState>>>,
}

impl TripleBufferState {
    /// The number of channels in this buffer.
    pub fn num_channels(&self) -> NonZeroChannelCount {
        self.num_channels
    }

    /// Get the latest audio data in the triple buffer.
    pub fn output<'a>(&'a mut self) -> OutputDataGuard<'a> {
        OutputDataGuard {
            guarded_state: self.active_state.lock().unwrap(),
        }
    }
}

struct ActiveState {
    consumer: triple_buffer::Output<TripleBufferData>,
    sample_rate: NonZeroU32,
}

pub struct OutputData<'a> {
    /// The samples of data.
    ///
    /// Note, the length of this buffer may be longer than the actual number of
    /// frames that are currently written to. Only read up to [`OutputData::frames`]
    /// from this buffer.
    pub buffer: &'a SequentialBuffer<f32>,

    /// The number of frames of usable data that are in [`OutputData::buffer`].
    pub frames: usize,

    /// A value equal to how many times the buffer has been updated since the node
    /// was first created. This can be used to quickly check if the buffer differs
    /// from the previous read.
    pub generation: u64,
}

pub struct OutputDataGuard<'a> {
    guarded_state: MutexGuard<'a, Option<ActiveState>>,
}

impl<'a> OutputDataGuard<'a> {
    /// Returns `true` if the node is currently active.
    pub fn is_active(&self) -> bool {
        self.guarded_state.is_some()
    }

    /// The sample rate of the audio data.
    ///
    /// If the node is not currently active, then this will return `None`.
    pub fn sample_rate(&self) -> Option<NonZeroU32> {
        self.guarded_state.as_ref().map(|s| s.sample_rate)
    }

    /// Get the latest audio data.
    ///
    /// If the node is not currently active, then this will return `None`.
    pub fn data<'b>(&'b mut self) -> Option<OutputData<'b>> {
        self.guarded_state.as_mut().map(|s| {
            let c = s.consumer.read();
            OutputData {
                buffer: &c.buffer,
                frames: c.frames,
                generation: c.generation,
            }
        })
    }

    /// Peek the data that is currently in the buffer without checking if
    /// there is new data.
    ///
    /// If the node is not currently active, then this will return `None`.
    pub fn peek_data<'b>(&'b self) -> Option<OutputData<'b>> {
        self.guarded_state.as_ref().map(|s| {
            let c = s.consumer.output_buffer();
            OutputData {
                buffer: &c.buffer,
                frames: c.frames,
                generation: c.generation,
            }
        })
    }
}

impl AudioNode for TripleBufferNode {
    type Configuration = TripleBufferConfig;

    fn info(&self, config: &Self::Configuration) -> Result<AudioNodeInfo, NodeError> {
        Ok(AudioNodeInfo::new()
            .debug_name("triple_buffer")
            .channel_config(ChannelConfig {
                num_inputs: config.channels.get(),
                num_outputs: ChannelCount::ZERO,
            })
            .custom_state(TripleBufferState {
                num_channels: config.channels,
                active_state: Arc::new(Mutex::new(None)),
            }))
    }

    fn construct_processor(
        &self,
        config: &Self::Configuration,
        mut cx: ConstructProcessorContext,
    ) -> Result<impl AudioNodeProcessor, NodeError> {
        let sample_rate = cx.stream_info.sample_rate;
        let max_window_size_frames = config.max_window_size.as_frames(sample_rate) as usize;

        let (producer, consumer) =
            triple_buffer::triple_buffer::<TripleBufferData>(&TripleBufferData::new(
                NonZeroUsize::new(config.channels.get().get() as usize).unwrap(),
                max_window_size_frames,
                0,
            ));

        let state = cx.custom_state_mut::<TripleBufferState>().unwrap();

        *state.active_state.lock().unwrap() = Some(ActiveState {
            consumer,
            sample_rate,
        });
        let active_state = Arc::clone(&state.active_state);

        let window_size_frames =
            (self.window_size.as_frames(sample_rate) as usize).min(max_window_size_frames);

        Ok(Processor {
            producer: Some(producer),
            config: *config,
            max_window_size_frames,
            params: *self,
            window_size_frames,
            tmp_ring_buffer: SequentialBuffer::new(
                NonZeroUsize::new(config.channels.get().get() as usize).unwrap(),
                max_window_size_frames,
            ),
            ring_buf_ptr: 0,
            active_state,
            generation: 0,
            prev_publish_was_silent: true,
            num_silent_frames_in_tmp: window_size_frames,
            tmp_buffer_needs_cleared: false,
            num_inputs: config.channels.get().get() as usize,
            did_resize: false,
        })
    }
}

struct Processor {
    producer: Option<triple_buffer::Input<TripleBufferData>>,
    config: TripleBufferConfig,
    max_window_size_frames: usize,

    params: TripleBufferNode,
    window_size_frames: usize,

    tmp_ring_buffer: SequentialBuffer<f32>,
    ring_buf_ptr: usize,

    // The processor only uses this when a new stream has started.
    active_state: Arc<Mutex<Option<ActiveState>>>,
    generation: u64,

    prev_publish_was_silent: bool,
    num_silent_frames_in_tmp: usize,
    tmp_buffer_needs_cleared: bool,
    num_inputs: usize,
    did_resize: bool,
}

impl AudioNodeProcessor for Processor {
    fn events(&mut self, info: &ProcInfo, events: &mut ProcEvents, _extra: &mut ProcExtra) {
        let mut new_window_size_frames = self.window_size_frames;
        for patch in events.drain_patches::<TripleBufferNode>() {
            match patch {
                TripleBufferNodePatch::WindowSize(window_size) => {
                    new_window_size_frames = (window_size.as_frames(info.sample_rate) as usize)
                        .min(self.max_window_size_frames);
                }
            }

            self.params.apply(patch);
        }

        let producer = self.producer.as_mut().unwrap();

        if self.window_size_frames != new_window_size_frames {
            let prev = self.window_size_frames;

            // Use the data in the triple buffer as a temporary scratch buffer.
            let data = producer.input_buffer_mut();

            for (buf_ch, tmp_ch) in data
                .buffer
                .iter_channels_mut()
                .zip(self.tmp_ring_buffer.iter_channels_mut())
            {
                let (head, tail) = tmp_ch[..prev].split_at(self.ring_buf_ptr);
                buf_ch[..tail.len()].copy_from_slice(tail);
                if tail.len() < prev {
                    buf_ch[tail.len()..prev].copy_from_slice(head);
                }

                // Rebuild tmp_ch at the new window size.
                if prev >= new_window_size_frames {
                    tmp_ch[..new_window_size_frames]
                        .copy_from_slice(&buf_ch[prev - new_window_size_frames..prev]);
                } else {
                    let pad = new_window_size_frames - prev;
                    tmp_ch[..pad].fill(0.0);
                    tmp_ch[pad..new_window_size_frames].copy_from_slice(&buf_ch[..prev]);
                }
            }

            self.window_size_frames = new_window_size_frames;
            self.ring_buf_ptr = 0;
            self.num_silent_frames_in_tmp = 0;
            self.did_resize = true;
        }
    }

    fn bypassed(&mut self, bypassed: bool) {
        let Some(producer) = self.producer.as_mut() else {
            return;
        };

        if bypassed {
            {
                let data = producer.input_buffer_mut();

                for buf_ch in data.buffer.iter_channels_mut() {
                    buf_ch[..self.window_size_frames].fill(0.0);
                }

                self.generation += 1;
                data.generation = self.generation;
                data.frames = self.window_size_frames;
            }

            producer.publish();

            for tmp_ch in self.tmp_ring_buffer.iter_channels_mut() {
                tmp_ch[..self.window_size_frames].fill(0.0);
            }

            self.ring_buf_ptr = 0;
            self.prev_publish_was_silent = true;
            self.num_silent_frames_in_tmp = self.window_size_frames;
            self.tmp_buffer_needs_cleared = false;
        }
    }

    fn process(
        &mut self,
        info: &ProcInfo,
        buffers: ProcBuffers,
        _extra: &mut ProcExtra,
    ) -> ProcessStatus {
        let input_is_silent = info.in_silence_mask.all_channels_silent(self.num_inputs);
        if input_is_silent {
            self.num_silent_frames_in_tmp =
                (self.num_silent_frames_in_tmp + info.frames).min(self.window_size_frames);
        } else {
            self.num_silent_frames_in_tmp = 0;
        }

        if self.num_silent_frames_in_tmp == self.window_size_frames
            && self.prev_publish_was_silent
            && !self.did_resize
        {
            // The previous publish already contained silence, so no need to publish again.
            self.tmp_buffer_needs_cleared = true;
            return ProcessStatus::ClearAllOutputs;
        }
        self.did_resize = false;

        if info.frames >= self.window_size_frames {
            // Just copy all the new data.
            for (tmp_ch, in_ch) in self
                .tmp_ring_buffer
                .iter_channels_mut()
                .zip(buffers.inputs.iter())
            {
                tmp_ch[..self.window_size_frames]
                    .copy_from_slice(&in_ch[info.frames - self.window_size_frames..info.frames]);
            }
            self.ring_buf_ptr = 0;
            self.tmp_buffer_needs_cleared = false;
        } else {
            if self.tmp_buffer_needs_cleared {
                self.tmp_buffer_needs_cleared = false;

                for tmp_ch in self.tmp_ring_buffer.iter_channels_mut() {
                    tmp_ch[..self.window_size_frames].fill(0.0);
                }
                self.ring_buf_ptr = 0;

                self.num_silent_frames_in_tmp = self.window_size_frames;
            }

            let first_copy_frames = info.frames.min(self.window_size_frames - self.ring_buf_ptr);
            let second_copy_frames = info.frames - first_copy_frames;

            for (tmp_ch, in_ch) in self
                .tmp_ring_buffer
                .iter_channels_mut()
                .zip(buffers.inputs.iter())
            {
                if first_copy_frames > 0 {
                    tmp_ch[self.ring_buf_ptr..self.ring_buf_ptr + first_copy_frames]
                        .copy_from_slice(&in_ch[..first_copy_frames]);
                }

                if second_copy_frames > 0 {
                    tmp_ch[..second_copy_frames]
                        .copy_from_slice(&in_ch[first_copy_frames..info.frames]);
                }
            }

            self.ring_buf_ptr = if second_copy_frames > 0 {
                second_copy_frames
            } else {
                self.ring_buf_ptr + first_copy_frames
            };
        }

        let producer = self.producer.as_mut().unwrap();

        {
            let buffer = producer.input_buffer_mut();

            for (buf_ch, tmp_ch) in buffer
                .buffer
                .iter_channels_mut()
                .zip(self.tmp_ring_buffer.iter_channels())
            {
                let (head, tail) = tmp_ch[..self.window_size_frames].split_at(self.ring_buf_ptr);
                buf_ch[..tail.len()].copy_from_slice(tail);
                buf_ch[tail.len()..self.window_size_frames].copy_from_slice(head);
            }

            self.generation += 1;
            buffer.generation = self.generation;
            buffer.frames = self.window_size_frames;
        }

        producer.publish();

        self.prev_publish_was_silent = self.num_silent_frames_in_tmp == self.window_size_frames;

        ProcessStatus::ClearAllOutputs
    }

    fn stream_stopped(&mut self, _context: &mut ProcStreamCtx) {
        *self.active_state.lock().unwrap() = None;
        self.producer = None;
    }

    fn new_stream(&mut self, stream_info: &StreamInfo, _context: &mut ProcStreamCtx) {
        self.max_window_size_frames = self
            .config
            .max_window_size
            .as_frames(stream_info.sample_rate) as usize;

        self.window_size_frames = (self.params.window_size.as_frames(stream_info.sample_rate)
            as usize)
            .min(self.max_window_size_frames);

        self.tmp_ring_buffer = SequentialBuffer::new(
            NonZeroUsize::new(self.config.channels.get().get() as usize).unwrap(),
            self.max_window_size_frames,
        );

        self.ring_buf_ptr = 0;
        self.num_silent_frames_in_tmp = self.window_size_frames;
        self.tmp_buffer_needs_cleared = false;
        self.prev_publish_was_silent = true;

        self.generation += 1;

        let (producer, consumer) =
            triple_buffer::triple_buffer::<TripleBufferData>(&TripleBufferData::new(
                NonZeroUsize::new(self.config.channels.get().get() as usize).unwrap(),
                self.max_window_size_frames,
                self.generation,
            ));

        *self.active_state.lock().unwrap() = Some(ActiveState {
            consumer,
            sample_rate: stream_info.sample_rate,
        });

        self.producer = Some(producer);
    }
}

// A wrapper to ensure that the triple buffer uses `reserve_exact` when cloning
// the initial buffers.
struct TripleBufferData {
    buffer: SequentialBuffer<f32>,
    max_frames: usize,
    frames: usize,
    generation: u64,
}

impl TripleBufferData {
    fn new(num_channels: NonZeroUsize, max_frames: usize, generation: u64) -> Self {
        Self {
            buffer: SequentialBuffer::new(num_channels, max_frames),
            max_frames,
            frames: 0,
            generation,
        }
    }
}

impl Clone for TripleBufferData {
    fn clone(&self) -> Self {
        Self::new(self.buffer.num_channels(), self.max_frames, self.generation)
    }
}