Sound Mixing - distortion problems

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)

Ben.–
Ben Campbell
Programmer, Creature Labs
ben.campbell at creaturelabs.com
http://www.creaturelabs.com

Try it … and you will love it… SDL_mixer solved my cracklin
sound-output…

Greets
PeZ

Would the SDL_mixer library do a better job than vanilla SDL_MixAudio()?

anything would — SDL_MixAudio is a pretty broken thing which we’re stuck
with since the API is frozen. just don’t use it

Tim ‘PeZ’ Theede wrote:

Try it … and you will love it… SDL_mixer solved my cracklin
sound-output…

I’ve used SDL_mixer on other projects (Creatures 3) and it seemed fine.

Does it actually do mixing any differently to SDL_MixAudio()?
Maybe I just need to try it and see…

Ben.–
Ben Campbell
Programmer, Creature Labs
ben.campbell at creaturelabs.com
http://www.creaturelabs.com

Mattias Engdeg?rd wrote:

Would the SDL_mixer library do a better job than vanilla SDL_MixAudio()?

anything would — SDL_MixAudio is a pretty broken thing which we’re stuck
with since the API is frozen. just don’t use it

Hmmm… but doesn’t SDL_mixer use SDL_MixAudio() to do it’s dirty work
anyway?

If I write my own mixing function, how would you suggest I avoid the
distortion I’m getting?

Ben.–
Ben Campbell
Programmer, Creature Labs
ben.campbell at creaturelabs.com
http://www.creaturelabs.com

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

Try increasing the buffers, It adds latency but it gets rid of crackles and
clipping(aka Distortion.)

I use 4096 buffers on 44.1Khz 16bit Stereo> ----- Original Message -----

From: owner-sdl@lokigames.com [mailto:owner-sdl at lokigames.com]On Behalf
Of Ben Campbell
Sent: May 29, 2001 2:41 AM
To: sdl at lokigames.com
Subject: [SDL] Sound Mixing - distortion problems

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)

Ben.

Ben Campbell
Programmer, Creature Labs
ben.campbell at creaturelabs.com
http://www.creaturelabs.com


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

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:

#define DECAY_RATE 50

Sint32	peak = 0;
for(each sample in buffer)
{
	Sint32 s = labs(input_sample);

	if(s > peak)
		peak = s;
	else
	{
		peak -= DECAY_RATE;
		if(peak < 32768)
			peak = 32768;
	}

	float gain = 32768.0 / (float)peak;

	output_sample = (Sint16)(input_sample * gain);
}

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?

Try increasing the buffers, It adds latency but it gets rid of crackles and
clipping(aka Distortion.)

increasing the buffer size has no effects on clipping