testaudiocapture.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #define CAPTURE_SECONDS 5
  15. static SDL_AudioSpec spec;
  16. static Uint8 *sound = NULL; /* Pointer to wave data */
  17. static Uint32 soundlen = 0; /* Length of wave data */
  18. static Uint32 processed = 0;
  19. static SDL_AudioDeviceID devid = 0;
  20. void SDLCALL
  21. capture_callback(void *arg, Uint8 * stream, int len)
  22. {
  23. const int avail = (int) (soundlen - processed);
  24. if (len > avail) {
  25. len = avail;
  26. }
  27. /*SDL_Log("CAPTURE CALLBACK: %d more bytes\n", len);*/
  28. SDL_memcpy(sound + processed, stream, len);
  29. processed += len;
  30. }
  31. void SDLCALL
  32. play_callback(void *arg, Uint8 * stream, int len)
  33. {
  34. const Uint8 *waveptr = sound + processed;
  35. const int avail = soundlen - processed;
  36. int cpy = len;
  37. if (cpy > avail) {
  38. cpy = avail;
  39. }
  40. /*SDL_Log("PLAY CALLBACK: %d more bytes\n", cpy);*/
  41. SDL_memcpy(stream, waveptr, cpy);
  42. processed += cpy;
  43. len -= cpy;
  44. if (len > 0) {
  45. SDL_memset(stream + cpy, spec.silence, len);
  46. }
  47. }
  48. static void
  49. loop()
  50. {
  51. SDL_Event e;
  52. SDL_bool please_quit = SDL_FALSE;
  53. while (SDL_PollEvent(&e)) {
  54. if (e.type == SDL_QUIT) {
  55. please_quit = SDL_TRUE;
  56. }
  57. }
  58. if ((!please_quit) && (processed >= soundlen)) {
  59. processed = 0;
  60. if (spec.callback == capture_callback) {
  61. SDL_Log("Done recording, playing back...\n");
  62. SDL_PauseAudioDevice(devid, 1);
  63. SDL_CloseAudioDevice(devid);
  64. spec.callback = play_callback;
  65. devid = SDL_OpenAudioDevice(NULL, 0, &spec, &spec, 0);
  66. if (!devid) {
  67. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback!\n");
  68. SDL_Quit();
  69. exit(1);
  70. }
  71. SDL_PauseAudioDevice(devid, 0);
  72. } else {
  73. SDL_Log("Done playing back.\n");
  74. please_quit = SDL_TRUE;
  75. }
  76. }
  77. if (please_quit) {
  78. /* stop playing back, quit. */
  79. SDL_Log("Shutting down.\n");
  80. SDL_PauseAudioDevice(devid, 1);
  81. SDL_CloseAudioDevice(devid);
  82. SDL_free(sound);
  83. sound = NULL;
  84. SDL_Quit();
  85. #ifdef __EMSCRIPTEN__
  86. emscripten_cancel_main_loop();
  87. #endif
  88. exit(0);
  89. }
  90. }
  91. int
  92. main(int argc, char **argv)
  93. {
  94. /* Enable standard application logging */
  95. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  96. /* Load the SDL library */
  97. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
  98. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  99. return (1);
  100. }
  101. /* Android apparently needs a window...? */
  102. #ifdef __ANDROID__
  103. SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
  104. #endif
  105. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  106. SDL_zero(spec);
  107. spec.freq = 44100;
  108. spec.format = AUDIO_F32SYS;
  109. spec.channels = 1;
  110. spec.samples = 1024;
  111. spec.callback = capture_callback;
  112. soundlen = spec.freq * (SDL_AUDIO_BITSIZE(spec.format) / 8) * spec.channels * CAPTURE_SECONDS;
  113. sound = (Uint8 *) SDL_malloc(soundlen);
  114. if (!sound) {
  115. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
  116. SDL_Quit();
  117. return 1;
  118. }
  119. devid = SDL_OpenAudioDevice(NULL, 1, &spec, &spec, 0);
  120. if (!devid) {
  121. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
  122. SDL_free(sound);
  123. SDL_Quit();
  124. exit(1);
  125. }
  126. SDL_Log("Recording for %d seconds...\n", CAPTURE_SECONDS);
  127. SDL_PauseAudioDevice(devid, 0);
  128. #ifdef __EMSCRIPTEN__
  129. emscripten_set_main_loop(loop, 0, 1);
  130. #else
  131. while (1) { loop(); SDL_Delay(16); }
  132. #endif
  133. return 0;
  134. }