Playing audio fx

please excuse, if this has been descused before, but i couldn’t find an
archive. what is the prefered way to play sound fx?

i have tried to things, both using SDL_CreateThread.

1.) open the audio device at startup and do somehing like

SDL_PauseAudio(0);
// wait for sound to complete
while (len > 0) SDL_Delay(1);
// stop playing
SDL_PauseAudio(1);

in the thread.

2.) open the device before playing and close it afterwards, everything
in that thread described above.

in the first version the sound gets significantly delayed, and the
second versions takes long bbefore the next sound can be played.

i bet i’m doing something wrong, could please anybody point me in the
right direction?

thanx …
clemens

| please excuse, if this has been descused before, but i couldn’t find an
| archive. what is the prefered way to play sound fx?

Have you looked at SDL_Mixer? It’s listed in the Libraries part of the
libsdl.org website.

It can background-play mp3s and suchlike too, which is cool :)On Tue, Jun 25, 2002 at 11:25:15AM +0200, Clemens Kirchgatterer wrote:


I will not say ‘Springfield’ just to get applause

PGP Fingerprint [6AD6 865A BF6E 76BB 1FC2 E4C4 DEEA 7D08 D511 E149]
PGP Public key [www.piku.org.uk/public-key.asc] - Home [www.piku.org.uk]

James wrote:

Have you looked at SDL_Mixer? It’s listed in the Libraries part of the
libsdl.org website.

It can background-play mp3s and suchlike too, which is cool :slight_smile:

yes i know it, but i wanted to avoid yet anouther dependency.

please excuse, if this has been descused before, but i couldn’t find an
archive. what is the prefered way to play sound fx?

The “prefered way” is the way you prefer, based on your needs. :wink:

i have tried to things, both using SDL_CreateThread.

Why…?

1.) open the audio device at startup and do somehing like

SDL_PauseAudio(0);
// wait for sound to complete
while (len > 0) SDL_Delay(1);
// stop playing
SDL_PauseAudio(1);

in the thread.

Don’t use a thread of your own in the first place. (Unless you’re talking directly to the audio API of whatever platform you’re on, that is. Check out Kobo Deluxe for an example of how to do that for OSS, as an alternative to SDL audio. The last few versions also support OSS “polling” debug mode, to run audio in the main thread.)

Anyway, SDL will either create a thread for you, or hook you up via a callback from interrupt context (*) depending on platform.

(*) …which means you should NOT do memory allocation, file I/O
and whatnot in your audio callback. The way FX plugin
instantion is done in Kobo Deluxe is just a quick hack that
happens to work on most platforms. Will be fixed soon.

2.) open the device before playing and close it afterwards, everything
in that thread described above.

Never do this. Sound cards and drivers behave in very different and unpredictable ways WRT open/close timing and general behavior. Some will even click and and do other nasty things…

And of course, this design doesn’t mix well with multichannel mixing. (Pun not intended. :slight_smile:

in the first version the sound gets significantly delayed,

How do you get data from your thread to SDL’s audio callback at all…?

and the
second versions takes long bbefore the next sound can be played.

As expected…

i bet i’m doing something wrong, could please anybody point me in the
right direction?

Well, just hook yourself up to SDL audio with a callback. SDL will ask you to fill in a buffer of samples at regular intervals, and all you have to do in the callback is some “DSP fun” to get the total sound output from your application into that buffer.

Check out my “speaker.tar.gz” hack for a simple example. (It generates sound in real time and applies filtering and “reverb”.)

http://olofson.net/mixed.html

To play a sampled waveform, you basically just copy some data from a waveform buffer instead of calculating it from “nothing”. To mix, just add the output from the “voices” together, and scale down (shift and/or multiply; never divide if you can help it…) to stay within the range of the output sample format. ([-32768, 32767] for 16 bit, [-128, 127] for 8 bit.)

Next, it’s probably a good idea to get rid of the output sample rate dependency. For this, you’ll need to “scale” the waveforms along the time axis - pretty much like you scale images, although along one axis instead of two. Indeed, just like with images, you need some sort of interpolation for nice results; I strongly recommend cubic (4 point) interpolation for anything but the lowest end systems.

(There are a bunch of fixed point “voice resamplers” in ‘a_mixers.h’ of Kobo Deluxe if you’re interested. Note that they’re a lot more complicated than your average sound effect mixer has to be, since it’s a synth meant to be usable for “real” music as well. It has to do per-sample volume ramping, sub-sample accurate looping and other stuff, apart from the actual resampling and mixing.)

All that said, there’s always SDL_mixer… :slight_smile:

..and the Kobo Deluxe audio engine, in case you're adventurous. :-) It's some 22,000 lines, and took me about half a year of "occasional hacking" to code. The most interesting feature is that it plays MIDI files using sounds rendered by a script driven off-line synth. A "CD quality" song takes around 2-5 kB compressed, and music can be authored using your favourite MIDI sequencing software.

Note that we’re not talking about GM/GS here; you can program your own sounds and use your own samples, just like in a professional MIDI studio. It’s just that the synth is Free Software, so you can send it along with your work, instead of making an mp3.
</shameless plug>

//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 Tue, 25/06/2002 11:25:15 , Clemens Kirchgatterer wrote:

You really want to hack a few thousand lines of code just to avoid that? :slight_smile:

Seriously, you can get away with just a few hundred lines for reasonably good SFX playback - but eventually you’ll probably want music and stuff. It’s fun stuff, and there’s lots of cool stuff you can do if you roll your own. However, there’s a lot more work involved than you might think at first. (Then again, isn’t there always? :wink:

//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 Tue, 25/06/2002 14:02:10 , Clemens Kirchgatterer wrote:

James wrote:

Have you looked at SDL_Mixer? It’s listed in the Libraries part of the
libsdl.org website.

It can background-play mp3s and suchlike too, which is cool :slight_smile:

yes i know it, but i wanted to avoid yet anouther dependency.

…and the Kobo Deluxe audio engine, in case you’re
adventurous. :slight_smile: It’s some 22,000 lines, and took me about half a year
of “occasional hacking” to code. The most interesting feature is that
it plays MIDI files using sounds rendered by a script driven off-line
synth. A “CD quality” song takes around 2-5 kB compressed, and music
can be authored using your favourite MIDI sequencing software.

Note that we’re not talking about GM/GS here; you can program your
own sounds and use your own samples, just like in a professional MIDI
studio. It’s just that the synth is Free Software, so you can send it
along with your work, instead of making an mp3.
</shameless plug>

I suspect that Kobo Deluxe also works as an email reader and makes
excellent coffee.

“Open the audio device with a smaller buffer” would have probably sufficed. :slight_smile:

–ryan.

i have tried to things, both using SDL_CreateThread.

The audio callback itself will run in a separate thread, so there may be
no reason to spin one yourself.

in the first version the sound gets significantly delayed, and the
second versions takes long bbefore the next sound can be played.

Your audio buffer is probably too big; this is what’s specified in the
"samples" field of the first SDL_AudioSpec you passed to SDL_OpenAudio().

Try lowering it, but not too low. My previous guidelines went something
like this:

if (desired.freq <= 11025)
desired.samples = 512;
else if (desired.freq <= 22050)
desired.samples = 1024;
else if (desired.freq <= 44100)
desired.samples = 2048;
else
desired.samples = 4096; // (shrug)

Tweak to preference.

–ryan.

You can also take a look at test/loopwave.c in the SDL source archive
for an example of using SDL’s audio API to play sounds. Note that you
should follow Ryan’s guidelines for setting the sample fragment size,
the value used in loopwave.c is more appropriate for music than sfx.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

| > …and the Kobo Deluxe audio engine, in case you’re
| > adventurous. :slight_smile: It’s some 22,000 lines, and took me about half a year
| > of “occasional hacking” to code. The most interesting feature is that
| > it plays MIDI files using sounds rendered by a script driven off-line
| > synth. A “CD quality” song takes around 2-5 kB compressed, and music
| > can be authored using your favourite MIDI sequencing software.
| >
| > Note that we’re not talking about GM/GS here; you can program your
| > own sounds and use your own samples, just like in a professional MIDI
| > studio. It’s just that the synth is Free Software, so you can send it
| > along with your work, instead of making an mp3.
| > </shameless plug>
|
| I suspect that Kobo Deluxe also works as an email reader and makes
| excellent coffee.

The game equivalent of Emacs? :-)On Tue, Jun 25, 2002 at 01:22:36PM -0400, Ryan C. Gordon wrote:


I will not bring sheep to class

PGP Fingerprint [6AD6 865A BF6E 76BB 1FC2 E4C4 DEEA 7D08 D511 E149]
PGP Public key [www.piku.org.uk/public-key.asc] - Home [www.piku.org.uk]

[…Kobo Deluxe - the Complete Collection of Engines…]

I suspect that Kobo Deluxe also works as an email reader and makes
excellent coffee.

Sorry, but not yet. I need the networking layer for the email, and I have yet to find a commonly available API for coffee brewing.

There are plans for 0.5, though. :wink:

“Open the audio device with a smaller buffer” would have probably sufficed. :slight_smile:

If that was the most serious problem. What about this use of extra threads, apart from what SDL audio sets up for you…?

//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 Tue, 25/06/2002 13:22:36 , Ryan C. Gordon wrote:

Another solution is to allow users to configure fragment size/latency manually.

I’ve found that different operating systems have very different lower limits (>60 ms for Win32, <20 ms for Linux), so IMHO, it’s not a great idea to hardcode anything that “seems to work here” - especially not if you release binaries.

//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 Tue, 25/06/2002 10:34:23 , Sam Lantinga wrote:

You can also take a look at test/loopwave.c in the SDL source archive
for an example of using SDL’s audio API to play sounds. Note that you
should follow Ryan’s guidelines for setting the sample fragment size,
the value used in loopwave.c is more appropriate for music than sfx.

[…Kobo Deluxe…]

|
| I suspect that Kobo Deluxe also works as an email reader and makes
| excellent coffee.

The game equivalent of Emacs? :slight_smile:

Damn! Emacs… I need to hack more and faster; got some catching up to do. :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 Tue, 25/06/2002 18:41:07 , James wrote:

On Tue, Jun 25, 2002 at 01:22:36PM -0400, Ryan C. Gordon wrote:

I suspect that Kobo Deluxe also works as an email reader and makes
excellent coffee.

Sorry, but not yet. I need the networking layer for the email, and I have
yet to find a commonly available API for coffee brewing.

I would suggest the Hyper Text Coffee Pot Control Protocol, or HTCPCP.
It is used by coffee.el, the emacs coffee controller.

There are plans for 0.5, though. :wink:

Great to hear.
Finally, there is really useful open source software ;)On Tuesday 25 June 2002 20:05, David Olofson wrote:

On Tue, 25/06/2002 13:22:36 , Ryan C. Gordon wrote:


Johannes Schmidt

< http://libufo.sourceforge.net > Your widget set for OpenGL

Clemens Kirchgatterer wrote:

in the first version the sound gets significantly delayed, and the
second versions takes long bbefore the next sound can be played.

i bet i’m doing something wrong, could please anybody point me in the
right direction?

thank you all (esp. Jmaes, Ryan, David) very much for the many
suggestions. i’m going to try them out and will come back to this with a
status report later on.

thanx …
clemens