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

205 lines
5.6 KiB

  1. /*
  2. Copyright (C) 1997-2023 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 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. }
  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 loop()
  110. {
  111. if (done)
  112. emscripten_cancel_main_loop();
  113. else
  114. iteration();
  115. }
  116. #endif
  117. int main(int argc, char *argv[])
  118. {
  119. int i;
  120. char *filename = NULL;
  121. /* Enable standard application logging */
  122. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  123. /* Load the SDL library */
  124. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
  125. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  126. return 1;
  127. }
  128. /* Some targets (Mac CoreAudio) need an event queue for audio hotplug, so make and immediately hide a window. */
  129. SDL_MinimizeWindow(SDL_CreateWindow("testaudiohotplug", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0));
  130. filename = GetResourceFilename(argc > 1 ? argv[1] : NULL, "sample.wav");
  131. if (filename == NULL) {
  132. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  133. quit(1);
  134. }
  135. /* Load the wave file into memory */
  136. if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
  137. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  138. quit(1);
  139. }
  140. #if HAVE_SIGNAL_H
  141. /* Set the signals */
  142. #ifdef SIGHUP
  143. (void)signal(SIGHUP, poked);
  144. #endif
  145. (void)signal(SIGINT, poked);
  146. #ifdef SIGQUIT
  147. (void)signal(SIGQUIT, poked);
  148. #endif
  149. (void)signal(SIGTERM, poked);
  150. #endif /* HAVE_SIGNAL_H */
  151. /* Show the list of available drivers */
  152. SDL_Log("Available audio drivers:");
  153. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  154. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  155. }
  156. SDL_Log("Select a driver with the SDL_AUDIODRIVER environment variable.\n");
  157. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  158. #ifdef __EMSCRIPTEN__
  159. emscripten_set_main_loop(loop, 0, 1);
  160. #else
  161. while (!done) {
  162. SDL_Delay(100);
  163. iteration();
  164. }
  165. #endif
  166. /* Clean up on signal */
  167. /* Quit audio first, then free WAV. This prevents access violations in the audio threads. */
  168. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  169. SDL_FreeWAV(sound);
  170. SDL_free(filename);
  171. SDL_Quit();
  172. return 0;
  173. }
  174. /* vi: set ts=4 sw=4 expandtab: */