Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6071


Ignore:
Timestamp:
Nov 15, 2009, 10:36:45 PM (15 years ago)
Author:
rgrieder
Message:

Improved synchronisability of the sound classes (not yet Synchronisable though!).

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

Legend:

Unmodified
Added
Removed
  • code/branches/sound3/src/orxonox/gamestates/GSMainMenu.cc

    r6031 r6071  
    102102        if (GameMode::playsSound())
    103103        {
    104             this->ambient_->setLoop(true);
    105                         this->ambient_->setPlayOnLoad(true);
     104            this->ambient_->setLooping(true);
     105            this->ambient_->play(); // works without source
    106106        }
    107107
     
    136136                if (GameMode::playsSound())
    137137        {
    138                         this->ambient_->setSource(soundPathMain_);
     138            this->ambient_->setAmbientSource(soundPathMain_);
    139139                }
    140140        }
  • code/branches/sound3/src/orxonox/sound/AmbientSound.cc

    r6069 r6071  
    3232#include "core/EventIncludes.h"
    3333#include "core/GameMode.h"
     34#include "core/Resource.h"
    3435#include "core/XMLPort.h"
    3536#include "SoundManager.h"
     37#include "MoodManager.h"
    3638
    3739namespace orxonox
     
    5658        SUPER(AmbientSound, XMLPort, xmlelement, mode);
    5759        XMLPortParamExtern(AmbientSound, BaseSound, this, "volume", setVolume, getVolume, xmlelement, mode);
    58         XMLPortParamExtern(AmbientSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode);
    59         XMLPortParamExtern(AmbientSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
    60         XMLPortParamExtern(AmbientSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
     60        XMLPortParamExtern(AmbientSound, BaseSound, this, "loop", setLooping, getLooping, xmlelement, mode);
     61        XMLPortParamExtern(AmbientSound, BaseSound, this, "play", play, isPlaying, xmlelement, mode);
     62        XMLPortParam(AmbientSound, "source", setAmbientSource, getAmbientSource, xmlelement, mode);
    6163    }
    6264
     
    107109    }
    108110
    109     void AmbientSound::setSource(const std::string& source)
     111    void AmbientSound::setAmbientSource(const std::string& source)
    110112    {
     113        this->ambientSource_ = source;
    111114        if (GameMode::playsSound())
    112115        {
    113             std::string filePath = SoundManager::getInstance().getAmbientPath(source);
    114             if (!filePath.empty())
    115             {
    116                 BaseSound::setSource(filePath);
    117                 return;
    118             }
    119             COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl;       
     116            std::string path = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
     117            shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
     118            if (fileInfo != NULL)
     119                this->setSource(path);
     120            else
     121                COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl;       
    120122        }
    121123    }
  • code/branches/sound3/src/orxonox/sound/AmbientSound.h

    r6069 r6071  
    5959        virtual void pause();
    6060
    61         virtual void setSource(const std::string& source);
     61        virtual void setAmbientSource(const std::string& source);
     62        const std::string& getAmbientSource() const { return this->ambientSource_; }
    6263
    6364    private:
    64         void doPlay();      // Continue playing without re-registering the sound
     65        void doPlay();
    6566        void doStop();
    6667        void doPause();
     68
     69        std::string ambientSource_; //!< Analogous to source_, but mood independent
    6770    };
    6871}
  • code/branches/sound3/src/orxonox/sound/BaseSound.cc

    r6070 r6071  
    2929#include "BaseSound.h"
    3030
     31#include <cassert>
    3132#include <vector>
    3233#include <AL/alut.h>
     
    4243        : audioSource_(0)
    4344        , audioBuffer_(0)
    44         , bPlayOnLoad_(false)
    4545        , bLoop_(false)
     46        , state_(Stopped)
    4647    {
    4748        RegisterRootObject(BaseSound);
    4849
    4950        if (GameMode::playsSound())
     51        {
    5052            alGenSources(1, &this->audioSource_);
     53            assert(this->audioSource_ != 0);
     54        }
    5155    }
    5256
     
    5458    {
    5559        this->setSource("");
    56         if (this->audioSource_)
     60        if (GameMode::playsSound())
    5761            alDeleteSources(1, &this->audioSource_);
    5862    }
     
    6064    void BaseSound::play()
    6165    {
    62         if (alIsSource(this->audioSource_))
    63         {
    64             if (this->bLoop_)
    65                 alSourcei(this->audioSource_, AL_LOOPING, AL_TRUE);
    66             else
    67                 alSourcei(this->audioSource_, AL_LOOPING, AL_FALSE);
     66        if (!this->isPlaying() && GameMode::showsGraphics())
     67        {
     68            this->state_ = Playing;
    6869            alSourcePlay(this->audioSource_);
    6970
    7071            if (alGetError() != AL_NO_ERROR)
    71             {
    7272                 COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
    73             }
    7473        }
    7574    }
     
    7776    void BaseSound::stop()
    7877    {
    79         if (alIsSource(this->audioSource_))
     78        this->state_ = Stopped;
     79        if (GameMode::playsSound())
    8080            alSourceStop(this->audioSource_);
    8181    }
     
    8383    void BaseSound::pause()
    8484    {
    85         if (alIsSource(this->audioSource_))
     85        if (this->isStopped())
     86            return;
     87        this->state_ = Paused;
     88        if (GameMode::playsSound())
    8689            alSourcePause(this->audioSource_);
    87     }
    88 
    89     bool BaseSound::isPlaying()
    90     {
    91         if (alIsSource(this->audioSource_))
    92             return getSourceState() == AL_PLAYING;
    93         return false;
    94     }
    95 
    96     bool BaseSound::isPaused()
    97     {
    98         if (alIsSource(this->audioSource_))
    99             return getSourceState() == AL_PAUSED;
    100         return false;
    101     }
    102 
    103     bool BaseSound::isStopped()
    104     {
    105         if (alIsSource(this->audioSource_))
    106             return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
    107         return true;
    10890    }
    10991
     
    121103    }
    122104
     105    void BaseSound::setLooping(bool val)
     106    {
     107        this->bLoop_ = val;
     108        if (GameMode::playsSound())
     109            alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE));
     110    }
     111
    123112    void BaseSound::setSource(const std::string& source)
    124113    {
     
    128117            return;
    129118        }
    130        
    131         if (alIsSource(this->audioBuffer_))
    132         {
    133             this->stop();
     119
     120        if (this->audioBuffer_ != 0 && alIsBuffer(this->audioBuffer_))
     121        {
     122            alSourceStop(this->audioSource_);
    134123            // Unload old sound first
    135124            alSourcei(this->audioSource_, AL_BUFFER, 0);
     
    183172
    184173        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
    185 
    186         this->setVolume(this->volume_);
    187 
    188         if (this->bPlayOnLoad_)
    189             this->play();
    190     }
    191 
    192     ALint BaseSound::getSourceState()
    193     {
    194         ALint state;
    195         alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state);
    196         return state;
     174        alSourcef (this->audioSource_, AL_GAIN, this->volume_);
     175        alSourcei (this->audioSource_, AL_LOOPING, (this->bLoop_ ? AL_TRUE : AL_FALSE));
     176        if (this->isPlaying() || this->isPaused())
     177            alSourcePlay(this->audioSource_);
     178        if (this->isPaused())
     179            alSourcePause(this->audioSource_);
     180
     181        if (alGetError() != AL_NO_ERROR)
     182            COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
    197183    }
    198184
  • code/branches/sound3/src/orxonox/sound/BaseSound.h

    r6069 r6071  
    5353        virtual void pause();
    5454
    55         bool isPlaying();
    56         bool isPaused();
    57         bool isStopped();
     55        bool isPlaying() { return this->state_ = Playing; }
     56        bool isPaused()  { return this->state_ = Paused; }
     57        bool isStopped() { return this->state_ = Stopped; }
    5858
    5959        virtual void setSource(const std::string& source);
     
    6363        float getVolume() const { return this->volume_; }
    6464
    65         bool getPlayOnLoad() const   { return this->bPlayOnLoad_; }
    66         void setPlayOnLoad(bool val) { this->bPlayOnLoad_ = val; }
    67 
    68         bool getLoop() const   { return this->bLoop_; }
    69         void setLoop(bool val) { this->bLoop_ = val; }
     65        bool getLooping() const   { return this->bLoop_; }
     66        void setLooping(bool val);
    7067
    7168        //ALuint getALAudioSource(void);
     
    7370    protected:
    7471        ALuint loadOggFile();
    75         ALint getSourceState();
    7672
    7773        ALuint audioSource_;
     
    7975
    8076    private:
     77        enum State
     78        {
     79            Stopped,
     80            Playing,
     81            Paused
     82        };
     83
    8184        std::string     source_;
    8285        float           volume_;
    83         bool            bPlayOnLoad_;
    8486        bool            bLoop_;
     87        State           state_;
    8588        DataStreamPtr   dataStream_;
    8689    };
  • code/branches/sound3/src/orxonox/sound/SoundManager.cc

    r6069 r6071  
    4040#include "core/GameMode.h"
    4141#include "core/ScopedSingletonManager.h"
    42 #include "core/Resource.h"
    4342#include "core/ConfigValueIncludes.h"
    4443#include "BaseSound.h"
    45 #include "MoodManager.h"
    4644#include "AmbientSound.h"
    4745
     
    223221    }
    224222
    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 
    237223    void SoundManager::fadeIn(AmbientSound* sound)
    238224    {
  • code/branches/sound3/src/orxonox/sound/SoundManager.h

    r6069 r6071  
    6060        void unregisterAmbientSound(AmbientSound* oldAmbient);
    6161        void pauseAmbientSound(AmbientSound* ambient);
    62         std::string getAmbientPath(const std::string& source);
    6362
    6463    private:
  • code/branches/sound3/src/orxonox/sound/WorldSound.cc

    r6069 r6071  
    5454        SUPER(WorldSound, XMLPort, xmlelement, mode);
    5555        XMLPortParamExtern(WorldSound, BaseSound, this, "volume", setVolume, getVolume, xmlelement, mode);
    56         XMLPortParamExtern(WorldSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode);
    57         XMLPortParamExtern(WorldSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
     56        XMLPortParamExtern(WorldSound, BaseSound, this, "loop", setLooping, getLooping, xmlelement, mode);
     57        XMLPortParamExtern(WorldSound, BaseSound, this, "play", play, isPlaying, xmlelement, mode);
    5858        XMLPortParamExtern(WorldSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
    5959    }
Note: See TracChangeset for help on using the changeset viewer.