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

Location:
code/branches/sound5/src/orxonox/sound
Files:
3 edited

Legend:

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

    r6769 r6931  
    3636#include "SoundManager.h"
    3737#include "SoundStreamer.h"
     38#include "util/Sleep.h"
    3839
    3940#include <AL/alut.h>
     
    4243{
    4344    CreateFactory(AmbientSound);
    44 
     45   
     46    // vorbis callbacks
     47    size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource);
     48    int seekVorbis(void* datasource, ogg_int64_t offset, int whence);
     49    long tellVorbis(void* datasource);
     50   
    4551    AmbientSound::AmbientSound(BaseObject* creator)
    4652        : BaseObject(creator)
     
    5157
    5258        // Ambient sounds always fade in
    53         this->setVolume(0);
     59        //this->setVolume(0);
    5460        this->registerVariables();
    5561    }
     
    95101    void AmbientSound::stop()
    96102    {
    97         if (GameMode::playsSound())
     103        if (GameMode::playsSound()) 
    98104            SoundManager::getInstance().unregisterAmbientSound(this);
    99105    }
     
    170176        if (this->soundstreamthread_.get_id() != boost::thread::id())
    171177        {
    172             this->soundstreamthread_.interrupt(); // unhandled interruptions lead to thread terminating ;-)
    173         }
     178            this->soundstreamthread_.interrupt(); // terminate an old thread if necessary
     179        }
     180
     181        // queue some init buffers
     182        COUT(4) << "Sound: Creating thread for " << source << std::endl;
    174183        // Get resource info
    175184        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source);
     
    181190        // Open data stream
    182191        DataStreamPtr dataStream = Resource::open(fileInfo);
    183 
    184         this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream);
     192       
     193        alSourcei(this->audioSource_, AL_BUFFER, 0);
     194
     195        // Open file with custom streaming
     196        ov_callbacks vorbisCallbacks;
     197        vorbisCallbacks.read_func  = &readVorbis;
     198        vorbisCallbacks.seek_func  = &seekVorbis;
     199        vorbisCallbacks.tell_func  = &tellVorbis;
     200        vorbisCallbacks.close_func = NULL;
     201
     202        OggVorbis_File* vf = new OggVorbis_File();
     203        int ret = ov_open_callbacks(dataStream.get(), vf, NULL, 0, vorbisCallbacks);
     204        if (ret < 0)
     205        {
     206            COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
     207            ov_clear(vf);
     208            return;
     209        }
     210        vorbis_info* vorbisInfo;
     211        vorbisInfo = ov_info(vf, -1);
     212        ALenum format;
     213        if (vorbisInfo->channels == 1)
     214            format = AL_FORMAT_MONO16;
     215        else
     216            format = AL_FORMAT_STEREO16;
     217
     218        char inbuffer[4096];
     219        ALuint initbuffers[10];
     220        alGenBuffers(10, initbuffers);
     221        if (ALint error = alGetError()) {
     222            COUT(2) << "Sound: Streamer: Could not generate buffer:" << getALErrorString(error) << std::endl;
     223            return;
     224        }
     225        int current_section;
     226
     227        for(int i = 0; i < 10; i++)
     228        {
     229            long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
     230            if (ret == 0)
     231            {
     232                break;
     233            }
     234            else if (ret < 0)
     235            {
     236                COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
     237                ov_clear(vf);
     238                return;
     239            }
     240
     241            alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate);
     242            if(ALint error = alGetError()) {
     243                COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl;
     244                break;
     245             }
     246             alSourceQueueBuffers(this->audioSource_, 1, &initbuffers[i]);
     247             if (ALint error = alGetError()) {
     248                 COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl;
     249             }
     250        }
     251       
     252        this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream, vf, current_section);
    185253        if(this->soundstreamthread_ == boost::thread())
    186254            COUT(2) << "Sound: Failed to create thread." << std::endl;
  • 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    }
  • code/branches/sound5/src/orxonox/sound/SoundStreamer.h

    r6506 r6931  
    3232
    3333#include <string>
     34#include <vorbis/vorbisfile.h>
    3435#include <OgreDataStream.h>
    3536#include "core/CorePrereqs.h"
     
    4041    {
    4142    public:
    42         void operator()(ALuint audioSource, DataStreamPtr dataStream);
     43        void operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section);
    4344    };
    4445}
Note: See TracChangeset for help on using the changeset viewer.