Best Method For Summing Variable Number of Input Channels


#1

Hi, I am working on a reverb plugin which sums all the input channels before processing them. I am wondering what the the most efficient method is for summing a variable number of input channels.

The two ways I could think of are having a separate buffer array the size of the input which is used for summing (I need to keep the original input buffer samples intact for wet dry mix) or to do a for loop which sums the buffers sample by sample. Both of these seem a little inefficient.

Can anyone recommend a solution?

Thanks!


#2

A good read so that you understand why: http://gameprogrammingpatterns.com/data-locality.html
Also: https://en.wikipedia.org/wiki/Automatic_vectorization


#3

Just to clarify, since this is what I am taking away from links you sent, are you suggesting I use vector instructions to do the array summing?

Doesn’t really answer the problem of having to create a separate array to store the summed samples. Sample by sample summing with vectors seems like it would get weird as well. Perhaps there is no way around these two limitations however.


#4

Vectorisation = SIMD = single instruction multiple data.

If you add sample by sample it is one instruction per sample per channel.
Instead if you use FloatVectorOperations or AudioBuffer.addFrom / copyFrom, you have a single instruction processing several samples, so the CPU is optimised for that (basically all recent processors have that extension). This is the most basic hardware acceleration you can have.


#5

Okay, that makes sense, so would the best practice in this case be to create an AudioBuffer to hold the summed input and then sum the input channels to that array using AudioBuffer.addFrom?