[SDL] Sound and Music

Marian Schedenig msched at msched.no-ip.org
Mon Sep 12 18:12:43 PDT 2005


My game (which has, in various versions, been in development for years) is 
finally nearing completeness, and I've gone through a couple of different 
audio systems in the process. I've stumbled on a couple of issues though, 
which I'd very much like to clear up before finalising the system.

1) SDL_mixer/OpenAL/anything else?

For the first version, I used SDL_mixer. Later, I switched to OpenAL, partly 
because I had performance problems with SDL_mixer and stereo panning (I 
suppose I did something wrong) but mainly because for a different project I 
needed surround sound and wanted to reuse my resource manager. I'm not overly 
happy with OpenAL though. The Linux version isn't up to date, and having 
sound effects in surround sound isn't that exciting when the music comes out 
only in mono...

From what I know, the Linux implementation of SDL supports surround sound 
through ALSA now, but I believe this isn't true for other target (e.g. 
Windows) and not planned for the forseeable future? Are there any interesting 
alternatives I should look into? I know about fmod, but don't fancy their 
license (and never managed to get the thing running on my Debian box anyway).

Of course, I'd also like to keep things as portable as possible. Currently, 
I'm using OpenGL for rendering, so OpenAL is fine in that regard, but I'm 
trying to keep the possibility of adding an SDL gfx backend to run on other 
platforms, and I assume I'd pretty much be out of luck expecting them to 
support OpenAL.

2) Streaming music

For music playback, I use ogg files which I stream from the harddisk. The 
problem is that the HD access results in a noticeable performance impact on 
at least some systems; every few frames, a frame gets delayed a little longer 
because the next bit of the stream is read. I tried to compensate for this by 
putting the streaming code in a separate thread, hoping that the I/O blocking 
mechanism wouldn't slow down my gfx routines that way, but the effect is 
still noticeable, and of course I now have to fight race conditions as well.

Is there any real gain in putting this in its own thread to begin with? Is 
there a recommended buffer size for streaming audio which works reasonably 
well for most platforms? Or should I simply cache the entire ogg files and 
just decode them on the fly?

3) Continuous updates

This has perhaps caused me the most trouble. Refreshing the screen is one 
thing, because it (hopefully) runs at the display's refresh rate and the 
human eye doesn't notice the difference. But how should I go about updating 
sound volumes and panning for moving sources? Is it enough to update them 
once per frame (all I know is that the ears are "quicker" than the eyes), or 
would these steps be audible? This also applies to fading music (currently 
done in the music thread, see #2).

When these questions are answered, I'm afraid I'll probably come up with a few 
more. ;)

Thanks in advance,

Hofstadter's law: "It always takes longer than you think, even when you take
account of Hofstadter's law".

More information about the SDL mailing list