loopbackaudio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright (C) 1997-2017 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 record audio from the microphone and play it back immediately */
  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_AudioDeviceID device;
  22. static SDL_AudioDeviceID capture;
  23. static void SDLCALL
  24. fillerup(void *unused, Uint8 * stream, int len)
  25. {
  26. SDL_QueueAudio(device, stream, len);
  27. }
  28. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  29. static void
  30. quit(int rc)
  31. {
  32. SDL_Quit();
  33. exit(rc);
  34. }
  35. static void
  36. close_audio()
  37. {
  38. if (capture != 0) {
  39. SDL_CloseAudioDevice(capture);
  40. capture = 0;
  41. }
  42. if (device != 0) {
  43. SDL_CloseAudioDevice(device);
  44. device = 0;
  45. }
  46. }
  47. static void
  48. open_audio()
  49. {
  50. SDL_AudioSpec spec;
  51. SDL_zero(spec);
  52. spec.freq = 44100;
  53. spec.format = AUDIO_S16;
  54. spec.channels = 1;
  55. spec.samples = 1024;
  56. spec.callback = fillerup;
  57. capture = SDL_OpenAudioDevice(NULL, SDL_TRUE, &spec, &spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
  58. if (!capture) {
  59. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open microphone: %s\n", SDL_GetError());
  60. quit(2);
  61. }
  62. spec.callback = NULL;
  63. device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, NULL, 0);
  64. if (!device) {
  65. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  66. quit(2);
  67. }
  68. /* Let the audio run */
  69. SDL_PauseAudioDevice(capture, SDL_FALSE);
  70. SDL_PauseAudioDevice(device, SDL_FALSE);
  71. }
  72. static void reopen_audio()
  73. {
  74. close_audio();
  75. open_audio();
  76. }
  77. static int done = 0;
  78. void
  79. poked(int sig)
  80. {
  81. done = 1;
  82. }
  83. #ifdef __EMSCRIPTEN__
  84. void
  85. loop()
  86. {
  87. if(done || (SDL_GetAudioDeviceStatus(capture) != SDL_AUDIO_PLAYING) || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING))
  88. emscripten_cancel_main_loop();
  89. }
  90. #endif
  91. int
  92. main(int argc, char *argv[])
  93. {
  94. int i;
  95. /* Enable standard application logging */
  96. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  97. /* Load the SDL library */
  98. if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS) < 0) {
  99. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  100. return (1);
  101. }
  102. /* Show the list of available drivers */
  103. SDL_Log("Available audio drivers:");
  104. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  105. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  106. }
  107. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  108. open_audio();
  109. SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
  110. #ifdef __EMSCRIPTEN__
  111. emscripten_set_main_loop(loop, 0, 1);
  112. #else
  113. while (!done) {
  114. SDL_Event event;
  115. while (SDL_PollEvent(&event) > 0) {
  116. if (event.type == SDL_QUIT) {
  117. done = 1;
  118. }
  119. if (event.type == SDL_AUDIODEVICEADDED ||
  120. (event.type == SDL_AUDIODEVICEREMOVED && event.adevice.iscapture && event.adevice.which == capture) ||
  121. (event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
  122. reopen_audio();
  123. }
  124. }
  125. SDL_Delay(100);
  126. }
  127. #endif
  128. /* Clean up on signal */
  129. close_audio();
  130. SDL_Quit();
  131. return (0);
  132. }
  133. /* vi: set ts=4 sw=4 expandtab: */