Index: code/branches/sound5/src/orxonox/sound/AmbientSound.cc
===================================================================
--- code/branches/sound5/src/orxonox/sound/AmbientSound.cc	(revision 6929)
+++ code/branches/sound5/src/orxonox/sound/AmbientSound.cc	(revision 6931)
@@ -36,4 +36,5 @@
 #include "SoundManager.h"
 #include "SoundStreamer.h"
+#include "util/Sleep.h"
 
 #include <AL/alut.h>
@@ -42,5 +43,10 @@
 {
     CreateFactory(AmbientSound);
-
+    
+    // vorbis callbacks
+    size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource);
+    int seekVorbis(void* datasource, ogg_int64_t offset, int whence);
+    long tellVorbis(void* datasource);
+    
     AmbientSound::AmbientSound(BaseObject* creator)
         : BaseObject(creator)
@@ -51,5 +57,5 @@
 
         // Ambient sounds always fade in
-        this->setVolume(0);
+        //this->setVolume(0);
         this->registerVariables();
     }
@@ -95,5 +101,5 @@
     void AmbientSound::stop()
     {
-        if (GameMode::playsSound())
+        if (GameMode::playsSound()) 
             SoundManager::getInstance().unregisterAmbientSound(this);
     }
@@ -170,6 +176,9 @@
         if (this->soundstreamthread_.get_id() != boost::thread::id())
         {
-            this->soundstreamthread_.interrupt(); // unhandled interruptions lead to thread terminating ;-)
-        }
+            this->soundstreamthread_.interrupt(); // terminate an old thread if necessary
+        }
+
+        // queue some init buffers
+        COUT(4) << "Sound: Creating thread for " << source << std::endl;
         // Get resource info
         shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source);
@@ -181,6 +190,65 @@
         // Open data stream
         DataStreamPtr dataStream = Resource::open(fileInfo);
-
-        this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream);
+        
+        alSourcei(this->audioSource_, AL_BUFFER, 0);
+
+        // 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 = new OggVorbis_File();
+        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;
+        }
+        vorbis_info* vorbisInfo;
+        vorbisInfo = ov_info(vf, -1);
+        ALenum format;
+        if (vorbisInfo->channels == 1)
+            format = AL_FORMAT_MONO16;
+        else
+            format = AL_FORMAT_STEREO16;
+
+        char inbuffer[4096];
+        ALuint initbuffers[10];
+        alGenBuffers(10, initbuffers);
+        if (ALint error = alGetError()) {
+            COUT(2) << "Sound: Streamer: Could not generate buffer:" << getALErrorString(error) << std::endl;
+            return;
+        }
+        int current_section;
+
+        for(int i = 0; i < 10; i++)
+        {
+            long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
+            if (ret == 0)
+            {
+                break;
+            }
+            else if (ret < 0)
+            {
+                COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
+                ov_clear(vf);
+                return;
+            }
+
+            alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate);
+            if(ALint error = alGetError()) {
+                COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl;
+                break;
+             }
+             alSourceQueueBuffers(this->audioSource_, 1, &initbuffers[i]);
+             if (ALint error = alGetError()) {
+                 COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl;
+             }
+        }
+        
+        this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream, vf, current_section);
         if(this->soundstreamthread_ == boost::thread())
             COUT(2) << "Sound: Failed to create thread." << std::endl;
Index: code/branches/sound5/src/orxonox/sound/SoundStreamer.cc
===================================================================
--- code/branches/sound5/src/orxonox/sound/SoundStreamer.cc	(revision 6929)
+++ code/branches/sound5/src/orxonox/sound/SoundStreamer.cc	(revision 6931)
@@ -28,6 +28,6 @@
 
 #include <boost/thread.hpp>
-#include <al.h>
-#include <alc.h>
+#include <AL/al.h>
+#include <AL/alc.h>
 #include <vorbis/vorbisfile.h>
 #include "SoundManager.h"
@@ -41,27 +41,9 @@
     long tellVorbis(void* datasource);
 
-    void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream)
+    void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section)
     {
-        COUT(4) << "Sound: Creating thread for " << dataStream->getName() << std::endl;
-
-        alSourcei(audioSource, AL_BUFFER, 0);
-
-        // 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);
-            return;
-        }
+        char inbuffer[4096];
         vorbis_info* vorbisInfo;
-        vorbisInfo = ov_info(&vf, -1);
+        vorbisInfo = ov_info(vf, -1);
         ALenum format;
         if (vorbisInfo->channels == 1)
@@ -69,44 +51,4 @@
         else
             format = AL_FORMAT_STEREO16;
-
-        char inbuffer[4096];
-        ALuint initbuffers[5];
-        alGenBuffers(5, initbuffers);
-        if (ALint error = alGetError()) {
-            COUT(2) << "Sound: Streamer: Could not generate buffer:" << getALErrorString(error) << std::endl;
-            return;
-        }
-        int current_section;
-
-        for(int i = 0; i < 5; i++)
-        {
-            long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
-            if (ret == 0)
-            {
-                break;
-            }
-            else if (ret < 0)
-            {
-                COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
-                ov_clear(&vf);
-                return;
-            }
-
-            alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate);
-            if(ALint error = alGetError()) {
-                COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl;
-                break;
-             }
-             alSourceQueueBuffers(audioSource, 1, &initbuffers[i]);
-             if (ALint error = alGetError()) {
-                 COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl;
-             }
-        }
-
-        //alSourcei(audioSource, AL_LOOPING, AL_TRUE);
-
-        alSourcePlay(audioSource);
-        if(ALint error = alGetError())
-            COUT(2) << "Sound: Could not start ambient sound" << getALErrorString(error) << std::endl;
 
         while(true) // Stream forever, control through thread control
@@ -118,5 +60,7 @@
                 COUT(4) << "Sound: " << dataStream->getName() << " is playing." << std::endl;
             else
+            {
                 COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl;
+            }
 
             if(alcGetCurrentContext() == NULL)
@@ -130,4 +74,5 @@
             if (ALint error = alGetError())
                 COUT(2) << "Sound: Warning: Couldn't get number of processed buffers: " << getALErrorString(error) << std::endl;
+
             COUT(4) << "Sound: processed buffers: " << processed << std::endl;
 
@@ -138,10 +83,17 @@
                 if (ALint error = alGetError())
                     COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl;
+            
+                int queued;
+                alGetSourcei(audioSource, AL_BUFFERS_QUEUED, &queued);
+                if (ALint error = alGetError())
+                    COUT(2) << "Sound: Warning: Couldn't get number of queued buffers: " << getALErrorString(error) << std::endl;
+                COUT(4) << "Sound: queued buffers: " << queued << std::endl;
 
                 for(int i = 0; i < processed; i++)
                 {
-                    long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
+                    long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
                     if (ret == 0)
                     {
+                        COUT(4) << "Sound: End of file " << dataStream->getName() << ", terminating thread" << std::endl;
                         return;
                     }
@@ -149,5 +101,5 @@
                     {
                         COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl;
-                        ov_clear(&vf);
+                        ov_clear(vf);
                         return;
                     }
@@ -164,4 +116,9 @@
                 }
             }
+            else
+            {
+                msleep(10); // perhaps another value here is better
+            }
+
             try {
                 boost::this_thread::interruption_point();
@@ -180,5 +137,4 @@
                 return;
             }
-            msleep(50); // perhaps another value here is better
         }
     }
Index: code/branches/sound5/src/orxonox/sound/SoundStreamer.h
===================================================================
--- code/branches/sound5/src/orxonox/sound/SoundStreamer.h	(revision 6929)
+++ code/branches/sound5/src/orxonox/sound/SoundStreamer.h	(revision 6931)
@@ -32,4 +32,5 @@
 
 #include <string>
+#include <vorbis/vorbisfile.h>
 #include <OgreDataStream.h>
 #include "core/CorePrereqs.h"
@@ -40,5 +41,5 @@
     {
     public:
-        void operator()(ALuint audioSource, DataStreamPtr dataStream);
+        void operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section);
     };
 }
