Debian Codegraph @

test/testaudiohotplug.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 test hotplugging of audio devices */
14
15 #include "SDL_config.h"
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #ifdef HAVE_SIGNAL_H
21 #include <signal.h>
22 #endif
23
24 #ifdef __EMSCRIPTEN__
25 #include <emscripten/emscripten.h>
26 #endif
27
28 #include "SDL.h"
29 #include "testutils.h"
30
31 static SDL_AudioSpec spec;
32 static Uint8 *sound = NULL; /* Pointer to wave data */
33 static Uint32 soundlen = 0; /* Length of wave data */
34
35 static int posindex = 0;
36 static Uint32 positions[64];
37
38 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
39 static void
40 quit(int rc)
41 {
42 SDL_Quit();
43 exit(rc);
44 }
45
46 void SDLCALL
47 fillerup(void *_pos, Uint8 *stream, int len)
48 {
49 Uint32 pos = *((Uint32 *)_pos);
50 Uint8 *waveptr;
51 int waveleft;
52
53 /* Set up the pointers */
54 waveptr = sound + pos;
55 waveleft = soundlen - pos;
56
57 /* Go! */
58 while (waveleft <= len) {
59 SDL_memcpy(stream, waveptr, waveleft);
60 stream += waveleft;
61 len -= waveleft;
62 waveptr = sound;
63 waveleft = soundlen;
64 pos = 0;
65 }
66 SDL_memcpy(stream, waveptr, len);
67 pos += len;
68 *((Uint32 *)_pos) = pos;
69 }
70
71 static int done = 0;
72 void poked(int sig)
73 {
74 done = 1;
75 }
76
77 static const char *
78 devtypestr(int iscapture)
79 {
80 return iscapture ? "capture" : "output";
81 }
82
83 static void
84 iteration(void)
85 {
86 SDL_Event e;
87 SDL_AudioDeviceID dev;
88 while (SDL_PollEvent(&e)) {
89 if (e.type == SDL_QUIT) {
90 done = 1;
91 } else if (e.type == SDL_KEYUP) {
92 if (e.key.keysym.sym == SDLK_ESCAPE) {
93 done = 1;
94 }
95 } else if (e.type == SDL_AUDIODEVICEADDED) {
96 int index = e.adevice.which;
97 int iscapture = e.adevice.iscapture;
98 const char *name = SDL_GetAudioDeviceName(index, iscapture);
99 if (name) {
100 SDL_Log("New %s audio device at index %u: %s\n", devtypestr(iscapture), (unsigned int)index, name);
101 } else {
102 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Got new %s device at index %u, but failed to get the name: %s\n",
103 devtypestr(iscapture), (unsigned int)index, SDL_GetError());
104 continue;
105 }
106 if (!iscapture) {
107 positions[posindex] = 0;
108 spec.userdata = &positions[posindex++];
109 spec.callback = fillerup;
110 dev = SDL_OpenAudioDevice(name, 0, &spec, NULL, 0);
111 if (!dev) {
112 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open '%s': %s\n", name, SDL_GetError());
113 } else {
114 SDL_Log("Opened '%s' as %u\n", name, (unsigned int)dev);
115 SDL_PauseAudioDevice(dev, 0);
116 }
117 }
118 } else if (e.type == SDL_AUDIODEVICEREMOVED) {
119 dev = (SDL_AudioDeviceID)e.adevice.which;
120 SDL_Log("%s device %u removed.\n", devtypestr(e.adevice.iscapture), (unsigned int)dev);
121 SDL_CloseAudioDevice(dev);
122 }
123 }
124 }
125
126 #ifdef __EMSCRIPTEN__
127 void loop(void)
128 {
129 if (done)
130 emscripten_cancel_main_loop();
131 else
132 iteration();
133 }
134 #endif
135
136 int main(int argc, char *argv[])
137 {
138 int i;
139 char *filename = NULL;
140
141 /* Enable standard application logging */
142 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
143
144 /* Load the SDL library */
145 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
146 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
147 return 1;
148 }
149
150 /* Some targets (Mac CoreAudio) need an event queue for audio hotplug, so make and immediately hide a window. */
151 SDL_MinimizeWindow(SDL_CreateWindow("testaudiohotplug", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0));
152
153 filename = GetResourceFilename(argc > 1 ? argv[1] : NULL, "sample.wav");
154
155 if (!filename) {
156 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
157 quit(1);
158 }
159
160 /* Load the wave file into memory */
161 if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
162 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
163 quit(1);
164 }
165
166 #ifdef HAVE_SIGNAL_H
167 /* Set the signals */
168 #ifdef SIGHUP
169 (void)signal(SIGHUP, poked);
170 #endif
171 (void)signal(SIGINT, poked);
172 #ifdef SIGQUIT
173 (void)signal(SIGQUIT, poked);
174 #endif
175 (void)signal(SIGTERM, poked);
176 #endif /* HAVE_SIGNAL_H */
177
178 /* Show the list of available drivers */
179 SDL_Log("Available audio drivers:");
180 for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
181 SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
182 }
183
184 SDL_Log("Select a driver with the SDL_AUDIODRIVER environment variable.\n");
185 SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
186
187 #ifdef __EMSCRIPTEN__
188 emscripten_set_main_loop(loop, 0, 1);
189 #else
190 while (!done) {
191 SDL_Delay(100);
192 iteration();
193 }
194 #endif
195
196 /* Clean up on signal */
197 /* Quit audio first, then free WAV. This prevents access violations in the audio threads. */
198 SDL_QuitSubSystem(SDL_INIT_AUDIO);
199 SDL_FreeWAV(sound);
200 SDL_free(filename);
201 SDL_Quit();
202 return 0;
203 }
204
205 /* vi: set ts=4 sw=4 expandtab: */