testmultiaudio.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #include <SDL3/SDL.h>
  11. #include <SDL3/SDL_main.h>
  12. #include <SDL3/SDL_test.h>
  13. #include "testutils.h"
  14. #include <stdio.h> /* for fflush() and stdout */
  15. #ifdef __EMSCRIPTEN__
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. #include "testutils.h"
  19. static SDL_AudioSpec spec;
  20. static Uint8 *sound = NULL; /* Pointer to wave data */
  21. static Uint32 soundlen = 0; /* Length of wave data */
  22. typedef struct
  23. {
  24. SDL_AudioDeviceID dev;
  25. int soundpos;
  26. SDL_AtomicInt done;
  27. } callback_data;
  28. static callback_data cbd[64];
  29. static void SDLCALL
  30. play_through_once(void *arg, Uint8 *stream, int len)
  31. {
  32. callback_data *cbdata = (callback_data *)arg;
  33. Uint8 *waveptr = sound + cbdata->soundpos;
  34. int waveleft = soundlen - cbdata->soundpos;
  35. int cpy = len;
  36. if (cpy > waveleft) {
  37. cpy = waveleft;
  38. }
  39. SDL_memcpy(stream, waveptr, cpy);
  40. len -= cpy;
  41. cbdata->soundpos += cpy;
  42. if (len > 0) {
  43. stream += cpy;
  44. SDL_memset(stream, spec.silence, len);
  45. SDL_AtomicSet(&cbdata->done, 1);
  46. }
  47. }
  48. #ifdef __EMSCRIPTEN__
  49. static void loop(void)
  50. {
  51. if (SDL_AtomicGet(&cbd[0].done)) {
  52. emscripten_cancel_main_loop();
  53. SDL_PauseAudioDevice(cbd[0].dev);
  54. SDL_CloseAudioDevice(cbd[0].dev);
  55. SDL_free(sound);
  56. SDL_Quit();
  57. }
  58. }
  59. #endif
  60. static void
  61. test_multi_audio(int devcount)
  62. {
  63. int keep_going = 1;
  64. int i;
  65. #ifdef __ANDROID__
  66. SDL_Event event;
  67. /* Create a Window to get fully initialized event processing for testing pause on Android. */
  68. SDL_CreateWindow("testmultiaudio", 320, 240, 0);
  69. #endif
  70. if (devcount > 64) {
  71. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Too many devices (%d), clamping to 64...\n",
  72. devcount);
  73. devcount = 64;
  74. }
  75. spec.callback = play_through_once;
  76. for (i = 0; i < devcount; i++) {
  77. const char *devname = SDL_GetAudioDeviceName(i, 0);
  78. SDL_Log("playing on device #%d: ('%s')...", i, devname);
  79. SDL_memset(&cbd[0], '\0', sizeof(callback_data));
  80. spec.userdata = &cbd[0];
  81. cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
  82. if (cbd[0].dev == 0) {
  83. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device failed: %s\n", SDL_GetError());
  84. } else {
  85. SDL_PlayAudioDevice(cbd[0].dev);
  86. #ifdef __EMSCRIPTEN__
  87. emscripten_set_main_loop(loop, 0, 1);
  88. #else
  89. while (!SDL_AtomicGet(&cbd[0].done)) {
  90. #ifdef __ANDROID__
  91. /* Empty queue, some application events would prevent pause. */
  92. while (SDL_PollEvent(&event)) {
  93. }
  94. #endif
  95. SDL_Delay(100);
  96. }
  97. SDL_PauseAudioDevice(cbd[0].dev);
  98. #endif
  99. SDL_Log("done.\n");
  100. SDL_CloseAudioDevice(cbd[0].dev);
  101. }
  102. }
  103. SDL_memset(cbd, '\0', sizeof(cbd));
  104. SDL_Log("playing on all devices...\n");
  105. for (i = 0; i < devcount; i++) {
  106. const char *devname = SDL_GetAudioDeviceName(i, 0);
  107. spec.userdata = &cbd[i];
  108. cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
  109. if (cbd[i].dev == 0) {
  110. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device %d failed: %s\n", i, SDL_GetError());
  111. }
  112. }
  113. for (i = 0; i < devcount; i++) {
  114. if (cbd[i].dev) {
  115. SDL_PlayAudioDevice(cbd[i].dev);
  116. }
  117. }
  118. while (keep_going) {
  119. keep_going = 0;
  120. for (i = 0; i < devcount; i++) {
  121. if ((cbd[i].dev) && (!SDL_AtomicGet(&cbd[i].done))) {
  122. keep_going = 1;
  123. }
  124. }
  125. #ifdef __ANDROID__
  126. /* Empty queue, some application events would prevent pause. */
  127. while (SDL_PollEvent(&event)) {
  128. }
  129. #endif
  130. SDL_Delay(100);
  131. }
  132. #ifndef __EMSCRIPTEN__
  133. for (i = 0; i < devcount; i++) {
  134. if (cbd[i].dev) {
  135. SDL_PauseAudioDevice(cbd[i].dev);
  136. SDL_CloseAudioDevice(cbd[i].dev);
  137. }
  138. }
  139. SDL_Log("All done!\n");
  140. #endif
  141. }
  142. int main(int argc, char **argv)
  143. {
  144. int devcount = 0;
  145. int i;
  146. char *filename = NULL;
  147. SDLTest_CommonState *state;
  148. /* Initialize test framework */
  149. state = SDLTest_CommonCreateState(argv, 0);
  150. if (state == NULL) {
  151. return 1;
  152. }
  153. /* Enable standard application logging */
  154. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  155. /* Parse commandline */
  156. for (i = 1; i < argc;) {
  157. int consumed;
  158. consumed = SDLTest_CommonArg(state, i);
  159. if (!consumed) {
  160. if (!filename) {
  161. filename = argv[i];
  162. consumed = 1;
  163. }
  164. }
  165. if (consumed <= 0) {
  166. static const char *options[] = { "[sample.wav]", NULL };
  167. SDLTest_CommonLogUsage(state, argv[0], options);
  168. return 1;
  169. }
  170. i += consumed;
  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. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  178. filename = GetResourceFilename(filename, "sample.wav");
  179. devcount = SDL_GetNumAudioDevices(0);
  180. if (devcount < 1) {
  181. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio devices!\n");
  182. } else {
  183. /* Load the wave file into memory */
  184. if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
  185. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename,
  186. SDL_GetError());
  187. } else {
  188. test_multi_audio(devcount);
  189. SDL_free(sound);
  190. }
  191. }
  192. SDL_free(filename);
  193. SDL_Quit();
  194. SDLTest_CommonDestroyState(state);
  195. return 0;
  196. }