Merged r3140:3141 from branches/SDL-1.2: loopwave fix.
2 /* Program to load a wave file and loop playing it using SDL sound */
4 /* loopwaves.c is much more robust in handling WAVE files --
5 This is only for simple WAVEs
7 #include "SDL_config.h"
17 #include "SDL_audio.h"
22 Uint8 *sound; /* Pointer to wave data */
23 Uint32 soundlen; /* Length of wave data */
24 int soundpos; /* Current play position */
28 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
38 fillerup(void *unused, Uint8 * stream, int len)
43 /* Set up the pointers */
44 waveptr = wave.sound + wave.soundpos;
45 waveleft = wave.soundlen - wave.soundpos;
48 while (waveleft <= len) {
49 SDL_memcpy(stream, waveptr, waveleft);
53 waveleft = wave.soundlen;
56 SDL_memcpy(stream, waveptr, len);
68 main(int argc, char *argv[])
72 /* Load the SDL library */
73 if (SDL_Init(SDL_INIT_AUDIO) < 0) {
74 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
78 if (argv[1] == NULL) {
79 argv[1] = "sample.wav";
81 /* Load the wave file into memory */
82 if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
83 fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
87 wave.spec.callback = fillerup;
91 signal(SIGHUP, poked);
93 signal(SIGINT, poked);
95 signal(SIGQUIT, poked);
97 signal(SIGTERM, poked);
98 #endif /* HAVE_SIGNAL_H */
100 /* Initialize fillerup() variables */
101 if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
102 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
103 SDL_FreeWAV(wave.sound);
108 /* Let the audio run */
109 while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
112 /* Clean up on signal */
114 SDL_FreeWAV(wave.sound);