Changeset 6069 for code/branches/sound3/src/orxonox/sound/SoundManager.cc
- Timestamp:
- Nov 15, 2009, 3:43:06 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/sound3/src/orxonox/sound/SoundManager.cc
r6046 r6069 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" … … 53 55 RegisterRootObject(SoundManager); 54 56 55 if (!alutInitWithoutContext(NULL, NULL))57 if (!alutInitWithoutContext(NULL, NULL)) 56 58 ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError())); 57 59 Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); … … 103 105 } 104 106 105 void SoundManager::update(const Clock &time) 106 { 107 this->fadeInAmbientSound(time.getDeltaTime()); 108 this->fadeOutAmbientSound(time.getDeltaTime()); 107 void SoundManager::update(const Clock& time) 108 { 109 this->processCrossFading(time.getDeltaTime()); 109 110 } 110 111 111 112 void SoundManager::setConfigValues() 112 113 { 113 SetConfigValue( fadeStep_, 0.2f)114 SetConfigValue(crossFadeStep_, 0.2f) 114 115 .description("Determines how fast sounds should fade, per second.") 115 116 .callback(this, &SoundManager::checkFadeStepValidity); 117 } 118 119 void SoundManager::checkFadeStepValidity() 120 { 121 if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 ) 122 { 123 COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl; 124 ResetConfigValue(crossFadeStep_); 125 } 126 COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl; 127 return; 116 128 } 117 129 … … 141 153 void SoundManager::registerAmbientSound(AmbientSound* newAmbient) 142 154 { 143 if(newAmbient != NULL) 144 { 145 if (!(this->ambientSounds_.empty())) 146 { 147 this->fadeOutList_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0)); 148 } 149 this->fadeInList_.push_front(std::make_pair(newAmbient, 0.0)); 150 this->ambientSounds_.push_front(newAmbient); 151 } 152 } 153 154 void SoundManager::unregisterAmbientSound(AmbientSound* currentAmbient) 155 { 156 if(currentAmbient == NULL || ambientSounds_.empty()) 155 if (newAmbient != NULL) 156 { 157 for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) 158 { 159 if (it->first == newAmbient) 160 { 161 COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl; 162 return; 163 } 164 } 165 166 if (!this->ambientSounds_.empty()) 167 { 168 this->fadeOut(ambientSounds_.front().first); 169 } 170 this->ambientSounds_.push_front(std::make_pair(newAmbient, false)); 171 newAmbient->doPlay(); 172 this->fadeIn(newAmbient); 173 } 174 } 175 176 void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient) 177 { 178 if (oldAmbient == NULL || ambientSounds_.empty()) 157 179 { 158 180 return; 159 181 } 160 if (this->ambientSounds_.front() == currentAmbient)161 { 162 this->fadeOut List_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0));182 if (this->ambientSounds_.front().first == oldAmbient) 183 { 184 this->fadeOut(oldAmbient); 163 185 this->ambientSounds_.pop_front(); 164 if(!(this->ambientSounds_.empty())) 165 { 166 this->fadeInList_.push_front(std::make_pair(this->ambientSounds_.front(), 0.0)); 186 if (!this->ambientSounds_.empty()) 187 { 188 if (!this->ambientSounds_.front().second) // Not paused before 189 { 190 this->ambientSounds_.front().first->doPlay(); 191 } 192 this->fadeIn(this->ambientSounds_.front().first); 167 193 } 168 194 } 169 195 else 170 196 { 171 for (std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++)172 { 173 if (*it == currentAmbient)174 { 175 currentAmbient->doStop();197 for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) 198 { 199 if (it->first == oldAmbient) 200 { 201 this->fadeOut(oldAmbient); 176 202 this->ambientSounds_.erase(it); 177 203 break; … … 181 207 } 182 208 183 // Get the current mood and return the full path string to the requested sound. 184 const std::string& SoundManager::getAmbientPath(const std::string& source) 185 { 186 lastReqPath_ = "ambient/" + MoodManager::getInstance().getMood() + "/" + source; 187 shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(lastReqPath_); 188 if(fileInfo == NULL) 189 { 190 return BLANKSTRING; 191 } 192 return lastReqPath_; 193 } 194 195 void SoundManager::fadeInAmbientSound(float dt) 196 { 197 if(!(this->fadeInList_.empty())) 198 { 199 for(std::list<std::pair<AmbientSound*, float> >::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); it++) 200 { 201 it->second += fadeStep_ * dt; 202 alSourcef(it->first->getALAudioSource(), AL_GAIN, it->second); 203 } 204 if(this->fadeInList_.back().second >= 1) 205 { 206 this->fadeInList_.pop_back(); 207 } 208 } 209 } 210 211 void SoundManager::fadeOutAmbientSound(float dt) 212 { 213 if(!(this->fadeInList_.empty())) 214 { 215 for(std::list<std::pair<AmbientSound*, float> >::iterator it= this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++) 216 { 217 it->second -= fadeStep_ * dt; 218 alSourcef(it->first->getALAudioSource(), AL_GAIN, it->second); 219 } 220 if(this->fadeOutList_.back().second <= 0) 221 { 222 bool pauseTest = false; 223 224 for(std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++) 225 { 226 if(*it == this->fadeOutList_.back().first) 209 void SoundManager::pauseAmbientSound(AmbientSound* ambient) 210 { 211 if (ambient != NULL) 212 { 213 for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) 214 { 215 if (it->first == ambient) 216 { 217 it->second = true; 218 this->fadeOut(it->first); 219 return; 220 } 221 } 222 } 223 } 224 225 //! Get the current mood and return the full path string to the requested sound. 226 std::string SoundManager::getAmbientPath(const std::string& source) 227 { 228 std::string path = "ambient/" + MoodManager::getInstance().getMood() + "/" + source; 229 shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path); 230 if (fileInfo == NULL) 231 { 232 return ""; 233 } 234 return path; 235 } 236 237 void SoundManager::fadeIn(AmbientSound* sound) 238 { 239 // If we're already fading out --> remove that 240 for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++) 241 { 242 if (*it == sound) 243 { 244 this->fadeOutList_.erase(it); 245 break; 246 } 247 } 248 // No duplicate entries 249 if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end()) 250 this->fadeInList_.push_back(sound); 251 } 252 253 void SoundManager::fadeOut(AmbientSound* sound) 254 { 255 // If we're already fading in --> remove that 256 for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++) 257 { 258 if (*it == sound) 259 { 260 this->fadeInList_.erase(it); 261 break; 262 } 263 } 264 // No duplicate entries 265 if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end()) 266 this->fadeOutList_.push_back(sound); 267 } 268 269 void SoundManager::processCrossFading(float dt) 270 { 271 // FADE IN 272 for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); it) 273 { 274 if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f) 275 { 276 (*it)->setVolume(1.0f); 277 this->fadeInList_.erase(it++); 278 } 279 else 280 { 281 (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt); 282 ++it; 283 } 284 } 285 286 // FADE OUT 287 for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it) 288 { 289 if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f) 290 { 291 (*it)->setVolume(0.0f); 292 293 // If sound is in the ambient list --> pause 294 for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2) 295 { 296 if (it2->first == *it) 227 297 { 228 pauseTest = true;298 (*it)->doPause(); 229 299 break; 230 300 } 231 301 } 232 if(pauseTest) 233 { 234 this->fadeOutList_.back().first->pause(); 235 } 236 else 237 { 238 this->fadeOutList_.back().first->doStop(); 239 } 240 this->fadeOutList_.pop_back(); 241 } 242 } 243 } 244 245 void SoundManager::checkFadeStepValidity() 246 { 247 if(fadeStep_ <= 0.0 || fadeStep_ >= 1.0 ) 248 { 249 ResetConfigValue(fadeStep_); 250 } 251 COUT(0) << "SoundManager: fade step now set to " << fadeStep_ << std::endl; 252 return; 302 // If not pause (by loop above for instance) --> stop 303 if (!(*it)->isPaused()) 304 (*it)->doStop(); 305 306 this->fadeOutList_.erase(it++); 307 } 308 else 309 { 310 (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt); 311 ++it; 312 } 313 } 253 314 } 254 315 }
Note: See TracChangeset
for help on using the changeset viewer.