test/loopwave.c
changeset 0 74212992fb08
child 1151 be9c9c8f6d53
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/loopwave.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,102 @@
     1.4 +
     1.5 +/* Program to load a wave file and loop playing it using SDL sound */
     1.6 +
     1.7 +/* loopwaves.c is much more robust in handling WAVE files -- 
     1.8 +	This is only for simple WAVEs
     1.9 +*/
    1.10 +
    1.11 +#include <stdio.h>
    1.12 +#include <stdlib.h>
    1.13 +#include <signal.h>
    1.14 +
    1.15 +#include "SDL.h"
    1.16 +#include "SDL_audio.h"
    1.17 +
    1.18 +struct {
    1.19 +	SDL_AudioSpec spec;
    1.20 +	Uint8   *sound;			/* Pointer to wave data */
    1.21 +	Uint32   soundlen;		/* Length of wave data */
    1.22 +	int      soundpos;		/* Current play position */
    1.23 +} wave;
    1.24 +
    1.25 +void fillerup(void *unused, Uint8 *stream, int len)
    1.26 +{
    1.27 +	Uint8 *waveptr;
    1.28 +	int    waveleft;
    1.29 +
    1.30 +	/* Set up the pointers */
    1.31 +	waveptr = wave.sound + wave.soundpos;
    1.32 +	waveleft = wave.soundlen - wave.soundpos;
    1.33 +
    1.34 +	/* Go! */
    1.35 +	while ( waveleft <= len ) {
    1.36 +		SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
    1.37 +		stream += waveleft;
    1.38 +		len -= waveleft;
    1.39 +		waveptr = wave.sound;
    1.40 +		waveleft = wave.soundlen;
    1.41 +		wave.soundpos = 0;
    1.42 +	}
    1.43 +	SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
    1.44 +	wave.soundpos += len;
    1.45 +}
    1.46 +
    1.47 +static int done = 0;
    1.48 +void poked(int sig)
    1.49 +{
    1.50 +	done = 1;
    1.51 +}
    1.52 +
    1.53 +int main(int argc, char *argv[])
    1.54 +{
    1.55 +	char name[32];
    1.56 +
    1.57 +	/* Load the SDL library */
    1.58 +	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
    1.59 +		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.60 +		exit(1);
    1.61 +	}
    1.62 +	atexit(SDL_Quit);
    1.63 +
    1.64 +	if ( argv[1] == NULL ) {
    1.65 +		fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]);
    1.66 +		exit(1);
    1.67 +	}
    1.68 +
    1.69 +	/* Load the wave file into memory */
    1.70 +	if ( SDL_LoadWAV(argv[1],
    1.71 +			&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
    1.72 +		fprintf(stderr, "Couldn't load %s: %s\n",
    1.73 +						argv[1], SDL_GetError());
    1.74 +		exit(1);
    1.75 +	}
    1.76 +	wave.spec.callback = fillerup;
    1.77 +
    1.78 +	/* Set the signals */
    1.79 +#ifdef SIGHUP
    1.80 +	signal(SIGHUP, poked);
    1.81 +#endif
    1.82 +	signal(SIGINT, poked);
    1.83 +#ifdef SIGQUIT
    1.84 +	signal(SIGQUIT, poked);
    1.85 +#endif
    1.86 +	signal(SIGTERM, poked);
    1.87 +
    1.88 +	/* Initialize fillerup() variables */
    1.89 +	if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
    1.90 +		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
    1.91 +		SDL_FreeWAV(wave.sound);
    1.92 +		exit(2);
    1.93 +	}
    1.94 +	SDL_PauseAudio(0);
    1.95 +
    1.96 +	/* Let the audio run */
    1.97 +	printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
    1.98 +	while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
    1.99 +		SDL_Delay(1000);
   1.100 +
   1.101 +	/* Clean up on signal */
   1.102 +	SDL_CloseAudio();
   1.103 +	SDL_FreeWAV(wave.sound);
   1.104 +	return(0);
   1.105 +}