test/loopwave.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 1151 be9c9c8f6d53
permissions -rw-r--r--
Initial revision
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@0
     7
slouken@0
     8
#include <stdio.h>
slouken@0
     9
#include <stdlib.h>
slouken@0
    10
#include <signal.h>
slouken@0
    11
slouken@0
    12
#include "SDL.h"
slouken@0
    13
#include "SDL_audio.h"
slouken@0
    14
slouken@0
    15
struct {
slouken@0
    16
	SDL_AudioSpec spec;
slouken@0
    17
	Uint8   *sound;			/* Pointer to wave data */
slouken@0
    18
	Uint32   soundlen;		/* Length of wave data */
slouken@0
    19
	int      soundpos;		/* Current play position */
slouken@0
    20
} wave;
slouken@0
    21
slouken@0
    22
void fillerup(void *unused, Uint8 *stream, int len)
slouken@0
    23
{
slouken@0
    24
	Uint8 *waveptr;
slouken@0
    25
	int    waveleft;
slouken@0
    26
slouken@0
    27
	/* Set up the pointers */
slouken@0
    28
	waveptr = wave.sound + wave.soundpos;
slouken@0
    29
	waveleft = wave.soundlen - wave.soundpos;
slouken@0
    30
slouken@0
    31
	/* Go! */
slouken@0
    32
	while ( waveleft <= len ) {
slouken@0
    33
		SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
slouken@0
    34
		stream += waveleft;
slouken@0
    35
		len -= waveleft;
slouken@0
    36
		waveptr = wave.sound;
slouken@0
    37
		waveleft = wave.soundlen;
slouken@0
    38
		wave.soundpos = 0;
slouken@0
    39
	}
slouken@0
    40
	SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
slouken@0
    41
	wave.soundpos += len;
slouken@0
    42
}
slouken@0
    43
slouken@0
    44
static int done = 0;
slouken@0
    45
void poked(int sig)
slouken@0
    46
{
slouken@0
    47
	done = 1;
slouken@0
    48
}
slouken@0
    49
slouken@0
    50
int main(int argc, char *argv[])
slouken@0
    51
{
slouken@0
    52
	char name[32];
slouken@0
    53
slouken@0
    54
	/* Load the SDL library */
slouken@0
    55
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
    56
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
    57
		exit(1);
slouken@0
    58
	}
slouken@0
    59
	atexit(SDL_Quit);
slouken@0
    60
slouken@0
    61
	if ( argv[1] == NULL ) {
slouken@0
    62
		fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]);
slouken@0
    63
		exit(1);
slouken@0
    64
	}
slouken@0
    65
slouken@0
    66
	/* Load the wave file into memory */
slouken@0
    67
	if ( SDL_LoadWAV(argv[1],
slouken@0
    68
			&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
slouken@0
    69
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
    70
						argv[1], SDL_GetError());
slouken@0
    71
		exit(1);
slouken@0
    72
	}
slouken@0
    73
	wave.spec.callback = fillerup;
slouken@0
    74
slouken@0
    75
	/* Set the signals */
slouken@0
    76
#ifdef SIGHUP
slouken@0
    77
	signal(SIGHUP, poked);
slouken@0
    78
#endif
slouken@0
    79
	signal(SIGINT, poked);
slouken@0
    80
#ifdef SIGQUIT
slouken@0
    81
	signal(SIGQUIT, poked);
slouken@0
    82
#endif
slouken@0
    83
	signal(SIGTERM, poked);
slouken@0
    84
slouken@0
    85
	/* Initialize fillerup() variables */
slouken@0
    86
	if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
slouken@0
    87
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@0
    88
		SDL_FreeWAV(wave.sound);
slouken@0
    89
		exit(2);
slouken@0
    90
	}
slouken@0
    91
	SDL_PauseAudio(0);
slouken@0
    92
slouken@0
    93
	/* Let the audio run */
slouken@0
    94
	printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
slouken@0
    95
	while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
slouken@0
    96
		SDL_Delay(1000);
slouken@0
    97
slouken@0
    98
	/* Clean up on signal */
slouken@0
    99
	SDL_CloseAudio();
slouken@0
   100
	SDL_FreeWAV(wave.sound);
slouken@0
   101
	return(0);
slouken@0
   102
}