View Full Version : sound mixing

15-01-2004, 12:11 PM
Could anybody give me any help mixing simultaneously multiple audio streams into one. I've tried several ways doing it, calculating average value of sample works ((stream1[sample] + stream2[sample]) / 2), but produces too quiet sound if some of the streams is silent or not too loud... If I do the mixing by using addition (stream1[sample] + stream[sample]) the resulting sample often exceeds the range and must be clipped... and if I mix something like 128 sounds sound is totally garbage...

Does anyone know any better methods? Using multiple DirectSound buffers and playing them simultaneusly works perfectly without any of these problems but I'd like to mix sounds myself without utililizing DirectSound.

15-01-2004, 03:47 PM
We are working on the same problem in the openmod project. I haven't written any test code yet but some of the research I have done should help. Start with this page http://oxygen.it.net.au/mixing/ all of the code is written in assembly but he does do a fairly good job in explaining how to mix two wav streams togther.


15-01-2004, 04:46 PM
Thanks wilbur! That tutorial seems to be exactly what I was looking for :P

and btw, I just love assembly coding so that isn't problem

15-01-2004, 05:36 PM
That tutorial told to calculate volume multiplier in order to prevent clipping by diving 256 (max 8 bit unsigned sample) by number voices and then multiplying samples with that. That's practically same thing as calculating average value of samples which I was doing (except that my way is way slower)... so it wasn't really help.

I wonder if I should implement some sort of "dynamic compressor" which analyzes sum of samples ahead and reduces max volume of sample when it notices that clipping _would_ occur. But that seems very slow and I don't think that is needed in normal mixing anyway...