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.
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.
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…
..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: