Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 20, 2010, 1:51:27 PM (15 years ago)
Author:
erwin
Message:

Merged changes from local sandbox to sound5

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/sound5/src/orxonox/sound/SoundStreamer.cc

    r6769 r6931  
    2828
    2929#include <boost/thread.hpp>
    30 #include <al.h>
    31 #include <alc.h>
     30#include <AL/al.h>
     31#include <AL/alc.h>
    3232#include <vorbis/vorbisfile.h>
    3333#include "SoundManager.h"
     
    4141    long tellVorbis(void* datasource);
    4242
    43     void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream)
     43    void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section)
    4444    {
    45         COUT(4) << "Sound: Creating thread for " << dataStream->getName() << std::endl;
    46 
    47         alSourcei(audioSource, AL_BUFFER, 0);
    48 
    49         // Open file with custom streaming
    50         ov_callbacks vorbisCallbacks;
    51         vorbisCallbacks.read_func  = &readVorbis;
    52         vorbisCallbacks.seek_func  = &seekVorbis;
    53         vorbisCallbacks.tell_func  = &tellVorbis;
    54         vorbisCallbacks.close_func = NULL;
    55 
    56         OggVorbis_File vf;
    57         int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks);
    58         if (ret < 0)
    59         {
    60             COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
    61             ov_clear(&vf);
    62             return;
    63         }
     45        char inbuffer[4096];
    6446        vorbis_info* vorbisInfo;
    65         vorbisInfo = ov_info(&vf, -1);
     47        vorbisInfo = ov_info(vf, -1);
    6648        ALenum format;
    6749        if (vorbisInfo->channels == 1)
     
    6951        else
    7052            format = AL_FORMAT_STEREO16;
    71 
    72         char inbuffer[4096];
    73         ALuint initbuffers[5];
    74         alGenBuffers(5, initbuffers);
    75         if (ALint error = alGetError()) {
    76             COUT(2) << "Sound: Streamer: Could not generate buffer:" << getALErrorString(error) << std::endl;
    77             return;
    78         }
    79         int current_section;
    80 
    81         for(int i = 0; i < 5; i++)
    82         {
    83             long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
    84             if (ret == 0)
    85             {
    86                 break;
    87             }
    88             else if (ret < 0)
    89             {
    90                 COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
    91                 ov_clear(&vf);
    92                 return;
    93             }
    94 
    95             alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate);
    96             if(ALint error = alGetError()) {
    97                 COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl;
    98                 break;
    99              }
    100              alSourceQueueBuffers(audioSource, 1, &initbuffers[i]);
    101              if (ALint error = alGetError()) {
    102                  COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl;
    103              }
    104         }
    105 
    106         //alSourcei(audioSource, AL_LOOPING, AL_TRUE);
    107 
    108         alSourcePlay(audioSource);
    109         if(ALint error = alGetError())
    110             COUT(2) << "Sound: Could not start ambient sound" << getALErrorString(error) << std::endl;
    11153
    11254        while(true) // Stream forever, control through thread control
     
    11860                COUT(4) << "Sound: " << dataStream->getName() << " is playing." << std::endl;
    11961            else
     62            {
    12063                COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl;
     64            }
    12165
    12266            if(alcGetCurrentContext() == NULL)
     
    13074            if (ALint error = alGetError())
    13175                COUT(2) << "Sound: Warning: Couldn't get number of processed buffers: " << getALErrorString(error) << std::endl;
     76
    13277            COUT(4) << "Sound: processed buffers: " << processed << std::endl;
    13378
     
    13883                if (ALint error = alGetError())
    13984                    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;
    14091
    14192                for(int i = 0; i < processed; i++)
    14293                {
    143                     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);
    14495                    if (ret == 0)
    14596                    {
     97                        COUT(4) << "Sound: End of file " << dataStream->getName() << ", terminating thread" << std::endl;
    14698                        return;
    14799                    }
     
    149101                    {
    150102                        COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl;
    151                         ov_clear(&vf);
     103                        ov_clear(vf);
    152104                        return;
    153105                    }
     
    164116                }
    165117            }
     118            else
     119            {
     120                msleep(10); // perhaps another value here is better
     121            }
     122
    166123            try {
    167124                boost::this_thread::interruption_point();
     
    180137                return;
    181138            }
    182             msleep(50); // perhaps another value here is better
    183139        }
    184140    }
Note: See TracChangeset for help on using the changeset viewer.