[Orxonox-commit 1485] r6203 - in code/branches/presentation2/src: libraries/core orxonox/sound
rgrieder at orxonox.net
rgrieder at orxonox.net
Wed Dec 2 16:57:24 CET 2009
Author: rgrieder
Date: 2009-12-02 16:57:24 +0100 (Wed, 02 Dec 2009)
New Revision: 6203
Added:
code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc
code/branches/presentation2/src/orxonox/sound/SoundBuffer.h
Modified:
code/branches/presentation2/src/libraries/core/Resource.h
code/branches/presentation2/src/orxonox/sound/AmbientSound.cc
code/branches/presentation2/src/orxonox/sound/BaseSound.cc
code/branches/presentation2/src/orxonox/sound/BaseSound.h
code/branches/presentation2/src/orxonox/sound/CMakeLists.txt
code/branches/presentation2/src/orxonox/sound/SoundManager.cc
code/branches/presentation2/src/orxonox/sound/SoundManager.h
Log:
Added buffer buffering: Sounds are only loaded into one buffer and then reused.
Modified: code/branches/presentation2/src/libraries/core/Resource.h
===================================================================
--- code/branches/presentation2/src/libraries/core/Resource.h 2009-12-02 15:52:42 UTC (rev 6202)
+++ code/branches/presentation2/src/libraries/core/Resource.h 2009-12-02 15:57:24 UTC (rev 6203)
@@ -85,6 +85,13 @@
const std::string& group = Resource::DEFAULT_GROUP,
bool bSearchGroupsIfNotFound = false);
+ //! Similar to open(string, string, bool), but with a fileInfo struct
+ static DataStreamPtr open(shared_ptr<ResourceInfo> fileInfo,
+ bool bSearchGroupsIfNotFound = false)
+ {
+ return open(fileInfo->filename, fileInfo->group, bSearchGroupsIfNotFound);
+ }
+
/**
@brief
Open all resources matching a given pattern (which can contain
Modified: code/branches/presentation2/src/orxonox/sound/AmbientSound.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/AmbientSound.cc 2009-12-02 15:52:42 UTC (rev 6202)
+++ code/branches/presentation2/src/orxonox/sound/AmbientSound.cc 2009-12-02 15:57:24 UTC (rev 6203)
@@ -70,7 +70,6 @@
{
if (GameMode::playsSound())
{
- COUT(3) << "Sound: " << this->getSource() << ": Playing" << std::endl;
SoundManager::getInstance().registerAmbientSound(this);
}
}
Modified: code/branches/presentation2/src/orxonox/sound/BaseSound.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/BaseSound.cc 2009-12-02 15:52:42 UTC (rev 6202)
+++ code/branches/presentation2/src/orxonox/sound/BaseSound.cc 2009-12-02 15:57:24 UTC (rev 6203)
@@ -30,20 +30,20 @@
#include <cassert>
#include <vector>
-#include <AL/alut.h>
-#include <vorbis/vorbisfile.h>
+#include <al.h>
#include "core/CoreIncludes.h"
#include "core/GameMode.h"
#include "core/Resource.h"
#include "core/XMLPort.h"
+#include "SoundBuffer.h"
#include "SoundManager.h"
namespace orxonox
{
BaseSound::BaseSound()
: audioSource_(0)
- , audioBuffer_(0)
+ , volume_(1.0)
, bLoop_(false)
, state_(Stopped)
{
@@ -81,8 +81,8 @@
this->state_ = Playing;
alSourcePlay(this->audioSource_);
- if (alGetError() != AL_NO_ERROR)
- COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
+ if (int error = alGetError())
+ COUT(2) << "Sound: Error playing sound: " << error << std::endl;
}
}
@@ -154,20 +154,18 @@
return;
}
- if (this->audioBuffer_ != 0 && alIsBuffer(this->audioBuffer_))
+ if (this->soundBuffer_ != NULL)
{
alSourceStop(this->audioSource_);
// Unload old sound first
alSourcei(this->audioSource_, AL_BUFFER, 0);
- alDeleteBuffers(1, &this->audioBuffer_);
- this->audioBuffer_ = 0;
+ this->soundBuffer_.reset();
}
this->source_ = source;
if (source_.empty())
return;
- COUT(3) << "Sound: OpenAL ALUT: loading file " << source << std::endl;
// Get DataStream from the resources
shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source);
if (fileInfo == NULL)
@@ -175,32 +173,12 @@
COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl;
return;
}
- dataStream_ = Resource::open(source);
- // Read everything into a temporary buffer
- char* buffer = new char[fileInfo->size];
- dataStream_->read(buffer, fileInfo->size);
- dataStream_->seek(0);
- this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size);
- delete[] buffer;
+ this->soundBuffer_ = SoundManager::getInstance().getSoundBuffer(fileInfo);
+ if (this->soundBuffer_ == NULL)
+ return;
- if (this->audioBuffer_ == AL_NONE)
- {
- COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
- if (source.find("ogg", 0) != std::string::npos)
- {
- COUT(2) << "Sound: Trying fallback ogg loader" << std::endl;
- this->audioBuffer_ = this->loadOggFile();
- }
-
- if (this->audioBuffer_ == AL_NONE)
- {
- COUT(2) << "Sound: fallback ogg loader failed: " << alutGetErrorString(alutGetError()) << std::endl;
- return;
- }
- }
-
- alSourcei(this->audioSource_, AL_BUFFER, this->audioBuffer_);
+ alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer());
if (alGetError() != AL_NO_ERROR)
{
COUT(2) << "Sound: OpenAL: Error loading sample file: " << source << std::endl;
@@ -216,90 +194,7 @@
if (this->isPaused())
alSourcePause(this->audioSource_);
- if (alGetError() != AL_NO_ERROR)
- COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
+ if (int error = alGetError())
+ COUT(2) << "Sound: OpenAL: Error playing sound: " << error << std::endl;
}
-
- size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource)
- {
- return static_cast<Ogre::DataStream*>(datasource)->read(ptr, size * nmemb);
- }
-
- int seekVorbis(void* datasource, ogg_int64_t offset, int whence)
- {
- Ogre::DataStream* stream = static_cast<Ogre::DataStream*>(datasource);
- int offset_beg = offset;
- if (whence == SEEK_CUR)
- offset_beg = stream->tell() + offset;
- else if (whence == SEEK_END)
- offset_beg = stream->size() + offset;
- else if (whence != SEEK_SET)
- return -1;
- stream->seek(offset_beg);
- return 0;
- }
-
- long tellVorbis(void* datasource)
- {
- return static_cast<long>(static_cast<Ogre::DataStream*>(datasource)->tell());
- }
-
- ALuint BaseSound::loadOggFile()
- {
- char inbuffer[256*1024];
- std::vector<char> outbuffer;
- outbuffer.reserve(80*1024*1024);
- OggVorbis_File vf;
- vorbis_info* vorbisInfo;
- int eof = false;
- int current_section;
- ALuint buffer;
- ALenum format;
-
- // Open file with custom streaming
- ov_callbacks vorbisCallbacks;
- vorbisCallbacks.read_func = &readVorbis;
- vorbisCallbacks.seek_func = &seekVorbis;
- vorbisCallbacks.tell_func = &tellVorbis;
- vorbisCallbacks.close_func = NULL;
-
- int ret = ov_open_callbacks(dataStream_.get(), &vf, NULL, 0, vorbisCallbacks);
- if (ret < 0)
- {
- COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
- ov_clear(&vf);
- return AL_NONE;
- }
-
- while (!eof)
- {
- long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section);
- if (ret == 0)
- {
- eof = true;
- }
- else if (ret < 0)
- {
- COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
- ov_clear(&vf);
- return AL_NONE;
- }
- else
- {
- outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + ret);
- }
- }
-
- vorbisInfo = ov_info(&vf, -1);
- if (vorbisInfo->channels == 1)
- format = AL_FORMAT_MONO16;
- else
- format = AL_FORMAT_STEREO16;
-
- alGenBuffers(1, &buffer);
- alBufferData(buffer, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate);
- ov_clear(&vf);
-
- return buffer;
- }
}
Modified: code/branches/presentation2/src/orxonox/sound/BaseSound.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/BaseSound.h 2009-12-02 15:52:42 UTC (rev 6202)
+++ code/branches/presentation2/src/orxonox/sound/BaseSound.h 2009-12-02 15:57:24 UTC (rev 6203)
@@ -32,11 +32,15 @@
#include "OrxonoxPrereqs.h"
#include <string>
+#include <boost/shared_ptr.hpp>
#include <OgreDataStream.h>
#include "core/OrxonoxClass.h"
namespace orxonox
{
+ // forward declaration
+ class SoundBuffer;
+
/**
* The BaseSound class is the base class for all sound file loader classes.
* It server as main interface to the OpenAL library.
@@ -82,7 +86,7 @@
ALuint loadOggFile();
ALuint audioSource_;
- ALuint audioBuffer_;
+ shared_ptr<SoundBuffer> soundBuffer_;
private:
enum State
Modified: code/branches/presentation2/src/orxonox/sound/CMakeLists.txt
===================================================================
--- code/branches/presentation2/src/orxonox/sound/CMakeLists.txt 2009-12-02 15:52:42 UTC (rev 6202)
+++ code/branches/presentation2/src/orxonox/sound/CMakeLists.txt 2009-12-02 15:57:24 UTC (rev 6203)
@@ -1,6 +1,7 @@
ADD_SOURCE_FILES(ORXONOX_SRC_FILES
AmbientSound.cc
BaseSound.cc
+ SoundBuffer.cc
SoundManager.cc
WorldSound.cc
)
Added: code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc (rev 0)
+++ code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc 2009-12-02 15:57:24 UTC (rev 6203)
@@ -0,0 +1,168 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Reto Grieder
+ * Erwin 'vaiursch' Herrsche
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include "SoundBuffer.h"
+
+#include <AL/alut.h>
+#include <vorbis/vorbisfile.h>
+
+#include "util/Exception.h"
+#include "util/StringUtils.h"
+#include "core/Resource.h"
+#include "sound/SoundManager.h"
+
+namespace orxonox
+{
+ SoundBuffer::SoundBuffer(shared_ptr<ResourceInfo> fileInfo)
+ : fileInfo_(fileInfo)
+ , audioBuffer_(AL_NONE)
+ {
+ if (this->fileInfo_ == NULL)
+ 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));
+ if (getLowercase(extension) == "ogg")
+ {
+ // Try ogg loader
+ this->loadOgg(dataStream);
+ }
+ else
+ {
+ // Try standard OpenAL loader
+ this->loadStandard(dataStream);
+ }
+ }
+
+ SoundBuffer::~SoundBuffer()
+ {
+ // Unregister buffer from SoundManager
+ SoundManager::getInstance().removeBuffer(this->fileInfo_);
+
+ // Destroy buffer
+ alDeleteBuffers(1, &this->audioBuffer_);
+ }
+
+ void SoundBuffer::loadStandard(DataStreamPtr dataStream)
+ {
+ // Read everything into a temporary buffer
+ char* buffer = new char[this->fileInfo_->size];
+ dataStream->read(buffer, this->fileInfo_->size);
+ dataStream->seek(0);
+
+ this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, this->fileInfo_->size);
+ delete[] buffer;
+
+ if (this->audioBuffer_ == AL_NONE)
+ ThrowException(General, "Sound: Standard file loader failed: " << alutGetErrorString(alutGetError()));
+ }
+
+ size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource)
+ {
+ return static_cast<Ogre::DataStream*>(datasource)->read(ptr, size * nmemb);
+ }
+
+ int seekVorbis(void* datasource, ogg_int64_t offset, int whence)
+ {
+ Ogre::DataStream* stream = static_cast<Ogre::DataStream*>(datasource);
+ int offset_beg = offset;
+ if (whence == SEEK_CUR)
+ offset_beg = stream->tell() + offset;
+ else if (whence == SEEK_END)
+ offset_beg = stream->size() + offset;
+ else if (whence != SEEK_SET)
+ return -1;
+ stream->seek(offset_beg);
+ return 0;
+ }
+
+ long tellVorbis(void* datasource)
+ {
+ return static_cast<long>(static_cast<Ogre::DataStream*>(datasource)->tell());
+ }
+
+ void SoundBuffer::loadOgg(DataStreamPtr dataStream)
+ {
+ char inbuffer[256*1024];
+ std::vector<char> outbuffer;
+ outbuffer.reserve(80*1024*1024);
+
+ // Open file with custom streaming
+ ov_callbacks vorbisCallbacks;
+ vorbisCallbacks.read_func = &readVorbis;
+ vorbisCallbacks.seek_func = &seekVorbis;
+ vorbisCallbacks.tell_func = &tellVorbis;
+ vorbisCallbacks.close_func = NULL;
+
+ OggVorbis_File vf;
+ int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks);
+ if (ret < 0)
+ {
+ COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
+ ov_clear(&vf);
+ ThrowException(General, "Sound: Ogg file loader failed when opening the bitstream");
+ }
+
+ int current_section;
+ int eof = false;
+ while (!eof)
+ {
+ long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section);
+ if (ret == 0)
+ {
+ eof = true;
+ }
+ else if (ret < 0)
+ {
+ COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
+ ov_clear(&vf);
+ ThrowException(General, "Sound: Ogg file loader failed when decoding the file");
+ }
+ else
+ {
+ outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + ret);
+ }
+ }
+
+ vorbis_info* vorbisInfo;
+ vorbisInfo = ov_info(&vf, -1);
+ ALenum format;
+ if (vorbisInfo->channels == 1)
+ format = AL_FORMAT_MONO16;
+ else
+ format = AL_FORMAT_STEREO16;
+
+ alGenBuffers(1, &this->audioBuffer_);
+ alBufferData(this->audioBuffer_, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate);
+ ov_clear(&vf);
+
+ if (this->audioBuffer_ == AL_NONE)
+ ThrowException(General, "Sound: Ogg file loader failed when creating the buffer.");
+ }
+}
Property changes on: code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc
___________________________________________________________________
Added: svn:eol-style
+ native
Added: code/branches/presentation2/src/orxonox/sound/SoundBuffer.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundBuffer.h (rev 0)
+++ code/branches/presentation2/src/orxonox/sound/SoundBuffer.h 2009-12-02 15:57:24 UTC (rev 6203)
@@ -0,0 +1,59 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Reto Grieder
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef _SoundBuffer_H__
+#define _SoundBuffer_H__
+
+#include "OrxonoxPrereqs.h"
+#include <boost/shared_ptr.hpp>
+
+namespace orxonox
+{
+ /*
+ @brief
+ The SoundBuffer class is to be used for sounds with position and orientation.
+ */
+ class _OrxonoxExport SoundBuffer
+ {
+ public:
+ SoundBuffer(shared_ptr<ResourceInfo> fileInfo);
+ ~SoundBuffer();
+
+ inline ALuint getBuffer()
+ { return this->audioBuffer_; }
+
+ private:
+ void loadStandard(DataStreamPtr dataStream);
+ void loadOgg(DataStreamPtr dataStream);
+
+ shared_ptr<ResourceInfo> fileInfo_;
+ ALuint audioBuffer_;
+ };
+}
+
+#endif /* _SoundBuffer_H__ */
Property changes on: code/branches/presentation2/src/orxonox/sound/SoundBuffer.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: code/branches/presentation2/src/orxonox/sound/SoundManager.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundManager.cc 2009-12-02 15:52:42 UTC (rev 6202)
+++ code/branches/presentation2/src/orxonox/sound/SoundManager.cc 2009-12-02 15:57:24 UTC (rev 6203)
@@ -37,9 +37,11 @@
#include "util/ScopeGuard.h"
#include "util/StringUtils.h"
#include "util/Clock.h"
+#include "core/ConfigValueIncludes.h"
#include "core/GameMode.h"
#include "core/ScopedSingletonManager.h"
-#include "core/ConfigValueIncludes.h"
+#include "core/Resource.h"
+#include "SoundBuffer.h"
#include "BaseSound.h"
#include "AmbientSound.h"
#include "WorldSound.h"
@@ -472,4 +474,26 @@
}
}
}
+
+ 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);
+ if (it != this->soundBuffers_.end())
+ return it->second.lock();
+ else
+ {
+ shared_ptr<SoundBuffer> buffer(new SoundBuffer(fileInfo));
+ this->soundBuffers_[fileInfo->group + '/' + fileInfo->filename] = buffer;
+ return buffer;
+ }
+ }
+
+ void SoundManager::removeBuffer(shared_ptr<ResourceInfo> fileInfo)
+ {
+ std::map<std::string, weak_ptr<SoundBuffer> >::const_iterator it
+ = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
+ if (it == this->soundBuffers_.end())
+ this->soundBuffers_.erase(it);
+ }
}
Modified: code/branches/presentation2/src/orxonox/sound/SoundManager.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundManager.h 2009-12-02 15:52:42 UTC (rev 6202)
+++ code/branches/presentation2/src/orxonox/sound/SoundManager.h 2009-12-02 15:57:24 UTC (rev 6203)
@@ -32,14 +32,18 @@
#include "OrxonoxPrereqs.h"
#include <list>
+#include <map>
#include <string>
-#include <map>
+#include <boost/weak_ptr.hpp>
+
#include "util/Singleton.h"
#include "core/OrxonoxClass.h"
// tolua_begin
namespace orxonox
{
+ // forward declaration
+ class SoundBuffer;
//! Enum for the sound type.
namespace SoundType
@@ -85,6 +89,9 @@
void toggleMute(SoundType::Value type); // tolua_export
bool getMute(SoundType::Value type); // tolua_export
+ shared_ptr<SoundBuffer> getSoundBuffer(shared_ptr<ResourceInfo> fileInfo);
+ void removeBuffer(shared_ptr<ResourceInfo> fileInfo);
+
private:
void processCrossFading(float dt);
void fadeIn(AmbientSound* sound);
@@ -117,6 +124,8 @@
float ambientVolume_;
float effectsVolume_;
std::map<SoundType::Value, bool> mute_;
+
+ std::map<std::string, weak_ptr<SoundBuffer> > soundBuffers_;
static SoundManager* singletonPtr_s;
}; // tolua_export
More information about the Orxonox-commit
mailing list