2 Copyright (C) 2004-2010 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)
117 Mix_FreeMusic(music);
122 engine->unpack(filename, PAK_MUSIC);
123 music = Mix_LoadMUS(tempPath);
125 music = Mix_LoadMUS(filename);
135 debug(("WARNING - Failed to load %s\n", filename));
140 snprintf(tempPath, sizeof tempPath, "%smusic.tags", engine->userHomeDirectory);
142 char tagfilename[PATH_MAX];
143 snprintf(tagfilename, sizeof tagfilename, "%s.tags", filename);
144 engine->unpack(tagfilename, PAK_TAGS);
146 snprintf(tempPath, sizeof tempPath, "%s.tags", filename);
148 FILE *fp = fopen(tempPath, "r");
151 while(fp && fgets(line, sizeof line, fp))
153 int l = strlen(line);
154 if(line[l - 1] == '\n')
157 if(!strncasecmp(line, "title=", 6))
158 strlcpy(songtitle, line + 6, sizeof songtitle);
159 else if(!strncasecmp(line, "album=", 6))
160 strlcpy(songalbum, line + 6, sizeof songalbum);
161 else if(!strncasecmp(line, "artist=", 7))
162 strlcpy(songartist, line + 7, sizeof songartist);
163 else if(!strncasecmp(line, "license=", 8))
165 if(!strncasecmp(line + 8, "CC-BY ", 6))
167 else if(!strncasecmp(line + 8, "CC-BY-SA ", 9))
175 strlcpy(levelMusicName, filename, sizeof levelMusicName);
180 void Audio::playSound(int snd, int channel)
182 if ((!engine->useAudio) || (soundVolume == 0))
190 Mix_Volume(channel, soundVolume);
192 Mix_PlayChannel(channel, sound[snd], 0);
195 void Audio::playMusic()
197 if (!engine->useAudio)
205 Mix_PlayMusic(music, -1);
207 Mix_VolumeMusic(musicVolume);
210 void Audio::playMusicOnce()
212 if (!engine->useAudio)
220 Mix_PlayMusic(music, 0);
222 Mix_VolumeMusic(musicVolume);
225 bool Audio::loadGameOverMusic()
227 char tempPath[PATH_MAX];
229 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
231 if (!engine->useAudio)
237 SDL_Delay(250); // wait a bit, just to be sure!
243 Mix_FreeMusic(music);
248 engine->unpack("music/gameover", PAK_MUSIC);
249 music = Mix_LoadMUS(tempPath);
251 music = Mix_LoadMUS("music/gameover");
262 bool Audio::reloadLevelMusic()
264 // remove the Game Over music first...
270 Mix_FreeMusic(music);
274 return loadMusic(levelMusicName);
277 void Audio::playAmbiance()
279 if ((!engine->useAudio) || (soundVolume == 0))
289 Mix_PlayChannel(CH_AMBIANCE, sound[SND_AMBIANCE], -1);
292 void Audio::stopAmbiance()
294 if ((!engine->useAudio) || (soundVolume == 0))
297 Mix_HaltChannel(CH_AMBIANCE);
300 int Audio::playMenuSound(int sound)
302 if ((!engine->useAudio) || (soundVolume == 0))
305 if ((sound == 0) || (sound == 3))
309 playSound(SND_HIGHLIGHT, CH_ANY);
312 playSound(SND_SELECT, CH_ANY);
319 if (!engine->useAudio)
322 for (int i = 0 ; i < 8 ; i++)
330 if (!engine->useAudio)
338 for (int i = 0 ; i < 8 ; i++)
344 void Audio::stopMusic()
346 if (!engine->useAudio)
352 void Audio::fadeMusic()
354 if (!engine->useAudio)
357 Mix_FadeOutMusic(3500);
362 for (int i = 0 ; i < MAX_SOUNDS - 3 ; i++)
364 if (sound[i] != NULL)
366 Mix_FreeChunk(sound[i]);
375 Mix_FreeMusic(music);
380 if (quickSound != NULL)
381 Mix_FreeChunk(quickSound);
386 void Audio::destroy()
390 for (int i = MAX_SOUNDS - 3 ; i < MAX_SOUNDS ; i++)
392 if (sound[i] != NULL)
394 Mix_FreeChunk(sound[i]);