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