2 /* Program to load a wave file and loop playing it using SDL sound */
4 /* loopwaves.c is much more robust in handling WAVE files --
5 This is only for simple WAVEs
7 #include "SDL_config.h"
17 #include "SDL_audio.h"
22 Uint8 *sound; /* Pointer to wave data */
23 Uint32 soundlen; /* Length of wave data */
24 int soundpos; /* Current play position */
28 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
38 fillerup(void *unused, Uint8 * stream, int len)
43 /* Set up the pointers */
44 waveptr = wave.sound + wave.soundpos;
45 waveleft = wave.soundlen - wave.soundpos;
48 while (waveleft <= len) {
49 SDL_memcpy(stream, waveptr, waveleft);
53 waveleft = wave.soundlen;
56 SDL_memcpy(stream, waveptr, len);
71 main(int argc, char *argv[])
75 /* Load the SDL library */
76 if (SDL_Init(SDL_INIT_AUDIO) < 0) {
77 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
81 if (argv[1] == NULL) {
82 argv[1] = "sample.wav";
84 /* Load the wave file into memory */
85 if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
86 fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
90 wave.spec.callback = fillerup;
94 signal(SIGHUP, poked);
96 signal(SIGINT, poked);
98 signal(SIGQUIT, poked);
100 signal(SIGTERM, poked);
101 #endif /* HAVE_SIGNAL_H */
103 /* Initialize fillerup() variables */
104 if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
105 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
106 SDL_FreeWAV(wave.sound);
110 /* Right now we're using the 1.2 SDL_OpenAudio(), but if we move to the
111 1.3 device enumeration version, we shouldn't hardcore device id #1 for
112 SDL_GetAudioDeviceName(), below. */
113 printf("Using audio driver: %s\n", SDL_AudioDriverName(name, NAMESIZE));
114 printf("Using audio device: %s\n", SDL_GetAudioDeviceName(1, 0));
116 /* Let the audio run */
118 while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
121 /* Clean up on signal */
123 SDL_FreeWAV(wave.sound);