little info-file format change
[supertux.git] / src / sound.cpp
1 /*
2   sound.c
3   
4   Super Tux - Audio Functions
5   
6   by Bill Kendrick
7   bill@newbreedsoftware.com
8   http://www.newbreedsoftware.com/supertux/
9  
10   April 22, 2000 - March 15, 2004
11 */
12
13 #include "defines.h"
14 #include "globals.h"
15 #include "sound.h"
16 #include "setup.h"
17
18 /*global variable*/
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 */
22 int current_music;
23
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"
43                                     };
44
45
46 #ifndef NOSOUND
47
48 #include <SDL_mixer.h>
49
50 Mix_Chunk * sounds[NUM_SOUNDS];
51 Mix_Music * level_song, * level_song_fast, * herring_song;
52
53 /* --- OPEN THE AUDIO DEVICE --- */
54
55 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
56 {
57   /* if success we reserved some channels and register panning effects */
58   if (Mix_OpenAudio( frequency, format, channels, chunksize ) == 0)
59     {
60       if (Mix_ReserveChannels( SOUND_RESERVED_CHANNELS )
61                             != SOUND_RESERVED_CHANNELS )
62         {
63           DEBUG_MSG( "Warning: open_audio could'nt reserve channels" );
64         }
65
66       /* prepare the spanning effects, no error checking */
67       Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
68       Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
69       return 0;
70     }
71   else
72     {
73       return -1;
74     }
75 }
76
77
78 /* --- CLOSE THE AUDIO DEVICE --- */
79
80 void close_audio( void )
81 {
82   if (audio_device == YES) {
83     Mix_UnregisterAllEffects( SOUND_LEFT_SPEAKER );
84     Mix_UnregisterAllEffects( SOUND_RIGHT_SPEAKER );
85     Mix_CloseAudio();
86   }
87 }
88
89
90 /* --- LOAD A SOUND --- */
91
92 Mix_Chunk * load_sound(char * file)
93 {
94   Mix_Chunk * snd;
95
96   snd = Mix_LoadWAV(file);
97
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);
101
102   return(snd);
103 }
104
105
106 /* --- LOAD A SONG --- */
107
108 Mix_Music * load_song(char * file)
109 {
110   Mix_Music * sng;
111
112   sng = Mix_LoadMUS(file);
113
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);
117   return (sng);
118 }
119
120
121 /* --- PLAY A SOUND ON LEFT OR RIGHT OR CENTER SPEAKER --- */
122
123 void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker)
124 {
125   /* this won't call the function if the user has disabled sound
126    * either via menu or via command-line option
127    */
128   if ((use_sound == YES) && (audio_device == YES))
129     {
130       Mix_PlayChannel( whichSpeaker, snd, 0);
131
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 );
137           break;
138         case SOUND_RIGHT_SPEAKER:
139           Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
140           break;
141         default:  // keep the compiler happy
142           break;
143       }
144     }
145 }
146
147
148 void free_chunk(Mix_Chunk *chunk)
149 {
150   if (chunk != NULL)
151     {
152       DEBUG_MSG( __PRETTY_FUNCTION__ );
153       Mix_FreeChunk( chunk );
154       chunk = NULL;
155     }
156 }
157
158
159 int playing_music(void)
160 {
161   if (use_music == YES)
162     {
163       return Mix_PlayingMusic();
164     }
165   else
166     {
167       /* we are in --disable-music we can't be playing music */
168       return 0;
169     }
170 }
171
172
173 int halt_music(void)
174 {
175   if ((use_music == YES) && (audio_device == YES))
176     {
177       return Mix_HaltMusic();
178     }
179   else
180     {
181       return 0;
182     }
183 }
184
185
186 int play_music(Mix_Music *music, int loops)
187 {
188   if ((use_music == YES) && (audio_device == YES))
189     {
190       DEBUG_MSG(__PRETTY_FUNCTION__);
191       return Mix_PlayMusic(music, loops);
192     }
193   else
194     {
195       /* return error since you're trying to play music in --disable-sound mode */
196       return -1;
197     }
198 }
199
200
201 void free_music(Mix_Music *music)
202 {
203   if ( music != NULL )
204     {
205       DEBUG_MSG(__PRETTY_FUNCTION__);
206       Mix_FreeMusic( music );
207       music = NULL;
208     }
209 }
210  int get_current_music()
211   {
212  return current_music;
213  }
214  
215  void set_current_music(int music)
216  {
217  current_music = music;
218  }
219  
220  void play_current_music()
221  {
222  if(playing_music())
223    halt_music();
224  
225  switch(current_music)
226    {
227    case LEVEL_MUSIC:
228      play_music(level_song, -1);  // -1 to play forever
229      break;
230    case HERRING_MUSIC:
231      play_music(herring_song, -1);
232      break;
233    case HURRYUP_MUSIC:
234      play_music(level_song_fast, -1);
235      break;
236    case NO_MUSIC:      // keep the compiler happy for the moment :-)
237      {}
238  /*default:*/
239  }
240  /* use halt_music whenever you want to stop it */
241 }
242
243 #else
244
245 void* sounds[NUM_SOUNDS];
246 void* level_song, * level_song_fast, * herring_song;
247
248 int open_audio (int frequency, int format, int channels, int chunksize)
249 {
250   return -1;
251 }
252
253
254 void close_audio(void)
255 {}
256
257
258 void* load_sound(void* file)
259 {
260   return NULL;
261 }
262
263
264 void play_sound(void * snd, enum Sound_Speaker whichSpeaker)
265 {}
266
267
268 void* load_song(void* file)
269 {
270   return NULL;
271 }
272
273
274 int playing_music()
275 {
276   return 0;
277 }
278
279
280 void halt_music()
281 {}
282
283
284 int play_music(void *music, int loops)
285 {
286   return 0;
287 }
288
289
290 void free_music(void *music)
291 {}
292
293
294 void free_chunk(void *chunk)
295 {}
296
297 int get_current_music()
298 {
299 }
300
301 void set_current_music(int music)
302 {
303 }
304
305 void play_current_music(void)
306 {}
307
308 #endif