[Orxonox-commit 1175] r5896 - in code/branches/core5: data/levels src/orxonox src/orxonox/controllers src/orxonox/gamestates src/orxonox/sound

rgrieder at orxonox.net rgrieder at orxonox.net
Tue Oct 6 23:12:01 CEST 2009


Author: rgrieder
Date: 2009-10-06 23:12:01 +0200 (Tue, 06 Oct 2009)
New Revision: 5896

Added:
   code/branches/core5/src/orxonox/sound/AmbientSound.cc
   code/branches/core5/src/orxonox/sound/AmbientSound.h
   code/branches/core5/src/orxonox/sound/BaseSound.cc
   code/branches/core5/src/orxonox/sound/BaseSound.h
   code/branches/core5/src/orxonox/sound/WorldSound.cc
   code/branches/core5/src/orxonox/sound/WorldSound.h
Removed:
   code/branches/core5/src/orxonox/sound/SoundBase.cc
   code/branches/core5/src/orxonox/sound/SoundBase.h
Modified:
   code/branches/core5/data/levels/presentation_pong.oxw
   code/branches/core5/src/orxonox/OrxonoxPrereqs.h
   code/branches/core5/src/orxonox/controllers/HumanController.cc
   code/branches/core5/src/orxonox/controllers/HumanController.h
   code/branches/core5/src/orxonox/gamestates/GSMainMenu.cc
   code/branches/core5/src/orxonox/gamestates/GSMainMenu.h
   code/branches/core5/src/orxonox/sound/CMakeLists.txt
   code/branches/core5/src/orxonox/sound/SoundManager.cc
   code/branches/core5/src/orxonox/sound/SoundManager.h
Log:
Added some basic sound classes: WorldSound (to be used for 3D sound, is a WorldEntity) and AmbientSound (BaseObject, just add it somewhere and it will play with playOnLoad=true).
Also moved the sound listener device updating to the HumanController. We might want to modify this again so that the listener is at the camera position again, not at the space ship.


Modified: code/branches/core5/data/levels/presentation_pong.oxw
===================================================================
--- code/branches/core5/data/levels/presentation_pong.oxw	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/data/levels/presentation_pong.oxw	2009-10-06 21:12:01 UTC (rev 5896)
@@ -45,6 +45,8 @@
  description  = "A simple testlevel"
  gametype     = Pong
 >
+  <AmbientSound soundFile="ambient/mainmenu.wav" playOnLoad=true />
+
   <Scene
    ambientlight = "0.5, 0.5, 0.5"
    skybox       = "Orxonox/skypanoramagen1"
@@ -82,6 +84,14 @@
           <ParticleSpawner name=scoreeffect_left position="-120,0,-30" source="Orxonox/BigExplosion1part2" lifetime=3.0 autostart=0 />
           <ParticleSpawner name=scoreeffect_left position="-120,0,-45" source="Orxonox/sparks2" lifetime=0.1 autostart=0 />
 
+          <WorldSound name="scoreSound" position="0,0,0" soundFile="sounds/pong_score.wav" >
+            <events>
+              <play>
+                <EventListener event=pongcenter />
+              </play>
+            </events>
+          </WorldSound>
+
 <?lua
 for i = 1, 15, 1
 do ?>

Modified: code/branches/core5/src/orxonox/OrxonoxPrereqs.h
===================================================================
--- code/branches/core5/src/orxonox/OrxonoxPrereqs.h	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/src/orxonox/OrxonoxPrereqs.h	2009-10-06 21:12:01 UTC (rev 5896)
@@ -149,9 +149,10 @@
     class Jump;
 
     //sound
-    class SoundBase;
+    class AmbientSound;
+    class BaseSound;
     class SoundManager;
-    class SoundMainMenu;
+    class WorldSound;
 
     // weaponsystem
     class DefaultWeaponmodeLink;

Modified: code/branches/core5/src/orxonox/controllers/HumanController.cc
===================================================================
--- code/branches/core5/src/orxonox/controllers/HumanController.cc	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/src/orxonox/controllers/HumanController.cc	2009-10-06 21:12:01 UTC (rev 5896)
@@ -35,6 +35,7 @@
 #include "gametypes/Gametype.h"
 #include "infos/PlayerInfo.h"
 #include "overlays/Map.h"
+#include "sound/SoundManager.h"
 #include "Radar.h"
 #include "Scene.h"
 
@@ -76,6 +77,19 @@
         HumanController::localController_s = 0;
     }
 
+    void HumanController::tick(float dt)
+    {
+        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
+        {
+            if (GameMode::playsSound())
+            {
+                // Update sound listener
+                SoundManager::getInstance().setListenerPosition(HumanController::localController_s->controllableEntity_->getPosition());
+                SoundManager::getInstance().setListenerOrientation(HumanController::localController_s->controllableEntity_->getOrientation());
+            }
+        }
+    }
+
     void HumanController::moveFrontBack(const Vector2& value)
     {
         if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)

Modified: code/branches/core5/src/orxonox/controllers/HumanController.h
===================================================================
--- code/branches/core5/src/orxonox/controllers/HumanController.h	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/src/orxonox/controllers/HumanController.h	2009-10-06 21:12:01 UTC (rev 5896)
@@ -30,16 +30,20 @@
 #define _HumanController_H__
 
 #include "OrxonoxPrereqs.h"
+
+#include "tools/interfaces/Tickable.h"
 #include "Controller.h"
 
 namespace orxonox
 {
-    class _OrxonoxExport HumanController : public Controller
+    class _OrxonoxExport HumanController : public Controller, public Tickable
     {
         public:
             HumanController(BaseObject* creator);
             virtual ~HumanController();
 
+            virtual void tick(float dt);
+
             static void moveFrontBack(const Vector2& value);
             static void moveRightLeft(const Vector2& value);
             static void moveUpDown(const Vector2& value);

Modified: code/branches/core5/src/orxonox/gamestates/GSMainMenu.cc
===================================================================
--- code/branches/core5/src/orxonox/gamestates/GSMainMenu.cc	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/src/orxonox/gamestates/GSMainMenu.cc	2009-10-06 21:12:01 UTC (rev 5896)
@@ -38,7 +38,7 @@
 #include "core/GraphicsManager.h"
 #include "core/GUIManager.h"
 #include "Scene.h"
-#include "sound/SoundBase.h"
+#include "sound/AmbientSound.h"
 
 namespace orxonox
 {
@@ -61,8 +61,8 @@
         if (GameMode::playsSound())
         {
             // Load sound
-            this->ambient_ = new SoundBase(0);
-            this->ambient_->loadFile("ambient/mainmenu.wav");
+            this->ambient_ = new AmbientSound(0);
+            this->ambient_->setSoundFile("ambient/mainmenu.wav");
         }
     }
 
@@ -95,7 +95,8 @@
 
         if (GameMode::playsSound())
         {
-            this->ambient_->play(true);
+            this->ambient_->setLoop(true);
+            this->ambient_->play();
         }
     }
 

Modified: code/branches/core5/src/orxonox/gamestates/GSMainMenu.h
===================================================================
--- code/branches/core5/src/orxonox/gamestates/GSMainMenu.h	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/src/orxonox/gamestates/GSMainMenu.h	2009-10-06 21:12:01 UTC (rev 5896)
@@ -58,7 +58,7 @@
         Ogre::Camera*     camera_;
 
         // ambient sound for the main menu
-        SoundBase*        ambient_;
+        AmbientSound*     ambient_;
     };
 }
 

Copied: code/branches/core5/src/orxonox/sound/AmbientSound.cc (from rev 5891, code/branches/core5/src/orxonox/sound/SoundBase.cc)
===================================================================
--- code/branches/core5/src/orxonox/sound/AmbientSound.cc	                        (rev 0)
+++ code/branches/core5/src/orxonox/sound/AmbientSound.cc	2009-10-06 21:12:01 UTC (rev 5896)
@@ -0,0 +1,61 @@
+/*
+ *   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:
+ *      ...
+ *
+ */
+
+#include "AmbientSound.h"
+
+#include "core/EventIncludes.h"
+#include "core/XMLPort.h"
+
+namespace orxonox
+{
+    CreateFactory(AmbientSound);
+
+    AmbientSound::AmbientSound(BaseObject* creator)
+        : BaseObject(creator)
+    {
+        RegisterObject(AmbientSound);
+    }
+
+    AmbientSound::~AmbientSound()
+    {
+    }
+
+    void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode)
+    {
+        SUPER(AmbientSound, XMLPort, xmlelement, mode);
+        XMLPortParamExtern(AmbientSound, BaseSound, this, "soundFile", setSoundFile, getSoundFile, xmlelement, mode);
+        XMLPortParamExtern(AmbientSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode);
+        XMLPortParamExtern(AmbientSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
+    }
+
+    void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
+    {
+        SUPER(AmbientSound, XMLEventPort, xmlelement, mode);
+        XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode);
+    }
+}

Copied: code/branches/core5/src/orxonox/sound/AmbientSound.h (from rev 5891, code/branches/core5/src/orxonox/sound/SoundBase.h)
===================================================================
--- code/branches/core5/src/orxonox/sound/AmbientSound.h	                        (rev 0)
+++ code/branches/core5/src/orxonox/sound/AmbientSound.h	2009-10-06 21:12:01 UTC (rev 5896)
@@ -0,0 +1,56 @@
+/*
+ *   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 _AmbientSound_H__
+#define _AmbientSound_H__
+
+#include "OrxonoxPrereqs.h"
+
+#include "core/BaseObject.h"
+#include "sound/BaseSound.h"
+
+namespace orxonox
+{
+    /**
+     * The AmbientSound class is the base class for all sound file loader classes.
+     * It server as main interface to the OpenAL library.
+     *
+     */
+    class _OrxonoxExport AmbientSound : public BaseSound, public BaseObject
+    {
+    public:
+        AmbientSound(BaseObject* creator);
+        virtual ~AmbientSound();
+
+        virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
+        virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
+
+    private:
+    };
+}
+
+#endif /* _AmbientSound_H__ */

Copied: code/branches/core5/src/orxonox/sound/BaseSound.cc (from rev 5891, code/branches/core5/src/orxonox/sound/SoundBase.cc)
===================================================================
--- code/branches/core5/src/orxonox/sound/BaseSound.cc	                        (rev 0)
+++ code/branches/core5/src/orxonox/sound/BaseSound.cc	2009-10-06 21:12:01 UTC (rev 5896)
@@ -0,0 +1,234 @@
+/*
+ *   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:
+ *      Reto Grieder
+ *
+ */
+
+#include "BaseSound.h"
+
+#include <vector>
+#include <AL/alut.h>
+#include <vorbis/vorbisfile.h>
+
+#include "core/CoreIncludes.h"
+#include "core/GameMode.h"
+#include "core/Resource.h"
+
+namespace orxonox
+{
+    BaseSound::BaseSound()
+        : source_(0)
+        , buffer_(0)
+        , bPlayOnLoad_(false)
+        , bLoop_(false)
+    {
+        RegisterRootObject(BaseSound);
+    }
+
+    BaseSound::~BaseSound()
+    {
+        this->setSoundFile("");
+    }
+
+    void BaseSound::play()
+    {
+        if (alIsSource(this->source_))
+        {
+            if (this->bLoop_)
+                alSourcei(this->source_, AL_LOOPING, AL_TRUE);
+            else
+                alSourcei(this->source_, AL_LOOPING, AL_FALSE);
+            alSourcePlay(this->source_);
+
+            if (alGetError() != AL_NO_ERROR)
+            {
+                 COUT(2) << "Sound: OpenAL: Error playin sound " << this->source_ << std::endl;
+            }
+        }
+    }
+
+    void BaseSound::stop()
+    {
+        if (alIsSource(this->source_))
+            alSourceStop(this->source_);
+    }
+
+    void BaseSound::pause()
+    {
+        if (alIsSource(this->source_))
+            alSourcePause(this->source_);
+    }
+
+    bool BaseSound::isPlaying()
+    {
+        if (alIsSource(this->source_))
+            return getSourceState() == AL_PLAYING;
+        return false;
+    }
+
+    bool BaseSound::isPaused()
+    {
+        if (alIsSource(this->source_))
+            return getSourceState() == AL_PAUSED;
+        return true;
+    }
+
+    bool BaseSound::isStopped()
+    {
+        if (alIsSource(this->source_))
+            return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
+        return true;
+    }
+
+    void BaseSound::setPlayOnLoad(bool val)
+    {
+        this->bPlayOnLoad_ = true;
+        this->play();
+    }
+
+    void BaseSound::setSoundFile(const std::string& soundFile)
+    {
+        this->soundFile_ = soundFile;
+        if (!GameMode::playsSound())
+            return;
+
+        if (soundFile.empty() && alIsSource(this->source_))
+        {
+            // Unload sound
+            alSourcei(this->source_, AL_BUFFER, 0);
+            alDeleteSources(1, &this->source_);
+            alDeleteBuffers(1, &this->buffer_);
+            return;
+        }
+
+        COUT(3) << "Sound: OpenAL ALUT: loading file " << soundFile << std::endl;
+        // Get DataStream from the resources
+        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(soundFile);
+        if (fileInfo == NULL)
+        {
+            COUT(2) << "Warning: Sound file '" << soundFile << "' not found" << std::endl;
+            return;
+        }
+        DataStreamPtr stream = Resource::open(soundFile);
+        // Read everything into a temporary buffer
+        char* buffer = new char[fileInfo->size];
+        stream->read(buffer, fileInfo->size);
+
+        this->buffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size);
+        delete[] buffer;
+
+        if (this->buffer_ == AL_NONE)
+        {
+            COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
+            return;
+            //if (filename.find("ogg", 0) != std::string::npos)
+            //{
+            //    COUT(2) << "Sound: Trying fallback ogg loader" << std::endl;
+            //    this->buffer_ = loadOggFile(filename);
+            //}
+
+            //if (this->buffer_ == AL_NONE)
+            //{
+            //    COUT(2) << "Sound: fallback ogg loader failed: " << alutGetErrorString(alutGetError()) << std::endl;
+            //    return;
+            //}
+        }
+
+        alGenSources(1, &this->source_);
+        alSourcei(this->source_, AL_BUFFER, this->buffer_);
+        if (alGetError() != AL_NO_ERROR)
+        {
+            COUT(2) << "Sound: OpenAL: Error loading sample file: " << soundFile << std::endl;
+            return;
+        }
+
+        alSource3f(this->source_, AL_POSITION,  0, 0, 0);
+
+        if (this->bPlayOnLoad_)
+            this->play();
+    }
+
+    ALint BaseSound::getSourceState()
+    {
+        ALint state;
+        alGetSourcei(this->source_, AL_SOURCE_STATE, &state);
+        return state;
+    }
+
+#if 0 // Not yet supported because of missing resource implementation
+    ALuint BaseSound::loadOggFile(const std::string& filename)
+    {
+        char inbuffer[4096];
+        std::vector<char> outbuffer;
+        OggVorbis_File vf;
+        vorbis_info* vorbisInfo;
+        int eof = false;
+        int current_section;
+        ALuint buffer;
+        ALenum format;
+
+        FILE* f = fopen(filename.c_str(), "rb");
+
+        if (ov_open(f, &vf, NULL, 0) < 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, &current_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 + sizeof(inbuffer));
+            }
+        }
+
+        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;
+    }
+#endif
+
+} // namespace: orxonox

Copied: code/branches/core5/src/orxonox/sound/BaseSound.h (from rev 5891, code/branches/core5/src/orxonox/sound/SoundBase.h)
===================================================================
--- code/branches/core5/src/orxonox/sound/BaseSound.h	                        (rev 0)
+++ code/branches/core5/src/orxonox/sound/BaseSound.h	2009-10-06 21:12:01 UTC (rev 5896)
@@ -0,0 +1,80 @@
+/*
+ *   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:
+ *      Reto Grieder
+ *
+ */
+#ifndef _BaseSound_H__
+#define _BaseSound_H__
+
+#include "OrxonoxPrereqs.h"
+
+#include <string>
+#include "core/OrxonoxClass.h"
+
+namespace orxonox
+{
+    /**
+     * The BaseSound class is the base class for all sound file loader classes.
+     * It server as main interface to the OpenAL library.
+     *
+     */
+    class _OrxonoxExport BaseSound : virtual public OrxonoxClass
+    {
+    public:
+        BaseSound();
+        virtual ~BaseSound();
+
+        void play();
+        void stop();
+        void pause();
+
+        bool isPlaying();
+        bool isPaused();
+        bool isStopped();
+
+        void setSoundFile(const std::string& soundFile);
+        const std::string& getSoundFile() { return this->soundFile_; }
+
+        bool getPlayOnLoad() { return this->bPlayOnLoad_; }
+        void setPlayOnLoad(bool val);
+
+        bool getLoop() { return this->bLoop_; }
+        void setLoop(bool val) { this->bLoop_ = val; }
+
+    protected:
+        //ALuint loadOggFile(const std::string& filename);
+        ALint getSourceState();
+
+        ALuint source_;
+        ALuint buffer_;
+
+    private:
+        std::string soundFile_;
+        bool bPlayOnLoad_;
+        bool bLoop_;
+    };
+}
+
+#endif /* _BaseSound_H__ */

Modified: code/branches/core5/src/orxonox/sound/CMakeLists.txt
===================================================================
--- code/branches/core5/src/orxonox/sound/CMakeLists.txt	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/src/orxonox/sound/CMakeLists.txt	2009-10-06 21:12:01 UTC (rev 5896)
@@ -1,5 +1,7 @@
 ADD_SOURCE_FILES(ORXONOX_SRC_FILES
+    AmbientSound.cc
+    BaseSound.cc
     SoundManager.cc
-    SoundBase.cc
+    WorldSound.cc
 )
 

Deleted: code/branches/core5/src/orxonox/sound/SoundBase.cc
===================================================================
--- code/branches/core5/src/orxonox/sound/SoundBase.cc	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/src/orxonox/sound/SoundBase.cc	2009-10-06 21:12:01 UTC (rev 5896)
@@ -1,239 +0,0 @@
-/*
- *   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 <string>
-#include <vector>
-#include <AL/alut.h>
-#include <vorbis/vorbisfile.h>
-
-#include "util/Math.h"
-#include "core/Resource.h"
-#include "worldentities/WorldEntity.h"
-#include "SoundManager.h"
-
-namespace orxonox
-{
-    SoundBase::SoundBase(WorldEntity* entity)
-    {
-        this->source_ = 0;
-        this->buffer_ = 0;
-        this->entity_ = entity;
-
-        SoundManager::getInstance().addSound(this);
-    }
-
-    SoundBase::~SoundBase()
-    {
-        alSourcei(this->source_, AL_BUFFER, 0);
-        alDeleteSources(1, &this->source_);
-        alDeleteBuffers(1, &this->buffer_);
-    }
-
-    void SoundBase::attachToEntity(WorldEntity* entity)
-    {
-        this->entity_ = entity;
-        this->update();
-    }
-
-    void SoundBase::update() {
-        if(this->entity_ != NULL && alIsSource(this->source_)) {
-            const 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) << "Sound: OpenAL: Invalid sound position" << std::endl;
-
-            const 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) << "Sound: OpenAL: Invalid sound velocity" << std::endl;
-
-            const 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) << "Sound: OpenAL: Invalid sound direction" << 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_);
-
-            if(alGetError() != AL_NO_ERROR)
-            {
-                 COUT(2) << "Sound: OpenAL: Error playin sound " << this->source_ << std::endl;
-            }
-        }
-    }
-
-    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;
-        }
-        return false;
-    }
-
-    bool SoundBase::isPaused() {
-        if(alIsSource(this->source_)) {
-            return getSourceState() == AL_PAUSED;
-        }
-        return true;
-    }
-
-    bool SoundBase::isStopped() {
-        if(alIsSource(this->source_)) {
-            return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
-        }
-        return true;
-    }
-
-    bool SoundBase::loadFile(const std::string& filename) {
-        if(!SoundManager::getInstance().isSoundAvailable())
-        {
-            COUT(3) << "Sound: not available, skipping " << filename << std::endl;
-            return false;
-        }
-
-        COUT(3) << "Sound: OpenAL ALUT: loading file " << filename << std::endl;
-        // Get DataStream from the resources
-        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(filename);
-        if (fileInfo == NULL) {
-            COUT(2) << "Warning: Sound file '" << filename << "' not found" << std::endl;
-            return false;
-        }
-        DataStreamPtr stream = Resource::open(filename);
-        // Read everything into a temporary buffer
-        char* buffer = new char[fileInfo->size];
-        stream->read(buffer, fileInfo->size);
-
-        this->buffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size);
-        delete[] buffer;
-
-        if(this->buffer_ == AL_NONE) {
-            COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
-            if(filename.find("ogg", 0) != std::string::npos)
-            {
-                COUT(2) << "Sound: Trying fallback ogg loader" << std::endl;
-                this->buffer_ = loadOggFile(filename);
-            }
-
-            if(this->buffer_ == AL_NONE)
-            {
-                COUT(2) << "Sound: fallback ogg loader failed: " << alutGetErrorString(alutGetError()) << std::endl;
-                return false;
-            }
-        }
-
-        alGenSources(1, &this->source_);
-        alSourcei(this->source_, AL_BUFFER, this->buffer_);
-        if(alGetError() != AL_NO_ERROR) {
-            COUT(2) << "Sound: OpenAL: Error loading sample file: " << filename << std::endl;
-            return false;
-        }
-        return true;
-    }
-
-    ALint SoundBase::getSourceState() {
-        ALint state;
-        alGetSourcei(this->source_, AL_SOURCE_STATE, &state);
-        return state;
-    }
-
-    ALuint SoundBase::loadOggFile(const std::string& filename)
-    {
-        char inbuffer[4096];
-        std::vector<char> outbuffer;
-        OggVorbis_File vf;
-        vorbis_info* vorbisInfo;
-        int eof = false;
-        int current_section;
-        ALuint buffer;
-        ALenum format;
-
-        FILE* f = fopen(filename.c_str(), "rb");
-
-        if(ov_open(f, &vf, NULL, 0) < 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, &current_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 + sizeof(inbuffer));
-            }
-        }
-
-        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;
-    }
-} // namespace: orxonox

Deleted: code/branches/core5/src/orxonox/sound/SoundBase.h
===================================================================
--- code/branches/core5/src/orxonox/sound/SoundBase.h	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/src/orxonox/sound/SoundBase.h	2009-10-06 21:12:01 UTC (rev 5896)
@@ -1,69 +0,0 @@
-/*
- *   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:
- *      ...
- *
- */
-#ifndef _SoundBase_H__
-#define _SoundBase_H__
-
-#include "OrxonoxPrereqs.h"
-#include <cstring> // define NULL
-
-namespace orxonox
-{
-    /**
-     * The SoundBase class is the base class for all sound file loader classes.
-     * It server as main interface to the OpenAL library.
-     *
-     */
-    class _OrxonoxExport SoundBase
-    {
-    public:
-        SoundBase(WorldEntity* entity = NULL);
-        ~SoundBase();
-
-        void attachToEntity(WorldEntity* entity);
-        void update();
-        void play(bool loop = false);
-        void stop();
-        void pause();
-
-        bool isPlaying();
-        bool isPaused();
-        bool isStopped();
-
-        bool loadFile(const std::string& filename);
-
-    private:
-        ALuint loadOggFile(const std::string& filename);
-        ALuint source_;
-        ALuint buffer_;
-        WorldEntity* entity_;
-
-        ALint getSourceState();
-    }; // class SoundBase
-} // namepsace orxonox
-
-#endif /* _SoundBase_H__ */

Modified: code/branches/core5/src/orxonox/sound/SoundManager.cc
===================================================================
--- code/branches/core5/src/orxonox/sound/SoundManager.cc	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/src/orxonox/sound/SoundManager.cc	2009-10-06 21:12:01 UTC (rev 5896)
@@ -35,9 +35,6 @@
 #include "util/ScopeGuard.h"
 #include "core/GameMode.h"
 #include "core/ScopedSingletonManager.h"
-#include "CameraManager.h"
-#include "graphics/Camera.h"
-#include "SoundBase.h"
 
 namespace orxonox
 {
@@ -88,75 +85,26 @@
         alutExit();
     }
 
-    /**
-     * 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)
+    void SoundManager::setListenerPosition(const Vector3& position)
     {
-        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)
-    {
-        if (!CameraManager::getInstancePtr())
-            return;
-
-        // update listener position
-        Camera* camera = CameraManager::getInstance().getActiveCamera();
-        if(camera == NULL) return;
-        Vector3 pos = camera->getPosition();
-        alListener3f(AL_POSITION, pos.x, pos.y, pos.z);
+        alListener3f(AL_POSITION, position.x, position.y, position.z);
         ALenum error = alGetError();
-        if(error == AL_INVALID_VALUE)
+        if (error == AL_INVALID_VALUE)
             COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl;
+    }
 
+    void SoundManager::setListenerOrientation(const Quaternion& orientation)
+    {
         // update listener orientation
-        const Quaternion& orient = camera->getOrientation();
-        Vector3 up = orient.xAxis(); // just a wild guess
-        Vector3 at = orient.zAxis();
+        Vector3 up = orientation.xAxis(); // just a wild guess
+        Vector3 at = orientation.zAxis();
 
-        ALfloat orientation[6] = { at.x, at.y, at.z,
-                                 up.x, up.y, up.z };
+        ALfloat orient[6] = { at.x, at.y, at.z,
+                              up.x, up.y, up.z };
 
-        alListenerfv(AL_POSITION, orientation);
-        error = alGetError();
-        if(error == AL_INVALID_VALUE)
+        alListenerfv(AL_POSITION, orient);
+        ALenum error = alGetError();
+        if (error == AL_INVALID_VALUE)
             COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
-
-        // update sounds
-        for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)
-            (*i)->update();
     }
-
-    /**
-    * Check if sound is available
-    */
-    bool SoundManager::isSoundAvailable()
-    {
-        return this->soundavailable_;
-    }
 }

Modified: code/branches/core5/src/orxonox/sound/SoundManager.h
===================================================================
--- code/branches/core5/src/orxonox/sound/SoundManager.h	2009-10-06 19:42:58 UTC (rev 5895)
+++ code/branches/core5/src/orxonox/sound/SoundManager.h	2009-10-06 21:12:01 UTC (rev 5896)
@@ -38,29 +38,25 @@
 {
     /**
      * 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.
+     * position. It is a singleton.
      *
      */
-    class _OrxonoxExport SoundManager : public Singleton<SoundManager>, public Tickable
+    class _OrxonoxExport SoundManager : public Singleton<SoundManager>
     {
         friend class Singleton<SoundManager>;
     public:
         SoundManager();
         ~SoundManager();
-        void addSound(SoundBase* sound);
-        void removeSound(SoundBase* sound);
-        void tick(float dt);
-        bool isSoundAvailable();
 
+        void setListenerPosition(const Vector3& position);
+        void setListenerOrientation(const Quaternion& orientation);
+
     private:
         ALCdevice* device_;
         ALCcontext* context_;
-        std::list<SoundBase*> soundlist_;
-        bool soundavailable_;
 
         static SoundManager* singletonPtr_s;
-    }; // class SoundManager
-} // namespace orxonox
+    };
+}
 
 #endif /* _SoundManager_H__ */

Copied: code/branches/core5/src/orxonox/sound/WorldSound.cc (from rev 5891, code/branches/core5/src/orxonox/sound/SoundBase.cc)
===================================================================
--- code/branches/core5/src/orxonox/sound/WorldSound.cc	                        (rev 0)
+++ code/branches/core5/src/orxonox/sound/WorldSound.cc	2009-10-06 21:12:01 UTC (rev 5896)
@@ -0,0 +1,95 @@
+/*
+ *   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
+ *      Reto Grieder
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "WorldSound.h"
+
+#include <AL/alut.h>
+#include "util/Math.h"
+#include "core/EventIncludes.h"
+#include "core/XMLPort.h"
+
+namespace orxonox
+{
+    CreateFactory(WorldSound);
+
+    WorldSound::WorldSound(BaseObject* creator)
+        : StaticEntity(creator)
+    {
+        RegisterObject(WorldSound);
+    }
+
+    WorldSound::~WorldSound()
+    {
+    }
+
+    void WorldSound::XMLPort(Element& xmlelement, XMLPort::Mode mode)
+    {
+        SUPER(WorldSound, XMLPort, xmlelement, mode);
+        XMLPortParamExtern(WorldSound, BaseSound, this, "soundFile", setSoundFile, getSoundFile, xmlelement, mode);
+        XMLPortParamExtern(WorldSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode);
+        XMLPortParamExtern(WorldSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
+    }
+
+    void WorldSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
+    {
+        SUPER(WorldSound, XMLEventPort, xmlelement, mode);
+        XMLPortEventState(WorldSound, BaseObject, "play", play, xmlelement, mode);
+    }
+
+    void WorldSound::processEvent(Event& event)
+    {
+        SUPER(WorldSound, processEvent, event);
+    }
+
+    void WorldSound::tick(float dt)
+    {
+        if (alIsSource(this->source_))
+        {
+            const Vector3& pos = this->getWorldPosition();
+            alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z);
+            ALenum error = alGetError();
+            if (error == AL_INVALID_VALUE)
+                COUT(2) << "Sound: OpenAL: Invalid sound position" << std::endl;
+
+            const Vector3& vel = this->getVelocity();
+            alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z);
+            error = alGetError();
+            if (error == AL_INVALID_VALUE)
+                COUT(2) << "Sound: OpenAL: Invalid sound velocity" << std::endl;
+
+            const Quaternion& orient = this->getWorldOrientation();
+            Vector3 at = orient.zAxis();
+            alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z);
+            error = alGetError();
+            if (error == AL_INVALID_VALUE)
+                COUT(2) << "Sound: OpenAL: Invalid sound direction" << std::endl;
+        }
+    }
+
+}

Copied: code/branches/core5/src/orxonox/sound/WorldSound.h (from rev 5891, code/branches/core5/src/orxonox/sound/SoundBase.h)
===================================================================
--- code/branches/core5/src/orxonox/sound/WorldSound.h	                        (rev 0)
+++ code/branches/core5/src/orxonox/sound/WorldSound.h	2009-10-06 21:12:01 UTC (rev 5896)
@@ -0,0 +1,60 @@
+/*
+ *   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 _WorldSound_H__
+#define _WorldSound_H__
+
+#include "OrxonoxPrereqs.h"
+
+#include "tools/interfaces/Tickable.h"
+#include "sound/BaseSound.h"
+#include "worldentities/StaticEntity.h"
+
+namespace orxonox
+{
+    /*
+    @brief
+        The WorldSound class is to be used for sounds with position and orientation.
+    */
+    class _OrxonoxExport WorldSound : public StaticEntity, public BaseSound, public Tickable
+    {
+    public:
+        WorldSound(BaseObject* creator);
+        virtual ~WorldSound();
+
+        virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
+        virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
+
+        virtual void tick(float dt);
+
+        virtual void processEvent(Event& event);
+
+    private:
+    };
+}
+
+#endif /* _WorldSound_H__ */




More information about the Orxonox-commit mailing list