testaudiohotplug.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. Copyright (C) 1997-2023 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. /* Program to test hotplugging of audio devices */
  11. #include <stdlib.h>
  12. #if HAVE_SIGNAL_H
  13. #include <signal.h>
  14. #endif
  15. #ifdef __EMSCRIPTEN__
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. #include <SDL3/SDL.h>
  19. #include <SDL3/SDL_main.h>
  20. #include "testutils.h"
  21. static SDL_AudioSpec spec;
  22. static Uint8 *sound = NULL; /* Pointer to wave data */
  23. static Uint32 soundlen = 0; /* Length of wave data */
  24. static int posindex = 0;
  25. static Uint32 positions[64];
  26. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  27. static void
  28. quit(int rc)
  29. {
  30. SDL_Quit();
  31. exit(rc);
  32. }
  33. static void SDLCALL
  34. fillerup(void *_pos, Uint8 *stream, int len)
  35. {
  36. Uint32 pos = *((Uint32 *)_pos);
  37. Uint8 *waveptr;
  38. int waveleft;
  39. /* Set up the pointers */
  40. waveptr = sound + pos;
  41. waveleft = soundlen - pos;
  42. /* Go! */
  43. while (waveleft <= len) {
  44. SDL_memcpy(stream, waveptr, waveleft);
  45. stream += waveleft;
  46. len -= waveleft;
  47. waveptr = sound;
  48. waveleft = soundlen;
  49. pos = 0;
  50. }
  51. SDL_memcpy(stream, waveptr, len);
  52. pos += len;
  53. *((Uint32 *)_pos) = pos;
  54. }
  55. static int done = 0;
  56. static void poked(int sig)
  57. {
  58. done = 1;
  59. }
  60. static const char *devtypestr(int iscapture)
  61. {
  62. return iscapture ? "capture" : "output";
  63. }
  64. static void iteration(void)
  65. {
  66. SDL_Event e;
  67. SDL_AudioDeviceID dev;
  68. while (SDL_PollEvent(&e)) {
  69. if (e.type == SDL_EVENT_QUIT) {
  70. done = 1;
  71. } else if (e.type == SDL_EVENT_KEY_UP) {
  72. if (e.key.keysym.sym == SDLK_ESCAPE) {
  73. done = 1;
  74. }
  75. } else if (e.type == SDL_EVENT_AUDIO_DEVICE_ADDED) {
  76. int index = e.adevice.which;
  77. int iscapture = e.adevice.iscapture;
  78. const char *name = SDL_GetAudioDeviceName(index, iscapture);
  79. if (name != NULL) {
  80. SDL_Log("New %s audio device at index %u: %s\n", devtypestr(iscapture), (unsigned int)index, name);
  81. } else {
  82. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Got new %s device at index %u, but failed to get the name: %s\n",
  83. devtypestr(iscapture), (unsigned int)index, SDL_GetError());
  84. continue;
  85. }
  86. if (!iscapture) {
  87. positions[posindex] = 0;
  88. spec.userdata = &positions[posindex++];
  89. spec.callback = fillerup;
  90. dev = SDL_OpenAudioDevice(name, 0, &spec, NULL, 0);
  91. if (!dev) {
  92. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open '%s': %s\n", name, SDL_GetError());
  93. } else {
  94. SDL_Log("Opened '%s' as %u\n", name, (unsigned int)dev);
  95. SDL_PlayAudioDevice(dev);
  96. }
  97. }
  98. } else if (e.type == SDL_EVENT_AUDIO_DEVICE_REMOVED) {
  99. dev = (SDL_AudioDeviceID)e.adevice.which;
  100. SDL_Log("%s device %u removed.\n", devtypestr(e.adevice.iscapture), (unsigned int)dev);
  101. SDL_CloseAudioDevice(dev);
  102. }
  103. }
  104. }
  105. #ifdef __EMSCRIPTEN__
  106. static void loop(void)
  107. {
  108. if (done)
  109. emscripten_cancel_main_loop();
  110. else
  111. iteration();
  112. }
  113. #endif
  114. int main(int argc, char *argv[])
  115. {
  116. int i;
  117. char *filename = NULL;
  118. /* Enable standard application logging */
  119. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  120. /* Load the SDL library */
  121. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
  122. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  123. return 1;
  124. }
  125. /* Some targets (Mac CoreAudio) need an event queue for audio hotplug, so make and immediately hide a window. */
  126. SDL_MinimizeWindow(SDL_CreateWindow("testaudiohotplug", 640, 480, 0));
  127. filename = GetResourceFilename(argc > 1 ? argv[1] : NULL, "sample.wav");
  128. if (filename == NULL) {
  129. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  130. quit(1);
  131. }
  132. /* Load the wave file into memory */
  133. if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
  134. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  135. quit(1);
  136. }
  137. #if HAVE_SIGNAL_H
  138. /* Set the signals */
  139. #ifdef SIGHUP
  140. (void)signal(SIGHUP, poked);
  141. #endif
  142. (void)signal(SIGINT, poked);
  143. #ifdef SIGQUIT
  144. (void)signal(SIGQUIT, poked);
  145. #endif
  146. (void)signal(SIGTERM, poked);
  147. #endif /* HAVE_SIGNAL_H */
  148. /* Show the list of available drivers */
  149. SDL_Log("Available audio drivers:");
  150. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  151. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  152. }
  153. SDL_Log("Select a driver with the SDL_AUDIO_DRIVER environment variable.\n");
  154. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  155. #ifdef __EMSCRIPTEN__
  156. emscripten_set_main_loop(loop, 0, 1);
  157. #else
  158. while (!done) {
  159. SDL_Delay(100);
  160. iteration();
  161. }
  162. #endif
  163. /* Clean up on signal */
  164. /* Quit audio first, then free WAV. This prevents access violations in the audio threads. */
  165. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  166. SDL_free(sound);
  167. SDL_free(filename);
  168. SDL_Quit();
  169. return 0;
  170. }