test/loopwave.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1660 8b9d79e7eacf
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
     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],
    97                      &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
    98         fprintf (stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError ());
    99         quit (1);
   100     }
   101 
   102     wave.spec.callback = fillerup;
   103 #if HAVE_SIGNAL_H
   104     /* Set the signals */
   105 #ifdef SIGHUP
   106     signal (SIGHUP, poked);
   107 #endif
   108     signal (SIGINT, poked);
   109 #ifdef SIGQUIT
   110     signal (SIGQUIT, poked);
   111 #endif
   112     signal (SIGTERM, poked);
   113 #endif /* HAVE_SIGNAL_H */
   114 
   115     /* Initialize fillerup() variables */
   116     if (SDL_OpenAudio (&wave.spec, NULL) < 0) {
   117         fprintf (stderr, "Couldn't open audio: %s\n", SDL_GetError ());
   118         SDL_FreeWAV (wave.sound);
   119         quit (2);
   120     }
   121     SDL_PauseAudio (0);
   122 
   123     /* Let the audio run */
   124     printf ("Using audio driver: %s\n", SDL_GetCurrentAudioDriver ());
   125     while (!done && (SDL_GetAudioStatus () == SDL_AUDIO_PLAYING))
   126         SDL_Delay (1000);
   127 
   128     /* Clean up on signal */
   129     SDL_CloseAudio ();
   130     SDL_FreeWAV (wave.sound);
   131     SDL_Quit ();
   132     return (0);
   133 }