comparison test/loopwave.c @ 0:74212992fb08

Initial revision
author Sam Lantinga <slouken@lokigames.com>
date Thu, 26 Apr 2001 16:45:43 +0000
parents
children be9c9c8f6d53
comparison
equal deleted inserted replaced
-1:000000000000 0:74212992fb08
1
2 /* Program to load a wave file and loop playing it using SDL sound */
3
4 /* loopwaves.c is much more robust in handling WAVE files --
5 This is only for simple WAVEs
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <signal.h>
11
12 #include "SDL.h"
13 #include "SDL_audio.h"
14
15 struct {
16 SDL_AudioSpec spec;
17 Uint8 *sound; /* Pointer to wave data */
18 Uint32 soundlen; /* Length of wave data */
19 int soundpos; /* Current play position */
20 } wave;
21
22 void fillerup(void *unused, Uint8 *stream, int len)
23 {
24 Uint8 *waveptr;
25 int waveleft;
26
27 /* Set up the pointers */
28 waveptr = wave.sound + wave.soundpos;
29 waveleft = wave.soundlen - wave.soundpos;
30
31 /* Go! */
32 while ( waveleft <= len ) {
33 SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
34 stream += waveleft;
35 len -= waveleft;
36 waveptr = wave.sound;
37 waveleft = wave.soundlen;
38 wave.soundpos = 0;
39 }
40 SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
41 wave.soundpos += len;
42 }
43
44 static int done = 0;
45 void poked(int sig)
46 {
47 done = 1;
48 }
49
50 int main(int argc, char *argv[])
51 {
52 char name[32];
53
54 /* Load the SDL library */
55 if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
56 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
57 exit(1);
58 }
59 atexit(SDL_Quit);
60
61 if ( argv[1] == NULL ) {
62 fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]);
63 exit(1);
64 }
65
66 /* Load the wave file into memory */
67 if ( SDL_LoadWAV(argv[1],
68 &wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
69 fprintf(stderr, "Couldn't load %s: %s\n",
70 argv[1], SDL_GetError());
71 exit(1);
72 }
73 wave.spec.callback = fillerup;
74
75 /* Set the signals */
76 #ifdef SIGHUP
77 signal(SIGHUP, poked);
78 #endif
79 signal(SIGINT, poked);
80 #ifdef SIGQUIT
81 signal(SIGQUIT, poked);
82 #endif
83 signal(SIGTERM, poked);
84
85 /* Initialize fillerup() variables */
86 if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
87 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
88 SDL_FreeWAV(wave.sound);
89 exit(2);
90 }
91 SDL_PauseAudio(0);
92
93 /* Let the audio run */
94 printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
95 while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
96 SDL_Delay(1000);
97
98 /* Clean up on signal */
99 SDL_CloseAudio();
100 SDL_FreeWAV(wave.sound);
101 return(0);
102 }