test/testaudiohotplug.c
changeset 9393 ed79a66e57e5
child 9402 bd133a872458
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testaudiohotplug.c	Mon Mar 16 02:11:39 2015 -0400
     1.3 @@ -0,0 +1,182 @@
     1.4 +/*
     1.5 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     1.6 +
     1.7 +  This software is provided 'as-is', without any express or implied
     1.8 +  warranty.  In no event will the authors be held liable for any damages
     1.9 +  arising from the use of this software.
    1.10 +
    1.11 +  Permission is granted to anyone to use this software for any purpose,
    1.12 +  including commercial applications, and to alter it and redistribute it
    1.13 +  freely.
    1.14 +*/
    1.15 +
    1.16 +/* Program to test hotplugging of audio devices */
    1.17 +
    1.18 +#include "SDL_config.h"
    1.19 +
    1.20 +#include <stdio.h>
    1.21 +#include <stdlib.h>
    1.22 +
    1.23 +#if HAVE_SIGNAL_H
    1.24 +#include <signal.h>
    1.25 +#endif
    1.26 +
    1.27 +#ifdef __EMSCRIPTEN__
    1.28 +#include <emscripten/emscripten.h>
    1.29 +#endif
    1.30 +
    1.31 +#include "SDL.h"
    1.32 +
    1.33 +static SDL_AudioSpec spec;
    1.34 +static Uint8 *sound = NULL;     /* Pointer to wave data */
    1.35 +static Uint32 soundlen = 0;     /* Length of wave data */
    1.36 +
    1.37 +static int posindex = 0;
    1.38 +static Uint32 positions[64];
    1.39 +
    1.40 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.41 +static void
    1.42 +quit(int rc)
    1.43 +{
    1.44 +    SDL_Quit();
    1.45 +    exit(rc);
    1.46 +}
    1.47 +
    1.48 +void SDLCALL
    1.49 +fillerup(void *_pos, Uint8 * stream, int len)
    1.50 +{
    1.51 +    Uint32 pos = *((Uint32 *) _pos);
    1.52 +    Uint8 *waveptr;
    1.53 +    int waveleft;
    1.54 +
    1.55 +    /* Set up the pointers */
    1.56 +    waveptr = sound + pos;
    1.57 +    waveleft = soundlen - pos;
    1.58 +
    1.59 +    /* Go! */
    1.60 +    while (waveleft <= len) {
    1.61 +        SDL_memcpy(stream, waveptr, waveleft);
    1.62 +        stream += waveleft;
    1.63 +        len -= waveleft;
    1.64 +        waveptr = sound;
    1.65 +        waveleft = soundlen;
    1.66 +        pos = 0;
    1.67 +    }
    1.68 +    SDL_memcpy(stream, waveptr, len);
    1.69 +    pos += len;
    1.70 +    *((Uint32 *) _pos) = pos;
    1.71 +}
    1.72 +
    1.73 +static int done = 0;
    1.74 +void
    1.75 +poked(int sig)
    1.76 +{
    1.77 +    done = 1;
    1.78 +}
    1.79 +
    1.80 +static void
    1.81 +iteration()
    1.82 +{
    1.83 +    SDL_Event e;
    1.84 +    SDL_AudioDeviceID dev;
    1.85 +    while (SDL_PollEvent(&e)) {
    1.86 +        if (e.type == SDL_QUIT) {
    1.87 +            done = 1;
    1.88 +        } else if (e.type == SDL_AUDIODEVICEADDED) {
    1.89 +            const char *name = SDL_GetAudioDeviceName(e.adevice.which, 0);
    1.90 +            SDL_Log("New %s audio device: %s\n", e.adevice.iscapture ? "capture" : "output", name);
    1.91 +            if (!e.adevice.iscapture) {
    1.92 +                positions[posindex] = 0;
    1.93 +                spec.userdata = &positions[posindex++];
    1.94 +                spec.callback = fillerup;
    1.95 +                dev = SDL_OpenAudioDevice(name, 0, &spec, NULL, 0);
    1.96 +                if (!dev) {
    1.97 +                    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open '%s': %s\n", name, SDL_GetError());
    1.98 +                } else {
    1.99 +                    SDL_Log("Opened '%s' as %u\n", name, (unsigned int) dev);
   1.100 +                    SDL_PauseAudioDevice(dev, 0);
   1.101 +                }
   1.102 +            }
   1.103 +        } else if (e.type == SDL_AUDIODEVICEREMOVED) {
   1.104 +            dev = (SDL_AudioDeviceID) e.adevice.which;
   1.105 +            SDL_Log("%s device %u removed.\n", e.adevice.iscapture ? "capture" : "output", (unsigned int) dev);
   1.106 +            SDL_CloseAudioDevice(dev);
   1.107 +        }
   1.108 +    }
   1.109 +}
   1.110 +
   1.111 +#ifdef __EMSCRIPTEN__
   1.112 +void
   1.113 +loop()
   1.114 +{
   1.115 +    if(done)
   1.116 +        emscripten_cancel_main_loop();
   1.117 +    else
   1.118 +        iteration();
   1.119 +}
   1.120 +#endif
   1.121 +
   1.122 +int
   1.123 +main(int argc, char *argv[])
   1.124 +{
   1.125 +    int i;
   1.126 +    char filename[4096];
   1.127 +
   1.128 +	/* Enable standard application logging */
   1.129 +	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   1.130 +
   1.131 +    /* Load the SDL library */
   1.132 +    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
   1.133 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.134 +        return (1);
   1.135 +    }
   1.136 +
   1.137 +    SDL_CreateWindow("testaudiohotplug", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
   1.138 +
   1.139 +    if (argc > 1) {
   1.140 +        SDL_strlcpy(filename, argv[1], sizeof(filename));
   1.141 +    } else {
   1.142 +        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
   1.143 +    }
   1.144 +    /* Load the wave file into memory */
   1.145 +    if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
   1.146 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
   1.147 +        quit(1);
   1.148 +    }
   1.149 +
   1.150 +#if HAVE_SIGNAL_H
   1.151 +    /* Set the signals */
   1.152 +#ifdef SIGHUP
   1.153 +    signal(SIGHUP, poked);
   1.154 +#endif
   1.155 +    signal(SIGINT, poked);
   1.156 +#ifdef SIGQUIT
   1.157 +    signal(SIGQUIT, poked);
   1.158 +#endif
   1.159 +    signal(SIGTERM, poked);
   1.160 +#endif /* HAVE_SIGNAL_H */
   1.161 +
   1.162 +    /* Show the list of available drivers */
   1.163 +    SDL_Log("Available audio drivers:");
   1.164 +    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   1.165 +		SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
   1.166 +    }
   1.167 +
   1.168 +    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   1.169 +
   1.170 +#ifdef __EMSCRIPTEN__
   1.171 +    emscripten_set_main_loop(loop, 0, 1);
   1.172 +#else
   1.173 +    while (!done) {
   1.174 +        SDL_Delay(100);
   1.175 +        iteration();
   1.176 +    }
   1.177 +#endif
   1.178 +
   1.179 +    /* Clean up on signal */
   1.180 +    SDL_Quit();
   1.181 +    SDL_FreeWAV(sound);
   1.182 +    return (0);
   1.183 +}
   1.184 +
   1.185 +/* vi: set ts=4 sw=4 expandtab: */