SDL audio routines

Hi. I’m at a point in my game where I want to put in sound effects now. I really don’t have any experience with this low level audio stuff SDL supports, though. What I need is either a tutorial or good example code to learn the theory with, because right now I’m just guessing at assumptions, and that’s never a very good idea. Can anyone point me to something useful? I’m not having much luck searching around on my own. I should point out that I’d like to try and just use what SDL supports natively, rather than adding in another library (such as SDL_mixer), unless someone can give me really good reasons to avoid this route.

Thanks.

-Jason

Hi. I’m at a point in my game where I want to put in sound effects now. I really don’t have any experience with this low level audio stuff SDL supports, though. What I need is either a tutorial or good example code to learn the theory with, because right now I’m just guessing at assumptions, and that’s never a very good idea. Can anyone point me to something useful? I’m not having much luck searching around on my own. I should point out that I’d like to try and just use what SDL supports natively, rather than adding in another library (such as SDL_mixer), unless someone can give me really good reasons to avoid this route.

Well, you need some basic DSP knowledge to hack a good sound engine, but of course, it gives you total control of you sound. Playing samples using SDL’s audio API isn’t particularly complicated - although it is rather complicated to get high quality sound, especially if you want to play samples back at different pitches than 1:1 with the output sample rate.

For a really simple example of playing real time generated sound effects, check out my “PC speaker emulator” at

http://olofson.net/mixed

(You could also look at the ditched Comprez entry - although that’s everything but designed for real time synthesis. It’s all float32, and uses oscillators with bilinear interpolation across banks of bandlimited wavetables.)

For more advanced and “usable” examples, you could have a look at the various generations of audio engines in Kobo Deluxe:

http://olofson.net/download

But be warned - the audio engine is turning into a full-blown synth/sampler with it’s own scripting language for sound generation, as you approach the latest 0.4-pre versions! :slight_smile:

//David

.---------------------------------------
| David Olofson
| Programmer

david.olofson at reologica.se
Address:
REOLOGICA Instruments AB
Scheelev?gen 30
223 63 LUND
Sweden
---------------------------------------
Phone: 046-12 77 60
Fax: 046-12 50 57
Mobil:
E-mail: david.olofson at reologica.se
WWW: http://www.reologica.se

`-----> We Make Rheology RealOn Fri, 8/03/2002 15:39:27 , Jason Hoffoss wrote:

Well, you need some basic DSP knowledge to hack a good sound engine, but
of course, it gives you total control of you sound. Playing samples using
SDL’s audio API isn’t particularly complicated - although it is rather
complicated to get high quality sound, especially if you want to play
samples back at different pitches than 1:1 with the output sample rate.

For a really simple example of playing real time generated sound effects,
check out my “PC speaker emulator” at

http://olofson.net/mixed

(You could also look at the ditched Comprez entry - although that’s
everything but designed for real time synthesis. It’s all float32, and uses
oscillators with bilinear interpolation across banks of bandlimited
wavetables.)

For more advanced and “usable” examples, you could have a look at the
various generations of audio engines in Kobo Deluxe:

http://olofson.net/download

But be warned - the audio engine is turning into a full-blown
synth/sampler with it’s own scripting language for sound generation, as you
approach the latest 0.4-pre versions! :slight_smile:

Thanks, I’ll check these out. I’ve been looking around more, inspecting the
source to SDL_Sound and SDL_Mixer, and one of the demos, which all use the
API, and I learned a few things, such as you should only probably open the
audio once, rather than before you want to play each sound. I wasn’t really
clear on how to tell the system your sound is done playing, etc, and then I
realized under this system you are never really done technically. When
there’s no more sound to play you just play silence. A lot more still to
learn I’m sure, but already it’s contradicted a lot of my assumptions.

Now I’m noticing that the playback function passed a pointer to Uint8, so I
guess samples you play need to be in Uint8 format? This doesn’t seem right
to me, so I guess now I’m assuming this Uint8 pointer isn’t really pointing
to samples, but just a byte stream.

This is starting to look like more work than I really want considering my
simple needs at this time. I’m going to take a look at SDL_Mixer too. All
I really need is a way to say “ok, play this wave file now” when various
events occur, such as the player firing a shot. I’ve already created all
the wave files a long time ago. I’ll try something fancier in a future
project.

A tutorial on this sure would be useful! :slight_smile: Btw, anyone know if there’s any
mirrors for “Roger’s SDL Tutorials”? Looks like exactly what I need, but
the kekkai.org site seems to be permanently down.

-Jason

----- Original Message -----
From: david.olofson@reologica.se (David Olofson)
To:
Sent: Saturday, March 09, 2002 12:33 PM
Subject: Re: [SDL] SDL audio routines

Thanks, I’ll check these out. I’ve been looking around more, inspecting the
source to SDL_Sound and SDL_Mixer, and one of the demos, which all use the
API, and I learned a few things, such as you should only probably open the
audio once, rather than before you want to play each sound. I wasn’t really
clear on how to tell the system your sound is done playing, etc, and then I
realized under this system you are never really done technically. When
there’s no more sound to play you just play silence. A lot more still to
learn I’m sure, but already it’s contradicted a lot of my assumptions.

The playsound application that comes with SDL_sound actually DOES open the
audio device for each new sound file, since SDL’s audio conversion
routines are a bit flakey, we try to get the audio device opened in the
actual format that we’re sending it data. This also means less CPU
processing, since we don’t have to convert sound formats. This is NOT a
good idea for a game, since it would result in clicking/popping (and the
shutting down of any other sounds that would be playing at the time we
restart the audio device, and the improper playback of sounds in
different formats, blahblahblah).

For something that isn’t meant for generic playback of a serialized list
of sound files, you’ll want to either convert on the fly or, better yet,
have all your sounds in a standard format, and do you best to get the
audio device opened in that format, too.

SDL_mixer covers that nastiness for you.

Now I’m noticing that the playback function passed a pointer to Uint8, so I
guess samples you play need to be in Uint8 format? This doesn’t seem right
to me, so I guess now I’m assuming this Uint8 pointer isn’t really pointing
to samples, but just a byte stream.

Yeah, it’s just so it’s easier to do pointer arithmatic in the audio
callback. The data you put to the stream should be in the format you and
SDL agreed upon when you opened the audio device.

This is starting to look like more work than I really want considering my
simple needs at this time. I’m going to take a look at SDL_Mixer too. All
I really need is a way to say “ok, play this wave file now” when various
events occur, such as the player firing a shot. I’ve already created all
the wave files a long time ago. I’ll try something fancier in a future
project.

SDL_mixer will fit your bill nicely, I think.

–ryan.

[…]

This is starting to look like more work than I really want considering
my simple needs at this time. I’m going to take a look at SDL_Mixer
too. All I really need is a way to say "ok, play this wave file now"
when various events occur, such as the player firing a shot. I’ve
already created all the wave files a long time ago. I’ll try something
fancier in a future project.

Yeah, hacking an audio engine isn’t something you do just because you
need one, unless you’re particularly fond of DSP coding. There are
various issues that make even the most trivial “engine” quite a bit less
trivial than you’d expect. (Thread safety issues, not doing any “real
time hostile” operations in the audio thread, supporting all possible
output sample rates, avoiding clipping/wrapping without getting a
ridiculously low master output level, etc, etc…)

Besides, if you’ll need something more sophisticated than SDL_Mixer some
time, maybe my engine (still no real name) will be ready for prime time
by then - unless you feel like joining the race for The Greatest
Multimedia Audio Engine. :wink:

//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.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Saturday 09 March 2002 20:34, Jason Hoffoss wrote:

Why don’t you guys use BASS, or FMOD or something similar for Linux? I can not imagine that no one has ever thought of something like that? If
anyone knows of a good API for Linux to mix wavs, mods, mp3 and/or midis, I think that would be interesting. Perhaps one of the API’s I mentioned
are already multi-platform?>On Saturday 09 March 2002 20:34, Jason Hoffoss wrote:

[…]

This is starting to look like more work than I really want considering
my simple needs at this time. I’m going to take a look at SDL_Mixer
too. All I really need is a way to say "ok, play this wave file now"
when various events occur, such as the player firing a shot. I’ve
already created all the wave files a long time ago. I’ll try something
fancier in a future project.

Besides, if you’ll need something more sophisticated than SDL_Mixer some
time, maybe my engine (still no real name) will be ready for prime time
by then - unless you feel like joining the race for The Greatest
Multimedia Audio Engine. :wink:

No, I’ve not enough to worry about without that too! I guess I’ll probably
just stay clear of doing audio myself and use SDL_Mixer from now on, after
everything you’ve said. I was thinking of maybe trying procedural sounds in
the future, but maybe SDL_Mixer is still the way to go with that too. I
don’t even have any idea what a procedural sound would be like, so maybe
it’s just a crazy idea that isn’t even practical. Just thought I’d
experiment and see what happens, which is where I always seem to start with
new game ideas. :slight_smile:

I’m using SDL_Mixer now, btw, and it’s working pretty well. The only
confusion I ran into is what ‘channels’ are. I think there’s 2 different
definitions for it. You open the audio device with either 1 or 2 channels
(mono or stereo I assume), but then when you play a chunk, you have to
specify a channel, which seems to be something completely different. So I’m
assuming it’s more like just a slot (oscillator pair, whatever). I looked
at the alien example program, and it opens 2 channels, but uses 3 channels
to play waves. Anyway, whenever I need to play a sound, I just play it
with -1 as the channel so it allocates one itself, and I haven’t had
problems yet. It could be playing a ton of sounds at once in theory, and
I’m not sure if that’s a bad thing or not. Anyone have advice on practical
limits one should set?

-Jason

At 01:43 12.03.2002 +0100, you wrote:

Why don’t you guys use BASS, or FMOD or something similar for Linux? I can
not imagine that no one has ever thought of something like that? If
anyone knows of a good API for Linux to mix wavs, mods, mp3 and/or midis,
I think that would be interesting. Perhaps one of the API’s I mentioned
are already multi-platform?
Yes, stuff like bass really rocks! I was surprised, when the music was
playing with bass after 4 function calls, and I never had to think about it
again. Also for mp3’s it’s really good. I found out, to seek a certain
position in milisecs, just give 20*ms as parameter and it starts playing at
about that very millisecond …
ok, off topic somehow …
St0fF.

[…]

This is starting to look like more work than I really want considering
my simple needs at this time. I’m going to take a look at SDL_Mixer
too. All I really need is a way to say "ok, play this wave file now"
when various events occur, such as the player firing a shot. I’ve
already created all the wave files a long time ago. I’ll try something
fancier in a future project.

Why don’t you guys use BASS, or FMOD or something similar for Linux? I can
not imagine that no one has ever thought of something like that? If
anyone knows of a good API for Linux to mix wavs, mods, mp3 and/or midis,
I think that would be interesting. Perhaps one of the API’s I mentioned
are already multi-platform?

Well, the reason I didn’t use BASS was because I never heard of it until you
mentioned it just now. :slight_smile: And not FMOD because that’s for playing MOD
files, right? As I said, I only need to play *.wav files for sound effects.
I have no background music. So there you go.

SDL_Mixer can do all the mixing you mentioned above I believe, and is cross
platform.

-Jason

----- Original Message -----
From: rheenen@home.nl (Martijn Melenhorst)
To:
Sent: Monday, March 11, 2002 7:43 PM
Subject: Re: [SDL] SDL audio routines

On Saturday 09 March 2002 20:34, Jason Hoffoss wrote:

[…]

now on, after everything you’ve said. I was thinking of maybe trying
procedural sounds in the future, but maybe SDL_Mixer is still the way
to go with that too.

Could probably be done, but I’m not sure it’s the optimal solution. (You
need to stay in sync with playback, unless you’re just going to prepare
one-shot sounds right before playing them.)

I don’t even have any idea what a procedural
sound would be like, so maybe it’s just a crazy idea that isn’t even
practical.

Real time synth? Actually, my engine doesn’t do much of that currently,
and the focus will probably stay on controlling the playback of
prerendered waveforms. Completely real time generated sounds tend to be
very CPU intensive if you want interesting results of reasonably
quality.

[…]

I’m using SDL_Mixer now, btw, and it’s working pretty well. The only
confusion I ran into is what ‘channels’ are. I think there’s 2
different definitions for it. You open the audio device with either 1
or 2 channels (mono or stereo I assume), but then when you play a
chunk, you have to specify a channel, which seems to be something
completely different.

Here’s another, to add to the confusion: My channels are just like MIDI
channels - they represent “contexts” from which you can fire off and
control one or more voices. (This is how normal synths handle chords.)

So I’m assuming it’s more like just a slot
(oscillator pair, whatever).

Yes, I think so. (Don’t know much about SDL_Mixer, actually…)

I looked at the alien example program,
and it opens 2 channels, but uses 3 channels to play waves. Anyway,
whenever I need to play a sound, I just play it with -1 as the channel
so it allocates one itself, and I haven’t had problems yet.

In Kobo Deluxe (which uses my engine) I just play sound effects on one
channel, music on another etc. While played and controlled through one
channel, MIDI’s allocate up to 16 “private” channels that are used for
the actual playing; one for each MIDI channel used. (I’ll support "ports"
as well, eventually, for really phatt arangements.)

Each channel can be configured WRT signal routing, insert effects (reverb
and the like) etc, so this makes it very easy to separate the “master
controls” for music, SFX etc.

Oh, and there are “groups” as well, allowing some aspects of a group of
voices to be controlled from a single place. Confused yet? :wink:

It could
be playing a ton of sounds at once in theory, and I’m not sure if
that’s a bad thing or not.

Well, Kobo Deluxe currently has a limit at 32 voices - but the main
problem with the in-game sound effects is that they were originally
programmed to have one monophonic voice each. That is, if you played
"EXPLO1" repeatedly, it would actually just restart, rather than grabbing
a now voice, as is the case now.

In short, if you use dynamic voice/channel allocation, you have to
consider the effect of sound effects hanging around. The good ol’ random
burst of exposions will quickly driver the output into clipping! (Or as
on my engine, force the output limiter to compress the signal, which
causes “pumping” if it’s done too heavily.)

Anyone have advice on practical limits one
should set?

Either keey you sound effects short, or don’t fire off lots of them all
the time. Or; one way or another, may sure you have some kind of control
over how many sound effects are playing at once.

(Refer to the latest Kobo Deluxe snapshots to hear how it should not be
done! I need to tweak all sound FX control code to make it sound right
again. :slight_smile:

//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.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Tuesday 12 March 2002 01:56, Jason Hoffoss wrote:

At 20:24 11.03.2002 -0500, you wrote:

Well, the reason I didn’t use BASS was because I never heard of it until you
mentioned it just now. :slight_smile: And not FMOD because that’s for playing MOD
files, right? As I said, I only need to play *.wav files for sound effects.
I have no background music. So there you go.

SDL_Mixer can do all the mixing you mentioned above I believe, and is cross
platform.
right, that’s the “bass”-problem … it’s windoof-only!
St0fF

PLEEEEEEASE not fmod. That library is such a pain in the ass. It follows almost none of the usual Linux packaging/filesystem conventions, and it is binary-only shareware.

One SDL Game Contest entry did much worse than it should have because it used fmod and I had quite a headache trying to get the library installed.

Sorry for the rant. I wish the FireLight guys the best, but let’s kill this monster. May I suggest MikMod and/or OpenAL? Between those two, all of fmod’s functionality is covered, plus source, minus annoying license.

-JohnOn Tue, Mar 12, 2002 at 01:43:39AM +0100, Martijn Melenhorst wrote:

On Saturday 09 March 2002 20:34, Jason Hoffoss wrote:
[…]

This is starting to look like more work than I really want considering
my simple needs at this time. I’m going to take a look at SDL_Mixer
too. All I really need is a way to say "ok, play this wave file now"
when various events occur, such as the player firing a shot. I’ve
already created all the wave files a long time ago. I’ll try something
fancier in a future project.

Why don’t you guys use BASS, or FMOD or something similar for Linux? I can not imagine that no one has ever thought of something like that? If
anyone knows of a good API for Linux to mix wavs, mods, mp3 and/or midis, I think that would be interesting. Perhaps one of the API’s I mentioned
are already multi-platform?


John R. Hall - KG4RUO - Stranded in the Sol System
Student, Georgia Tech; Author, Programming Linux Games

Well I can’t use fmod because my code is GPL. The GPL forbids use of a
lib like FMOD. This issue cased many long flamewars over KDE and Qt. And
FMOD has the same problem - the author of FMOD says in his license he’s
granting you permission to use it with GPL’d code.

The author of the GPL’d code, OTOH…On Tue, Mar 12, 2002 at 01:43:39AM +0100, Martijn Melenhorst wrote:

This is starting to look like more work than I really want considering
my simple needs at this time. I’m going to take a look at SDL_Mixer
too. All I really need is a way to say "ok, play this wave file now"
when various events occur, such as the player firing a shot. I’ve
already created all the wave files a long time ago. I’ll try something
fancier in a future project.

Why don’t you guys use BASS, or FMOD or something similar for Linux? I can not imagine that no one has ever thought of something like that? If
anyone knows of a good API for Linux to mix wavs, mods, mp3 and/or midis, I think that would be interesting. Perhaps one of the API’s I mentioned
are already multi-platform?


Joseph Carter Not many fishes

but, then I used an Atari, I was more likely to win the lottery in
ten countries simultaneously than get accelerated X

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 273 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020311/da76c9e2/attachment.pgp

Sorry for the rant. I wish the FireLight guys the best, but let’s kill this monster. May I suggest MikMod and/or OpenAL? Between those two, all of
fmod’s functionality is covered, plus source, minus annoying license.

Okay, no FMOD then, I only had crashes with it on Windows anyway :slight_smile:

OpenAL is a great API, by the way, but is it already working 100% for Windows, does anyone know that? If so, then you’re done. Sound for games
really isn’t complicated at all, it’s just how dificult you want to make it for yourself. I would say that creating a pool of channels, or perhaps OpenAL
already does this, would solve the allocation problem. Then, let a mod player or something take care of playing music: Just jump to a different
pattern in the .mod to indicate another ‘mood’ in the game (That’s DirectMusic in a nutshell :slight_smile: ) Really, I haven’t spent more than 3 hours
implementing sound and music in each of my games, just use what’s freely available already if you want to start now.

to play waves. Anyway, whenever I need to play a sound, I just play it
with -1 as the channel so it allocates one itself, and I haven’t had
problems yet. It could be playing a ton of sounds at once in theory, and
I’m not sure if that’s a bad thing or not. Anyone have advice on practical
limits one should set?

FAKK2 mixed a ton of sounds at once (32 and more samples at some
points…all those twittering birds add up!). This was with OpenAL, which
uses its own mixer, but add a 3D engine, distance attenuation, and the
fact the MP3s were decoded on-the-fly before mixing…the short answer is,
“you can mix a lot”.

Realistically, most games don’t need to mix more than 8 samples at once.
Frequently, even that number is high.

–ryan.

However, you have to consider the effect of “stealing” voices when
starting new effects… Clicking is a very, very Bad Thing! :wink:

Seriously, distance attenuation and a sufficiently smart voice priority
system might be nice. That way, you’d be able to limit the number of
voices so that only the loudest ones are played, rather than just not
considering distant sound sources.

//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.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Tuesday 12 March 2002 20:32, Ryan C. Gordon wrote:

to play waves. Anyway, whenever I need to play a sound, I just play
it with -1 as the channel so it allocates one itself, and I haven’t
had problems yet. It could be playing a ton of sounds at once in
theory, and I’m not sure if that’s a bad thing or not. Anyone have
advice on practical limits one should set?

FAKK2 mixed a ton of sounds at once (32 and more samples at some
points…all those twittering birds add up!). This was with OpenAL,
which uses its own mixer, but add a 3D engine, distance attenuation,
and the fact the MP3s were decoded on-the-fly before mixing…the short
answer is, “you can mix a lot”.

Realistically, most games don’t need to mix more than 8 samples at
once. Frequently, even that number is high.