test/testaudiocapture.c
changeset 10245 ec1844380edb
parent 10243 e8facf18d5bd
child 10254 2b8bb76140a5
equal deleted inserted replaced
10244:aaed7b1f783a 10245:ec1844380edb
    18 static SDL_Window *window = NULL;
    18 static SDL_Window *window = NULL;
    19 static SDL_Renderer *renderer = NULL;
    19 static SDL_Renderer *renderer = NULL;
    20 static SDL_AudioSpec spec;
    20 static SDL_AudioSpec spec;
    21 static SDL_AudioDeviceID devid_in = 0;
    21 static SDL_AudioDeviceID devid_in = 0;
    22 static SDL_AudioDeviceID devid_out = 0;
    22 static SDL_AudioDeviceID devid_out = 0;
    23 
       
    24 void SDLCALL
       
    25 capture_callback(void *arg, Uint8 * stream, int len)
       
    26 {
       
    27     SDL_QueueAudio(devid_out, stream, len);
       
    28 }
       
    29 
    23 
    30 static void
    24 static void
    31 loop()
    25 loop()
    32 {
    26 {
    33     SDL_bool please_quit = SDL_FALSE;
    27     SDL_bool please_quit = SDL_FALSE;
    74         #ifdef __EMSCRIPTEN__
    68         #ifdef __EMSCRIPTEN__
    75         emscripten_cancel_main_loop();
    69         emscripten_cancel_main_loop();
    76         #endif
    70         #endif
    77         exit(0);
    71         exit(0);
    78     }
    72     }
       
    73 
       
    74     /* Note that it would be easier to just have a one-line function that
       
    75         calls SDL_QueueAudio() as a capture device callback, but we're
       
    76         trying to test the API, so we use SDL_DequeueAudio() here. */
       
    77     while (SDL_TRUE) {
       
    78         Uint8 buf[1024];
       
    79         const Uint32 br = SDL_DequeueAudio(devid_in, buf, sizeof (buf));
       
    80         SDL_QueueAudio(devid_out, buf, br);
       
    81         if (br < sizeof (buf)) {
       
    82             break;
       
    83         }
       
    84     }
    79 }
    85 }
    80 
    86 
    81 int
    87 int
    82 main(int argc, char **argv)
    88 main(int argc, char **argv)
    83 {
    89 {
   111     SDL_zero(spec);
   117     SDL_zero(spec);
   112     spec.freq = 44100;
   118     spec.freq = 44100;
   113     spec.format = AUDIO_F32SYS;
   119     spec.format = AUDIO_F32SYS;
   114     spec.channels = 1;
   120     spec.channels = 1;
   115     spec.samples = 1024;
   121     spec.samples = 1024;
   116     spec.callback = capture_callback;
   122     spec.callback = NULL;
   117 
   123 
   118     SDL_Log("Opening capture device %s%s%s...\n",
   124     SDL_Log("Opening capture device %s%s%s...\n",
   119             devname ? "'" : "",
   125             devname ? "'" : "",
   120             devname ? devname : "[[default]]",
   126             devname ? devname : "[[default]]",
   121             devname ? "'" : "");
   127             devname ? "'" : "");
   126         SDL_Quit();
   132         SDL_Quit();
   127         exit(1);
   133         exit(1);
   128     }
   134     }
   129 
   135 
   130     SDL_Log("Opening default playback device...\n");
   136     SDL_Log("Opening default playback device...\n");
   131     spec.callback = NULL;
       
   132     devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, &spec, 0);
   137     devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, &spec, 0);
   133     if (!devid_out) {
   138     if (!devid_out) {
   134         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
   139         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
   135         SDL_Quit();
   140         SDL_Quit();
   136         exit(1);
   141         exit(1);