🛠️🐜 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.

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