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
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@1668
    30
quit(int rc)
icculus@1151
    31
{
slouken@1668
    32
    SDL_Quit();
slouken@1668
    33
    exit(rc);
icculus@1151
    34
}
icculus@1151
    35
icculus@1151
    36
slouken@1662
    37
void SDLCALL
slouken@1668
    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@1668
    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@1668
    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@1668
    62
poked(int sig)
slouken@0
    63
{
slouken@1662
    64
    done = 1;
slouken@0
    65
}
slouken@0
    66
slouken@1662
    67
int
slouken@1668
    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@1668
    73
    n = SDL_GetNumAudioDrivers();
slouken@1662
    74
    if (n == 0) {
slouken@1668
    75
        printf("No built-in audio drivers\n");
slouken@1662
    76
    } else {
slouken@1668
    77
        printf("Built-in audio drivers:");
slouken@1662
    78
        for (i = 0; i < n; ++i) {
slouken@1662
    79
            if (i > 0) {
slouken@1668
    80
                printf(",");
slouken@1662
    81
            }
slouken@1668
    82
            printf(" %s", SDL_GetAudioDriver(i));
slouken@1662
    83
        }
slouken@1668
    84
        printf("\n");
slouken@1662
    85
    }
slouken@0
    86
slouken@1662
    87
    /* Load the SDL library */
slouken@1668
    88
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
slouken@1668
    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@1668
    96
    if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
slouken@1668
    97
        fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
slouken@1668
    98
        quit(1);
slouken@1662
    99
    }
slouken@1463
   100
slouken@1662
   101
    wave.spec.callback = fillerup;
slouken@1463
   102
#if HAVE_SIGNAL_H
slouken@1662
   103
    /* Set the signals */
slouken@0
   104
#ifdef SIGHUP
slouken@1668
   105
    signal(SIGHUP, poked);
slouken@0
   106
#endif
slouken@1668
   107
    signal(SIGINT, poked);
slouken@0
   108
#ifdef SIGQUIT
slouken@1668
   109
    signal(SIGQUIT, poked);
slouken@0
   110
#endif
slouken@1668
   111
    signal(SIGTERM, poked);
slouken@1463
   112
#endif /* HAVE_SIGNAL_H */
slouken@0
   113
slouken@1662
   114
    /* Initialize fillerup() variables */
slouken@1668
   115
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
slouken@1668
   116
        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@1668
   117
        SDL_FreeWAV(wave.sound);
slouken@1668
   118
        quit(2);
slouken@1662
   119
    }
slouken@1668
   120
    SDL_PauseAudio(0);
slouken@0
   121
slouken@1662
   122
    /* Let the audio run */
slouken@1668
   123
    printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
slouken@1668
   124
    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
slouken@1668
   125
        SDL_Delay(1000);
slouken@0
   126
slouken@1662
   127
    /* Clean up on signal */
slouken@1668
   128
    SDL_CloseAudio();
slouken@1668
   129
    SDL_FreeWAV(wave.sound);
slouken@1668
   130
    SDL_Quit();
slouken@1662
   131
    return (0);
slouken@0
   132
}