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