ugly but working fix for the beam bug.
[supertux.git] / src / sound.c
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 - December 28, 2003
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
211 void play_current_music(void)
212 {
213           switch (current_music)
214             {
215             case LEVEL_MUSIC:
216               play_music(level_song, 1);
217               break;
218             case HERRING_MUSIC:
219               play_music(herring_song, 1);
220               break;
221             case HURRYUP_MUSIC: // keep the compiler happy
222               play_music(level_song_fast, 1);
223               break;
224             case NO_MUSIC:      // keep the compiler happy for the moment :-)
225             {}
226               /*default:*/
227             }
228 }
229
230 #else
231
232 void* sounds[NUM_SOUNDS];
233 void* level_song, *herring_song;
234
235 int open_audio (int frequency, int format, int channels, int chunksize)
236 {
237   return -1;
238 }
239
240
241 void close_audio(void)
242 {}
243
244
245 void* load_sound(void* file)
246 {
247   return NULL;
248 }
249
250
251 void play_sound(void * snd, enum Sound_Speaker whichSpeaker)
252 {}
253
254
255 void* load_song(void* file)
256 {
257   return NULL;
258 }
259
260
261 int playing_music()
262 {
263   return 0;
264 }
265
266
267 void halt_music()
268 {}
269
270
271 int play_music(void *music, int loops)
272 {
273   return 0;
274 }
275
276
277 void free_music(void *music)
278 {}
279
280
281 void free_chunk(void *chunk)
282 {}
283
284 void play_current_music(void)
285 {}
286
287 #endif