test/loopwave.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 18 Feb 2019 00:26:53 -0500
changeset 69 a2c5f6a01a55
parent 0 a6cb692b8939
permissions -rw-r--r--
Fill in a default palette for 8-bit screen surfaces.
icculus@0
     1
icculus@0
     2
/* Program to load a wave file and loop playing it using SDL sound */
icculus@0
     3
icculus@0
     4
/* loopwaves.c is much more robust in handling WAVE files -- 
icculus@0
     5
	This is only for simple WAVEs
icculus@0
     6
*/
icculus@0
     7
#include "SDL_config.h"
icculus@0
     8
icculus@0
     9
#include <stdio.h>
icculus@0
    10
#include <stdlib.h>
icculus@0
    11
icculus@0
    12
#if HAVE_SIGNAL_H
icculus@0
    13
#include <signal.h>
icculus@0
    14
#endif
icculus@0
    15
icculus@0
    16
#include "SDL.h"
icculus@0
    17
#include "SDL_audio.h"
icculus@0
    18
icculus@0
    19
struct {
icculus@0
    20
	SDL_AudioSpec spec;
icculus@0
    21
	Uint8   *sound;			/* Pointer to wave data */
icculus@0
    22
	Uint32   soundlen;		/* Length of wave data */
icculus@0
    23
	int      soundpos;		/* Current play position */
icculus@0
    24
} wave;
icculus@0
    25
icculus@0
    26
icculus@0
    27
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@0
    28
static void quit(int rc)
icculus@0
    29
{
icculus@0
    30
	SDL_Quit();
icculus@0
    31
	exit(rc);
icculus@0
    32
}
icculus@0
    33
icculus@0
    34
icculus@0
    35
void SDLCALL fillerup(void *unused, Uint8 *stream, int len)
icculus@0
    36
{
icculus@0
    37
	Uint8 *waveptr;
icculus@0
    38
	int    waveleft;
icculus@0
    39
icculus@0
    40
	/* Set up the pointers */
icculus@0
    41
	waveptr = wave.sound + wave.soundpos;
icculus@0
    42
	waveleft = wave.soundlen - wave.soundpos;
icculus@0
    43
icculus@0
    44
	/* Go! */
icculus@0
    45
	while ( waveleft <= len ) {
icculus@0
    46
		SDL_memcpy(stream, waveptr, waveleft);
icculus@0
    47
		stream += waveleft;
icculus@0
    48
		len -= waveleft;
icculus@0
    49
		waveptr = wave.sound;
icculus@0
    50
		waveleft = wave.soundlen;
icculus@0
    51
		wave.soundpos = 0;
icculus@0
    52
	}
icculus@0
    53
	SDL_memcpy(stream, waveptr, len);
icculus@0
    54
	wave.soundpos += len;
icculus@0
    55
}
icculus@0
    56
icculus@0
    57
static int done = 0;
icculus@0
    58
void poked(int sig)
icculus@0
    59
{
icculus@0
    60
	done = 1;
icculus@0
    61
}
icculus@0
    62
icculus@0
    63
int main(int argc, char *argv[])
icculus@0
    64
{
icculus@0
    65
	char name[32];
icculus@0
    66
icculus@0
    67
	/* Load the SDL library */
icculus@0
    68
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
icculus@0
    69
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@0
    70
		return(1);
icculus@0
    71
	}
icculus@0
    72
	if ( argv[1] == NULL ) {
icculus@0
    73
		argv[1] = "sample.wav";
icculus@0
    74
	}
icculus@0
    75
	/* Load the wave file into memory */
icculus@0
    76
	if ( SDL_LoadWAV(argv[1],
icculus@0
    77
			&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
icculus@0
    78
		fprintf(stderr, "Couldn't load %s: %s\n",
icculus@0
    79
						argv[1], SDL_GetError());
icculus@0
    80
		quit(1);
icculus@0
    81
	}
icculus@0
    82
icculus@0
    83
	wave.spec.callback = fillerup;
icculus@0
    84
#if HAVE_SIGNAL_H
icculus@0
    85
	/* Set the signals */
icculus@0
    86
#ifdef SIGHUP
icculus@0
    87
	signal(SIGHUP, poked);
icculus@0
    88
#endif
icculus@0
    89
	signal(SIGINT, poked);
icculus@0
    90
#ifdef SIGQUIT
icculus@0
    91
	signal(SIGQUIT, poked);
icculus@0
    92
#endif
icculus@0
    93
	signal(SIGTERM, poked);
icculus@0
    94
#endif /* HAVE_SIGNAL_H */
icculus@0
    95
icculus@0
    96
	/* Initialize fillerup() variables */
icculus@0
    97
	if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
icculus@0
    98
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
icculus@0
    99
		SDL_FreeWAV(wave.sound);
icculus@0
   100
		quit(2);
icculus@0
   101
	}
icculus@0
   102
	SDL_PauseAudio(0);
icculus@0
   103
icculus@0
   104
	/* Let the audio run */
icculus@0
   105
	printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
icculus@0
   106
	while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
icculus@0
   107
		SDL_Delay(1000);
icculus@0
   108
icculus@0
   109
	/* Clean up on signal */
icculus@0
   110
	SDL_CloseAudio();
icculus@0
   111
	SDL_FreeWAV(wave.sound);
icculus@0
   112
	SDL_Quit();
icculus@0
   113
	return(0);
icculus@0
   114
}