Hi all,
I'm trying to mix multiple sample buffers (16bit) together into a single output. I do this by using pretty much the following code..
The audio does mix ok, however at points when there are a lot of channels being mixed it produces very noticeable 'blips and clicks' in the sound.Code:#define BUFFER_LEN 256 #include "limits.h" // stuff here...... short buffer[BUFFER_LEN]; short output[BUFFER_LEN]; long temp; while (!done) { getsamples(buffer); for(int i=0; i<BUFFER_LEN; i++) { temp = long(output[i] += buffer[i]*0.5); if (temp > SHRT_MAX) temp=SHRT_MAX; else if (temp < SHRT_MIN) temp=SHRT_MIN; output[i] = temp; } // stuff .... }
The "long temp" variable was an attempt to overcome this by preventing overflow of the 16 bit short's, but this didn't really do anything.
The only other method I know of is dividing by the number of channels, which I don't really want to do since the number of channels at any one time could quite easily go above 10 (depending on the input), and dividing would loose a lot of volume and therefor accuracy.
Has anyone got a better suggestion?
Thanks.