Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 1, 2011, 5:10:29 AM (13 years ago)
Author:
rgrieder
Message:

Merged sound5 into sound6 branch.

Location:
code/branches/usability
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/usability

  • code/branches/usability/src/orxonox/sound/SoundStreamer.cc

    r7163 r8005  
    2727#include "SoundStreamer.h"
    2828
    29 #include <al.h>
     29#include <boost/thread.hpp>
     30#include <AL/al.h>
     31#include <AL/alc.h>
    3032#include <vorbis/vorbisfile.h>
    3133#include "SoundManager.h"
     34#include "util/Sleep.h"
    3235
    3336namespace orxonox
     
    3841    long tellVorbis(void* datasource);
    3942
    40     void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream)
     43    void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section)
    4144    {
    42         // Open file with custom streaming
    43         ov_callbacks vorbisCallbacks;
    44         vorbisCallbacks.read_func  = &readVorbis;
    45         vorbisCallbacks.seek_func  = &seekVorbis;
    46         vorbisCallbacks.tell_func  = &tellVorbis;
    47         vorbisCallbacks.close_func = NULL;
    48 
    49         OggVorbis_File vf;
    50         int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks);
    51         if (ret < 0)
    52         {
    53             COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
    54             ov_clear(&vf);
    55             return;
    56         }
     45        char inbuffer[4096];
    5746        vorbis_info* vorbisInfo;
    58         vorbisInfo = ov_info(&vf, -1);
     47        vorbisInfo = ov_info(vf, -1);
    5948        ALenum format;
    6049        if (vorbisInfo->channels == 1)
     
    6352            format = AL_FORMAT_STEREO16;
    6453
    65         char inbuffer[256*1024];
    66         ALuint initbuffers[4];
    67         alGenBuffers(4, initbuffers);
    68         int current_section;
     54        while(true) // Stream forever, control through thread control
     55        {
    6956
    70         for(int i = 0; i < 4; i++)
    71         {
    72             long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
    73             if (ret == 0)
     57            int info;
     58            alGetSourcei(audioSource, AL_SOURCE_STATE, &info);
     59            if(info == AL_PLAYING)
     60                COUT(4) << "Sound: " << dataStream->getName() << " is playing." << std::endl;
     61            else
    7462            {
    75                 return;
     63                COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl;
    7664            }
    77             else if (ret < 0)
     65
     66            if(alcGetCurrentContext() == NULL)
    7867            {
    79                 COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
    80                 ov_clear(&vf);
     68                COUT(2) << "Sound: There is no context, terminating thread for " << dataStream->getName() << std::endl;
    8169                return;
    8270            }
    8371
    84             alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate);
    85         }
    86         alSourceQueueBuffers(audioSource, 4, initbuffers);
    87 
    88         while(true) // Stream forever, control through thread control
    89         {
    9072            int processed;
    9173            alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed);
    9274            if (ALint error = alGetError())
    93             COUT(2) << "Sound Warning: Couldn't get number of processed buffers: "
    94                     << SoundManager::getALErrorString(error) << std::endl;
     75                COUT(2) << "Sound: Warning: Couldn't get number of processed buffers: " << getALErrorString(error) << std::endl;
     76
     77            COUT(4) << "Sound: processed buffers: " << processed << std::endl;
    9578
    9679            if(processed > 0)
     
    9982                alSourceUnqueueBuffers(audioSource, processed, buffers);
    10083                if (ALint error = alGetError())
    101                     COUT(2) << "Sound Warning: Couldn't unqueue buffers: "
    102                     << SoundManager::getALErrorString(error) << std::endl;
     84                    COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl;
     85           
     86                int queued;
     87                alGetSourcei(audioSource, AL_BUFFERS_QUEUED, &queued);
     88                if (ALint error = alGetError())
     89                    COUT(2) << "Sound: Warning: Couldn't get number of queued buffers: " << getALErrorString(error) << std::endl;
     90                COUT(4) << "Sound: queued buffers: " << queued << std::endl;
    10391
    10492                for(int i = 0; i < processed; i++)
    10593                {
    106                     long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
     94                    long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
    10795                    if (ret == 0)
    10896                    {
     97                        COUT(4) << "Sound: End of file " << dataStream->getName() << ", terminating thread" << std::endl;
    10998                        return;
    11099                    }
    111100                    else if (ret < 0)
    112101                    {
    113                         COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
    114                         ov_clear(&vf);
     102                        COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl;
     103                        ov_clear(vf);
    115104                        return;
    116105                    }
    117106
    118107                    alBufferData(buffers[i], format, &inbuffer, ret, vorbisInfo->rate);
     108                    if(ALint error = alGetError()) {
     109                        COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl;
     110                        break;
     111                    }
     112                    alSourceQueueBuffers(audioSource, 1, &buffers[i]);
     113                    if (ALint error = alGetError()) {
     114                        COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl;
     115                    }
    119116                }
     117            }
     118            else
     119            {
     120                msleep(10); // perhaps another value here is better
     121            }
    120122
    121                 alSourceQueueBuffers(audioSource, processed, buffers);
     123            try {
     124                boost::this_thread::interruption_point();
     125            }
     126            catch(boost::thread_interrupted) {
     127                COUT(4) << "Sound: Catched interruption. Terminating thread for " << dataStream->getName() << std::endl;
     128                ALuint* buffers = new ALuint[5];
     129                alSourceUnqueueBuffers(audioSource, 5, buffers);
    122130                if (ALint error = alGetError())
    123                     COUT(2) << "Sound Warning: Couldn't queue buffers: "
    124                     << SoundManager::getALErrorString(error) << std::endl;
     131                    COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl;
     132
     133                alDeleteBuffers(5, buffers);
     134                if (ALint error = alGetError())
     135                    COUT(2) << "Sound: Warning: Couldn't delete buffers: " << getALErrorString(error) << std::endl;
     136
     137                return;
    125138            }
    126139        }
Note: See TracChangeset for help on using the changeset viewer.