firewheel_core/node.rs
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
use std::error::Error;
use crate::SilenceMask;
pub trait AudioNode<C>: 'static {
fn info(&self) -> AudioNodeInfo;
/// Activate the audio node for processing.
fn activate(
&mut self,
sample_rate: u32,
max_block_frames: usize,
num_inputs: usize,
num_outputs: usize,
) -> Result<Box<dyn AudioNodeProcessor<C>>, Box<dyn Error>>;
/// Called when the processor counterpart has been deactivated
/// and dropped.
///
/// If the audio graph counterpart has gracefully shut down, then
/// the processor counterpart is returned.
#[allow(unused)]
fn deactivate(&mut self, processor: Option<Box<dyn AudioNodeProcessor<C>>>) {}
}
pub trait AudioNodeProcessor<C>: 'static + Send {
/// Process the given block of audio. Only process data in the
/// buffers up to `frames`.
///
/// Note, all output buffers *MUST* be filled with data up to
/// `frames`.
///
/// If any output buffers contain all zeros up to `frames` (silent),
/// then mark that buffer as silent in [`ProcInfo::out_silence_mask`].
fn process(
&mut self,
frames: usize,
proc_info: ProcInfo<C>,
inputs: &[&[f32]],
outputs: &mut [&mut [f32]],
);
}
/// Additional information about an [`AudioNode`]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AudioNodeInfo {
/// The minimum number of input buffers this node supports
pub num_min_supported_inputs: u32,
/// The maximum number of input buffers this node supports
///
/// This value must be less than `64`.
pub num_max_supported_inputs: u32,
/// The minimum number of output buffers this node supports
pub num_min_supported_outputs: u32,
/// The maximum number of output buffers this node supports
///
/// This value must be less than `64`.
pub num_max_supported_outputs: u32,
}
/// Additional information for processing audio
pub struct ProcInfo<'a, C> {
/// An optional optimization hint on which input channels contain
/// all zeros (silence). The first bit (`0x1`) is the first channel,
/// the second bit is the second channel, and so on.
pub in_silence_mask: SilenceMask,
/// An optional optimization hint to notify the host which output
/// channels contain all zeros (silence). The first bit (`0x1`) is
/// the first channel, the second bit is the second channel, and so
/// on.
///
/// By default no channels are flagged as silent.
pub out_silence_mask: &'a mut SilenceMask,
/// A global user-defined context.
pub cx: &'a mut C,
}