I’m in the process of porting some sound code from DirectSound to SDL -
it’s basically a simple mixer system to let the game play up to 16
samples at once.
I’ve got it all working, but I’m getting distortion that the original
DSound version didn’t have. I’m guessing that my output is getting
oversaturated and samples are being clipped.
I’ve tried reducing the volume I pass into SDL_MixAudio to half (64),
but I still get distortion from time to time.
Does anyone know of a good strategy to reduce this problem?
Would the SDL_mixer library do a better job than vanilla SDL_MixAudio()?
Any ideas appreciated!
(BTW, for those interested, it’s for a Linux port of SpaceTripper - see www.pompom.org.uk)
If I write my own mixing function, how would you suggest I avoid the
distortion I’m getting?
mix all channels into an accumulating buffer with more bits per sample
(thus no clipping needed). Then copy this buffer to the output buffer,
clipping samples as you go
of course you need to avoid saturating your channels in the first place;
this typically occurs when you have the same sample played in several
channels simultaneously starting exactly simultaneously. don’t do that
I’m in the process of porting some sound code from DirectSound to SDL -
it’s basically a simple mixer system to let the game play up to 16
samples at once.
I’ve got it all working, but I’m getting distortion that the original
DSound version didn’t have. I’m guessing that my output is getting
oversaturated and samples are being clipped.
I’ve tried reducing the volume I pass into SDL_MixAudio to half (64),
but I still get distortion from time to time.
Does anyone know of a good strategy to reduce this problem?
Would the SDL_mixer library do a better job than vanilla SDL_MixAudio()?
Any ideas appreciated!
(BTW, for those interested, it’s for a Linux port of SpaceTripper - see www.pompom.org.uk)
Mix all audio into a buffer with 32 bits/sample, to make sure you don’t get
any overflows/wrapping. Then just transfer the samples to the output buffer,
clipping (saturating) as you go. Try to minimize clipping by mixing the sound
fx and music at reasonable levels.
If it’s hard to figure out how many relatively loud effects that can be
playing at once, and/or you wan’t better audio quality, you could use a
compressor algorithm instead of clipping. Basically, a compressor is just an
envelope generator controlled by the level of the input signal. Try something
like this, for starters:
NOTE: You should not use floating point maths for integer audio processing
in real code, for performance reasons. For example, the “gain” variable
should be a fixed point value instead.
A more advanced and better sounding compressor would either delay the signal
or look at the Ds/Dt of the signal, to be able to see into the “future”, and
thus slide the gain down smoothly before a high peak, rather than just
kicking it down in “zero” time.
//David Olofson — Programmer, Reologica Instruments AB
.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia | ----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Tuesday 29 May 2001 17:40, Ben Campbell wrote:
If I write my own mixing function, how would you suggest I avoid the
distortion I’m getting?