SDL2 - Play Multiple WAV Sounds Simultaneously(no SDL2_Mixer)?



With using just SDL2 and no SDL2_Mixer, how can we play two or more WAV sounds simultaneously?
The current build of our new project can play only one WAV sound at a time?

Please look at our code on GitHub below and suggest a solution to the above issue, thanks!



You can do it using SDL_MixAudioFormat() but bear in mind that the two WAVs that you mix together need to be in the same format (same sample rate, same number of channels, same number of bits per channel). Whilst you could do a format conversion in real time using SDL_ConvertAudio() personally I would always prefer to do the conversion ‘offline’ and supply matching WAVs with the app.

What becomes more interesting is mixing a second WAV ‘asynchronously’ with a currently-playing WAV (such as adding sound effects on top of music) without using SDL_mixer. For a long time I thought that was impossible, but it can be done with the proviso that there’s a race hazard (I asked a question about this here recently). It also seems not to work properly when using WASAPI in Windows so I force the audio driver to directsound in that case.

It’s amazing what you can do without using SDL_mixer if you try hard enough!


Thanks for the reply…

Sound rather complicated.
Is there a code same or a tutorial somewhere on the web for playing multiple WAVs simultaneously?
(using just vanilla SDL2)



Not really, it’s only using the functions as they are documented. I’d post my code except that it’s in BBC BASIC which I suspect is not the language you would prefer! :wink: