🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.4 KiB

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