Changeset 6322 for code/branches/presentation2/src/orxonox
- Timestamp:
- Dec 11, 2009, 2:15:43 PM (15 years ago)
- Location:
- code/branches/presentation2/src/orxonox/sound
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation2/src/orxonox/sound/AmbientSound.cc
r6320 r6322 119 119 float AmbientSound::getVolumeGain() 120 120 { 121 assert(GameMode::playsSound()); 121 122 return SoundManager::getInstance().getVolume(SoundType::ambient); 122 123 } -
code/branches/presentation2/src/orxonox/sound/AmbientSound.h
r6307 r6322 50 50 public: 51 51 AmbientSound(BaseObject* creator); 52 virtual~AmbientSound();52 ~AmbientSound(); 53 53 54 v irtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);55 v irtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);56 v irtual void changedActivity();54 void XMLPort(Element& xmlelement, XMLPort::Mode mode); 55 void XMLEventPort(Element& xmlelement, XMLPort::Mode mode); 56 void changedActivity(); 57 57 58 v irtual void play();59 v irtual void stop();60 v irtual void pause();58 void play(); 59 void stop(); 60 void pause(); 61 61 62 virtualfloat getVolumeGain();62 float getVolumeGain(); 63 63 64 v irtual void setAmbientSource(const std::string& source);64 void setAmbientSource(const std::string& source); 65 65 const std::string& getAmbientSource() const { return this->ambientSource_; } 66 66 inline void ambientSourceChanged(){ this->setAmbientSource(this->ambientSource_); } -
code/branches/presentation2/src/orxonox/sound/BaseSound.cc
r6320 r6322 43 43 { 44 44 BaseSound::BaseSound() 45 : audioSource_(0) 46 , bPooling_(false) 45 : bPooling_(false) 47 46 , volume_(1.0) 48 47 , bLooping_(false) … … 52 51 RegisterRootObject(BaseSound); 53 52 54 if (GameMode::playsSound()) 55 { 56 alGenSources(1, &this->audioSource_); 57 if (!alIsSource(this->audioSource_)) 58 COUT(1) << "Sound: Source generation failed: " << SoundManager::getALErrorString(alGetError()) << std::endl; 59 60 if (alIsSource(this->audioSource_)) 61 { 62 alSourcei(this->audioSource_, AL_REFERENCE_DISTANCE, 20); 63 alSourcei(this->audioSource_, AL_MAX_DISTANCE, 10000); 64 } 65 } 53 // Initialise audioSource_ to a value that is not a source 54 // 0 is unfortunately not guaranteed to be no source ID. 55 this->audioSource_ = 123456789; 56 while (alIsSource(++this->audioSource_)); 66 57 } 67 58 68 59 BaseSound::~BaseSound() 69 60 { 70 this->setSource(std::string()); 71 if (GameMode::playsSound() && alIsSource(this->audioSource_)) 72 alDeleteSources(1, &this->audioSource_); 61 this->stop(); 73 62 } 74 63 … … 84 73 { 85 74 this->state_ = Playing; 86 if (GameMode::playsSound() && alIsSource(this->audioSource_) && this->getSourceState() != AL_PLAYING) 87 { 75 if (GameMode::playsSound() && this->getSourceState() != AL_PLAYING && this->soundBuffer_ != NULL) 76 { 77 if (!alIsSource(this->audioSource_)) 78 this->audioSource_ = SoundManager::getInstance().getSoundSource(); 79 if (!alIsSource(this->audioSource_)) 80 return; 81 this->initialiseSource(); 82 88 83 alSourcePlay(this->audioSource_); 89 90 84 if (int error = alGetError()) 91 COUT(2) << "Sound: Error playing sound: " << error<< std::endl;85 COUT(2) << "Sound: Error playing sound: " << SoundManager::getALErrorString(error) << std::endl; 92 86 } 93 87 } … … 96 90 { 97 91 this->state_ = Stopped; 98 if (GameMode::playsSound() && alIsSource(this->audioSource_)) 92 if (alIsSource(this->audioSource_)) 93 { 99 94 alSourceStop(this->audioSource_); 95 // Release buffer 96 alSourcei(this->audioSource_, AL_BUFFER, AL_NONE); 97 // Release source again 98 SoundManager::getInstance().releaseSoundSource(this->audioSource_); 99 // Get a no source ID 100 this->audioSource_ += 123455; 101 while (alIsSource(++this->audioSource_)); 102 } 100 103 } 101 104 … … 105 108 return; 106 109 this->state_ = Paused; 107 if ( GameMode::playsSound() &&alIsSource(this->audioSource_))110 if (alIsSource(this->audioSource_)) 108 111 alSourcePause(this->audioSource_); 109 112 } … … 111 114 ALint BaseSound::getSourceState() const 112 115 { 113 if ( GameMode::playsSound() &&alIsSource(this->audioSource_))116 if (alIsSource(this->audioSource_)) 114 117 { 115 118 ALint state; … … 121 124 } 122 125 126 void BaseSound::initialiseSource() 127 { 128 this->updateVolume(); 129 this->setPitch(this->getPitch()); 130 this->setLooping(this->getLooping()); 131 alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0); 132 alSource3f(this->audioSource_, AL_VELOCITY, 0, 0, 0); 133 alSource3f(this->audioSource_, AL_DIRECTION, 0, 0, 0); 134 alSourcei(this->audioSource_, AL_REFERENCE_DISTANCE, 20); 135 alSourcei(this->audioSource_, AL_MAX_DISTANCE, 10000); 136 if (ALint error = alGetError()) 137 COUT(2) << "Sound Warning: Setting source parameters to 0 failed: " 138 << SoundManager::getALErrorString(error) << std::endl; 139 assert(this->soundBuffer_ != NULL); 140 alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer()); 141 if (ALuint error = alGetError()) 142 COUT(1) << "Sound Error: Could not set buffer \"" << this->source_ << "\": " << SoundManager::getALErrorString(error) << std::endl; 143 } 144 123 145 void BaseSound::setVolume(float vol) 124 146 { … … 136 158 float BaseSound::getVolumeGain() 137 159 { 160 assert(GameMode::playsSound()); 138 161 return SoundManager::getInstance().getVolume(SoundType::none); 139 162 } … … 143 166 if (alIsSource(this->audioSource_)) 144 167 { 145 alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain()); 168 float volume = this->volume_ * this->getVolumeGain(); 169 alSourcef(this->audioSource_, AL_GAIN, volume); 146 170 if (int error = alGetError()) 147 COUT(2) << "Sound: Error setting volume: " << error << std::endl; 171 COUT(2) << "Sound: Error setting volume to " << volume 172 << ": " << SoundManager::getALErrorString(error) << std::endl; 148 173 } 149 174 } … … 152 177 { 153 178 this->bLooping_ = val; 154 if ( GameMode::playsSound() &&alIsSource(this->audioSource_))179 if (alIsSource(this->audioSource_)) 155 180 alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE)); 156 181 } … … 165 190 } 166 191 this->pitch_ = pitch; 167 if ( GameMode::playsSound() &&alIsSource(this->audioSource_))192 if (alIsSource(this->audioSource_)) 168 193 { 169 194 if (int error = alGetError()) 170 COUT(2) << "Sound: Error setting pitch: " << error<< std::endl;195 COUT(2) << "Sound: Error setting pitch: " << SoundManager::getALErrorString(error) << std::endl; 171 196 alSourcef(this->audioSource_, AL_PITCH, pitch); 172 197 } … … 183 208 if (this->soundBuffer_ != NULL) 184 209 { 210 // Stopping is imperative here! 185 211 if (alIsSource(this->audioSource_)) 186 212 { 187 213 alSourceStop(this->audioSource_); 188 // Unload old buffer first 189 alSourcei(this->audioSource_, AL_BUFFER, 0); 214 alSourcei(this->audioSource_, AL_BUFFER, AL_NONE); 190 215 } 191 216 SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_); … … 194 219 195 220 this->source_ = source; 196 if (source_.empty() || !alIsSource(this->audioSource_))221 if (source_.empty()) 197 222 return; 198 223 … … 201 226 return; 202 227 203 alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer()); 204 if (ALuint error = alGetError()) 205 { 206 COUT(1) << "Sound Error: Could not load file \"" << source << "\": " << SoundManager::getALErrorString(error) << std::endl; 207 return; 208 } 209 210 alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0); 211 this->updateVolume(); 212 this->setPitch(this->getPitch()); 213 this->setLooping(getLooping()); 214 if (this->isPlaying() || this->isPaused()) 215 { 216 alSourcePlay(this->audioSource_); 217 if (int error = alGetError()) 218 COUT(2) << "Sound: Error playing sound: " << error << std::endl; 219 } 220 if (this->isPaused()) 221 alSourcePause(this->audioSource_); 228 if (alIsSource(this->audioSource_)) 229 { 230 alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer()); 231 if (ALuint error = alGetError()) 232 { 233 COUT(1) << "Sound Error: Could not set buffer \"" << source << "\": " << SoundManager::getALErrorString(error) << std::endl; 234 return; 235 } 236 237 if (this->isPlaying() || this->isPaused()) 238 { 239 alSourcePlay(this->audioSource_); 240 if (int error = alGetError()) 241 COUT(2) << "Sound: Error playing sound: " << SoundManager::getALErrorString(error) << std::endl; 242 } 243 if (this->isPaused()) 244 alSourcePause(this->audioSource_); 245 } 222 246 } 223 247 -
code/branches/presentation2/src/orxonox/sound/BaseSound.h
r6320 r6322 96 96 Paused 97 97 }; 98 virtual void initialiseSource(); 98 99 ALint getSourceState() const; 99 100 -
code/branches/presentation2/src/orxonox/sound/SoundManager.cc
r6298 r6322 87 87 COUT(1) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; 88 88 #endif 89 ThrowException(InitialisationFailed, "Sound : OpenAL error: Could not open sound device.");89 ThrowException(InitialisationFailed, "Sound Error: Could not open sound device."); 90 90 } 91 91 Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); … … 100 100 101 101 GameMode::setPlaysSound(true); 102 Loki::ScopeGuard resetPlaysSoundGuard = Loki::MakeGuard(&GameMode::setPlaysSound, false); 102 103 103 104 // Get some information about the sound … … 110 111 else 111 112 COUT(2) << "Sound Warning: MIME Type retrieval failed: " << alutGetErrorString(alutGetError()) << std::endl; 113 114 this->setVolumeInternal(1.0, SoundType::none); 115 this->setVolumeInternal(1.0, SoundType::ambient); 116 this->setVolumeInternal(1.0, SoundType::effects); 117 118 this->mute_[SoundType::none] = false; 119 this->mute_[SoundType::ambient] = false; 120 this->mute_[SoundType::effects] = false; 121 122 this->setConfigValues(); 123 124 // Try to get at least one source 125 ALuint source; 126 alGenSources(1, &source); 127 if (!alGetError() && alIsSource(source)) 128 this->soundSources_.push_back(source); 129 else 130 ThrowException(InitialisationFailed, "Sound Error: Could not even create a single source"); 131 // Get the rest of the sources 132 alGenSources(1, &source); 133 unsigned int count = 1; 134 while (alIsSource(source) && !alGetError() && count <= this->maxSources_) 135 { 136 this->soundSources_.push_back(source); 137 alGenSources(1, &source); 138 ++count; 139 } 112 140 113 141 // Disarm guards … … 115 143 closeDeviceGuard.Dismiss(); 116 144 desroyContextGuard.Dismiss(); 117 118 this->setVolumeInternal(1.0, SoundType::none); 119 this->setVolumeInternal(1.0, SoundType::ambient); 120 this->setVolumeInternal(1.0, SoundType::effects); 121 122 this->mute_[SoundType::none] = false; 123 this->mute_[SoundType::ambient] = false; 124 this->mute_[SoundType::effects] = false; 125 126 this->setConfigValues(); 145 resetPlaysSoundGuard.Dismiss(); 127 146 128 147 COUT(4) << "Sound: Initialisation complete" << std::endl; … … 164 183 .description("Determines how fast sounds should fade, per second.") 165 184 .callback(this, &SoundManager::checkFadeStepValidity); 166 185 167 186 SetConfigValue(soundVolume_, 1.0f) 168 187 .description("Defines the overall volume.") 169 188 .callback(this, &SoundManager::checkSoundVolumeValidity); 170 189 171 190 SetConfigValue(ambientVolume_, 1.0f) 172 191 .description("Defines the ambient volume.") 173 192 .callback(this, &SoundManager::checkAmbientVolumeValidity); 174 193 175 194 SetConfigValue(effectsVolume_, 1.0f) 176 195 .description("Defines the effects volume.") 177 196 .callback(this, &SoundManager::checkEffectsVolumeValidity); 197 198 SetConfigValue(maxSources_, 1024) 199 .description("Maximum number of sources to be made available"); 178 200 } 179 201 … … 595 617 } 596 618 } 619 620 ALuint SoundManager::getSoundSource() 621 { 622 if (!this->soundSources_.empty()) 623 { 624 ALuint source = this->soundSources_.back(); 625 this->soundSources_.pop_back(); 626 return source; 627 } 628 else 629 { 630 // Return no source ID 631 ALuint source = 123456789; 632 while (alIsSource(++source)); 633 return source; 634 } 635 } 636 637 void SoundManager::releaseSoundSource(ALuint source) 638 { 639 #ifndef NDEBUG 640 for (std::vector<ALuint>::const_iterator it = this->soundSources_.begin(); it != this->soundSources_.end(); ++it) 641 assert((*it) != source); 642 #endif 643 this->soundSources_.push_back(source); 644 } 597 645 } -
code/branches/presentation2/src/orxonox/sound/SoundManager.h
r6278 r6322 102 102 void releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer); 103 103 104 ALuint getSoundSource(); 105 void releaseSoundSource(ALuint source); 106 104 107 static std::string getALErrorString(ALenum error); 105 108 … … 144 147 typedef std::map<std::string, shared_ptr<SoundBuffer> > SoundBufferMap; 145 148 SoundBufferMap soundBuffers_; 149 150 unsigned int maxSources_; 151 std::vector<ALuint> soundSources_; 146 152 147 153 static SoundManager* singletonPtr_s; -
code/branches/presentation2/src/orxonox/sound/WorldSound.cc
r6320 r6322 76 76 } 77 77 78 void WorldSound::initialiseSource() 79 { 80 BaseSound::initialiseSource(); 81 this->tick(0); // update position, orientation and velocity 82 } 83 78 84 void WorldSound::tick(float dt) 79 85 { -
code/branches/presentation2/src/orxonox/sound/WorldSound.h
r6307 r6322 58 58 private: 59 59 void registerVariables(); 60 void initialiseSource(); 60 61 }; 61 62 }
Note: See TracChangeset
for help on using the changeset viewer.