test/testaudiocapture.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 01 Aug 2016 00:18:56 -0400
changeset 10213 4b0893d63092
child 10219 0d344873cd93
permissions -rw-r--r--
audio: Initial bits to enable audio capture support.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
  This software is provided 'as-is', without any express or implied
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
  arising from the use of this software.
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
  freely.
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
*/
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include "SDL.h"
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#ifdef __EMSCRIPTEN__
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include <emscripten/emscripten.h>
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#endif
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#define CAPTURE_SECONDS 5
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
static SDL_AudioSpec spec;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
static Uint8 *sound = NULL;     /* Pointer to wave data */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
static Uint32 soundlen = 0;     /* Length of wave data */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
static Uint32 processed = 0;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
static SDL_AudioDeviceID devid = 0;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
void SDLCALL
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
capture_callback(void *arg, Uint8 * stream, int len)
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
{
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
    const int avail = (int) (soundlen - processed);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    if (len > avail) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
        len = avail;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
    /*SDL_Log("CAPTURE CALLBACK: %d more bytes\n", len);*/
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    SDL_memcpy(sound + processed, stream, len);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    processed += len;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
}
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
void SDLCALL
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
play_callback(void *arg, Uint8 * stream, int len)
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
{
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    const Uint8 *waveptr = sound + processed;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    const int avail = soundlen - processed;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    int cpy = len;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    if (cpy > avail) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
        cpy = avail;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    /*SDL_Log("PLAY CALLBACK: %d more bytes\n", cpy);*/
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    SDL_memcpy(stream, waveptr, cpy);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    processed += cpy;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    len -= cpy;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    if (len > 0) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
        SDL_memset(stream + cpy, spec.silence, len);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
}
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
static void
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
loop()
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
{
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    SDL_Event e;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    SDL_bool please_quit = SDL_FALSE;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    while (SDL_PollEvent(&e)) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
        if (e.type == SDL_QUIT) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
            please_quit = SDL_TRUE;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
        }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    if ((!please_quit) && (processed >= soundlen)) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
        processed = 0;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
        if (spec.callback == capture_callback) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
            SDL_Log("Done recording, playing back...\n");
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
            SDL_PauseAudioDevice(devid, 1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
            SDL_CloseAudioDevice(devid);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
            spec.callback = play_callback;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
            devid = SDL_OpenAudioDevice(NULL, 0, &spec, &spec, 0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
            if (!devid) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback!\n");
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
                SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
                exit(1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
            }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
            SDL_PauseAudioDevice(devid, 0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
        } else {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
            SDL_Log("Done playing back.\n");
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
            please_quit = SDL_TRUE;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
        }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
    if (please_quit) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
        /* stop playing back, quit. */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
        SDL_Log("Shutting down.\n");
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
        SDL_PauseAudioDevice(devid, 1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
        SDL_CloseAudioDevice(devid);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
        SDL_free(sound);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
        sound = NULL;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
        SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
        #ifdef __EMSCRIPTEN__
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
        emscripten_cancel_main_loop();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
        #endif
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
        exit(0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
}
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
int
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
main(int argc, char **argv)
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
{
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    /* Enable standard application logging */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    /* Load the SDL library */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
        return (1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    /* Android apparently needs a window...? */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    #ifdef __ANDROID__  
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    #endif
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
    SDL_zero(spec);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
    spec.freq = 44100;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
    spec.format = AUDIO_F32SYS;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    spec.channels = 1;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    spec.samples = 1024;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    spec.callback = capture_callback;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    soundlen = spec.freq * (SDL_AUDIO_BITSIZE(spec.format) / 8) * spec.channels * CAPTURE_SECONDS;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
    sound = (Uint8 *) SDL_malloc(soundlen);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    if (!sound) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
        SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
        return 1;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    devid = SDL_OpenAudioDevice(NULL, 1, &spec, &spec, 0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    if (!devid) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
        SDL_free(sound);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
        SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
        exit(1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    SDL_Log("Recording for %d seconds...\n", CAPTURE_SECONDS);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    SDL_PauseAudioDevice(devid, 0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
#ifdef __EMSCRIPTEN__
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    emscripten_set_main_loop(loop, 0, 1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
#else
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
    while (1) { loop(); SDL_Delay(16); }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
#endif
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    return 0;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
}
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161