test/loopwave.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 28 Sep 2005 11:36:20 +0000
changeset 1151 be9c9c8f6d53
parent 0 74212992fb08
child 1463 0394f8ebc42d
permissions -rw-r--r--
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
if SDL is built with a non-cdecl calling convention, and it's just generally
bad practice anyhow.

Now programs explicitly call SDL_Quit() where appropriate, wrap SDL_Quit() in
a cdecl function where it can't be avoided, and rely on the parachute where
a crash might have hit the atexit() before (these ARE test programs, after
all!).
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
icculus@1151
    22
icculus@1151
    23
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@1151
    24
static void quit(int rc)
icculus@1151
    25
{
icculus@1151
    26
	SDL_Quit();
icculus@1151
    27
	exit(rc);
icculus@1151
    28
}
icculus@1151
    29
icculus@1151
    30
slouken@0
    31
void fillerup(void *unused, Uint8 *stream, int len)
slouken@0
    32
{
slouken@0
    33
	Uint8 *waveptr;
slouken@0
    34
	int    waveleft;
slouken@0
    35
slouken@0
    36
	/* Set up the pointers */
slouken@0
    37
	waveptr = wave.sound + wave.soundpos;
slouken@0
    38
	waveleft = wave.soundlen - wave.soundpos;
slouken@0
    39
slouken@0
    40
	/* Go! */
slouken@0
    41
	while ( waveleft <= len ) {
slouken@0
    42
		SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
slouken@0
    43
		stream += waveleft;
slouken@0
    44
		len -= waveleft;
slouken@0
    45
		waveptr = wave.sound;
slouken@0
    46
		waveleft = wave.soundlen;
slouken@0
    47
		wave.soundpos = 0;
slouken@0
    48
	}
slouken@0
    49
	SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
slouken@0
    50
	wave.soundpos += len;
slouken@0
    51
}
slouken@0
    52
slouken@0
    53
static int done = 0;
slouken@0
    54
void poked(int sig)
slouken@0
    55
{
slouken@0
    56
	done = 1;
slouken@0
    57
}
slouken@0
    58
slouken@0
    59
int main(int argc, char *argv[])
slouken@0
    60
{
slouken@0
    61
	char name[32];
slouken@0
    62
slouken@0
    63
	/* Load the SDL library */
slouken@0
    64
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
    65
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    66
		return(1);
slouken@0
    67
	}
slouken@0
    68
slouken@0
    69
	if ( argv[1] == NULL ) {
slouken@0
    70
		fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]);
icculus@1151
    71
		quit(1);
slouken@0
    72
	}
slouken@0
    73
slouken@0
    74
	/* Load the wave file into memory */
slouken@0
    75
	if ( SDL_LoadWAV(argv[1],
slouken@0
    76
			&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
slouken@0
    77
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
    78
						argv[1], SDL_GetError());
icculus@1151
    79
		quit(1);
slouken@0
    80
	}
slouken@0
    81
	wave.spec.callback = fillerup;
slouken@0
    82
slouken@0
    83
	/* Set the signals */
slouken@0
    84
#ifdef SIGHUP
slouken@0
    85
	signal(SIGHUP, poked);
slouken@0
    86
#endif
slouken@0
    87
	signal(SIGINT, poked);
slouken@0
    88
#ifdef SIGQUIT
slouken@0
    89
	signal(SIGQUIT, poked);
slouken@0
    90
#endif
slouken@0
    91
	signal(SIGTERM, poked);
slouken@0
    92
slouken@0
    93
	/* Initialize fillerup() variables */
slouken@0
    94
	if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
slouken@0
    95
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@0
    96
		SDL_FreeWAV(wave.sound);
icculus@1151
    97
		quit(2);
slouken@0
    98
	}
slouken@0
    99
	SDL_PauseAudio(0);
slouken@0
   100
slouken@0
   101
	/* Let the audio run */
slouken@0
   102
	printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
slouken@0
   103
	while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
slouken@0
   104
		SDL_Delay(1000);
slouken@0
   105
slouken@0
   106
	/* Clean up on signal */
slouken@0
   107
	SDL_CloseAudio();
slouken@0
   108
	SDL_FreeWAV(wave.sound);
icculus@1151
   109
	SDL_Quit();
slouken@0
   110
	return(0);
slouken@0
   111
}