test/testmultiaudio.c
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 06 Jun 2014 18:33:17 -0300
changeset 8834 b300e097899d
parent 8787 8e20b31043cb
child 8851 a7cfe554d337
permissions -rw-r--r--
Adds missing Makefile for NACL test and a couple of smaller fixes
     1 /*
     2   Copyright (C) 1997-2014 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 
    14 #include <stdio.h> /* for fflush() and stdout */
    15 
    16 static SDL_AudioSpec spec;
    17 static Uint8 *sound = NULL;     /* Pointer to wave data */
    18 static Uint32 soundlen = 0;     /* Length of wave data */
    19 
    20 typedef struct
    21 {
    22     SDL_AudioDeviceID dev;
    23     int soundpos;
    24     volatile int done;
    25 } callback_data;
    26 
    27 void SDLCALL
    28 play_through_once(void *arg, Uint8 * stream, int len)
    29 {
    30     callback_data *cbd = (callback_data *) arg;
    31     Uint8 *waveptr = sound + cbd->soundpos;
    32     int waveleft = soundlen - cbd->soundpos;
    33     int cpy = len;
    34     if (cpy > waveleft)
    35         cpy = waveleft;
    36 
    37     SDL_memcpy(stream, waveptr, cpy);
    38     len -= cpy;
    39     cbd->soundpos += cpy;
    40     if (len > 0) {
    41         stream += cpy;
    42         SDL_memset(stream, spec.silence, len);
    43         cbd->done++;
    44     }
    45 }
    46 
    47 static void
    48 test_multi_audio(int devcount)
    49 {
    50     callback_data cbd[64];
    51     int keep_going = 1;
    52     int i;
    53 
    54     if (devcount > 64) {
    55         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Too many devices (%d), clamping to 64...\n",
    56                 devcount);
    57         devcount = 64;
    58     }
    59 
    60     spec.callback = play_through_once;
    61 
    62     for (i = 0; i < devcount; i++) {
    63         const char *devname = SDL_GetAudioDeviceName(i, 0);
    64         SDL_Log("playing on device #%d: ('%s')...", i, devname);
    65         fflush(stdout);
    66 
    67         SDL_memset(&cbd[0], '\0', sizeof(callback_data));
    68         spec.userdata = &cbd[0];
    69         cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
    70         if (cbd[0].dev == 0) {
    71             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device failed: %s\n", SDL_GetError());
    72         } else {
    73             SDL_PauseAudioDevice(cbd[0].dev, 0);
    74             while (!cbd[0].done)
    75                 SDL_Delay(100);
    76             SDL_PauseAudioDevice(cbd[0].dev, 1);
    77             SDL_Log("done.\n");
    78             SDL_CloseAudioDevice(cbd[0].dev);
    79         }
    80     }
    81 
    82     SDL_memset(cbd, '\0', sizeof(cbd));
    83 
    84     SDL_Log("playing on all devices...\n");
    85     for (i = 0; i < devcount; i++) {
    86         const char *devname = SDL_GetAudioDeviceName(i, 0);
    87         spec.userdata = &cbd[i];
    88         cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
    89         if (cbd[i].dev == 0) {
    90             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device %d failed: %s\n", i, SDL_GetError());
    91         }
    92     }
    93 
    94     for (i = 0; i < devcount; i++) {
    95         if (cbd[i].dev) {
    96             SDL_PauseAudioDevice(cbd[i].dev, 0);
    97         }
    98     }
    99 
   100     while (keep_going) {
   101         keep_going = 0;
   102         for (i = 0; i < devcount; i++) {
   103             if ((cbd[i].dev) && (!cbd[i].done)) {
   104                 keep_going = 1;
   105             }
   106         }
   107         SDL_Delay(100);
   108     }
   109 
   110     for (i = 0; i < devcount; i++) {
   111         if (cbd[i].dev) {
   112             SDL_PauseAudioDevice(cbd[i].dev, 1);
   113             SDL_CloseAudioDevice(cbd[i].dev);
   114         }
   115     }
   116 
   117     SDL_Log("All done!\n");
   118 }
   119 
   120 
   121 int
   122 main(int argc, char **argv)
   123 {
   124     int devcount = 0;
   125 
   126 	/* Enable standard application logging */
   127     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   128 
   129     /* Load the SDL library */
   130     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   131         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   132         return (1);
   133     }
   134 
   135     SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   136     
   137     #if __NACL__
   138     SDL_RWUmount("/");
   139     SDL_RWMount(
   140         "",  /* source */
   141         "/",  /* target */
   142         "httpfs",  /* filesystemtype */
   143         0,  /* mountflags */
   144         "");  /* data specific to the html5fs type */
   145 #endif
   146 
   147     devcount = SDL_GetNumAudioDevices(0);
   148     if (devcount < 1) {
   149         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio devices!\n");
   150     } else {
   151         if (argv[1] == NULL) {
   152             argv[1] = "sample.wav";
   153         }
   154 
   155         /* Load the wave file into memory */
   156         if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) {
   157             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1],
   158                     SDL_GetError());
   159         } else {
   160             test_multi_audio(devcount);
   161             SDL_FreeWAV(sound);
   162         }
   163     }
   164 
   165     SDL_Quit();
   166     return 0;
   167 }