| 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 |
#include "SDL.h" |
| 13 |
|
| 14 |
#include <stdio.h> /* for fflush() and stdout */ |
| 15 |
|
| 16 |
#ifdef __EMSCRIPTEN__ |
| 17 |
#include <emscripten/emscripten.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "testutils.h" |
| 21 |
|
| 22 |
static SDL_AudioSpec spec; |
| 23 |
static Uint8 *sound = NULL; /* Pointer to wave data */ |
| 24 |
static Uint32 soundlen = 0; /* Length of wave data */ |
| 25 |
|
| 26 |
typedef struct |
| 27 |
{ |
| 28 |
SDL_AudioDeviceID dev; |
| 29 |
int soundpos; |
| 30 |
SDL_atomic_t done; |
| 31 |
} callback_data; |
| 32 |
|
| 33 |
callback_data cbd[64]; |
| 34 |
|
| 35 |
void SDLCALL |
| 36 |
play_through_once(void *arg, Uint8 *stream, int len) |
| 37 |
{ |
| 38 |
callback_data *cbdata = (callback_data *)arg; |
| 39 |
Uint8 *waveptr = sound + cbdata->soundpos; |
| 40 |
int waveleft = soundlen - cbdata->soundpos; |
| 41 |
int cpy = len; |
| 42 |
if (cpy > waveleft) { |
| 43 |
cpy = waveleft; |
| 44 |
} |
| 45 |
|
| 46 |
SDL_memcpy(stream, waveptr, cpy); |
| 47 |
len -= cpy; |
| 48 |
cbdata->soundpos += cpy; |
| 49 |
if (len > 0) { |
| 50 |
stream += cpy; |
| 51 |
SDL_memset(stream, spec.silence, len); |
| 52 |
SDL_AtomicSet(&cbdata->done, 1); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
void loop(void) |
| 57 |
{ |
| 58 |
if (SDL_AtomicGet(&cbd[0].done)) { |
| 59 |
#ifdef __EMSCRIPTEN__ |
| 60 |
emscripten_cancel_main_loop(); |
| 61 |
#endif |
| 62 |
SDL_PauseAudioDevice(cbd[0].dev, 1); |
| 63 |
SDL_CloseAudioDevice(cbd[0].dev); |
| 64 |
SDL_FreeWAV(sound); |
| 65 |
SDL_Quit(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
test_multi_audio(int devcount) |
| 71 |
{ |
| 72 |
int keep_going = 1; |
| 73 |
int i; |
| 74 |
|
| 75 |
#ifdef __ANDROID__ |
| 76 |
SDL_Event event; |
| 77 |
|
| 78 |
/* Create a Window to get fully initialized event processing for testing pause on Android. */ |
| 79 |
SDL_CreateWindow("testmultiaudio", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0); |
| 80 |
#endif |
| 81 |
|
| 82 |
if (devcount > 64) { |
| 83 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Too many devices (%d), clamping to 64...\n", |
| 84 |
devcount); |
| 85 |
devcount = 64; |
| 86 |
} |
| 87 |
|
| 88 |
spec.callback = play_through_once; |
| 89 |
|
| 90 |
for (i = 0; i < devcount; i++) { |
| 91 |
const char *devname = SDL_GetAudioDeviceName(i, 0); |
| 92 |
SDL_Log("playing on device #%d: ('%s')...", i, devname); |
| 93 |
|
| 94 |
SDL_memset(&cbd[0], '\0', sizeof(callback_data)); |
| 95 |
spec.userdata = &cbd[0]; |
| 96 |
cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0); |
| 97 |
if (cbd[0].dev == 0) { |
| 98 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device failed: %s\n", SDL_GetError()); |
| 99 |
} else { |
| 100 |
SDL_PauseAudioDevice(cbd[0].dev, 0); |
| 101 |
#ifdef __EMSCRIPTEN__ |
| 102 |
emscripten_set_main_loop(loop, 0, 1); |
| 103 |
#else |
| 104 |
while (!SDL_AtomicGet(&cbd[0].done)) { |
| 105 |
#ifdef __ANDROID__ |
| 106 |
/* Empty queue, some application events would prevent pause. */ |
| 107 |
while (SDL_PollEvent(&event)) { |
| 108 |
} |
| 109 |
#endif |
| 110 |
SDL_Delay(100); |
| 111 |
} |
| 112 |
SDL_PauseAudioDevice(cbd[0].dev, 1); |
| 113 |
#endif |
| 114 |
SDL_Log("done.\n"); |
| 115 |
SDL_CloseAudioDevice(cbd[0].dev); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
SDL_memset(cbd, '\0', sizeof(cbd)); |
| 120 |
|
| 121 |
SDL_Log("playing on all devices...\n"); |
| 122 |
for (i = 0; i < devcount; i++) { |
| 123 |
const char *devname = SDL_GetAudioDeviceName(i, 0); |
| 124 |
spec.userdata = &cbd[i]; |
| 125 |
cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0); |
| 126 |
if (cbd[i].dev == 0) { |
| 127 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device %d failed: %s\n", i, SDL_GetError()); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
for (i = 0; i < devcount; i++) { |
| 132 |
if (cbd[i].dev) { |
| 133 |
SDL_PauseAudioDevice(cbd[i].dev, 0); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
while (keep_going) { |
| 138 |
keep_going = 0; |
| 139 |
for (i = 0; i < devcount; i++) { |
| 140 |
if ((cbd[i].dev) && (!SDL_AtomicGet(&cbd[i].done))) { |
| 141 |
keep_going = 1; |
| 142 |
} |
| 143 |
} |
| 144 |
#ifdef __ANDROID__ |
| 145 |
/* Empty queue, some application events would prevent pause. */ |
| 146 |
while (SDL_PollEvent(&event)) { |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
SDL_Delay(100); |
| 151 |
} |
| 152 |
|
| 153 |
#ifndef __EMSCRIPTEN__ |
| 154 |
for (i = 0; i < devcount; i++) { |
| 155 |
if (cbd[i].dev) { |
| 156 |
SDL_PauseAudioDevice(cbd[i].dev, 1); |
| 157 |
SDL_CloseAudioDevice(cbd[i].dev); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
SDL_Log("All done!\n"); |
| 162 |
#endif |
| 163 |
} |
| 164 |
|
| 165 |
int main(int argc, char **argv) |
| 166 |
{ |
| 167 |
int devcount = 0; |
| 168 |
|
| 169 |
/* Enable standard application logging */ |
| 170 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 171 |
|
| 172 |
/* Load the SDL library */ |
| 173 |
if (SDL_Init(SDL_INIT_AUDIO) < 0) { |
| 174 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 175 |
return 1; |
| 176 |
} |
| 177 |
|
| 178 |
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver()); |
| 179 |
|
| 180 |
devcount = SDL_GetNumAudioDevices(0); |
| 181 |
if (devcount < 1) { |
| 182 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio devices!\n"); |
| 183 |
} else { |
| 184 |
char *file = GetResourceFilename(argc > 1 ? argv[1] : NULL, "sample.wav"); |
| 185 |
|
| 186 |
/* Load the wave file into memory */ |
| 187 |
if (SDL_LoadWAV(file, &spec, &sound, &soundlen) == NULL) { |
| 188 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, |
| 189 |
SDL_GetError()); |
| 190 |
} else { |
| 191 |
test_multi_audio(devcount); |
| 192 |
SDL_FreeWAV(sound); |
| 193 |
} |
| 194 |
|
| 195 |
SDL_free(file); |
| 196 |
} |
| 197 |
|
| 198 |
SDL_Quit(); |
| 199 |
return 0; |
| 200 |
} |