- Timestamp:
- Nov 22, 2009, 4:01:16 PM (15 years ago)
- Location:
- code/branches/presentation2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation2
- Property svn:mergeinfo changed
/code/branches/sound3 (added) merged: 5941,5943,5946,5954,5956-5957,5962,5982,6031,6046,6069-6072,6074,6088,6093,6097,6100,6102
- Property svn:mergeinfo changed
-
code/branches/presentation2/src/orxonox/sound/SoundManager.cc
r5929 r6117 22 22 * Author: 23 23 * Erwin 'vaiursch' Herrsche 24 * Kevin Young 24 25 * Co-authors: 25 26 * ... … … 30 31 31 32 #include <AL/alut.h> 33 #include <utility> 32 34 33 35 #include "util/Exception.h" 34 36 #include "util/Math.h" 35 37 #include "util/ScopeGuard.h" 38 #include "util/StringUtils.h" 39 #include "util/Clock.h" 36 40 #include "core/GameMode.h" 37 41 #include "core/ScopedSingletonManager.h" 42 #include "core/ConfigValueIncludes.h" 43 #include "BaseSound.h" 44 #include "AmbientSound.h" 38 45 39 46 namespace orxonox … … 44 51 SoundManager::SoundManager() 45 52 { 46 if (!alutInitWithoutContext(NULL,NULL)) 47 ThrowException(InitialisationFailed, "OpenAL ALUT error: " << alutGetErrorString(alutGetError())); 53 RegisterRootObject(SoundManager); 54 55 if (!alutInitWithoutContext(NULL, NULL)) 56 ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError())); 48 57 Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); 49 58 50 COUT(3) << " OpenAL: Opening sound device..." << std::endl;59 COUT(3) << "Sound: OpenAL: Opening sound device..." << std::endl; 51 60 this->device_ = alcOpenDevice(NULL); 52 61 if (this->device_ == NULL) 53 62 { 54 COUT(0) << " OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl;63 COUT(0) << "Sound: OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl; 55 64 #ifdef ORXONOX_PLATFORM_WINDOWS 56 COUT(0) << " Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl;65 COUT(0) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; 57 66 #endif 58 ThrowException(InitialisationFailed, " OpenAL error: Could not open sound device.");67 ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device."); 59 68 } 60 69 Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); 61 70 62 COUT(3) << " OpenAL: Sound device opened" << std::endl;71 COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl; 63 72 this->context_ = alcCreateContext(this->device_, NULL); 64 73 if (this->context_ == NULL) 65 ThrowException(InitialisationFailed, " OpenAL error: Could not create sound context");74 ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not create sound context"); 66 75 Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_); 67 76 68 77 if (alcMakeContextCurrent(this->context_) == AL_TRUE) 69 COUT(3) << " OpenAL: Context " << this->context_ << " loaded" << std::endl;78 COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl; 70 79 71 80 COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl; … … 73 82 const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER); 74 83 if (str == NULL) 75 COUT(2) << " OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl;84 COUT(2) << "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl; 76 85 else 77 COUT(4) << " OpenAL ALUT supported MIME types: " << str << std::endl;86 COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl; 78 87 79 88 GameMode::setPlaysSound(true); … … 82 91 closeDeviceGuard.Dismiss(); 83 92 desroyContextGuard.Dismiss(); 93 94 this->setConfigValues(); 84 95 } 85 96 … … 92 103 } 93 104 105 void SoundManager::update(const Clock& time) 106 { 107 this->processCrossFading(time.getDeltaTime()); 108 } 109 110 void SoundManager::setConfigValues() 111 { 112 SetConfigValue(crossFadeStep_, 0.2f) 113 .description("Determines how fast sounds should fade, per second.") 114 .callback(this, &SoundManager::checkFadeStepValidity); 115 } 116 117 void SoundManager::checkFadeStepValidity() 118 { 119 if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 ) 120 { 121 COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl; 122 ResetConfigValue(crossFadeStep_); 123 } 124 COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl; 125 return; 126 } 127 94 128 void SoundManager::setListenerPosition(const Vector3& position) 95 129 { … … 114 148 COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; 115 149 } 150 151 void SoundManager::registerAmbientSound(AmbientSound* newAmbient) 152 { 153 if (newAmbient != NULL) 154 { 155 for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) 156 { 157 if (it->first == newAmbient) 158 { 159 COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl; 160 return; 161 } 162 } 163 164 if (!this->ambientSounds_.empty()) 165 { 166 this->fadeOut(ambientSounds_.front().first); 167 } 168 this->ambientSounds_.push_front(std::make_pair(newAmbient, false)); 169 newAmbient->doPlay(); 170 this->fadeIn(newAmbient); 171 } 172 } 173 174 void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient) 175 { 176 if (oldAmbient == NULL || ambientSounds_.empty()) 177 { 178 return; 179 } 180 if (this->ambientSounds_.front().first == oldAmbient) 181 { 182 this->fadeOut(oldAmbient); 183 this->ambientSounds_.pop_front(); 184 if (!this->ambientSounds_.empty()) 185 { 186 if (!this->ambientSounds_.front().second) // Not paused before 187 { 188 this->ambientSounds_.front().first->doPlay(); 189 } 190 this->fadeIn(this->ambientSounds_.front().first); 191 } 192 } 193 else 194 { 195 for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) 196 { 197 if (it->first == oldAmbient) 198 { 199 this->fadeOut(oldAmbient); 200 this->ambientSounds_.erase(it); 201 break; 202 } 203 } 204 } 205 } 206 207 void SoundManager::pauseAmbientSound(AmbientSound* ambient) 208 { 209 if (ambient != NULL) 210 { 211 for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) 212 { 213 if (it->first == ambient) 214 { 215 it->second = true; 216 this->fadeOut(it->first); 217 return; 218 } 219 } 220 } 221 } 222 223 void SoundManager::fadeIn(AmbientSound* sound) 224 { 225 // If we're already fading out --> remove that 226 for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++) 227 { 228 if (*it == sound) 229 { 230 this->fadeOutList_.erase(it); 231 break; 232 } 233 } 234 // No duplicate entries 235 if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end()) 236 this->fadeInList_.push_back(sound); 237 } 238 239 void SoundManager::fadeOut(AmbientSound* sound) 240 { 241 // If we're already fading in --> remove that 242 for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++) 243 { 244 if (*it == sound) 245 { 246 this->fadeInList_.erase(it); 247 break; 248 } 249 } 250 // No duplicate entries 251 if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end()) 252 this->fadeOutList_.push_back(sound); 253 } 254 255 void SoundManager::processCrossFading(float dt) 256 { 257 258 // Hacky solution to the fade delay while loading a level. 259 if(dt > 0.2) 260 { 261 return; 262 } 263 264 // FADE IN 265 for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); it) 266 { 267 if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f) 268 { 269 (*it)->setVolume(1.0f); 270 this->fadeInList_.erase(it++); 271 } 272 else 273 { 274 (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt); 275 ++it; 276 } 277 } 278 279 // FADE OUT 280 for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it) 281 { 282 if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f) 283 { 284 (*it)->setVolume(0.0f); 285 286 // If sound is in the ambient list --> pause 287 for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2) 288 { 289 if (it2->first == *it) 290 { 291 (*it)->doPause(); 292 break; 293 } 294 } 295 // If not pause (by loop above for instance) --> stop 296 if (!(*it)->isPaused()) 297 (*it)->doStop(); 298 299 this->fadeOutList_.erase(it++); 300 } 301 else 302 { 303 (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt); 304 ++it; 305 } 306 } 307 } 116 308 }
Note: See TracChangeset
for help on using the changeset viewer.