Changeset 6069 for code/branches/sound3/src
- Timestamp:
- Nov 15, 2009, 3:43:06 PM (15 years ago)
- Location:
- code/branches/sound3/src/orxonox/sound
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/sound3/src/orxonox/sound/AmbientSound.cc
r6046 r6069 31 31 #include "core/CoreIncludes.h" 32 32 #include "core/EventIncludes.h" 33 #include "core/GameMode.h" 33 34 #include "core/XMLPort.h" 34 35 #include "SoundManager.h" … … 42 43 { 43 44 RegisterObject(AmbientSound); 45 46 // Ambient sounds always fade in 47 this->setVolume(0); 44 48 } 45 49 … … 51 55 { 52 56 SUPER(AmbientSound, XMLPort, xmlelement, mode); 53 XMLPortParamExtern(AmbientSound, BaseSound, this, " source", setSource, getSource, xmlelement, mode);57 XMLPortParamExtern(AmbientSound, BaseSound, this, "volume", setVolume, getVolume, xmlelement, mode); 54 58 XMLPortParamExtern(AmbientSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode); 55 59 XMLPortParamExtern(AmbientSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode); 60 XMLPortParamExtern(AmbientSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode); 56 61 } 57 62 … … 64 69 void AmbientSound::play() 65 70 { 66 COUT(3) << this->getSource() << ": Playing" << std::endl; 67 if(GameMode::playsSound()) 71 if (GameMode::playsSound()) 68 72 { 73 COUT(3) << "Sound: " << this->getSource() << ": Playing" << std::endl; 69 74 SoundManager::getInstance().registerAmbientSound(this); 70 this->BaseSound::play();71 75 } 72 76 } 73 77 74 void AmbientSound:: replay()78 void AmbientSound::doPlay() 75 79 { 76 this->BaseSound::play();80 BaseSound::play(); 77 81 } 78 82 79 83 void AmbientSound::stop() 80 84 { 81 if (GameMode::playsSound())85 if (GameMode::playsSound()) 82 86 { 83 87 SoundManager::getInstance().unregisterAmbientSound(this); … … 87 91 void AmbientSound::doStop() 88 92 { 89 this->BaseSound::stop(); 93 BaseSound::stop(); 94 } 95 96 void AmbientSound::pause() 97 { 98 if (GameMode::playsSound()) 99 { 100 SoundManager::getInstance().pauseAmbientSound(this); 101 } 102 } 103 104 void AmbientSound::doPause() 105 { 106 BaseSound::pause(); 90 107 } 91 108 92 109 void AmbientSound::setSource(const std::string& source) 93 110 { 94 if (source.find('/') == std::string.npos &&GameMode::playsSound())111 if (GameMode::playsSound()) 95 112 { 96 113 std::string filePath = SoundManager::getInstance().getAmbientPath(source); 97 if (!(filePath.empty()))114 if (!filePath.empty()) 98 115 { 99 this->BaseSound::setSource(filePath);116 BaseSound::setSource(filePath); 100 117 return; 101 118 } 119 COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl; 102 120 } 103 COUT(3) << source << ": Not a valid name! Ambient sound will not change." << std::endl;104 121 } 105 122 106 123 void AmbientSound::changedActivity() 107 124 { 108 COUT(3) << this->getSource() << ": ChangedActivity: " << this->isActive() << std::endl;125 COUT(3) << "Sound: " << this->getSource() << ": ChangedActivity: " << this->isActive() << std::endl; 109 126 this->BaseObject::changedActivity(); 110 if (this->isActive())127 if (this->isActive()) 111 128 { 112 129 this->play(); -
code/branches/sound3/src/orxonox/sound/AmbientSound.h
r6046 r6069 22 22 * Author: 23 23 * Reto Grieder 24 * Kevin Young 24 25 * Co-authors: 25 26 * ... 26 27 * 27 28 */ 29 28 30 #ifndef _AmbientSound_H__ 29 31 #define _AmbientSound_H__ … … 43 45 class _OrxonoxExport AmbientSound : public BaseSound, public BaseObject 44 46 { 47 friend class SoundManager; 48 45 49 public: 46 50 AmbientSound(BaseObject* creator); 47 51 virtual ~AmbientSound(); 48 49 virtual void play();50 void replay(); // Continue playing without re-registering the sound51 virtual void stop();52 void doStop();53 54 virtual void setSource(const std::string& source);55 52 56 53 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); … … 58 55 virtual void changedActivity(); 59 56 57 virtual void play(); 58 virtual void stop(); 59 virtual void pause(); 60 61 virtual void setSource(const std::string& source); 60 62 61 63 private: 64 void doPlay(); // Continue playing without re-registering the sound 65 void doStop(); 66 void doPause(); 62 67 }; 63 68 } -
code/branches/sound3/src/orxonox/sound/BaseSound.cc
r6046 r6069 93 93 if (alIsSource(this->audioSource_)) 94 94 return getSourceState() == AL_PAUSED; 95 return true;95 return false; 96 96 } 97 97 … … 103 103 } 104 104 105 void BaseSound::setPlayOnLoad(bool val) 106 { 107 this->bPlayOnLoad_ = val; 108 if(val) 109 { 110 this->play(); 111 } 105 void BaseSound::setVolume(float vol) 106 { 107 if (vol > 1 || vol < 0) 108 { 109 COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl; 110 vol = vol > 1 ? 1 : vol; 111 vol = vol < 0 ? 0 : vol; 112 } 113 this->volume_ = vol; 114 if (alIsSource(this->audioSource_)) 115 alSourcef(this->audioSource_, AL_GAIN, vol); 112 116 } 113 117 … … 178 182 alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0); 179 183 184 this->setVolume(this->volume_); 185 180 186 if (this->bPlayOnLoad_) 181 187 this->play(); 182 }183 184 ALuint BaseSound::getALAudioSource()185 {186 return audioSource_;187 188 } 188 189 … … 275 276 return buffer; 276 277 } 277 278 } // namespace: orxonox 278 } -
code/branches/sound3/src/orxonox/sound/BaseSound.h
r6046 r6069 26 26 * 27 27 */ 28 28 29 #ifndef _BaseSound_H__ 29 30 #define _BaseSound_H__ … … 32 33 33 34 #include <string> 34 #include <OgreSharedPtr.h>35 35 #include <OgreDataStream.h> 36 36 #include "core/OrxonoxClass.h" … … 51 51 virtual void play(); 52 52 virtual void stop(); 53 v oid pause();53 virtual void pause(); 54 54 55 55 bool isPlaying(); … … 58 58 59 59 virtual void setSource(const std::string& source); 60 const std::string& getSource(){ return this->source_; }60 virtual const std::string& getSource() const { return this->source_; } 61 61 62 bool getPlayOnLoad() { return this->bPlayOnLoad_; }63 void setPlayOnLoad(bool val);62 void setVolume(float vol); 63 float getVolume() const { return this->volume_; } 64 64 65 bool getLoop() { return this->bLoop_; } 65 bool getPlayOnLoad() const { return this->bPlayOnLoad_; } 66 void setPlayOnLoad(bool val) { this->bPlayOnLoad_ = val; } 67 68 bool getLoop() const { return this->bLoop_; } 66 69 void setLoop(bool val) { this->bLoop_ = val; } 67 70 68 ALuint getALAudioSource(void);71 //ALuint getALAudioSource(void); 69 72 70 73 protected: … … 76 79 77 80 private: 78 std::string source_; 79 bool bPlayOnLoad_; 80 bool bLoop_; 81 DataStreamPtr dataStream_; 81 std::string source_; 82 float volume_; 83 bool bPlayOnLoad_; 84 bool bLoop_; 85 DataStreamPtr dataStream_; 82 86 }; 83 87 } -
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 } -
code/branches/sound3/src/orxonox/sound/SoundManager.h
r6046 r6069 22 22 * Author: 23 23 * Erwin 'vaiursch' Herrsche 24 * Kevin Young 24 25 * Co-authors: 25 26 * ... 26 27 */ 28 27 29 #ifndef _SoundManager_H__ 28 30 #define _SoundManager_H__ … … 30 32 #include "OrxonoxPrereqs.h" 31 33 32 #include <cassert>33 34 #include <list> 35 #include <string> 34 36 #include "util/Singleton.h" 35 #include "tools/interfaces/Tickable.h"36 37 37 38 namespace orxonox … … 45 46 { 46 47 friend class Singleton<SoundManager>; 48 47 49 public: 48 50 SoundManager(); 49 51 ~SoundManager(); 50 52 51 void update(const Clock &time);52 void setConfigValues( void);53 void update(const Clock& time); 54 void setConfigValues(); 53 55 54 56 void setListenerPosition(const Vector3& position); … … 56 58 57 59 void registerAmbientSound(AmbientSound* newAmbient); 58 void unregisterAmbientSound(AmbientSound* currentAmbient); 59 const std::string& getAmbientPath(const std::string& source); 60 void fadeInAmbientSound(float dt); 61 void fadeOutAmbientSound(float dt); 62 void checkFadeStepValidity(void); 60 void unregisterAmbientSound(AmbientSound* oldAmbient); 61 void pauseAmbientSound(AmbientSound* ambient); 62 std::string getAmbientPath(const std::string& source); 63 63 64 64 private: 65 void processCrossFading(float dt); 66 void fadeIn(AmbientSound* sound); 67 void fadeOut(AmbientSound* sound); 68 69 void checkFadeStepValidity(); 70 65 71 ALCdevice* device_; 66 72 ALCcontext* context_; 67 73 68 std::list<AmbientSound*> ambientSounds_; 74 typedef std::list<std::pair<AmbientSound*, bool> > AmbientList; 75 AmbientList ambientSounds_; 69 76 70 float fadeStep_; //per second71 std::list< std::pair<AmbientSound*, float>> fadeInList_;72 std::list< std::pair<AmbientSound*, float>> fadeOutList_;77 float crossFadeStep_; //!< Absolute change per second (0.1 means 10% of the nominal volume) for cross fading 78 std::list<AmbientSound*> fadeInList_; 79 std::list<AmbientSound*> fadeOutList_; 73 80 74 std::string lastReqPath_;75 76 81 static SoundManager* singletonPtr_s; 77 82 }; -
code/branches/sound3/src/orxonox/sound/WorldSound.cc
r5929 r6069 53 53 { 54 54 SUPER(WorldSound, XMLPort, xmlelement, mode); 55 XMLPortParamExtern(WorldSound, BaseSound, this, " source", setSource, getSource, xmlelement, mode);55 XMLPortParamExtern(WorldSound, BaseSound, this, "volume", setVolume, getVolume, xmlelement, mode); 56 56 XMLPortParamExtern(WorldSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode); 57 57 XMLPortParamExtern(WorldSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode); 58 XMLPortParamExtern(WorldSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode); 58 59 } 59 60 … … 88 89 } 89 90 } 90 91 91 } -
code/branches/sound3/src/orxonox/sound/WorldSound.h
r5929 r6069 26 26 * 27 27 */ 28 28 29 #ifndef _WorldSound_H__ 29 30 #define _WorldSound_H__
Note: See TracChangeset
for help on using the changeset viewer.