Sound FX

The current state of sound on the web is a bit of a mess and has been for quite a while. For a long time, there were issues with loading MP3 versus OGG files based on which browser you were using. Recently, there have been issues with browsers blocking sounds that play automatically to prevent annoying audio spam. This feature in Chrome sometimes seems to create problems when playing audio in our game. I have noticed that, if Chrome does not initially play the audio, it will usually play if you reload the page. I have not had this problem on Firefox.

You will need to include several images and audio files in your build to make this project work. Make sure that you include the /Chapter12/sprites/ folder as well as the /Chapter12/audio/ folder from the project's GitHub. If you haven't yet downloaded the GitHub project, you can get it online at https://github.com/PacktPublishing/Hands-On-Game-Development-with-WebAssembly.

Emscripten’s support for audio playback is not as good as I would like it to be. On the message boards, Emscripten's defenders are quick to blame the state of audio on the web as opposed to Emscripten itself, and there is some truth to that assessment. Emscripten's FAQ claims that Emscripten supports the use of SDL1 Audio, SDL2 Audio, and OpenAL, but, in my experience, I have found that using a very limited set of SDL2 Audio provides the best outcomes. I am going to keep our use of SDL2 Audio to a minimum, using the audio queue instead of mixing sound effects. You may wish to expand on or modify what I have done here. OpenAL, in theory, should work with Emscripten, although I have not had much luck with it. Also, you may wish to look into SDL_MixAudio (https://wiki.libsdl.org/SDL_MixAudio) and SDL_AudioStream (https://wiki.libsdl.org/Tutorials/AudioStream) to improve the audio system in your game, but be aware that performance and support for streaming and mixing audio on the web may not be ready for prime time.

We will cover the following topics in this chapter: