I'm currently using the following formula: SUM(channels)-(PRODUCT(channels)/USHRT_MAX)
to mix all my sound channels (which are already prepared to the same samplerate and bitdepth(float/(un)signed char/(un)signed short => short).
However, when I'm mixing, although I still hear all contributing channels, I also get some noise when there's none generated by the different channels (without mixing them using the above method, just using one channel at the time, I get a clear sound without noise).
Anyone knows what's going wrong when mixing the channels?
I'm not sure why you're subtracting PRODUCT(channels)/USHRT_MAX so that might be part of it. The other thing is that I seem to recall that any time you do processing on a digital signal you introduce some aliasing or noise and have to run it through a low-pass filter to get rid of it.
I removed the functionality, just simply SUM(channels) and finally clipping arround SHORT_MAX:
1 2 3 4 5 6 7
/* Add the channel to the mixer */
result_l += sample_l; //Mix the channels equally together based on volume!
result_r += sample_r; //See above!
/* Clip the channels */
result_l = (result_l>SHRT_MAX)?SHRT_MAX:((result_l<SHRT_MIN)?SHRT_MIN:result_l);
result_r = (result_r>SHRT_MAX)?SHRT_MAX:((result_r<SHRT_MIN)?SHRT_MIN:result_r);
It seems to work: I hear both channels without any noise.