2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 for (int i = 0 ; i < MAX_SOUNDS ; i++)
38 levelMusicName[0] = 0;
45 void Audio::setSoundVolume(int soundVolume)
47 this->soundVolume = soundVolume;
49 Mix_Volume(-1, soundVolume);
52 void Audio::setMusicVolume(int musicVolume)
54 this->musicVolume = musicVolume;
57 Mix_VolumeMusic(musicVolume);
61 void Audio::registerEngine(Engine *engine)
63 this->engine = engine;
66 bool Audio::loadSound(int i, const char *filename)
68 if (!engine->useAudio)
75 printf("ERROR: SOUND INDEX IS HIGHER THAN MAXIMUM ALLOWED %d >= %d\n", i, MAX_SOUNDS);
81 Mix_FreeChunk(sound[i]);
86 engine->unpack(filename, PAK_SOUND);
87 sound[i] = Mix_LoadWAV_RW(engine->sdlrw, 1);
89 sound[i] = Mix_LoadWAV(filename);
94 debug(("WARNING - Failed to load %s\n", filename));
101 bool Audio::loadMusic(const char *filename)
103 char tempPath[PATH_MAX];
105 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
107 if (!engine->useAudio)
118 Mix_FreeMusic(music);
123 engine->unpack(filename, PAK_MUSIC);
124 music = Mix_LoadMUS(tempPath);
126 music = Mix_LoadMUS(filename);
136 debug(("WARNING - Failed to load %s\n", filename));
141 snprintf(tempPath, sizeof tempPath, "%smusic.tags", engine->userHomeDirectory);
143 char tagfilename[PATH_MAX];
144 snprintf(tagfilename, sizeof tagfilename, "%s.tags", filename);
145 engine->unpack(tagfilename, PAK_TAGS);
147 snprintf(tempPath, sizeof tempPath, "%s.tags", filename);
149 FILE *fp = fopen(tempPath, "r");
152 while(fp && fgets(line, sizeof line, fp))
154 int l = strlen(line);
155 if(line[l - 1] == '\n')
158 if(!strncasecmp(line, "title=", 6))
159 strlcpy(songtitle, line + 6, sizeof songtitle);
160 else if(!strncasecmp(line, "album=", 6))
161 strlcpy(songalbum, line + 6, sizeof songalbum);
162 else if(!strncasecmp(line, "artist=", 7))
163 strlcpy(songartist, line + 7, sizeof songartist);
164 else if(!strncasecmp(line, "license=", 8))
166 if(!strncasecmp(line + 8, "CC-BY ", 6))
168 else if(!strncasecmp(line + 8, "CC-BY-SA ", 9))
176 strlcpy(levelMusicName, filename, sizeof levelMusicName);
181 void Audio::playSoundRelative(int snd, int channel, float x)
183 if ((!engine->useAudio) || (soundVolume == 0))
191 int angle = atanf(x / 480) * 180 / M_PI;
192 int attenuation = fabsf(x) / 40;
197 if (attenuation > 255)
200 Mix_Volume(channel, soundVolume);
201 Mix_PlayChannel(channel, sound[snd], 0);
202 Mix_SetPosition(channel, angle, attenuation);
205 void Audio::playSound(int snd, int channel, float x)
207 x -= (engine->playerPosX + 320);
208 playSoundRelative(snd, channel, x);
211 void Audio::playSound(int snd, int channel)
213 playSoundRelative(snd, channel, 0);
216 void Audio::playMusic()
218 if (!engine->useAudio)
226 Mix_PlayMusic(music, -1);
228 Mix_VolumeMusic(musicVolume);
231 void Audio::playMusicOnce()
233 if (!engine->useAudio)
241 Mix_PlayMusic(music, 0);
243 Mix_VolumeMusic(musicVolume);
246 bool Audio::loadGameOverMusic()
248 char tempPath[PATH_MAX];
250 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
252 if (!engine->useAudio)
258 SDL_Delay(250); // wait a bit, just to be sure!
264 Mix_FreeMusic(music);
269 engine->unpack("music/gameover", PAK_MUSIC);
270 music = Mix_LoadMUS(tempPath);
272 music = Mix_LoadMUS("music/gameover");
283 bool Audio::reloadLevelMusic()
285 // remove the Game Over music first...
291 Mix_FreeMusic(music);
295 return loadMusic(levelMusicName);
298 void Audio::playAmbiance()
300 if ((!engine->useAudio) || (soundVolume == 0))
310 Mix_PlayChannel(CH_AMBIANCE, sound[SND_AMBIANCE], -1);
313 void Audio::stopAmbiance()
315 if ((!engine->useAudio) || (soundVolume == 0))
318 Mix_HaltChannel(CH_AMBIANCE);
321 int Audio::playMenuSound(int sound)
323 if ((!engine->useAudio) || (soundVolume == 0))
326 if ((sound == 0) || (sound == 3))
330 playSound(SND_HIGHLIGHT, CH_ANY);
333 playSound(SND_SELECT, CH_ANY);
340 if (!engine->useAudio)
343 for (int i = 0 ; i < 8 ; i++)
351 if (!engine->useAudio)
359 for (int i = 0 ; i < 8 ; i++)
365 void Audio::stopMusic()
367 if (!engine->useAudio)
373 void Audio::fadeMusic()
375 if (!engine->useAudio)
378 Mix_FadeOutMusic(3500);
383 for (int i = 0 ; i < MAX_SOUNDS - 3 ; i++)
385 if (sound[i] != NULL)
387 Mix_FreeChunk(sound[i]);
396 Mix_FreeMusic(music);
401 if (quickSound != NULL)
402 Mix_FreeChunk(quickSound);
407 void Audio::destroy()
411 for (int i = MAX_SOUNDS - 3 ; i < MAX_SOUNDS ; i++)
413 if (sound[i] != NULL)
415 Mix_FreeChunk(sound[i]);