19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Program to load a wave file and loop playing it using SDL sound */
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* loopwaves.c is much more robust in handling WAVE files --
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	This is only for simple WAVEs
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h>
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdlib.h>
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if HAVE_SIGNAL_H
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h>
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct {
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioSpec spec;
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8   *sound;			/* Pointer to wave data */
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32   soundlen;		/* Length of wave data */
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int      soundpos;		/* Current play position */
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} wave;
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void quit(int rc)
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Quit();
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	exit(rc);
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDLCALL fillerup(void *unused, Uint8 *stream, int len)
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *waveptr;
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int    waveleft;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the pointers */
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveptr = wave.sound + wave.soundpos;
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveleft = wave.soundlen - wave.soundpos;
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Go! */
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( waveleft <= len ) {
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(stream, waveptr, waveleft);
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		stream += waveleft;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		len -= waveleft;
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		waveptr = wave.sound;
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		waveleft = wave.soundlen;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		wave.soundpos = 0;
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memcpy(stream, waveptr, len);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	wave.soundpos += len;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int done = 0;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid poked(int sig)
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	done = 1;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint main(int argc, char *argv[])
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char name[32];
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Load the SDL library */
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(1);
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( argv[1] == NULL ) {
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		argv[1] = "sample.wav";
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Load the wave file into memory */
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_LoadWAV(argv[1],
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "Couldn't load %s: %s\n",
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						argv[1], SDL_GetError());
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		quit(1);
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	wave.spec.callback = fillerup;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if HAVE_SIGNAL_H
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the signals */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SIGHUP
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	signal(SIGHUP, poked);
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	signal(SIGINT, poked);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SIGQUIT
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	signal(SIGQUIT, poked);
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	signal(SIGTERM, poked);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_SIGNAL_H */
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize fillerup() variables */
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeWAV(wave.sound);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		quit(2);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_PauseAudio(0);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Let the audio run */
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Delay(1000);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Clean up on signal */
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CloseAudio();
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_FreeWAV(wave.sound);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Quit();
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
115