Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6069


Ignore:
Timestamp:
Nov 15, 2009, 3:43:06 PM (14 years ago)
Author:
rgrieder
Message:

Detail changes in the sound classes.
Plus fixed the level sound problem (order of XML parameters was wrong).
There is still a large issue when changing an ambient sound source though.

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

Legend:

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

    r6046 r6069  
    3131#include "core/CoreIncludes.h"
    3232#include "core/EventIncludes.h"
     33#include "core/GameMode.h"
    3334#include "core/XMLPort.h"
    3435#include "SoundManager.h"
     
    4243    {
    4344        RegisterObject(AmbientSound);
     45
     46        // Ambient sounds always fade in
     47        this->setVolume(0);
    4448    }
    4549
     
    5155    {
    5256        SUPER(AmbientSound, XMLPort, xmlelement, mode);
    53         XMLPortParamExtern(AmbientSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
     57        XMLPortParamExtern(AmbientSound, BaseSound, this, "volume", setVolume, getVolume, xmlelement, mode);
    5458        XMLPortParamExtern(AmbientSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode);
    5559        XMLPortParamExtern(AmbientSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
     60        XMLPortParamExtern(AmbientSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
    5661    }
    5762
     
    6469    void AmbientSound::play()
    6570    {
    66         COUT(3) << this->getSource() << ": Playing" << std::endl;
    67         if(GameMode::playsSound())
     71        if (GameMode::playsSound())
    6872        {
     73            COUT(3) << "Sound: " << this->getSource() << ": Playing" << std::endl;
    6974            SoundManager::getInstance().registerAmbientSound(this);
    70             this->BaseSound::play();
    7175        }
    7276    }
    7377
    74     void AmbientSound::replay()
     78    void AmbientSound::doPlay()
    7579    {
    76         this->BaseSound::play();
     80        BaseSound::play();
    7781    }
    7882
    7983    void AmbientSound::stop()
    8084    {
    81         if(GameMode::playsSound())
     85        if (GameMode::playsSound())
    8286        {
    8387            SoundManager::getInstance().unregisterAmbientSound(this);
     
    8791    void AmbientSound::doStop()
    8892    {
    89         this->BaseSound::stop();
     93        BaseSound::stop();
     94    }
     95
     96    void AmbientSound::pause()
     97    {
     98        if (GameMode::playsSound())
     99        {
     100            SoundManager::getInstance().pauseAmbientSound(this);
     101        }
     102    }
     103
     104    void AmbientSound::doPause()
     105    {
     106        BaseSound::pause();
    90107    }
    91108
    92109    void AmbientSound::setSource(const std::string& source)
    93110    {
    94         if(source.find('/') == std::string.npos && GameMode::playsSound())
     111        if (GameMode::playsSound())
    95112        {
    96113            std::string filePath = SoundManager::getInstance().getAmbientPath(source);
    97             if(!(filePath.empty()))
     114            if (!filePath.empty())
    98115            {
    99                 this->BaseSound::setSource(filePath);
     116                BaseSound::setSource(filePath);
    100117                return;
    101118            }
     119            COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl;       
    102120        }
    103         COUT(3) << source << ": Not a valid name! Ambient sound will not change." << std::endl;       
    104121    }
    105122
    106123    void AmbientSound::changedActivity()
    107124    {
    108         COUT(3) << this->getSource() << ": ChangedActivity: " << this->isActive() << std::endl;
     125        COUT(3) << "Sound: " << this->getSource() << ": ChangedActivity: " << this->isActive() << std::endl;
    109126        this->BaseObject::changedActivity();
    110         if(this->isActive())
     127        if (this->isActive())
    111128        {
    112129            this->play();
  • code/branches/sound3/src/orxonox/sound/AmbientSound.h

    r6046 r6069  
    2222 *   Author:
    2323 *      Reto Grieder
     24 *      Kevin Young
    2425 *   Co-authors:
    2526 *      ...
    2627 *
    2728 */
     29
    2830#ifndef _AmbientSound_H__
    2931#define _AmbientSound_H__
     
    4345    class _OrxonoxExport AmbientSound : public BaseSound, public BaseObject
    4446    {
     47        friend class SoundManager;
     48
    4549    public:
    4650        AmbientSound(BaseObject* creator);
    4751        virtual ~AmbientSound();
    48 
    49         virtual void play();
    50         void replay();      // Continue playing without re-registering the sound
    51         virtual void stop();
    52         void doStop();
    53 
    54         virtual void setSource(const std::string& source);
    5552
    5653        virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     
    5855        virtual void changedActivity();
    5956
     57        virtual void play();
     58        virtual void stop();
     59        virtual void pause();
     60
     61        virtual void setSource(const std::string& source);
    6062
    6163    private:
     64        void doPlay();      // Continue playing without re-registering the sound
     65        void doStop();
     66        void doPause();
    6267    };
    6368}
  • code/branches/sound3/src/orxonox/sound/BaseSound.cc

    r6046 r6069  
    9393        if (alIsSource(this->audioSource_))
    9494            return getSourceState() == AL_PAUSED;
    95         return true;
     95        return false;
    9696    }
    9797
     
    103103    }
    104104
    105     void BaseSound::setPlayOnLoad(bool val)
    106     {
    107         this->bPlayOnLoad_ = val;
    108         if(val)
    109         {
    110             this->play();
    111         }
     105    void BaseSound::setVolume(float vol)
     106    {
     107        if (vol > 1 || vol < 0)
     108        {
     109            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
     110            vol = vol > 1 ? 1 : vol;
     111            vol = vol < 0 ? 0 : vol;
     112        }
     113        this->volume_ = vol;
     114        if (alIsSource(this->audioSource_))
     115            alSourcef(this->audioSource_, AL_GAIN, vol);
    112116    }
    113117
     
    178182        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
    179183
     184        this->setVolume(this->volume_);
     185
    180186        if (this->bPlayOnLoad_)
    181187            this->play();
    182     }
    183 
    184     ALuint BaseSound::getALAudioSource()
    185     {
    186         return audioSource_;
    187188    }
    188189
     
    275276        return buffer;
    276277    }
    277 
    278 } // namespace: orxonox
     278}
  • code/branches/sound3/src/orxonox/sound/BaseSound.h

    r6046 r6069  
    2626 *
    2727 */
     28
    2829#ifndef _BaseSound_H__
    2930#define _BaseSound_H__
     
    3233
    3334#include <string>
    34 #include <OgreSharedPtr.h>
    3535#include <OgreDataStream.h>
    3636#include "core/OrxonoxClass.h"
     
    5151        virtual void play();
    5252        virtual void stop();
    53         void pause();
     53        virtual void pause();
    5454
    5555        bool isPlaying();
     
    5858
    5959        virtual void setSource(const std::string& source);
    60         const std::string& getSource() { return this->source_; }
     60        virtual const std::string& getSource() const { return this->source_; }
    6161
    62         bool getPlayOnLoad() { return this->bPlayOnLoad_; }
    63         void setPlayOnLoad(bool val);
     62        void setVolume(float vol);
     63        float getVolume() const { return this->volume_; }
    6464
    65         bool getLoop() { return this->bLoop_; }
     65        bool getPlayOnLoad() const   { return this->bPlayOnLoad_; }
     66        void setPlayOnLoad(bool val) { this->bPlayOnLoad_ = val; }
     67
     68        bool getLoop() const   { return this->bLoop_; }
    6669        void setLoop(bool val) { this->bLoop_ = val; }
    6770
    68         ALuint getALAudioSource(void);
     71        //ALuint getALAudioSource(void);
    6972
    7073    protected:
     
    7679
    7780    private:
    78         std::string source_;
    79         bool bPlayOnLoad_;
    80         bool bLoop_;
    81         DataStreamPtr dataStream_;
     81        std::string     source_;
     82        float           volume_;
     83        bool            bPlayOnLoad_;
     84        bool            bLoop_;
     85        DataStreamPtr   dataStream_;
    8286    };
    8387}
  • code/branches/sound3/src/orxonox/sound/SoundManager.cc

    r6046 r6069  
    2222 *   Author:
    2323 *       Erwin 'vaiursch' Herrsche
     24 *       Kevin Young
    2425 *   Co-authors:
    2526 *      ...
     
    3031
    3132#include <AL/alut.h>
     33#include <utility>
    3234
    3335#include "util/Exception.h"
     
    5355        RegisterRootObject(SoundManager);
    5456
    55         if (!alutInitWithoutContext(NULL,NULL))
     57        if (!alutInitWithoutContext(NULL, NULL))
    5658            ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
    5759        Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit);
     
    103105    }
    104106
    105     void SoundManager::update(const Clock &time)
    106     {
    107         this->fadeInAmbientSound(time.getDeltaTime());
    108         this->fadeOutAmbientSound(time.getDeltaTime());
     107    void SoundManager::update(const Clock& time)
     108    {
     109        this->processCrossFading(time.getDeltaTime());
    109110    }
    110111
    111112    void SoundManager::setConfigValues()
    112113    {
    113         SetConfigValue(fadeStep_, 0.2f)
     114        SetConfigValue(crossFadeStep_, 0.2f)
    114115            .description("Determines how fast sounds should fade, per second.")
    115116            .callback(this, &SoundManager::checkFadeStepValidity);
     117    }
     118
     119    void SoundManager::checkFadeStepValidity()
     120    {
     121        if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 )
     122        {
     123            COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl;
     124            ResetConfigValue(crossFadeStep_);
     125        }
     126        COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl;
     127        return;
    116128    }
    117129
     
    141153    void SoundManager::registerAmbientSound(AmbientSound* newAmbient)
    142154    {
    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)
    155     {
    156         if(currentAmbient == NULL || ambientSounds_.empty())
     155        if (newAmbient != NULL)
     156        {
     157            for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
     158            {
     159                if (it->first == newAmbient)
     160                {
     161                    COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl;
     162                    return;
     163                }
     164            }
     165
     166            if (!this->ambientSounds_.empty())
     167            {
     168                this->fadeOut(ambientSounds_.front().first);
     169            }
     170            this->ambientSounds_.push_front(std::make_pair(newAmbient, false));
     171            newAmbient->doPlay();
     172            this->fadeIn(newAmbient);
     173        }
     174    }
     175
     176    void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient)
     177    {
     178        if (oldAmbient == NULL || ambientSounds_.empty())
    157179        {
    158180            return;
    159181        }
    160         if(this->ambientSounds_.front() == currentAmbient)
    161         {
    162             this->fadeOutList_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0));
     182        if (this->ambientSounds_.front().first == oldAmbient)
     183        {
     184            this->fadeOut(oldAmbient);
    163185            this->ambientSounds_.pop_front();
    164             if(!(this->ambientSounds_.empty()))
    165             {
    166                 this->fadeInList_.push_front(std::make_pair(this->ambientSounds_.front(), 0.0));
     186            if (!this->ambientSounds_.empty())
     187            {
     188                if (!this->ambientSounds_.front().second) // Not paused before
     189                {
     190                    this->ambientSounds_.front().first->doPlay();
     191                }
     192                this->fadeIn(this->ambientSounds_.front().first);
    167193            }
    168194        }
    169195        else
    170196        {
    171             for(std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++)
    172             {
    173                 if(*it == currentAmbient)
    174                 {
    175                     currentAmbient->doStop();
     197            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
     198            {
     199                if (it->first == oldAmbient)
     200                {
     201                    this->fadeOut(oldAmbient);
    176202                    this->ambientSounds_.erase(it);
    177203                    break;
     
    181207    }
    182208
    183     // Get the current mood and return the full path string to the requested sound.
    184     const std::string& SoundManager::getAmbientPath(const std::string& source)
    185     {
    186         lastReqPath_ = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
    187         shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(lastReqPath_);
    188         if(fileInfo == NULL)
    189         {
    190             return BLANKSTRING;
    191         }
    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)
     209    void SoundManager::pauseAmbientSound(AmbientSound* ambient)
     210    {
     211        if (ambient != NULL)
     212        {
     213            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
     214            {
     215                if (it->first == ambient)
     216                {
     217                    it->second = true;
     218                    this->fadeOut(it->first);
     219                    return;
     220                }
     221            }
     222        }
     223    }
     224
     225    //! Get the current mood and return the full path string to the requested sound.
     226    std::string SoundManager::getAmbientPath(const std::string& source)
     227    {
     228        std::string path = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
     229        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
     230        if (fileInfo == NULL)
     231        {
     232            return "";
     233        }
     234        return path;
     235    }
     236
     237    void SoundManager::fadeIn(AmbientSound* sound)
     238    {
     239        // If we're already fading out --> remove that
     240        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++)
     241        {
     242            if (*it == sound)
     243            {
     244                this->fadeOutList_.erase(it);
     245                break;
     246            }
     247        }
     248        // No duplicate entries
     249        if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end())
     250            this->fadeInList_.push_back(sound);
     251    }
     252
     253    void SoundManager::fadeOut(AmbientSound* sound)
     254    {
     255        // If we're already fading in --> remove that
     256        for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++)
     257        {
     258            if (*it == sound)
     259            {
     260                this->fadeInList_.erase(it);
     261                break;
     262            }
     263        }
     264        // No duplicate entries
     265        if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end())
     266            this->fadeOutList_.push_back(sound);
     267    }
     268
     269    void SoundManager::processCrossFading(float dt)
     270    {
     271        // FADE IN
     272        for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); it)
     273        {
     274            if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f)
     275            {
     276                (*it)->setVolume(1.0f);
     277                this->fadeInList_.erase(it++);
     278            }
     279            else
     280            {
     281                (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt);
     282                ++it;
     283            }
     284        }
     285
     286        // FADE OUT
     287        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it)
     288        {
     289            if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f)
     290            {
     291                (*it)->setVolume(0.0f);
     292
     293                // If sound is in the ambient list --> pause
     294                for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2)
     295                {
     296                    if (it2->first == *it)
    227297                    {
    228                         pauseTest = true;
     298                        (*it)->doPause();
    229299                        break;
    230300                    }
    231301                }
    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;
     302                // If not pause (by loop above for instance) --> stop
     303                if (!(*it)->isPaused())
     304                    (*it)->doStop();
     305
     306                this->fadeOutList_.erase(it++);
     307            }
     308            else
     309            {
     310                (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt);
     311                ++it;
     312            }
     313        }
    253314    }
    254315}
  • code/branches/sound3/src/orxonox/sound/SoundManager.h

    r6046 r6069  
    2222 *   Author:
    2323 *       Erwin 'vaiursch' Herrsche
     24 *       Kevin Young
    2425 *   Co-authors:
    2526 *      ...
    2627 */
     28
    2729#ifndef _SoundManager_H__
    2830#define _SoundManager_H__
     
    3032#include "OrxonoxPrereqs.h"
    3133
    32 #include <cassert>
    3334#include <list>
     35#include <string>
    3436#include "util/Singleton.h"
    35 #include "tools/interfaces/Tickable.h"
    3637
    3738namespace orxonox
     
    4546    {
    4647        friend class Singleton<SoundManager>;
     48
    4749    public:
    4850        SoundManager();
    4951        ~SoundManager();
    5052
    51         void update(const Clock &time);
    52         void setConfigValues(void);
     53        void update(const Clock& time);
     54        void setConfigValues();
    5355
    5456        void setListenerPosition(const Vector3& position);
     
    5658
    5759        void registerAmbientSound(AmbientSound* newAmbient);
    58         void unregisterAmbientSound(AmbientSound* currentAmbient);
    59         const std::string& getAmbientPath(const std::string& source);
    60         void fadeInAmbientSound(float dt);
    61         void fadeOutAmbientSound(float dt);
    62         void checkFadeStepValidity(void);
     60        void unregisterAmbientSound(AmbientSound* oldAmbient);
     61        void pauseAmbientSound(AmbientSound* ambient);
     62        std::string getAmbientPath(const std::string& source);
    6363
    6464    private:
     65        void processCrossFading(float dt);
     66        void fadeIn(AmbientSound* sound);
     67        void fadeOut(AmbientSound* sound);
     68
     69        void checkFadeStepValidity();
     70
    6571        ALCdevice* device_;
    6672        ALCcontext* context_;
    6773       
    68         std::list<AmbientSound*> ambientSounds_;
     74        typedef std::list<std::pair<AmbientSound*, bool> > AmbientList;
     75        AmbientList ambientSounds_;
    6976       
    70         float fadeStep_;       //per second
    71         std::list<std::pair<AmbientSound*, float> > fadeInList_;
    72         std::list<std::pair<AmbientSound*, float> > fadeOutList_;
     77        float crossFadeStep_;       //!< Absolute change per second (0.1 means 10% of the nominal volume) for cross fading
     78        std::list<AmbientSound*> fadeInList_;
     79        std::list<AmbientSound*> fadeOutList_;
    7380       
    74         std::string lastReqPath_;
    75 
    7681        static SoundManager* singletonPtr_s;
    7782    };
  • code/branches/sound3/src/orxonox/sound/WorldSound.cc

    r5929 r6069  
    5353    {
    5454        SUPER(WorldSound, XMLPort, xmlelement, mode);
    55         XMLPortParamExtern(WorldSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
     55        XMLPortParamExtern(WorldSound, BaseSound, this, "volume", setVolume, getVolume, xmlelement, mode);
    5656        XMLPortParamExtern(WorldSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode);
    5757        XMLPortParamExtern(WorldSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
     58        XMLPortParamExtern(WorldSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
    5859    }
    5960
     
    8889        }
    8990    }
    90 
    9191}
  • code/branches/sound3/src/orxonox/sound/WorldSound.h

    r5929 r6069  
    2626 *
    2727 */
     28
    2829#ifndef _WorldSound_H__
    2930#define _WorldSound_H__
Note: See TracChangeset for help on using the changeset viewer.