testmultiaudio.c 5.4 KB

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