View Full Version : looping only portion of wav with openal (using sound forge)

29-06-2008, 01:09 PM
Usually specific sound effects in games aren't just play once and forget, you have special kind of effects, let's say off the top of my head you have a star trek like phaser weapon on your spaceship, a phaser blast starts with the charging-up effect, then it does the discharge effect which loops for as long as you keep firing, and then the release effect or alternatively imagine a handbrake tire sliding effect which would work the same, it starts with specific asphalt-friction-tearing sound, then as long as you slide the specific region of the sound effect would loop and it would end with the stopping sound.

Now this looks pretty complex since for openal you need to create 3 buffers when loading your wav file, and you need a program to set the looping region, but there's a nice solution out there, sound forge (i'm using ancient old v4.5) is able to save the looping region markers into wav files (for which you'll need a pretty nice RIFF parser) or into separate .sfl files (which are still riff files by structure, but of constant size and so they can be read in a much simplier way)

So all you need to do is read the .sfl file, and grab the only two relevant pieces of data at constant offsets (which are only as constant as long as you use only marker region ):

f.position:= 28;
f.Read(Result.rgnstart, sizeof(Result.rgnstart));
f.position:= 72;
f.Read(Result.rgnlen, sizeof(Result.rgnlen));

edit: watch out for these numbers you get are number of samples, so multiply it with bytes per sample to get number of bytes of waveform data!

The two values are of longword type, and specify the region start and region length (in bytes).

Now, when loading the wav file you need to create 3 buffers, 1 for each part of the wav file, because openal doesn't support loping only a part of source's buffer.

To playback the effect, this is all there's left to do:
Queue first "intro" buffer, and the looping buffer, and then check the AL_BUFFERS_PROCESSED if the first buffer has been completed to change the AL_LOOPING to 1 (checking this every few frames shouldn't cause any timing issues) and then queue the "outro" buffer. then whenever you feel you'd like to stop the looping, change the source back to non-looping and the looping effect will end with outro buffer following and then the source will go to AL_STOPPED.

If anybody has any documents on what the following chunks in wav riff files do, i can write a proper .sfl file parser to support proper loading of regions.