test/loopwave.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
     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     /* Print available audio drivers */
    73     n = SDL_GetNumAudioDrivers();
    74     if (n == 0) {
    75         printf("No built-in audio drivers\n");
    76     } else {
    77         printf("Built-in audio drivers:");
    78         for (i = 0; i < n; ++i) {
    79             if (i > 0) {
    80                 printf(",");
    81             }
    82             printf(" %s", SDL_GetAudioDriver(i));
    83         }
    84         printf("\n");
    85     }
    86 
    87     /* Load the SDL library */
    88     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    89         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    90         return (1);
    91     }
    92     if (argv[1] == NULL) {
    93         argv[1] = "sample.wav";
    94     }
    95     /* Load the wave file into memory */
    96     if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
    97         fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
    98         quit(1);
    99     }
   100 
   101     wave.spec.callback = fillerup;
   102 #if HAVE_SIGNAL_H
   103     /* Set the signals */
   104 #ifdef SIGHUP
   105     signal(SIGHUP, poked);
   106 #endif
   107     signal(SIGINT, poked);
   108 #ifdef SIGQUIT
   109     signal(SIGQUIT, poked);
   110 #endif
   111     signal(SIGTERM, poked);
   112 #endif /* HAVE_SIGNAL_H */
   113 
   114     /* Initialize fillerup() variables */
   115     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   116         fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   117         SDL_FreeWAV(wave.sound);
   118         quit(2);
   119     }
   120     SDL_PauseAudio(0);
   121 
   122     /* Let the audio run */
   123     printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   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 }