2 Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Program to load a wave file and loop playing it using SDL audio */
15 /* loopwaves.c is much more robust in handling WAVE files --
16 This is only for simple WAVEs
18 #include "SDL_config.h"
24 #include <emscripten/emscripten.h>
32 Uint8 *sound; /* Pointer to wave data */
33 Uint32 soundlen; /* Length of wave data */
34 int soundpos; /* Current play position */
37 static SDL_AudioDeviceID device;
39 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
51 SDL_CloseAudioDevice(device);
59 /* Initialize fillerup() variables */
60 device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wave.spec, NULL, 0);
62 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
63 SDL_FreeWAV(wave.sound);
68 /* Let the audio run */
69 SDL_PauseAudioDevice(device, SDL_FALSE);
72 static void reopen_audio()
80 fillerup(void *unused, Uint8 * stream, int len)
85 /* Set up the pointers */
86 waveptr = wave.sound + wave.soundpos;
87 waveleft = wave.soundlen - wave.soundpos;
90 while (waveleft <= len) {
91 SDL_memcpy(stream, waveptr, waveleft);
95 waveleft = wave.soundlen;
98 SDL_memcpy(stream, waveptr, len);
104 #ifdef __EMSCRIPTEN__
108 if(done || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING))
109 emscripten_cancel_main_loop();
114 main(int argc, char *argv[])
119 /* Enable standard application logging */
120 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
122 /* Load the SDL library */
123 if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS) < 0) {
124 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
129 SDL_strlcpy(filename, argv[1], sizeof(filename));
131 SDL_strlcpy(filename, "sample.wav", sizeof(filename));
133 /* Load the wave file into memory */
134 if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
135 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
139 wave.spec.callback = fillerup;
141 /* Show the list of available drivers */
142 SDL_Log("Available audio drivers:");
143 for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
144 SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
147 SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
151 SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
153 #ifdef __EMSCRIPTEN__
154 emscripten_set_main_loop(loop, 0, 1);
159 while (SDL_PollEvent(&event) > 0) {
160 if (event.type == SDL_QUIT) {
163 if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
164 (event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
172 /* Clean up on signal */
174 SDL_FreeWAV(wave.sound);
179 /* vi: set ts=4 sw=4 expandtab: */