test/testmultiaudio.c
changeset 2060 866052b01ee5
parent 2049 5f6550e5184f
child 2146 9fcde304c7b6
equal deleted inserted replaced
2059:4685ccd33d0e 2060:866052b01ee5
     1 #include "SDL.h"
     1 #include "SDL.h"
     2 
     2 
     3 static SDL_AudioSpec spec;
     3 static SDL_AudioSpec spec;
     4 static Uint8 *sound = NULL;            /* Pointer to wave data */
     4 static Uint8 *sound = NULL;     /* Pointer to wave data */
     5 static Uint32 soundlen = 0;            /* Length of wave data */
     5 static Uint32 soundlen = 0;     /* Length of wave data */
     6 
     6 
     7 typedef struct
     7 typedef struct
     8 {
     8 {
     9     SDL_AudioDeviceID dev;
     9     SDL_AudioDeviceID dev;
    10     int soundpos;
    10     int soundpos;
    11     volatile int done;
    11     volatile int done;
    12 } callback_data;
    12 } callback_data;
    13 
    13 
    14 void SDLCALL play_through_once(void *arg, Uint8 * stream, int len)
    14 void SDLCALL
       
    15 play_through_once(void *arg, Uint8 * stream, int len)
    15 {
    16 {
    16     callback_data *cbd = (callback_data *) arg;
    17     callback_data *cbd = (callback_data *) arg;
    17     Uint8 *waveptr = sound + cbd->soundpos;
    18     Uint8 *waveptr = sound + cbd->soundpos;
    18     int waveleft = soundlen - cbd->soundpos;
    19     int waveleft = soundlen - cbd->soundpos;
    19     int cpy = len;
    20     int cpy = len;
    28         memset(stream, spec.silence, len);
    29         memset(stream, spec.silence, len);
    29         cbd->done++;
    30         cbd->done++;
    30     }
    31     }
    31 }
    32 }
    32 
    33 
    33 static void test_multi_audio(int devcount)
    34 static void
       
    35 test_multi_audio(int devcount)
    34 {
    36 {
    35     callback_data cbd[64];
    37     callback_data cbd[64];
    36     int keep_going = 1;
    38     int keep_going = 1;
    37     int i;
    39     int i;
    38 
    40 
    39     if (devcount > 64) {
    41     if (devcount > 64) {
    40         fprintf(stderr, "Too many devices (%d), clamping to 64...\n", devcount);
    42         fprintf(stderr, "Too many devices (%d), clamping to 64...\n",
       
    43                 devcount);
    41         devcount = 64;
    44         devcount = 64;
    42     }
    45     }
    43 
    46 
    44     spec.callback = play_through_once;
    47     spec.callback = play_through_once;
    45 
    48 
    46     for (i = 0; i < devcount; i++) {
    49     for (i = 0; i < devcount; i++) {
    47         const char *devname = SDL_GetAudioDeviceName(i, 0);
    50         const char *devname = SDL_GetAudioDeviceName(i, 0);
    48         printf("playing on device #%d: ('%s')...", i, devname);
    51         printf("playing on device #%d: ('%s')...", i, devname);
    49         fflush(stdout);
    52         fflush(stdout);
    50 
    53 
    51         memset(&cbd[0], '\0', sizeof (callback_data));
    54         memset(&cbd[0], '\0', sizeof(callback_data));
    52         spec.userdata = &cbd[0];
    55         spec.userdata = &cbd[0];
    53         cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL);
    56         cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL);
    54         if (cbd[0].dev == 0) {
    57         if (cbd[0].dev == 0) {
    55             printf("Open device failed: %s\n", SDL_GetError());
    58             printf("Open device failed: %s\n", SDL_GetError());
    56         } else {
    59         } else {
    61             printf("done.\n");
    64             printf("done.\n");
    62             SDL_CloseAudioDevice(cbd[0].dev);
    65             SDL_CloseAudioDevice(cbd[0].dev);
    63         }
    66         }
    64     }
    67     }
    65 
    68 
    66     memset(cbd, '\0', sizeof (cbd));
    69     memset(cbd, '\0', sizeof(cbd));
    67 
    70 
    68     printf("playing on all devices...\n");
    71     printf("playing on all devices...\n");
    69     for (i = 0; i < devcount; i++) {
    72     for (i = 0; i < devcount; i++) {
    70         const char *devname = SDL_GetAudioDeviceName(i, 0);
    73         const char *devname = SDL_GetAudioDeviceName(i, 0);
    71         spec.userdata = &cbd[i];
    74         spec.userdata = &cbd[i];
   100 
   103 
   101     printf("All done!\n");
   104     printf("All done!\n");
   102 }
   105 }
   103 
   106 
   104 
   107 
   105 int main(int argc, char **argv)
   108 int
       
   109 main(int argc, char **argv)
   106 {
   110 {
   107     int devcount = 0;
   111     int devcount = 0;
   108 
   112 
   109     /* Load the SDL library */
   113     /* Load the SDL library */
   110     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   114     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   122             argv[1] = "sample.wav";
   126             argv[1] = "sample.wav";
   123         }
   127         }
   124 
   128 
   125         /* Load the wave file into memory */
   129         /* Load the wave file into memory */
   126         if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) {
   130         if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) {
   127             fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
   131             fprintf(stderr, "Couldn't load %s: %s\n", argv[1],
       
   132                     SDL_GetError());
   128         } else {
   133         } else {
   129             test_multi_audio(devcount);
   134             test_multi_audio(devcount);
   130             SDL_FreeWAV(sound);
   135             SDL_FreeWAV(sound);
   131         }
   136         }
   132     }
   137     }