test/testaudiocapture.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 03 Aug 2016 00:31:08 -0400
changeset 10246 d7ae395db9f2
parent 10244 656799817375
child 10261 e8facf18d5bd
permissions -rw-r--r--
testaudiocapture: made test app interactive.

(hold down mouse/finger to record, then it plays back what it heard. Repeat.)
icculus@10231
     1
/*
icculus@10231
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
icculus@10231
     3
icculus@10231
     4
  This software is provided 'as-is', without any express or implied
icculus@10231
     5
  warranty.  In no event will the authors be held liable for any damages
icculus@10231
     6
  arising from the use of this software.
icculus@10231
     7
icculus@10231
     8
  Permission is granted to anyone to use this software for any purpose,
icculus@10231
     9
  including commercial applications, and to alter it and redistribute it
icculus@10231
    10
  freely.
icculus@10231
    11
*/
icculus@10231
    12
#include "SDL.h"
icculus@10231
    13
icculus@10231
    14
#ifdef __EMSCRIPTEN__
icculus@10231
    15
#include <emscripten/emscripten.h>
icculus@10231
    16
#endif
icculus@10231
    17
icculus@10244
    18
static SDL_Window *window = NULL;
icculus@10244
    19
static SDL_Renderer *renderer = NULL;
icculus@10231
    20
static SDL_AudioSpec spec;
icculus@10246
    21
static SDL_AudioDeviceID devid_in = 0;
icculus@10246
    22
static SDL_AudioDeviceID devid_out = 0;
icculus@10231
    23
icculus@10231
    24
void SDLCALL
icculus@10231
    25
capture_callback(void *arg, Uint8 * stream, int len)
icculus@10231
    26
{
icculus@10246
    27
    SDL_QueueAudio(devid_out, stream, len);
icculus@10231
    28
}
icculus@10231
    29
icculus@10231
    30
static void
icculus@10231
    31
loop()
icculus@10231
    32
{
icculus@10246
    33
    SDL_bool please_quit = SDL_FALSE;
icculus@10231
    34
    SDL_Event e;
icculus@10231
    35
icculus@10231
    36
    while (SDL_PollEvent(&e)) {
icculus@10231
    37
        if (e.type == SDL_QUIT) {
icculus@10231
    38
            please_quit = SDL_TRUE;
icculus@10246
    39
        } else if (e.type == SDL_KEYDOWN) {
icculus@10246
    40
            if (e.key.keysym.sym == SDLK_ESCAPE) {
icculus@10246
    41
                please_quit = SDL_TRUE;
icculus@10246
    42
            }
icculus@10246
    43
        } else if (e.type == SDL_MOUSEBUTTONDOWN) {
icculus@10246
    44
            if (e.button.button == 1) {
icculus@10246
    45
                SDL_PauseAudioDevice(devid_out, SDL_TRUE);
icculus@10246
    46
                SDL_PauseAudioDevice(devid_in, SDL_FALSE);
icculus@10246
    47
            }
icculus@10246
    48
        } else if (e.type == SDL_MOUSEBUTTONUP) {
icculus@10246
    49
            if (e.button.button == 1) {
icculus@10246
    50
                SDL_PauseAudioDevice(devid_in, SDL_TRUE);
icculus@10246
    51
                SDL_PauseAudioDevice(devid_out, SDL_FALSE);
icculus@10246
    52
            }
icculus@10231
    53
        }
icculus@10231
    54
    }
icculus@10231
    55
icculus@10246
    56
    if (SDL_GetAudioDeviceStatus(devid_in) == SDL_AUDIO_PLAYING) {
icculus@10244
    57
        SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
icculus@10244
    58
    } else {
icculus@10244
    59
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
icculus@10244
    60
    }
icculus@10244
    61
    SDL_RenderClear(renderer);
icculus@10244
    62
    SDL_RenderPresent(renderer);
icculus@10231
    63
icculus@10231
    64
    if (please_quit) {
icculus@10231
    65
        /* stop playing back, quit. */
icculus@10231
    66
        SDL_Log("Shutting down.\n");
icculus@10246
    67
        SDL_PauseAudioDevice(devid_in, 1);
icculus@10246
    68
        SDL_CloseAudioDevice(devid_in);
icculus@10246
    69
        SDL_PauseAudioDevice(devid_out, 1);
icculus@10246
    70
        SDL_CloseAudioDevice(devid_out);
icculus@10246
    71
        SDL_DestroyRenderer(renderer);
icculus@10246
    72
        SDL_DestroyWindow(window);
icculus@10231
    73
        SDL_Quit();
icculus@10231
    74
        #ifdef __EMSCRIPTEN__
icculus@10231
    75
        emscripten_cancel_main_loop();
icculus@10231
    76
        #endif
icculus@10231
    77
        exit(0);
icculus@10231
    78
    }
icculus@10231
    79
}
icculus@10231
    80
icculus@10231
    81
int
icculus@10231
    82
main(int argc, char **argv)
icculus@10231
    83
{
icculus@10237
    84
    /* (argv[1] == NULL means "open default device.") */
icculus@10237
    85
    const char *devname = argv[1];
icculus@10237
    86
    int devcount;
icculus@10237
    87
    int i;
icculus@10237
    88
icculus@10231
    89
    /* Enable standard application logging */
icculus@10231
    90
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
icculus@10231
    91
icculus@10231
    92
    /* Load the SDL library */
icculus@10231
    93
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
icculus@10231
    94
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
icculus@10231
    95
        return (1);
icculus@10231
    96
    }
icculus@10231
    97
icculus@10244
    98
    window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, SDL_WINDOW_FULLSCREEN_DESKTOP);
icculus@10244
    99
    renderer = SDL_CreateRenderer(window, -1, 0);
icculus@10244
   100
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
icculus@10244
   101
    SDL_RenderClear(renderer);
icculus@10244
   102
    SDL_RenderPresent(renderer);
icculus@10231
   103
icculus@10231
   104
    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
icculus@10231
   105
icculus@10237
   106
    devcount = SDL_GetNumAudioDevices(SDL_TRUE);
icculus@10237
   107
    for (i = 0; i < devcount; i++) {
icculus@10237
   108
        SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
icculus@10237
   109
    }
icculus@10237
   110
icculus@10231
   111
    SDL_zero(spec);
icculus@10231
   112
    spec.freq = 44100;
icculus@10231
   113
    spec.format = AUDIO_F32SYS;
icculus@10231
   114
    spec.channels = 1;
icculus@10231
   115
    spec.samples = 1024;
icculus@10231
   116
    spec.callback = capture_callback;
icculus@10231
   117
icculus@10246
   118
    SDL_Log("Opening capture device %s%s%s...\n",
icculus@10237
   119
            devname ? "'" : "",
icculus@10237
   120
            devname ? devname : "[[default]]",
icculus@10237
   121
            devname ? "'" : "");
icculus@10237
   122
icculus@10246
   123
    devid_in = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
icculus@10246
   124
    if (!devid_in) {
icculus@10231
   125
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
icculus@10231
   126
        SDL_Quit();
icculus@10231
   127
        exit(1);
icculus@10231
   128
    }
icculus@10231
   129
icculus@10246
   130
    SDL_Log("Opening default playback device...\n");
icculus@10246
   131
    spec.callback = NULL;
icculus@10246
   132
    devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, &spec, 0);
icculus@10246
   133
    if (!devid_out) {
icculus@10246
   134
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
icculus@10246
   135
        SDL_Quit();
icculus@10246
   136
        exit(1);
icculus@10246
   137
    }
icculus@10246
   138
icculus@10246
   139
    SDL_Log("Ready! Hold down mouse or finger to record!\n");
icculus@10231
   140
icculus@10231
   141
#ifdef __EMSCRIPTEN__
icculus@10231
   142
    emscripten_set_main_loop(loop, 0, 1);
icculus@10231
   143
#else
icculus@10231
   144
    while (1) { loop(); SDL_Delay(16); }
icculus@10231
   145
#endif
icculus@10231
   146
icculus@10231
   147
    return 0;
icculus@10231
   148
}
icculus@10231
   149