test/testmultiaudio.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 17 Oct 2006 09:15:21 +0000
changeset 2049 5f6550e5184f
child 2060 866052b01ee5
permissions -rw-r--r--
Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
     1 #include "SDL.h"
     2 
     3 static SDL_AudioSpec spec;
     4 static Uint8 *sound = NULL;            /* Pointer to wave data */
     5 static Uint32 soundlen = 0;            /* Length of wave data */
     6 
     7 typedef struct
     8 {
     9     SDL_AudioDeviceID dev;
    10     int soundpos;
    11     volatile int done;
    12 } callback_data;
    13 
    14 void SDLCALL play_through_once(void *arg, Uint8 * stream, int len)
    15 {
    16     callback_data *cbd = (callback_data *) arg;
    17     Uint8 *waveptr = sound + cbd->soundpos;
    18     int waveleft = soundlen - cbd->soundpos;
    19     int cpy = len;
    20     if (cpy > waveleft)
    21         cpy = waveleft;
    22 
    23     memcpy(stream, waveptr, cpy);
    24     len -= cpy;
    25     cbd->soundpos += cpy;
    26     if (len > 0) {
    27         stream += cpy;
    28         memset(stream, spec.silence, len);
    29         cbd->done++;
    30     }
    31 }
    32 
    33 static void test_multi_audio(int devcount)
    34 {
    35     callback_data cbd[64];
    36     int keep_going = 1;
    37     int i;
    38 
    39     if (devcount > 64) {
    40         fprintf(stderr, "Too many devices (%d), clamping to 64...\n", devcount);
    41         devcount = 64;
    42     }
    43 
    44     spec.callback = play_through_once;
    45 
    46     for (i = 0; i < devcount; i++) {
    47         const char *devname = SDL_GetAudioDeviceName(i, 0);
    48         printf("playing on device #%d: ('%s')...", i, devname);
    49         fflush(stdout);
    50 
    51         memset(&cbd[0], '\0', sizeof (callback_data));
    52         spec.userdata = &cbd[0];
    53         cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL);
    54         if (cbd[0].dev == 0) {
    55             printf("Open device failed: %s\n", SDL_GetError());
    56         } else {
    57             SDL_PauseAudioDevice(cbd[0].dev, 0);
    58             while (!cbd[0].done)
    59                 SDL_Delay(100);
    60             SDL_PauseAudioDevice(cbd[0].dev, 1);
    61             printf("done.\n");
    62             SDL_CloseAudioDevice(cbd[0].dev);
    63         }
    64     }
    65 
    66     memset(cbd, '\0', sizeof (cbd));
    67 
    68     printf("playing on all devices...\n");
    69     for (i = 0; i < devcount; i++) {
    70         const char *devname = SDL_GetAudioDeviceName(i, 0);
    71         spec.userdata = &cbd[i];
    72         cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL);
    73         if (cbd[i].dev == 0) {
    74             printf("Open device %d failed: %s\n", i, SDL_GetError());
    75         }
    76     }
    77 
    78     for (i = 0; i < devcount; i++) {
    79         if (cbd[i].dev) {
    80             SDL_PauseAudioDevice(cbd[i].dev, 0);
    81         }
    82     }
    83 
    84     while (keep_going) {
    85         keep_going = 0;
    86         for (i = 0; i < devcount; i++) {
    87             if ((cbd[i].dev) && (!cbd[i].done)) {
    88                 keep_going = 1;
    89             }
    90         }
    91         SDL_Delay(100);
    92     }
    93 
    94     for (i = 0; i < devcount; i++) {
    95         if (cbd[i].dev) {
    96             SDL_PauseAudioDevice(cbd[i].dev, 1);
    97             SDL_CloseAudioDevice(cbd[i].dev);
    98         }
    99     }
   100 
   101     printf("All done!\n");
   102 }
   103 
   104 
   105 int main(int argc, char **argv)
   106 {
   107     int devcount = 0;
   108 
   109     /* Load the SDL library */
   110     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   111         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   112         return (1);
   113     }
   114 
   115     printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   116 
   117     devcount = SDL_GetNumAudioDevices(0);
   118     if (devcount < 1) {
   119         fprintf(stderr, "Don't see any specific audio devices!\n");
   120     } else {
   121         if (argv[1] == NULL) {
   122             argv[1] = "sample.wav";
   123         }
   124 
   125         /* Load the wave file into memory */
   126         if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) {
   127             fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
   128         } else {
   129             test_multi_audio(devcount);
   130             SDL_FreeWAV(sound);
   131         }
   132     }
   133 
   134     SDL_Quit();
   135     return 0;
   136 }