test/testaudiocapture.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 02 Aug 2016 13:38:56 -0400
changeset 10219 0d344873cd93
parent 10213 4b0893d63092
child 10226 656799817375
permissions -rw-r--r--
testaudiocapture: Let specific devices be opened.
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
{
10219
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   111
    /* (argv[1] == NULL means "open default device.") */
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   112
    const char *devname = argv[1];
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   113
    int devcount;
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   114
    int i;
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   115
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    /* Enable standard application logging */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    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
   118
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    /* Load the SDL library */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    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
   121
        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
   122
        return (1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    }
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
    /* Android apparently needs a window...? */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
    #ifdef __ANDROID__  
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
    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
   128
    #endif
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    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
   131
10219
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   132
    devcount = SDL_GetNumAudioDevices(SDL_TRUE);
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   133
    for (i = 0; i < devcount; i++) {
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   134
        SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   135
    }
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   136
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    SDL_zero(spec);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    spec.freq = 44100;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    spec.format = AUDIO_F32SYS;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    spec.channels = 1;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    spec.samples = 1024;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    spec.callback = capture_callback;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    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
   145
    sound = (Uint8 *) SDL_malloc(soundlen);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    if (!sound) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
        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
   148
        SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
        return 1;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
10219
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   152
    SDL_Log("Opening device %s%s%s...\n",
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   153
            devname ? "'" : "",
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   154
            devname ? devname : "[[default]]",
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   155
            devname ? "'" : "");
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   156
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   157
    devid = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    if (!devid) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
        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
   160
        SDL_free(sound);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
        SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
        exit(1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    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
   166
    SDL_PauseAudioDevice(devid, 0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
#ifdef __EMSCRIPTEN__
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    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
   170
#else
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
    while (1) { loop(); SDL_Delay(16); }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
#endif
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    return 0;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
}
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176