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