test/testaudiocapture.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 09 Aug 2016 16:57:49 -0400
changeset 10254 2b8bb76140a5
parent 10245 ec1844380edb
child 10267 b1f031450499
permissions -rw-r--r--
testaudiocapture: open capture device to same spec as output device. ...since our resampler is still terrible (sorry!).
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
10254
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
    14
#include <stdlib.h>
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
    15
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#ifdef __EMSCRIPTEN__
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#include <emscripten/emscripten.h>
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#endif
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
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
    21
static SDL_Renderer *renderer = NULL;
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
static SDL_AudioSpec spec;
10228
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    23
static SDL_AudioDeviceID devid_in = 0;
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    24
static SDL_AudioDeviceID devid_out = 0;
10213
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
static void
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
loop()
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
{
10228
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    29
    SDL_bool please_quit = SDL_FALSE;
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    SDL_Event e;
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
    while (SDL_PollEvent(&e)) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
        if (e.type == SDL_QUIT) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
            please_quit = SDL_TRUE;
10228
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    35
        } else if (e.type == SDL_KEYDOWN) {
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    36
            if (e.key.keysym.sym == SDLK_ESCAPE) {
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    37
                please_quit = SDL_TRUE;
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    38
            }
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    39
        } else if (e.type == SDL_MOUSEBUTTONDOWN) {
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    40
            if (e.button.button == 1) {
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    41
                SDL_PauseAudioDevice(devid_out, SDL_TRUE);
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    42
                SDL_PauseAudioDevice(devid_in, SDL_FALSE);
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    43
            }
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    44
        } else if (e.type == SDL_MOUSEBUTTONUP) {
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    45
            if (e.button.button == 1) {
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    46
                SDL_PauseAudioDevice(devid_in, SDL_TRUE);
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    47
                SDL_PauseAudioDevice(devid_out, SDL_FALSE);
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    48
            }
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
        }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
10228
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    52
    if (SDL_GetAudioDeviceStatus(devid_in) == SDL_AUDIO_PLAYING) {
10226
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    53
        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
    54
    } else {
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    55
        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
    56
    }
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    57
    SDL_RenderClear(renderer);
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
    58
    SDL_RenderPresent(renderer);
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    if (please_quit) {
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        /* stop playing back, quit. */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
        SDL_Log("Shutting down.\n");
10228
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    63
        SDL_PauseAudioDevice(devid_in, 1);
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    64
        SDL_CloseAudioDevice(devid_in);
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    65
        SDL_PauseAudioDevice(devid_out, 1);
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    66
        SDL_CloseAudioDevice(devid_out);
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    67
        SDL_DestroyRenderer(renderer);
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
    68
        SDL_DestroyWindow(window);
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
        SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
        #ifdef __EMSCRIPTEN__
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
        emscripten_cancel_main_loop();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
        #endif
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
        exit(0);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    }
10245
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    75
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    76
    /* Note that it would be easier to just have a one-line function that
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    77
        calls SDL_QueueAudio() as a capture device callback, but we're
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    78
        trying to test the API, so we use SDL_DequeueAudio() here. */
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    79
    while (SDL_TRUE) {
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    80
        Uint8 buf[1024];
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    81
        const Uint32 br = SDL_DequeueAudio(devid_in, buf, sizeof (buf));
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    82
        SDL_QueueAudio(devid_out, buf, br);
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    83
        if (br < sizeof (buf)) {
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    84
            break;
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    85
        }
ec1844380edb testaudiocapture: use capture device buffer queueing, for better test coverage.
Ryan C. Gordon <icculus@icculus.org>
parents: 10243
diff changeset
    86
    }
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
}
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
int
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
main(int argc, char **argv)
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
{
10219
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
    92
    /* (argv[1] == NULL means "open default device.") */
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
    93
    const char *devname = argv[1];
10254
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
    94
    SDL_AudioSpec wanted;
10219
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
    95
    int devcount;
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
    96
    int i;
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
    97
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    /* Enable standard application logging */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    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
   100
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    /* Load the SDL library */
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    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
   103
        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
   104
        return (1);
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
10243
e8facf18d5bd testaudiocapture: don't use fullscreen for the window.
Ryan C. Gordon <icculus@icculus.org>
parents: 10228
diff changeset
   107
    window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
10226
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
   108
    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
   109
    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
   110
    SDL_RenderClear(renderer);
656799817375 testaudiocapture: Make a simple green/red window when recording/playing.
Ryan C. Gordon <icculus@icculus.org>
parents: 10219
diff changeset
   111
    SDL_RenderPresent(renderer);
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    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
   114
10219
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   115
    devcount = SDL_GetNumAudioDevices(SDL_TRUE);
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   116
    for (i = 0; i < devcount; i++) {
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   117
        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
   118
    }
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   119
10254
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   120
    SDL_zero(wanted);
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   121
    wanted.freq = 44100;
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   122
    wanted.format = AUDIO_F32SYS;
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   123
    wanted.channels = 1;
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   124
    wanted.samples = 1024;
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   125
    wanted.callback = NULL;
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   126
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
    SDL_zero(spec);
10254
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   128
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   129
    /* DirectSound can fail in some instances if you open the same hardware
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   130
       for both capture and output and didn't open the output end first,
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   131
       according to the docs, so if you're doing something like this, always
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   132
       open your capture devices second in case you land in those bizarre
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   133
       circumstances. */
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   134
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   135
    SDL_Log("Opening default playback device...\n");
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   136
    devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wanted, &spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   137
    if (!devid_out) {
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   138
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   139
        SDL_Quit();
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   140
        exit(1);
2b8bb76140a5 testaudiocapture: open capture device to same spec as output device.
Ryan C. Gordon <icculus@icculus.org>
parents: 10245
diff changeset
   141
    }
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
10228
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
   143
    SDL_Log("Opening capture device %s%s%s...\n",
10219
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   144
            devname ? "'" : "",
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   145
            devname ? devname : "[[default]]",
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   146
            devname ? "'" : "");
0d344873cd93 testaudiocapture: Let specific devices be opened.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   147
10228
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
   148
    devid_in = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
   149
    if (!devid_in) {
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
        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
   151
        SDL_Quit();
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
        exit(1);
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
    }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
10228
d7ae395db9f2 testaudiocapture: made test app interactive.
Ryan C. Gordon <icculus@icculus.org>
parents: 10226
diff changeset
   155
    SDL_Log("Ready! Hold down mouse or finger to record!\n");
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
#ifdef __EMSCRIPTEN__
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    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
   159
#else
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    while (1) { loop(); SDL_Delay(16); }
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
#endif
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    return 0;
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