test/testaudiocapture.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 02 Aug 2016 19:17:51 -0400
changeset 10226 656799817375
parent 10219 0d344873cd93
child 10228 d7ae395db9f2
permissions -rw-r--r--
testaudiocapture: Make a simple green/red window when recording/playing.
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
10226
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    20
#define DO_VIDEO defined(__ANDROID__) || defined(__IPHONEOS__) || defined(__EMSCRIPTEN__)
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    21
#if DO_VIDEO
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    22
static SDL_Window *window = NULL;
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    23
static SDL_Renderer *renderer = NULL;
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    24
#endif
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    25
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
static SDL_AudioSpec spec;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
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
    28
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
    29
static Uint32 processed = 0;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
static SDL_AudioDeviceID devid = 0;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
void SDLCALL
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
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
    34
{
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    const int avail = (int) (soundlen - processed);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    if (len > avail) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
        len = avail;
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
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    /*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
    41
    SDL_memcpy(sound + processed, stream, len);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    processed += len;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
}
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
void SDLCALL
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
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
    47
{
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    const Uint8 *waveptr = sound + processed;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    const int avail = soundlen - processed;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    int cpy = len;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    if (cpy > avail) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
        cpy = avail;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    /*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
    56
    SDL_memcpy(stream, waveptr, cpy);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    processed += cpy;
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
    len -= cpy;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    if (len > 0) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        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
    62
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
}
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
static void
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
loop()
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
{
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    SDL_Event e;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    SDL_bool please_quit = SDL_FALSE;
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
    while (SDL_PollEvent(&e)) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
        if (e.type == SDL_QUIT) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
            please_quit = SDL_TRUE;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
        }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
10226
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    77
    #if DO_VIDEO
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    78
    if (spec.callback == capture_callback) {
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    79
        SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    80
    } else {
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    81
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    82
    }
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    83
    SDL_RenderClear(renderer);
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    84
    SDL_RenderPresent(renderer);
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    85
    #endif
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    86
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    if ((!please_quit) && (processed >= soundlen)) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
        processed = 0;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
        if (spec.callback == capture_callback) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
            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
    91
            SDL_PauseAudioDevice(devid, 1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
            SDL_CloseAudioDevice(devid);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
            spec.callback = play_callback;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
            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
    96
            if (!devid) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
                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
    98
                SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
                exit(1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
            }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
            SDL_PauseAudioDevice(devid, 0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
        } else {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
            SDL_Log("Done playing back.\n");
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
            please_quit = SDL_TRUE;
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
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    if (please_quit) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
        /* stop playing back, quit. */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
        SDL_Log("Shutting down.\n");
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
        SDL_PauseAudioDevice(devid, 1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
        SDL_CloseAudioDevice(devid);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
        SDL_free(sound);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
        sound = NULL;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
        SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
        #ifdef __EMSCRIPTEN__
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
        emscripten_cancel_main_loop();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
        #endif
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
        exit(0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
}
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
int
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
main(int argc, char **argv)
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
{
10219
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   127
    /* (argv[1] == NULL means "open default device.") */
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   128
    const char *devname = argv[1];
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   129
    int devcount;
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   130
    int i;
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   131
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    /* Enable standard application logging */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    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
   134
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
    /* Load the SDL library */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    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
   137
        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
   138
        return (1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    }
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
    /* Android apparently needs a window...? */
10226
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
   142
    #if DO_VIDEO
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
   143
    window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, SDL_WINDOW_FULLSCREEN_DESKTOP);
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
   144
    renderer = SDL_CreateRenderer(window, -1, 0);
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
   145
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
   146
    SDL_RenderClear(renderer);
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
   147
    SDL_RenderPresent(renderer);
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    #endif
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("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
   151
10219
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   152
    devcount = SDL_GetNumAudioDevices(SDL_TRUE);
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   153
    for (i = 0; i < devcount; i++) {
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   154
        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
   155
    }
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   156
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    SDL_zero(spec);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    spec.freq = 44100;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    spec.format = AUDIO_F32SYS;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    spec.channels = 1;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    spec.samples = 1024;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    spec.callback = capture_callback;
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
    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
   165
    sound = (Uint8 *) SDL_malloc(soundlen);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
    if (!sound) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
        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
   168
        SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
        return 1;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
10219
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   172
    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
   173
            devname ? "'" : "",
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   174
            devname ? devname : "[[default]]",
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   175
            devname ? "'" : "");
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   176
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   177
    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
   178
    if (!devid) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
        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
   180
        SDL_free(sound);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
        SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
        exit(1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
    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
   186
    SDL_PauseAudioDevice(devid, 0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
#ifdef __EMSCRIPTEN__
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    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
   190
#else
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    while (1) { loop(); SDL_Delay(16); }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
#endif
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    return 0;
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
}
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196