use crate::types::AudioUnit;
pub struct MixerNode {
gain: f32,
pub clipping: bool,
}
impl Default for MixerNode {
fn default() -> Self {
Self::new()
}
}
impl MixerNode {
pub fn new() -> Self {
Self {
gain: 1.0,
clipping: true,
}
}
pub fn with_gain(gain: f32) -> Self {
Self {
gain,
clipping: true,
}
}
pub fn set_gain(&mut self, gain: f32) {
self.gain = gain;
}
#[inline(always)]
pub fn process(&mut self, input: Option<&AudioUnit>, output: &mut AudioUnit) {
if let Some(in_unit) = input {
output.copy_from_slice(in_unit);
if self.clipping {
dasp::slice::map_in_place(&mut output[..], |frame| {
[
(frame[0] * self.gain).clamp(-1.0, 1.0),
(frame[1] * self.gain).clamp(-1.0, 1.0),
]
});
} else {
dasp::slice::map_in_place(&mut output[..], |frame| {
[frame[0] * self.gain, frame[1] * self.gain]
});
}
} else {
dasp::slice::equilibrium(&mut output[..]);
}
}
}