test/loopwave.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 17 Oct 2006 09:15:21 +0000
changeset 2049 5f6550e5184f
parent 1895 c121d94672cb
child 2145 5c14978b1e97
permissions -rw-r--r--
Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
     1 
     2 /* Program to load a wave file and loop playing it using SDL sound */
     3 
     4 /* loopwaves.c is much more robust in handling WAVE files -- 
     5 	This is only for simple WAVEs
     6 */
     7 #include "SDL_config.h"
     8 
     9 #include <stdio.h>
    10 #include <stdlib.h>
    11 
    12 #if HAVE_SIGNAL_H
    13 #include <signal.h>
    14 #endif
    15 
    16 #include "SDL.h"
    17 #include "SDL_audio.h"
    18 
    19 struct
    20 {
    21     SDL_AudioSpec spec;
    22     Uint8 *sound;               /* Pointer to wave data */
    23     Uint32 soundlen;            /* Length of wave data */
    24     int soundpos;               /* Current play position */
    25 } wave;
    26 
    27 
    28 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    29 static void
    30 quit(int rc)
    31 {
    32     SDL_Quit();
    33     exit(rc);
    34 }
    35 
    36 
    37 void SDLCALL
    38 fillerup(void *unused, Uint8 * stream, int len)
    39 {
    40     Uint8 *waveptr;
    41     int waveleft;
    42 
    43     /* Set up the pointers */
    44     waveptr = wave.sound + wave.soundpos;
    45     waveleft = wave.soundlen - wave.soundpos;
    46 
    47     /* Go! */
    48     while (waveleft <= len) {
    49         SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
    50         stream += waveleft;
    51         len -= waveleft;
    52         waveptr = wave.sound;
    53         waveleft = wave.soundlen;
    54         wave.soundpos = 0;
    55     }
    56     SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
    57     wave.soundpos += len;
    58 }
    59 
    60 static int done = 0;
    61 void
    62 poked(int sig)
    63 {
    64     done = 1;
    65 }
    66 
    67 int
    68 main(int argc, char *argv[])
    69 {
    70     int i, n;
    71 
    72     /* Load the SDL library */
    73     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    74         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    75         return (1);
    76     }
    77 
    78     if (argv[1] == NULL) {
    79         argv[1] = "sample.wav";
    80     }
    81     /* Load the wave file into memory */
    82     if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
    83         fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
    84         quit(1);
    85     }
    86 
    87     wave.spec.callback = fillerup;
    88 #if HAVE_SIGNAL_H
    89     /* Set the signals */
    90 #ifdef SIGHUP
    91     signal(SIGHUP, poked);
    92 #endif
    93     signal(SIGINT, poked);
    94 #ifdef SIGQUIT
    95     signal(SIGQUIT, poked);
    96 #endif
    97     signal(SIGTERM, poked);
    98 #endif /* HAVE_SIGNAL_H */
    99 
   100     /* Initialize fillerup() variables */
   101     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   102         fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   103         SDL_FreeWAV(wave.sound);
   104         quit(2);
   105     }
   106     SDL_PauseAudio(0);
   107 
   108     /* Let the audio run */
   109     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   110         SDL_Delay(1000);
   111 
   112     /* Clean up on signal */
   113     SDL_CloseAudio();
   114     SDL_FreeWAV(wave.sound);
   115     SDL_Quit();
   116     return (0);
   117 }