2 Copyright (C) 1997-2016 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"
28 #include <emscripten/emscripten.h>
36 Uint8 *sound; /* Pointer to wave data */
37 Uint32 soundlen; /* Length of wave data */
38 int soundpos; /* Current play position */
42 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
52 fillerup(void *unused, Uint8 * stream, int len)
57 /* Set up the pointers */
58 waveptr = wave.sound + wave.soundpos;
59 waveleft = wave.soundlen - wave.soundpos;
62 while (waveleft <= len) {
63 SDL_memcpy(stream, waveptr, waveleft);
67 waveleft = wave.soundlen;
70 SDL_memcpy(stream, waveptr, len);
85 if(done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING))
86 emscripten_cancel_main_loop();
91 main(int argc, char *argv[])
96 /* Enable standard application logging */
97 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
99 /* Load the SDL library */
100 if (SDL_Init(SDL_INIT_AUDIO) < 0) {
101 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
106 SDL_strlcpy(filename, argv[1], sizeof(filename));
108 SDL_strlcpy(filename, "sample.wav", sizeof(filename));
110 /* Load the wave file into memory */
111 if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
112 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
116 wave.spec.callback = fillerup;
118 /* Set the signals */
120 signal(SIGHUP, poked);
122 signal(SIGINT, poked);
124 signal(SIGQUIT, poked);
126 signal(SIGTERM, poked);
127 #endif /* HAVE_SIGNAL_H */
129 /* Show the list of available drivers */
130 SDL_Log("Available audio drivers:");
131 for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
132 SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
135 /* Initialize fillerup() variables */
136 if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
137 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
138 SDL_FreeWAV(wave.sound);
142 SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
144 /* Let the audio run */
147 #ifdef __EMSCRIPTEN__
148 emscripten_set_main_loop(loop, 0, 1);
150 while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
154 /* Clean up on signal */
156 SDL_FreeWAV(wave.sound);
161 /* vi: set ts=4 sw=4 expandtab: */