test/testaudiocapture.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 06 Aug 2016 02:48:00 -0400
changeset 10263 ec1844380edb
parent 10261 e8facf18d5bd
child 10272 2b8bb76140a5
permissions -rw-r--r--
testaudiocapture: use capture device buffer queueing, for better test coverage.
     1 /*
     2   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 #include "SDL.h"
    13 
    14 #ifdef __EMSCRIPTEN__
    15 #include <emscripten/emscripten.h>
    16 #endif
    17 
    18 static SDL_Window *window = NULL;
    19 static SDL_Renderer *renderer = NULL;
    20 static SDL_AudioSpec spec;
    21 static SDL_AudioDeviceID devid_in = 0;
    22 static SDL_AudioDeviceID devid_out = 0;
    23 
    24 static void
    25 loop()
    26 {
    27     SDL_bool please_quit = SDL_FALSE;
    28     SDL_Event e;
    29 
    30     while (SDL_PollEvent(&e)) {
    31         if (e.type == SDL_QUIT) {
    32             please_quit = SDL_TRUE;
    33         } else if (e.type == SDL_KEYDOWN) {
    34             if (e.key.keysym.sym == SDLK_ESCAPE) {
    35                 please_quit = SDL_TRUE;
    36             }
    37         } else if (e.type == SDL_MOUSEBUTTONDOWN) {
    38             if (e.button.button == 1) {
    39                 SDL_PauseAudioDevice(devid_out, SDL_TRUE);
    40                 SDL_PauseAudioDevice(devid_in, SDL_FALSE);
    41             }
    42         } else if (e.type == SDL_MOUSEBUTTONUP) {
    43             if (e.button.button == 1) {
    44                 SDL_PauseAudioDevice(devid_in, SDL_TRUE);
    45                 SDL_PauseAudioDevice(devid_out, SDL_FALSE);
    46             }
    47         }
    48     }
    49 
    50     if (SDL_GetAudioDeviceStatus(devid_in) == SDL_AUDIO_PLAYING) {
    51         SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
    52     } else {
    53         SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    54     }
    55     SDL_RenderClear(renderer);
    56     SDL_RenderPresent(renderer);
    57 
    58     if (please_quit) {
    59         /* stop playing back, quit. */
    60         SDL_Log("Shutting down.\n");
    61         SDL_PauseAudioDevice(devid_in, 1);
    62         SDL_CloseAudioDevice(devid_in);
    63         SDL_PauseAudioDevice(devid_out, 1);
    64         SDL_CloseAudioDevice(devid_out);
    65         SDL_DestroyRenderer(renderer);
    66         SDL_DestroyWindow(window);
    67         SDL_Quit();
    68         #ifdef __EMSCRIPTEN__
    69         emscripten_cancel_main_loop();
    70         #endif
    71         exit(0);
    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     }
    85 }
    86 
    87 int
    88 main(int argc, char **argv)
    89 {
    90     /* (argv[1] == NULL means "open default device.") */
    91     const char *devname = argv[1];
    92     int devcount;
    93     int i;
    94 
    95     /* Enable standard application logging */
    96     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    97 
    98     /* Load the SDL library */
    99     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
   100         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   101         return (1);
   102     }
   103 
   104     window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
   105     renderer = SDL_CreateRenderer(window, -1, 0);
   106     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
   107     SDL_RenderClear(renderer);
   108     SDL_RenderPresent(renderer);
   109 
   110     SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   111 
   112     devcount = SDL_GetNumAudioDevices(SDL_TRUE);
   113     for (i = 0; i < devcount; i++) {
   114         SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
   115     }
   116 
   117     SDL_zero(spec);
   118     spec.freq = 44100;
   119     spec.format = AUDIO_F32SYS;
   120     spec.channels = 1;
   121     spec.samples = 1024;
   122     spec.callback = NULL;
   123 
   124     SDL_Log("Opening capture device %s%s%s...\n",
   125             devname ? "'" : "",
   126             devname ? devname : "[[default]]",
   127             devname ? "'" : "");
   128 
   129     devid_in = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
   130     if (!devid_in) {
   131         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
   132         SDL_Quit();
   133         exit(1);
   134     }
   135 
   136     SDL_Log("Opening default playback device...\n");
   137     devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, &spec, 0);
   138     if (!devid_out) {
   139         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
   140         SDL_Quit();
   141         exit(1);
   142     }
   143 
   144     SDL_Log("Ready! Hold down mouse or finger to record!\n");
   145 
   146 #ifdef __EMSCRIPTEN__
   147     emscripten_set_main_loop(loop, 0, 1);
   148 #else
   149     while (1) { loop(); SDL_Delay(16); }
   150 #endif
   151 
   152     return 0;
   153 }
   154