Search code examples
c++openal

How to play a sound instantly after another sound ended?


I cant ensure this by checking if the sound has stopped in my program because the sounds are handled in different thread, isnt that right?

So the problem comes when i want to be 100% sure that once a sound has stopped from playing, another sound would start instantly after it with no possibility for any delay.

The second sound will be looping sound type, so i cant just glue the two sounds together.

How can i do this?


Solution

  • I am not aware of any function in OpenAL that would let you do such a thing direcly, e.g. by blocking or notifying you, so gapless playback in a way as you said would be possible. Any solution like "sleep for the duration of the first sound, then start the second" is bound to fail, because sleep is not reliable. Checking repeatedly whether the source has stopped playing will not work either, because at the time you see that the source is done, there is already an audible gap.

    However, you can get this to work indirectly by using alSourceQueueBuffers once with the first sound, and once with the second (looping) sound.

    Start playing the source.

    Then regularly call alGetSourcei(source, AL_BUFFERS_PROCESSED, &num_done). You don't need to call it all the time, just more often than the duration of your loop sound.

    If this tells you that a buffer is done, unqueue it. That's the first sound done, and the second one is playing right now. Now, quickly queue the buffer holding the second sound again, and keep queueing/unqueueing as long as you want the sound to loop.