[linux-audio-dev] [newbie] mixing audio streams

Alex Marandon al at alpage.org
Sun Mar 14 10:54:53 UTC 2004


On Sat, Mar 13, 2004 at 06:49:25PM +0100, Jan Weil wrote:
> [snip clipping problem]
> > 	    /* -- mixage, cf. http://www.vttoth.com/digimix.htm -- */
> > 	    for ( i = 0; i <= BUFFER_SIZE; i++ ) {
> > 		mixed_buffer[i] 
> > 		    = buffer1[i] + buffer2[i] - buffer1[i] * buffer2[i] / DYNAMIC_RANGE;
> > 	    }
> 
> buffer1 is declared as a short (i. e. usually 16 bits).
> I'd guess this introduces your clipping problems.
> Try this instead:
> 
> mixed_buffer[i] = buffer1[i] + buffer2[i] - 
> 		(long) buffer1[i] * buffer2[i] / DYNAMIC_RANGE;

Thanks for your answer. It was actually a mistake, but the alogrithm
itself is also intrinsically wrong because when input values are high,
it produces an output which is over the range of signed short integers.
So when it's converted to integers it makes number of the opposite sign.

I whish I did more maths when I was in school :o)

I've made some tries with simply dividing the sum of samples by the
number of sample (ie: buffer1[i] + buffer2[i] / 2) and the result is not
so bad.



More information about the Linux-audio-dev mailing list