incoporated patches from Duong-Khang (stereo sound!) and Ricardo Cruz <rick2@aeiou...
[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 /* why do we need this ?
14 #ifdef LINUX
15 #include <pwd.h>
16 #include <sys/types.h>
17 #include <ctype.h>
18 #endif
19 */
20
21 #include "defines.h"
22 #include "globals.h"
23 #include "sound.h"
24 #include "setup.h"
25
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"
45                                     };
46
47
48 #ifndef NOSOUND
49
50 #include <SDL_mixer.h>
51
52 /* --- OPEN THE AUDIO DEVICE --- */
53
54 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
55 {
56   /* if success we reserved some channels and register panning effects */
57   if (Mix_OpenAudio( frequency, format, channels, chunksize ) == 0)
58     {
59       if (Mix_ReserveChannels( SOUND_RESERVED_CHANNELS )
60                             != SOUND_RESERVED_CHANNELS )
61         {
62           DEBUG_MSG( "Warning: open_audio could'nt reserve channels" );
63         }
64
65       /* prepare the spanning effects, no error checking */
66       Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
67       Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
68       return 0;
69     }
70   else
71     {
72       return -1;
73     }
74 }
75
76
77 /* --- CLOSE THE AUDIO DEVICE --- */
78
79 void close_audio( void )
80 {
81   if (audio_device == YES) {
82     Mix_UnregisterAllEffects( SOUND_LEFT_SPEAKER );
83     Mix_UnregisterAllEffects( SOUND_RIGHT_SPEAKER );
84     Mix_CloseAudio();
85   }
86 }
87
88
89 /* --- LOAD A SOUND --- */
90
91 Mix_Chunk * load_sound(char * file)
92 {
93   Mix_Chunk * snd;
94
95   snd = Mix_LoadWAV(file);
96
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);
100
101   return(snd);
102 }
103
104
105 /* --- LOAD A SONG --- */
106
107 Mix_Music * load_song(char * file)
108 {
109   Mix_Music * sng;
110
111   sng = Mix_LoadMUS(file);
112
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);
116   return (sng);
117 }
118
119
120 /* --- PLAY A SOUND ON LEFT OR RIGHT OR CENTER SPEAKER --- */
121
122 void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker)
123 {
124   /* this won't call the function if the user has disabled sound
125    * either via menu or via command-line option
126    */
127   if ((use_sound == YES) && (audio_device == YES))
128     {
129       Mix_PlayChannel( whichSpeaker, snd, 0);
130
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 );
136           break;
137         case SOUND_RIGHT_SPEAKER:
138           Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
139           break;
140         default:  // keep the compiler happy
141           break;
142       }
143     }
144 }
145
146
147 void free_chunk(Mix_Chunk *chunk)
148 {
149   if (chunk != NULL)
150     {
151       DEBUG_MSG( __PRETTY_FUNCTION__ );
152       Mix_FreeChunk( chunk );
153       chunk = NULL;
154     }
155 }
156
157
158 int playing_music(void)
159 {
160   if (use_music == YES)
161     {
162       return Mix_PlayingMusic();
163     }
164   else
165     {
166       /* we are in --disable-music we can't be playing music */
167       return 0;
168     }
169 }
170
171
172 int halt_music(void)
173 {
174   if ((use_music == YES) && (audio_device == YES))
175     {
176       return Mix_HaltMusic();
177     }
178   else
179     {
180       return 0;
181     }
182 }
183
184
185 int play_music(Mix_Music *music, int loops)
186 {
187   if ((use_music == YES) && (audio_device == YES))
188     {
189       DEBUG_MSG(__PRETTY_FUNCTION__);
190       return Mix_PlayMusic(music, loops);
191     }
192   else
193     {
194       /* return error since you're trying to play music in --disable-sound mode */
195       return -1;
196     }
197 }
198
199
200 void free_music(Mix_Music *music)
201 {
202   if ( music != NULL )
203     {
204       DEBUG_MSG(__PRETTY_FUNCTION__);
205       Mix_FreeMusic( music );
206       music = NULL;
207     }
208 }
209
210 #else
211
212 int open_audio (int frequency, int format, int channels, int chunksize)
213 {
214   return -1;
215 }
216
217
218 void close_audio(void)
219 {}
220
221
222 void* load_sound(void* file)
223 {
224   return NULL;
225 }
226
227
228 void play_sound(void * snd, enum Sound_Speaker whichSpeaker)
229 {}
230
231
232 void* load_song(void* file)
233 {
234   return NULL;
235 }
236
237
238 int playing_music()
239 {
240   return 0;
241 }
242
243
244 void halt_music()
245 {}
246
247
248 int play_music(void *music, int loops)
249 {
250   return 0;
251 }
252
253
254 void free_music(void *music)
255 {}
256
257
258 void free_chunk(void *chunk)
259 {}
260
261 #endif