test/loopwave.c
author David Ludwig <dludwig@pobox.com>
Thu, 22 Nov 2012 22:35:38 -0500
changeset 8354 c086e9e73916
parent 5535 96594ac5fd1a
child 8377 3a7e8110201e
permissions -rw-r--r--
WinRT: modified the loopwave test to run if and when argv is NULL
     1 /*
     2   Copyright (C) 1997-2011 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 
    13 /* Program to load a wave file and loop playing it using SDL sound */
    14 
    15 /* loopwaves.c is much more robust in handling WAVE files -- 
    16 	This is only for simple WAVEs
    17 */
    18 #include "SDL_config.h"
    19 
    20 #include <stdio.h>
    21 #include <stdlib.h>
    22 
    23 #if HAVE_SIGNAL_H
    24 #include <signal.h>
    25 #endif
    26 
    27 #include "SDL.h"
    28 #include "SDL_audio.h"
    29 
    30 struct
    31 {
    32     SDL_AudioSpec spec;
    33     Uint8 *sound;               /* Pointer to wave data */
    34     Uint32 soundlen;            /* Length of wave data */
    35     int soundpos;               /* Current play position */
    36 } wave;
    37 
    38 
    39 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    40 static void
    41 quit(int rc)
    42 {
    43     SDL_Quit();
    44     exit(rc);
    45 }
    46 
    47 
    48 void SDLCALL
    49 fillerup(void *unused, Uint8 * stream, int len)
    50 {
    51     Uint8 *waveptr;
    52     int waveleft;
    53 
    54     /* Set up the pointers */
    55     waveptr = wave.sound + wave.soundpos;
    56     waveleft = wave.soundlen - wave.soundpos;
    57 
    58     /* Go! */
    59     while (waveleft <= len) {
    60         SDL_memcpy(stream, waveptr, waveleft);
    61         stream += waveleft;
    62         len -= waveleft;
    63         waveptr = wave.sound;
    64         waveleft = wave.soundlen;
    65         wave.soundpos = 0;
    66     }
    67     SDL_memcpy(stream, waveptr, len);
    68     wave.soundpos += len;
    69 }
    70 
    71 static int done = 0;
    72 void
    73 poked(int sig)
    74 {
    75     done = 1;
    76 }
    77 
    78 int
    79 main(int argc, char *argv[])
    80 {
    81     char filename[4096];
    82 
    83     /* Load the SDL library */
    84     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    85         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    86         return (1);
    87     }
    88 
    89     if (argc >= 1) {
    90         SDL_strlcpy(filename, argv[1], sizeof(filename));
    91     } else {
    92         SDL_strlcpy(filename, "sample.wav", sizeof(filename));
    93     }
    94     /* Load the wave file into memory */
    95     if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
    96         fprintf(stderr, "Couldn't load %s: %s\n", filename, SDL_GetError());
    97         quit(1);
    98     }
    99 
   100     wave.spec.callback = fillerup;
   101 #if HAVE_SIGNAL_H
   102     /* Set the signals */
   103 #ifdef SIGHUP
   104     signal(SIGHUP, poked);
   105 #endif
   106     signal(SIGINT, poked);
   107 #ifdef SIGQUIT
   108     signal(SIGQUIT, poked);
   109 #endif
   110     signal(SIGTERM, poked);
   111 #endif /* HAVE_SIGNAL_H */
   112 
   113     /* Initialize fillerup() variables */
   114     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   115         fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   116         SDL_FreeWAV(wave.sound);
   117         quit(2);
   118     }
   119 
   120     printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   121 
   122     /* Let the audio run */
   123     SDL_PauseAudio(0);
   124     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   125         SDL_Delay(1000);
   126 
   127     /* Clean up on signal */
   128     SDL_CloseAudio();
   129     SDL_FreeWAV(wave.sound);
   130     SDL_Quit();
   131     return (0);
   132 }