Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6046


Ignore:
Timestamp:
Nov 11, 2009, 5:55:26 PM (14 years ago)
Author:
youngk
Message:

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.

Location:
code/branches/sound3/src/orxonox/sound
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/sound3/src/orxonox/sound/AmbientSound.cc

    r6031 r6046  
    6565    {
    6666        COUT(3) << this->getSource() << ": Playing" << std::endl;
    67         SoundManager::getInstance().registerAmbientSound(this);
    68         SUPER(AmbientSound, play);
     67        if(GameMode::playsSound())
     68        {
     69            SoundManager::getInstance().registerAmbientSound(this);
     70            this->BaseSound::play();
     71        }
    6972    }
    7073
    7174    void AmbientSound::replay()
    7275    {
    73         SUPER(AmbientSound, play);
     76        this->BaseSound::play();
    7477    }
    7578
    7679    void AmbientSound::stop()
    7780    {
    78         SUPER(AmbientSound, stop);
    79         SoundManager::getInstance().unregisterAmbientSound(this);
     81        if(GameMode::playsSound())
     82        {
     83            SoundManager::getInstance().unregisterAmbientSound(this);
     84        }
    8085    }
    8186
    82     void AmbientSound::pause()
     87    void AmbientSound::doStop()
    8388    {
    84         SUPER(AmbientSound, pause);
     89        this->BaseSound::stop();
    8590    }
    8691
    8792    void AmbientSound::setSource(const std::string& source)
    8893    {
    89         if(source.find('/') == std::string.npos)
     94        if(source.find('/') == std::string.npos && GameMode::playsSound())
    9095        {
    9196            std::string filePath = SoundManager::getInstance().getAmbientPath(source);
    9297            if(!(filePath.empty()))
    9398            {
    94                 BaseSound::setSource(filePath);
     99                this->BaseSound::setSource(filePath);
    95100                return;
    96101            }
     
    102107    {
    103108        COUT(3) << this->getSource() << ": ChangedActivity: " << this->isActive() << std::endl;
    104         SUPER(AmbientSound, changedActivity);
     109        this->BaseObject::changedActivity();
    105110        if(this->isActive())
    106111        {
     
    112117        }
    113118    }
    114 
    115119}
  • code/branches/sound3/src/orxonox/sound/AmbientSound.h

    r6031 r6046  
    4848
    4949        virtual void play();
    50         virtual void replay();      // is only needed for the AmbientSound list in SoundManager
     50        void replay();      // Continue playing without re-registering the sound
    5151        virtual void stop();
    52         virtual void pause();
     52        void doStop();
    5353
    5454        virtual void setSource(const std::string& source);
  • code/branches/sound3/src/orxonox/sound/BaseSound.cc

    r5982 r6046  
    7070    }
    7171
    72     void BaseSound::replay()
    73     {
    74         BaseSound::play();
    75     }
    76 
    7772    void BaseSound::stop()
    7873    {
     
    185180        if (this->bPlayOnLoad_)
    186181            this->play();
     182    }
     183
     184    ALuint BaseSound::getALAudioSource()
     185    {
     186        return audioSource_;
    187187    }
    188188
  • code/branches/sound3/src/orxonox/sound/BaseSound.h

    r6031 r6046  
    5050
    5151        virtual void play();
    52         virtual void replay();      // is only needed for the AmbientSound list in SoundManager
    5352        virtual void stop();
    54         virtual void pause();
     53        void pause();
    5554
    5655        bool isPlaying();
     
    6665        bool getLoop() { return this->bLoop_; }
    6766        void setLoop(bool val) { this->bLoop_ = val; }
     67
     68        ALuint getALAudioSource(void);
    6869
    6970    protected:
  • code/branches/sound3/src/orxonox/sound/SoundManager.cc

    r6031 r6046  
    3535#include "util/ScopeGuard.h"
    3636#include "util/StringUtils.h"
     37#include "util/Clock.h"
    3738#include "core/GameMode.h"
    3839#include "core/ScopedSingletonManager.h"
    3940#include "core/Resource.h"
     41#include "core/ConfigValueIncludes.h"
    4042#include "BaseSound.h"
    4143#include "MoodManager.h"
     44#include "AmbientSound.h"
    4245
    4346namespace orxonox
     
    4851    SoundManager::SoundManager()
    4952    {
     53        RegisterRootObject(SoundManager);
     54
    5055        if (!alutInitWithoutContext(NULL,NULL))
    5156            ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
     
    8691        closeDeviceGuard.Dismiss();
    8792        desroyContextGuard.Dismiss();
     93
     94        this->setConfigValues();
    8895    }
    8996
     
    96103    }
    97104
     105    void SoundManager::update(const Clock &time)
     106    {
     107        this->fadeInAmbientSound(time.getDeltaTime());
     108        this->fadeOutAmbientSound(time.getDeltaTime());
     109    }
     110
     111    void SoundManager::setConfigValues()
     112    {
     113        SetConfigValue(fadeStep_, 0.2f)
     114            .description("Determines how fast sounds should fade, per second.")
     115            .callback(this, &SoundManager::checkFadeStepValidity);
     116    }
     117
    98118    void SoundManager::setListenerPosition(const Vector3& position)
    99119    {
     
    119139    }
    120140
    121     void SoundManager::registerAmbientSound(BaseSound* newAmbient)
    122     {
    123         if (!(this->ambientSounds_.empty()))
    124         {
    125             this->ambientSounds_.front()->pause();
    126         }
    127         this->ambientSounds_.push_front(newAmbient);
    128     }
    129 
    130     void SoundManager::unregisterAmbientSound(BaseSound* currentAmbient)
     141    void SoundManager::registerAmbientSound(AmbientSound* newAmbient)
     142    {
     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)
    131155    {
    132156        if(currentAmbient == NULL || ambientSounds_.empty())
     
    136160        if(this->ambientSounds_.front() == currentAmbient)
    137161        {
     162            this->fadeOutList_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0));
    138163            this->ambientSounds_.pop_front();
    139164            if(!(this->ambientSounds_.empty()))
    140165            {
    141                 this->ambientSounds_.front()->replay();
     166                this->fadeInList_.push_front(std::make_pair(this->ambientSounds_.front(), 0.0));
    142167            }
    143168        }
    144169        else
    145170        {
    146             for(std::list<BaseSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++)
     171            for(std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++)
    147172            {
    148173                if(*it == currentAmbient)
    149174                {
     175                    currentAmbient->doStop();
    150176                    this->ambientSounds_.erase(it);
    151177                    break;
     
    158184    const std::string& SoundManager::getAmbientPath(const std::string& source)
    159185    {
    160         lastReqPath = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
    161         shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(lastReqPath);
     186        lastReqPath_ = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
     187        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(lastReqPath_);
    162188        if(fileInfo == NULL)
    163189        {
    164190            return BLANKSTRING;
    165191        }
    166         return lastReqPath;
     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)
     227                    {
     228                        pauseTest = true;
     229                        break;
     230                    }
     231                }
     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;
    167253    }
    168254}
  • code/branches/sound3/src/orxonox/sound/SoundManager.h

    r6031 r6046  
    4242     *
    4343     */
    44     class _OrxonoxExport SoundManager : public Singleton<SoundManager>
     44    class _OrxonoxExport SoundManager : public Singleton<SoundManager>, public OrxonoxClass
    4545    {
    4646        friend class Singleton<SoundManager>;
     
    4949        ~SoundManager();
    5050
     51        void update(const Clock &time);
     52        void setConfigValues(void);
     53
    5154        void setListenerPosition(const Vector3& position);
    5255        void setListenerOrientation(const Quaternion& orientation);
    53         void registerAmbientSound(BaseSound* newAmbient);
    54         void unregisterAmbientSound(BaseSound* currentAmbient);
     56
     57        void registerAmbientSound(AmbientSound* newAmbient);
     58        void unregisterAmbientSound(AmbientSound* currentAmbient);
    5559        const std::string& getAmbientPath(const std::string& source);
     60        void fadeInAmbientSound(float dt);
     61        void fadeOutAmbientSound(float dt);
     62        void checkFadeStepValidity(void);
    5663
    5764    private:
    5865        ALCdevice* device_;
    5966        ALCcontext* context_;
    60         std::list<BaseSound*> ambientSounds_;
    61         std::string lastReqPath;
     67       
     68        std::list<AmbientSound*> ambientSounds_;
     69       
     70        float fadeStep_;       //per second
     71        std::list<std::pair<AmbientSound*, float> > fadeInList_;
     72        std::list<std::pair<AmbientSound*, float> > fadeOutList_;
     73       
     74        std::string lastReqPath_;
    6275
    6376        static SoundManager* singletonPtr_s;
Note: See TracChangeset for help on using the changeset viewer.