Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 22, 2009, 4:01:16 PM (14 years ago)
Author:
rgrieder
Message:

Merged sound3 branch to presentation2.

Location:
code/branches/presentation2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2

  • code/branches/presentation2/src/orxonox/sound/BaseSound.cc

    r5929 r6117  
    2929#include "BaseSound.h"
    3030
     31#include <cassert>
    3132#include <vector>
    3233#include <AL/alut.h>
     
    3637#include "core/GameMode.h"
    3738#include "core/Resource.h"
     39#include "core/XMLPort.h"
    3840
    3941namespace orxonox
     
    4244        : audioSource_(0)
    4345        , audioBuffer_(0)
    44         , bPlayOnLoad_(false)
    4546        , bLoop_(false)
     47        , state_(Stopped)
    4648    {
    4749        RegisterRootObject(BaseSound);
     50
     51        if (GameMode::playsSound())
     52        {
     53            alGenSources(1, &this->audioSource_);
     54            assert(this->audioSource_ != 0);
     55        }
    4856    }
    4957
     
    5159    {
    5260        this->setSource("");
     61        if (GameMode::playsSound())
     62            alDeleteSources(1, &this->audioSource_);
     63    }
     64
     65    void BaseSound::XMLPortExtern(Element& xmlelement, XMLPort::Mode mode)
     66    {
     67        XMLPortParam(BaseSound, "volume", setVolume,  getVolume,  xmlelement, mode);
     68        XMLPortParam(BaseSound, "loop",   setLooping, getLooping, xmlelement, mode);
     69        XMLPortParam(BaseSound, "play",   play,       isPlaying,  xmlelement, mode);
     70        XMLPortParam(BaseSound, "source", setSource,  getSource,  xmlelement, mode);
    5371    }
    5472
    5573    void BaseSound::play()
    5674    {
     75        if (!this->isPlaying() && GameMode::showsGraphics())
     76        {
     77            this->state_ = Playing;
     78            alSourcePlay(this->audioSource_);
     79
     80            if (alGetError() != AL_NO_ERROR)
     81                 COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
     82        }
     83    }
     84
     85    void BaseSound::stop()
     86    {
     87        this->state_ = Stopped;
     88        if (GameMode::playsSound())
     89            alSourceStop(this->audioSource_);
     90    }
     91
     92    void BaseSound::pause()
     93    {
     94        if (this->isStopped())
     95            return;
     96        this->state_ = Paused;
     97        if (GameMode::playsSound())
     98            alSourcePause(this->audioSource_);
     99    }
     100
     101    void BaseSound::setVolume(float vol)
     102    {
     103        if (vol > 1 || vol < 0)
     104        {
     105            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
     106            vol = vol > 1 ? 1 : vol;
     107            vol = vol < 0 ? 0 : vol;
     108        }
     109        this->volume_ = vol;
    57110        if (alIsSource(this->audioSource_))
    58         {
    59             if (this->bLoop_)
    60                 alSourcei(this->audioSource_, AL_LOOPING, AL_TRUE);
    61             else
    62                 alSourcei(this->audioSource_, AL_LOOPING, AL_FALSE);
    63             alSourcePlay(this->audioSource_);
    64 
    65             if (alGetError() != AL_NO_ERROR)
    66             {
    67                  COUT(2) << "Sound: OpenAL: Error playin sound " << this->audioSource_ << std::endl;
    68             }
    69         }
    70     }
    71 
    72     void BaseSound::stop()
    73     {
    74         if (alIsSource(this->audioSource_))
     111            alSourcef(this->audioSource_, AL_GAIN, vol);
     112    }
     113
     114    void BaseSound::setLooping(bool val)
     115    {
     116        this->bLoop_ = val;
     117        if (GameMode::playsSound())
     118            alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE));
     119    }
     120
     121    void BaseSound::setSource(const std::string& source)
     122    {
     123        if (!GameMode::playsSound() || source == this->source_)
     124        {
     125            this->source_ = source;
     126            return;
     127        }
     128
     129        if (this->audioBuffer_ != 0 && alIsBuffer(this->audioBuffer_))
     130        {
    75131            alSourceStop(this->audioSource_);
    76     }
    77 
    78     void BaseSound::pause()
    79     {
    80         if (alIsSource(this->audioSource_))
    81             alSourcePause(this->audioSource_);
    82     }
    83 
    84     bool BaseSound::isPlaying()
    85     {
    86         if (alIsSource(this->audioSource_))
    87             return getSourceState() == AL_PLAYING;
    88         return false;
    89     }
    90 
    91     bool BaseSound::isPaused()
    92     {
    93         if (alIsSource(this->audioSource_))
    94             return getSourceState() == AL_PAUSED;
    95         return true;
    96     }
    97 
    98     bool BaseSound::isStopped()
    99     {
    100         if (alIsSource(this->audioSource_))
    101             return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
    102         return true;
    103     }
    104 
    105     void BaseSound::setPlayOnLoad(bool val)
    106     {
    107         this->bPlayOnLoad_ = true;
    108         this->play();
    109     }
    110 
    111     void BaseSound::setSource(const std::string& source)
    112     {
     132            // Unload old sound first
     133            alSourcei(this->audioSource_, AL_BUFFER, 0);
     134            alDeleteBuffers(1, &this->audioBuffer_);
     135            this->audioBuffer_ = 0;
     136        }
     137
    113138        this->source_ = source;
    114         if (!GameMode::playsSound())
    115             return;
    116 
    117         if (source.empty() && alIsSource(this->audioSource_))
    118         {
    119             // Unload sound
    120             alSourcei(this->audioSource_, AL_BUFFER, 0);
    121             alDeleteSources(1, &this->audioSource_);
    122             alDeleteBuffers(1, &this->audioBuffer_);
    123             return;
    124         }
     139        if (source_.empty())
     140            return;
    125141
    126142        COUT(3) << "Sound: OpenAL ALUT: loading file " << source << std::endl;
     
    129145        if (fileInfo == NULL)
    130146        {
    131             COUT(2) << "Warning: Sound file '" << source << "' not found" << std::endl;
     147            COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl;
    132148            return;
    133149        }
     
    147163            {
    148164                COUT(2) << "Sound: Trying fallback ogg loader" << std::endl;
    149                 this->audioBuffer_ = loadOggFile();
     165                this->audioBuffer_ = this->loadOggFile();
    150166            }
    151167
     
    157173        }
    158174
    159         alGenSources(1, &this->audioSource_);
    160175        alSourcei(this->audioSource_, AL_BUFFER, this->audioBuffer_);
    161176        if (alGetError() != AL_NO_ERROR)
     
    166181
    167182        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
    168 
    169         if (this->bPlayOnLoad_)
    170             this->play();
    171     }
    172 
    173     ALint BaseSound::getSourceState()
    174     {
    175         ALint state;
    176         alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state);
    177         return state;
     183        alSourcef (this->audioSource_, AL_GAIN, this->volume_);
     184        alSourcei (this->audioSource_, AL_LOOPING, (this->bLoop_ ? AL_TRUE : AL_FALSE));
     185        if (this->isPlaying() || this->isPaused())
     186            alSourcePlay(this->audioSource_);
     187        if (this->isPaused())
     188            alSourcePause(this->audioSource_);
     189
     190        if (alGetError() != AL_NO_ERROR)
     191            COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
    178192    }
    179193
     
    259273        return buffer;
    260274    }
    261 
    262 } // namespace: orxonox
     275}
Note: See TracChangeset for help on using the changeset viewer.