test/loopwave.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 2179 8b76cc268771
child 3359 aecbdf3362c3
permissions -rw-r--r--
Added testresample.c
     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_memcpy(stream, waveptr, waveleft);
    50         stream += waveleft;
    51         len -= waveleft;
    52         waveptr = wave.sound;
    53         waveleft = wave.soundlen;
    54         wave.soundpos = 0;
    55     }
    56     SDL_memcpy(stream, waveptr, len);
    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     /* Load the SDL library */
    71     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    72         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    73         return (1);
    74     }
    75 
    76     if (argv[1] == NULL) {
    77         argv[1] = "sample.wav";
    78     }
    79     /* Load the wave file into memory */
    80     if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
    81         fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
    82         quit(1);
    83     }
    84 
    85     wave.spec.callback = fillerup;
    86 #if HAVE_SIGNAL_H
    87     /* Set the signals */
    88 #ifdef SIGHUP
    89     signal(SIGHUP, poked);
    90 #endif
    91     signal(SIGINT, poked);
    92 #ifdef SIGQUIT
    93     signal(SIGQUIT, poked);
    94 #endif
    95     signal(SIGTERM, poked);
    96 #endif /* HAVE_SIGNAL_H */
    97 
    98     /* Initialize fillerup() variables */
    99     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   100         fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   101         SDL_FreeWAV(wave.sound);
   102         quit(2);
   103     }
   104     SDL_PauseAudio(0);
   105 
   106     /* Let the audio run */
   107     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   108         SDL_Delay(1000);
   109 
   110     /* Clean up on signal */
   111     SDL_CloseAudio();
   112     SDL_FreeWAV(wave.sound);
   113     SDL_Quit();
   114     return (0);
   115 }