I suspect one thing that can really slow the mixer down is that it takes Box<dyn Source>. Since the next() calls cannot be inlined here this probably slows everything down. We can make this less of an issue by getting chunks (up to some size) in the mixer. Roughly:
impl source -> Sample
chunked(impl source) -> [Sample] // returns array
Box<dyn chunked> -> [Sample] // return array
Mixer() -> Sample (uses the chunks internally)
Now the only way to get a nice API with this is to hide the whole chunking inside the mixer. Like this:
struct Mixer {...}
impl Mixer {
fn add(&mut self, s: impl Source) {
let chunked = Chunked::from(s);
let chunked = Box::new(chunked) as Box<dyn Chunk>;
self.sources.push(chunked)
}
...
}
impl Iterator for Mixer {
fn next(&mut self) -> Option<Sample> {
if self.cached_chunks.is_empty() {
for chunked in self.sources {
self.cached_chunks.push(chunked.next_chunk())?;
}
}
let mut sample = 0.0;
for chunk in &mut self.cached_chunks {
sample += chunk.pop_front();
}
Some(sample)
}
}
I suspect one thing that can really slow the mixer down is that it takes
Box<dyn Source>. Since the next() calls cannot be inlined here this probably slows everything down. We can make this less of an issue by getting chunks (up to some size) in the mixer. Roughly:Now the only way to get a nice API with this is to hide the whole chunking inside the mixer. Like this: