1
0

testaudiocapture.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "SDL.h"
  11. #ifdef __EMSCRIPTEN__
  12. #include <emscripten/emscripten.h>
  13. #endif
  14. static SDL_Window *window = NULL;
  15. static SDL_Renderer *renderer = NULL;
  16. static SDL_AudioSpec spec;
  17. static SDL_AudioDeviceID devid_in = 0;
  18. static SDL_AudioDeviceID devid_out = 0;
  19. void SDLCALL
  20. capture_callback(void *arg, Uint8 * stream, int len)
  21. {
  22. SDL_QueueAudio(devid_out, stream, len);
  23. }
  24. static void
  25. loop()
  26. {
  27. SDL_bool please_quit = SDL_FALSE;
  28. SDL_Event e;
  29. while (SDL_PollEvent(&e)) {
  30. if (e.type == SDL_QUIT) {
  31. please_quit = SDL_TRUE;
  32. } else if (e.type == SDL_KEYDOWN) {
  33. if (e.key.keysym.sym == SDLK_ESCAPE) {
  34. please_quit = SDL_TRUE;
  35. }
  36. } else if (e.type == SDL_MOUSEBUTTONDOWN) {
  37. if (e.button.button == 1) {
  38. SDL_PauseAudioDevice(devid_out, SDL_TRUE);
  39. SDL_PauseAudioDevice(devid_in, SDL_FALSE);
  40. }
  41. } else if (e.type == SDL_MOUSEBUTTONUP) {
  42. if (e.button.button == 1) {
  43. SDL_PauseAudioDevice(devid_in, SDL_TRUE);
  44. SDL_PauseAudioDevice(devid_out, SDL_FALSE);
  45. }
  46. }
  47. }
  48. if (SDL_GetAudioDeviceStatus(devid_in) == SDL_AUDIO_PLAYING) {
  49. SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
  50. } else {
  51. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  52. }
  53. SDL_RenderClear(renderer);
  54. SDL_RenderPresent(renderer);
  55. if (please_quit) {
  56. /* stop playing back, quit. */
  57. SDL_Log("Shutting down.\n");
  58. SDL_PauseAudioDevice(devid_in, 1);
  59. SDL_CloseAudioDevice(devid_in);
  60. SDL_PauseAudioDevice(devid_out, 1);
  61. SDL_CloseAudioDevice(devid_out);
  62. SDL_DestroyRenderer(renderer);
  63. SDL_DestroyWindow(window);
  64. SDL_Quit();
  65. #ifdef __EMSCRIPTEN__
  66. emscripten_cancel_main_loop();
  67. #endif
  68. exit(0);
  69. }
  70. }
  71. int
  72. main(int argc, char **argv)
  73. {
  74. /* (argv[1] == NULL means "open default device.") */
  75. const char *devname = argv[1];
  76. int devcount;
  77. int i;
  78. /* Enable standard application logging */
  79. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  80. /* Load the SDL library */
  81. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
  82. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  83. return (1);
  84. }
  85. window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, SDL_WINDOW_FULLSCREEN_DESKTOP);
  86. renderer = SDL_CreateRenderer(window, -1, 0);
  87. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  88. SDL_RenderClear(renderer);
  89. SDL_RenderPresent(renderer);
  90. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  91. devcount = SDL_GetNumAudioDevices(SDL_TRUE);
  92. for (i = 0; i < devcount; i++) {
  93. SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
  94. }
  95. SDL_zero(spec);
  96. spec.freq = 44100;
  97. spec.format = AUDIO_F32SYS;
  98. spec.channels = 1;
  99. spec.samples = 1024;
  100. spec.callback = capture_callback;
  101. SDL_Log("Opening capture device %s%s%s...\n",
  102. devname ? "'" : "",
  103. devname ? devname : "[[default]]",
  104. devname ? "'" : "");
  105. devid_in = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
  106. if (!devid_in) {
  107. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
  108. SDL_Quit();
  109. exit(1);
  110. }
  111. SDL_Log("Opening default playback device...\n");
  112. spec.callback = NULL;
  113. devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, &spec, 0);
  114. if (!devid_out) {
  115. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
  116. SDL_Quit();
  117. exit(1);
  118. }
  119. SDL_Log("Ready! Hold down mouse or finger to record!\n");
  120. #ifdef __EMSCRIPTEN__
  121. emscripten_set_main_loop(loop, 0, 1);
  122. #else
  123. while (1) { loop(); SDL_Delay(16); }
  124. #endif
  125. return 0;
  126. }