[Orxonox-commit 1536] r6254 - code/branches/presentation2/src/orxonox/sound

rgrieder at orxonox.net rgrieder at orxonox.net
Sun Dec 6 17:02:11 CET 2009


Author: rgrieder
Date: 2009-12-06 17:02:11 +0100 (Sun, 06 Dec 2009)
New Revision: 6254

Modified:
   code/branches/presentation2/src/orxonox/sound/BaseSound.cc
   code/branches/presentation2/src/orxonox/sound/BaseSound.h
   code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc
   code/branches/presentation2/src/orxonox/sound/SoundBuffer.h
   code/branches/presentation2/src/orxonox/sound/SoundManager.cc
   code/branches/presentation2/src/orxonox/sound/SoundManager.h
   code/branches/presentation2/src/orxonox/sound/WorldSound.cc
Log:
Added sound effects pooling. This should avoid long respawns (due to sound loading) if there are no more enemies.

Modified: code/branches/presentation2/src/orxonox/sound/BaseSound.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/BaseSound.cc	2009-12-06 14:22:56 UTC (rev 6253)
+++ code/branches/presentation2/src/orxonox/sound/BaseSound.cc	2009-12-06 16:02:11 UTC (rev 6254)
@@ -43,6 +43,7 @@
 {
     BaseSound::BaseSound()
         : audioSource_(0)
+        , bPooling_(false)
         , volume_(1.0)
         , bLoop_(false)
         , state_(Stopped)
@@ -165,6 +166,7 @@
             alSourceStop(this->audioSource_);
             // Unload old sound first
             alSourcei(this->audioSource_, AL_BUFFER, 0);
+            SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_);
             this->soundBuffer_.reset();
         }
 

Modified: code/branches/presentation2/src/orxonox/sound/BaseSound.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/BaseSound.h	2009-12-06 14:22:56 UTC (rev 6253)
+++ code/branches/presentation2/src/orxonox/sound/BaseSound.h	2009-12-06 16:02:11 UTC (rev 6254)
@@ -85,7 +85,8 @@
     protected:
         ALuint loadOggFile();
 
-        ALuint audioSource_;
+        ALuint          audioSource_;
+        bool            bPooling_;
         shared_ptr<SoundBuffer> soundBuffer_;
 
     private:

Modified: code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc	2009-12-06 14:22:56 UTC (rev 6253)
+++ code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc	2009-12-06 16:02:11 UTC (rev 6254)
@@ -62,13 +62,20 @@
 
     SoundBuffer::~SoundBuffer()
     {
-        // Unregister buffer from SoundManager
-        SoundManager::getInstance().removeBuffer(this->fileInfo_);
-
         // Destroy buffer
         alDeleteBuffers(1, &this->audioBuffer_);
     }
 
+    unsigned int SoundBuffer::getSize() const
+    {
+        ALint size;
+        alGetBufferi(this->audioBuffer_, AL_SIZE, &size);
+        if (!alGetError())
+            return size;
+        else
+            return 0;
+    }
+
     void SoundBuffer::loadStandard(DataStreamPtr dataStream)
     {
         // Read everything into a temporary buffer

Modified: code/branches/presentation2/src/orxonox/sound/SoundBuffer.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundBuffer.h	2009-12-06 14:22:56 UTC (rev 6253)
+++ code/branches/presentation2/src/orxonox/sound/SoundBuffer.h	2009-12-06 16:02:11 UTC (rev 6254)
@@ -40,6 +40,8 @@
     */
     class _OrxonoxExport SoundBuffer
     {
+        friend class SoundManager;
+
     public:
         SoundBuffer(shared_ptr<ResourceInfo> fileInfo);
         ~SoundBuffer();
@@ -47,12 +49,24 @@
         inline ALuint getBuffer()
             { return this->audioBuffer_; }
 
+        unsigned int getSize() const;
+
+        shared_ptr<ResourceInfo> getFileInfo() const
+            { return this->fileInfo_; }
+
+        void setPooling(bool val)
+            { this->bPooling_ = true; }
+        bool getPooling() const
+            { return this->bPooling_; }
+
     private:
         void loadStandard(DataStreamPtr dataStream);
         void loadOgg(DataStreamPtr dataStream);
 
         shared_ptr<ResourceInfo> fileInfo_;
         ALuint audioBuffer_;
+        std::list<shared_ptr<SoundBuffer> >::iterator poolIterator_;
+        bool bPooling_;
     };
 }
 

Modified: code/branches/presentation2/src/orxonox/sound/SoundManager.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundManager.cc	2009-12-06 14:22:56 UTC (rev 6253)
+++ code/branches/presentation2/src/orxonox/sound/SoundManager.cc	2009-12-06 16:02:11 UTC (rev 6254)
@@ -51,6 +51,7 @@
     ManageScopedSingleton(SoundManager, ScopeID::Graphics, true);
 
     SoundManager::SoundManager()
+        : effectsPoolSize_(0)
     {
         RegisterRootObject(SoundManager);
 
@@ -528,32 +529,68 @@
 
     shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(shared_ptr<ResourceInfo> fileInfo)
     {
-        std::map<std::string, weak_ptr<SoundBuffer> >::const_iterator it
-            = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
+        shared_ptr<SoundBuffer> buffer;
+        // Check active or pooled buffers
+        SoundBufferMap::const_iterator it = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
         if (it != this->soundBuffers_.end())
-            return it->second.lock();
+        {
+            buffer = it->second;
+
+            // Remove from effects pool if not active used before
+            if (buffer->poolIterator_ != this->effectsPool_.end())
+            {
+                this->effectsPoolSize_ -= buffer->getSize();
+                this->effectsPool_.erase(buffer->poolIterator_);
+                buffer->poolIterator_ = this->effectsPool_.end();
+            }
+        }
         else
         {
-            shared_ptr<SoundBuffer> buffer;
             try
             {
                 buffer.reset(new SoundBuffer(fileInfo));
+                buffer->poolIterator_ = this->effectsPool_.end();
             }
             catch (...)
             {
                 COUT(1) << Exception::handleMessage() << std::endl;
-                return shared_ptr<SoundBuffer>();
+                return buffer;
             }
             this->soundBuffers_[fileInfo->group + '/' + fileInfo->filename] = buffer;
-            return buffer;
         }
+        return buffer;
     }
 
-    void SoundManager::removeBuffer(shared_ptr<ResourceInfo> fileInfo)
+    void SoundManager::releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer)
     {
-        std::map<std::string, weak_ptr<SoundBuffer> >::iterator it
-            = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
+        // Check if others are still using the buffer
+        if (buffer.use_count() != 2)
+            return;
+        SoundBufferMap::iterator it = this->soundBuffers_.find(buffer->fileInfo_->group + '/' + buffer->fileInfo_->filename);
         if (it != this->soundBuffers_.end())
-            this->soundBuffers_.erase(it);
+        {
+            if (bPoolBuffer)
+            {
+                // Pool already too large?
+                while (this->effectsPoolSize_ + it->second->getSize() > this->maxEffectsPoolSize_s && !this->effectsPool_.empty())
+                {
+                    shared_ptr<SoundBuffer> bufferDel = this->effectsPool_.back();
+                    this->effectsPoolSize_ -= bufferDel->getSize();
+                    bufferDel->poolIterator_ = this->effectsPool_.end();
+                    this->effectsPool_.pop_back();
+                    // Remove from buffer map too
+                    SoundBufferMap::iterator itDel = this->soundBuffers_.find(bufferDel->fileInfo_->group + '/' + bufferDel->fileInfo_->filename);
+                    if (itDel != this->soundBuffers_.end())
+                        this->soundBuffers_.erase(itDel);
+                }
+                // Put buffer into the pool
+                this->effectsPoolSize_ += it->second->getSize();
+                this->effectsPool_.push_front(it->second);
+                it->second->poolIterator_ = this->effectsPool_.begin();
+                COUT(0) << "pool size: " << this->effectsPoolSize_ << std::endl;
+            }
+            else
+                this->soundBuffers_.erase(it);
+        }
     }
 }

Modified: code/branches/presentation2/src/orxonox/sound/SoundManager.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundManager.h	2009-12-06 14:22:56 UTC (rev 6253)
+++ code/branches/presentation2/src/orxonox/sound/SoundManager.h	2009-12-06 16:02:11 UTC (rev 6254)
@@ -35,7 +35,7 @@
 #include <list>
 #include <map>
 #include <string>
-#include <boost/weak_ptr.hpp>
+#include <boost/shared_ptr.hpp>
 
 #include "util/Singleton.h"
 #include "core/OrxonoxClass.h"
@@ -100,7 +100,7 @@
         bool getMute(SoundType::Value type); // tolua_export
 
         shared_ptr<SoundBuffer> getSoundBuffer(shared_ptr<ResourceInfo> fileInfo);
-        void removeBuffer(shared_ptr<ResourceInfo> fileInfo);
+        void releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer);
 
         static std::string getALErrorString(ALenum error);
 
@@ -138,7 +138,12 @@
         float effectsVolume_;
         std::map<SoundType::Value, bool> mute_;
 
-        std::map<std::string, weak_ptr<SoundBuffer> > soundBuffers_;
+        static const unsigned int maxEffectsPoolSize_s = 40 * 1024 * 1024;
+        unsigned int effectsPoolSize_;
+        typedef std::list<shared_ptr<SoundBuffer> > EffectsPoolList;
+        EffectsPoolList effectsPool_;
+        typedef std::map<std::string, shared_ptr<SoundBuffer> > SoundBufferMap;
+        SoundBufferMap soundBuffers_;
         
         static SoundManager* singletonPtr_s;
     }; // tolua_export

Modified: code/branches/presentation2/src/orxonox/sound/WorldSound.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/WorldSound.cc	2009-12-06 14:22:56 UTC (rev 6253)
+++ code/branches/presentation2/src/orxonox/sound/WorldSound.cc	2009-12-06 16:02:11 UTC (rev 6254)
@@ -44,6 +44,8 @@
         : StaticEntity(creator)
     {
         RegisterObject(WorldSound);
+        // WorldSound buffers should be pooled when they're not used anymore
+        this->bPooling_ = true;
     }
 
     WorldSound::~WorldSound()




More information about the Orxonox-commit mailing list