test/loopwave.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 05 Jul 2007 02:45:47 +0000
changeset 2145 5c14978b1e97
parent 2049 5f6550e5184f
child 2179 8b76cc268771
permissions -rw-r--r--
Merged r3140:3141 from branches/SDL-1.2: loopwave fix.
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
    int i, n;
slouken@0
    71
slouken@1895
    72
    /* Load the SDL library */
slouken@1895
    73
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
slouken@1895
    74
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    75
        return (1);
slouken@1895
    76
    }
icculus@2049
    77
slouken@1895
    78
    if (argv[1] == NULL) {
slouken@1895
    79
        argv[1] = "sample.wav";
slouken@1895
    80
    }
slouken@1895
    81
    /* Load the wave file into memory */
slouken@1895
    82
    if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
slouken@1895
    83
        fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
slouken@1895
    84
        quit(1);
slouken@1895
    85
    }
slouken@1895
    86
slouken@1895
    87
    wave.spec.callback = fillerup;
slouken@1463
    88
#if HAVE_SIGNAL_H
slouken@1895
    89
    /* Set the signals */
slouken@0
    90
#ifdef SIGHUP
slouken@1895
    91
    signal(SIGHUP, poked);
slouken@0
    92
#endif
slouken@1895
    93
    signal(SIGINT, poked);
slouken@0
    94
#ifdef SIGQUIT
slouken@1895
    95
    signal(SIGQUIT, poked);
slouken@0
    96
#endif
slouken@1895
    97
    signal(SIGTERM, poked);
slouken@1463
    98
#endif /* HAVE_SIGNAL_H */
slouken@0
    99
slouken@1895
   100
    /* Initialize fillerup() variables */
slouken@1895
   101
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
slouken@1895
   102
        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@1895
   103
        SDL_FreeWAV(wave.sound);
slouken@1895
   104
        quit(2);
slouken@1895
   105
    }
slouken@1895
   106
    SDL_PauseAudio(0);
slouken@0
   107
slouken@1895
   108
    /* Let the audio run */
slouken@1895
   109
    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
slouken@1895
   110
        SDL_Delay(1000);
slouken@0
   111
slouken@1895
   112
    /* Clean up on signal */
slouken@1895
   113
    SDL_CloseAudio();
slouken@1895
   114
    SDL_FreeWAV(wave.sound);
slouken@1895
   115
    SDL_Quit();
slouken@1895
   116
    return (0);
slouken@0
   117
}