Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 15, 2009, 10:36:45 PM (16 years ago)
Author:
rgrieder
Message:

Improved synchronisability of the sound classes (not yet Synchronisable though!).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/sound3/src/orxonox/sound/BaseSound.cc

    r6070 r6071  
    2929#include "BaseSound.h"
    3030
     31#include <cassert>
    3132#include <vector>
    3233#include <AL/alut.h>
     
    4243        : audioSource_(0)
    4344        , audioBuffer_(0)
    44         , bPlayOnLoad_(false)
    4545        , bLoop_(false)
     46        , state_(Stopped)
    4647    {
    4748        RegisterRootObject(BaseSound);
    4849
    4950        if (GameMode::playsSound())
     51        {
    5052            alGenSources(1, &this->audioSource_);
     53            assert(this->audioSource_ != 0);
     54        }
    5155    }
    5256
     
    5458    {
    5559        this->setSource("");
    56         if (this->audioSource_)
     60        if (GameMode::playsSound())
    5761            alDeleteSources(1, &this->audioSource_);
    5862    }
     
    6064    void BaseSound::play()
    6165    {
    62         if (alIsSource(this->audioSource_))
    63         {
    64             if (this->bLoop_)
    65                 alSourcei(this->audioSource_, AL_LOOPING, AL_TRUE);
    66             else
    67                 alSourcei(this->audioSource_, AL_LOOPING, AL_FALSE);
     66        if (!this->isPlaying() && GameMode::showsGraphics())
     67        {
     68            this->state_ = Playing;
    6869            alSourcePlay(this->audioSource_);
    6970
    7071            if (alGetError() != AL_NO_ERROR)
    71             {
    7272                 COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
    73             }
    7473        }
    7574    }
     
    7776    void BaseSound::stop()
    7877    {
    79         if (alIsSource(this->audioSource_))
     78        this->state_ = Stopped;
     79        if (GameMode::playsSound())
    8080            alSourceStop(this->audioSource_);
    8181    }
     
    8383    void BaseSound::pause()
    8484    {
    85         if (alIsSource(this->audioSource_))
     85        if (this->isStopped())
     86            return;
     87        this->state_ = Paused;
     88        if (GameMode::playsSound())
    8689            alSourcePause(this->audioSource_);
    87     }
    88 
    89     bool BaseSound::isPlaying()
    90     {
    91         if (alIsSource(this->audioSource_))
    92             return getSourceState() == AL_PLAYING;
    93         return false;
    94     }
    95 
    96     bool BaseSound::isPaused()
    97     {
    98         if (alIsSource(this->audioSource_))
    99             return getSourceState() == AL_PAUSED;
    100         return false;
    101     }
    102 
    103     bool BaseSound::isStopped()
    104     {
    105         if (alIsSource(this->audioSource_))
    106             return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
    107         return true;
    10890    }
    10991
     
    121103    }
    122104
     105    void BaseSound::setLooping(bool val)
     106    {
     107        this->bLoop_ = val;
     108        if (GameMode::playsSound())
     109            alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE));
     110    }
     111
    123112    void BaseSound::setSource(const std::string& source)
    124113    {
     
    128117            return;
    129118        }
    130        
    131         if (alIsSource(this->audioBuffer_))
    132         {
    133             this->stop();
     119
     120        if (this->audioBuffer_ != 0 && alIsBuffer(this->audioBuffer_))
     121        {
     122            alSourceStop(this->audioSource_);
    134123            // Unload old sound first
    135124            alSourcei(this->audioSource_, AL_BUFFER, 0);
     
    183172
    184173        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
    185 
    186         this->setVolume(this->volume_);
    187 
    188         if (this->bPlayOnLoad_)
    189             this->play();
    190     }
    191 
    192     ALint BaseSound::getSourceState()
    193     {
    194         ALint state;
    195         alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state);
    196         return state;
     174        alSourcef (this->audioSource_, AL_GAIN, this->volume_);
     175        alSourcei (this->audioSource_, AL_LOOPING, (this->bLoop_ ? AL_TRUE : AL_FALSE));
     176        if (this->isPlaying() || this->isPaused())
     177            alSourcePlay(this->audioSource_);
     178        if (this->isPaused())
     179            alSourcePause(this->audioSource_);
     180
     181        if (alGetError() != AL_NO_ERROR)
     182            COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
    197183    }
    198184
Note: See TracChangeset for help on using the changeset viewer.