mikejsavage.co.uk / blog

RSS feed

05 Jan 2019 / Immediate mode audio

In games you typically have a fire and forget API, where you start a sound and it plays to completion. Maybe it returns some handle so you can stop it later on.

PlayingSound ps = PlaySound( "path/to/sound" );
StopSound( ps );

Most of the time you want to play sounds to completion so this is nice and convenient, but sometimes you have sounds attached to objects and you want the sound to stop when the object is destroyed. Things like rocket thrusters and voice lines.

For those it’s nicer to have an immediate mode API, where you have to keep calling the play function or it stops. For example:

void RocketThink() {
        if( collided with something ) {
                // destroys the rocket, so RocketThink doesn't get called anymore,
                // so PlaySoundImmediate doesn't get called anymore, so the sound stops playing

        PlaySoundImmediate( "path/to/sound" );

It’s trivial to implement and there’s no way you can forget to stop a sound.