testmultiaudio.c 6.1 KB

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