testmultiaudio.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /* these have to be in globals so the Emscripten port can see them in the mainloop. :/ */
  23. static SDL_AudioStream *stream = NULL;
  24. #ifdef __EMSCRIPTEN__
  25. static void loop(void)
  26. {
  27. if (SDL_GetAudioStreamAvailable(stream) == 0) {
  28. SDL_Log("done.");
  29. SDL_DestroyAudioStream(stream);
  30. SDL_free(sound);
  31. SDL_Quit();
  32. emscripten_cancel_main_loop();
  33. }
  34. }
  35. #endif
  36. static void
  37. test_multi_audio(SDL_AudioDeviceID *devices, int devcount)
  38. {
  39. int keep_going = 1;
  40. SDL_AudioStream **streams = NULL;
  41. int i;
  42. #ifdef __ANDROID__ /* !!! FIXME: maybe always create a window, in the SDLTest layer, so these #ifdefs don't have to be here? */
  43. SDL_Event event;
  44. /* Create a Window to get fully initialized event processing for testing pause on Android. */
  45. SDL_CreateWindow("testmultiaudio", 320, 240, 0);
  46. #endif
  47. for (i = 0; i < devcount; i++) {
  48. char *devname = SDL_GetAudioDeviceName(devices[i]);
  49. SDL_Log("Playing on device #%d of %d: id=%u, name='%s'...", i, devcount, (unsigned int) devices[i], devname);
  50. if ((stream = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL)) == NULL) {
  51. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed: %s", SDL_GetError());
  52. } else {
  53. SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));
  54. SDL_PutAudioStreamData(stream, sound, soundlen);
  55. SDL_FlushAudioStream(stream);
  56. #ifdef __EMSCRIPTEN__
  57. emscripten_set_main_loop(loop, 0, 1);
  58. #else
  59. while (SDL_GetAudioStreamAvailable(stream) > 0) {
  60. #ifdef __ANDROID__
  61. /* Empty queue, some application events would prevent pause. */
  62. while (SDL_PollEvent(&event)) {
  63. }
  64. #endif
  65. SDL_Delay(100);
  66. }
  67. #endif
  68. SDL_Log("done.");
  69. SDL_DestroyAudioStream(stream);
  70. }
  71. SDL_free(devname);
  72. stream = NULL;
  73. }
  74. /* note that Emscripten currently doesn't run this part (but maybe only has a single audio device anyhow?) */
  75. SDL_Log("Playing on all devices...\n");
  76. streams = (SDL_AudioStream **) SDL_calloc(devcount, sizeof (SDL_AudioStream *));
  77. if (!streams) {
  78. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
  79. } else {
  80. for (i = 0; i < devcount; i++) {
  81. streams[i] = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL);
  82. if (streams[i] == NULL) {
  83. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed for device %d of %d: %s", i, devcount, SDL_GetError());
  84. } else {
  85. SDL_PutAudioStreamData(streams[i], sound, soundlen);
  86. SDL_FlushAudioStream(streams[i]);
  87. }
  88. }
  89. /* try to start all the devices about the same time. SDL does not guarantee sync across physical devices. */
  90. for (i = 0; i < devcount; i++) {
  91. if (streams[i]) {
  92. SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(streams[i]));
  93. }
  94. }
  95. while (keep_going) {
  96. keep_going = 0;
  97. for (i = 0; i < devcount; i++) {
  98. if (streams[i] && (SDL_GetAudioStreamAvailable(streams[i]) > 0)) {
  99. keep_going = 1;
  100. }
  101. }
  102. #ifdef __ANDROID__
  103. /* Empty queue, some application events would prevent pause. */
  104. while (SDL_PollEvent(&event)) {}
  105. #endif
  106. SDL_Delay(100);
  107. }
  108. for (i = 0; i < devcount; i++) {
  109. SDL_DestroyAudioStream(streams[i]);
  110. }
  111. SDL_free(streams);
  112. }
  113. SDL_Log("All done!\n");
  114. }
  115. int main(int argc, char **argv)
  116. {
  117. SDL_AudioDeviceID *devices = NULL;
  118. int devcount = 0;
  119. int i;
  120. char *filename = NULL;
  121. SDLTest_CommonState *state;
  122. /* Initialize test framework */
  123. state = SDLTest_CommonCreateState(argv, 0);
  124. if (!state) {
  125. return 1;
  126. }
  127. /* Enable standard application logging */
  128. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  129. /* Parse commandline */
  130. for (i = 1; i < argc;) {
  131. int consumed;
  132. consumed = SDLTest_CommonArg(state, i);
  133. if (!consumed) {
  134. if (!filename) {
  135. filename = argv[i];
  136. consumed = 1;
  137. }
  138. }
  139. if (consumed <= 0) {
  140. static const char *options[] = { "[sample.wav]", NULL };
  141. SDLTest_CommonLogUsage(state, argv[0], options);
  142. return 1;
  143. }
  144. i += consumed;
  145. }
  146. /* Load the SDL library */
  147. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  148. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  149. return 1;
  150. }
  151. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  152. filename = GetResourceFilename(filename, "sample.wav");
  153. devices = SDL_GetAudioOutputDevices(&devcount);
  154. if (!devices) {
  155. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio devices!");
  156. } else {
  157. /* Load the wave file into memory */
  158. if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == -1) {
  159. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename,
  160. SDL_GetError());
  161. } else {
  162. test_multi_audio(devices, devcount);
  163. SDL_free(sound);
  164. }
  165. }
  166. SDL_free(devices);
  167. SDL_free(filename);
  168. SDL_Quit();
  169. SDLTest_CommonDestroyState(state);
  170. return 0;
  171. }