use dasp_graph::{Buffer, Input};
use crate::node::{AudioNode, ProcessContext};
pub struct Mixer {
channels: usize,
}
impl Mixer {
pub fn new(channels: usize) -> Self {
Self { channels }
}
pub fn stereo() -> Self {
Self::new(2)
}
pub fn mono() -> Self {
Self::new(1)
}
}
impl AudioNode for Mixer {
type Message = ();
fn process(
&mut self,
_ctx: &ProcessContext,
_messages: impl Iterator<Item = Self::Message>,
inputs: &[Input],
output: &mut [Buffer],
) {
for buf in output.iter_mut() {
buf.iter_mut().for_each(|s| *s = 0.0);
}
for input in inputs {
let input_channels = input.buffers().len();
for (out_ch, out_buf) in output.iter_mut().enumerate() {
let in_ch = if input_channels == 1 {
0 } else {
out_ch.min(input_channels - 1)
};
let in_buf = &input.buffers()[in_ch];
for (out_sample, in_sample) in out_buf.iter_mut().zip(in_buf.iter()) {
*out_sample += *in_sample;
}
}
}
}
fn num_inputs(&self) -> usize {
usize::MAX
}
fn num_outputs(&self) -> usize {
self.channels
}
}