summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testmultiaudio.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/test/testmultiaudio.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testmultiaudio.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testmultiaudio.c201
1 files changed, 201 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testmultiaudio.c b/src/contrib/SDL-3.2.20/test/testmultiaudio.c
new file mode 100644
index 0000000..7e1b8dc
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testmultiaudio.c
@@ -0,0 +1,201 @@
1/*
2 Copyright (C) 1997-2025 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#include <SDL3/SDL.h>
13#include <SDL3/SDL_main.h>
14#include <SDL3/SDL_test.h>
15
16#include "testutils.h"
17
18#ifdef SDL_PLATFORM_EMSCRIPTEN
19#include <emscripten/emscripten.h>
20#endif
21
22#include <stdio.h> /* for fflush() and stdout */
23
24static SDL_AudioSpec spec;
25static Uint8 *sound = NULL; /* Pointer to wave data */
26static Uint32 soundlen = 0; /* Length of wave data */
27
28/* these have to be in globals so the Emscripten port can see them in the mainloop. :/ */
29static SDL_AudioStream *stream = NULL;
30
31
32#ifdef SDL_PLATFORM_EMSCRIPTEN
33static void loop(void)
34{
35 if (SDL_GetAudioStreamAvailable(stream) == 0) {
36 SDL_Log("done.");
37 SDL_DestroyAudioStream(stream);
38 SDL_free(sound);
39 SDL_Quit();
40 emscripten_cancel_main_loop();
41 }
42}
43#endif
44
45static void
46test_multi_audio(const SDL_AudioDeviceID *devices, int devcount)
47{
48 int keep_going = 1;
49 SDL_AudioStream **streams = NULL;
50 int i;
51
52#ifdef SDL_PLATFORM_ANDROID /* !!! FIXME: maybe always create a window, in the SDLTest layer, so these #ifdefs don't have to be here? */
53 SDL_Event event;
54
55 /* Create a Window to get fully initialized event processing for testing pause on Android. */
56 SDL_CreateWindow("testmultiaudio", 320, 240, 0);
57#endif
58
59 for (i = 0; i < devcount; i++) {
60 const char *devname = SDL_GetAudioDeviceName(devices[i]);
61
62 SDL_Log("Playing on device #%d of %d: id=%u, name='%s'...", i, devcount, (unsigned int) devices[i], devname);
63
64 if ((stream = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL)) == NULL) {
65 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed: %s", SDL_GetError());
66 } else {
67 SDL_ResumeAudioStreamDevice(stream);
68 SDL_PutAudioStreamData(stream, sound, soundlen);
69 SDL_FlushAudioStream(stream);
70#ifdef SDL_PLATFORM_EMSCRIPTEN
71 emscripten_set_main_loop(loop, 0, 1);
72#else
73 while (SDL_GetAudioStreamAvailable(stream) > 0) {
74#ifdef SDL_PLATFORM_ANDROID
75 /* Empty queue, some application events would prevent pause. */
76 while (SDL_PollEvent(&event)) {
77 }
78#endif
79 SDL_Delay(100);
80 }
81#endif
82 SDL_Log("done.");
83 SDL_DestroyAudioStream(stream);
84 }
85 stream = NULL;
86 }
87
88 /* note that Emscripten currently doesn't run this part (but maybe only has a single audio device anyhow?) */
89 SDL_Log("Playing on all devices...");
90 streams = (SDL_AudioStream **) SDL_calloc(devcount, sizeof (SDL_AudioStream *));
91 if (!streams) {
92 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
93 } else {
94 for (i = 0; i < devcount; i++) {
95 streams[i] = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL);
96 if (streams[i] == NULL) {
97 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed for device %d of %d: %s", i, devcount, SDL_GetError());
98 } else {
99 SDL_PutAudioStreamData(streams[i], sound, soundlen);
100 SDL_FlushAudioStream(streams[i]);
101 }
102 }
103
104 /* try to start all the devices about the same time. SDL does not guarantee sync across physical devices. */
105 for (i = 0; i < devcount; i++) {
106 if (streams[i]) {
107 SDL_ResumeAudioStreamDevice(streams[i]);
108 }
109 }
110
111 while (keep_going) {
112 keep_going = 0;
113 for (i = 0; i < devcount; i++) {
114 if (streams[i] && (SDL_GetAudioStreamAvailable(streams[i]) > 0)) {
115 keep_going = 1;
116 }
117 }
118#ifdef SDL_PLATFORM_ANDROID
119 /* Empty queue, some application events would prevent pause. */
120 while (SDL_PollEvent(&event)) {}
121#endif
122
123 SDL_Delay(100);
124 }
125
126 for (i = 0; i < devcount; i++) {
127 SDL_DestroyAudioStream(streams[i]);
128 }
129
130 SDL_free(streams);
131 }
132
133 SDL_Log("All done!");
134}
135
136int main(int argc, char **argv)
137{
138 SDL_AudioDeviceID *devices;
139 int devcount = 0;
140 int i;
141 char *filename = NULL;
142 SDLTest_CommonState *state;
143
144 /* Initialize test framework */
145 state = SDLTest_CommonCreateState(argv, SDL_INIT_AUDIO);
146 if (!state) {
147 return 1;
148 }
149
150 /* Parse commandline */
151 for (i = 1; i < argc;) {
152 int consumed;
153
154 consumed = SDLTest_CommonArg(state, i);
155 if (!consumed) {
156 if (!filename) {
157 filename = argv[i];
158 consumed = 1;
159 }
160 }
161 if (consumed <= 0) {
162 static const char *options[] = { "[sample.wav]", NULL };
163 SDLTest_CommonLogUsage(state, argv[0], options);
164 return 1;
165 }
166
167 i += consumed;
168 }
169
170 /* Load the SDL library */
171 if (!SDLTest_CommonInit(state)) {
172 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
173 return 1;
174 }
175
176 SDL_Log("Using audio driver: %s", SDL_GetCurrentAudioDriver());
177
178 filename = GetResourceFilename(filename, "sample.wav");
179
180 devices = SDL_GetAudioPlaybackDevices(&devcount);
181 if (!devices) {
182 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio playback devices!");
183 } else {
184 /* Load the wave file into memory */
185 if (SDL_LoadWAV(filename, &spec, &sound, &soundlen)) {
186 test_multi_audio(devices, devcount);
187 SDL_free(sound);
188 } else {
189 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", filename,
190 SDL_GetError());
191 }
192 SDL_free(devices);
193 }
194
195 SDL_free(filename);
196
197 SDLTest_CommonQuit(state);
198
199 return 0;
200}
201