Added an audio recording test program
authorSam Lantinga <slouken@libsdl.org>
Wed, 15 Mar 2017 11:39:54 -0700
changeset 1093890e5cb81c483
parent 10937 9ff840e45cfb
child 10939 e212b4d00d84
Added an audio recording test program
test/Makefile.in
test/README
test/loopbackaudio.c
     1.1 --- a/test/Makefile.in	Tue Mar 14 07:22:08 2017 -0700
     1.2 +++ b/test/Makefile.in	Wed Mar 15 11:39:54 2017 -0700
     1.3 @@ -9,6 +9,7 @@
     1.4  
     1.5  TARGETS = \
     1.6  	checkkeys$(EXE) \
     1.7 +	loopbackaudio$(EXE) \
     1.8  	loopwave$(EXE) \
     1.9  	loopwavequeue$(EXE) \
    1.10  	testatomic$(EXE) \
    1.11 @@ -75,6 +76,9 @@
    1.12  checkkeys$(EXE): $(srcdir)/checkkeys.c
    1.13  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.14  
    1.15 +loopbackaudio$(EXE): $(srcdir)/loopbackaudio.c
    1.16 +	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.17 +
    1.18  loopwave$(EXE): $(srcdir)/loopwave.c
    1.19  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.20  
     2.1 --- a/test/README	Tue Mar 14 07:22:08 2017 -0700
     2.2 +++ b/test/README	Wed Mar 15 11:39:54 2017 -0700
     2.3 @@ -2,6 +2,7 @@
     2.4  These are test programs for the SDL library:
     2.5  
     2.6  	checkkeys	Watch the key events to check the keyboard
     2.7 +	loopbackaudio	Audio test -- record from the microphone and play it back immediately
     2.8  	loopwave	Audio test -- loop playing a WAV file
     2.9  	loopwavequeue	Audio test -- loop playing a WAV file with SDL_QueueAudio
    2.10  	testaudioinfo	Lists audio device capabilities
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/loopbackaudio.c	Wed Mar 15 11:39:54 2017 -0700
     3.3 @@ -0,0 +1,166 @@
     3.4 +/*
     3.5 +  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     3.6 +
     3.7 +  This software is provided 'as-is', without any express or implied
     3.8 +  warranty.  In no event will the authors be held liable for any damages
     3.9 +  arising from the use of this software.
    3.10 +
    3.11 +  Permission is granted to anyone to use this software for any purpose,
    3.12 +  including commercial applications, and to alter it and redistribute it
    3.13 +  freely.
    3.14 +*/
    3.15 +
    3.16 +/* Program to record audio from the microphone and play it back immediately */
    3.17 +
    3.18 +#include "SDL_config.h"
    3.19 +
    3.20 +#include <stdio.h>
    3.21 +#include <stdlib.h>
    3.22 +
    3.23 +#if HAVE_SIGNAL_H
    3.24 +#include <signal.h>
    3.25 +#endif
    3.26 +
    3.27 +#ifdef __EMSCRIPTEN__
    3.28 +#include <emscripten/emscripten.h>
    3.29 +#endif
    3.30 +
    3.31 +#include "SDL.h"
    3.32 +
    3.33 +static SDL_AudioDeviceID device;
    3.34 +static SDL_AudioDeviceID capture;
    3.35 +
    3.36 +static void SDLCALL
    3.37 +fillerup(void *unused, Uint8 * stream, int len)
    3.38 +{
    3.39 +	SDL_QueueAudio(device, stream, len);
    3.40 +}
    3.41 +
    3.42 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    3.43 +static void
    3.44 +quit(int rc)
    3.45 +{
    3.46 +    SDL_Quit();
    3.47 +    exit(rc);
    3.48 +}
    3.49 +
    3.50 +static void
    3.51 +close_audio()
    3.52 +{
    3.53 +    if (capture != 0) {
    3.54 +        SDL_CloseAudioDevice(capture);
    3.55 +        capture = 0;
    3.56 +    }
    3.57 +    if (device != 0) {
    3.58 +        SDL_CloseAudioDevice(device);
    3.59 +        device = 0;
    3.60 +    }
    3.61 +}
    3.62 +
    3.63 +static void
    3.64 +open_audio()
    3.65 +{
    3.66 +    SDL_AudioSpec spec;
    3.67 +
    3.68 +    SDL_zero(spec);
    3.69 +    spec.freq = 44100;
    3.70 +    spec.format = AUDIO_S16;
    3.71 +    spec.channels = 1;
    3.72 +    spec.samples = 1024;
    3.73 +    spec.callback = fillerup;
    3.74 +
    3.75 +	capture = SDL_OpenAudioDevice(NULL, SDL_TRUE, &spec, &spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
    3.76 +	if (!capture) {
    3.77 +		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open microphone: %s\n", SDL_GetError());
    3.78 +		quit(2);
    3.79 +	}
    3.80 +
    3.81 +	spec.callback = NULL;
    3.82 +
    3.83 +	device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, NULL, 0);
    3.84 +    if (!device) {
    3.85 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
    3.86 +        quit(2);
    3.87 +    }
    3.88 +
    3.89 +	/* Let the audio run */
    3.90 +	SDL_PauseAudioDevice(capture, SDL_FALSE);
    3.91 +    SDL_PauseAudioDevice(device, SDL_FALSE);
    3.92 +}
    3.93 +
    3.94 +static void reopen_audio()
    3.95 +{
    3.96 +    close_audio();
    3.97 +    open_audio();
    3.98 +}
    3.99 +
   3.100 +
   3.101 +static int done = 0;
   3.102 +void
   3.103 +poked(int sig)
   3.104 +{
   3.105 +    done = 1;
   3.106 +}
   3.107 +
   3.108 +#ifdef __EMSCRIPTEN__
   3.109 +void
   3.110 +loop()
   3.111 +{
   3.112 +    if(done || (SDL_GetAudioDeviceStatus(capture) != SDL_AUDIO_PLAYING) || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING))
   3.113 +        emscripten_cancel_main_loop();
   3.114 +}
   3.115 +#endif
   3.116 +
   3.117 +int
   3.118 +main(int argc, char *argv[])
   3.119 +{
   3.120 +    int i;
   3.121 +
   3.122 +    /* Enable standard application logging */
   3.123 +    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   3.124 +
   3.125 +    /* Load the SDL library */
   3.126 +    if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS) < 0) {
   3.127 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   3.128 +        return (1);
   3.129 +    }
   3.130 +
   3.131 +    /* Show the list of available drivers */
   3.132 +    SDL_Log("Available audio drivers:");
   3.133 +    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   3.134 +        SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
   3.135 +    }
   3.136 +
   3.137 +    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   3.138 +
   3.139 +    open_audio();
   3.140 +
   3.141 +    SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
   3.142 +
   3.143 +#ifdef __EMSCRIPTEN__
   3.144 +    emscripten_set_main_loop(loop, 0, 1);
   3.145 +#else
   3.146 +    while (!done) {
   3.147 +        SDL_Event event;
   3.148 +
   3.149 +        while (SDL_PollEvent(&event) > 0) {
   3.150 +            if (event.type == SDL_QUIT) {
   3.151 +                done = 1;
   3.152 +            }
   3.153 +            if (event.type == SDL_AUDIODEVICEADDED ||
   3.154 +                (event.type == SDL_AUDIODEVICEREMOVED && event.adevice.iscapture && event.adevice.which == capture) ||
   3.155 +                (event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
   3.156 +                reopen_audio();
   3.157 +            }
   3.158 +        }
   3.159 +        SDL_Delay(100);
   3.160 +    }
   3.161 +#endif
   3.162 +
   3.163 +    /* Clean up on signal */
   3.164 +    close_audio();
   3.165 +    SDL_Quit();
   3.166 +    return (0);
   3.167 +}
   3.168 +
   3.169 +/* vi: set ts=4 sw=4 expandtab: */