Test program to play audio to every available device, one at a time, then all SDL-ryan-multiple-audio-device
authorRyan C. Gordon
Tue, 03 Oct 2006 23:45:36 +0000
branchSDL-ryan-multiple-audio-device
changeset 3794db24e43972ac
parent 3793 2b4795e92b62
child 3795 589bc3d060cd
Test program to play audio to every available device, one at a time, then all
at once.
test/Makefile.in
test/testmultiaudio.c
     1.1 --- a/test/Makefile.in	Tue Oct 03 22:19:45 2006 +0000
     1.2 +++ b/test/Makefile.in	Tue Oct 03 23:45:36 2006 +0000
     1.3 @@ -7,7 +7,7 @@
     1.4  CFLAGS  = @CFLAGS@
     1.5  LIBS	= @LIBS@
     1.6  
     1.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testaudioinfo$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE)
     1.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE)
     1.9  
    1.10  all: Makefile $(TARGETS)
    1.11  
    1.12 @@ -26,6 +26,9 @@
    1.13  testaudioinfo$(EXE): $(srcdir)/testaudioinfo.c
    1.14  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.15  
    1.16 +testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c
    1.17 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.18 +
    1.19  testalpha$(EXE): $(srcdir)/testalpha.c
    1.20  	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    1.21  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/testmultiaudio.c	Tue Oct 03 23:45:36 2006 +0000
     2.3 @@ -0,0 +1,136 @@
     2.4 +#include "SDL.h"
     2.5 +
     2.6 +static SDL_AudioSpec spec;
     2.7 +static Uint8 *sound = NULL;            /* Pointer to wave data */
     2.8 +static Uint32 soundlen = 0;            /* Length of wave data */
     2.9 +
    2.10 +typedef struct
    2.11 +{
    2.12 +    SDL_AudioDeviceID dev;
    2.13 +    int soundpos;
    2.14 +    volatile int done;
    2.15 +} callback_data;
    2.16 +
    2.17 +void SDLCALL play_through_once(void *arg, Uint8 * stream, int len)
    2.18 +{
    2.19 +    callback_data *cbd = (callback_data *) arg;
    2.20 +    Uint8 *waveptr = sound + cbd->soundpos;
    2.21 +    int waveleft = soundlen - cbd->soundpos;
    2.22 +    int cpy = len;
    2.23 +    if (cpy > waveleft)
    2.24 +        cpy = waveleft;
    2.25 +
    2.26 +    memcpy(stream, waveptr, cpy);
    2.27 +    len -= cpy;
    2.28 +    cbd->soundpos += cpy;
    2.29 +    if (len > 0) {
    2.30 +        stream += cpy;
    2.31 +        memset(stream, spec.silence, len);
    2.32 +        cbd->done++;
    2.33 +    }
    2.34 +}
    2.35 +
    2.36 +static void test_multi_audio(int devcount)
    2.37 +{
    2.38 +    callback_data cbd[64];
    2.39 +    int keep_going = 1;
    2.40 +    int i;
    2.41 +
    2.42 +    if (devcount > 64) {
    2.43 +        fprintf(stderr, "Too many devices (%d), clamping to 64...\n", devcount);
    2.44 +        devcount = 64;
    2.45 +    }
    2.46 +
    2.47 +    spec.callback = play_through_once;
    2.48 +
    2.49 +    for (i = 0; i < devcount; i++) {
    2.50 +        const char *devname = SDL_GetAudioDeviceName(i, 0);
    2.51 +        printf("playing on device #%d: ('%s')...", i, devname);
    2.52 +        fflush(stdout);
    2.53 +
    2.54 +        memset(&cbd[0], '\0', sizeof (callback_data));
    2.55 +        spec.userdata = &cbd[0];
    2.56 +        cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL);
    2.57 +        if (cbd[0].dev == 0) {
    2.58 +            printf("Open device failed: %s\n", SDL_GetError());
    2.59 +        } else {
    2.60 +            SDL_PauseAudioDevice(cbd[0].dev, 0);
    2.61 +            while (!cbd[0].done)
    2.62 +                SDL_Delay(100);
    2.63 +            SDL_PauseAudioDevice(cbd[0].dev, 1);
    2.64 +            printf("done.\n");
    2.65 +            SDL_CloseAudioDevice(cbd[0].dev);
    2.66 +        }
    2.67 +    }
    2.68 +
    2.69 +    memset(cbd, '\0', sizeof (cbd));
    2.70 +
    2.71 +    printf("playing on all devices...\n");
    2.72 +    for (i = 0; i < devcount; i++) {
    2.73 +        const char *devname = SDL_GetAudioDeviceName(i, 0);
    2.74 +        spec.userdata = &cbd[i];
    2.75 +        cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL);
    2.76 +        if (cbd[i].dev == 0) {
    2.77 +            printf("Open device %d failed: %s\n", i, SDL_GetError());
    2.78 +        }
    2.79 +    }
    2.80 +
    2.81 +    for (i = 0; i < devcount; i++) {
    2.82 +        if (cbd[i].dev) {
    2.83 +            SDL_PauseAudioDevice(cbd[i].dev, 0);
    2.84 +        }
    2.85 +    }
    2.86 +
    2.87 +    while (keep_going) {
    2.88 +        keep_going = 0;
    2.89 +        for (i = 0; i < devcount; i++) {
    2.90 +            if ((cbd[i].dev) && (!cbd[i].done)) {
    2.91 +                keep_going = 1;
    2.92 +            }
    2.93 +        }
    2.94 +        SDL_Delay(100);
    2.95 +    }
    2.96 +
    2.97 +    for (i = 0; i < devcount; i++) {
    2.98 +        if (cbd[i].dev) {
    2.99 +            SDL_PauseAudioDevice(cbd[i].dev, 1);
   2.100 +            SDL_CloseAudioDevice(cbd[i].dev);
   2.101 +        }
   2.102 +    }
   2.103 +
   2.104 +    printf("All done!\n");
   2.105 +}
   2.106 +
   2.107 +
   2.108 +int main(int argc, char **argv)
   2.109 +{
   2.110 +    int devcount = 0;
   2.111 +
   2.112 +    /* Load the SDL library */
   2.113 +    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   2.114 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   2.115 +        return (1);
   2.116 +    }
   2.117 +
   2.118 +    printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   2.119 +
   2.120 +    devcount = SDL_GetNumAudioDevices(0);
   2.121 +    if (devcount < 1) {
   2.122 +        fprintf(stderr, "Don't see more than one device!\n");
   2.123 +    } else {
   2.124 +        if (argv[1] == NULL) {
   2.125 +            argv[1] = "sample.wav";
   2.126 +        }
   2.127 +
   2.128 +        /* Load the wave file into memory */
   2.129 +        if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) {
   2.130 +            fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
   2.131 +        } else {
   2.132 +            test_multi_audio(devcount);
   2.133 +            SDL_FreeWAV(sound);
   2.134 +        }
   2.135 +    }
   2.136 +
   2.137 +    SDL_Quit();
   2.138 +    return 0;
   2.139 +}