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