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