[Orxonox-commit 242] r2899 - branches/sound/src/sound
erwin at orxonox.net
erwin at orxonox.net
Mon Apr 6 15:08:09 CEST 2009
Author: erwin
Date: 2009-04-06 15:08:09 +0200 (Mon, 06 Apr 2009)
New Revision: 2899
Added:
branches/sound/src/sound/SoundBase.cc
branches/sound/src/sound/SoundManager.cc
Modified:
branches/sound/src/sound/SoundBase.h
branches/sound/src/sound/SoundManager.h
Log:
implemented SoundManager class
Added: branches/sound/src/sound/SoundBase.cc
===================================================================
--- branches/sound/src/sound/SoundBase.cc (rev 0)
+++ branches/sound/src/sound/SoundBase.cc 2009-04-06 13:08:09 UTC (rev 2899)
@@ -0,0 +1,51 @@
+/*
+ * 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:
+ * Erwin 'vaiursch' Herrsche
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include <SoundBase.h>
+#include <SoundManager.h>
+
+#include <Ogre/SceneNode.h>
+
+namespace orxonox
+{
+ SoundBase::SoundBase(Ogre::SceneNode* node)
+ {
+ this->source_ = 0;
+ this->buffer_ = 0;
+ this->node_ = node;
+
+ if(SoundBase::soundmanager_s == null)
+ SoundBase::soundmanager_s == SoundManager::instance();
+ }
+
+ void SoundBase::attachToNode(Ogre::ScenceNode* node)
+ {
+ this->node_ = node;
+ }
+
+} // namespace: orxonox
Modified: branches/sound/src/sound/SoundBase.h
===================================================================
--- branches/sound/src/sound/SoundBase.h 2009-04-06 12:34:01 UTC (rev 2898)
+++ branches/sound/src/sound/SoundBase.h 2009-04-06 13:08:09 UTC (rev 2899)
@@ -27,13 +27,18 @@
*/
#include <al.h>
+#include <string>
class Ogre::SceneNode;
class Orxonox::SoundManager;
namespace orxonox
{
-
+ /**
+ * The SoudBase class is the base class for all sound file loader classes.
+ * It server as main interface to the OpenAL library.
+ *
+ */
class SoundBase
{
public:
@@ -48,6 +53,8 @@
bool isPlaying();
+ virtual void loadFile(std::string filename) = 0;
+
private:
ALuint source_;
ALuint buffer_;
Added: branches/sound/src/sound/SoundManager.cc
===================================================================
--- branches/sound/src/sound/SoundManager.cc (rev 0)
+++ branches/sound/src/sound/SoundManager.cc 2009-04-06 13:08:09 UTC (rev 2899)
@@ -0,0 +1,129 @@
+/*
+ * 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:
+ * Erwin 'vaiursch' Herrsche
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include <OgreSceneNode.h>
+
+#include "orxonox/CameraManager.h"
+#include "SoundManager.h"
+
+namespace orxonox
+{
+ /**
+ * Static function to get the singleton instance of SoundManager.
+ *
+ * @return The singleton instance
+ */
+ SoundManager* SoundManager::instance()
+ {
+ if(SoundManager::singleton_ == NULL)
+ {
+ SoundManager::singleton_ = new SoundManager();
+ }
+
+ return SoundManager::singleton_;
+ }
+
+ /**
+ * Default constructor
+ */
+ SoundManager::SoundManager()
+ {
+ // OpenAL device and context creation
+ this->device_ = alcOpenDevice(NULL); // we operate on the default sound device
+ if(this->device_)
+ COUT(2) << "OpenAL: Could not create sound device, there will be no sound!" << std::endl;
+
+ this->context_ = alcCreateContext(this->device_, NULL);
+ alcMakeContextCurrent(this->context_);
+ ALenum error = alcGetError();
+ if(error != ALC_NO_ERROR)
+ COUT(2) << "OpenAL: Could not create sound context." << std::endl;
+
+ }
+
+ /**
+ * Add a SoundBase object to the list. Every SoundBase object should be in
+ * this list.
+ *
+ * @param sound Pointer to the SoundBase object to add
+ */
+ void SoundManager::addSound(SoundBase* sound)
+ {
+ this->soundlist_.push_back(sound);
+ }
+
+ /**
+ * Remove a SoundBase object from the list and destroy it.
+ */
+ void SoundManager::removeSound(SoundBase* sound)
+ {
+ std::list<SoundBase*>::iterator pos = this->soundlist_.end();
+ for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)
+ {
+ if((*i) == sound)
+ pos = i;
+ }
+
+ delete (*pos);
+ this->soundlist_.erase(pos);
+ }
+
+ /**
+ * Tick function, updates listener and registred SoundBase objects
+ *
+ * @param dt @see Orxonox::Tickable
+ */
+ void SoundManager::tick(float dt)
+ {
+ // update listener position
+ Camera* camera = CameraManager::getInstance().getActiveCamera();
+ Vector3 pos = camera->getPosition();
+ alListener3f(AL_POSITION, pos.x, pos.y, pos.z);
+ ALenum error = alGetError();
+ if(error != AL_INVALID_VALUE)
+ COUT(2) << "OpenAL: Invalid listener position" << std::endl;
+
+ // update listener orientation
+ Quaternion orient = camera->getOrientation();
+ Vector3 up = orient.xAxis(); // just a wild guess
+ Vecotr3 at = orient.zAxis();
+
+ float[3][2] orientation = { { at.x, at.y, at.z },
+ { up.x, up.y, up.z } };
+
+ alListenerfv(AL_POSITION, orientation);
+ error = alGetError();
+ if(error == AL_INVALID_VALUE)
+ COUT(2) << "OpenAL: Invalid listener orientation" << std::endl;
+
+ // update sounds
+ for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)
+ (*i)->update();
+ }
+
+} // namespace orxonox
Modified: branches/sound/src/sound/SoundManager.h
===================================================================
--- branches/sound/src/sound/SoundManager.h 2009-04-06 12:34:01 UTC (rev 2898)
+++ branches/sound/src/sound/SoundManager.h 2009-04-06 13:08:09 UTC (rev 2899)
@@ -28,21 +28,32 @@
#include <al.h>
#include <alc.h>
+#include <Tickable.h>
+
class SoundBase;
namespace Orxonox
{
+ /**
+ * The SoundManager class manages the OpenAL device, context and listener
+ * position. It has a list of all SoundBase objects and calls their update
+ * function every tick. It is a singleton.
+ *
+ */
class SoundManager : public Tickable
{
public:
- SoundManager();
+ static SoundManager* instance();
void addSound(SoundBase* sound);
void removeSound(SoundBase* sound);
-
+
void tick(float dt);
private:
+ SoundManager(); // private constructor -> singleton
+ static SoundManager* singleton_;
+
ALCcontext* context_;
ALCdevice* device_;
More information about the Orxonox-commit
mailing list