2 Copyright (C) 2004 Parallel Realities
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 for (int i = 0 ; i < MAX_SOUNDS ; i++)
37 levelMusicName[0] = 0;
44 void Audio::setSoundVolume(int soundVolume)
46 this->soundVolume = soundVolume;
48 Mix_Volume(-1, soundVolume);
51 void Audio::setMusicVolume(int musicVolume)
53 this->musicVolume = musicVolume;
56 Mix_VolumeMusic(musicVolume);
60 void Audio::registerEngine(Engine *engine)
62 this->engine = engine;
65 bool Audio::loadSound(int i, const char *filename)
67 if (!engine->useAudio)
74 printf("ERROR: SOUND INDEX IS HIGHER THAN MAXIMUM ALLOWED %d >= %d\n", i, MAX_SOUNDS);
80 Mix_FreeChunk(sound[i]);
85 engine->unpack(filename, PAK_SOUND);
86 sound[i] = Mix_LoadWAV_RW(engine->sdlrw, 1);
88 sound[i] = Mix_LoadWAV(filename);
93 debug(("WARNING - Failed to load %s\n", filename));
100 bool Audio::loadMusic(const char *filename)
102 char tempPath[PATH_MAX];
104 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
106 if (!engine->useAudio)
113 SDL_Delay(250); // wait a bit, just to be sure!
119 Mix_FreeMusic(music);
124 engine->unpack(filename, PAK_MUSIC);
125 music = Mix_LoadMUS(tempPath);
127 music = Mix_LoadMUS(filename);
137 debug(("WARNING - Failed to load %s\n", filename));
141 snprintf(tempPath, sizeof tempPath, "%s.tags", filename);
142 FILE *fp = fopen(tempPath, "r");
145 while(fp && fgets(line, sizeof line, fp))
147 int l = strlen(line);
148 if(line[l - 1] == '\n')
151 if(!strncasecmp(line, "title=", 6))
152 strlcpy(songtitle, line + 6, sizeof songtitle);
153 else if(!strncasecmp(line, "album=", 6))
154 strlcpy(songalbum, line + 6, sizeof songalbum);
155 else if(!strncasecmp(line, "artist=", 7))
156 strlcpy(songartist, line + 7, sizeof songartist);
157 else if(!strncasecmp(line, "license=", 8))
159 if(!strncasecmp(line + 8, "CC-BY ", 6))
161 else if(!strncasecmp(line + 8, "CC-BY-SA ", 9))
169 strlcpy(levelMusicName, filename, sizeof levelMusicName);
174 void Audio::playSound(int snd, int channel)
176 if ((!engine->useAudio) || (soundVolume == 0))
184 Mix_Volume(channel, soundVolume);
186 Mix_PlayChannel(channel, sound[snd], 0);
189 void Audio::playMusic()
191 if (!engine->useAudio)
199 Mix_PlayMusic(music, -1);
201 Mix_VolumeMusic(musicVolume);
204 void Audio::playMusicOnce()
206 if (!engine->useAudio)
214 Mix_PlayMusic(music, 0);
216 Mix_VolumeMusic(musicVolume);
219 bool Audio::loadGameOverMusic()
221 char tempPath[PATH_MAX];
223 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
225 if (!engine->useAudio)
231 SDL_Delay(250); // wait a bit, just to be sure!
237 Mix_FreeMusic(music);
242 engine->unpack("music/gameover", PAK_MUSIC);
243 music = Mix_LoadMUS(tempPath);
245 music = Mix_LoadMUS("music/gameover");
256 bool Audio::reloadLevelMusic()
258 // remove the Game Over music first...
264 Mix_FreeMusic(music);
268 return loadMusic(levelMusicName);
271 void Audio::playAmbiance()
273 if ((!engine->useAudio) || (soundVolume == 0))
283 Mix_PlayChannel(CH_AMBIANCE, sound[SND_AMBIANCE], -1);
286 void Audio::stopAmbiance()
288 if ((!engine->useAudio) || (soundVolume == 0))
291 Mix_HaltChannel(CH_AMBIANCE);
294 int Audio::playMenuSound(int sound)
296 if ((!engine->useAudio) || (soundVolume == 0))
299 if ((sound == 0) || (sound == 3))
303 playSound(SND_HIGHLIGHT, CH_ANY);
306 playSound(SND_SELECT, CH_ANY);
313 if (!engine->useAudio)
316 for (int i = 0 ; i < 8 ; i++)
324 if (!engine->useAudio)
332 for (int i = 0 ; i < 8 ; i++)
338 void Audio::stopMusic()
340 if (!engine->useAudio)
346 void Audio::fadeMusic()
348 if (!engine->useAudio)
351 Mix_FadeOutMusic(3500);
356 for (int i = 0 ; i < MAX_SOUNDS - 3 ; i++)
358 if (sound[i] != NULL)
360 Mix_FreeChunk(sound[i]);
369 Mix_FreeMusic(music);
374 if (quickSound != NULL)
375 Mix_FreeChunk(quickSound);
380 void Audio::destroy()
384 for (int i = MAX_SOUNDS - 3 ; i < MAX_SOUNDS ; i++)
386 if (sound[i] != NULL)
388 Mix_FreeChunk(sound[i]);