[SDL] Suggestion: Looping Audio

Ryan C. Gordon icculus at clutteredmind.org
Mon Jul 29 00:54:01 PDT 2002

> I would like to suggest a looping audio feature in the audio part of
> the library of both libsdl and sdl_sound. Often, a music piece will
> consist of two parts, an intro plus a part which is endlessly
> repeated. It would be great if both the .WAV and MIDI file types could
> be looped such that the intro part plays, then the main part which is
> looped endlessly. The SDL_PauseAudio would then look something like
> void SDL_PauseAudio(int pause_on, int repeat_start);

This wouldn't be feasible inside SDL itself, since it would need to retain
the entire waveform...which can be upwards of ten megabytes of data per
minute of audio (if not more).

SDL_sound can do this, but not directly; when you hit the end of a sample
you are decoding, you can call Sound_Seek() and jump to the loop point and
start decoding again from there.  The library doesn't do this directly,
since it's a trivial amount of code in your app; if you wrap
Sound_Decode() in a function that loops at the end of the sample, you can
even make this transparent to your app; it'll just keep returning an
endless stream of looped data.

If you just want to loop the whole sound, instead of jumping to an
arbitrary point in the middle, Sound_Rewind() is more efficient in many

Alternately, for small samples like a gun shot that you want to repeat
for a machinegun sound, you can call Sound_DecodeAll() and then manage all
the looping with the full waveform in memory, which might be easier, and
is probably more efficient in the long run.


More information about the SDL mailing list