test/testmultiaudio.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 05 Jun 2013 12:48:44 -0700
changeset 7277 1290cd7f34af
parent 5597 c19d7122cb1a
child 7517 965d57022c01
permissions -rw-r--r--
Mac: Fix invalid PS3 controller mapping.

PS3 controller had swapped D-Pad down & left buttons, as well as X & Y buttons.
Thanks to Alex Szpakowski for the bug report and fix.

Fixes http://bugzilla.libsdl.org/show_bug.cgi?id=1891
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 #include "SDL.h"
    13 #include <stdio.h>
    14 
    15 static SDL_AudioSpec spec;
    16 static Uint8 *sound = NULL;     /* Pointer to wave data */
    17 static Uint32 soundlen = 0;     /* Length of wave data */
    18 
    19 typedef struct
    20 {
    21     SDL_AudioDeviceID dev;
    22     int soundpos;
    23     volatile int done;
    24 } callback_data;
    25 
    26 void SDLCALL
    27 play_through_once(void *arg, Uint8 * stream, int len)
    28 {
    29     callback_data *cbd = (callback_data *) arg;
    30     Uint8 *waveptr = sound + cbd->soundpos;
    31     int waveleft = soundlen - cbd->soundpos;
    32     int cpy = len;
    33     if (cpy > waveleft)
    34         cpy = waveleft;
    35 
    36     SDL_memcpy(stream, waveptr, cpy);
    37     len -= cpy;
    38     cbd->soundpos += cpy;
    39     if (len > 0) {
    40         stream += cpy;
    41         SDL_memset(stream, spec.silence, len);
    42         cbd->done++;
    43     }
    44 }
    45 
    46 static void
    47 test_multi_audio(int devcount)
    48 {
    49     callback_data cbd[64];
    50     int keep_going = 1;
    51     int i;
    52 
    53     if (devcount > 64) {
    54         fprintf(stderr, "Too many devices (%d), clamping to 64...\n",
    55                 devcount);
    56         devcount = 64;
    57     }
    58 
    59     spec.callback = play_through_once;
    60 
    61     for (i = 0; i < devcount; i++) {
    62         const char *devname = SDL_GetAudioDeviceName(i, 0);
    63         printf("playing on device #%d: ('%s')...", i, devname);
    64         fflush(stdout);
    65 
    66         SDL_memset(&cbd[0], '\0', sizeof(callback_data));
    67         spec.userdata = &cbd[0];
    68         cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
    69         if (cbd[0].dev == 0) {
    70             printf("\nOpen device failed: %s\n", SDL_GetError());
    71         } else {
    72             SDL_PauseAudioDevice(cbd[0].dev, 0);
    73             while (!cbd[0].done)
    74                 SDL_Delay(100);
    75             SDL_PauseAudioDevice(cbd[0].dev, 1);
    76             printf("done.\n");
    77             SDL_CloseAudioDevice(cbd[0].dev);
    78         }
    79     }
    80 
    81     SDL_memset(cbd, '\0', sizeof(cbd));
    82 
    83     printf("playing on all devices...\n");
    84     for (i = 0; i < devcount; i++) {
    85         const char *devname = SDL_GetAudioDeviceName(i, 0);
    86         spec.userdata = &cbd[i];
    87         cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
    88         if (cbd[i].dev == 0) {
    89             printf("Open device %d failed: %s\n", i, SDL_GetError());
    90         }
    91     }
    92 
    93     for (i = 0; i < devcount; i++) {
    94         if (cbd[i].dev) {
    95             SDL_PauseAudioDevice(cbd[i].dev, 0);
    96         }
    97     }
    98 
    99     while (keep_going) {
   100         keep_going = 0;
   101         for (i = 0; i < devcount; i++) {
   102             if ((cbd[i].dev) && (!cbd[i].done)) {
   103                 keep_going = 1;
   104             }
   105         }
   106         SDL_Delay(100);
   107     }
   108 
   109     for (i = 0; i < devcount; i++) {
   110         if (cbd[i].dev) {
   111             SDL_PauseAudioDevice(cbd[i].dev, 1);
   112             SDL_CloseAudioDevice(cbd[i].dev);
   113         }
   114     }
   115 
   116     printf("All done!\n");
   117 }
   118 
   119 
   120 int
   121 main(int argc, char **argv)
   122 {
   123     int devcount = 0;
   124 
   125     /* Load the SDL library */
   126     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   127         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   128         return (1);
   129     }
   130 
   131     printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   132 
   133     devcount = SDL_GetNumAudioDevices(0);
   134     if (devcount < 1) {
   135         fprintf(stderr, "Don't see any specific audio devices!\n");
   136     } else {
   137         if (argv[1] == NULL) {
   138             argv[1] = "sample.wav";
   139         }
   140 
   141         /* Load the wave file into memory */
   142         if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) {
   143             fprintf(stderr, "Couldn't load %s: %s\n", argv[1],
   144                     SDL_GetError());
   145         } else {
   146             test_multi_audio(devcount);
   147             SDL_FreeWAV(sound);
   148         }
   149     }
   150 
   151     SDL_Quit();
   152     return 0;
   153 }