1 /*
2 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
3
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.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11 */
12
13 /* Program to load a wave file and loop playing it using SDL sound queueing */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #ifdef __EMSCRIPTEN__
19 #include <emscripten/emscripten.h>
20 #endif
21
22 #include "SDL.h"
23
24 #if HAVE_SIGNAL_H
25 #include <signal.h>
26 #endif
27
28 struct
29 {
30 SDL_AudioSpec spec;
31 Uint8 *sound; /* Pointer to wave data */
32 Uint32 soundlen; /* Length of wave data */
33 int soundpos; /* Current play position */
34 } wave;
35
36
37 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
38 static void
quit(int rc)39 quit(int rc)
40 {
41 SDL_Quit();
42 exit(rc);
43 }
44
45 static int done = 0;
46 void
poked(int sig)47 poked(int sig)
48 {
49 done = 1;
50 }
51
52 void
loop()53 loop()
54 {
55 #ifdef __EMSCRIPTEN__
56 if (done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)) {
57 emscripten_cancel_main_loop();
58 }
59 else
60 #endif
61 {
62 /* The device from SDL_OpenAudio() is always device #1. */
63 const Uint32 queued = SDL_GetQueuedAudioSize(1);
64 SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
65 if (queued <= 8192) { /* time to requeue the whole thing? */
66 if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
67 SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
68 } else {
69 SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
70 }
71 }
72 }
73 }
74
75 int
main(int argc,char * argv[])76 main(int argc, char *argv[])
77 {
78 char filename[4096];
79
80 /* Enable standard application logging */
81 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
82
83 /* Load the SDL library */
84 if (SDL_Init(SDL_INIT_AUDIO) < 0) {
85 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
86 return (1);
87 }
88
89 if (argc > 1) {
90 SDL_strlcpy(filename, argv[1], sizeof(filename));
91 } else {
92 SDL_strlcpy(filename, "sample.wav", sizeof(filename));
93 }
94 /* Load the wave file into memory */
95 if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
96 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
97 quit(1);
98 }
99
100 wave.spec.callback = NULL; /* we'll push audio. */
101
102 #if HAVE_SIGNAL_H
103 /* Set the signals */
104 #ifdef SIGHUP
105 signal(SIGHUP, poked);
106 #endif
107 signal(SIGINT, poked);
108 #ifdef SIGQUIT
109 signal(SIGQUIT, poked);
110 #endif
111 signal(SIGTERM, poked);
112 #endif /* HAVE_SIGNAL_H */
113
114 /* Initialize fillerup() variables */
115 if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
116 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
117 SDL_FreeWAV(wave.sound);
118 quit(2);
119 }
120
121 /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
122
123 /* Let the audio run */
124 SDL_PauseAudio(0);
125
126 done = 0;
127
128 /* Note that we stuff the entire audio buffer into the queue in one
129 shot. Most apps would want to feed it a little at a time, as it
130 plays, but we're going for simplicity here. */
131
132 #ifdef __EMSCRIPTEN__
133 emscripten_set_main_loop(loop, 0, 1);
134 #else
135 while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
136 {
137 loop();
138
139 SDL_Delay(100); /* let it play for awhile. */
140 }
141 #endif
142
143 /* Clean up on signal */
144 SDL_CloseAudio();
145 SDL_FreeWAV(wave.sound);
146 SDL_Quit();
147 return 0;
148 }
149
150 /* vi: set ts=4 sw=4 expandtab: */
151