Changeset 6370 for code/branches/presentation2/src/orxonox/sound
- Timestamp:
- Dec 17, 2009, 11:10:39 AM (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
r6349 r6370 67 67 { 68 68 registerVariable(ambientSource_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::ambientSourceChanged)); 69 registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback< BaseSound>(static_cast<BaseSound*>(this), &BaseSound::loopingChanged));70 registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback< BaseSound>(static_cast<BaseSound*>(this), &BaseSound::pitchChanged));71 registerVariable((int&)(BaseSound::state_), ObjectDirection::ToClient, new NetworkCallback< BaseSound>(static_cast<BaseSound*>(this), &BaseSound::stateChanged));69 registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::loopingChanged)); 70 registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::pitchChanged)); 71 registerVariable((int&)(BaseSound::state_), ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::stateChanged)); 72 72 } 73 73 … … 125 125 } 126 126 127 float AmbientSound::get VolumeGain()127 float AmbientSound::getRealVolume() 128 128 { 129 129 assert(GameMode::playsSound()); 130 return SoundManager::getInstance().get Volume(SoundType::ambient);130 return SoundManager::getInstance().getRealVolume(SoundType::Music); 131 131 } 132 132 -
code/branches/presentation2/src/orxonox/sound/AmbientSound.h
r6349 r6370 59 59 void stop(); 60 60 void pause(); 61 62 float getVolumeGain();63 61 64 62 void setAmbientSource(const std::string& source); … … 71 69 void doStop(); 72 70 void doPause(); 73 74 71 void registerVariables(); 72 float getRealVolume(); 75 73 76 74 std::string ambientSource_; //!< Analogous to source_, but mood independent -
code/branches/presentation2/src/orxonox/sound/BaseSound.cc
r6340 r6370 145 145 void BaseSound::setVolume(float vol) 146 146 { 147 if (vol > 1 || vol < 0) 148 { 149 COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl; 150 vol = vol > 1 ? 1 : vol; 151 vol = vol < 0 ? 0 : vol; 152 } 153 this->volume_ = vol; 154 147 this->volume_ = clamp(vol, 0.0f, 1.0f); 148 if (this->volume_ != vol) 149 COUT(2) << "Sound warning: volume out of range, clamping value." << std::endl; 155 150 this->updateVolume(); 156 151 } 157 152 158 float BaseSound::getVolumeGain() 159 { 160 assert(GameMode::playsSound()); 161 return SoundManager::getInstance().getVolume(SoundType::none); 162 } 163 164 void BaseSound::updateVolume(void) 165 { 166 if (alIsSource(this->audioSource_)) 167 { 168 float volume = this->volume_ * this->getVolumeGain(); 153 void BaseSound::updateVolume() 154 { 155 if (alIsSource(this->audioSource_)) 156 { 157 float volume = this->volume_ * this->getRealVolume(); 169 158 alSourcef(this->audioSource_, AL_GAIN, volume); 170 159 if (int error = alGetError()) -
code/branches/presentation2/src/orxonox/sound/BaseSound.h
r6322 r6370 67 67 68 68 virtual void setSource(const std::string& source); 69 virtual const std::string& getSource() const { return this->source_; }70 inline void sourceChanged(){ this->setSource(this->source_); }69 virtual const std::string& getSource() const 70 { return this->source_; } 71 71 72 72 void setVolume(float vol); 73 float getVolume() const { return this->volume_; } 74 inline void volumeChanged(){ this->setVolume(this->volume_); } 75 76 virtual float getVolumeGain(); 77 void updateVolume(void); 73 float getVolume() const 74 { return this->volume_; } 75 void updateVolume(); 78 76 79 bool getLooping() const { return this->bLooping_; } 77 bool getLooping() const 78 { return this->bLooping_; } 80 79 void setLooping(bool val); 81 inline void loopingChanged(){ this->setLooping(this->bLooping_); }82 80 83 float getPitch() const { return this->pitch_; } 81 float getPitch() const 82 { return this->pitch_; } 84 83 void setPitch(float pitch); 85 inline void pitchChanged(){ this->setPitch(this->pitch_); }86 87 void stateChanged();88 89 //ALuint getALAudioSource(void);90 84 91 85 protected: … … 96 90 Paused 97 91 }; 92 93 // network callbacks 94 inline void pitchChanged() 95 { this->setPitch(this->pitch_); } 96 inline void loopingChanged() 97 { this->setLooping(this->bLooping_); } 98 inline void volumeChanged() 99 { this->setVolume(this->volume_); } 100 inline void sourceChanged() 101 { this->setSource(this->source_); } 102 void stateChanged(); 103 98 104 virtual void initialiseSource(); 99 105 ALint getSourceState() const; 106 107 virtual float getRealVolume() = 0; 100 108 101 109 ALuint audioSource_; -
code/branches/presentation2/src/orxonox/sound/SoundManager.cc
r6349 r6370 51 51 ManageScopedSingleton(SoundManager, ScopeID::Graphics, true); 52 52 53 std::string SoundManager::getALErrorString(ALenum code) 54 { 55 switch (code) 56 { 57 case AL_NO_ERROR: return "No error"; 58 case AL_INVALID_NAME: return "Invalid AL parameter name"; 59 case AL_INVALID_ENUM: return "Invalid AL enum"; 60 case AL_INVALID_VALUE: return "Invalid AL value"; 61 case AL_INVALID_OPERATION: return "Invalid AL operation"; 62 case AL_OUT_OF_MEMORY: return "AL reports out of memory"; 63 default: return "Unknown AL error"; 64 } 65 } 66 53 67 SoundManager::SoundManager() 54 68 : effectsPoolSize_(0) … … 114 128 COUT(2) << "Sound Warning: MIME Type retrieval failed: " << alutGetErrorString(alutGetError()) << std::endl; 115 129 116 this->setVolumeInternal(1.0, SoundType::none); 117 this->setVolumeInternal(1.0, SoundType::ambient); 118 this->setVolumeInternal(1.0, SoundType::effects); 119 120 this->mute_[SoundType::none] = false; 121 this->mute_[SoundType::ambient] = false; 122 this->mute_[SoundType::effects] = false; 130 this->mute_[SoundType::All] = 1.0f; 131 this->mute_[SoundType::Music] = 1.0f; 132 this->mute_[SoundType::Effects] = 1.0f; 123 133 124 134 this->setConfigValues(); … … 186 196 .callback(this, &SoundManager::checkFadeStepValidity); 187 197 188 SetConfigValue (soundVolume_, 1.0f)198 SetConfigValueAlias(volume_[SoundType::All], "soundVolume_", 1.0f) 189 199 .description("Defines the overall volume.") 190 200 .callback(this, &SoundManager::checkSoundVolumeValidity); 191 192 SetConfigValue(ambientVolume_, 1.0f) 201 SetConfigValueAlias(volume_[SoundType::Music], "ambientVolume_", 1.0f) 193 202 .description("Defines the ambient volume.") 194 203 .callback(this, &SoundManager::checkAmbientVolumeValidity); 195 196 SetConfigValue(effectsVolume_, 1.0f) 204 SetConfigValueAlias(volume_[SoundType::Effects], "effectsVolume_", 1.0f) 197 205 .description("Defines the effects volume.") 198 206 .callback(this, &SoundManager::checkEffectsVolumeValidity); … … 202 210 } 203 211 204 std::string SoundManager::getALErrorString(ALenum code)205 {206 switch (code)207 {208 case AL_NO_ERROR: return "No error";209 case AL_INVALID_NAME: return "Invalid AL parameter name";210 case AL_INVALID_ENUM: return "Invalid AL enum";211 case AL_INVALID_VALUE: return "Invalid AL value";212 case AL_INVALID_OPERATION: return "Invalid AL operation";213 case AL_OUT_OF_MEMORY: return "AL reports out of memory";214 default: return "Unknown AL error";215 }216 }217 218 212 void SoundManager::checkFadeStepValidity() 219 213 { 220 214 if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 ) 221 215 { 222 COUT(2) << "Sound warning: Soundstep out of range, ignoring change." << std::endl;216 COUT(2) << "Sound warning: fade step out of range, ignoring change." << std::endl; 223 217 ResetConfigValue(crossFadeStep_); 224 218 } 225 COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl; 226 return; 227 } 228 229 bool SoundManager::checkVolumeValidity(SoundType::Value type) 230 { 231 bool valid = true; 232 233 if(this->getVolumeInternal(type) < 0.0 || this->getVolumeInternal(type) > 1.0) 234 { 235 COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl; 236 valid = false; 237 } 238 219 } 220 221 void SoundManager::checkVolumeValidity(SoundType::Value type) 222 { 223 float clampedVolume = clamp(this->volume_[type], 0.0f, 1.0f); 224 if (clampedVolume != this->volume_[type]) 225 COUT(2) << "Sound warning: Volume setting (" << type << ") out of range, clamping." << std::endl; 239 226 this->updateVolume(type); 240 COUT(4) << "SoundManager: volume set to " << this->getVolumeInternal(type) << std::endl; 241 return valid; 242 } 243 244 void SoundManager::checkSoundVolumeValidity() 245 { 246 if(!checkVolumeValidity(SoundType::none)) 247 { 248 ResetConfigValue(soundVolume_); 249 } 250 } 251 252 void SoundManager::checkAmbientVolumeValidity() 253 { 254 if(!checkVolumeValidity(SoundType::ambient)) 255 { 256 ResetConfigValue(ambientVolume_); 257 } 258 } 259 260 void SoundManager::checkEffectsVolumeValidity() 261 { 262 if(!checkVolumeValidity(SoundType::effects)) 263 { 264 ResetConfigValue(effectsVolume_); 265 } 227 } 228 229 void SoundManager::setVolume(float vol, SoundType::Value type) 230 { 231 if (type < 0 || type > SoundType::Effects) 232 return; 233 this->volume_[type] = vol; 234 this->checkVolumeValidity(type); 235 } 236 237 float SoundManager::getVolume(SoundType::Value type) 238 { 239 if (type < 0 || type > SoundType::Effects) 240 return 0.0f; 241 return this->volume_[type]; 242 } 243 244 float SoundManager::getRealVolume(SoundType::Value type) 245 { 246 if (type != SoundType::Music && type != SoundType::Effects) 247 return 0.0f; 248 return this->volume_[SoundType::All] * this->mute_[SoundType::All] * this->volume_[type] * this->mute_[type]; 249 } 250 251 void SoundManager::updateVolume(SoundType::Value type) 252 { 253 switch(type) 254 { 255 case SoundType::All: 256 for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it) 257 (*it)->updateVolume(); 258 break; 259 case SoundType::Music: 260 for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it) 261 (*it)->updateVolume(); 262 break; 263 case SoundType::Effects: 264 for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it) 265 (*it)->updateVolume(); 266 break; 267 default: 268 assert(false); 269 } 270 } 271 272 void SoundManager::toggleMute(SoundType::Value type) 273 { 274 if (type < 0 || type > SoundType::Effects) 275 return; 276 this->mute_[type] = (this->mute_[type] == 0) ? 1.0f : 0.0f; 277 this->updateVolume(type); 278 } 279 280 bool SoundManager::getMute(SoundType::Value type) 281 { 282 if (type < 0 || type > SoundType::Effects) 283 return true; 284 return (this->mute_[type] == 0); 266 285 } 267 286 … … 358 377 } 359 378 } 360 361 362 void SoundManager::setVolume(float vol, SoundType::Value type)363 {364 vol = this->checkVolumeRange(vol);365 366 this->setVolumeInternal(vol, type);367 368 this->updateVolume(type);369 }370 371 float SoundManager::checkVolumeRange(float vol)372 {373 if(vol < 0.0 || vol > 1.0)374 {375 COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;376 vol = vol > 1 ? 1 : vol;377 vol = vol < 0 ? 0 : vol;378 }379 380 return vol;381 }382 383 void SoundManager::updateVolume(SoundType::Value type)384 {385 switch(type)386 {387 case SoundType::none:388 for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it)389 {390 (*it)->updateVolume();391 }392 break;393 case SoundType::ambient:394 for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it)395 {396 (*it)->updateVolume();397 }398 break;399 case SoundType::effects:400 for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it)401 {402 (*it)->updateVolume();403 }404 break;405 default:406 COUT(2) << "Invalid SoundType in SoundManager::updateVolume() - Not updating!" << std::endl;407 }408 }409 410 void SoundManager::setVolumeInternal(float vol, SoundType::Value type)411 {412 switch(type)413 {414 case SoundType::none:415 this->soundVolume_ = vol;416 break;417 case SoundType::ambient:418 this->ambientVolume_ = vol;419 break;420 case SoundType::effects:421 this->effectsVolume_ = vol;422 break;423 default:424 COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Not setting any volume!" << std::endl;425 }426 }427 428 float SoundManager::getVolumeInternal(SoundType::Value type)429 {430 switch(type)431 {432 case SoundType::none:433 return this->soundVolume_;434 case SoundType::ambient:435 return this->ambientVolume_;436 case SoundType::effects:437 return this->effectsVolume_;438 default:439 COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Returning 0.0!" << std::endl;440 return 0.0;441 }442 }443 444 float SoundManager::getVolume(SoundType::Value type)445 {446 if(this->mute_[SoundType::none] || this->mute_[type])447 return 0.0;448 449 if(type == SoundType::none)450 return this->getVolumeInternal(type);451 452 return this->getVolumeInternal(SoundType::none)*this->getVolumeInternal(type);453 }454 455 void SoundManager::toggleMute(SoundType::Value type)456 {457 bool mute = !this->mute_[type];458 this->mute_[type] = mute;459 460 this->updateVolume(type);461 }462 463 bool SoundManager::getMute(SoundType::Value type)464 {465 return this->mute_[type];466 }467 468 379 469 380 void SoundManager::fadeIn(const SmartPtr<AmbientSound>& sound) -
code/branches/presentation2/src/orxonox/sound/SoundManager.h
r6349 r6370 50 50 // forward declaration 51 51 class SoundBuffer; 52 52 53 53 //! Enum for the sound type. 54 54 namespace SoundType … … 56 56 enum Value 57 57 { 58 none,59 ambient,60 effects58 All = 0, 59 Music = 1, 60 Effects = 2 61 61 }; 62 62 } 63 64 /** 65 * The SoundManager class manages the OpenAL device, context and listener 66 * position. It is a singleton. 67 * 68 */ 63 64 //! The SoundManager class manages the OpenAL device, context and listener position. 69 65 class _OrxonoxExport SoundManager 70 66 // tolua_end … … 94 90 void pauseAmbientSound(AmbientSound* ambient); 95 91 92 // tolua_begin 96 93 void setVolume(float vol, SoundType::Value type); 97 float getVolume(SoundType::Value type); // tolua_export 94 float getVolume(SoundType::Value type); 95 float getRealVolume(SoundType::Value type); 98 96 99 void toggleMute(SoundType::Value type); // tolua_export 100 bool getMute(SoundType::Value type); // tolua_export 97 void toggleMute(SoundType::Value type); 98 bool getMute(SoundType::Value type); 99 // tolua_end 101 100 102 101 shared_ptr<SoundBuffer> getSoundBuffer(const std::string& filename); … … 114 113 115 114 void checkFadeStepValidity(); 116 bool checkVolumeValidity(SoundType::Value type);117 void checkSoundVolumeValidity(void);118 void checkAmbientVolumeValidity(void);119 void checkEffectsVolumeValidity(void);120 115 121 float checkVolumeRange(float vol); 122 116 void checkVolumeValidity(SoundType::Value type); 117 void checkSoundVolumeValidity() { this->checkVolumeValidity(SoundType::All); } 118 void checkAmbientVolumeValidity() { this->checkVolumeValidity(SoundType::Music); } 119 void checkEffectsVolumeValidity() { this->checkVolumeValidity(SoundType::Effects); } 123 120 void updateVolume(SoundType::Value type); 124 121 125 void setVolumeInternal(float vol, SoundType::Value type); 126 float getVolumeInternal(SoundType::Value type); 127 122 // OpenAL device/context related 128 123 std::vector<std::string> deviceNames_; 129 124 ALCdevice* device_; 130 125 ALCcontext* context_; 131 126 127 // Ambient sound related 132 128 typedef std::list<std::pair<AmbientSound*, bool> > AmbientList; 133 AmbientList ambientSounds_;134 135 float crossFadeStep_; //!< Absolute change per second (0.1 means 10% of the nominal volume) for cross fading129 AmbientList ambientSounds_; 130 //! Absolute change per second (0.1 means 10% of the nominal volume) for cross fading 131 float crossFadeStep_; 136 132 std::list<SmartPtr<AmbientSound> > fadeInList_; 137 133 std::list<SmartPtr<AmbientSound> > fadeOutList_; 138 134 139 float soundVolume_; 140 float ambientVolume_; 141 float effectsVolume_; 142 std::map<SoundType::Value, bool> mute_; 135 // Volume related 136 float volume_[3]; 137 float mute_[3]; 143 138 139 // Sound buffer related 144 140 static const unsigned int maxEffectsPoolSize_s = 40 * 1024 * 1024; 145 141 unsigned int effectsPoolSize_; … … 149 145 SoundBufferMap soundBuffers_; 150 146 147 // Sound source related 151 148 unsigned int maxSources_; 152 149 std::vector<ALuint> soundSources_; -
code/branches/presentation2/src/orxonox/sound/WorldSound.cc
r6322 r6370 57 57 void WorldSound::registerVariables() 58 58 { 59 registerVariable(volume_, ObjectDirection::ToClient, new NetworkCallback< BaseSound>(static_cast<BaseSound*>(this), &BaseSound::volumeChanged));60 registerVariable(source_, ObjectDirection::ToClient, new NetworkCallback< BaseSound>(static_cast<BaseSound*>(this), &BaseSound::sourceChanged));61 registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback< BaseSound>(static_cast<BaseSound*>(this), &BaseSound::loopingChanged));62 registerVariable((int&)(BaseSound::state_), ObjectDirection::ToClient, new NetworkCallback< BaseSound>(static_cast<BaseSound*>(this), &BaseSound::stateChanged));63 registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback< BaseSound>(static_cast<BaseSound*>(this), &BaseSound::pitchChanged));59 registerVariable(volume_, ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::volumeChanged)); 60 registerVariable(source_, ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::sourceChanged)); 61 registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::loopingChanged)); 62 registerVariable((int&)(BaseSound::state_), ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::stateChanged)); 63 registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::pitchChanged)); 64 64 } 65 65 … … 115 115 } 116 116 117 float WorldSound::get VolumeGain()117 float WorldSound::getRealVolume() 118 118 { 119 return SoundManager::getInstance().get Volume(SoundType::effects);119 return SoundManager::getInstance().getRealVolume(SoundType::Effects); 120 120 } 121 121 } -
code/branches/presentation2/src/orxonox/sound/WorldSound.h
r6322 r6370 46 46 public: 47 47 WorldSound(BaseObject* creator); 48 virtual~WorldSound();48 ~WorldSound(); 49 49 50 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 51 virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode); 52 virtual void changedActivity(); 53 54 virtual float getVolumeGain(); 50 void XMLPort(Element& xmlelement, XMLPort::Mode mode); 51 void XMLEventPort(Element& xmlelement, XMLPort::Mode mode); 52 void changedActivity(); 55 53 56 v irtual void tick(float dt);54 void tick(float dt); 57 55 58 56 private: 59 57 void registerVariables(); 60 58 void initialiseSource(); 59 float getRealVolume(); 61 60 }; 62 61 }
Note: See TracChangeset
for help on using the changeset viewer.