Skip to content

[experiment] force chunking when using dynamic sources #909

Description

@yara-blue

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)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions