test/loopwave.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 20 May 2006 04:35:58 +0000
branchSDL-1.3
changeset 1660 8b9d79e7eacf
parent 1659 14717b52abc0
child 1662 782fd950bd46
permissions -rw-r--r--
Added API requested in bug #43:

Added SDL_GetNumVideoDrivers() and SDL_GetVideoDriver().
Replaced SDL_VideoDriverName() with SDL_GetCurrentVideoDriver()
Added SDL_GetNumAudioDrivers() and SDL_GetAudioDriver().
Replaced SDL_AudioDriverName() with SDL_GetCurrentAudioDriver()
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@1660
    65
	int i, n;
slouken@1660
    66
slouken@1660
    67
	/* Print available audio drivers */
slouken@1660
    68
	n = SDL_GetNumAudioDrivers();
slouken@1660
    69
	if ( n == 0 ) {
slouken@1660
    70
		printf("No built-in audio drivers\n");
slouken@1660
    71
	} else {
slouken@1660
    72
		printf("Built-in audio drivers:");
slouken@1660
    73
		for ( i = 0; i < n; ++i ) {
slouken@1660
    74
			if ( i > 0 ) {
slouken@1660
    75
				printf(",");
slouken@1660
    76
			}
slouken@1660
    77
			printf(" %s", SDL_GetAudioDriver(i));
slouken@1660
    78
		}
slouken@1660
    79
		printf("\n");
slouken@1660
    80
	}
slouken@0
    81
slouken@0
    82
	/* Load the SDL library */
slouken@0
    83
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
    84
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    85
		return(1);
slouken@0
    86
	}
slouken@0
    87
	if ( argv[1] == NULL ) {
slouken@1463
    88
		argv[1] = "sample.wav";
slouken@0
    89
	}
slouken@0
    90
	/* Load the wave file into memory */
slouken@0
    91
	if ( SDL_LoadWAV(argv[1],
slouken@0
    92
			&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
slouken@0
    93
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
    94
						argv[1], SDL_GetError());
icculus@1151
    95
		quit(1);
slouken@0
    96
	}
slouken@1463
    97
slouken@0
    98
	wave.spec.callback = fillerup;
slouken@1463
    99
#if HAVE_SIGNAL_H
slouken@0
   100
	/* Set the signals */
slouken@0
   101
#ifdef SIGHUP
slouken@0
   102
	signal(SIGHUP, poked);
slouken@0
   103
#endif
slouken@0
   104
	signal(SIGINT, poked);
slouken@0
   105
#ifdef SIGQUIT
slouken@0
   106
	signal(SIGQUIT, poked);
slouken@0
   107
#endif
slouken@0
   108
	signal(SIGTERM, poked);
slouken@1463
   109
#endif /* HAVE_SIGNAL_H */
slouken@0
   110
slouken@0
   111
	/* Initialize fillerup() variables */
slouken@0
   112
	if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
slouken@0
   113
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@0
   114
		SDL_FreeWAV(wave.sound);
icculus@1151
   115
		quit(2);
slouken@0
   116
	}
slouken@0
   117
	SDL_PauseAudio(0);
slouken@0
   118
slouken@0
   119
	/* Let the audio run */
slouken@1660
   120
	printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
slouken@0
   121
	while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
slouken@0
   122
		SDL_Delay(1000);
slouken@0
   123
slouken@0
   124
	/* Clean up on signal */
slouken@0
   125
	SDL_CloseAudio();
slouken@0
   126
	SDL_FreeWAV(wave.sound);
icculus@1151
   127
	SDL_Quit();
slouken@0
   128
	return(0);
slouken@0
   129
}