| [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 |
|---|
| [3060] | 25 | * Co-authors: |
|---|
| 26 | * ... |
|---|
| 27 | * |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| [3196] | 30 | #include "SoundManager.h" |
|---|
| 31 | |
|---|
| [3060] | 32 | #include <AL/alut.h> |
|---|
| [6117] | 33 | #include <utility> |
|---|
| [3060] | 34 | |
|---|
| [5929] | 35 | #include "util/Exception.h" |
|---|
| [3196] | 36 | #include "util/Math.h" |
|---|
| [5929] | 37 | #include "util/ScopeGuard.h" |
|---|
| [6117] | 38 | #include "util/StringUtils.h" |
|---|
| 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() |
|---|
| 54 | { |
|---|
| [6117] | 55 | RegisterRootObject(SoundManager); |
|---|
| 56 | |
|---|
| 57 | if (!alutInitWithoutContext(NULL, NULL)) |
|---|
| 58 | ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError())); |
|---|
| [5929] | 59 | Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); |
|---|
| 60 | |
|---|
| [6117] | 61 | COUT(3) << "Sound: OpenAL: Opening sound device..." << std::endl; |
|---|
| [5929] | 62 | this->device_ = alcOpenDevice(NULL); |
|---|
| 63 | if (this->device_ == NULL) |
|---|
| [3060] | 64 | { |
|---|
| [6117] | 65 | COUT(0) << "Sound: OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl; |
|---|
| [5929] | 66 | #ifdef ORXONOX_PLATFORM_WINDOWS |
|---|
| [6117] | 67 | COUT(0) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; |
|---|
| [5929] | 68 | #endif |
|---|
| [6117] | 69 | ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device."); |
|---|
| [3060] | 70 | } |
|---|
| [5929] | 71 | Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); |
|---|
| 72 | |
|---|
| [6117] | 73 | COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl; |
|---|
| [5929] | 74 | this->context_ = alcCreateContext(this->device_, NULL); |
|---|
| 75 | if (this->context_ == NULL) |
|---|
| [6117] | 76 | ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not create sound context"); |
|---|
| [5929] | 77 | Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_); |
|---|
| 78 | |
|---|
| 79 | if (alcMakeContextCurrent(this->context_) == AL_TRUE) |
|---|
| [6117] | 80 | COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl; |
|---|
| [5929] | 81 | |
|---|
| 82 | COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl; |
|---|
| 83 | |
|---|
| 84 | const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER); |
|---|
| 85 | if (str == NULL) |
|---|
| [6117] | 86 | COUT(2) << "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl; |
|---|
| [3060] | 87 | else |
|---|
| [6117] | 88 | COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl; |
|---|
| [3060] | 89 | |
|---|
| [5929] | 90 | GameMode::setPlaysSound(true); |
|---|
| 91 | // Disarm guards |
|---|
| 92 | alutExitGuard.Dismiss(); |
|---|
| 93 | closeDeviceGuard.Dismiss(); |
|---|
| 94 | desroyContextGuard.Dismiss(); |
|---|
| [6184] | 95 | |
|---|
| [6186] | 96 | this->setVolumeInternal(1.0, SoundType::none); |
|---|
| 97 | this->setVolumeInternal(1.0, SoundType::ambient); |
|---|
| 98 | this->setVolumeInternal(1.0, SoundType::effects); |
|---|
| 99 | |
|---|
| 100 | this->mute_[SoundType::none] = false; |
|---|
| 101 | this->mute_[SoundType::ambient] = false; |
|---|
| 102 | this->mute_[SoundType::effects] = false; |
|---|
| [6117] | 103 | |
|---|
| 104 | this->setConfigValues(); |
|---|
| [3060] | 105 | } |
|---|
| 106 | |
|---|
| 107 | SoundManager::~SoundManager() |
|---|
| 108 | { |
|---|
| [5929] | 109 | GameMode::setPlaysSound(false); |
|---|
| [3060] | 110 | alcDestroyContext(this->context_); |
|---|
| [3280] | 111 | alcCloseDevice(this->device_); |
|---|
| [3060] | 112 | alutExit(); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| [6183] | 115 | void SoundManager::preUpdate(const Clock& time) |
|---|
| [6117] | 116 | { |
|---|
| 117 | this->processCrossFading(time.getDeltaTime()); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void SoundManager::setConfigValues() |
|---|
| 121 | { |
|---|
| 122 | SetConfigValue(crossFadeStep_, 0.2f) |
|---|
| 123 | .description("Determines how fast sounds should fade, per second.") |
|---|
| 124 | .callback(this, &SoundManager::checkFadeStepValidity); |
|---|
| [6184] | 125 | |
|---|
| [6186] | 126 | SetConfigValue(soundVolume_, 1.0f) |
|---|
| [6184] | 127 | .description("Defines the overall volume.") |
|---|
| [6186] | 128 | .callback(this, &SoundManager::checkSoundVolumeValidity); |
|---|
| [6184] | 129 | |
|---|
| 130 | SetConfigValue(ambientVolume_, 1.0f) |
|---|
| 131 | .description("Defines the ambient volume.") |
|---|
| 132 | .callback(this, &SoundManager::checkAmbientVolumeValidity); |
|---|
| 133 | |
|---|
| 134 | SetConfigValue(effectsVolume_, 1.0f) |
|---|
| 135 | .description("Defines the effects volume.") |
|---|
| 136 | .callback(this, &SoundManager::checkEffectsVolumeValidity); |
|---|
| [6117] | 137 | } |
|---|
| 138 | |
|---|
| 139 | void SoundManager::checkFadeStepValidity() |
|---|
| 140 | { |
|---|
| 141 | if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 ) |
|---|
| 142 | { |
|---|
| 143 | COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl; |
|---|
| 144 | ResetConfigValue(crossFadeStep_); |
|---|
| 145 | } |
|---|
| 146 | COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl; |
|---|
| 147 | return; |
|---|
| 148 | } |
|---|
| [6184] | 149 | |
|---|
| [6186] | 150 | bool SoundManager::checkVolumeValidity(SoundType::Value type) |
|---|
| [6184] | 151 | { |
|---|
| [6186] | 152 | bool valid = true; |
|---|
| 153 | |
|---|
| 154 | if(this->getVolumeInternal(type) < 0.0 || this->getVolumeInternal(type) > 1.0) |
|---|
| [6184] | 155 | { |
|---|
| 156 | COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl; |
|---|
| [6186] | 157 | valid = false; |
|---|
| [6184] | 158 | } |
|---|
| 159 | |
|---|
| [6186] | 160 | this->updateVolume(type); |
|---|
| 161 | COUT(3) << "SoundManager: volume set to " << this->getVolumeInternal(type) << std::endl; |
|---|
| 162 | return valid; |
|---|
| [6184] | 163 | } |
|---|
| 164 | |
|---|
| [6186] | 165 | void SoundManager::checkSoundVolumeValidity() |
|---|
| 166 | { |
|---|
| [6197] | 167 | if(!checkVolumeValidity(SoundType::none)) |
|---|
| [6186] | 168 | { |
|---|
| 169 | ResetConfigValue(soundVolume_); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| [6184] | 173 | void SoundManager::checkAmbientVolumeValidity() |
|---|
| 174 | { |
|---|
| [6197] | 175 | if(!checkVolumeValidity(SoundType::ambient)) |
|---|
| [6184] | 176 | { |
|---|
| 177 | ResetConfigValue(ambientVolume_); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | void SoundManager::checkEffectsVolumeValidity() |
|---|
| 182 | { |
|---|
| [6197] | 183 | if(!checkVolumeValidity(SoundType::effects)) |
|---|
| [6184] | 184 | { |
|---|
| 185 | ResetConfigValue(effectsVolume_); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| [6117] | 188 | |
|---|
| [5929] | 189 | void SoundManager::setListenerPosition(const Vector3& position) |
|---|
| [3060] | 190 | { |
|---|
| [5929] | 191 | alListener3f(AL_POSITION, position.x, position.y, position.z); |
|---|
| 192 | ALenum error = alGetError(); |
|---|
| 193 | if (error == AL_INVALID_VALUE) |
|---|
| 194 | COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl; |
|---|
| [3060] | 195 | } |
|---|
| 196 | |
|---|
| [5929] | 197 | void SoundManager::setListenerOrientation(const Quaternion& orientation) |
|---|
| [3060] | 198 | { |
|---|
| [5929] | 199 | // update listener orientation |
|---|
| 200 | Vector3 up = orientation.xAxis(); // just a wild guess |
|---|
| 201 | Vector3 at = orientation.zAxis(); |
|---|
| [3060] | 202 | |
|---|
| [5929] | 203 | ALfloat orient[6] = { at.x, at.y, at.z, |
|---|
| 204 | up.x, up.y, up.z }; |
|---|
| [3060] | 205 | |
|---|
| [5929] | 206 | alListenerfv(AL_POSITION, orient); |
|---|
| [3060] | 207 | ALenum error = alGetError(); |
|---|
| [5929] | 208 | if (error == AL_INVALID_VALUE) |
|---|
| [3060] | 209 | COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; |
|---|
| 210 | } |
|---|
| [6117] | 211 | |
|---|
| 212 | void SoundManager::registerAmbientSound(AmbientSound* newAmbient) |
|---|
| 213 | { |
|---|
| 214 | if (newAmbient != NULL) |
|---|
| 215 | { |
|---|
| 216 | for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
|---|
| 217 | { |
|---|
| 218 | if (it->first == newAmbient) |
|---|
| 219 | { |
|---|
| 220 | COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl; |
|---|
| 221 | return; |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | if (!this->ambientSounds_.empty()) |
|---|
| 226 | { |
|---|
| 227 | this->fadeOut(ambientSounds_.front().first); |
|---|
| 228 | } |
|---|
| 229 | this->ambientSounds_.push_front(std::make_pair(newAmbient, false)); |
|---|
| 230 | newAmbient->doPlay(); |
|---|
| 231 | this->fadeIn(newAmbient); |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient) |
|---|
| 236 | { |
|---|
| 237 | if (oldAmbient == NULL || ambientSounds_.empty()) |
|---|
| 238 | { |
|---|
| 239 | return; |
|---|
| 240 | } |
|---|
| 241 | if (this->ambientSounds_.front().first == oldAmbient) |
|---|
| 242 | { |
|---|
| 243 | this->fadeOut(oldAmbient); |
|---|
| 244 | this->ambientSounds_.pop_front(); |
|---|
| 245 | if (!this->ambientSounds_.empty()) |
|---|
| 246 | { |
|---|
| 247 | if (!this->ambientSounds_.front().second) // Not paused before |
|---|
| 248 | { |
|---|
| 249 | this->ambientSounds_.front().first->doPlay(); |
|---|
| 250 | } |
|---|
| 251 | this->fadeIn(this->ambientSounds_.front().first); |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | else |
|---|
| 255 | { |
|---|
| 256 | for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
|---|
| 257 | { |
|---|
| 258 | if (it->first == oldAmbient) |
|---|
| 259 | { |
|---|
| 260 | this->fadeOut(oldAmbient); |
|---|
| 261 | this->ambientSounds_.erase(it); |
|---|
| 262 | break; |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | void SoundManager::pauseAmbientSound(AmbientSound* ambient) |
|---|
| 269 | { |
|---|
| 270 | if (ambient != NULL) |
|---|
| 271 | { |
|---|
| 272 | for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
|---|
| 273 | { |
|---|
| 274 | if (it->first == ambient) |
|---|
| 275 | { |
|---|
| 276 | it->second = true; |
|---|
| 277 | this->fadeOut(it->first); |
|---|
| 278 | return; |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| [6184] | 283 | |
|---|
| [6186] | 284 | |
|---|
| 285 | void SoundManager::setVolume(float vol, SoundType::Value type) |
|---|
| [6184] | 286 | { |
|---|
| [6186] | 287 | vol = this->checkVolumeRange(vol); |
|---|
| 288 | |
|---|
| 289 | this->setVolumeInternal(vol, type); |
|---|
| 290 | |
|---|
| 291 | this->updateVolume(type); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | float SoundManager::checkVolumeRange(float vol) |
|---|
| 295 | { |
|---|
| 296 | if(vol < 0.0 || vol > 1.0) |
|---|
| [6184] | 297 | { |
|---|
| 298 | COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl; |
|---|
| 299 | vol = vol > 1 ? 1 : vol; |
|---|
| 300 | vol = vol < 0 ? 0 : vol; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| [6186] | 303 | return vol; |
|---|
| [6184] | 304 | } |
|---|
| 305 | |
|---|
| [6186] | 306 | void SoundManager::updateVolume(SoundType::Value type) |
|---|
| [6184] | 307 | { |
|---|
| [6186] | 308 | switch(type) |
|---|
| [6184] | 309 | { |
|---|
| [6186] | 310 | case SoundType::none: |
|---|
| 311 | for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it) |
|---|
| 312 | { |
|---|
| 313 | (*it)->updateVolume(); |
|---|
| 314 | } |
|---|
| 315 | break; |
|---|
| 316 | case SoundType::ambient: |
|---|
| 317 | for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it) |
|---|
| 318 | { |
|---|
| 319 | (*it)->updateVolume(); |
|---|
| 320 | } |
|---|
| 321 | break; |
|---|
| 322 | case SoundType::effects: |
|---|
| 323 | for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it) |
|---|
| 324 | { |
|---|
| 325 | (*it)->updateVolume(); |
|---|
| 326 | } |
|---|
| 327 | break; |
|---|
| 328 | default: |
|---|
| 329 | COUT(2) << "Invalid SoundType in SoundManager::updateVolume() - Not updating!" << std::endl; |
|---|
| [6184] | 330 | } |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| [6186] | 333 | void SoundManager::setVolumeInternal(float vol, SoundType::Value type) |
|---|
| [6184] | 334 | { |
|---|
| [6186] | 335 | switch(type) |
|---|
| [6184] | 336 | { |
|---|
| [6186] | 337 | case SoundType::none: |
|---|
| 338 | this->soundVolume_ = vol; |
|---|
| 339 | break; |
|---|
| 340 | case SoundType::ambient: |
|---|
| 341 | this->ambientVolume_ = vol; |
|---|
| 342 | break; |
|---|
| 343 | case SoundType::effects: |
|---|
| 344 | this->effectsVolume_ = vol; |
|---|
| 345 | break; |
|---|
| 346 | default: |
|---|
| 347 | COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Not setting any volume!" << std::endl; |
|---|
| [6184] | 348 | } |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| [6186] | 351 | float SoundManager::getVolumeInternal(SoundType::Value type) |
|---|
| [6184] | 352 | { |
|---|
| [6186] | 353 | switch(type) |
|---|
| 354 | { |
|---|
| 355 | case SoundType::none: |
|---|
| 356 | return this->soundVolume_; |
|---|
| 357 | case SoundType::ambient: |
|---|
| 358 | return this->ambientVolume_; |
|---|
| 359 | case SoundType::effects: |
|---|
| 360 | return this->effectsVolume_; |
|---|
| 361 | default: |
|---|
| 362 | COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Returning 0.0!" << std::endl; |
|---|
| 363 | return 0.0; |
|---|
| 364 | } |
|---|
| [6184] | 365 | } |
|---|
| 366 | |
|---|
| [6186] | 367 | float SoundManager::getVolume(SoundType::Value type) |
|---|
| [6184] | 368 | { |
|---|
| [6186] | 369 | if(this->mute_[SoundType::none] || this->mute_[type]) |
|---|
| 370 | return 0.0; |
|---|
| 371 | |
|---|
| 372 | if(type == SoundType::none) |
|---|
| 373 | return this->getVolumeInternal(type); |
|---|
| 374 | |
|---|
| 375 | return this->getVolumeInternal(SoundType::none)*this->getVolumeInternal(type); |
|---|
| [6184] | 376 | } |
|---|
| 377 | |
|---|
| [6186] | 378 | void SoundManager::toggleMute(SoundType::Value type) |
|---|
| [6184] | 379 | { |
|---|
| [6186] | 380 | bool mute = !this->mute_[type]; |
|---|
| 381 | this->mute_[type] = mute; |
|---|
| 382 | |
|---|
| 383 | this->updateVolume(type); |
|---|
| [6184] | 384 | } |
|---|
| [6186] | 385 | |
|---|
| 386 | bool SoundManager::getMute(SoundType::Value type) |
|---|
| 387 | { |
|---|
| 388 | return this->mute_[type]; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| [6117] | 391 | |
|---|
| 392 | void SoundManager::fadeIn(AmbientSound* sound) |
|---|
| 393 | { |
|---|
| 394 | // If we're already fading out --> remove that |
|---|
| 395 | for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++) |
|---|
| 396 | { |
|---|
| 397 | if (*it == sound) |
|---|
| 398 | { |
|---|
| 399 | this->fadeOutList_.erase(it); |
|---|
| 400 | break; |
|---|
| 401 | } |
|---|
| 402 | } |
|---|
| 403 | // No duplicate entries |
|---|
| 404 | if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end()) |
|---|
| 405 | this->fadeInList_.push_back(sound); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | void SoundManager::fadeOut(AmbientSound* sound) |
|---|
| 409 | { |
|---|
| 410 | // If we're already fading in --> remove that |
|---|
| 411 | for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++) |
|---|
| 412 | { |
|---|
| 413 | if (*it == sound) |
|---|
| 414 | { |
|---|
| 415 | this->fadeInList_.erase(it); |
|---|
| 416 | break; |
|---|
| 417 | } |
|---|
| 418 | } |
|---|
| 419 | // No duplicate entries |
|---|
| 420 | if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end()) |
|---|
| 421 | this->fadeOutList_.push_back(sound); |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | void SoundManager::processCrossFading(float dt) |
|---|
| 425 | { |
|---|
| 426 | |
|---|
| 427 | // Hacky solution to the fade delay while loading a level. |
|---|
| 428 | if(dt > 0.2) |
|---|
| 429 | { |
|---|
| 430 | return; |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | // FADE IN |
|---|
| [6138] | 434 | for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); ) |
|---|
| [6117] | 435 | { |
|---|
| 436 | if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f) |
|---|
| 437 | { |
|---|
| 438 | (*it)->setVolume(1.0f); |
|---|
| 439 | this->fadeInList_.erase(it++); |
|---|
| 440 | } |
|---|
| 441 | else |
|---|
| 442 | { |
|---|
| 443 | (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt); |
|---|
| 444 | ++it; |
|---|
| 445 | } |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | // FADE OUT |
|---|
| [6138] | 449 | for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); ) |
|---|
| [6117] | 450 | { |
|---|
| 451 | if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f) |
|---|
| 452 | { |
|---|
| 453 | (*it)->setVolume(0.0f); |
|---|
| 454 | |
|---|
| 455 | // If sound is in the ambient list --> pause |
|---|
| 456 | for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2) |
|---|
| 457 | { |
|---|
| 458 | if (it2->first == *it) |
|---|
| 459 | { |
|---|
| 460 | (*it)->doPause(); |
|---|
| 461 | break; |
|---|
| 462 | } |
|---|
| 463 | } |
|---|
| 464 | // If not pause (by loop above for instance) --> stop |
|---|
| 465 | if (!(*it)->isPaused()) |
|---|
| 466 | (*it)->doStop(); |
|---|
| 467 | |
|---|
| 468 | this->fadeOutList_.erase(it++); |
|---|
| 469 | } |
|---|
| 470 | else |
|---|
| 471 | { |
|---|
| 472 | (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt); |
|---|
| 473 | ++it; |
|---|
| 474 | } |
|---|
| 475 | } |
|---|
| 476 | } |
|---|
| [6203] | 477 | |
|---|
| 478 | shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(shared_ptr<ResourceInfo> fileInfo) |
|---|
| 479 | { |
|---|
| 480 | std::map<std::string, weak_ptr<SoundBuffer> >::const_iterator it |
|---|
| 481 | = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename); |
|---|
| 482 | if (it != this->soundBuffers_.end()) |
|---|
| 483 | return it->second.lock(); |
|---|
| 484 | else |
|---|
| 485 | { |
|---|
| 486 | shared_ptr<SoundBuffer> buffer(new SoundBuffer(fileInfo)); |
|---|
| 487 | this->soundBuffers_[fileInfo->group + '/' + fileInfo->filename] = buffer; |
|---|
| 488 | return buffer; |
|---|
| 489 | } |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | void SoundManager::removeBuffer(shared_ptr<ResourceInfo> fileInfo) |
|---|
| 493 | { |
|---|
| [6208] | 494 | std::map<std::string, weak_ptr<SoundBuffer> >::iterator it |
|---|
| [6203] | 495 | = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename); |
|---|
| 496 | if (it == this->soundBuffers_.end()) |
|---|
| 497 | this->soundBuffers_.erase(it); |
|---|
| 498 | } |
|---|
| [3060] | 499 | } |
|---|