- Timestamp:
- Dec 11, 2009, 2:15:43 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note: See TracChangeset
for help on using the changeset viewer.