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

167 lines
5.1 KiB

  1. /*
  2. Copyright (C) 1997-2023 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 main(int argc, char **argv)
  79. {
  80. /* (argv[1] == NULL means "open default device.") */
  81. const char *devname = argv[1];
  82. SDL_AudioSpec wanted;
  83. int devcount;
  84. int i;
  85. /* Enable standard application logging */
  86. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  87. /* Load the SDL library */
  88. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
  89. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  90. return 1;
  91. }
  92. window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
  93. renderer = SDL_CreateRenderer(window, -1, 0);
  94. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  95. SDL_RenderClear(renderer);
  96. SDL_RenderPresent(renderer);
  97. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  98. devcount = SDL_GetNumAudioDevices(SDL_TRUE);
  99. for (i = 0; i < devcount; i++) {
  100. SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
  101. }
  102. SDL_zero(wanted);
  103. wanted.freq = 44100;
  104. wanted.format = AUDIO_F32SYS;
  105. wanted.channels = 1;
  106. wanted.samples = 4096;
  107. wanted.callback = NULL;
  108. SDL_zero(spec);
  109. /* DirectSound can fail in some instances if you open the same hardware
  110. for both capture and output and didn't open the output end first,
  111. according to the docs, so if you're doing something like this, always
  112. open your capture devices second in case you land in those bizarre
  113. circumstances. */
  114. SDL_Log("Opening default playback device...\n");
  115. devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wanted, &spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
  116. if (!devid_out) {
  117. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback: %s!\n", SDL_GetError());
  118. SDL_Quit();
  119. exit(1);
  120. }
  121. SDL_Log("Opening capture device %s%s%s...\n",
  122. devname ? "'" : "",
  123. devname ? devname : "[[default]]",
  124. devname ? "'" : "");
  125. devid_in = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
  126. if (!devid_in) {
  127. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
  128. SDL_Quit();
  129. exit(1);
  130. }
  131. SDL_Log("Ready! Hold down mouse or finger to record!\n");
  132. #ifdef __EMSCRIPTEN__
  133. emscripten_set_main_loop(loop, 0, 1);
  134. #else
  135. while (1) {
  136. loop();
  137. SDL_Delay(16);
  138. }
  139. #endif
  140. return 0;
  141. }