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