[gameprogrammer] Re: mixing sounds

On Monday 04 August 2008, Alan Wolfe wrote:
> Hey,
> 
> I was wondering does anyone know how to properly mix sound data?
> 
> I'm working with some raw wav data and i had heard that just adding
> sound wavs together mixes them, but what happens in the case of
> overflow? (is that clipping?)

That's the correct way of doing it, but indeed, bad things happen if 
the result overflows. However, it's actually much, much worse than 
clipping. It's wrapping! :-) (Unless you take special measures, 
you're just throwing high bits away, so there won't be any "proper" 
clipping.)


> It seems like when i average the wav data that it sounds like it
> mixes ok so not sure what is the best way :P

Averaging is pretty close to what you do, from a practical POV. 
However, the "right" way of doing it is to decide on a 0 dB reference 
level somewhere below that of the output, so you have some headroom 
to avoid clipping.

Theoretically, you can scale first (you'll probably need per-voice 
volume control anyway, so you can just scale the volume levels and 
avoid scaling every sample twice) and then mix. If you deal in 
floating point, you can actually do that - but with integers, you 
need to use fixed point (as in, reserving some fraction bits for 
correct rounding), and/or use langer integers, so you can do the 
scaling *after* the mixing.

Either way, as long as you only need a few (say 4) simultaneous 
voices, you can get away by just scaling things so clipping cannot 
occur - but if you try that with 16+ voices, you'll end up with a 
very low output level.

To get reasonably loud output (from a typical consumer sound system, 
at least) and a sensible dynamic range (for your average user), you 
need to do some dynamic processing, one way or another.

You can find a very simple approach in Kobo Deluxe, where I use 32 bit 
mixing buffers (actually 8:24 fixed point, IIRC), and end the chain 
with a simple compressor/limiter, before converting to the output 
format.

For better results, you should probably divide the frequency range 
into three or more bands, and process each band separately, as a 
single band compressor tends to generate that typical pumping 
compressor sound if you push it too hard. (That can be a useful 
effect to make explosions and stuff more "in your face", but 
normally, you want compression to sound as transparent as possible.)


//David Olofson - Programmer, Composer, Open Source Advocate

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'

---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: