Changeset 6071 for code/branches/sound3/src/orxonox/sound/BaseSound.cc
- Timestamp:
- Nov 15, 2009, 10:36:45 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/sound3/src/orxonox/sound/BaseSound.cc
r6070 r6071 29 29 #include "BaseSound.h" 30 30 31 #include <cassert> 31 32 #include <vector> 32 33 #include <AL/alut.h> … … 42 43 : audioSource_(0) 43 44 , audioBuffer_(0) 44 , bPlayOnLoad_(false)45 45 , bLoop_(false) 46 , state_(Stopped) 46 47 { 47 48 RegisterRootObject(BaseSound); 48 49 49 50 if (GameMode::playsSound()) 51 { 50 52 alGenSources(1, &this->audioSource_); 53 assert(this->audioSource_ != 0); 54 } 51 55 } 52 56 … … 54 58 { 55 59 this->setSource(""); 56 if ( this->audioSource_)60 if (GameMode::playsSound()) 57 61 alDeleteSources(1, &this->audioSource_); 58 62 } … … 60 64 void BaseSound::play() 61 65 { 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; 68 69 alSourcePlay(this->audioSource_); 69 70 70 71 if (alGetError() != AL_NO_ERROR) 71 {72 72 COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl; 73 }74 73 } 75 74 } … … 77 76 void BaseSound::stop() 78 77 { 79 if (alIsSource(this->audioSource_)) 78 this->state_ = Stopped; 79 if (GameMode::playsSound()) 80 80 alSourceStop(this->audioSource_); 81 81 } … … 83 83 void BaseSound::pause() 84 84 { 85 if (alIsSource(this->audioSource_)) 85 if (this->isStopped()) 86 return; 87 this->state_ = Paused; 88 if (GameMode::playsSound()) 86 89 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;108 90 } 109 91 … … 121 103 } 122 104 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 123 112 void BaseSound::setSource(const std::string& source) 124 113 { … … 128 117 return; 129 118 } 130 131 if ( alIsSource(this->audioBuffer_))132 { 133 this->stop();119 120 if (this->audioBuffer_ != 0 && alIsBuffer(this->audioBuffer_)) 121 { 122 alSourceStop(this->audioSource_); 134 123 // Unload old sound first 135 124 alSourcei(this->audioSource_, AL_BUFFER, 0); … … 183 172 184 173 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; 197 183 } 198 184
Note: See TracChangeset
for help on using the changeset viewer.