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

165 lines
5.1 KiB

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