1 #include <string.h>
2 #include "audio.h"
3 
4 extern char *dataFolder;
5 
6 Sound  *sound_pop;
7 Sound  *sound_slide;
8 Sound  *sound_bam1;
9 Sound  *sound_fff;
10 Sound  *sound_yahoohoo1[NB_YAHOOHOO1];
11 Sound  *sound_yahoohoo2[NB_YAHOOHOO2];
12 Sound  *sound_yahoohoo3[NB_YAHOOHOO3];
13 Sound  *sound_splash[8];
14 Sound  *sound_bim[2];
15 
16 
17 #ifdef USE_AUDIO
18 #include "glSDL.h"
19 static Mix_Music *music[4];
20 
21 static char *MUSIC_THEME[2][4] = {
22   { "flobopuyo_menu.xm", "flobopuyo_game1.xm", "flobopuyo_game2.xm", "flobopuyo_gameover.xm" },
23   { "flobopuyo_menu.xm", "strange_fear.xm", "strange_fear2.xm", "strange_gameover.xm" }
24 };
25 
26 static int sound_supported;
27 static int volume;
28 static int sound_on;
29 static int music_on;
30 static int currentMus = -1;
31 
CustomMix_LoadWAV(char * path,char * fileName,int volume)32 static Sound *CustomMix_LoadWAV(char *path, char *fileName, int volume)
33 {
34     Sound *result;
35     char temp[1024];
36     if (!sound_supported) return NULL;
37     sprintf(temp, "%s/sfx/%s", dataFolder, fileName);
38     result = Mix_LoadWAV(temp);
39     if (result)
40       Mix_VolumeChunk (result, volume);
41     return result;
42 }
43 
CustomMix_LoadMUS(char * path,char * fileName)44 static Mix_Music *CustomMix_LoadMUS(char *path, char *fileName)
45 {
46     Mix_Music *result;
47     char temp[1024];
48     if (!sound_supported) return NULL;
49     sprintf(temp, "%s/sfx/%s", dataFolder, fileName);
50     result = Mix_LoadMUS(temp);
51     return result;
52 }
53 #endif
54 
55 void
audio_init()56 audio_init ()
57 {
58 #ifdef USE_AUDIO
59 	int     audio_rate;
60 	Uint16  audio_format;
61 	int     audio_channels;
62   int i;
63 
64 	sound_supported =
65     (Mix_OpenAudio (MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) == 0);
66 
67   if (!sound_supported) return;
68 
69 	sound_pop   = CustomMix_LoadWAV (dataFolder, "pop.wav", 30);
70 	sound_bam1  = CustomMix_LoadWAV (dataFolder, "bam1.wav", 12);
71 	sound_fff   = CustomMix_LoadWAV (dataFolder, "fff.wav", 48);
72 	sound_slide = CustomMix_LoadWAV (dataFolder, "slide.wav", 128);
73 
74   sound_yahoohoo1[0] = NULL;
75   sound_yahoohoo1[1] = CustomMix_LoadWAV (dataFolder, "yahoohoo.wav", 50);
76   sound_yahoohoo1[2] = CustomMix_LoadWAV (dataFolder, "woho.wav", 32);
77   sound_yahoohoo1[3] = CustomMix_LoadWAV (dataFolder, "pastaga.wav", 70);
78 
79   sound_yahoohoo2[0] = sound_yahoohoo1[2];
80   sound_yahoohoo2[1] = CustomMix_LoadWAV (dataFolder, "woo.wav", 45);
81 
82   sound_yahoohoo3[0] = CustomMix_LoadWAV (dataFolder, "applose.wav", 96);
83 
84   sound_splash[0] = CustomMix_LoadWAV (dataFolder, "splash1.wav",72);
85 	sound_splash[1] = CustomMix_LoadWAV (dataFolder, "splash2.wav",72);
86 	sound_splash[2] = CustomMix_LoadWAV (dataFolder, "splash3.wav",72);
87 	sound_splash[3] = CustomMix_LoadWAV (dataFolder, "splash4.wav",72);
88 	sound_splash[4] = CustomMix_LoadWAV (dataFolder, "splash5.wav",72);
89 	sound_splash[5] = CustomMix_LoadWAV (dataFolder, "splash6.wav",72);
90 	sound_splash[6] = CustomMix_LoadWAV (dataFolder, "splash7.wav",72);
91 	sound_splash[7] = CustomMix_LoadWAV (dataFolder, "splash8.wav",72);
92 
93     sound_bim[0] = CustomMix_LoadWAV (dataFolder, "bim1.wav",72);
94     sound_bim[1] = CustomMix_LoadWAV (dataFolder, "bim2.wav",72);
95 
96   music[0] = CustomMix_LoadMUS (dataFolder, "flobopuyo_menu.xm");
97 	music[1] = CustomMix_LoadMUS (dataFolder, "flobopuyo_game1.xm");
98 	music[2] = CustomMix_LoadMUS (dataFolder, "flobopuyo_game2.xm");
99 	music[3] = CustomMix_LoadMUS (dataFolder, "flobopuyo_gameover.xm");
100 
101 	Mix_QuerySpec (&audio_rate, &audio_format, &audio_channels);
102 
103 	sound_on = 1;
104 	music_on = 1;
105 	audio_music_set_volume (100);
106 	audio_set_volume (50);
107 #endif
108 }
109 
110 void
audio_music_start(int num)111 audio_music_start (int num)
112 {
113 #ifdef USE_AUDIO
114   if (!sound_supported) return;
115   if ((currentMus != num) && (music_on==true)) {
116     currentMus = num;
117     Mix_HaltMusic ();
118     if (music[num])
119       Mix_PlayMusic (music[num], -1);
120   }
121 //	audio_music_set_volume (50);
122 #endif
123 }
124 
125 void
audio_sound_play(Sound * s)126 audio_sound_play (Sound * s)
127 {
128 #ifdef USE_AUDIO
129   if (!sound_supported) return;
130   if (s)
131   	Mix_PlayChannel (-1, s, 0);
132 #endif
133 }
134 
135 void
audio_close()136 audio_close ()
137 {
138 #ifdef USE_AUDIO
139   if (!sound_supported) return;
140 	Mix_CloseAudio ();
141 #endif
142 }
143 
144 // vol compris entre 0 et 100;
145 void
audio_music_set_volume(int vol)146 audio_music_set_volume (int vol)
147 {
148 #ifdef USE_AUDIO
149   if (!sound_supported) return;
150 	Mix_VolumeMusic ((int) (128.0 * (double) vol / 100.0));
151 #endif
152 }
153 
154 // vol compris entre 0 et 100;
155 void
audio_set_volume(int vol)156 audio_set_volume (int vol)
157 {
158 #ifdef USE_AUDIO
159   if (!sound_supported) return;
160 	if (sound_on) {
161 		volume = Mix_Volume (-1, (int) (128.0 * (double) vol / 100.0));
162 	}
163 	else {
164 		volume = (int) (128.0 * (double) vol / 100.0);
165 	}
166 #endif
167 }
168 
169 void
audio_set_music_on_off(int on)170 audio_set_music_on_off (int on)
171 {
172 #ifdef USE_AUDIO
173   if (!sound_supported) return;
174     if (on) {
175         if (Mix_PausedMusic()) Mix_ResumeMusic();
176         else
177         {
178             while (Mix_FadingMusic() == MIX_FADING_OUT) SDL_Delay(100);
179         }
180         if (music[currentMus])
181             Mix_FadeInMusic (music[currentMus], -1, 1000);
182     }
183     else {
184         while (Mix_FadingMusic() == MIX_FADING_IN) SDL_Delay(100);
185         Mix_FadeOutMusic (1000);
186     }
187     music_on = on;
188 #endif
189 }
190 
191 void
audio_set_sound_on_off(int on)192 audio_set_sound_on_off (int on)
193 {
194 #ifdef USE_AUDIO
195   if (!sound_supported) return;
196 	if (on) {
197 		Mix_Volume (-1, volume);
198 	}
199 	else {
200 		volume = Mix_Volume (-1, -1);
201 		Mix_Volume (-1, 0);
202 	}
203 	sound_on = on;
204 #endif
205 }
206 
audio_music_switch_theme(int theme_number)207 void    audio_music_switch_theme(int theme_number)
208 {
209 #ifdef USE_AUDIO
210   int i;
211   if (!sound_supported) return;
212 
213   Mix_Music * themusic[4];
214 
215   for (i=0; i<4; ++i) {
216       themusic[i] = music[i];
217       music[i] = CustomMix_LoadMUS (dataFolder, MUSIC_THEME[theme_number][i]);
218   }
219 
220   Mix_HaltMusic();
221 
222   for (i=0; i<4; ++i) {
223     Mix_FreeMusic(themusic[i]);
224   }
225 
226  // Mix_PlayMusic();
227 
228 #endif
229 }
230