Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 6, 2009, 5:02:11 PM (15 years ago)
Author:
rgrieder
Message:

Added sound effects pooling. This should avoid long respawns (due to sound loading) if there are no more enemies.

Location:
code/branches/presentation2/src/orxonox/sound
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2/src/orxonox/sound/BaseSound.cc

    r6234 r6254  
    4444    BaseSound::BaseSound()
    4545        : audioSource_(0)
     46        , bPooling_(false)
    4647        , volume_(1.0)
    4748        , bLoop_(false)
     
    166167            // Unload old sound first
    167168            alSourcei(this->audioSource_, AL_BUFFER, 0);
     169            SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_);
    168170            this->soundBuffer_.reset();
    169171        }
  • code/branches/presentation2/src/orxonox/sound/BaseSound.h

    r6231 r6254  
    8686        ALuint loadOggFile();
    8787
    88         ALuint audioSource_;
     88        ALuint          audioSource_;
     89        bool            bPooling_;
    8990        shared_ptr<SoundBuffer> soundBuffer_;
    9091
  • code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc

    r6232 r6254  
    6363    SoundBuffer::~SoundBuffer()
    6464    {
    65         // Unregister buffer from SoundManager
    66         SoundManager::getInstance().removeBuffer(this->fileInfo_);
    67 
    6865        // Destroy buffer
    6966        alDeleteBuffers(1, &this->audioBuffer_);
     67    }
     68
     69    unsigned int SoundBuffer::getSize() const
     70    {
     71        ALint size;
     72        alGetBufferi(this->audioBuffer_, AL_SIZE, &size);
     73        if (!alGetError())
     74            return size;
     75        else
     76            return 0;
    7077    }
    7178
  • code/branches/presentation2/src/orxonox/sound/SoundBuffer.h

    r6203 r6254  
    4141    class _OrxonoxExport SoundBuffer
    4242    {
     43        friend class SoundManager;
     44
    4345    public:
    4446        SoundBuffer(shared_ptr<ResourceInfo> fileInfo);
     
    4850            { return this->audioBuffer_; }
    4951
     52        unsigned int getSize() const;
     53
     54        shared_ptr<ResourceInfo> getFileInfo() const
     55            { return this->fileInfo_; }
     56
     57        void setPooling(bool val)
     58            { this->bPooling_ = true; }
     59        bool getPooling() const
     60            { return this->bPooling_; }
     61
    5062    private:
    5163        void loadStandard(DataStreamPtr dataStream);
     
    5466        shared_ptr<ResourceInfo> fileInfo_;
    5567        ALuint audioBuffer_;
     68        std::list<shared_ptr<SoundBuffer> >::iterator poolIterator_;
     69        bool bPooling_;
    5670    };
    5771}
  • code/branches/presentation2/src/orxonox/sound/SoundManager.cc

    r6244 r6254  
    5252
    5353    SoundManager::SoundManager()
     54        : effectsPoolSize_(0)
    5455    {
    5556        RegisterRootObject(SoundManager);
     
    529530    shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(shared_ptr<ResourceInfo> fileInfo)
    530531    {
    531         std::map<std::string, weak_ptr<SoundBuffer> >::const_iterator it
    532             = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
     532        shared_ptr<SoundBuffer> buffer;
     533        // Check active or pooled buffers
     534        SoundBufferMap::const_iterator it = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
    533535        if (it != this->soundBuffers_.end())
    534             return it->second.lock();
     536        {
     537            buffer = it->second;
     538
     539            // Remove from effects pool if not active used before
     540            if (buffer->poolIterator_ != this->effectsPool_.end())
     541            {
     542                this->effectsPoolSize_ -= buffer->getSize();
     543                this->effectsPool_.erase(buffer->poolIterator_);
     544                buffer->poolIterator_ = this->effectsPool_.end();
     545            }
     546        }
    535547        else
    536548        {
    537             shared_ptr<SoundBuffer> buffer;
    538549            try
    539550            {
    540551                buffer.reset(new SoundBuffer(fileInfo));
     552                buffer->poolIterator_ = this->effectsPool_.end();
    541553            }
    542554            catch (...)
    543555            {
    544556                COUT(1) << Exception::handleMessage() << std::endl;
    545                 return shared_ptr<SoundBuffer>();
     557                return buffer;
    546558            }
    547559            this->soundBuffers_[fileInfo->group + '/' + fileInfo->filename] = buffer;
    548             return buffer;
    549         }
    550     }
    551 
    552     void SoundManager::removeBuffer(shared_ptr<ResourceInfo> fileInfo)
    553     {
    554         std::map<std::string, weak_ptr<SoundBuffer> >::iterator it
    555             = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
     560        }
     561        return buffer;
     562    }
     563
     564    void SoundManager::releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer)
     565    {
     566        // Check if others are still using the buffer
     567        if (buffer.use_count() != 2)
     568            return;
     569        SoundBufferMap::iterator it = this->soundBuffers_.find(buffer->fileInfo_->group + '/' + buffer->fileInfo_->filename);
    556570        if (it != this->soundBuffers_.end())
    557             this->soundBuffers_.erase(it);
     571        {
     572            if (bPoolBuffer)
     573            {
     574                // Pool already too large?
     575                while (this->effectsPoolSize_ + it->second->getSize() > this->maxEffectsPoolSize_s && !this->effectsPool_.empty())
     576                {
     577                    shared_ptr<SoundBuffer> bufferDel = this->effectsPool_.back();
     578                    this->effectsPoolSize_ -= bufferDel->getSize();
     579                    bufferDel->poolIterator_ = this->effectsPool_.end();
     580                    this->effectsPool_.pop_back();
     581                    // Remove from buffer map too
     582                    SoundBufferMap::iterator itDel = this->soundBuffers_.find(bufferDel->fileInfo_->group + '/' + bufferDel->fileInfo_->filename);
     583                    if (itDel != this->soundBuffers_.end())
     584                        this->soundBuffers_.erase(itDel);
     585                }
     586                // Put buffer into the pool
     587                this->effectsPoolSize_ += it->second->getSize();
     588                this->effectsPool_.push_front(it->second);
     589                it->second->poolIterator_ = this->effectsPool_.begin();
     590                COUT(0) << "pool size: " << this->effectsPoolSize_ << std::endl;
     591            }
     592            else
     593                this->soundBuffers_.erase(it);
     594        }
    558595    }
    559596}
  • code/branches/presentation2/src/orxonox/sound/SoundManager.h

    r6244 r6254  
    3636#include <map>
    3737#include <string>
    38 #include <boost/weak_ptr.hpp>
     38#include <boost/shared_ptr.hpp>
    3939
    4040#include "util/Singleton.h"
     
    101101
    102102        shared_ptr<SoundBuffer> getSoundBuffer(shared_ptr<ResourceInfo> fileInfo);
    103         void removeBuffer(shared_ptr<ResourceInfo> fileInfo);
     103        void releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer);
    104104
    105105        static std::string getALErrorString(ALenum error);
     
    139139        std::map<SoundType::Value, bool> mute_;
    140140
    141         std::map<std::string, weak_ptr<SoundBuffer> > soundBuffers_;
     141        static const unsigned int maxEffectsPoolSize_s = 40 * 1024 * 1024;
     142        unsigned int effectsPoolSize_;
     143        typedef std::list<shared_ptr<SoundBuffer> > EffectsPoolList;
     144        EffectsPoolList effectsPool_;
     145        typedef std::map<std::string, shared_ptr<SoundBuffer> > SoundBufferMap;
     146        SoundBufferMap soundBuffers_;
    142147       
    143148        static SoundManager* singletonPtr_s;
  • code/branches/presentation2/src/orxonox/sound/WorldSound.cc

    r6204 r6254  
    4545    {
    4646        RegisterObject(WorldSound);
     47        // WorldSound buffers should be pooled when they're not used anymore
     48        this->bPooling_ = true;
    4749    }
    4850
Note: See TracChangeset for help on using the changeset viewer.