4 Super Tux - Audio Functions
7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 22, 2000 - December 28, 2003
19 int use_sound; /* handle sound on/off menu and command-line option */
20 int use_music; /* handle music on/off menu and command-line option */
21 int audio_device; /* != 0: available and initialized */
24 char * soundfilenames[NUM_SOUNDS] = {
25 DATA_PREFIX "/sounds/jump.wav",
26 DATA_PREFIX "/sounds/bigjump.wav",
27 DATA_PREFIX "/sounds/skid.wav",
28 DATA_PREFIX "/sounds/distro.wav",
29 DATA_PREFIX "/sounds/herring.wav",
30 DATA_PREFIX "/sounds/brick.wav",
31 DATA_PREFIX "/sounds/hurt.wav",
32 DATA_PREFIX "/sounds/squish.wav",
33 DATA_PREFIX "/sounds/fall.wav",
34 DATA_PREFIX "/sounds/ricochet.wav",
35 DATA_PREFIX "/sounds/bump-upgrade.wav",
36 DATA_PREFIX "/sounds/upgrade.wav",
37 DATA_PREFIX "/sounds/excellent.wav",
38 DATA_PREFIX "/sounds/coffee.wav",
39 DATA_PREFIX "/sounds/shoot.wav",
40 DATA_PREFIX "/sounds/lifeup.wav",
41 DATA_PREFIX "/sounds/stomp.wav",
42 DATA_PREFIX "/sounds/kick.wav"
48 #include <SDL_mixer.h>
50 Mix_Chunk * sounds[NUM_SOUNDS];
51 Mix_Music * level_song, * level_song_fast, * herring_song;
53 /* --- OPEN THE AUDIO DEVICE --- */
55 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
57 /* if success we reserved some channels and register panning effects */
58 if (Mix_OpenAudio( frequency, format, channels, chunksize ) == 0)
60 if (Mix_ReserveChannels( SOUND_RESERVED_CHANNELS )
61 != SOUND_RESERVED_CHANNELS )
63 DEBUG_MSG( "Warning: open_audio could'nt reserve channels" );
66 /* prepare the spanning effects, no error checking */
67 Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
68 Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
78 /* --- CLOSE THE AUDIO DEVICE --- */
80 void close_audio( void )
82 if (audio_device == YES) {
83 Mix_UnregisterAllEffects( SOUND_LEFT_SPEAKER );
84 Mix_UnregisterAllEffects( SOUND_RIGHT_SPEAKER );
90 /* --- LOAD A SOUND --- */
92 Mix_Chunk * load_sound(char * file)
96 snd = Mix_LoadWAV(file);
98 /* printf message and abort if there is an initialized audio device */
99 if ((snd == NULL) && (audio_device == YES))
100 st_abort("Can't load", file);
106 /* --- LOAD A SONG --- */
108 Mix_Music * load_song(char * file)
112 sng = Mix_LoadMUS(file);
114 /* printf message and abort if there is an initialized audio device */
115 if ((sng == NULL) && (audio_device == YES))
116 st_abort("Can't load", file);
121 /* --- PLAY A SOUND ON LEFT OR RIGHT OR CENTER SPEAKER --- */
123 void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker)
125 /* this won't call the function if the user has disabled sound
126 * either via menu or via command-line option
128 if ((use_sound == YES) && (audio_device == YES))
130 Mix_PlayChannel( whichSpeaker, snd, 0);
132 /* prepare for panning effects for next call */
133 /* warning: currently, I do not check for errors here */
134 switch (whichSpeaker) {
135 case SOUND_LEFT_SPEAKER:
136 Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
138 case SOUND_RIGHT_SPEAKER:
139 Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
141 default: // keep the compiler happy
148 void free_chunk(Mix_Chunk *chunk)
152 DEBUG_MSG( __PRETTY_FUNCTION__ );
153 Mix_FreeChunk( chunk );
159 int playing_music(void)
161 if (use_music == YES)
163 return Mix_PlayingMusic();
167 /* we are in --disable-music we can't be playing music */
175 if ((use_music == YES) && (audio_device == YES))
177 return Mix_HaltMusic();
186 int play_music(Mix_Music *music, int loops)
188 if ((use_music == YES) && (audio_device == YES))
190 DEBUG_MSG(__PRETTY_FUNCTION__);
191 return Mix_PlayMusic(music, loops);
195 /* return error since you're trying to play music in --disable-sound mode */
201 void free_music(Mix_Music *music)
205 DEBUG_MSG(__PRETTY_FUNCTION__);
206 Mix_FreeMusic( music );
211 void play_current_music(void)
213 switch (current_music)
216 play_music(level_song, 1);
219 play_music(herring_song, 1);
221 case HURRYUP_MUSIC: // keep the compiler happy
222 play_music(level_song_fast, 1);
224 case NO_MUSIC: // keep the compiler happy for the moment :-)
232 void* sounds[NUM_SOUNDS];
233 void* level_song, *herring_song;
235 int open_audio (int frequency, int format, int channels, int chunksize)
241 void close_audio(void)
245 void* load_sound(void* file)
251 void play_sound(void * snd, enum Sound_Speaker whichSpeaker)
255 void* load_song(void* file)
271 int play_music(void *music, int loops)
277 void free_music(void *music)
281 void free_chunk(void *chunk)
284 void play_current_music(void)