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