test/loopwave.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 May 2006 08:18:28 +0000
branchSDL-1.3
changeset 1659 14717b52abc0
parent 1463 0394f8ebc42d
child 1660 8b9d79e7eacf
child 1895 c121d94672cb
child 4002 e905bb5d9bc3
permissions -rw-r--r--
Merge trunk-1.3-3
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@1463
     7
#include "SDL_config.h"
slouken@0
     8
slouken@0
     9
#include <stdio.h>
slouken@0
    10
#include <stdlib.h>
slouken@1463
    11
slouken@1463
    12
#if HAVE_SIGNAL_H
slouken@0
    13
#include <signal.h>
slouken@1463
    14
#endif
slouken@0
    15
slouken@0
    16
#include "SDL.h"
slouken@0
    17
#include "SDL_audio.h"
slouken@0
    18
slouken@0
    19
struct {
slouken@0
    20
	SDL_AudioSpec spec;
slouken@0
    21
	Uint8   *sound;			/* Pointer to wave data */
slouken@0
    22
	Uint32   soundlen;		/* Length of wave data */
slouken@0
    23
	int      soundpos;		/* Current play position */
slouken@0
    24
} wave;
slouken@0
    25
icculus@1151
    26
icculus@1151
    27
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@1151
    28
static void quit(int rc)
icculus@1151
    29
{
icculus@1151
    30
	SDL_Quit();
icculus@1151
    31
	exit(rc);
icculus@1151
    32
}
icculus@1151
    33
icculus@1151
    34
slouken@1659
    35
void SDLCALL fillerup(void *unused, Uint8 *stream, int len)
slouken@0
    36
{
slouken@0
    37
	Uint8 *waveptr;
slouken@0
    38
	int    waveleft;
slouken@0
    39
slouken@0
    40
	/* Set up the pointers */
slouken@0
    41
	waveptr = wave.sound + wave.soundpos;
slouken@0
    42
	waveleft = wave.soundlen - wave.soundpos;
slouken@0
    43
slouken@0
    44
	/* Go! */
slouken@0
    45
	while ( waveleft <= len ) {
slouken@0
    46
		SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
slouken@0
    47
		stream += waveleft;
slouken@0
    48
		len -= waveleft;
slouken@0
    49
		waveptr = wave.sound;
slouken@0
    50
		waveleft = wave.soundlen;
slouken@0
    51
		wave.soundpos = 0;
slouken@0
    52
	}
slouken@0
    53
	SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
slouken@0
    54
	wave.soundpos += len;
slouken@0
    55
}
slouken@0
    56
slouken@0
    57
static int done = 0;
slouken@0
    58
void poked(int sig)
slouken@0
    59
{
slouken@0
    60
	done = 1;
slouken@0
    61
}
slouken@0
    62
slouken@0
    63
int main(int argc, char *argv[])
slouken@0
    64
{
slouken@0
    65
	char name[32];
slouken@0
    66
slouken@0
    67
	/* Load the SDL library */
slouken@0
    68
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
    69
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    70
		return(1);
slouken@0
    71
	}
slouken@0
    72
	if ( argv[1] == NULL ) {
slouken@1463
    73
		argv[1] = "sample.wav";
slouken@0
    74
	}
slouken@0
    75
	/* Load the wave file into memory */
slouken@0
    76
	if ( SDL_LoadWAV(argv[1],
slouken@0
    77
			&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
slouken@0
    78
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
    79
						argv[1], SDL_GetError());
icculus@1151
    80
		quit(1);
slouken@0
    81
	}
slouken@1463
    82
slouken@0
    83
	wave.spec.callback = fillerup;
slouken@1463
    84
#if HAVE_SIGNAL_H
slouken@0
    85
	/* Set the signals */
slouken@0
    86
#ifdef SIGHUP
slouken@0
    87
	signal(SIGHUP, poked);
slouken@0
    88
#endif
slouken@0
    89
	signal(SIGINT, poked);
slouken@0
    90
#ifdef SIGQUIT
slouken@0
    91
	signal(SIGQUIT, poked);
slouken@0
    92
#endif
slouken@0
    93
	signal(SIGTERM, poked);
slouken@1463
    94
#endif /* HAVE_SIGNAL_H */
slouken@0
    95
slouken@0
    96
	/* Initialize fillerup() variables */
slouken@0
    97
	if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
slouken@0
    98
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@0
    99
		SDL_FreeWAV(wave.sound);
icculus@1151
   100
		quit(2);
slouken@0
   101
	}
slouken@0
   102
	SDL_PauseAudio(0);
slouken@0
   103
slouken@0
   104
	/* Let the audio run */
slouken@0
   105
	printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
slouken@0
   106
	while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
slouken@0
   107
		SDL_Delay(1000);
slouken@0
   108
slouken@0
   109
	/* Clean up on signal */
slouken@0
   110
	SDL_CloseAudio();
slouken@0
   111
	SDL_FreeWAV(wave.sound);
icculus@1151
   112
	SDL_Quit();
slouken@0
   113
	return(0);
slouken@0
   114
}