4 Super Tux - Audio Functions
7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 22, 2000 - December 28, 2003
13 /* why do we need this ?
16 #include <sys/types.h>
26 char * soundfilenames[NUM_SOUNDS] = {
27 DATA_PREFIX "/sounds/jump.wav",
28 DATA_PREFIX "/sounds/bigjump.wav",
29 DATA_PREFIX "/sounds/skid.wav",
30 DATA_PREFIX "/sounds/distro.wav",
31 DATA_PREFIX "/sounds/herring.wav",
32 DATA_PREFIX "/sounds/brick.wav",
33 DATA_PREFIX "/sounds/hurt.wav",
34 DATA_PREFIX "/sounds/squish.wav",
35 DATA_PREFIX "/sounds/fall.wav",
36 DATA_PREFIX "/sounds/ricochet.wav",
37 DATA_PREFIX "/sounds/bump-upgrade.wav",
38 DATA_PREFIX "/sounds/upgrade.wav",
39 DATA_PREFIX "/sounds/excellent.wav",
40 DATA_PREFIX "/sounds/coffee.wav",
41 DATA_PREFIX "/sounds/shoot.wav",
42 DATA_PREFIX "/sounds/lifeup.wav",
43 DATA_PREFIX "/sounds/stomp.wav",
44 DATA_PREFIX "/sounds/kick.wav"
50 #include <SDL_mixer.h>
52 /* --- OPEN THE AUDIO DEVICE --- */
54 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
56 /* if success we reserved some channels and register panning effects */
57 if (Mix_OpenAudio( frequency, format, channels, chunksize ) == 0)
59 if (Mix_ReserveChannels( SOUND_RESERVED_CHANNELS )
60 != SOUND_RESERVED_CHANNELS )
62 DEBUG_MSG( "Warning: open_audio could'nt reserve channels" );
65 /* prepare the spanning effects, no error checking */
66 Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
67 Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
77 /* --- CLOSE THE AUDIO DEVICE --- */
79 void close_audio( void )
81 if (audio_device == YES) {
82 Mix_UnregisterAllEffects( SOUND_LEFT_SPEAKER );
83 Mix_UnregisterAllEffects( SOUND_RIGHT_SPEAKER );
89 /* --- LOAD A SOUND --- */
91 Mix_Chunk * load_sound(char * file)
95 snd = Mix_LoadWAV(file);
97 /* printf message and abort if there is an initialized audio device */
98 if ((snd == NULL) && (audio_device == YES))
99 st_abort("Can't load", file);
105 /* --- LOAD A SONG --- */
107 Mix_Music * load_song(char * file)
111 sng = Mix_LoadMUS(file);
113 /* printf message and abort if there is an initialized audio device */
114 if ((sng == NULL) && (audio_device == YES))
115 st_abort("Can't load", file);
120 /* --- PLAY A SOUND ON LEFT OR RIGHT OR CENTER SPEAKER --- */
122 void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker)
124 /* this won't call the function if the user has disabled sound
125 * either via menu or via command-line option
127 if ((use_sound == YES) && (audio_device == YES))
129 Mix_PlayChannel( whichSpeaker, snd, 0);
131 /* prepare for panning effects for next call */
132 /* warning: currently, I do not check for errors here */
133 switch (whichSpeaker) {
134 case SOUND_LEFT_SPEAKER:
135 Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
137 case SOUND_RIGHT_SPEAKER:
138 Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
140 default: // keep the compiler happy
147 void free_chunk(Mix_Chunk *chunk)
151 DEBUG_MSG( __PRETTY_FUNCTION__ );
152 Mix_FreeChunk( chunk );
158 int playing_music(void)
160 if (use_music == YES)
162 return Mix_PlayingMusic();
166 /* we are in --disable-music we can't be playing music */
174 if ((use_music == YES) && (audio_device == YES))
176 return Mix_HaltMusic();
185 int play_music(Mix_Music *music, int loops)
187 if ((use_music == YES) && (audio_device == YES))
189 DEBUG_MSG(__PRETTY_FUNCTION__);
190 return Mix_PlayMusic(music, loops);
194 /* return error since you're trying to play music in --disable-sound mode */
200 void free_music(Mix_Music *music)
204 DEBUG_MSG(__PRETTY_FUNCTION__);
205 Mix_FreeMusic( music );
210 void play_current_music(void)
212 switch (current_music)
215 play_music(level_song, 1);
218 play_music(herring_song, 1);
220 case HURRYUP_MUSIC: // keep the compiler happy
221 play_music(level_song_fast, 1);
223 case NO_MUSIC: // keep the compiler happy for the moment :-)
231 int open_audio (int frequency, int format, int channels, int chunksize)
237 void close_audio(void)
241 void* load_sound(void* file)
247 void play_sound(void * snd, enum Sound_Speaker whichSpeaker)
251 void* load_song(void* file)
267 int play_music(void *music, int loops)
273 void free_music(void *music)
277 void free_chunk(void *chunk)
280 void play_current_music(void)