[3060] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Erwin 'vaiursch' Herrsche |
---|
[6117] | 24 | * Kevin Young |
---|
[6244] | 25 | * Reto Grieder |
---|
[3060] | 26 | * Co-authors: |
---|
| 27 | * ... |
---|
| 28 | * |
---|
| 29 | */ |
---|
| 30 | |
---|
[3196] | 31 | #include "SoundManager.h" |
---|
| 32 | |
---|
[3060] | 33 | #include <AL/alut.h> |
---|
[6117] | 34 | #include <utility> |
---|
[3060] | 35 | |
---|
[5929] | 36 | #include "util/Exception.h" |
---|
[3196] | 37 | #include "util/Math.h" |
---|
[5929] | 38 | #include "util/ScopeGuard.h" |
---|
[6117] | 39 | #include "util/Clock.h" |
---|
[6203] | 40 | #include "core/ConfigValueIncludes.h" |
---|
[5929] | 41 | #include "core/GameMode.h" |
---|
| 42 | #include "core/ScopedSingletonManager.h" |
---|
[6203] | 43 | #include "core/Resource.h" |
---|
| 44 | #include "SoundBuffer.h" |
---|
[6117] | 45 | #include "BaseSound.h" |
---|
| 46 | #include "AmbientSound.h" |
---|
[6184] | 47 | #include "WorldSound.h" |
---|
[3060] | 48 | |
---|
| 49 | namespace orxonox |
---|
| 50 | { |
---|
[5929] | 51 | ManageScopedSingleton(SoundManager, ScopeID::Graphics, true); |
---|
[3060] | 52 | |
---|
| 53 | SoundManager::SoundManager() |
---|
[6254] | 54 | : effectsPoolSize_(0) |
---|
[3060] | 55 | { |
---|
[6117] | 56 | RegisterRootObject(SoundManager); |
---|
| 57 | |
---|
| 58 | if (!alutInitWithoutContext(NULL, NULL)) |
---|
[6244] | 59 | ThrowException(InitialisationFailed, "Sound Error: ALUT initialisation failed: " << alutGetErrorString(alutGetError())); |
---|
[5929] | 60 | Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); |
---|
| 61 | |
---|
[6244] | 62 | // Get list of available sound devices and display them |
---|
[6298] | 63 | /* const char* devices = alcGetString(NULL, ALC_DEVICE_SPECIFIER); |
---|
| 64 | char* device = new char[strlen(devices)+1]; |
---|
| 65 | strcpy(device, devices); |
---|
[6244] | 66 | std::string renderDevice; |
---|
[6298] | 67 | // SetConfigValue(renderDevice, std::string(device)).description("Sound device used for rendering"); |
---|
[6244] | 68 | COUT(4) << "Sound: Available devices: "; |
---|
| 69 | while (true) |
---|
| 70 | { |
---|
| 71 | this->deviceNames_.push_back(devices); |
---|
| 72 | COUT(4) << "\"" << devices << "\", "; |
---|
| 73 | devices += strlen(devices) + 1; |
---|
| 74 | if (*devices == '\0') |
---|
| 75 | break; |
---|
| 76 | } |
---|
| 77 | COUT(4) << std::endl; |
---|
| 78 | |
---|
| 79 | // Open the selected device |
---|
| 80 | COUT(3) << "Sound: Opening device \"" << renderDevice << "\"" << std::endl; |
---|
[6298] | 81 | this->device_ = alcOpenDevice(renderDevice.c_str());*/ |
---|
| 82 | this->device_ = alcOpenDevice(NULL); |
---|
[5929] | 83 | if (this->device_ == NULL) |
---|
[3060] | 84 | { |
---|
[6244] | 85 | COUT(1) << "Sound: Could not open sound device. Have you installed OpenAL?" << std::endl; |
---|
[5929] | 86 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[6244] | 87 | COUT(1) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; |
---|
[5929] | 88 | #endif |
---|
[6322] | 89 | ThrowException(InitialisationFailed, "Sound Error: Could not open sound device."); |
---|
[3060] | 90 | } |
---|
[5929] | 91 | Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); |
---|
| 92 | |
---|
[6244] | 93 | // Create sound context and make it the currently used one |
---|
[5929] | 94 | this->context_ = alcCreateContext(this->device_, NULL); |
---|
| 95 | if (this->context_ == NULL) |
---|
[6244] | 96 | ThrowException(InitialisationFailed, "Sound Error: Could not create ALC context"); |
---|
[5929] | 97 | Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_); |
---|
[6244] | 98 | if (!alcMakeContextCurrent(this->context_)) |
---|
| 99 | ThrowException(InitialisationFailed, "Sound Error: Could not use ALC context"); |
---|
[5929] | 100 | |
---|
[6244] | 101 | GameMode::setPlaysSound(true); |
---|
[6322] | 102 | Loki::ScopeGuard resetPlaysSoundGuard = Loki::MakeGuard(&GameMode::setPlaysSound, false); |
---|
[5929] | 103 | |
---|
[6244] | 104 | // Get some information about the sound |
---|
| 105 | if (const char* version = alGetString(AL_VERSION)) |
---|
| 106 | COUT(4) << "Sound: --- OpenAL Version: " << version << std::endl; |
---|
| 107 | if (const char* vendor = alGetString(AL_VENDOR)) |
---|
| 108 | COUT(4) << "Sound: --- OpenAL Vendor : " << vendor << std::endl; |
---|
| 109 | if (const char* types = alutGetMIMETypes(ALUT_LOADER_BUFFER)) |
---|
| 110 | COUT(4) << "Sound: --- Supported MIME Types: " << types << std::endl; |
---|
[3060] | 111 | else |
---|
[6244] | 112 | COUT(2) << "Sound Warning: MIME Type retrieval failed: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
[6184] | 113 | |
---|
[6186] | 114 | this->setVolumeInternal(1.0, SoundType::none); |
---|
| 115 | this->setVolumeInternal(1.0, SoundType::ambient); |
---|
| 116 | this->setVolumeInternal(1.0, SoundType::effects); |
---|
| 117 | |
---|
| 118 | this->mute_[SoundType::none] = false; |
---|
| 119 | this->mute_[SoundType::ambient] = false; |
---|
| 120 | this->mute_[SoundType::effects] = false; |
---|
[6117] | 121 | |
---|
| 122 | this->setConfigValues(); |
---|
[6244] | 123 | |
---|
[6322] | 124 | // Try to get at least one source |
---|
| 125 | ALuint source; |
---|
| 126 | alGenSources(1, &source); |
---|
| 127 | if (!alGetError() && alIsSource(source)) |
---|
| 128 | this->soundSources_.push_back(source); |
---|
| 129 | else |
---|
| 130 | ThrowException(InitialisationFailed, "Sound Error: Could not even create a single source"); |
---|
| 131 | // Get the rest of the sources |
---|
| 132 | alGenSources(1, &source); |
---|
| 133 | unsigned int count = 1; |
---|
| 134 | while (alIsSource(source) && !alGetError() && count <= this->maxSources_) |
---|
| 135 | { |
---|
| 136 | this->soundSources_.push_back(source); |
---|
| 137 | alGenSources(1, &source); |
---|
| 138 | ++count; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | // Disarm guards |
---|
| 142 | alutExitGuard.Dismiss(); |
---|
| 143 | closeDeviceGuard.Dismiss(); |
---|
| 144 | desroyContextGuard.Dismiss(); |
---|
| 145 | resetPlaysSoundGuard.Dismiss(); |
---|
| 146 | |
---|
[6244] | 147 | COUT(4) << "Sound: Initialisation complete" << std::endl; |
---|
[3060] | 148 | } |
---|
| 149 | |
---|
| 150 | SoundManager::~SoundManager() |
---|
| 151 | { |
---|
[5929] | 152 | GameMode::setPlaysSound(false); |
---|
[6244] | 153 | |
---|
| 154 | // Relieve context to destroy it |
---|
| 155 | if (!alcMakeContextCurrent(NULL)) |
---|
| 156 | COUT(1) << "Sound Error: Could not unset ALC context" << std::endl; |
---|
[3060] | 157 | alcDestroyContext(this->context_); |
---|
[6244] | 158 | if (ALCenum error = alcGetError(this->device_)) |
---|
| 159 | { |
---|
| 160 | if (error == AL_INVALID_OPERATION) |
---|
| 161 | COUT(1) << "Sound Error: Could not destroy ALC context because it is the current one" << std::endl; |
---|
| 162 | else |
---|
| 163 | COUT(1) << "Sound Error: Could not destroy ALC context because it is invalid" << std::endl; |
---|
| 164 | } |
---|
| 165 | #ifdef AL_VERSION_1_1 |
---|
| 166 | if (!alcCloseDevice(this->device_)) |
---|
| 167 | COUT(1) << "Sound Error: Could not destroy ALC device. This might be because there are still buffers in use!" << std::endl; |
---|
| 168 | #else |
---|
[3280] | 169 | alcCloseDevice(this->device_); |
---|
[6244] | 170 | #endif |
---|
| 171 | if (!alutExit()) |
---|
| 172 | COUT(1) << "Sound Error: Closing ALUT failed: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
[3060] | 173 | } |
---|
| 174 | |
---|
[6183] | 175 | void SoundManager::preUpdate(const Clock& time) |
---|
[6117] | 176 | { |
---|
| 177 | this->processCrossFading(time.getDeltaTime()); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | void SoundManager::setConfigValues() |
---|
| 181 | { |
---|
| 182 | SetConfigValue(crossFadeStep_, 0.2f) |
---|
| 183 | .description("Determines how fast sounds should fade, per second.") |
---|
| 184 | .callback(this, &SoundManager::checkFadeStepValidity); |
---|
[6322] | 185 | |
---|
[6186] | 186 | SetConfigValue(soundVolume_, 1.0f) |
---|
[6184] | 187 | .description("Defines the overall volume.") |
---|
[6186] | 188 | .callback(this, &SoundManager::checkSoundVolumeValidity); |
---|
[6322] | 189 | |
---|
[6184] | 190 | SetConfigValue(ambientVolume_, 1.0f) |
---|
| 191 | .description("Defines the ambient volume.") |
---|
| 192 | .callback(this, &SoundManager::checkAmbientVolumeValidity); |
---|
[6322] | 193 | |
---|
[6184] | 194 | SetConfigValue(effectsVolume_, 1.0f) |
---|
| 195 | .description("Defines the effects volume.") |
---|
| 196 | .callback(this, &SoundManager::checkEffectsVolumeValidity); |
---|
[6322] | 197 | |
---|
| 198 | SetConfigValue(maxSources_, 1024) |
---|
| 199 | .description("Maximum number of sources to be made available"); |
---|
[6117] | 200 | } |
---|
| 201 | |
---|
[6244] | 202 | std::string SoundManager::getALErrorString(ALenum code) |
---|
| 203 | { |
---|
| 204 | switch (code) |
---|
| 205 | { |
---|
| 206 | case AL_NO_ERROR: return "No error"; |
---|
| 207 | case AL_INVALID_NAME: return "Invalid AL parameter name"; |
---|
| 208 | case AL_INVALID_ENUM: return "Invalid AL enum"; |
---|
| 209 | case AL_INVALID_VALUE: return "Invalid AL value"; |
---|
| 210 | case AL_INVALID_OPERATION: return "Invalid AL operation"; |
---|
| 211 | case AL_OUT_OF_MEMORY: return "AL reports out of memory"; |
---|
| 212 | default: return "Unknown AL error"; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
[6117] | 216 | void SoundManager::checkFadeStepValidity() |
---|
| 217 | { |
---|
| 218 | if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 ) |
---|
| 219 | { |
---|
| 220 | COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl; |
---|
| 221 | ResetConfigValue(crossFadeStep_); |
---|
| 222 | } |
---|
| 223 | COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl; |
---|
| 224 | return; |
---|
| 225 | } |
---|
[6184] | 226 | |
---|
[6186] | 227 | bool SoundManager::checkVolumeValidity(SoundType::Value type) |
---|
[6184] | 228 | { |
---|
[6186] | 229 | bool valid = true; |
---|
| 230 | |
---|
| 231 | if(this->getVolumeInternal(type) < 0.0 || this->getVolumeInternal(type) > 1.0) |
---|
[6184] | 232 | { |
---|
| 233 | COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl; |
---|
[6186] | 234 | valid = false; |
---|
[6184] | 235 | } |
---|
| 236 | |
---|
[6186] | 237 | this->updateVolume(type); |
---|
[6241] | 238 | COUT(4) << "SoundManager: volume set to " << this->getVolumeInternal(type) << std::endl; |
---|
[6186] | 239 | return valid; |
---|
[6184] | 240 | } |
---|
| 241 | |
---|
[6186] | 242 | void SoundManager::checkSoundVolumeValidity() |
---|
| 243 | { |
---|
[6197] | 244 | if(!checkVolumeValidity(SoundType::none)) |
---|
[6186] | 245 | { |
---|
| 246 | ResetConfigValue(soundVolume_); |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
[6184] | 250 | void SoundManager::checkAmbientVolumeValidity() |
---|
| 251 | { |
---|
[6197] | 252 | if(!checkVolumeValidity(SoundType::ambient)) |
---|
[6184] | 253 | { |
---|
| 254 | ResetConfigValue(ambientVolume_); |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | void SoundManager::checkEffectsVolumeValidity() |
---|
| 259 | { |
---|
[6197] | 260 | if(!checkVolumeValidity(SoundType::effects)) |
---|
[6184] | 261 | { |
---|
| 262 | ResetConfigValue(effectsVolume_); |
---|
| 263 | } |
---|
| 264 | } |
---|
[6117] | 265 | |
---|
[5929] | 266 | void SoundManager::setListenerPosition(const Vector3& position) |
---|
[3060] | 267 | { |
---|
[5929] | 268 | alListener3f(AL_POSITION, position.x, position.y, position.z); |
---|
| 269 | ALenum error = alGetError(); |
---|
| 270 | if (error == AL_INVALID_VALUE) |
---|
| 271 | COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl; |
---|
[3060] | 272 | } |
---|
| 273 | |
---|
[5929] | 274 | void SoundManager::setListenerOrientation(const Quaternion& orientation) |
---|
[3060] | 275 | { |
---|
[5929] | 276 | // update listener orientation |
---|
[6269] | 277 | const Vector3& direction = -orientation.zAxis(); |
---|
| 278 | const Vector3& up = orientation.yAxis(); |
---|
[3060] | 279 | |
---|
[6269] | 280 | ALfloat orient[6] = { direction.x, direction.y, direction.z, up.x, up.y, up.z }; |
---|
[3060] | 281 | |
---|
[6269] | 282 | alListenerfv(AL_ORIENTATION, orient); |
---|
[3060] | 283 | ALenum error = alGetError(); |
---|
[5929] | 284 | if (error == AL_INVALID_VALUE) |
---|
[3060] | 285 | COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; |
---|
| 286 | } |
---|
[6117] | 287 | |
---|
| 288 | void SoundManager::registerAmbientSound(AmbientSound* newAmbient) |
---|
| 289 | { |
---|
| 290 | if (newAmbient != NULL) |
---|
| 291 | { |
---|
| 292 | for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
---|
| 293 | { |
---|
| 294 | if (it->first == newAmbient) |
---|
| 295 | { |
---|
| 296 | COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl; |
---|
| 297 | return; |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | if (!this->ambientSounds_.empty()) |
---|
| 302 | { |
---|
| 303 | this->fadeOut(ambientSounds_.front().first); |
---|
| 304 | } |
---|
| 305 | this->ambientSounds_.push_front(std::make_pair(newAmbient, false)); |
---|
| 306 | newAmbient->doPlay(); |
---|
| 307 | this->fadeIn(newAmbient); |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient) |
---|
| 312 | { |
---|
| 313 | if (oldAmbient == NULL || ambientSounds_.empty()) |
---|
| 314 | { |
---|
| 315 | return; |
---|
| 316 | } |
---|
| 317 | if (this->ambientSounds_.front().first == oldAmbient) |
---|
| 318 | { |
---|
| 319 | this->fadeOut(oldAmbient); |
---|
| 320 | this->ambientSounds_.pop_front(); |
---|
| 321 | if (!this->ambientSounds_.empty()) |
---|
| 322 | { |
---|
| 323 | if (!this->ambientSounds_.front().second) // Not paused before |
---|
| 324 | { |
---|
| 325 | this->ambientSounds_.front().first->doPlay(); |
---|
| 326 | } |
---|
| 327 | this->fadeIn(this->ambientSounds_.front().first); |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | else |
---|
| 331 | { |
---|
| 332 | for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
---|
| 333 | { |
---|
| 334 | if (it->first == oldAmbient) |
---|
| 335 | { |
---|
| 336 | this->fadeOut(oldAmbient); |
---|
| 337 | this->ambientSounds_.erase(it); |
---|
| 338 | break; |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | void SoundManager::pauseAmbientSound(AmbientSound* ambient) |
---|
| 345 | { |
---|
| 346 | if (ambient != NULL) |
---|
| 347 | { |
---|
| 348 | for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
---|
| 349 | { |
---|
| 350 | if (it->first == ambient) |
---|
| 351 | { |
---|
| 352 | it->second = true; |
---|
| 353 | this->fadeOut(it->first); |
---|
| 354 | return; |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | } |
---|
| 358 | } |
---|
[6184] | 359 | |
---|
[6186] | 360 | |
---|
| 361 | void SoundManager::setVolume(float vol, SoundType::Value type) |
---|
[6184] | 362 | { |
---|
[6186] | 363 | vol = this->checkVolumeRange(vol); |
---|
| 364 | |
---|
| 365 | this->setVolumeInternal(vol, type); |
---|
| 366 | |
---|
| 367 | this->updateVolume(type); |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | float SoundManager::checkVolumeRange(float vol) |
---|
| 371 | { |
---|
| 372 | if(vol < 0.0 || vol > 1.0) |
---|
[6184] | 373 | { |
---|
| 374 | COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl; |
---|
| 375 | vol = vol > 1 ? 1 : vol; |
---|
| 376 | vol = vol < 0 ? 0 : vol; |
---|
| 377 | } |
---|
| 378 | |
---|
[6186] | 379 | return vol; |
---|
[6184] | 380 | } |
---|
| 381 | |
---|
[6186] | 382 | void SoundManager::updateVolume(SoundType::Value type) |
---|
[6184] | 383 | { |
---|
[6186] | 384 | switch(type) |
---|
[6184] | 385 | { |
---|
[6186] | 386 | case SoundType::none: |
---|
| 387 | for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it) |
---|
| 388 | { |
---|
| 389 | (*it)->updateVolume(); |
---|
| 390 | } |
---|
| 391 | break; |
---|
| 392 | case SoundType::ambient: |
---|
| 393 | for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it) |
---|
| 394 | { |
---|
| 395 | (*it)->updateVolume(); |
---|
| 396 | } |
---|
| 397 | break; |
---|
| 398 | case SoundType::effects: |
---|
| 399 | for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it) |
---|
| 400 | { |
---|
| 401 | (*it)->updateVolume(); |
---|
| 402 | } |
---|
| 403 | break; |
---|
| 404 | default: |
---|
| 405 | COUT(2) << "Invalid SoundType in SoundManager::updateVolume() - Not updating!" << std::endl; |
---|
[6184] | 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
[6186] | 409 | void SoundManager::setVolumeInternal(float vol, SoundType::Value type) |
---|
[6184] | 410 | { |
---|
[6186] | 411 | switch(type) |
---|
[6184] | 412 | { |
---|
[6186] | 413 | case SoundType::none: |
---|
| 414 | this->soundVolume_ = vol; |
---|
| 415 | break; |
---|
| 416 | case SoundType::ambient: |
---|
| 417 | this->ambientVolume_ = vol; |
---|
| 418 | break; |
---|
| 419 | case SoundType::effects: |
---|
| 420 | this->effectsVolume_ = vol; |
---|
| 421 | break; |
---|
| 422 | default: |
---|
| 423 | COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Not setting any volume!" << std::endl; |
---|
[6184] | 424 | } |
---|
| 425 | } |
---|
| 426 | |
---|
[6186] | 427 | float SoundManager::getVolumeInternal(SoundType::Value type) |
---|
[6184] | 428 | { |
---|
[6186] | 429 | switch(type) |
---|
| 430 | { |
---|
| 431 | case SoundType::none: |
---|
| 432 | return this->soundVolume_; |
---|
| 433 | case SoundType::ambient: |
---|
| 434 | return this->ambientVolume_; |
---|
| 435 | case SoundType::effects: |
---|
| 436 | return this->effectsVolume_; |
---|
| 437 | default: |
---|
| 438 | COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Returning 0.0!" << std::endl; |
---|
| 439 | return 0.0; |
---|
| 440 | } |
---|
[6184] | 441 | } |
---|
| 442 | |
---|
[6186] | 443 | float SoundManager::getVolume(SoundType::Value type) |
---|
[6184] | 444 | { |
---|
[6186] | 445 | if(this->mute_[SoundType::none] || this->mute_[type]) |
---|
| 446 | return 0.0; |
---|
| 447 | |
---|
| 448 | if(type == SoundType::none) |
---|
| 449 | return this->getVolumeInternal(type); |
---|
| 450 | |
---|
| 451 | return this->getVolumeInternal(SoundType::none)*this->getVolumeInternal(type); |
---|
[6184] | 452 | } |
---|
| 453 | |
---|
[6186] | 454 | void SoundManager::toggleMute(SoundType::Value type) |
---|
[6184] | 455 | { |
---|
[6186] | 456 | bool mute = !this->mute_[type]; |
---|
| 457 | this->mute_[type] = mute; |
---|
| 458 | |
---|
| 459 | this->updateVolume(type); |
---|
[6184] | 460 | } |
---|
[6186] | 461 | |
---|
| 462 | bool SoundManager::getMute(SoundType::Value type) |
---|
| 463 | { |
---|
| 464 | return this->mute_[type]; |
---|
| 465 | } |
---|
| 466 | |
---|
[6117] | 467 | |
---|
| 468 | void SoundManager::fadeIn(AmbientSound* sound) |
---|
| 469 | { |
---|
| 470 | // If we're already fading out --> remove that |
---|
| 471 | for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++) |
---|
| 472 | { |
---|
| 473 | if (*it == sound) |
---|
| 474 | { |
---|
| 475 | this->fadeOutList_.erase(it); |
---|
| 476 | break; |
---|
| 477 | } |
---|
| 478 | } |
---|
| 479 | // No duplicate entries |
---|
| 480 | if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end()) |
---|
| 481 | this->fadeInList_.push_back(sound); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | void SoundManager::fadeOut(AmbientSound* sound) |
---|
| 485 | { |
---|
| 486 | // If we're already fading in --> remove that |
---|
| 487 | for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++) |
---|
| 488 | { |
---|
| 489 | if (*it == sound) |
---|
| 490 | { |
---|
| 491 | this->fadeInList_.erase(it); |
---|
| 492 | break; |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | // No duplicate entries |
---|
| 496 | if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end()) |
---|
| 497 | this->fadeOutList_.push_back(sound); |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | void SoundManager::processCrossFading(float dt) |
---|
| 501 | { |
---|
| 502 | |
---|
| 503 | // Hacky solution to the fade delay while loading a level. |
---|
| 504 | if(dt > 0.2) |
---|
| 505 | { |
---|
| 506 | return; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | // FADE IN |
---|
[6138] | 510 | for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); ) |
---|
[6117] | 511 | { |
---|
| 512 | if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f) |
---|
| 513 | { |
---|
| 514 | (*it)->setVolume(1.0f); |
---|
| 515 | this->fadeInList_.erase(it++); |
---|
| 516 | } |
---|
| 517 | else |
---|
| 518 | { |
---|
| 519 | (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt); |
---|
| 520 | ++it; |
---|
| 521 | } |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | // FADE OUT |
---|
[6138] | 525 | for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); ) |
---|
[6117] | 526 | { |
---|
| 527 | if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f) |
---|
| 528 | { |
---|
| 529 | (*it)->setVolume(0.0f); |
---|
| 530 | |
---|
| 531 | // If sound is in the ambient list --> pause |
---|
| 532 | for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2) |
---|
| 533 | { |
---|
| 534 | if (it2->first == *it) |
---|
| 535 | { |
---|
| 536 | (*it)->doPause(); |
---|
| 537 | break; |
---|
| 538 | } |
---|
| 539 | } |
---|
| 540 | // If not pause (by loop above for instance) --> stop |
---|
| 541 | if (!(*it)->isPaused()) |
---|
| 542 | (*it)->doStop(); |
---|
| 543 | |
---|
| 544 | this->fadeOutList_.erase(it++); |
---|
| 545 | } |
---|
| 546 | else |
---|
| 547 | { |
---|
| 548 | (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt); |
---|
| 549 | ++it; |
---|
| 550 | } |
---|
| 551 | } |
---|
| 552 | } |
---|
[6203] | 553 | |
---|
[6270] | 554 | shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(const std::string& filename) |
---|
[6203] | 555 | { |
---|
[6254] | 556 | shared_ptr<SoundBuffer> buffer; |
---|
| 557 | // Check active or pooled buffers |
---|
[6270] | 558 | SoundBufferMap::const_iterator it = this->soundBuffers_.find(filename); |
---|
[6203] | 559 | if (it != this->soundBuffers_.end()) |
---|
[6254] | 560 | { |
---|
| 561 | buffer = it->second; |
---|
| 562 | |
---|
| 563 | // Remove from effects pool if not active used before |
---|
| 564 | if (buffer->poolIterator_ != this->effectsPool_.end()) |
---|
| 565 | { |
---|
| 566 | this->effectsPoolSize_ -= buffer->getSize(); |
---|
| 567 | this->effectsPool_.erase(buffer->poolIterator_); |
---|
| 568 | buffer->poolIterator_ = this->effectsPool_.end(); |
---|
| 569 | } |
---|
| 570 | } |
---|
[6203] | 571 | else |
---|
| 572 | { |
---|
[6232] | 573 | try |
---|
| 574 | { |
---|
[6270] | 575 | buffer.reset(new SoundBuffer(filename)); |
---|
[6254] | 576 | buffer->poolIterator_ = this->effectsPool_.end(); |
---|
[6232] | 577 | } |
---|
| 578 | catch (...) |
---|
| 579 | { |
---|
| 580 | COUT(1) << Exception::handleMessage() << std::endl; |
---|
[6254] | 581 | return buffer; |
---|
[6232] | 582 | } |
---|
[6270] | 583 | this->soundBuffers_[filename] = buffer; |
---|
[6203] | 584 | } |
---|
[6254] | 585 | return buffer; |
---|
[6203] | 586 | } |
---|
| 587 | |
---|
[6254] | 588 | void SoundManager::releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer) |
---|
[6203] | 589 | { |
---|
[6254] | 590 | // Check if others are still using the buffer |
---|
| 591 | if (buffer.use_count() != 2) |
---|
| 592 | return; |
---|
[6270] | 593 | SoundBufferMap::iterator it = this->soundBuffers_.find(buffer->getFilename()); |
---|
[6232] | 594 | if (it != this->soundBuffers_.end()) |
---|
[6254] | 595 | { |
---|
| 596 | if (bPoolBuffer) |
---|
| 597 | { |
---|
| 598 | // Pool already too large? |
---|
| 599 | while (this->effectsPoolSize_ + it->second->getSize() > this->maxEffectsPoolSize_s && !this->effectsPool_.empty()) |
---|
| 600 | { |
---|
| 601 | shared_ptr<SoundBuffer> bufferDel = this->effectsPool_.back(); |
---|
| 602 | this->effectsPoolSize_ -= bufferDel->getSize(); |
---|
| 603 | bufferDel->poolIterator_ = this->effectsPool_.end(); |
---|
| 604 | this->effectsPool_.pop_back(); |
---|
| 605 | // Remove from buffer map too |
---|
[6270] | 606 | SoundBufferMap::iterator itDel = this->soundBuffers_.find(bufferDel->getFilename()); |
---|
[6254] | 607 | if (itDel != this->soundBuffers_.end()) |
---|
| 608 | this->soundBuffers_.erase(itDel); |
---|
| 609 | } |
---|
| 610 | // Put buffer into the pool |
---|
| 611 | this->effectsPoolSize_ += it->second->getSize(); |
---|
| 612 | this->effectsPool_.push_front(it->second); |
---|
| 613 | it->second->poolIterator_ = this->effectsPool_.begin(); |
---|
| 614 | } |
---|
| 615 | else |
---|
| 616 | this->soundBuffers_.erase(it); |
---|
| 617 | } |
---|
[6203] | 618 | } |
---|
[6322] | 619 | |
---|
| 620 | ALuint SoundManager::getSoundSource() |
---|
| 621 | { |
---|
| 622 | if (!this->soundSources_.empty()) |
---|
| 623 | { |
---|
| 624 | ALuint source = this->soundSources_.back(); |
---|
| 625 | this->soundSources_.pop_back(); |
---|
| 626 | return source; |
---|
| 627 | } |
---|
| 628 | else |
---|
| 629 | { |
---|
| 630 | // Return no source ID |
---|
| 631 | ALuint source = 123456789; |
---|
| 632 | while (alIsSource(++source)); |
---|
| 633 | return source; |
---|
| 634 | } |
---|
| 635 | } |
---|
| 636 | |
---|
| 637 | void SoundManager::releaseSoundSource(ALuint source) |
---|
| 638 | { |
---|
| 639 | #ifndef NDEBUG |
---|
| 640 | for (std::vector<ALuint>::const_iterator it = this->soundSources_.begin(); it != this->soundSources_.end(); ++it) |
---|
| 641 | assert((*it) != source); |
---|
| 642 | #endif |
---|
| 643 | this->soundSources_.push_back(source); |
---|
| 644 | } |
---|
[3060] | 645 | } |
---|