Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.