[Orxonox-commit 1328] r6046 - code/branches/sound3/src/orxonox/sound

youngk at orxonox.net youngk at orxonox.net
Wed Nov 11 17:55:26 CET 2009


Author: youngk
Date: 2009-11-11 17:55:26 +0100 (Wed, 11 Nov 2009)
New Revision: 6046

Modified:
   code/branches/sound3/src/orxonox/sound/AmbientSound.cc
   code/branches/sound3/src/orxonox/sound/AmbientSound.h
   code/branches/sound3/src/orxonox/sound/BaseSound.cc
   code/branches/sound3/src/orxonox/sound/BaseSound.h
   code/branches/sound3/src/orxonox/sound/SoundManager.cc
   code/branches/sound3/src/orxonox/sound/SoundManager.h
Log:
Made a big change to the sound system: Implemented cross-fading for multiple changing sources.
Compiles, however the game doesn't load sounds properly in the main menu and on entering a level.
It can load a sound via DistanceTrigger and fades in/out correctly, but second loading fails.

Please test.

Modified: code/branches/sound3/src/orxonox/sound/AmbientSound.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/AmbientSound.cc	2009-11-11 16:39:53 UTC (rev 6045)
+++ code/branches/sound3/src/orxonox/sound/AmbientSound.cc	2009-11-11 16:55:26 UTC (rev 6046)
@@ -64,34 +64,39 @@
     void AmbientSound::play()
     {
         COUT(3) << this->getSource() << ": Playing" << std::endl;
-        SoundManager::getInstance().registerAmbientSound(this);
-        SUPER(AmbientSound, play);
+        if(GameMode::playsSound())
+        {
+            SoundManager::getInstance().registerAmbientSound(this);
+            this->BaseSound::play();
+        }
     }
 
     void AmbientSound::replay()
     {
-        SUPER(AmbientSound, play);
+        this->BaseSound::play();
     }
 
     void AmbientSound::stop()
     {
-        SUPER(AmbientSound, stop);
-        SoundManager::getInstance().unregisterAmbientSound(this);
+        if(GameMode::playsSound())
+        {
+            SoundManager::getInstance().unregisterAmbientSound(this);
+        }
     }
 
-    void AmbientSound::pause()
+    void AmbientSound::doStop()
     {
-        SUPER(AmbientSound, pause);
+        this->BaseSound::stop();
     }
 
     void AmbientSound::setSource(const std::string& source)
     {
-        if(source.find('/') == std::string.npos)
+        if(source.find('/') == std::string.npos && GameMode::playsSound())
         {
             std::string filePath = SoundManager::getInstance().getAmbientPath(source);
             if(!(filePath.empty()))
             {
-                BaseSound::setSource(filePath);
+                this->BaseSound::setSource(filePath);
                 return;
             }
         }
@@ -101,7 +106,7 @@
     void AmbientSound::changedActivity() 
     {
         COUT(3) << this->getSource() << ": ChangedActivity: " << this->isActive() << std::endl;
-        SUPER(AmbientSound, changedActivity);
+        this->BaseObject::changedActivity();
         if(this->isActive())
         {
             this->play();
@@ -111,5 +116,4 @@
             this->stop();
         }
     }
-
 }

Modified: code/branches/sound3/src/orxonox/sound/AmbientSound.h
===================================================================
--- code/branches/sound3/src/orxonox/sound/AmbientSound.h	2009-11-11 16:39:53 UTC (rev 6045)
+++ code/branches/sound3/src/orxonox/sound/AmbientSound.h	2009-11-11 16:55:26 UTC (rev 6046)
@@ -47,9 +47,9 @@
         virtual ~AmbientSound();
 
         virtual void play();
-        virtual void replay();      // is only needed for the AmbientSound list in SoundManager
+        void replay();      // Continue playing without re-registering the sound
         virtual void stop();
-        virtual void pause();
+        void doStop();
 
         virtual void setSource(const std::string& source);
 

Modified: code/branches/sound3/src/orxonox/sound/BaseSound.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/BaseSound.cc	2009-11-11 16:39:53 UTC (rev 6045)
+++ code/branches/sound3/src/orxonox/sound/BaseSound.cc	2009-11-11 16:55:26 UTC (rev 6046)
@@ -69,11 +69,6 @@
         }
     }
 
-    void BaseSound::replay()
-    {
-        BaseSound::play();
-    }
-
     void BaseSound::stop()
     {
         if (alIsSource(this->audioSource_))
@@ -186,6 +181,11 @@
             this->play();
     }
 
+    ALuint BaseSound::getALAudioSource()
+    {
+        return audioSource_;
+    }
+
     ALint BaseSound::getSourceState()
     {
         ALint state;

Modified: code/branches/sound3/src/orxonox/sound/BaseSound.h
===================================================================
--- code/branches/sound3/src/orxonox/sound/BaseSound.h	2009-11-11 16:39:53 UTC (rev 6045)
+++ code/branches/sound3/src/orxonox/sound/BaseSound.h	2009-11-11 16:55:26 UTC (rev 6046)
@@ -49,9 +49,8 @@
         virtual ~BaseSound();
 
         virtual void play();
-        virtual void replay();      // is only needed for the AmbientSound list in SoundManager
         virtual void stop();
-        virtual void pause();
+        void pause();
 
         bool isPlaying();
         bool isPaused();
@@ -66,6 +65,8 @@
         bool getLoop() { return this->bLoop_; }
         void setLoop(bool val) { this->bLoop_ = val; }
 
+        ALuint getALAudioSource(void);
+
     protected:
         ALuint loadOggFile();
         ALint getSourceState();

Modified: code/branches/sound3/src/orxonox/sound/SoundManager.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/SoundManager.cc	2009-11-11 16:39:53 UTC (rev 6045)
+++ code/branches/sound3/src/orxonox/sound/SoundManager.cc	2009-11-11 16:55:26 UTC (rev 6046)
@@ -34,11 +34,14 @@
 #include "util/Math.h"
 #include "util/ScopeGuard.h"
 #include "util/StringUtils.h"
+#include "util/Clock.h"
 #include "core/GameMode.h"
 #include "core/ScopedSingletonManager.h"
 #include "core/Resource.h"
+#include "core/ConfigValueIncludes.h"
 #include "BaseSound.h"
 #include "MoodManager.h"
+#include "AmbientSound.h"
 
 namespace orxonox
 {
@@ -47,6 +50,8 @@
 
     SoundManager::SoundManager()
     {
+        RegisterRootObject(SoundManager);
+
         if (!alutInitWithoutContext(NULL,NULL))
             ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
         Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit);
@@ -85,6 +90,8 @@
         alutExitGuard.Dismiss();
         closeDeviceGuard.Dismiss();
         desroyContextGuard.Dismiss();
+
+        this->setConfigValues();
     }
 
     SoundManager::~SoundManager()
@@ -95,6 +102,19 @@
         alutExit();
     }
 
+    void SoundManager::update(const Clock &time)
+    {
+        this->fadeInAmbientSound(time.getDeltaTime());
+        this->fadeOutAmbientSound(time.getDeltaTime());
+    }
+
+    void SoundManager::setConfigValues()
+    {
+        SetConfigValue(fadeStep_, 0.2f)
+            .description("Determines how fast sounds should fade, per second.")
+            .callback(this, &SoundManager::checkFadeStepValidity);
+    }
+
     void SoundManager::setListenerPosition(const Vector3& position)
     {
         alListener3f(AL_POSITION, position.x, position.y, position.z);
@@ -118,16 +138,20 @@
             COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
     }
 
-    void SoundManager::registerAmbientSound(BaseSound* newAmbient)
+    void SoundManager::registerAmbientSound(AmbientSound* newAmbient)
     {
-        if (!(this->ambientSounds_.empty())) 
+        if(newAmbient != NULL)
         {
-            this->ambientSounds_.front()->pause();
+            if (!(this->ambientSounds_.empty())) 
+            {
+                this->fadeOutList_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0));
+            }
+            this->fadeInList_.push_front(std::make_pair(newAmbient, 0.0));
+            this->ambientSounds_.push_front(newAmbient);
         }
-        this->ambientSounds_.push_front(newAmbient);
     }
 
-    void SoundManager::unregisterAmbientSound(BaseSound* currentAmbient)
+    void SoundManager::unregisterAmbientSound(AmbientSound* currentAmbient)
     {
         if(currentAmbient == NULL || ambientSounds_.empty())
         {
@@ -135,18 +159,20 @@
         }
         if(this->ambientSounds_.front() == currentAmbient) 
         {
+            this->fadeOutList_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0));
             this->ambientSounds_.pop_front();
             if(!(this->ambientSounds_.empty()))
             {
-                this->ambientSounds_.front()->replay();
+                this->fadeInList_.push_front(std::make_pair(this->ambientSounds_.front(), 0.0));
             }
         }
         else
         {
-            for(std::list<BaseSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++)
+            for(std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++)
             {
                 if(*it == currentAmbient)
                 {
+                    currentAmbient->doStop();
                     this->ambientSounds_.erase(it);
                     break;
                 }
@@ -157,12 +183,72 @@
     // Get the current mood and return the full path string to the requested sound.
     const std::string& SoundManager::getAmbientPath(const std::string& source)
     {
-        lastReqPath = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
-        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(lastReqPath);
+        lastReqPath_ = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
+        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(lastReqPath_);
         if(fileInfo == NULL)
         {
             return BLANKSTRING;
         }
-        return lastReqPath;
+        return lastReqPath_;
     }
+
+    void SoundManager::fadeInAmbientSound(float dt)
+    {
+        if(!(this->fadeInList_.empty()))
+        {
+            for(std::list<std::pair<AmbientSound*, float> >::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); it++)
+            {
+                it->second += fadeStep_ * dt;
+                alSourcef(it->first->getALAudioSource(), AL_GAIN, it->second);
+            }
+            if(this->fadeInList_.back().second >= 1)
+            {
+                this->fadeInList_.pop_back();
+            }
+        }
+    }
+
+    void SoundManager::fadeOutAmbientSound(float dt)
+    {
+        if(!(this->fadeInList_.empty()))
+        {
+            for(std::list<std::pair<AmbientSound*, float> >::iterator it= this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++)
+            {
+                it->second -= fadeStep_ * dt;
+                alSourcef(it->first->getALAudioSource(), AL_GAIN, it->second);
+            }
+            if(this->fadeOutList_.back().second <= 0)
+            {
+                bool pauseTest = false;
+            
+                for(std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++)
+                {
+                    if(*it == this->fadeOutList_.back().first)
+                    {
+                        pauseTest = true;
+                        break;
+                    }
+                }
+                if(pauseTest)
+                {
+                    this->fadeOutList_.back().first->pause();
+                }
+                else
+                {
+                    this->fadeOutList_.back().first->doStop();
+                }
+                this->fadeOutList_.pop_back();
+            }
+        }
+    }
+
+    void SoundManager::checkFadeStepValidity()
+    {
+        if(fadeStep_ <= 0.0 || fadeStep_ >= 1.0 )
+        {
+            ResetConfigValue(fadeStep_);
+        }
+        COUT(0) << "SoundManager: fade step now set to " << fadeStep_ << std::endl;
+        return;
+    }
 }

Modified: code/branches/sound3/src/orxonox/sound/SoundManager.h
===================================================================
--- code/branches/sound3/src/orxonox/sound/SoundManager.h	2009-11-11 16:39:53 UTC (rev 6045)
+++ code/branches/sound3/src/orxonox/sound/SoundManager.h	2009-11-11 16:55:26 UTC (rev 6046)
@@ -41,24 +41,37 @@
      * position. It is a singleton.
      *
      */
-    class _OrxonoxExport SoundManager : public Singleton<SoundManager>
+    class _OrxonoxExport SoundManager : public Singleton<SoundManager>, public OrxonoxClass
     {
         friend class Singleton<SoundManager>;
     public:
         SoundManager();
         ~SoundManager();
 
+        void update(const Clock &time);
+        void setConfigValues(void);
+
         void setListenerPosition(const Vector3& position);
         void setListenerOrientation(const Quaternion& orientation);
-        void registerAmbientSound(BaseSound* newAmbient);
-        void unregisterAmbientSound(BaseSound* currentAmbient);
+
+        void registerAmbientSound(AmbientSound* newAmbient);
+        void unregisterAmbientSound(AmbientSound* currentAmbient);
         const std::string& getAmbientPath(const std::string& source);
+        void fadeInAmbientSound(float dt);
+        void fadeOutAmbientSound(float dt);
+        void checkFadeStepValidity(void);
 
     private:
         ALCdevice* device_;
         ALCcontext* context_;
-        std::list<BaseSound*> ambientSounds_;
-        std::string lastReqPath;
+       
+        std::list<AmbientSound*> ambientSounds_;
+        
+        float fadeStep_;       //per second
+        std::list<std::pair<AmbientSound*, float> > fadeInList_;
+        std::list<std::pair<AmbientSound*, float> > fadeOutList_;
+        
+        std::string lastReqPath_;
 
         static SoundManager* singletonPtr_s;
     };




More information about the Orxonox-commit mailing list