testaudiohotplug.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright (C) 1997-2022 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. 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. void poked(int sig)
  57. {
  58. done = 1;
  59. }
  60. static const char *
  61. devtypestr(int iscapture)
  62. {
  63. return iscapture ? "capture" : "output";
  64. }
  65. static void
  66. iteration()
  67. {
  68. SDL_Event e;
  69. SDL_AudioDeviceID dev;
  70. while (SDL_PollEvent(&e)) {
  71. if (e.type == SDL_QUIT) {
  72. done = 1;
  73. } else if (e.type == SDL_KEYUP) {
  74. if (e.key.keysym.sym == SDLK_ESCAPE) {
  75. done = 1;
  76. }
  77. } else if (e.type == SDL_AUDIODEVICEADDED) {
  78. int index = e.adevice.which;
  79. int iscapture = e.adevice.iscapture;
  80. const char *name = SDL_GetAudioDeviceName(index, iscapture);
  81. if (name != NULL) {
  82. SDL_Log("New %s audio device at index %u: %s\n", devtypestr(iscapture), (unsigned int)index, name);
  83. } else {
  84. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Got new %s device at index %u, but failed to get the name: %s\n",
  85. devtypestr(iscapture), (unsigned int)index, SDL_GetError());
  86. continue;
  87. }
  88. if (!iscapture) {
  89. positions[posindex] = 0;
  90. spec.userdata = &positions[posindex++];
  91. spec.callback = fillerup;
  92. dev = SDL_OpenAudioDevice(name, 0, &spec, NULL, 0);
  93. if (!dev) {
  94. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open '%s': %s\n", name, SDL_GetError());
  95. } else {
  96. SDL_Log("Opened '%s' as %u\n", name, (unsigned int)dev);
  97. SDL_PlayAudioDevice(dev);
  98. }
  99. }
  100. } else if (e.type == SDL_AUDIODEVICEREMOVED) {
  101. dev = (SDL_AudioDeviceID)e.adevice.which;
  102. SDL_Log("%s device %u removed.\n", devtypestr(e.adevice.iscapture), (unsigned int)dev);
  103. SDL_CloseAudioDevice(dev);
  104. }
  105. }
  106. }
  107. #ifdef __EMSCRIPTEN__
  108. void loop()
  109. {
  110. if (done)
  111. emscripten_cancel_main_loop();
  112. else
  113. iteration();
  114. }
  115. #endif
  116. int main(int argc, char *argv[])
  117. {
  118. int i;
  119. char *filename = NULL;
  120. /* Enable standard application logging */
  121. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  122. /* Load the SDL library */
  123. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
  124. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  125. return 1;
  126. }
  127. /* Some targets (Mac CoreAudio) need an event queue for audio hotplug, so make and immediately hide a window. */
  128. SDL_MinimizeWindow(SDL_CreateWindow("testaudiohotplug", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0));
  129. filename = GetResourceFilename(argc > 1 ? argv[1] : NULL, "sample.wav");
  130. if (filename == NULL) {
  131. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  132. quit(1);
  133. }
  134. /* Load the wave file into memory */
  135. if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
  136. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  137. quit(1);
  138. }
  139. #if HAVE_SIGNAL_H
  140. /* Set the signals */
  141. #ifdef SIGHUP
  142. (void)signal(SIGHUP, poked);
  143. #endif
  144. (void)signal(SIGINT, poked);
  145. #ifdef SIGQUIT
  146. (void)signal(SIGQUIT, poked);
  147. #endif
  148. (void)signal(SIGTERM, poked);
  149. #endif /* HAVE_SIGNAL_H */
  150. /* Show the list of available drivers */
  151. SDL_Log("Available audio drivers:");
  152. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  153. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  154. }
  155. SDL_Log("Select a driver with the SDL_AUDIO_DRIVER environment variable.\n");
  156. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  157. #ifdef __EMSCRIPTEN__
  158. emscripten_set_main_loop(loop, 0, 1);
  159. #else
  160. while (!done) {
  161. SDL_Delay(100);
  162. iteration();
  163. }
  164. #endif
  165. /* Clean up on signal */
  166. /* Quit audio first, then free WAV. This prevents access violations in the audio threads. */
  167. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  168. SDL_free(sound);
  169. SDL_free(filename);
  170. SDL_Quit();
  171. return 0;
  172. }