test/loopwave.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 12 Jul 2007 06:53:20 +0000
changeset 2179 8b76cc268771
parent 2145 5c14978b1e97
child 3359 aecbdf3362c3
permissions -rw-r--r--
Visual C++ compiler warning fixes
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@1895
    19
struct
slouken@1895
    20
{
slouken@1895
    21
    SDL_AudioSpec spec;
slouken@1895
    22
    Uint8 *sound;               /* Pointer to wave data */
slouken@1895
    23
    Uint32 soundlen;            /* Length of wave data */
slouken@1895
    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@1895
    29
static void
slouken@1895
    30
quit(int rc)
icculus@1151
    31
{
slouken@1895
    32
    SDL_Quit();
slouken@1895
    33
    exit(rc);
icculus@1151
    34
}
icculus@1151
    35
icculus@1151
    36
slouken@1895
    37
void SDLCALL
slouken@1895
    38
fillerup(void *unused, Uint8 * stream, int len)
slouken@0
    39
{
slouken@1895
    40
    Uint8 *waveptr;
slouken@1895
    41
    int waveleft;
slouken@0
    42
slouken@1895
    43
    /* Set up the pointers */
slouken@1895
    44
    waveptr = wave.sound + wave.soundpos;
slouken@1895
    45
    waveleft = wave.soundlen - wave.soundpos;
slouken@0
    46
slouken@1895
    47
    /* Go! */
slouken@1895
    48
    while (waveleft <= len) {
icculus@2145
    49
        SDL_memcpy(stream, waveptr, waveleft);
slouken@1895
    50
        stream += waveleft;
slouken@1895
    51
        len -= waveleft;
slouken@1895
    52
        waveptr = wave.sound;
slouken@1895
    53
        waveleft = wave.soundlen;
slouken@1895
    54
        wave.soundpos = 0;
slouken@1895
    55
    }
icculus@2145
    56
    SDL_memcpy(stream, waveptr, len);
slouken@1895
    57
    wave.soundpos += len;
slouken@0
    58
}
slouken@0
    59
slouken@0
    60
static int done = 0;
slouken@1895
    61
void
slouken@1895
    62
poked(int sig)
slouken@0
    63
{
slouken@1895
    64
    done = 1;
slouken@0
    65
}
slouken@0
    66
slouken@1895
    67
int
slouken@1895
    68
main(int argc, char *argv[])
slouken@0
    69
{
slouken@1895
    70
    /* Load the SDL library */
slouken@1895
    71
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
slouken@1895
    72
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    73
        return (1);
slouken@1895
    74
    }
icculus@2049
    75
slouken@1895
    76
    if (argv[1] == NULL) {
slouken@1895
    77
        argv[1] = "sample.wav";
slouken@1895
    78
    }
slouken@1895
    79
    /* Load the wave file into memory */
slouken@1895
    80
    if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
slouken@1895
    81
        fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
slouken@1895
    82
        quit(1);
slouken@1895
    83
    }
slouken@1895
    84
slouken@1895
    85
    wave.spec.callback = fillerup;
slouken@1463
    86
#if HAVE_SIGNAL_H
slouken@1895
    87
    /* Set the signals */
slouken@0
    88
#ifdef SIGHUP
slouken@1895
    89
    signal(SIGHUP, poked);
slouken@0
    90
#endif
slouken@1895
    91
    signal(SIGINT, poked);
slouken@0
    92
#ifdef SIGQUIT
slouken@1895
    93
    signal(SIGQUIT, poked);
slouken@0
    94
#endif
slouken@1895
    95
    signal(SIGTERM, poked);
slouken@1463
    96
#endif /* HAVE_SIGNAL_H */
slouken@0
    97
slouken@1895
    98
    /* Initialize fillerup() variables */
slouken@1895
    99
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
slouken@1895
   100
        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@1895
   101
        SDL_FreeWAV(wave.sound);
slouken@1895
   102
        quit(2);
slouken@1895
   103
    }
slouken@1895
   104
    SDL_PauseAudio(0);
slouken@0
   105
slouken@1895
   106
    /* Let the audio run */
slouken@1895
   107
    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
slouken@1895
   108
        SDL_Delay(1000);
slouken@0
   109
slouken@1895
   110
    /* Clean up on signal */
slouken@1895
   111
    SDL_CloseAudio();
slouken@1895
   112
    SDL_FreeWAV(wave.sound);
slouken@1895
   113
    SDL_Quit();
slouken@1895
   114
    return (0);
slouken@0
   115
}