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

207 lines
5.5 KiB

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