Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 20, 2010, 1:51:27 PM (14 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/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;
Note: See TracChangeset for help on using the changeset viewer.