4 Super Tux - Audio Functions
7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 22, 2000 - March 15, 2004
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] = {
26 "/sounds/bigjump.wav",
29 "/sounds/herring.wav",
34 "/sounds/ricochet.wav",
35 "/sounds/bump-upgrade.wav",
36 "/sounds/upgrade.wav",
37 "/sounds/excellent.wav",
46 #include <SDL_mixer.h>
48 Mix_Chunk * sounds[NUM_SOUNDS];
49 Mix_Music * level_song, * level_song_fast, * herring_song;
51 /* --- OPEN THE AUDIO DEVICE --- */
53 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
55 /* if success we reserved some channels and register panning effects */
56 if (Mix_OpenAudio( frequency, format, channels, chunksize ) == 0)
58 if (Mix_ReserveChannels( SOUND_RESERVED_CHANNELS )
59 != SOUND_RESERVED_CHANNELS )
61 DEBUG_MSG( "Warning: open_audio could'nt reserve channels" );
64 /* prepare the spanning effects, no error checking */
65 Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
66 Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
76 /* --- CLOSE THE AUDIO DEVICE --- */
78 void close_audio( void )
80 if (audio_device == YES) {
81 Mix_UnregisterAllEffects( SOUND_LEFT_SPEAKER );
82 Mix_UnregisterAllEffects( SOUND_RIGHT_SPEAKER );
88 /* --- LOAD A SOUND --- */
90 Mix_Chunk * load_sound(const std::string& file)
94 snd = Mix_LoadWAV(file.c_str());
96 /* printf message and abort if there is an initialized audio device */
97 if ((snd == NULL) && (audio_device == YES))
98 st_abort("Can't load", file);
104 /* --- LOAD A SONG --- */
106 Mix_Music * load_song(const std::string& file)
110 sng = Mix_LoadMUS(file.c_str());
112 /* printf message and abort if there is an initialized audio device */
113 if ((sng == NULL) && (audio_device == YES))
114 st_abort("Can't load", file);
119 /* --- PLAY A SOUND ON LEFT OR RIGHT OR CENTER SPEAKER --- */
121 void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker)
123 /* this won't call the function if the user has disabled sound
124 * either via menu or via command-line option
126 if ((use_sound == YES) && (audio_device == YES))
128 Mix_PlayChannel( whichSpeaker, snd, 0);
130 /* prepare for panning effects for next call */
131 /* warning: currently, I do not check for errors here */
132 switch (whichSpeaker) {
133 case SOUND_LEFT_SPEAKER:
134 Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
136 case SOUND_RIGHT_SPEAKER:
137 Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
139 default: // keep the compiler happy
146 void free_chunk(Mix_Chunk *chunk)
150 DEBUG_MSG( __PRETTY_FUNCTION__ );
151 Mix_FreeChunk( chunk );
157 int playing_music(void)
159 if (use_music == YES)
161 return Mix_PlayingMusic();
165 /* we are in --disable-music we can't be playing music */
173 if ((use_music == YES) && (audio_device == YES))
175 return Mix_HaltMusic();
184 int play_music(Mix_Music *music, int loops)
186 if ((use_music == YES) && (audio_device == YES))
188 DEBUG_MSG(__PRETTY_FUNCTION__);
189 return Mix_PlayMusic(music, loops);
193 /* return error since you're trying to play music in --disable-sound mode */
199 void free_music(Mix_Music *music)
203 DEBUG_MSG(__PRETTY_FUNCTION__);
204 Mix_FreeMusic( music );
208 int get_current_music()
210 return current_music;
213 void set_current_music(int music)
215 current_music = music;
218 void play_current_music()
223 switch(current_music)
226 play_music(level_song, -1); // -1 to play forever
229 play_music(herring_song, -1);
232 play_music(level_song_fast, -1);
234 case NO_MUSIC: // keep the compiler happy for the moment :-)
238 /* use halt_music whenever you want to stop it */