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

203 lines
5.4 KiB

  1. /*
  2. Copyright (C) 1997-2020 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. /* Program to test hotplugging of audio devices */
  11. #include "SDL_config.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #if HAVE_SIGNAL_H
  15. #include <signal.h>
  16. #endif
  17. #ifdef __EMSCRIPTEN__
  18. #include <emscripten/emscripten.h>
  19. #endif
  20. #include "SDL.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
  57. poked(int sig)
  58. {
  59. done = 1;
  60. }
  61. static const char*
  62. devtypestr(int iscapture)
  63. {
  64. return iscapture ? "capture" : "output";
  65. }
  66. static void
  67. iteration()
  68. {
  69. SDL_Event e;
  70. SDL_AudioDeviceID dev;
  71. while (SDL_PollEvent(&e)) {
  72. if (e.type == SDL_QUIT) {
  73. done = 1;
  74. } else if (e.type == SDL_KEYUP) {
  75. if (e.key.keysym.sym == SDLK_ESCAPE)
  76. done = 1;
  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_PauseAudioDevice(dev, 0);
  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
  109. loop()
  110. {
  111. if(done)
  112. emscripten_cancel_main_loop();
  113. else
  114. iteration();
  115. }
  116. #endif
  117. int
  118. main(int argc, char *argv[])
  119. {
  120. int i;
  121. char filename[4096];
  122. /* Enable standard application logging */
  123. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  124. /* Load the SDL library */
  125. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
  126. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  127. return (1);
  128. }
  129. /* Some targets (Mac CoreAudio) need an event queue for audio hotplug, so make and immediately hide a window. */
  130. SDL_MinimizeWindow(SDL_CreateWindow("testaudiohotplug", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0));
  131. if (argc > 1) {
  132. SDL_strlcpy(filename, argv[1], sizeof(filename));
  133. } else {
  134. SDL_strlcpy(filename, "sample.wav", sizeof(filename));
  135. }
  136. /* Load the wave file into memory */
  137. if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
  138. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  139. quit(1);
  140. }
  141. #if HAVE_SIGNAL_H
  142. /* Set the signals */
  143. #ifdef SIGHUP
  144. signal(SIGHUP, poked);
  145. #endif
  146. signal(SIGINT, poked);
  147. #ifdef SIGQUIT
  148. signal(SIGQUIT, poked);
  149. #endif
  150. signal(SIGTERM, poked);
  151. #endif /* HAVE_SIGNAL_H */
  152. /* Show the list of available drivers */
  153. SDL_Log("Available audio drivers:");
  154. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  155. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  156. }
  157. SDL_Log("Select a driver with the SDL_AUDIODRIVER environment variable.\n");
  158. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  159. #ifdef __EMSCRIPTEN__
  160. emscripten_set_main_loop(loop, 0, 1);
  161. #else
  162. while (!done) {
  163. SDL_Delay(100);
  164. iteration();
  165. }
  166. #endif
  167. /* Clean up on signal */
  168. /* Quit audio first, then free WAV. This prevents access violations in the audio threads. */
  169. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  170. SDL_FreeWAV(sound);
  171. SDL_Quit();
  172. return (0);
  173. }
  174. /* vi: set ts=4 sw=4 expandtab: */