test/testaudiocapture.c
changeset 10231 4b0893d63092
child 10237 0d344873cd93
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testaudiocapture.c	Mon Aug 01 00:18:56 2016 -0400
     1.3 @@ -0,0 +1,161 @@
     1.4 +/*
     1.5 +  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     1.6 +
     1.7 +  This software is provided 'as-is', without any express or implied
     1.8 +  warranty.  In no event will the authors be held liable for any damages
     1.9 +  arising from the use of this software.
    1.10 +
    1.11 +  Permission is granted to anyone to use this software for any purpose,
    1.12 +  including commercial applications, and to alter it and redistribute it
    1.13 +  freely.
    1.14 +*/
    1.15 +#include "SDL.h"
    1.16 +
    1.17 +#ifdef __EMSCRIPTEN__
    1.18 +#include <emscripten/emscripten.h>
    1.19 +#endif
    1.20 +
    1.21 +#define CAPTURE_SECONDS 5
    1.22 +
    1.23 +static SDL_AudioSpec spec;
    1.24 +static Uint8 *sound = NULL;     /* Pointer to wave data */
    1.25 +static Uint32 soundlen = 0;     /* Length of wave data */
    1.26 +static Uint32 processed = 0;
    1.27 +static SDL_AudioDeviceID devid = 0;
    1.28 +
    1.29 +void SDLCALL
    1.30 +capture_callback(void *arg, Uint8 * stream, int len)
    1.31 +{
    1.32 +    const int avail = (int) (soundlen - processed);
    1.33 +    if (len > avail) {
    1.34 +        len = avail;
    1.35 +    }
    1.36 +
    1.37 +    /*SDL_Log("CAPTURE CALLBACK: %d more bytes\n", len);*/
    1.38 +    SDL_memcpy(sound + processed, stream, len);
    1.39 +    processed += len;
    1.40 +}
    1.41 +
    1.42 +void SDLCALL
    1.43 +play_callback(void *arg, Uint8 * stream, int len)
    1.44 +{
    1.45 +    const Uint8 *waveptr = sound + processed;
    1.46 +    const int avail = soundlen - processed;
    1.47 +    int cpy = len;
    1.48 +    if (cpy > avail) {
    1.49 +        cpy = avail;
    1.50 +    }
    1.51 +
    1.52 +    /*SDL_Log("PLAY CALLBACK: %d more bytes\n", cpy);*/
    1.53 +    SDL_memcpy(stream, waveptr, cpy);
    1.54 +    processed += cpy;
    1.55 +
    1.56 +    len -= cpy;
    1.57 +    if (len > 0) {
    1.58 +        SDL_memset(stream + cpy, spec.silence, len);
    1.59 +    }
    1.60 +}
    1.61 +
    1.62 +static void
    1.63 +loop()
    1.64 +{
    1.65 +    SDL_Event e;
    1.66 +    SDL_bool please_quit = SDL_FALSE;
    1.67 +
    1.68 +    while (SDL_PollEvent(&e)) {
    1.69 +        if (e.type == SDL_QUIT) {
    1.70 +            please_quit = SDL_TRUE;
    1.71 +        }
    1.72 +    }
    1.73 +
    1.74 +    if ((!please_quit) && (processed >= soundlen)) {
    1.75 +        processed = 0;
    1.76 +        if (spec.callback == capture_callback) {
    1.77 +            SDL_Log("Done recording, playing back...\n");
    1.78 +            SDL_PauseAudioDevice(devid, 1);
    1.79 +            SDL_CloseAudioDevice(devid);
    1.80 +
    1.81 +            spec.callback = play_callback;
    1.82 +            devid = SDL_OpenAudioDevice(NULL, 0, &spec, &spec, 0);
    1.83 +            if (!devid) {
    1.84 +                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback!\n");
    1.85 +                SDL_Quit();
    1.86 +                exit(1);
    1.87 +            }
    1.88 +
    1.89 +            SDL_PauseAudioDevice(devid, 0);
    1.90 +        } else {
    1.91 +            SDL_Log("Done playing back.\n");
    1.92 +            please_quit = SDL_TRUE;
    1.93 +        }
    1.94 +    }
    1.95 +
    1.96 +    if (please_quit) {
    1.97 +        /* stop playing back, quit. */
    1.98 +        SDL_Log("Shutting down.\n");
    1.99 +        SDL_PauseAudioDevice(devid, 1);
   1.100 +        SDL_CloseAudioDevice(devid);
   1.101 +        SDL_free(sound);
   1.102 +        sound = NULL;
   1.103 +        SDL_Quit();
   1.104 +        #ifdef __EMSCRIPTEN__
   1.105 +        emscripten_cancel_main_loop();
   1.106 +        #endif
   1.107 +        exit(0);
   1.108 +    }
   1.109 +}
   1.110 +
   1.111 +int
   1.112 +main(int argc, char **argv)
   1.113 +{
   1.114 +    /* Enable standard application logging */
   1.115 +    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   1.116 +
   1.117 +    /* Load the SDL library */
   1.118 +    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
   1.119 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.120 +        return (1);
   1.121 +    }
   1.122 +
   1.123 +    /* Android apparently needs a window...? */
   1.124 +    #ifdef __ANDROID__  
   1.125 +    SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
   1.126 +    #endif
   1.127 +
   1.128 +    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   1.129 +
   1.130 +    SDL_zero(spec);
   1.131 +    spec.freq = 44100;
   1.132 +    spec.format = AUDIO_F32SYS;
   1.133 +    spec.channels = 1;
   1.134 +    spec.samples = 1024;
   1.135 +    spec.callback = capture_callback;
   1.136 +
   1.137 +    soundlen = spec.freq * (SDL_AUDIO_BITSIZE(spec.format) / 8) * spec.channels * CAPTURE_SECONDS;
   1.138 +    sound = (Uint8 *) SDL_malloc(soundlen);
   1.139 +    if (!sound) {
   1.140 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
   1.141 +        SDL_Quit();
   1.142 +        return 1;
   1.143 +    }
   1.144 +
   1.145 +    devid = SDL_OpenAudioDevice(NULL, 1, &spec, &spec, 0);
   1.146 +    if (!devid) {
   1.147 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
   1.148 +        SDL_free(sound);
   1.149 +        SDL_Quit();
   1.150 +        exit(1);
   1.151 +    }
   1.152 +
   1.153 +    SDL_Log("Recording for %d seconds...\n", CAPTURE_SECONDS);
   1.154 +    SDL_PauseAudioDevice(devid, 0);
   1.155 +
   1.156 +#ifdef __EMSCRIPTEN__
   1.157 +    emscripten_set_main_loop(loop, 0, 1);
   1.158 +#else
   1.159 +    while (1) { loop(); SDL_Delay(16); }
   1.160 +#endif
   1.161 +
   1.162 +    return 0;
   1.163 +}
   1.164 +