Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6186


Ignore:
Timestamp:
Nov 30, 2009, 11:50:17 PM (14 years ago)
Author:
dafrick
Message:

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.

Location:
code/branches/presentation2
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2/data/gui/scripts/NewAudioMenu.lua

    r6150 r6186  
    2323
    2424function P.AudioMuteMusicCheckbox_clicked(e)
    25     -- mute music
     25    soundMgr = orxonox.SoundManager:getInstance()
     26    soundMgr:toggleMute(orxonox.SoundType.ambient)
    2627    debug("event: mute music")
    2728end
    2829
    2930function P.AudioMuteSoundCheckbox_clicked(e)
    30     -- mute sound
     31    soundMgr = orxonox.SoundManager:getInstance()
     32    soundMgr:toggleMute(orxonox.SoundType.none)
    3133    debug("event: mute sound")
    3234end
  • code/branches/presentation2/src/orxonox/sound/AmbientSound.cc

    r6117 r6186  
    101101        }
    102102    }
     103   
     104    float AmbientSound::getVolumeGain()
     105    {
     106        return SoundManager::getInstance().getVolume(SoundType::ambient);
     107    }
    103108
    104109    void AmbientSound::doPause()
  • code/branches/presentation2/src/orxonox/sound/AmbientSound.h

    r6117 r6186  
    5858        virtual void stop();
    5959        virtual void pause();
     60       
     61        virtual float getVolumeGain();
    6062
    6163        virtual void setAmbientSource(const std::string& source);
  • code/branches/presentation2/src/orxonox/sound/BaseSound.cc

    r6184 r6186  
    3838#include "core/Resource.h"
    3939#include "core/XMLPort.h"
     40#include "SoundManager.h"
    4041
    4142namespace orxonox
     
    5455            assert(this->audioSource_ != 0);
    5556        }
     57       
    5658    }
    5759
     
    108110        }
    109111        this->volume_ = vol;
     112       
     113        this->updateVolume();
     114    }
     115   
     116    float BaseSound::getVolumeGain()
     117    {
     118        return SoundManager::getInstance().getVolume(SoundType::none);
     119    }
     120   
     121    void BaseSound::updateVolume(void)
     122    {
    110123        if (alIsSource(this->audioSource_))
    111             alSourcef(this->audioSource_, AL_GAIN, this->getEffectiveVolume());
    112     }
    113    
    114     void BaseSound::setVolumeGain(float gain)
    115     {
    116         COUT(1) << "blubb: " << gain << std::endl;
    117         if (gain > 1 || gain < 0)
    118         {
    119             COUT(2) << "Sound warning: volume gain out of range, cropping value." << std::endl;
    120             gain = gain > 1 ? 1 : gain;
    121             gain = gain < 0 ? 0 : gain;
    122         }
    123         this->volumeGain_ = gain;
    124         if (alIsSource(this->audioSource_))
    125             alSourcef(this->audioSource_, AL_GAIN, this->getEffectiveVolume());
    126     }
    127    
    128     float BaseSound::getEffectiveVolume(void)
    129     {
    130         return this->volume_*this->volumeGain_;
     124            alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain());
    131125    }
    132126
     
    200194
    201195        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
    202         alSourcef (this->audioSource_, AL_GAIN, this->getEffectiveVolume());
     196        this->updateVolume();
    203197        alSourcei (this->audioSource_, AL_LOOPING, (this->bLoop_ ? AL_TRUE : AL_FALSE));
    204198        if (this->isPlaying() || this->isPaused())
  • code/branches/presentation2/src/orxonox/sound/BaseSound.h

    r6184 r6186  
    6565        float getVolume() const { return this->volume_; }
    6666       
    67         void setVolumeGain(float gain);
    68         inline float getVolumeGain()
    69             { return this->volumeGain_; }
    70            
    71         float getEffectiveVolume(void);
     67        virtual float getVolumeGain();
     68        void updateVolume(void);
    7269
    7370        bool getLooping() const   { return this->bLoop_; }
     
    9289        std::string     source_;
    9390        float           volume_;
    94         float           volumeGain_;
    9591        bool            bLoop_;
    9692        State           state_;
  • code/branches/presentation2/src/orxonox/sound/SoundManager.cc

    r6184 r6186  
    9292        desroyContextGuard.Dismiss();
    9393       
    94         this->volume_ = 1.0;
    95         this->ambientVolume_ = 1.0;
    96         this->effectsVolume_ = 1.0;
     94        this->setVolumeInternal(1.0, SoundType::none);
     95        this->setVolumeInternal(1.0, SoundType::ambient);
     96        this->setVolumeInternal(1.0, SoundType::effects);
     97       
     98        this->mute_[SoundType::none] = false;
     99        this->mute_[SoundType::ambient] = false;
     100        this->mute_[SoundType::effects] = false;
    97101
    98102        this->setConfigValues();
     
    118122            .callback(this, &SoundManager::checkFadeStepValidity);
    119123           
    120         SetConfigValue(volume_, 1.0f)
     124        SetConfigValue(soundVolume_, 1.0f)
    121125            .description("Defines the overall volume.")
    122             .callback(this, &SoundManager::checkVolumeValidity);
     126            .callback(this, &SoundManager::checkSoundVolumeValidity);
    123127           
    124128        SetConfigValue(ambientVolume_, 1.0f)
     
    142146    }
    143147   
    144     void SoundManager::checkVolumeValidity()
    145     {
    146         if(volume_ < 0.0 || volume_ > 1.0)
     148    bool SoundManager::checkVolumeValidity(SoundType::Value type)
     149    {
     150        bool valid = true;
     151       
     152        if(this->getVolumeInternal(type) < 0.0 || this->getVolumeInternal(type) > 1.0)
    147153        {
    148154            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
    149             ResetConfigValue(volume_);
    150         }
    151        
    152         this->updateVolume();
    153         COUT(3) << "SoundManager: Overall volume set to " << volume_ << std::endl;
    154         return;
     155            valid = false;
     156        }
     157       
     158        this->updateVolume(type);
     159        COUT(3) << "SoundManager: volume set to " << this->getVolumeInternal(type) << std::endl;
     160        return valid;
     161    }
     162   
     163    void SoundManager::checkSoundVolumeValidity()
     164    {
     165        if(this->soundVolume_ < 0.0 || this->soundVolume_ > 1.0)
     166        {
     167            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
     168            ResetConfigValue(soundVolume_);
     169        }
     170       
     171        this->updateVolume(SoundType::none);
     172        COUT(3) << "SoundManager: volume set to " << this->soundVolume_ << std::endl;
     173           
    155174    }
    156175   
     
    159178        if(this->ambientVolume_ < 0.0 || this->ambientVolume_ > 1.0)
    160179        {
    161             COUT(2) << "Sound warning: Ambient volume out of range, ignoring change." << std::endl;
     180            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
    162181            ResetConfigValue(ambientVolume_);
    163182        }
    164183       
    165         this->updateAmbientVolume();
    166         COUT(3) << "SoundManager: Ambient volume set to " << this->ambientVolume_ << std::endl;
    167         return;
     184        this->updateVolume(SoundType::ambient);
     185        COUT(3) << "SoundManager: volume set to " << this->ambientVolume_ << std::endl;
    168186    }
    169187   
     
    172190        if(this->effectsVolume_ < 0.0 || this->effectsVolume_ > 1.0)
    173191        {
    174             COUT(2) << "Sound warning: effects volume out of range, ignoring change." << std::endl;
     192            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
    175193            ResetConfigValue(effectsVolume_);
    176194        }
    177195       
    178         this->updateEffectsVolume();
    179         COUT(3) << "SoundManager: Effects volume set to " << this->effectsVolume_ << std::endl;
    180         return;
    181     }
     196        this->updateVolume(SoundType::effects);
     197        COUT(3) << "SoundManager: volume set to " << this->effectsVolume_ << std::endl;
     198    }
     199   
     200//     void SoundManager::checkSoundVolumeValidity()
     201//     {
     202//         if(!checkVolumeValidity(SoundType::none))
     203//             ResetConfigValue(soundVolume_)
     204//     }
     205//     
     206//     void SoundManager::checkAmbientVolumeValidity()
     207//     {
     208//         if(!checkVolumeValidity(SoundType::ambient))
     209//             ResetConfigValue(ambientVolume_);
     210//     }
     211//     
     212//     void SoundManager::checkEffectsVolumeValidity()
     213//     {
     214//         if(!checkVolumeValidity(SoundType::effects))
     215//             ResetConfigValue(effectsVolume_);
     216//     }
    182217
    183218    void SoundManager::setListenerPosition(const Vector3& position)
     
    276311    }
    277312   
    278     void SoundManager::setAmbientVolume(float vol)
    279     {
    280         if (vol > 1 || vol < 0)
     313   
     314    void SoundManager::setVolume(float vol, SoundType::Value type)
     315    {
     316        vol = this->checkVolumeRange(vol);
     317       
     318        this->setVolumeInternal(vol, type);
     319       
     320        this->updateVolume(type);
     321    }
     322   
     323    float SoundManager::checkVolumeRange(float vol)
     324    {
     325        if(vol < 0.0 || vol > 1.0)
    281326        {
    282327            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
     
    284329            vol = vol < 0 ? 0 : vol;
    285330        }
    286         this->ambientVolume_ = vol;
    287        
    288         this->updateAmbientVolume();
    289     }
    290    
    291     void SoundManager::setEffectsVolume(float vol)
    292     {
    293         if (vol > 1 || vol < 0)
    294         {
    295             COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
    296             vol = vol > 1 ? 1 : vol;
    297             vol = vol < 0 ? 0 : vol;
    298         }
    299         this->effectsVolume_ = vol;
    300        
    301         this->updateEffectsVolume();
    302     }
    303    
    304     void SoundManager::setVolume(float vol)
    305     {
    306         if (vol > 1 || vol < 0)
    307         {
    308             COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
    309             vol = vol > 1 ? 1 : vol;
    310             vol = vol < 0 ? 0 : vol;
    311         }
    312         this->volume_ = vol;
    313        
    314         this->updateVolume();
    315     }
    316    
    317     float SoundManager::getAmbientVolume(void)
    318     {
    319         return this->ambientVolume_;
    320     }
    321    
    322     float SoundManager::getEffectsVolume(void)
    323     {
    324         return this->effectsVolume_;
    325     }
    326    
    327     float SoundManager::getVolume(void)
    328     {
    329         return this->volume_;
    330     }
     331       
     332        return vol;
     333    }
     334   
     335    void SoundManager::updateVolume(SoundType::Value type)
     336    {
     337        switch(type)
     338        {
     339            case SoundType::none:
     340                for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it)
     341                {
     342                    (*it)->updateVolume();
     343                }
     344                break;
     345            case SoundType::ambient:
     346                for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it)
     347                {
     348                    (*it)->updateVolume();
     349                }
     350                break;
     351            case SoundType::effects:
     352                for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it)
     353                {
     354                    (*it)->updateVolume();
     355                }
     356                break;
     357            default:
     358                COUT(2) << "Invalid SoundType in SoundManager::updateVolume() - Not updating!" << std::endl;
     359        }
     360    }
     361   
     362    void SoundManager::setVolumeInternal(float vol, SoundType::Value type)
     363    {
     364        switch(type)
     365        {
     366            case SoundType::none:
     367                this->soundVolume_ = vol;
     368                break;
     369            case SoundType::ambient:
     370                this->ambientVolume_ = vol;
     371                break;
     372            case SoundType::effects:
     373                this->effectsVolume_ = vol;
     374                break;
     375            default:
     376                COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Not setting any volume!" << std::endl;
     377        }
     378    }
     379   
     380    float SoundManager::getVolumeInternal(SoundType::Value type)
     381    {
     382        switch(type)
     383        {
     384            case SoundType::none:
     385                return this->soundVolume_;
     386            case SoundType::ambient:
     387                return this->ambientVolume_;
     388            case SoundType::effects:
     389                return this->effectsVolume_;
     390            default:
     391                COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Returning 0.0!" << std::endl;
     392                return 0.0;
     393        }
     394    }
     395   
     396    float SoundManager::getVolume(SoundType::Value type)
     397    {
     398        if(this->mute_[SoundType::none] || this->mute_[type])
     399            return 0.0;
     400       
     401        if(type == SoundType::none)
     402            return this->getVolumeInternal(type);
     403       
     404        return this->getVolumeInternal(SoundType::none)*this->getVolumeInternal(type);
     405    }
     406   
     407    void SoundManager::toggleMute(SoundType::Value type)
     408    {
     409        bool mute = !this->mute_[type];
     410        this->mute_[type] = mute;
     411       
     412        this->updateVolume(type);
     413    }
     414   
     415    bool SoundManager::getMute(SoundType::Value type)
     416    {
     417        return this->mute_[type];
     418    }
     419   
    331420
    332421    void SoundManager::fadeIn(AmbientSound* sound)
     
    416505    }
    417506   
    418     void SoundManager::updateAmbientVolume(void)
    419     {
    420         for(ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it)
    421         {
    422             (*it)->setVolumeGain(this->volume_*this->ambientVolume_);
    423         }
    424     }
    425    
    426     void SoundManager::updateEffectsVolume(void)
    427     {
    428         for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it)
    429         {
    430             (*it)->setVolumeGain(this->volume_*this->effectsVolume_);
    431         }
    432     }
    433    
    434     void SoundManager::updateVolume(void)
    435     {
    436         for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it)
    437         {
    438             AmbientSound* ambient = dynamic_cast<AmbientSound*>(*it);
    439             WorldSound* world = dynamic_cast<WorldSound*>(*it);
    440             if(ambient != NULL)
    441             {
    442                 ambient->setVolumeGain(this->volume_*this->ambientVolume_);
    443             }
    444             else if(world != NULL)
    445             {
    446                 world->setVolumeGain(this->volume_*this->effectsVolume_);
    447             }
    448             else
    449             {
    450                 (*it)->setVolumeGain(this->volume_);
    451             }
    452         }
    453     }
    454507}
  • code/branches/presentation2/src/orxonox/sound/SoundManager.h

    r6184 r6186  
    3434#include <list>
    3535#include <string>
     36#include <map>
    3637#include "util/Singleton.h"
    3738#include "core/OrxonoxClass.h"
     
    4041namespace orxonox
    4142{
     43   
     44    //! Enum for the sound type.
     45    namespace SoundType
     46    {
     47        enum Value
     48        {
     49            none,
     50            ambient,
     51            effects
     52        };
     53    }
     54   
    4255    /**
    4356     * The SoundManager class manages the OpenAL device, context and listener
     
    6780        void pauseAmbientSound(AmbientSound* ambient);
    6881       
    69         void setAmbientVolume(float vol); // tolua_export
    70         void setEffectsVolume(float vol); // tolua_export
    71         void setVolume(float vol); // tolua_export
     82        void setVolume(float vol, SoundType::Value type); // tolua_export
     83        float getVolume(SoundType::Value type); // tolua_export
    7284       
    73         float getAmbientVolume(void); // tolua_export
    74         float getEffectsVolume(void); // tolua_export
    75         float getVolume(void); // tolua_export
     85        void toggleMute(SoundType::Value type); // tolua_export
     86        bool getMute(SoundType::Value type); // tolua_export
    7687
    7788    private:
     
    8192
    8293        void checkFadeStepValidity();
    83         void checkVolumeValidity();
    84         void checkAmbientVolumeValidity();
    85         void checkEffectsVolumeValidity();
     94        bool checkVolumeValidity(SoundType::Value type);
     95        void checkSoundVolumeValidity(void);
     96        void checkAmbientVolumeValidity(void);
     97        void checkEffectsVolumeValidity(void);
    8698       
    87         void updateAmbientVolume(void);
    88         void updateEffectsVolume(void);
    89         void updateVolume(void);
     99        float checkVolumeRange(float vol);
     100       
     101        void updateVolume(SoundType::Value type);
     102       
     103        void setVolumeInternal(float vol, SoundType::Value type);
     104        float getVolumeInternal(SoundType::Value type);
    90105
    91106        ALCdevice* device_;
     
    99114        std::list<AmbientSound*> fadeOutList_;
    100115       
     116        float soundVolume_;
    101117        float ambientVolume_;
    102118        float effectsVolume_;
    103         float volume_;
     119        std::map<SoundType::Value, bool> mute_;
    104120       
    105121        static SoundManager* singletonPtr_s;
  • code/branches/presentation2/src/orxonox/sound/WorldSound.cc

    r6117 r6186  
    3535#include "core/EventIncludes.h"
    3636#include "core/XMLPort.h"
     37#include "SoundManager.h"
    3738
    3839namespace orxonox
     
    9596            this->stop();
    9697    }
     98   
     99    float WorldSound::getVolumeGain()
     100    {
     101        return SoundManager::getInstance().getVolume(SoundType::effects);
     102    }
    97103}
  • code/branches/presentation2/src/orxonox/sound/WorldSound.h

    r6117 r6186  
    5151        virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
    5252        virtual void changedActivity();
     53       
     54        virtual float getVolumeGain();
    5355
    5456        virtual void tick(float dt);
Note: See TracChangeset for help on using the changeset viewer.