Any way to read from n AudioFormatReaders into 1 buffer?


#1

Hi everyone,

Is there a way to read samples from multiple sources (& mix) into a single buffer?

I am building a sampler wich loads layered samples (think of the regular SamplerSound, but with N layers of actual sample files). It would be awesome if I can keep my version of SamplerSound with just one buffer, not a buffer per layer.

In other words, is there something like AudioFormatReader::readAndMix (AudioSampleBuffer* buffer,...) or an easy way to make it?

Thanks in advance for any responses.

 

Cheers.


#2

Read into a temp buffer. Add it to the destination.


#3

Thanks for the response, @jules. I'm working with a huge lib of samples, which required me to stream the samples. So these temp buffers will be created constantly during playing for every next "refill" of the playing SamplerSounds, not just at initialization of the instrument.


#4

Huh? You can just keep a single temp buffer and re-use it, you know!


#5

Streaming is multithreaded. I can keep one per SamplerSound, but that will double the memory usage, constantly.

I could keep one per voice, I suppose... This should work good enough - I'll just have 256 temp buffers instead of 1760 (the number of multi-layer SamplerSounds).

Is the instantiation of AudioSampleBuffer a costly operation? If it isn't, I prefer to just create them on the fly and not constantly take up that space.


#6

It sounds like you're doing something very wrong, or misunderstanding something. Obviously you can't expect to read and add data directly into a single buffer from multiple threads, as that'd cause horrible race conditions. So you need 1 re-usable temp buffer per thread. Or have a thread-safe pool of re-usable buffers. But no, don't allocate them on the fly in your audio processing code!


#7

1 reusable buffer per thread sounds like what I am searching for.


#8

But if you create a temporary buffer on the stack that refers to the channel data of your actual buffer, there would be no allocation and no need for a temp buffer.

This won't of course avoid race conditions but solves the problem of multiple files per buffer.

I am using this technique for multichannel samples myself.