test/testmultiaudio.c
branchSDL-ryan-multiple-audio-device
changeset 3794 db24e43972ac
child 3800 2d77a732bd20
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testmultiaudio.c	Tue Oct 03 23:45:36 2006 +0000
     1.3 @@ -0,0 +1,136 @@
     1.4 +#include "SDL.h"
     1.5 +
     1.6 +static SDL_AudioSpec spec;
     1.7 +static Uint8 *sound = NULL;            /* Pointer to wave data */
     1.8 +static Uint32 soundlen = 0;            /* Length of wave data */
     1.9 +
    1.10 +typedef struct
    1.11 +{
    1.12 +    SDL_AudioDeviceID dev;
    1.13 +    int soundpos;
    1.14 +    volatile int done;
    1.15 +} callback_data;
    1.16 +
    1.17 +void SDLCALL play_through_once(void *arg, Uint8 * stream, int len)
    1.18 +{
    1.19 +    callback_data *cbd = (callback_data *) arg;
    1.20 +    Uint8 *waveptr = sound + cbd->soundpos;
    1.21 +    int waveleft = soundlen - cbd->soundpos;
    1.22 +    int cpy = len;
    1.23 +    if (cpy > waveleft)
    1.24 +        cpy = waveleft;
    1.25 +
    1.26 +    memcpy(stream, waveptr, cpy);
    1.27 +    len -= cpy;
    1.28 +    cbd->soundpos += cpy;
    1.29 +    if (len > 0) {
    1.30 +        stream += cpy;
    1.31 +        memset(stream, spec.silence, len);
    1.32 +        cbd->done++;
    1.33 +    }
    1.34 +}
    1.35 +
    1.36 +static void test_multi_audio(int devcount)
    1.37 +{
    1.38 +    callback_data cbd[64];
    1.39 +    int keep_going = 1;
    1.40 +    int i;
    1.41 +
    1.42 +    if (devcount > 64) {
    1.43 +        fprintf(stderr, "Too many devices (%d), clamping to 64...\n", devcount);
    1.44 +        devcount = 64;
    1.45 +    }
    1.46 +
    1.47 +    spec.callback = play_through_once;
    1.48 +
    1.49 +    for (i = 0; i < devcount; i++) {
    1.50 +        const char *devname = SDL_GetAudioDeviceName(i, 0);
    1.51 +        printf("playing on device #%d: ('%s')...", i, devname);
    1.52 +        fflush(stdout);
    1.53 +
    1.54 +        memset(&cbd[0], '\0', sizeof (callback_data));
    1.55 +        spec.userdata = &cbd[0];
    1.56 +        cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL);
    1.57 +        if (cbd[0].dev == 0) {
    1.58 +            printf("Open device failed: %s\n", SDL_GetError());
    1.59 +        } else {
    1.60 +            SDL_PauseAudioDevice(cbd[0].dev, 0);
    1.61 +            while (!cbd[0].done)
    1.62 +                SDL_Delay(100);
    1.63 +            SDL_PauseAudioDevice(cbd[0].dev, 1);
    1.64 +            printf("done.\n");
    1.65 +            SDL_CloseAudioDevice(cbd[0].dev);
    1.66 +        }
    1.67 +    }
    1.68 +
    1.69 +    memset(cbd, '\0', sizeof (cbd));
    1.70 +
    1.71 +    printf("playing on all devices...\n");
    1.72 +    for (i = 0; i < devcount; i++) {
    1.73 +        const char *devname = SDL_GetAudioDeviceName(i, 0);
    1.74 +        spec.userdata = &cbd[i];
    1.75 +        cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL);
    1.76 +        if (cbd[i].dev == 0) {
    1.77 +            printf("Open device %d failed: %s\n", i, SDL_GetError());
    1.78 +        }
    1.79 +    }
    1.80 +
    1.81 +    for (i = 0; i < devcount; i++) {
    1.82 +        if (cbd[i].dev) {
    1.83 +            SDL_PauseAudioDevice(cbd[i].dev, 0);
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    while (keep_going) {
    1.88 +        keep_going = 0;
    1.89 +        for (i = 0; i < devcount; i++) {
    1.90 +            if ((cbd[i].dev) && (!cbd[i].done)) {
    1.91 +                keep_going = 1;
    1.92 +            }
    1.93 +        }
    1.94 +        SDL_Delay(100);
    1.95 +    }
    1.96 +
    1.97 +    for (i = 0; i < devcount; i++) {
    1.98 +        if (cbd[i].dev) {
    1.99 +            SDL_PauseAudioDevice(cbd[i].dev, 1);
   1.100 +            SDL_CloseAudioDevice(cbd[i].dev);
   1.101 +        }
   1.102 +    }
   1.103 +
   1.104 +    printf("All done!\n");
   1.105 +}
   1.106 +
   1.107 +
   1.108 +int main(int argc, char **argv)
   1.109 +{
   1.110 +    int devcount = 0;
   1.111 +
   1.112 +    /* Load the SDL library */
   1.113 +    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   1.114 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.115 +        return (1);
   1.116 +    }
   1.117 +
   1.118 +    printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   1.119 +
   1.120 +    devcount = SDL_GetNumAudioDevices(0);
   1.121 +    if (devcount < 1) {
   1.122 +        fprintf(stderr, "Don't see more than one device!\n");
   1.123 +    } else {
   1.124 +        if (argv[1] == NULL) {
   1.125 +            argv[1] = "sample.wav";
   1.126 +        }
   1.127 +
   1.128 +        /* Load the wave file into memory */
   1.129 +        if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) {
   1.130 +            fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
   1.131 +        } else {
   1.132 +            test_multi_audio(devcount);
   1.133 +            SDL_FreeWAV(sound);
   1.134 +        }
   1.135 +    }
   1.136 +
   1.137 +    SDL_Quit();
   1.138 +    return 0;
   1.139 +}