Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Reverted last commit (forgot to tick "Switch working copy to new branch"…)

Location:
code/branches/usability
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/usability

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

    r8005 r8006  
    2727#include "SoundStreamer.h"
    2828
    29 #include <boost/thread.hpp>
    30 #include <AL/al.h>
    31 #include <AL/alc.h>
     29#include <al.h>
    3230#include <vorbis/vorbisfile.h>
    3331#include "SoundManager.h"
    34 #include "util/Sleep.h"
    3532
    3633namespace orxonox
     
    4138    long tellVorbis(void* datasource);
    4239
    43     void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section)
     40    void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream)
    4441    {
    45         char inbuffer[4096];
     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        }
    4657        vorbis_info* vorbisInfo;
    47         vorbisInfo = ov_info(vf, -1);
     58        vorbisInfo = ov_info(&vf, -1);
    4859        ALenum format;
    4960        if (vorbisInfo->channels == 1)
     
    5263            format = AL_FORMAT_STEREO16;
    5364
    54         while(true) // Stream forever, control through thread control
     65        char inbuffer[256*1024];
     66        ALuint initbuffers[4];
     67        alGenBuffers(4, initbuffers);
     68        int current_section;
     69
     70        for(int i = 0; i < 4; i++)
    5571        {
    56 
    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
     72            long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
     73            if (ret == 0)
    6274            {
    63                 COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl;
     75                return;
    6476            }
    65 
    66             if(alcGetCurrentContext() == NULL)
     77            else if (ret < 0)
    6778            {
    68                 COUT(2) << "Sound: There is no context, terminating thread for " << dataStream->getName() << std::endl;
     79                COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
     80                ov_clear(&vf);
    6981                return;
    7082            }
    7183
     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        {
    7290            int processed;
    7391            alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed);
    7492            if (ALint error = alGetError())
    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;
     93            COUT(2) << "Sound Warning: Couldn't get number of processed buffers: "
     94                    << SoundManager::getALErrorString(error) << std::endl;
    7895
    7996            if(processed > 0)
     
    8299                alSourceUnqueueBuffers(audioSource, processed, buffers);
    83100                if (ALint error = alGetError())
    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;
     101                    COUT(2) << "Sound Warning: Couldn't unqueue buffers: "
     102                    << SoundManager::getALErrorString(error) << std::endl;
    91103
    92104                for(int i = 0; i < processed; i++)
    93105                {
    94                     long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
     106                    long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
    95107                    if (ret == 0)
    96108                    {
    97                         COUT(4) << "Sound: End of file " << dataStream->getName() << ", terminating thread" << std::endl;
    98109                        return;
    99110                    }
    100111                    else if (ret < 0)
    101112                    {
    102                         COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl;
    103                         ov_clear(vf);
     113                        COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
     114                        ov_clear(&vf);
    104115                        return;
    105116                    }
    106117
    107118                    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                     }
    116119                }
    117             }
    118             else
    119             {
    120                 msleep(10); // perhaps another value here is better
    121             }
    122120
    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);
     121                alSourceQueueBuffers(audioSource, processed, buffers);
    130122                if (ALint error = alGetError())
    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;
     123                    COUT(2) << "Sound Warning: Couldn't queue buffers: "
     124                    << SoundManager::getALErrorString(error) << std::endl;
    138125            }
    139126        }
Note: See TracChangeset for help on using the changeset viewer.