[Orxonox-commit 2215] r6931 - code/branches/sound5/src/orxonox/sound

erwin at orxonox.net erwin at orxonox.net
Thu May 20 13:51:27 CEST 2010


Author: erwin
Date: 2010-05-20 13:51:27 +0200 (Thu, 20 May 2010)
New Revision: 6931

Modified:
   code/branches/sound5/src/orxonox/sound/AmbientSound.cc
   code/branches/sound5/src/orxonox/sound/SoundStreamer.cc
   code/branches/sound5/src/orxonox/sound/SoundStreamer.h
Log:
Merged changes from local sandbox to sound5

Modified: code/branches/sound5/src/orxonox/sound/AmbientSound.cc
===================================================================
--- code/branches/sound5/src/orxonox/sound/AmbientSound.cc	2010-05-20 11:11:11 UTC (rev 6930)
+++ code/branches/sound5/src/orxonox/sound/AmbientSound.cc	2010-05-20 11:51:27 UTC (rev 6931)
@@ -35,13 +35,19 @@
 #include "core/XMLPort.h"
 #include "SoundManager.h"
 #include "SoundStreamer.h"
+#include "util/Sleep.h"
 
 #include <AL/alut.h>
 
 namespace orxonox
 {
     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)
         , Synchronisable(creator)
@@ -50,7 +56,7 @@
         RegisterObject(AmbientSound);
 
         // Ambient sounds always fade in
-        this->setVolume(0);
+        //this->setVolume(0);
         this->registerVariables();
     }
 
@@ -94,7 +100,7 @@
 
     void AmbientSound::stop()
     {
-        if (GameMode::playsSound())
+        if (GameMode::playsSound()) 
             SoundManager::getInstance().unregisterAmbientSound(this);
     }
 
@@ -169,8 +175,11 @@
 
         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);
         if (fileInfo == NULL)
@@ -180,8 +189,67 @@
         }
         // Open data stream
         DataStreamPtr dataStream = Resource::open(fileInfo);
+        
+        alSourcei(this->audioSource_, AL_BUFFER, 0);
 
-        this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream);
+        // 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;
         //SoundStreamer streamer;

Modified: code/branches/sound5/src/orxonox/sound/SoundStreamer.cc
===================================================================
--- code/branches/sound5/src/orxonox/sound/SoundStreamer.cc	2010-05-20 11:11:11 UTC (rev 6930)
+++ code/branches/sound5/src/orxonox/sound/SoundStreamer.cc	2010-05-20 11:51:27 UTC (rev 6931)
@@ -27,8 +27,8 @@
 #include "SoundStreamer.h"
 
 #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"
 #include "util/Sleep.h"
@@ -40,75 +40,17 @@
     int seekVorbis(void* datasource, ogg_int64_t offset, int whence);
     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)
             format = AL_FORMAT_MONO16;
         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
         {
 
@@ -117,7 +59,9 @@
             if(info == AL_PLAYING)
                 COUT(4) << "Sound: " << dataStream->getName() << " is playing." << std::endl;
             else
+            {
                 COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl;
+            }
 
             if(alcGetCurrentContext() == NULL)
             {
@@ -129,6 +73,7 @@
             alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed);
             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;
 
             if(processed > 0)
@@ -137,18 +82,25 @@
                 alSourceUnqueueBuffers(audioSource, processed, buffers);
                 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;
                     }
                     else if (ret < 0)
                     {
                         COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl;
-                        ov_clear(&vf);
+                        ov_clear(vf);
                         return;
                     }
 
@@ -163,6 +115,11 @@
                     }
                 }
             }
+            else
+            {
+                msleep(10); // perhaps another value here is better
+            }
+
             try {
                 boost::this_thread::interruption_point();
             }
@@ -179,7 +136,6 @@
 
                 return;
             }
-            msleep(50); // perhaps another value here is better
         }
     }
 }

Modified: code/branches/sound5/src/orxonox/sound/SoundStreamer.h
===================================================================
--- code/branches/sound5/src/orxonox/sound/SoundStreamer.h	2010-05-20 11:11:11 UTC (rev 6930)
+++ code/branches/sound5/src/orxonox/sound/SoundStreamer.h	2010-05-20 11:51:27 UTC (rev 6931)
@@ -31,6 +31,7 @@
 #include "sound/SoundPrereqs.h"
 
 #include <string>
+#include <vorbis/vorbisfile.h>
 #include <OgreDataStream.h>
 #include "core/CorePrereqs.h"
 
@@ -39,7 +40,7 @@
     class _OrxonoxExport SoundStreamer // class for stream threads
     {
     public:
-        void operator()(ALuint audioSource, DataStreamPtr dataStream);
+        void operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section);
     };
 }
 




More information about the Orxonox-commit mailing list