[Orxonox-commit 1552] r6270 - code/branches/presentation2/src/orxonox/sound

rgrieder at orxonox.net rgrieder at orxonox.net
Tue Dec 8 10:09:39 CET 2009


Author: rgrieder
Date: 2009-12-08 10:09:39 +0100 (Tue, 08 Dec 2009)
New Revision: 6270

Modified:
   code/branches/presentation2/src/orxonox/sound/BaseSound.cc
   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
Log:
Switched from ResourceInfo to std::string for the buffer identification. That should speed up BaseSound::setSource() by factor 10 if the buffer already exists.

Modified: code/branches/presentation2/src/orxonox/sound/BaseSound.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/BaseSound.cc	2009-12-08 09:07:04 UTC (rev 6269)
+++ code/branches/presentation2/src/orxonox/sound/BaseSound.cc	2009-12-08 09:09:39 UTC (rev 6270)
@@ -61,7 +61,7 @@
 
     BaseSound::~BaseSound()
     {
-        this->setSource("");
+        this->setSource(std::string());
         if (GameMode::playsSound() && alIsSource(this->audioSource_))
             alDeleteSources(1, &this->audioSource_);
     }
@@ -190,15 +190,7 @@
         if (source_.empty() || !alIsSource(this->audioSource_))
             return;
 
-        // Get DataStream from the resources
-        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source);
-        if (fileInfo == NULL)
-        {
-            COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl;
-            return;
-        }
-
-        this->soundBuffer_ = SoundManager::getInstance().getSoundBuffer(fileInfo);
+        this->soundBuffer_ = SoundManager::getInstance().getSoundBuffer(this->source_);
         if (this->soundBuffer_ == NULL)
             return;
 

Modified: code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc	2009-12-08 09:07:04 UTC (rev 6269)
+++ code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc	2009-12-08 09:09:39 UTC (rev 6270)
@@ -39,24 +39,33 @@
 
 namespace orxonox
 {
-    SoundBuffer::SoundBuffer(shared_ptr<ResourceInfo> fileInfo)
-        : fileInfo_(fileInfo)
+    SoundBuffer::SoundBuffer(const std::string& filename)
+        : filename_(filename)
         , audioBuffer_(AL_NONE)
     {
-        if (this->fileInfo_ == NULL)
+        if (this->filename_.empty())
             ThrowException(General, "SoundBuffer construction: fileInfo was NULL");
-        DataStreamPtr dataStream = Resource::open(this->fileInfo_);
 
-        std::string extension(this->fileInfo_->basename.substr(this->fileInfo_->basename.find_last_of('.') + 1));
+        // Get resource info
+        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(filename);
+        if (fileInfo == NULL)
+        {
+            COUT(2) << "Sound: Warning: Sound file '" << filename << "' not found" << std::endl;
+            return;
+        }
+        // Open data stream
+        DataStreamPtr dataStream = Resource::open(fileInfo);
+
+        std::string extension(this->filename_.substr(this->filename_.find_last_of('.') + 1));
         if (getLowercase(extension) == "ogg")
         {
             // Try ogg loader
-            this->loadOgg(dataStream);
+            this->loadOgg(fileInfo, dataStream);
         }
         else
         {
             // Try standard OpenAL loader
-            this->loadStandard(dataStream);
+            this->loadStandard(fileInfo, dataStream);
         }
     }
 
@@ -76,14 +85,14 @@
             return 0;
     }
 
-    void SoundBuffer::loadStandard(DataStreamPtr dataStream)
+    void SoundBuffer::loadStandard(const shared_ptr<ResourceInfo>& fileInfo, DataStreamPtr dataStream)
     {
         // Read everything into a temporary buffer
-        char* buffer = new char[this->fileInfo_->size];
-        dataStream->read(buffer, this->fileInfo_->size);
+        char* buffer = new char[fileInfo->size];
+        dataStream->read(buffer, fileInfo->size);
         dataStream->seek(0);
 
-        this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, this->fileInfo_->size);
+        this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size);
         delete[] buffer;
 
         if (!alIsBuffer(this->audioBuffer_))
@@ -114,7 +123,7 @@
         return static_cast<long>(static_cast<Ogre::DataStream*>(datasource)->tell());
     }
 
-    void SoundBuffer::loadOgg(DataStreamPtr dataStream)
+    void SoundBuffer::loadOgg(const shared_ptr<ResourceInfo>& fileInfo, DataStreamPtr dataStream)
     {
         char inbuffer[256*1024];
         std::vector<char> outbuffer;

Modified: code/branches/presentation2/src/orxonox/sound/SoundBuffer.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundBuffer.h	2009-12-08 09:07:04 UTC (rev 6269)
+++ code/branches/presentation2/src/orxonox/sound/SoundBuffer.h	2009-12-08 09:09:39 UTC (rev 6270)
@@ -43,7 +43,7 @@
         friend class SoundManager;
 
     public:
-        SoundBuffer(shared_ptr<ResourceInfo> fileInfo);
+        SoundBuffer(const std::string& filename);
         ~SoundBuffer();
 
         inline ALuint getBuffer()
@@ -51,8 +51,8 @@
 
         unsigned int getSize() const;
 
-        shared_ptr<ResourceInfo> getFileInfo() const
-            { return this->fileInfo_; }
+        const std::string& getFilename() const
+            { return this->filename_; }
 
         void setPooling(bool val)
             { this->bPooling_ = true; }
@@ -60,10 +60,10 @@
             { return this->bPooling_; }
 
     private:
-        void loadStandard(DataStreamPtr dataStream);
-        void loadOgg(DataStreamPtr dataStream);
+        void loadStandard(const shared_ptr<ResourceInfo>& fileInfo, DataStreamPtr dataStream);
+        void loadOgg(const shared_ptr<ResourceInfo>& fileInfo, DataStreamPtr dataStream);
 
-        shared_ptr<ResourceInfo> fileInfo_;
+        std::string filename_;
         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-08 09:07:04 UTC (rev 6269)
+++ code/branches/presentation2/src/orxonox/sound/SoundManager.cc	2009-12-08 09:09:39 UTC (rev 6270)
@@ -527,11 +527,11 @@
         }
     }
 
-    shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(shared_ptr<ResourceInfo> fileInfo)
+    shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(const std::string& filename)
     {
         shared_ptr<SoundBuffer> buffer;
         // Check active or pooled buffers
-        SoundBufferMap::const_iterator it = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
+        SoundBufferMap::const_iterator it = this->soundBuffers_.find(filename);
         if (it != this->soundBuffers_.end())
         {
             buffer = it->second;
@@ -548,7 +548,7 @@
         {
             try
             {
-                buffer.reset(new SoundBuffer(fileInfo));
+                buffer.reset(new SoundBuffer(filename));
                 buffer->poolIterator_ = this->effectsPool_.end();
             }
             catch (...)
@@ -556,7 +556,7 @@
                 COUT(1) << Exception::handleMessage() << std::endl;
                 return buffer;
             }
-            this->soundBuffers_[fileInfo->group + '/' + fileInfo->filename] = buffer;
+            this->soundBuffers_[filename] = buffer;
         }
         return buffer;
     }
@@ -566,7 +566,7 @@
         // 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);
+        SoundBufferMap::iterator it = this->soundBuffers_.find(buffer->getFilename());
         if (it != this->soundBuffers_.end())
         {
             if (bPoolBuffer)
@@ -579,7 +579,7 @@
                     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);
+                    SoundBufferMap::iterator itDel = this->soundBuffers_.find(bufferDel->getFilename());
                     if (itDel != this->soundBuffers_.end())
                         this->soundBuffers_.erase(itDel);
                 }

Modified: code/branches/presentation2/src/orxonox/sound/SoundManager.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundManager.h	2009-12-08 09:07:04 UTC (rev 6269)
+++ code/branches/presentation2/src/orxonox/sound/SoundManager.h	2009-12-08 09:09:39 UTC (rev 6270)
@@ -99,7 +99,7 @@
         void toggleMute(SoundType::Value type); // tolua_export
         bool getMute(SoundType::Value type); // tolua_export
 
-        shared_ptr<SoundBuffer> getSoundBuffer(shared_ptr<ResourceInfo> fileInfo);
+        shared_ptr<SoundBuffer> getSoundBuffer(const std::string& filename);
         void releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer);
 
         static std::string getALErrorString(ALenum error);




More information about the Orxonox-commit mailing list