[sdlsound] Minimal Example playing a MOD File using SDL Sound

Ryan C. Gordon icculus at clutteredmind.org
Sat Oct 1 12:06:55 EDT 2005

> The playsound example that comes with SDL sound is good, but a little bit
> complex.

You aren't the first to say that...since this comes up a lot, I've
trimmed the crap out of playsound.c, so all it does is play back a list
of audio files, like this:

  ./playsound_simple myfile.mp3 myfile2.mod myfile.ogg [...etc...]

Total file size, with a LOT of comments is now 190 or so lines of code.
In terms of actual relevant code, it's probably less than 50 lines.

This is in subversion for both 1.0.x and 1.1.x, but here's the file


The general strategy looks like this:

The main app:
1. Set up SDL and SDL_Sound.
2. Start audio callback, and wait for it to notify you that it has
finished playing back the file with a global variable (or whatever).
3. Clean up and exit.

The audio callback:
1. See if there's data already decoded, if not, decode more.
2. If there's still no data, notify the main app, write silence, return.
3. Write available decoded data to device, up to max it is asking for.
4. If the device wants more than you wrote this iteration, go to #1
until it's full or we've run out in #2.
5. Update state for next callback, return.

...let me know if you have any questions.


More information about the sdlsound mailing list