testaudiohotplug.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Copyright (C) 1997-2016 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 void
  62. iteration()
  63. {
  64. SDL_Event e;
  65. SDL_AudioDeviceID dev;
  66. while (SDL_PollEvent(&e)) {
  67. if (e.type == SDL_QUIT) {
  68. done = 1;
  69. } else if (e.type == SDL_AUDIODEVICEADDED) {
  70. const char *name = SDL_GetAudioDeviceName(e.adevice.which, 0);
  71. SDL_Log("New %s audio device: %s\n", e.adevice.iscapture ? "capture" : "output", name);
  72. if (!e.adevice.iscapture) {
  73. positions[posindex] = 0;
  74. spec.userdata = &positions[posindex++];
  75. spec.callback = fillerup;
  76. dev = SDL_OpenAudioDevice(name, 0, &spec, NULL, 0);
  77. if (!dev) {
  78. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open '%s': %s\n", name, SDL_GetError());
  79. } else {
  80. SDL_Log("Opened '%s' as %u\n", name, (unsigned int) dev);
  81. SDL_PauseAudioDevice(dev, 0);
  82. }
  83. }
  84. } else if (e.type == SDL_AUDIODEVICEREMOVED) {
  85. dev = (SDL_AudioDeviceID) e.adevice.which;
  86. SDL_Log("%s device %u removed.\n", e.adevice.iscapture ? "capture" : "output", (unsigned int) dev);
  87. SDL_CloseAudioDevice(dev);
  88. }
  89. }
  90. }
  91. #ifdef __EMSCRIPTEN__
  92. void
  93. loop()
  94. {
  95. if(done)
  96. emscripten_cancel_main_loop();
  97. else
  98. iteration();
  99. }
  100. #endif
  101. int
  102. main(int argc, char *argv[])
  103. {
  104. int i;
  105. char filename[4096];
  106. /* Enable standard application logging */
  107. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  108. /* Load the SDL library */
  109. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
  110. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  111. return (1);
  112. }
  113. /* Some targets (Mac CoreAudio) need an event queue for audio hotplug, so make and immediately hide a window. */
  114. SDL_MinimizeWindow(SDL_CreateWindow("testaudiohotplug", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0));
  115. if (argc > 1) {
  116. SDL_strlcpy(filename, argv[1], sizeof(filename));
  117. } else {
  118. SDL_strlcpy(filename, "sample.wav", sizeof(filename));
  119. }
  120. /* Load the wave file into memory */
  121. if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
  122. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  123. quit(1);
  124. }
  125. #if HAVE_SIGNAL_H
  126. /* Set the signals */
  127. #ifdef SIGHUP
  128. signal(SIGHUP, poked);
  129. #endif
  130. signal(SIGINT, poked);
  131. #ifdef SIGQUIT
  132. signal(SIGQUIT, poked);
  133. #endif
  134. signal(SIGTERM, poked);
  135. #endif /* HAVE_SIGNAL_H */
  136. /* Show the list of available drivers */
  137. SDL_Log("Available audio drivers:");
  138. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  139. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  140. }
  141. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  142. #ifdef __EMSCRIPTEN__
  143. emscripten_set_main_loop(loop, 0, 1);
  144. #else
  145. while (!done) {
  146. SDL_Delay(100);
  147. iteration();
  148. }
  149. #endif
  150. /* Clean up on signal */
  151. SDL_FreeWAV(sound);
  152. SDL_Quit();
  153. return (0);
  154. }
  155. /* vi: set ts=4 sw=4 expandtab: */