[Orxonox-commit 1468] r6186 - in code/branches/presentation2: data/gui/scripts src/orxonox/sound

dafrick at orxonox.net dafrick at orxonox.net
Mon Nov 30 23:50:18 CET 2009


Author: dafrick
Date: 2009-11-30 23:50:17 +0100 (Mon, 30 Nov 2009)
New Revision: 6186

Modified:
   code/branches/presentation2/data/gui/scripts/NewAudioMenu.lua
   code/branches/presentation2/src/orxonox/sound/AmbientSound.cc
   code/branches/presentation2/src/orxonox/sound/AmbientSound.h
   code/branches/presentation2/src/orxonox/sound/BaseSound.cc
   code/branches/presentation2/src/orxonox/sound/BaseSound.h
   code/branches/presentation2/src/orxonox/sound/SoundManager.cc
   code/branches/presentation2/src/orxonox/sound/SoundManager.h
   code/branches/presentation2/src/orxonox/sound/WorldSound.cc
   code/branches/presentation2/src/orxonox/sound/WorldSound.h
Log:
Some cleanup in SoundManager and related classes. Overrall volume works now. Mute function has been implemented into the gui.
Once again you'll have to delete your orxonox.ini file to be trouble free.


Modified: code/branches/presentation2/data/gui/scripts/NewAudioMenu.lua
===================================================================
--- code/branches/presentation2/data/gui/scripts/NewAudioMenu.lua	2009-11-30 22:32:53 UTC (rev 6185)
+++ code/branches/presentation2/data/gui/scripts/NewAudioMenu.lua	2009-11-30 22:50:17 UTC (rev 6186)
@@ -22,12 +22,14 @@
 end
 
 function P.AudioMuteMusicCheckbox_clicked(e)
-    -- mute music
+    soundMgr = orxonox.SoundManager:getInstance()
+    soundMgr:toggleMute(orxonox.SoundType.ambient)
     debug("event: mute music")
 end
 
 function P.AudioMuteSoundCheckbox_clicked(e)
-    -- mute sound
+    soundMgr = orxonox.SoundManager:getInstance()
+    soundMgr:toggleMute(orxonox.SoundType.none)
     debug("event: mute sound")
 end
 

Modified: code/branches/presentation2/src/orxonox/sound/AmbientSound.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/AmbientSound.cc	2009-11-30 22:32:53 UTC (rev 6185)
+++ code/branches/presentation2/src/orxonox/sound/AmbientSound.cc	2009-11-30 22:50:17 UTC (rev 6186)
@@ -100,6 +100,11 @@
             SoundManager::getInstance().pauseAmbientSound(this);
         }
     }
+    
+    float AmbientSound::getVolumeGain()
+    {
+        return SoundManager::getInstance().getVolume(SoundType::ambient);
+    }
 
     void AmbientSound::doPause()
     {

Modified: code/branches/presentation2/src/orxonox/sound/AmbientSound.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/AmbientSound.h	2009-11-30 22:32:53 UTC (rev 6185)
+++ code/branches/presentation2/src/orxonox/sound/AmbientSound.h	2009-11-30 22:50:17 UTC (rev 6186)
@@ -57,6 +57,8 @@
         virtual void play();
         virtual void stop();
         virtual void pause();
+        
+        virtual float getVolumeGain();
 
         virtual void setAmbientSource(const std::string& source);
         const std::string& getAmbientSource() const { return this->ambientSource_; }

Modified: code/branches/presentation2/src/orxonox/sound/BaseSound.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/BaseSound.cc	2009-11-30 22:32:53 UTC (rev 6185)
+++ code/branches/presentation2/src/orxonox/sound/BaseSound.cc	2009-11-30 22:50:17 UTC (rev 6186)
@@ -37,6 +37,7 @@
 #include "core/GameMode.h"
 #include "core/Resource.h"
 #include "core/XMLPort.h"
+#include "SoundManager.h"
 
 namespace orxonox
 {
@@ -53,6 +54,7 @@
             alGenSources(1, &this->audioSource_);
             assert(this->audioSource_ != 0);
         }
+        
     }
 
     BaseSound::~BaseSound()
@@ -107,27 +109,19 @@
             vol = vol < 0 ? 0 : vol;
         }
         this->volume_ = vol;
-        if (alIsSource(this->audioSource_))
-            alSourcef(this->audioSource_, AL_GAIN, this->getEffectiveVolume());
+        
+        this->updateVolume();
     }
     
-    void BaseSound::setVolumeGain(float gain)
+    float BaseSound::getVolumeGain()
     {
-        COUT(1) << "blubb: " << gain << std::endl;
-        if (gain > 1 || gain < 0)
-        {
-            COUT(2) << "Sound warning: volume gain out of range, cropping value." << std::endl;
-            gain = gain > 1 ? 1 : gain;
-            gain = gain < 0 ? 0 : gain;
-        }
-        this->volumeGain_ = gain;
-        if (alIsSource(this->audioSource_))
-            alSourcef(this->audioSource_, AL_GAIN, this->getEffectiveVolume());
+        return SoundManager::getInstance().getVolume(SoundType::none);
     }
     
-    float BaseSound::getEffectiveVolume(void)
+    void BaseSound::updateVolume(void)
     {
-        return this->volume_*this->volumeGain_;
+        if (alIsSource(this->audioSource_))
+            alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain());
     }
 
     void BaseSound::setLooping(bool val)
@@ -199,7 +193,7 @@
         }
 
         alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
-        alSourcef (this->audioSource_, AL_GAIN, this->getEffectiveVolume());
+        this->updateVolume();
         alSourcei (this->audioSource_, AL_LOOPING, (this->bLoop_ ? AL_TRUE : AL_FALSE));
         if (this->isPlaying() || this->isPaused())
             alSourcePlay(this->audioSource_);

Modified: code/branches/presentation2/src/orxonox/sound/BaseSound.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/BaseSound.h	2009-11-30 22:32:53 UTC (rev 6185)
+++ code/branches/presentation2/src/orxonox/sound/BaseSound.h	2009-11-30 22:50:17 UTC (rev 6186)
@@ -64,11 +64,8 @@
         void setVolume(float vol);
         float getVolume() const { return this->volume_; }
         
-        void setVolumeGain(float gain);
-        inline float getVolumeGain()
-            { return this->volumeGain_; }
-            
-        float getEffectiveVolume(void);
+        virtual float getVolumeGain();
+        void updateVolume(void);
 
         bool getLooping() const   { return this->bLoop_; }
         void setLooping(bool val);
@@ -91,7 +88,6 @@
 
         std::string     source_;
         float           volume_;
-        float           volumeGain_;
         bool            bLoop_;
         State           state_;
         DataStreamPtr   dataStream_;

Modified: code/branches/presentation2/src/orxonox/sound/SoundManager.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundManager.cc	2009-11-30 22:32:53 UTC (rev 6185)
+++ code/branches/presentation2/src/orxonox/sound/SoundManager.cc	2009-11-30 22:50:17 UTC (rev 6186)
@@ -91,9 +91,13 @@
         closeDeviceGuard.Dismiss();
         desroyContextGuard.Dismiss();
         
-        this->volume_ = 1.0;
-        this->ambientVolume_ = 1.0;
-        this->effectsVolume_ = 1.0;
+        this->setVolumeInternal(1.0, SoundType::none);
+        this->setVolumeInternal(1.0, SoundType::ambient);
+        this->setVolumeInternal(1.0, SoundType::effects);
+        
+        this->mute_[SoundType::none] = false;
+        this->mute_[SoundType::ambient] = false;
+        this->mute_[SoundType::effects] = false;
 
         this->setConfigValues();
     }
@@ -117,9 +121,9 @@
             .description("Determines how fast sounds should fade, per second.")
             .callback(this, &SoundManager::checkFadeStepValidity);
             
-        SetConfigValue(volume_, 1.0f)
+        SetConfigValue(soundVolume_, 1.0f)
             .description("Defines the overall volume.")
-            .callback(this, &SoundManager::checkVolumeValidity);
+            .callback(this, &SoundManager::checkSoundVolumeValidity);
             
         SetConfigValue(ambientVolume_, 1.0f)
             .description("Defines the ambient volume.")
@@ -141,44 +145,75 @@
         return;
     }
     
-    void SoundManager::checkVolumeValidity()
+    bool SoundManager::checkVolumeValidity(SoundType::Value type)
     {
-        if(volume_ < 0.0 || volume_ > 1.0)
+        bool valid = true;
+        
+        if(this->getVolumeInternal(type) < 0.0 || this->getVolumeInternal(type) > 1.0)
         {
             COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
-            ResetConfigValue(volume_);
+            valid = false;
         }
         
-        this->updateVolume();
-        COUT(3) << "SoundManager: Overall volume set to " << volume_ << std::endl;
-        return;
+        this->updateVolume(type);
+        COUT(3) << "SoundManager: volume set to " << this->getVolumeInternal(type) << std::endl;
+        return valid;
     }
     
+    void SoundManager::checkSoundVolumeValidity()
+    {
+        if(this->soundVolume_ < 0.0 || this->soundVolume_ > 1.0)
+        {
+            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
+            ResetConfigValue(soundVolume_);
+        }
+        
+        this->updateVolume(SoundType::none);
+        COUT(3) << "SoundManager: volume set to " << this->soundVolume_ << std::endl;
+            
+    }
+    
     void SoundManager::checkAmbientVolumeValidity()
     {
         if(this->ambientVolume_ < 0.0 || this->ambientVolume_ > 1.0)
         {
-            COUT(2) << "Sound warning: Ambient volume out of range, ignoring change." << std::endl;
+            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
             ResetConfigValue(ambientVolume_);
         }
         
-        this->updateAmbientVolume();
-        COUT(3) << "SoundManager: Ambient volume set to " << this->ambientVolume_ << std::endl;
-        return;
+        this->updateVolume(SoundType::ambient);
+        COUT(3) << "SoundManager: volume set to " << this->ambientVolume_ << std::endl;
     }
     
     void SoundManager::checkEffectsVolumeValidity()
     {
         if(this->effectsVolume_ < 0.0 || this->effectsVolume_ > 1.0)
         {
-            COUT(2) << "Sound warning: effects volume out of range, ignoring change." << std::endl;
+            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
             ResetConfigValue(effectsVolume_);
         }
         
-        this->updateEffectsVolume();
-        COUT(3) << "SoundManager: Effects volume set to " << this->effectsVolume_ << std::endl;
-        return;
+        this->updateVolume(SoundType::effects);
+        COUT(3) << "SoundManager: volume set to " << this->effectsVolume_ << std::endl;
     }
+    
+//     void SoundManager::checkSoundVolumeValidity()
+//     {
+//         if(!checkVolumeValidity(SoundType::none))
+//             ResetConfigValue(soundVolume_)
+//     }
+//     
+//     void SoundManager::checkAmbientVolumeValidity()
+//     {
+//         if(!checkVolumeValidity(SoundType::ambient))
+//             ResetConfigValue(ambientVolume_);
+//     }
+//     
+//     void SoundManager::checkEffectsVolumeValidity()
+//     {
+//         if(!checkVolumeValidity(SoundType::effects))
+//             ResetConfigValue(effectsVolume_);
+//     }
 
     void SoundManager::setListenerPosition(const Vector3& position)
     {
@@ -275,59 +310,113 @@
         }
     }
     
-    void SoundManager::setAmbientVolume(float vol)
+    
+    void SoundManager::setVolume(float vol, SoundType::Value type)
     {
-        if (vol > 1 || vol < 0)
+        vol = this->checkVolumeRange(vol);
+        
+        this->setVolumeInternal(vol, type);
+        
+        this->updateVolume(type);
+    }
+    
+    float SoundManager::checkVolumeRange(float vol)
+    {
+        if(vol < 0.0 || vol > 1.0)
         {
             COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
             vol = vol > 1 ? 1 : vol;
             vol = vol < 0 ? 0 : vol;
         }
-        this->ambientVolume_ = vol;
         
-        this->updateAmbientVolume();
+        return vol;
     }
     
-    void SoundManager::setEffectsVolume(float vol)
+    void SoundManager::updateVolume(SoundType::Value type)
     {
-        if (vol > 1 || vol < 0)
+        switch(type)
         {
-            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
-            vol = vol > 1 ? 1 : vol;
-            vol = vol < 0 ? 0 : vol;
+            case SoundType::none:
+                for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it)
+                {
+                    (*it)->updateVolume();
+                }
+                break;
+            case SoundType::ambient:
+                for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it)
+                {
+                    (*it)->updateVolume();
+                }
+                break;
+            case SoundType::effects:
+                for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it)
+                {
+                    (*it)->updateVolume();
+                }
+                break;
+            default:
+                COUT(2) << "Invalid SoundType in SoundManager::updateVolume() - Not updating!" << std::endl;
         }
-        this->effectsVolume_ = vol;
-        
-        this->updateEffectsVolume();
     }
     
-    void SoundManager::setVolume(float vol)
+    void SoundManager::setVolumeInternal(float vol, SoundType::Value type)
     {
-        if (vol > 1 || vol < 0)
+        switch(type)
         {
-            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
-            vol = vol > 1 ? 1 : vol;
-            vol = vol < 0 ? 0 : vol;
+            case SoundType::none:
+                this->soundVolume_ = vol;
+                break;
+            case SoundType::ambient:
+                this->ambientVolume_ = vol;
+                break;
+            case SoundType::effects:
+                this->effectsVolume_ = vol;
+                break;
+            default:
+                COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Not setting any volume!" << std::endl;
         }
-        this->volume_ = vol;
-        
-        this->updateVolume();
     }
     
-    float SoundManager::getAmbientVolume(void)
+    float SoundManager::getVolumeInternal(SoundType::Value type)
     {
-        return this->ambientVolume_;
+        switch(type)
+        {
+            case SoundType::none:
+                return this->soundVolume_;
+            case SoundType::ambient:
+                return this->ambientVolume_;
+            case SoundType::effects:
+                return this->effectsVolume_;
+            default:
+                COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Returning 0.0!" << std::endl;
+                return 0.0;
+        }
     }
     
-    float SoundManager::getEffectsVolume(void)
+    float SoundManager::getVolume(SoundType::Value type) 
     {
-        return this->effectsVolume_;
+        if(this->mute_[SoundType::none] || this->mute_[type])
+            return 0.0;
+        
+        if(type == SoundType::none)
+            return this->getVolumeInternal(type);
+        
+        return this->getVolumeInternal(SoundType::none)*this->getVolumeInternal(type);
     }
     
-    float SoundManager::getVolume(void) 
+    void SoundManager::toggleMute(SoundType::Value type)
     {
-        return this->volume_;
+        bool mute = !this->mute_[type];
+        this->mute_[type] = mute;
+        
+        this->updateVolume(type);
     }
+    
+    bool SoundManager::getMute(SoundType::Value type)
+    {
+        return this->mute_[type];
+    }
+    
 
     void SoundManager::fadeIn(AmbientSound* sound)
     {
@@ -415,40 +504,4 @@
         }
     }
     
-    void SoundManager::updateAmbientVolume(void)
-    {
-        for(ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it)
-        {
-            (*it)->setVolumeGain(this->volume_*this->ambientVolume_);
-        }
-    }
-    
-    void SoundManager::updateEffectsVolume(void)
-    {
-        for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it)
-        {
-            (*it)->setVolumeGain(this->volume_*this->effectsVolume_);
-        }
-    }
-    
-    void SoundManager::updateVolume(void)
-    {
-        for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it)
-        {
-            AmbientSound* ambient = dynamic_cast<AmbientSound*>(*it);
-            WorldSound* world = dynamic_cast<WorldSound*>(*it);
-            if(ambient != NULL)
-            {
-                ambient->setVolumeGain(this->volume_*this->ambientVolume_);
-            }
-            else if(world != NULL)
-            {
-                world->setVolumeGain(this->volume_*this->effectsVolume_);
-            }
-            else
-            {
-                (*it)->setVolumeGain(this->volume_);
-            }
-        }
-    }
 }

Modified: code/branches/presentation2/src/orxonox/sound/SoundManager.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundManager.h	2009-11-30 22:32:53 UTC (rev 6185)
+++ code/branches/presentation2/src/orxonox/sound/SoundManager.h	2009-11-30 22:50:17 UTC (rev 6186)
@@ -33,12 +33,25 @@
 
 #include <list>
 #include <string>
+#include <map>
 #include "util/Singleton.h"
 #include "core/OrxonoxClass.h"
 
 // tolua_begin
 namespace orxonox
 {
+    
+    //! Enum for the sound type.
+    namespace SoundType
+    {
+        enum Value
+        {
+            none,
+            ambient,
+            effects
+        };
+    }
+    
     /**
      * The SoundManager class manages the OpenAL device, context and listener
      * position. It is a singleton.
@@ -66,13 +79,11 @@
         void unregisterAmbientSound(AmbientSound* oldAmbient);
         void pauseAmbientSound(AmbientSound* ambient);
         
-        void setAmbientVolume(float vol); // tolua_export
-        void setEffectsVolume(float vol); // tolua_export
-        void setVolume(float vol); // tolua_export
+        void setVolume(float vol, SoundType::Value type); // tolua_export
+        float getVolume(SoundType::Value type); // tolua_export
         
-        float getAmbientVolume(void); // tolua_export
-        float getEffectsVolume(void); // tolua_export
-        float getVolume(void); // tolua_export
+        void toggleMute(SoundType::Value type); // tolua_export
+        bool getMute(SoundType::Value type); // tolua_export
 
     private:
         void processCrossFading(float dt);
@@ -80,13 +91,17 @@
         void fadeOut(AmbientSound* sound);
 
         void checkFadeStepValidity();
-        void checkVolumeValidity();
-        void checkAmbientVolumeValidity();
-        void checkEffectsVolumeValidity();
+        bool checkVolumeValidity(SoundType::Value type);
+        void checkSoundVolumeValidity(void);
+        void checkAmbientVolumeValidity(void);
+        void checkEffectsVolumeValidity(void);
         
-        void updateAmbientVolume(void);
-        void updateEffectsVolume(void);
-        void updateVolume(void);
+        float checkVolumeRange(float vol);
+        
+        void updateVolume(SoundType::Value type);
+        
+        void setVolumeInternal(float vol, SoundType::Value type);
+        float getVolumeInternal(SoundType::Value type);
 
         ALCdevice* device_;
         ALCcontext* context_;
@@ -98,9 +113,10 @@
         std::list<AmbientSound*> fadeInList_;
         std::list<AmbientSound*> fadeOutList_;
         
+        float soundVolume_;
         float ambientVolume_;
         float effectsVolume_;
-        float volume_;
+        std::map<SoundType::Value, bool> mute_;
         
         static SoundManager* singletonPtr_s;
     }; // tolua_export

Modified: code/branches/presentation2/src/orxonox/sound/WorldSound.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/WorldSound.cc	2009-11-30 22:32:53 UTC (rev 6185)
+++ code/branches/presentation2/src/orxonox/sound/WorldSound.cc	2009-11-30 22:50:17 UTC (rev 6186)
@@ -34,6 +34,7 @@
 #include "core/CoreIncludes.h"
 #include "core/EventIncludes.h"
 #include "core/XMLPort.h"
+#include "SoundManager.h"
 
 namespace orxonox
 {
@@ -94,4 +95,9 @@
         else 
             this->stop();
     }
+    
+    float WorldSound::getVolumeGain()
+    {
+        return SoundManager::getInstance().getVolume(SoundType::effects);
+    }
 }

Modified: code/branches/presentation2/src/orxonox/sound/WorldSound.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/WorldSound.h	2009-11-30 22:32:53 UTC (rev 6185)
+++ code/branches/presentation2/src/orxonox/sound/WorldSound.h	2009-11-30 22:50:17 UTC (rev 6186)
@@ -50,6 +50,8 @@
         virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
         virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
         virtual void changedActivity();
+        
+        virtual float getVolumeGain();
 
         virtual void tick(float dt);
 




More information about the Orxonox-commit mailing list