testaudiocapture.c 4.6 KB

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