- Timestamp:
- Nov 22, 2009, 4:01:16 PM (14 years ago)
- Location:
- code/branches/presentation2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation2
- Property svn:mergeinfo changed
/code/branches/sound3 (added) merged: 5941,5943,5946,5954,5956-5957,5962,5982,6031,6046,6069-6072,6074,6088,6093,6097,6100,6102
- Property svn:mergeinfo changed
-
code/branches/presentation2/src/orxonox/sound/BaseSound.cc
r5929 r6117 29 29 #include "BaseSound.h" 30 30 31 #include <cassert> 31 32 #include <vector> 32 33 #include <AL/alut.h> … … 36 37 #include "core/GameMode.h" 37 38 #include "core/Resource.h" 39 #include "core/XMLPort.h" 38 40 39 41 namespace orxonox … … 42 44 : audioSource_(0) 43 45 , audioBuffer_(0) 44 , bPlayOnLoad_(false)45 46 , bLoop_(false) 47 , state_(Stopped) 46 48 { 47 49 RegisterRootObject(BaseSound); 50 51 if (GameMode::playsSound()) 52 { 53 alGenSources(1, &this->audioSource_); 54 assert(this->audioSource_ != 0); 55 } 48 56 } 49 57 … … 51 59 { 52 60 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); 53 71 } 54 72 55 73 void BaseSound::play() 56 74 { 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; 57 110 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 { 75 131 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 113 138 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; 125 141 126 142 COUT(3) << "Sound: OpenAL ALUT: loading file " << source << std::endl; … … 129 145 if (fileInfo == NULL) 130 146 { 131 COUT(2) << " Warning: Sound file '" << source << "' not found" << std::endl;147 COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl; 132 148 return; 133 149 } … … 147 163 { 148 164 COUT(2) << "Sound: Trying fallback ogg loader" << std::endl; 149 this->audioBuffer_ = loadOggFile();165 this->audioBuffer_ = this->loadOggFile(); 150 166 } 151 167 … … 157 173 } 158 174 159 alGenSources(1, &this->audioSource_);160 175 alSourcei(this->audioSource_, AL_BUFFER, this->audioBuffer_); 161 176 if (alGetError() != AL_NO_ERROR) … … 166 181 167 182 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; 178 192 } 179 193 … … 259 273 return buffer; 260 274 } 261 262 } // namespace: orxonox 275 }
Note: See TracChangeset
for help on using the changeset viewer.