test/testaudiohotplug.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Thu, 21 May 2015 21:25:32 +0200
changeset 9610 27485b58df22
parent 9402 bd133a872458
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Changed clean-up order in test program.
icculus@9393
     1
/*
icculus@9393
     2
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
icculus@9393
     3
icculus@9393
     4
  This software is provided 'as-is', without any express or implied
icculus@9393
     5
  warranty.  In no event will the authors be held liable for any damages
icculus@9393
     6
  arising from the use of this software.
icculus@9393
     7
icculus@9393
     8
  Permission is granted to anyone to use this software for any purpose,
icculus@9393
     9
  including commercial applications, and to alter it and redistribute it
icculus@9393
    10
  freely.
icculus@9393
    11
*/
icculus@9393
    12
icculus@9393
    13
/* Program to test hotplugging of audio devices */
icculus@9393
    14
icculus@9393
    15
#include "SDL_config.h"
icculus@9393
    16
icculus@9393
    17
#include <stdio.h>
icculus@9393
    18
#include <stdlib.h>
icculus@9393
    19
icculus@9393
    20
#if HAVE_SIGNAL_H
icculus@9393
    21
#include <signal.h>
icculus@9393
    22
#endif
icculus@9393
    23
icculus@9393
    24
#ifdef __EMSCRIPTEN__
icculus@9393
    25
#include <emscripten/emscripten.h>
icculus@9393
    26
#endif
icculus@9393
    27
icculus@9393
    28
#include "SDL.h"
icculus@9393
    29
icculus@9393
    30
static SDL_AudioSpec spec;
icculus@9393
    31
static Uint8 *sound = NULL;     /* Pointer to wave data */
icculus@9393
    32
static Uint32 soundlen = 0;     /* Length of wave data */
icculus@9393
    33
icculus@9393
    34
static int posindex = 0;
icculus@9393
    35
static Uint32 positions[64];
icculus@9393
    36
icculus@9393
    37
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@9393
    38
static void
icculus@9393
    39
quit(int rc)
icculus@9393
    40
{
icculus@9393
    41
    SDL_Quit();
icculus@9393
    42
    exit(rc);
icculus@9393
    43
}
icculus@9393
    44
icculus@9393
    45
void SDLCALL
icculus@9393
    46
fillerup(void *_pos, Uint8 * stream, int len)
icculus@9393
    47
{
icculus@9393
    48
    Uint32 pos = *((Uint32 *) _pos);
icculus@9393
    49
    Uint8 *waveptr;
icculus@9393
    50
    int waveleft;
icculus@9393
    51
icculus@9393
    52
    /* Set up the pointers */
icculus@9393
    53
    waveptr = sound + pos;
icculus@9393
    54
    waveleft = soundlen - pos;
icculus@9393
    55
icculus@9393
    56
    /* Go! */
icculus@9393
    57
    while (waveleft <= len) {
icculus@9393
    58
        SDL_memcpy(stream, waveptr, waveleft);
icculus@9393
    59
        stream += waveleft;
icculus@9393
    60
        len -= waveleft;
icculus@9393
    61
        waveptr = sound;
icculus@9393
    62
        waveleft = soundlen;
icculus@9393
    63
        pos = 0;
icculus@9393
    64
    }
icculus@9393
    65
    SDL_memcpy(stream, waveptr, len);
icculus@9393
    66
    pos += len;
icculus@9393
    67
    *((Uint32 *) _pos) = pos;
icculus@9393
    68
}
icculus@9393
    69
icculus@9393
    70
static int done = 0;
icculus@9393
    71
void
icculus@9393
    72
poked(int sig)
icculus@9393
    73
{
icculus@9393
    74
    done = 1;
icculus@9393
    75
}
icculus@9393
    76
icculus@9393
    77
static void
icculus@9393
    78
iteration()
icculus@9393
    79
{
icculus@9393
    80
    SDL_Event e;
icculus@9393
    81
    SDL_AudioDeviceID dev;
icculus@9393
    82
    while (SDL_PollEvent(&e)) {
icculus@9393
    83
        if (e.type == SDL_QUIT) {
icculus@9393
    84
            done = 1;
icculus@9393
    85
        } else if (e.type == SDL_AUDIODEVICEADDED) {
icculus@9393
    86
            const char *name = SDL_GetAudioDeviceName(e.adevice.which, 0);
icculus@9393
    87
            SDL_Log("New %s audio device: %s\n", e.adevice.iscapture ? "capture" : "output", name);
icculus@9393
    88
            if (!e.adevice.iscapture) {
icculus@9393
    89
                positions[posindex] = 0;
icculus@9393
    90
                spec.userdata = &positions[posindex++];
icculus@9393
    91
                spec.callback = fillerup;
icculus@9393
    92
                dev = SDL_OpenAudioDevice(name, 0, &spec, NULL, 0);
icculus@9393
    93
                if (!dev) {
icculus@9393
    94
                    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open '%s': %s\n", name, SDL_GetError());
icculus@9393
    95
                } else {
icculus@9393
    96
                    SDL_Log("Opened '%s' as %u\n", name, (unsigned int) dev);
icculus@9393
    97
                    SDL_PauseAudioDevice(dev, 0);
icculus@9393
    98
                }
icculus@9393
    99
            }
icculus@9393
   100
        } else if (e.type == SDL_AUDIODEVICEREMOVED) {
icculus@9393
   101
            dev = (SDL_AudioDeviceID) e.adevice.which;
icculus@9393
   102
            SDL_Log("%s device %u removed.\n", e.adevice.iscapture ? "capture" : "output", (unsigned int) dev);
icculus@9393
   103
            SDL_CloseAudioDevice(dev);
icculus@9393
   104
        }
icculus@9393
   105
    }
icculus@9393
   106
}
icculus@9393
   107
icculus@9393
   108
#ifdef __EMSCRIPTEN__
icculus@9393
   109
void
icculus@9393
   110
loop()
icculus@9393
   111
{
icculus@9393
   112
    if(done)
icculus@9393
   113
        emscripten_cancel_main_loop();
icculus@9393
   114
    else
icculus@9393
   115
        iteration();
icculus@9393
   116
}
icculus@9393
   117
#endif
icculus@9393
   118
icculus@9393
   119
int
icculus@9393
   120
main(int argc, char *argv[])
icculus@9393
   121
{
icculus@9393
   122
    int i;
icculus@9393
   123
    char filename[4096];
icculus@9393
   124
icculus@9393
   125
	/* Enable standard application logging */
icculus@9393
   126
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
icculus@9393
   127
icculus@9393
   128
    /* Load the SDL library */
icculus@9393
   129
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
icculus@9393
   130
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
icculus@9393
   131
        return (1);
icculus@9393
   132
    }
icculus@9393
   133
icculus@9402
   134
    /* Some targets (Mac CoreAudio) need an event queue for audio hotplug, so make and immediately hide a window. */
icculus@9402
   135
    SDL_MinimizeWindow(SDL_CreateWindow("testaudiohotplug", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0));
icculus@9393
   136
icculus@9393
   137
    if (argc > 1) {
icculus@9393
   138
        SDL_strlcpy(filename, argv[1], sizeof(filename));
icculus@9393
   139
    } else {
icculus@9393
   140
        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
icculus@9393
   141
    }
icculus@9393
   142
    /* Load the wave file into memory */
icculus@9393
   143
    if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
icculus@9393
   144
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
icculus@9393
   145
        quit(1);
icculus@9393
   146
    }
icculus@9393
   147
icculus@9393
   148
#if HAVE_SIGNAL_H
icculus@9393
   149
    /* Set the signals */
icculus@9393
   150
#ifdef SIGHUP
icculus@9393
   151
    signal(SIGHUP, poked);
icculus@9393
   152
#endif
icculus@9393
   153
    signal(SIGINT, poked);
icculus@9393
   154
#ifdef SIGQUIT
icculus@9393
   155
    signal(SIGQUIT, poked);
icculus@9393
   156
#endif
icculus@9393
   157
    signal(SIGTERM, poked);
icculus@9393
   158
#endif /* HAVE_SIGNAL_H */
icculus@9393
   159
icculus@9393
   160
    /* Show the list of available drivers */
icculus@9393
   161
    SDL_Log("Available audio drivers:");
icculus@9393
   162
    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
icculus@9393
   163
		SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
icculus@9393
   164
    }
icculus@9393
   165
icculus@9393
   166
    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
icculus@9393
   167
icculus@9393
   168
#ifdef __EMSCRIPTEN__
icculus@9393
   169
    emscripten_set_main_loop(loop, 0, 1);
icculus@9393
   170
#else
icculus@9393
   171
    while (!done) {
icculus@9393
   172
        SDL_Delay(100);
icculus@9393
   173
        iteration();
icculus@9393
   174
    }
icculus@9393
   175
#endif
icculus@9393
   176
icculus@9393
   177
    /* Clean up on signal */
philipp@9610
   178
    SDL_FreeWAV(sound);
icculus@9393
   179
    SDL_Quit();
icculus@9393
   180
    return (0);
icculus@9393
   181
}
icculus@9393
   182
icculus@9393
   183
/* vi: set ts=4 sw=4 expandtab: */