Debian Codegraph @

test/loopwavequeue.c

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