Index: /trunk/src/defs/debug.h
===================================================================
--- /trunk/src/defs/debug.h	(revision 5929)
+++ /trunk/src/defs/debug.h	(revision 5930)
@@ -78,4 +78,5 @@
   #define DEBUG_MODULE_SPATIAL_SEPARATION    2
   #define DEBUG_MODULE_GUI                   2
+  #define DEBUG_MODULE_SOUND                 2
 
   // MISC
Index: /trunk/src/lib/sound/sound_buffer.cc
===================================================================
--- /trunk/src/lib/sound/sound_buffer.cc	(revision 5929)
+++ /trunk/src/lib/sound/sound_buffer.cc	(revision 5930)
@@ -20,7 +20,5 @@
 #include "sound_engine.h"
 
-
 using namespace std;
-
 
 //////////////////
@@ -36,6 +34,4 @@
   this->setName(fileName);
 
-  SoundEngine::getInstance()->addBuffer(this);
-
   ALenum format;
   ALvoid* data;
@@ -47,5 +43,5 @@
   alGenBuffers(1, &this->bufferID);
   if ((result = alGetError()) != AL_NO_ERROR)
-    SoundEngine::PrintALErrorString(result);
+    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
 
   // read in the wav data
@@ -59,15 +55,15 @@
 #endif
   if ((result = alGetError()) != AL_NO_ERROR)
-    SoundEngine::PrintALErrorString(result);
+    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
 
   // send the loaded wav data to the buffer
   alBufferData(this->bufferID, format, data, this->size, freq);
   if ((result = alGetError()) != AL_NO_ERROR)
-    SoundEngine::PrintALErrorString(result);
+    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
 
   // remove the wav data (redundant)
   alutUnloadWAV(format, data, this->size, freq);
   if ((result = alGetError()) != AL_NO_ERROR)
-    SoundEngine::PrintALErrorString(result);
+    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
 }
 
Index: /trunk/src/lib/sound/sound_engine.cc
===================================================================
--- /trunk/src/lib/sound/sound_engine.cc	(revision 5929)
+++ /trunk/src/lib/sound/sound_engine.cc	(revision 5930)
@@ -47,4 +47,9 @@
   this->bufferList = NULL;
   this->sourceList = NULL;
+
+  this->device = NULL;
+  this->context = NULL;
+
+  this->maxSourceCount = 32;
 }
 
@@ -64,4 +69,10 @@
     while (this->sourceList->size() > 0)
       delete dynamic_cast<SoundSource*>(this->sourceList->front());
+  }
+
+  while(!this->ALSources.empty())
+  {
+    alDeleteSources(1, &this->ALSources.top());
+    this->ALSources.pop();
   }
 
@@ -118,40 +129,22 @@
 
 
-/**
- *  adds a SoundBuffer to the bufferList of the SoundEngine
- * @param buffer The buffer to add to the bufferList
-*/
-void SoundEngine::addBuffer(SoundBuffer* buffer)
-{
-  if (unlikely(this->bufferList == NULL))
-    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
-}
-
-/**
- *  removes a SoundBuffer from the bufferList of the SoundEngine
- * @param buffer The buffer to delete from the SoundEngine
-*/
-void SoundEngine::removeBuffer(SoundBuffer* buffer)
-{
-  // look if there are any sources that have the buffer still loaded
-  if (this->sourceList != NULL)
-  {
-    list<BaseObject*>::const_iterator source;
-    for (source = this->sourceList->begin(); source != this->sourceList->end(); source++)
-    {
-      if (buffer == static_cast<SoundSource*>(*source)->getBuffer())
-        delete (*source);
+void SoundEngine::popALSource(ALuint& source)
+{
+  if (source != 0)
+    return;
+  else
+  {
+
+    /// @TODO try to create more sources if needed
+    if (!this->ALSources.empty())
+    {
+
+      source = this->ALSources.top();
+      printf("test: : %d\n", source);
+      this->ALSources.pop();
     }
   }
 }
 
-/**
- * adds a SoundSource to the sourceList of the SoundEngine
- * @param source The source to add to the sourceList
-*/
-void SoundEngine::addSource(SoundSource* source)
-{
-  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
-}
 
 /**
@@ -180,5 +173,5 @@
 
   // updating all the Sources positions
-  if (likely(this->sourceList != NULL))
+  if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL))
   {
     list<BaseObject*>::const_iterator sourceIT;
@@ -187,14 +180,27 @@
     {
       source = static_cast<SoundSource*>(*sourceIT);
-      if (likely(source->getNode() != NULL))
+      if (source->isPlaying())
       {
-        alSource3f(source->getID(), AL_POSITION,
-                   source->getNode()->getAbsCoor().x,
-                   source->getNode()->getAbsCoor().y,
-                   source->getNode()->getAbsCoor().z);
-        alSource3f(source->getID(), AL_VELOCITY,
-                   source->getNode()->getVelocity().x,
-                   source->getNode()->getVelocity().y,
-                   source->getNode()->getVelocity().z);
+        int play;
+        alGetSourcei(source->getID(), AL_SOURCE_STATE, &play);
+        if(play == AL_PLAYING)
+        {
+          if (likely(source->getNode() != NULL))
+          {
+            alSource3f(source->getID(), AL_POSITION,
+                       source->getNode()->getAbsCoor().x,
+                       source->getNode()->getAbsCoor().y,
+                       source->getNode()->getAbsCoor().z);
+            alSource3f(source->getID(), AL_VELOCITY,
+                       source->getNode()->getVelocity().x,
+                       source->getNode()->getVelocity().y,
+                       source->getNode()->getVelocity().z);
+          }
+
+        }
+        else
+        {
+          source->stop();
+        }
       }
     }
@@ -300,5 +306,5 @@
 
   if ((result = alGetError()) != AL_NO_ERROR)
-    SoundEngine::PrintALErrorString(result);
+    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
 
   this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
@@ -313,24 +319,18 @@
 bool SoundEngine::allocateSources(unsigned int count)
 {
-  ALuint* sourceList = new ALuint[count];
   ALenum result;
-
-  alGenSources(count, sourceList);
-  if ((result = alGetError()) != AL_NO_ERROR)
-  {
-    SoundEngine::PrintALErrorString(result);
-    return false;
-  }
-
-  /// @TODO check syntax
-
-
   // Setting default values.
-  for (int i = 0; i < count; i++)
-  {
-    alSourcef (sourceList[i], AL_PITCH,    1.0      );
-    alSourcef (sourceList[i], AL_GAIN,     this->getEffectsVolume() );
-    alSourcei (sourceList[i], AL_LOOPING,  AL_FALSE );
-    this->ALSources.push(sourceList[i]);
+  for (unsigned int i = 0; i < count; i++)
+  {
+    ALuint source;
+
+    alGenSources(1, &source);
+    if ((result = alGetError()) != AL_NO_ERROR)
+      PRINTF(1)("Error Generating Sources: '%s'\n", SoundEngine::getALErrorString(result));
+
+    alSourcef (source, AL_PITCH,    1.0      );
+    alSourcef (source, AL_GAIN,     this->getEffectsVolume() );
+    alSourcei (source, AL_LOOPING,  AL_FALSE );
+    this->ALSources.push(source);
   }
   return true;
@@ -341,31 +341,20 @@
  * @param err The error found
 */
-void SoundEngine::PrintALErrorString(ALenum err)
+const char* SoundEngine::getALErrorString(ALenum err)
 {
   switch(err)
     {
     case AL_NO_ERROR:
-      PRINTF(4)("AL_NO_ERROR\n");
-      break;
-
+      return ("AL_NO_ERROR");
     case AL_INVALID_NAME:
-      PRINTF(2)("AL_INVALID_NAME\n");
-      break;
-
+      return ("AL_INVALID_NAME");
     case AL_INVALID_ENUM:
-      PRINTF(2)("AL_INVALID_ENUM\n");
-      break;
-
+      return ("AL_INVALID_ENUM");
     case AL_INVALID_VALUE:
-      PRINTF(2)("AL_INVALID_VALUE\n");
-      break;
-
+      return ("AL_INVALID_VALUE");
     case AL_INVALID_OPERATION:
-      PRINTF(2)("AL_INVALID_OPERATION\n");
-      break;
-
+      return ("AL_INVALID_OPERATION");
     case AL_OUT_OF_MEMORY:
-      PRINTF(2)("AL_OUT_OF_MEMORY\n");
-      break;
+      return ("AL_OUT_OF_MEMORY");
     };
 }
@@ -373,5 +362,4 @@
 void SoundEngine::listDevices()
 {
-
   printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
 }
Index: /trunk/src/lib/sound/sound_engine.h
===================================================================
--- /trunk/src/lib/sound/sound_engine.h	(revision 5929)
+++ /trunk/src/lib/sound/sound_engine.h	(revision 5930)
@@ -26,5 +26,4 @@
 //! A class that handles audio via the openAudioLibrary
 class SoundEngine : public BaseObject {
-
   public:
     virtual ~SoundEngine();
@@ -49,7 +48,6 @@
 
   // administrative
-    void addBuffer(SoundBuffer* buffer);
-    void removeBuffer(SoundBuffer* buffer);
-    void addSource(SoundSource* source);
+    void popALSource(ALuint& source);
+    void pushALSource(ALuint& source) { if (source != 0) this->ALSources.push(source); };
 
     void flushUnusedBuffers();
@@ -57,14 +55,13 @@
     void flushAllSources();
 
+    bool initAudio();
     bool allocateSources(unsigned int count);
-    bool initAudio();
 
   // error handling:
-    static void PrintALErrorString(ALenum err);
-  //  static void PrintALCErrorString(ALenum err);
-
+    static const char* getALErrorString(ALenum err);
 
   private:
     SoundEngine();
+
     void listDevices();
 
Index: /trunk/src/lib/sound/sound_source.cc
===================================================================
--- /trunk/src/lib/sound/sound_source.cc	(revision 5929)
+++ /trunk/src/lib/sound/sound_source.cc	(revision 5930)
@@ -34,17 +34,9 @@
 
   // adding the Source to the SourcesList of the SoundEngine
-  SoundEngine::getInstance()->addSource(this);
-
   this->buffer = buffer;
   this->sourceNode = sourceNode;
 
-  alGenSources(1, &this->sourceID);
-  if ((result = alGetError()) != AL_NO_ERROR)
-    SoundEngine::PrintALErrorString(result);
-  if (this->buffer != NULL)
-    alSourcei (this->sourceID, AL_BUFFER,   this->buffer->getID());
-  alSourcef (this->sourceID, AL_PITCH,    1.0      );
-  alSourcef (this->sourceID, AL_GAIN,     SoundEngine::getInstance()->getEffectsVolume() );
-  alSourcei (sourceID, AL_LOOPING,  AL_FALSE     );
+  this->sourceID = 0;
+  this->bPlay = false;
 }
 
@@ -54,6 +46,5 @@
 SoundSource::~SoundSource()
 {
-  //SoundEngine::getInstance()->removeSource(this);
-  alDeleteSources(1, &this->sourceID);
+  SoundEngine::getInstance()->pushALSource(this->sourceID);
 }
 
@@ -63,5 +54,8 @@
 void SoundSource::play()
 {
+  if (this->sourceID == 0)
+    SoundEngine::getInstance()->popALSource(this->sourceID);
   alSourcePlay(this->sourceID);
+  this->bPlay = true;
 }
 
@@ -72,4 +66,8 @@
 void SoundSource::play(const SoundBuffer* buffer)
 {
+  if (unlikely(this->sourceID == 0))
+    SoundEngine::getInstance()->popALSource(this->sourceID);
+
+  printf("%d\n",sourceID);
   alSourceStop(this->sourceID);
   alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
@@ -78,4 +76,5 @@
   if (unlikely(this->buffer != NULL))
     alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
+  this->bPlay = true;
 }
 
@@ -85,5 +84,7 @@
 void SoundSource::stop()
 {
+  this->bPlay = false;
   alSourceStop(this->sourceID);
+  SoundEngine::getInstance()->pushALSource(this->sourceID);
 }
 
Index: /trunk/src/lib/sound/sound_source.h
===================================================================
--- /trunk/src/lib/sound/sound_source.h	(revision 5929)
+++ /trunk/src/lib/sound/sound_source.h	(revision 5930)
@@ -31,4 +31,6 @@
     /** @returns The ID of this Source */
     inline ALuint getID() const { return this->sourceID; }
+    /** @returns true, if the Source is Playing */
+    inline bool   isPlaying() const { return this->bPlay; };
     /** @returns the SoundBuffer of this Source */
     inline const SoundBuffer* getBuffer() const { return this->buffer; }
Index: /trunk/src/orxonox.cc
===================================================================
--- /trunk/src/orxonox.cc	(revision 5929)
+++ /trunk/src/orxonox.cc	(revision 5930)
@@ -235,4 +235,5 @@
   // SDL_InitSubSystem(SDL_INIT_AUDIO);
   SoundEngine::getInstance()->initAudio();
+  SoundEngine::getInstance()->allocateSources(1);
 
   SoundEngine::getInstance()->loadSettings(this->iniParser);
Index: /trunk/src/util/loading/resource_manager.cc
===================================================================
--- /trunk/src/util/loading/resource_manager.cc	(revision 5929)
+++ /trunk/src/util/loading/resource_manager.cc	(revision 5930)
@@ -33,5 +33,5 @@
 #endif /* NO_TEXT */
 #ifndef NO_AUDIO
-#include "sound_engine.h"
+#include "sound_buffer.h"
 #include "ogg_player.h"
 #endif /* NO_AUDIO */
Index: /trunk/src/world_entities/weapons/aiming_turret.cc
===================================================================
--- /trunk/src/world_entities/weapons/aiming_turret.cc	(revision 5929)
+++ /trunk/src/world_entities/weapons/aiming_turret.cc	(revision 5930)
@@ -26,5 +26,4 @@
 
 #include "animation3d.h"
-#include "sound_engine.h"
 
 #include "factory.h"
Index: /trunk/src/world_entities/weapons/cannon.cc
===================================================================
--- /trunk/src/world_entities/weapons/cannon.cc	(revision 5929)
+++ /trunk/src/world_entities/weapons/cannon.cc	(revision 5930)
@@ -32,5 +32,4 @@
 #include "list.h"
 #include "animation3d.h"
-#include "sound_engine.h"
 
 #include "null_parent.h"
Index: /trunk/src/world_entities/weapons/test_gun.cc
===================================================================
--- /trunk/src/world_entities/weapons/test_gun.cc	(revision 5929)
+++ /trunk/src/world_entities/weapons/test_gun.cc	(revision 5930)
@@ -32,5 +32,4 @@
 #include "list.h"
 #include "animation3d.h"
-#include "sound_engine.h"
 
 #include "null_parent.h"
Index: /trunk/src/world_entities/weapons/turret.cc
===================================================================
--- /trunk/src/world_entities/weapons/turret.cc	(revision 5929)
+++ /trunk/src/world_entities/weapons/turret.cc	(revision 5930)
@@ -26,5 +26,4 @@
 #include "list.h"
 #include "animation3d.h"
-#include "sound_engine.h"
 
 #include "factory.h"
Index: /trunk/src/world_entities/weapons/weapon.cc
===================================================================
--- /trunk/src/world_entities/weapons/weapon.cc	(revision 5929)
+++ /trunk/src/world_entities/weapons/weapon.cc	(revision 5930)
@@ -28,7 +28,9 @@
 #include "load_param.h"
 #include "state.h"
-#include "sound_engine.h"
 #include "animation3d.h"
 #include "vector.h"
+
+#include "sound_source.h"
+#include "sound_buffer.h"
 
 ////////////////////
