[Orxonox-commit 296] r2931 - branches/sound/src/sound

erwin at orxonox.net erwin at orxonox.net
Mon Apr 27 14:41:51 CEST 2009


Author: erwin
Date: 2009-04-27 14:41:50 +0200 (Mon, 27 Apr 2009)
New Revision: 2931

Modified:
   branches/sound/src/sound/SoundBase.cc
   branches/sound/src/sound/SoundBase.h
   branches/sound/src/sound/SoundManager.cc
Log:
Sound: SoundBase complete

Modified: branches/sound/src/sound/SoundBase.cc
===================================================================
--- branches/sound/src/sound/SoundBase.cc	2009-04-27 11:18:13 UTC (rev 2930)
+++ branches/sound/src/sound/SoundBase.cc	2009-04-27 12:41:50 UTC (rev 2931)
@@ -26,26 +26,95 @@
  *
  */
 
-#include <SoundBase.h>
-#include <SoundManager.h>
+#include "orxonox/objects/worldentities/WorldEntity.h"
+#include "util/Math.h"
+#include "SoundManager.h"
+#include "SoundBase.h"
 
-#include <Ogre/SceneNode.h>
-
 namespace orxonox 
 {
-    SoundBase::SoundBase(Ogre::SceneNode* node)
+    SoundBase::SoundBase(WorldEntity* entity)
     {
         this->source_ = 0;
         this->buffer_ = 0;
-        this->node_ = node;
+        this->entity_ = entity;
 
-        if(SoundBase::soundmanager_s == null)
-            SoundBase::soundmanager_s == SoundManager::instance();
+        if(SoundBase::soundmanager_s == NULL)
+            SoundBase::soundmanager_s = SoundManager::instance();
     }
 
-    void SoundBase::attachToNode(Ogre::ScenceNode* node)
+    void SoundBase::attachToEntity(WorldEntity* entity)
     {
-        this->node_ = node;
+        this->entity_ = entity;
+        this->update();
     }
 
+    void SoundBase::update() {
+        if(alIsSource(this->source_)) {
+            Vector3 pos = this->entity_->getPosition();
+            alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z);
+            ALenum error = alGetError();
+            if(error == AL_INVALID_VALUE)
+                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
+
+            Vector3 vel = this->entity_->getVelocity();
+            alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z);
+            error = alGetError();
+            if(error == AL_INVALID_VALUE)
+                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
+
+            Quaternion orient = this->entity_->getOrientation();
+            Vector3 at = orient.zAxis();
+            alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z);
+            error = alGetError();
+            if(error == AL_INVALID_VALUE)
+                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
+        }
+    }
+
+    void SoundBase::play(bool loop) {
+        if(alIsSource(this->source_)) {
+            if(loop)
+                alSourcei(this->source_, AL_LOOPING, AL_TRUE);
+            else
+                alSourcei(this->source_, AL_LOOPING, AL_FALSE);
+            alSourcePlay(this->source_);
+        }
+    }
+
+    void SoundBase::stop() {
+        if(alIsSource(this->source_)) {
+            alSourceStop(this->source_);
+        }
+    }
+
+    void SoundBase::pause() {
+        if(alIsSource(this->source_)) {
+            alSourcePause(this->source_);
+        }
+    }
+
+    bool SoundBase::isPlaying() {
+        if(alIsSource(this->source_)) {
+            return getSourceState() == AL_PLAYING;
+        }
+    }
+
+    bool SoundBase::isPaused() {
+        if(alIsSource(this->source_)) {
+            return getSourceState() == AL_PAUSED;
+        }
+    }
+
+    bool SoundBase::isStopped() {
+        if(alIsSource(this->source_)) {
+            return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
+        }
+    }
+
+    ALint SoundBase::getSourceState() {
+        ALint state;
+        alGetSourcei(this->source_, AL_SOURCE_STATE, &state);
+        return state;
+    }
 } // namespace: orxonox

Modified: branches/sound/src/sound/SoundBase.h
===================================================================
--- branches/sound/src/sound/SoundBase.h	2009-04-27 11:18:13 UTC (rev 2930)
+++ branches/sound/src/sound/SoundBase.h	2009-04-27 12:41:50 UTC (rev 2931)
@@ -31,11 +31,10 @@
 #include <al.h>
 #include <string>
 
-class Ogre::SceneNode;
-class Orxonox::SoundManager;
-
 namespace orxonox
 {
+    class SoundManager;
+    class WorldEntity;
     /**
      * The SoudBase class is the base class for all sound file loader classes.
      * It server as main interface to the OpenAL library.
@@ -44,25 +43,29 @@
     class SoundBase
     {
     public:
-        SoundBase(Ogre::SceneNode* node);
+        SoundBase(WorldEntity* entity);
         ~SoundBase();
 
-        void attachToNode(Ogre::SceneNode* node);
+        void attachToEntity(WorldEntity* entity);
         void update();
         void play(bool loop);
         void stop();
         void pause();
 
         bool isPlaying();
+        bool isPaused();
+        bool isStopped();
 
         virtual void loadFile(std::string filename) = 0;
 
     private:
         ALuint source_;
         ALuint buffer_;
-        Ogre::SceneNode* node_;
+        WorldEntity* entity_;
 
         static SoundManager* soundmanager_s;
+
+        ALint getSourceState();
     }; // class SoundBase
 } // namepsace orxonox
 

Modified: branches/sound/src/sound/SoundManager.cc
===================================================================
--- branches/sound/src/sound/SoundManager.cc	2009-04-27 11:18:13 UTC (rev 2930)
+++ branches/sound/src/sound/SoundManager.cc	2009-04-27 12:41:50 UTC (rev 2931)
@@ -31,6 +31,7 @@
 #include "orxonox/CameraManager.h"
 #include "orxonox/objects/worldentities/Camera.h"
 #include "util/Math.h"
+#include "SoundBase.h"
 #include "SoundManager.h"
 
 namespace orxonox
@@ -107,7 +108,7 @@
         Vector3 pos = camera->getPosition();
         alListener3f(AL_POSITION, pos.x, pos.y, pos.z);
         ALenum error = alGetError();
-        if(error != AL_INVALID_VALUE)
+        if(error == AL_INVALID_VALUE)
             COUT(2) << "OpenAL: Invalid listener position" << std::endl;
 
         // update listener orientation
@@ -115,7 +116,7 @@
         Vector3 up = orient.xAxis(); // just a wild guess
         Vector3 at = orient.zAxis();
 
-        ALfloat* orientation = { at.x, at.y, at.z, 
+        ALfloat orientation[6] = { at.x, at.y, at.z, 
                                  up.x, up.y, up.z };
 
         alListenerfv(AL_POSITION, orientation);




More information about the Orxonox-commit mailing list