Before we add sound effects to our main game, I will show you how to make an audio player in the audio.c file to demonstrate how SDL Audio can be used to play sound effects in a WebAssembly application. This application will take five sound effects that we will use in our game and allow the user to press number keys one to five to play all of the chosen sound effects. I will first show you the code broken into two sections, and then I will walk you through what everything does. Here is all of the code in audio.c with the exception of the main function:
#include <SDL2/SDL.h>
#include <emscripten.h>
#include <stdio.h>
#include <stdbool.h>
#define ENEMY_LASER "/audio/enemy-laser.wav"
#define PLAYER_LASER "/audio/player-laser.wav"
#define LARGE_EXPLOSION "/audio/large-explosion.wav"
#define SMALL_EXPLOSION...