[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 | |
---|
[6370] | 53 | std::string SoundManager::getALErrorString(ALenum code) |
---|
| 54 | { |
---|
| 55 | switch (code) |
---|
| 56 | { |
---|
| 57 | case AL_NO_ERROR: return "No error"; |
---|
| 58 | case AL_INVALID_NAME: return "Invalid AL parameter name"; |
---|
| 59 | case AL_INVALID_ENUM: return "Invalid AL enum"; |
---|
| 60 | case AL_INVALID_VALUE: return "Invalid AL value"; |
---|
| 61 | case AL_INVALID_OPERATION: return "Invalid AL operation"; |
---|
| 62 | case AL_OUT_OF_MEMORY: return "AL reports out of memory"; |
---|
| 63 | default: return "Unknown AL error"; |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
[3060] | 67 | SoundManager::SoundManager() |
---|
[6254] | 68 | : effectsPoolSize_(0) |
---|
[3060] | 69 | { |
---|
[6117] | 70 | RegisterRootObject(SoundManager); |
---|
| 71 | |
---|
| 72 | if (!alutInitWithoutContext(NULL, NULL)) |
---|
[6244] | 73 | ThrowException(InitialisationFailed, "Sound Error: ALUT initialisation failed: " << alutGetErrorString(alutGetError())); |
---|
[5929] | 74 | Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); |
---|
| 75 | |
---|
[6332] | 76 | /* |
---|
[6244] | 77 | // Get list of available sound devices and display them |
---|
[6332] | 78 | const char* devices = alcGetString(NULL, ALC_DEVICE_SPECIFIER); |
---|
[6298] | 79 | char* device = new char[strlen(devices)+1]; |
---|
| 80 | strcpy(device, devices); |
---|
[6244] | 81 | std::string renderDevice; |
---|
[6332] | 82 | SetConfigValue(renderDevice, std::string(device)).description("Sound device used for rendering"); |
---|
[6244] | 83 | COUT(4) << "Sound: Available devices: "; |
---|
| 84 | while (true) |
---|
| 85 | { |
---|
| 86 | this->deviceNames_.push_back(devices); |
---|
| 87 | COUT(4) << "\"" << devices << "\", "; |
---|
| 88 | devices += strlen(devices) + 1; |
---|
| 89 | if (*devices == '\0') |
---|
| 90 | break; |
---|
| 91 | } |
---|
| 92 | COUT(4) << std::endl; |
---|
| 93 | |
---|
| 94 | // Open the selected device |
---|
| 95 | COUT(3) << "Sound: Opening device \"" << renderDevice << "\"" << std::endl; |
---|
[6332] | 96 | this->device_ = alcOpenDevice(renderDevice.c_str()); |
---|
| 97 | */ |
---|
[6298] | 98 | this->device_ = alcOpenDevice(NULL); |
---|
[5929] | 99 | if (this->device_ == NULL) |
---|
[3060] | 100 | { |
---|
[6244] | 101 | COUT(1) << "Sound: Could not open sound device. Have you installed OpenAL?" << std::endl; |
---|
[5929] | 102 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[6244] | 103 | COUT(1) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; |
---|
[5929] | 104 | #endif |
---|
[6322] | 105 | ThrowException(InitialisationFailed, "Sound Error: Could not open sound device."); |
---|
[3060] | 106 | } |
---|
[5929] | 107 | Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); |
---|
| 108 | |
---|
[6244] | 109 | // Create sound context and make it the currently used one |
---|
[5929] | 110 | this->context_ = alcCreateContext(this->device_, NULL); |
---|
| 111 | if (this->context_ == NULL) |
---|
[6244] | 112 | ThrowException(InitialisationFailed, "Sound Error: Could not create ALC context"); |
---|
[5929] | 113 | Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_); |
---|
[6244] | 114 | if (!alcMakeContextCurrent(this->context_)) |
---|
| 115 | ThrowException(InitialisationFailed, "Sound Error: Could not use ALC context"); |
---|
[5929] | 116 | |
---|
[6244] | 117 | GameMode::setPlaysSound(true); |
---|
[6322] | 118 | Loki::ScopeGuard resetPlaysSoundGuard = Loki::MakeGuard(&GameMode::setPlaysSound, false); |
---|
[5929] | 119 | |
---|
[6244] | 120 | // Get some information about the sound |
---|
| 121 | if (const char* version = alGetString(AL_VERSION)) |
---|
| 122 | COUT(4) << "Sound: --- OpenAL Version: " << version << std::endl; |
---|
| 123 | if (const char* vendor = alGetString(AL_VENDOR)) |
---|
| 124 | COUT(4) << "Sound: --- OpenAL Vendor : " << vendor << std::endl; |
---|
| 125 | if (const char* types = alutGetMIMETypes(ALUT_LOADER_BUFFER)) |
---|
| 126 | COUT(4) << "Sound: --- Supported MIME Types: " << types << std::endl; |
---|
[3060] | 127 | else |
---|
[6244] | 128 | COUT(2) << "Sound Warning: MIME Type retrieval failed: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
[6184] | 129 | |
---|
[6370] | 130 | this->mute_[SoundType::All] = 1.0f; |
---|
| 131 | this->mute_[SoundType::Music] = 1.0f; |
---|
| 132 | this->mute_[SoundType::Effects] = 1.0f; |
---|
[6117] | 133 | |
---|
| 134 | this->setConfigValues(); |
---|
[6244] | 135 | |
---|
[6322] | 136 | // Try to get at least one source |
---|
| 137 | ALuint source; |
---|
| 138 | alGenSources(1, &source); |
---|
| 139 | if (!alGetError() && alIsSource(source)) |
---|
| 140 | this->soundSources_.push_back(source); |
---|
| 141 | else |
---|
| 142 | ThrowException(InitialisationFailed, "Sound Error: Could not even create a single source"); |
---|
| 143 | // Get the rest of the sources |
---|
| 144 | alGenSources(1, &source); |
---|
| 145 | unsigned int count = 1; |
---|
| 146 | while (alIsSource(source) && !alGetError() && count <= this->maxSources_) |
---|
| 147 | { |
---|
| 148 | this->soundSources_.push_back(source); |
---|
| 149 | alGenSources(1, &source); |
---|
| 150 | ++count; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | // Disarm guards |
---|
| 154 | alutExitGuard.Dismiss(); |
---|
| 155 | closeDeviceGuard.Dismiss(); |
---|
| 156 | desroyContextGuard.Dismiss(); |
---|
| 157 | resetPlaysSoundGuard.Dismiss(); |
---|
| 158 | |
---|
[6244] | 159 | COUT(4) << "Sound: Initialisation complete" << std::endl; |
---|
[3060] | 160 | } |
---|
| 161 | |
---|
| 162 | SoundManager::~SoundManager() |
---|
| 163 | { |
---|
[5929] | 164 | GameMode::setPlaysSound(false); |
---|
[6244] | 165 | |
---|
| 166 | // Relieve context to destroy it |
---|
| 167 | if (!alcMakeContextCurrent(NULL)) |
---|
| 168 | COUT(1) << "Sound Error: Could not unset ALC context" << std::endl; |
---|
[3060] | 169 | alcDestroyContext(this->context_); |
---|
[6244] | 170 | if (ALCenum error = alcGetError(this->device_)) |
---|
| 171 | { |
---|
| 172 | if (error == AL_INVALID_OPERATION) |
---|
| 173 | COUT(1) << "Sound Error: Could not destroy ALC context because it is the current one" << std::endl; |
---|
| 174 | else |
---|
| 175 | COUT(1) << "Sound Error: Could not destroy ALC context because it is invalid" << std::endl; |
---|
| 176 | } |
---|
| 177 | #ifdef AL_VERSION_1_1 |
---|
| 178 | if (!alcCloseDevice(this->device_)) |
---|
| 179 | COUT(1) << "Sound Error: Could not destroy ALC device. This might be because there are still buffers in use!" << std::endl; |
---|
| 180 | #else |
---|
[3280] | 181 | alcCloseDevice(this->device_); |
---|
[6244] | 182 | #endif |
---|
| 183 | if (!alutExit()) |
---|
| 184 | COUT(1) << "Sound Error: Closing ALUT failed: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
[3060] | 185 | } |
---|
| 186 | |
---|
[6183] | 187 | void SoundManager::preUpdate(const Clock& time) |
---|
[6117] | 188 | { |
---|
| 189 | this->processCrossFading(time.getDeltaTime()); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | void SoundManager::setConfigValues() |
---|
| 193 | { |
---|
| 194 | SetConfigValue(crossFadeStep_, 0.2f) |
---|
| 195 | .description("Determines how fast sounds should fade, per second.") |
---|
| 196 | .callback(this, &SoundManager::checkFadeStepValidity); |
---|
[6322] | 197 | |
---|
[6370] | 198 | SetConfigValueAlias(volume_[SoundType::All], "soundVolume_", 1.0f) |
---|
[6184] | 199 | .description("Defines the overall volume.") |
---|
[6186] | 200 | .callback(this, &SoundManager::checkSoundVolumeValidity); |
---|
[6370] | 201 | SetConfigValueAlias(volume_[SoundType::Music], "ambientVolume_", 1.0f) |
---|
[6184] | 202 | .description("Defines the ambient volume.") |
---|
| 203 | .callback(this, &SoundManager::checkAmbientVolumeValidity); |
---|
[6370] | 204 | SetConfigValueAlias(volume_[SoundType::Effects], "effectsVolume_", 1.0f) |
---|
[6184] | 205 | .description("Defines the effects volume.") |
---|
| 206 | .callback(this, &SoundManager::checkEffectsVolumeValidity); |
---|
[6322] | 207 | |
---|
| 208 | SetConfigValue(maxSources_, 1024) |
---|
| 209 | .description("Maximum number of sources to be made available"); |
---|
[6117] | 210 | } |
---|
| 211 | |
---|
| 212 | void SoundManager::checkFadeStepValidity() |
---|
| 213 | { |
---|
| 214 | if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 ) |
---|
| 215 | { |
---|
[6370] | 216 | COUT(2) << "Sound warning: fade step out of range, ignoring change." << std::endl; |
---|
[6117] | 217 | ResetConfigValue(crossFadeStep_); |
---|
| 218 | } |
---|
| 219 | } |
---|
[6370] | 220 | |
---|
| 221 | void SoundManager::checkVolumeValidity(SoundType::Value type) |
---|
[6184] | 222 | { |
---|
[6370] | 223 | float clampedVolume = clamp(this->volume_[type], 0.0f, 1.0f); |
---|
| 224 | if (clampedVolume != this->volume_[type]) |
---|
| 225 | COUT(2) << "Sound warning: Volume setting (" << type << ") out of range, clamping." << std::endl; |
---|
[6186] | 226 | this->updateVolume(type); |
---|
[6184] | 227 | } |
---|
[6370] | 228 | |
---|
| 229 | void SoundManager::setVolume(float vol, SoundType::Value type) |
---|
[6186] | 230 | { |
---|
[6370] | 231 | if (type < 0 || type > SoundType::Effects) |
---|
| 232 | return; |
---|
| 233 | this->volume_[type] = vol; |
---|
| 234 | this->checkVolumeValidity(type); |
---|
[6186] | 235 | } |
---|
[6370] | 236 | |
---|
| 237 | float SoundManager::getVolume(SoundType::Value type) |
---|
[6184] | 238 | { |
---|
[6370] | 239 | if (type < 0 || type > SoundType::Effects) |
---|
| 240 | return 0.0f; |
---|
| 241 | return this->volume_[type]; |
---|
[6184] | 242 | } |
---|
[6370] | 243 | |
---|
| 244 | float SoundManager::getRealVolume(SoundType::Value type) |
---|
[6184] | 245 | { |
---|
[6370] | 246 | if (type != SoundType::Music && type != SoundType::Effects) |
---|
| 247 | return 0.0f; |
---|
| 248 | return this->volume_[SoundType::All] * this->mute_[SoundType::All] * this->volume_[type] * this->mute_[type]; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | void SoundManager::updateVolume(SoundType::Value type) |
---|
| 252 | { |
---|
| 253 | switch(type) |
---|
[6184] | 254 | { |
---|
[6370] | 255 | case SoundType::All: |
---|
| 256 | for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it) |
---|
| 257 | (*it)->updateVolume(); |
---|
| 258 | break; |
---|
| 259 | case SoundType::Music: |
---|
| 260 | for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it) |
---|
| 261 | (*it)->updateVolume(); |
---|
| 262 | break; |
---|
| 263 | case SoundType::Effects: |
---|
| 264 | for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it) |
---|
| 265 | (*it)->updateVolume(); |
---|
| 266 | break; |
---|
| 267 | default: |
---|
| 268 | assert(false); |
---|
[6184] | 269 | } |
---|
| 270 | } |
---|
[6117] | 271 | |
---|
[6370] | 272 | void SoundManager::toggleMute(SoundType::Value type) |
---|
| 273 | { |
---|
| 274 | if (type < 0 || type > SoundType::Effects) |
---|
| 275 | return; |
---|
| 276 | this->mute_[type] = (this->mute_[type] == 0) ? 1.0f : 0.0f; |
---|
| 277 | this->updateVolume(type); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | bool SoundManager::getMute(SoundType::Value type) |
---|
| 281 | { |
---|
| 282 | if (type < 0 || type > SoundType::Effects) |
---|
| 283 | return true; |
---|
| 284 | return (this->mute_[type] == 0); |
---|
| 285 | } |
---|
| 286 | |
---|
[5929] | 287 | void SoundManager::setListenerPosition(const Vector3& position) |
---|
[3060] | 288 | { |
---|
[5929] | 289 | alListener3f(AL_POSITION, position.x, position.y, position.z); |
---|
| 290 | ALenum error = alGetError(); |
---|
| 291 | if (error == AL_INVALID_VALUE) |
---|
| 292 | COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl; |
---|
[3060] | 293 | } |
---|
| 294 | |
---|
[5929] | 295 | void SoundManager::setListenerOrientation(const Quaternion& orientation) |
---|
[3060] | 296 | { |
---|
[5929] | 297 | // update listener orientation |
---|
[6269] | 298 | const Vector3& direction = -orientation.zAxis(); |
---|
| 299 | const Vector3& up = orientation.yAxis(); |
---|
[3060] | 300 | |
---|
[6269] | 301 | ALfloat orient[6] = { direction.x, direction.y, direction.z, up.x, up.y, up.z }; |
---|
[3060] | 302 | |
---|
[6269] | 303 | alListenerfv(AL_ORIENTATION, orient); |
---|
[3060] | 304 | ALenum error = alGetError(); |
---|
[5929] | 305 | if (error == AL_INVALID_VALUE) |
---|
[3060] | 306 | COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; |
---|
| 307 | } |
---|
[6117] | 308 | |
---|
| 309 | void SoundManager::registerAmbientSound(AmbientSound* newAmbient) |
---|
| 310 | { |
---|
| 311 | if (newAmbient != NULL) |
---|
| 312 | { |
---|
| 313 | for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
---|
| 314 | { |
---|
| 315 | if (it->first == newAmbient) |
---|
| 316 | { |
---|
| 317 | COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl; |
---|
| 318 | return; |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | if (!this->ambientSounds_.empty()) |
---|
| 323 | { |
---|
| 324 | this->fadeOut(ambientSounds_.front().first); |
---|
| 325 | } |
---|
| 326 | this->ambientSounds_.push_front(std::make_pair(newAmbient, false)); |
---|
| 327 | newAmbient->doPlay(); |
---|
| 328 | this->fadeIn(newAmbient); |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient) |
---|
| 333 | { |
---|
| 334 | if (oldAmbient == NULL || ambientSounds_.empty()) |
---|
| 335 | return; |
---|
[6349] | 336 | |
---|
[6117] | 337 | if (this->ambientSounds_.front().first == oldAmbient) |
---|
| 338 | { |
---|
| 339 | this->fadeOut(oldAmbient); |
---|
| 340 | this->ambientSounds_.pop_front(); |
---|
| 341 | if (!this->ambientSounds_.empty()) |
---|
| 342 | { |
---|
| 343 | if (!this->ambientSounds_.front().second) // Not paused before |
---|
| 344 | { |
---|
| 345 | this->ambientSounds_.front().first->doPlay(); |
---|
| 346 | } |
---|
| 347 | this->fadeIn(this->ambientSounds_.front().first); |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | else |
---|
| 351 | { |
---|
| 352 | for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
---|
| 353 | { |
---|
| 354 | if (it->first == oldAmbient) |
---|
| 355 | { |
---|
| 356 | this->fadeOut(oldAmbient); |
---|
| 357 | this->ambientSounds_.erase(it); |
---|
| 358 | break; |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | void SoundManager::pauseAmbientSound(AmbientSound* ambient) |
---|
| 365 | { |
---|
| 366 | if (ambient != NULL) |
---|
| 367 | { |
---|
| 368 | for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
---|
| 369 | { |
---|
| 370 | if (it->first == ambient) |
---|
| 371 | { |
---|
| 372 | it->second = true; |
---|
| 373 | this->fadeOut(it->first); |
---|
| 374 | return; |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | } |
---|
| 378 | } |
---|
| 379 | |
---|
[6349] | 380 | void SoundManager::fadeIn(const SmartPtr<AmbientSound>& sound) |
---|
[6117] | 381 | { |
---|
| 382 | // If we're already fading out --> remove that |
---|
[6349] | 383 | for (std::list<SmartPtr<AmbientSound> >::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++) |
---|
[6117] | 384 | { |
---|
| 385 | if (*it == sound) |
---|
| 386 | { |
---|
| 387 | this->fadeOutList_.erase(it); |
---|
| 388 | break; |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | // No duplicate entries |
---|
| 392 | if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end()) |
---|
| 393 | this->fadeInList_.push_back(sound); |
---|
| 394 | } |
---|
| 395 | |
---|
[6349] | 396 | void SoundManager::fadeOut(const SmartPtr<AmbientSound>& sound) |
---|
[6117] | 397 | { |
---|
| 398 | // If we're already fading in --> remove that |
---|
[6349] | 399 | for (std::list<SmartPtr<AmbientSound> >::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++) |
---|
[6117] | 400 | { |
---|
| 401 | if (*it == sound) |
---|
| 402 | { |
---|
| 403 | this->fadeInList_.erase(it); |
---|
| 404 | break; |
---|
| 405 | } |
---|
| 406 | } |
---|
| 407 | // No duplicate entries |
---|
| 408 | if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end()) |
---|
| 409 | this->fadeOutList_.push_back(sound); |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | void SoundManager::processCrossFading(float dt) |
---|
| 413 | { |
---|
| 414 | |
---|
| 415 | // Hacky solution to the fade delay while loading a level. |
---|
| 416 | if(dt > 0.2) |
---|
| 417 | { |
---|
| 418 | return; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | // FADE IN |
---|
[6349] | 422 | for (std::list<SmartPtr<AmbientSound> >::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); ) |
---|
[6117] | 423 | { |
---|
| 424 | if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f) |
---|
| 425 | { |
---|
| 426 | (*it)->setVolume(1.0f); |
---|
| 427 | this->fadeInList_.erase(it++); |
---|
| 428 | } |
---|
| 429 | else |
---|
| 430 | { |
---|
| 431 | (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt); |
---|
| 432 | ++it; |
---|
| 433 | } |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | // FADE OUT |
---|
[6349] | 437 | for (std::list<SmartPtr<AmbientSound> >::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); ) |
---|
[6117] | 438 | { |
---|
| 439 | if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f) |
---|
| 440 | { |
---|
| 441 | (*it)->setVolume(0.0f); |
---|
| 442 | |
---|
| 443 | // If sound is in the ambient list --> pause |
---|
| 444 | for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2) |
---|
| 445 | { |
---|
| 446 | if (it2->first == *it) |
---|
| 447 | { |
---|
| 448 | (*it)->doPause(); |
---|
| 449 | break; |
---|
| 450 | } |
---|
| 451 | } |
---|
| 452 | // If not pause (by loop above for instance) --> stop |
---|
| 453 | if (!(*it)->isPaused()) |
---|
| 454 | (*it)->doStop(); |
---|
| 455 | |
---|
| 456 | this->fadeOutList_.erase(it++); |
---|
| 457 | } |
---|
| 458 | else |
---|
| 459 | { |
---|
| 460 | (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt); |
---|
| 461 | ++it; |
---|
| 462 | } |
---|
| 463 | } |
---|
| 464 | } |
---|
[6203] | 465 | |
---|
[6270] | 466 | shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(const std::string& filename) |
---|
[6203] | 467 | { |
---|
[6254] | 468 | shared_ptr<SoundBuffer> buffer; |
---|
| 469 | // Check active or pooled buffers |
---|
[6270] | 470 | SoundBufferMap::const_iterator it = this->soundBuffers_.find(filename); |
---|
[6203] | 471 | if (it != this->soundBuffers_.end()) |
---|
[6254] | 472 | { |
---|
| 473 | buffer = it->second; |
---|
| 474 | |
---|
| 475 | // Remove from effects pool if not active used before |
---|
| 476 | if (buffer->poolIterator_ != this->effectsPool_.end()) |
---|
| 477 | { |
---|
| 478 | this->effectsPoolSize_ -= buffer->getSize(); |
---|
| 479 | this->effectsPool_.erase(buffer->poolIterator_); |
---|
| 480 | buffer->poolIterator_ = this->effectsPool_.end(); |
---|
| 481 | } |
---|
| 482 | } |
---|
[6203] | 483 | else |
---|
| 484 | { |
---|
[6232] | 485 | try |
---|
| 486 | { |
---|
[6332] | 487 | buffer.reset(new SoundBuffer(filename, this->effectsPool_.end())); |
---|
[6232] | 488 | } |
---|
| 489 | catch (...) |
---|
| 490 | { |
---|
| 491 | COUT(1) << Exception::handleMessage() << std::endl; |
---|
[6254] | 492 | return buffer; |
---|
[6232] | 493 | } |
---|
[6270] | 494 | this->soundBuffers_[filename] = buffer; |
---|
[6203] | 495 | } |
---|
[6254] | 496 | return buffer; |
---|
[6203] | 497 | } |
---|
| 498 | |
---|
[6254] | 499 | void SoundManager::releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer) |
---|
[6203] | 500 | { |
---|
[6254] | 501 | // Check if others are still using the buffer |
---|
| 502 | if (buffer.use_count() != 2) |
---|
| 503 | return; |
---|
[6270] | 504 | SoundBufferMap::iterator it = this->soundBuffers_.find(buffer->getFilename()); |
---|
[6232] | 505 | if (it != this->soundBuffers_.end()) |
---|
[6254] | 506 | { |
---|
| 507 | if (bPoolBuffer) |
---|
| 508 | { |
---|
| 509 | // Pool already too large? |
---|
| 510 | while (this->effectsPoolSize_ + it->second->getSize() > this->maxEffectsPoolSize_s && !this->effectsPool_.empty()) |
---|
| 511 | { |
---|
| 512 | shared_ptr<SoundBuffer> bufferDel = this->effectsPool_.back(); |
---|
| 513 | this->effectsPoolSize_ -= bufferDel->getSize(); |
---|
| 514 | bufferDel->poolIterator_ = this->effectsPool_.end(); |
---|
| 515 | this->effectsPool_.pop_back(); |
---|
| 516 | // Remove from buffer map too |
---|
[6270] | 517 | SoundBufferMap::iterator itDel = this->soundBuffers_.find(bufferDel->getFilename()); |
---|
[6254] | 518 | if (itDel != this->soundBuffers_.end()) |
---|
| 519 | this->soundBuffers_.erase(itDel); |
---|
| 520 | } |
---|
| 521 | // Put buffer into the pool |
---|
| 522 | this->effectsPoolSize_ += it->second->getSize(); |
---|
| 523 | this->effectsPool_.push_front(it->second); |
---|
| 524 | it->second->poolIterator_ = this->effectsPool_.begin(); |
---|
| 525 | } |
---|
| 526 | else |
---|
| 527 | this->soundBuffers_.erase(it); |
---|
| 528 | } |
---|
[6203] | 529 | } |
---|
[6322] | 530 | |
---|
| 531 | ALuint SoundManager::getSoundSource() |
---|
| 532 | { |
---|
| 533 | if (!this->soundSources_.empty()) |
---|
| 534 | { |
---|
| 535 | ALuint source = this->soundSources_.back(); |
---|
| 536 | this->soundSources_.pop_back(); |
---|
| 537 | return source; |
---|
| 538 | } |
---|
| 539 | else |
---|
| 540 | { |
---|
| 541 | // Return no source ID |
---|
| 542 | ALuint source = 123456789; |
---|
| 543 | while (alIsSource(++source)); |
---|
| 544 | return source; |
---|
| 545 | } |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | void SoundManager::releaseSoundSource(ALuint source) |
---|
| 549 | { |
---|
| 550 | #ifndef NDEBUG |
---|
| 551 | for (std::vector<ALuint>::const_iterator it = this->soundSources_.begin(); it != this->soundSources_.end(); ++it) |
---|
| 552 | assert((*it) != source); |
---|
| 553 | #endif |
---|
| 554 | this->soundSources_.push_back(source); |
---|
| 555 | } |
---|
[3060] | 556 | } |
---|