Changeset 8052 in orxonox.OLD for branches/atmospheric_engine/src/lib/sound
- Timestamp:
- Jun 1, 2006, 11:12:42 AM (19 years ago)
- Location:
- branches/atmospheric_engine/src/lib/sound
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/atmospheric_engine/src/lib/sound/sound_source.cc
r7810 r8052 1 1 /* 2 3 4 5 6 7 8 9 10 11 12 13 2 orxonox - the future of 3D-vertical-scrollers 3 4 Copyright (C) 2004 orx 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 ### File Specific: 12 main-programmer: Benjamin Grauer 13 co-programmer: ... 14 14 */ 15 15 … … 24 24 namespace OrxSound 25 25 { 26 /** 27 * @brief creates a SoundSource at position sourceNode with the SoundBuffer buffer 28 */ 29 SoundSource::SoundSource(const PNode* sourceNode, const SoundBuffer* buffer) 30 { 31 this->setClassID(CL_SOUND_SOURCE, "SoundSource"); 32 33 // adding the Source to the SourcesList of the SoundEngine 34 this->buffer = buffer; 35 this->sourceNode = sourceNode; 36 this->resident = false; 37 38 this->sourceID = 0; 39 this->bPlay = false; 40 } 41 42 43 /** 44 * @brief construct a SoundSource out of the another soundSource 45 * @param source the Source to create this source from. 46 * 47 * Copies the buffer from source to this Source. 48 * Acquires a new SourceID if source is Playing. 49 */ 50 SoundSource::SoundSource(const SoundSource& source) 51 { 52 this->setClassID(CL_SOUND_SOURCE, "SoundSource"); 53 54 // adding the Source to the SourcesList of the SoundEngine 55 this->buffer = source.buffer; 56 this->sourceNode = source.sourceNode; 57 this->resident = source.resident; 58 59 this->sourceID = 0; 60 if (source.bPlay == true) 61 { 62 this->bPlay = true; 63 SoundEngine::getInstance()->popALSource(this->sourceID); 64 } 65 else 66 this->bPlay = false; 67 } 68 69 70 /** 71 * @brief paste a copy of the source into this Source. 72 * @param source the SoundSource to paste into this one. 73 * @returns a Reference to this Source. 74 * 75 */ 76 SoundSource& SoundSource::operator=(const SoundSource& source) 77 { 78 this->buffer = source.buffer; 79 this->sourceNode = sourceNode; 80 this->resident = source.resident; 81 82 if (source.bPlay) 83 this->play(); 84 else 85 this->stop(); 86 } 87 88 89 /** 90 * @brief compares two Sources with each other. 91 * @param source the Source to compare against this One. 92 * Two Sources are the same, if the PNodes match, and the Sound Played are the same. 93 * The alSource must not match, because no two Sources can have the same alSource. 94 */ 95 bool SoundSource::operator==(const SoundSource& source) 96 { 97 return (this->buffer == source.buffer && 98 this->bPlay == source.bPlay && 99 this->sourceNode == source.sourceNode); 100 } 101 102 103 /** 104 * @brief deletes a SoundSource 105 */ 106 SoundSource::~SoundSource() 107 { 108 this->stop(); 109 if (this->sourceID != 0) 110 SoundEngine::getInstance()->pushALSource(this->sourceID); 111 } 112 113 114 /** 115 * @brief Plays back a SoundSource 116 */ 117 void SoundSource::play() 118 { 119 if (this->buffer && this->retrieveSource()) 120 { 121 if (this->bPlay) 122 alSourceStop(this->sourceID); 123 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 124 alSourcei (this->sourceID, AL_LOOPING, AL_FALSE ); 125 alSourcePlay(this->sourceID); 126 127 if (DEBUG_LEVEL >= 3) 128 SoundEngine::checkError("Play Source", __LINE__); 129 this->bPlay = true; 130 } 131 } 132 133 134 /** 135 * @brief Plays back buffer on this Source 136 * @param buffer the buffer to play back on this Source 137 */ 138 void SoundSource::play(const SoundBuffer* buffer) 139 { 140 if (!this->retrieveSource()) 141 { 142 PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n"); 143 return; 144 } 145 146 alSourceStop(this->sourceID); 147 alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); 148 alSourcei (this->sourceID, AL_LOOPING, AL_FALSE ); 149 alSourcePlay(this->sourceID); 150 151 if (unlikely(this->buffer != NULL)) 152 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 153 this->bPlay = true; 154 155 if (DEBUG_LEVEL >= 3) 156 SoundEngine::checkError("Play Source", __LINE__); 157 } 158 26 /** 27 * @brief creates a SoundSource at position sourceNode with the SoundBuffer buffer 28 */ 29 SoundSource::SoundSource(const PNode* sourceNode, const SoundBuffer* buffer) 30 { 31 this->setClassID(CL_SOUND_SOURCE, "SoundSource"); 32 33 // adding the Source to the SourcesList of the SoundEngine 34 this->buffer = buffer; 35 this->sourceNode = sourceNode; 36 this->resident = false; 37 38 this->sourceID = 0; 39 this->bPlay = false; 40 } 41 42 43 /** 44 * @brief construct a SoundSource out of the another soundSource 45 * @param source the Source to create this source from. 46 * 47 * Copies the buffer from source to this Source. 48 * Acquires a new SourceID if source is Playing. 49 */ 50 SoundSource::SoundSource(const SoundSource& source) 51 { 52 this->setClassID(CL_SOUND_SOURCE, "SoundSource"); 53 54 // adding the Source to the SourcesList of the SoundEngine 55 this->buffer = source.buffer; 56 this->sourceNode = source.sourceNode; 57 this->resident = source.resident; 58 59 this->sourceID = 0; 60 if (source.bPlay == true) 61 { 62 this->bPlay = true; 63 SoundEngine::getInstance()->popALSource(this->sourceID); 64 } 65 else 66 this->bPlay = false; 67 } 68 69 70 /** 71 * @brief paste a copy of the source into this Source. 72 * @param source the SoundSource to paste into this one. 73 * @returns a Reference to this Source. 74 * 75 */ 76 SoundSource& SoundSource::operator=(const SoundSource& source) 77 { 78 this->buffer = source.buffer; 79 this->sourceNode = sourceNode; 80 this->resident = source.resident; 81 82 if (source.bPlay) 83 this->play(); 84 else 85 this->stop(); 86 } 87 88 89 /** 90 * @brief compares two Sources with each other. 91 * @param source the Source to compare against this One. 92 * Two Sources are the same, if the PNodes match, and the Sound Played are the same. 93 * The alSource must not match, because no two Sources can have the same alSource. 94 */ 95 bool SoundSource::operator==(const SoundSource& source) 96 { 97 return (this->buffer == source.buffer && 98 this->bPlay == source.bPlay && 99 this->sourceNode == source.sourceNode); 100 } 101 102 103 /** 104 * @brief deletes a SoundSource 105 */ 106 SoundSource::~SoundSource() 107 { 108 this->stop(); 109 if (this->sourceID != 0) 110 SoundEngine::getInstance()->pushALSource(this->sourceID); 111 } 112 113 114 /** 115 * @brief Plays back a SoundSource 116 */ 117 void SoundSource::play() 118 { 119 if (this->buffer && this->retrieveSource()) 120 { 121 if (this->bPlay) 122 alSourceStop(this->sourceID); 123 124 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 125 alSourcei (this->sourceID, AL_LOOPING, AL_FALSE); 126 alSourcef (this->sourceID, AL_GAIN, 1); 127 alSourcePlay(this->sourceID); 128 129 if (DEBUG_LEVEL >= 3) 130 SoundEngine::checkError("Play Source", __LINE__); 131 this->bPlay = true; 132 } 133 } 134 135 136 /** 137 * @brief Plays back buffer on this Source 138 * @param buffer the buffer to play back on this Source 139 */ 140 void SoundSource::play(const SoundBuffer* buffer) 141 { 142 if (!this->retrieveSource()) 143 { 144 PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n"); 145 return; 146 } 147 148 alSourceStop(this->sourceID); 149 alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); 150 alSourcei (this->sourceID, AL_LOOPING, AL_FALSE); 151 alSourcef (this->sourceID, AL_GAIN, 1); 152 153 alSourcePlay(this->sourceID); 154 155 if (unlikely(this->buffer != NULL)) 156 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 157 this->bPlay = true; 158 159 if (DEBUG_LEVEL >= 3) 160 SoundEngine::checkError("Play Source", __LINE__); 161 } 162 163 164 /** 165 * @brief Plays back buffer on this Source with gain 166 * @param buffer the buffer to play back on this Source 167 */ 168 void SoundSource::play(const SoundBuffer* buffer, float gain) 169 { 170 if (!this->retrieveSource()) 171 { 172 PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n"); 173 return; 174 } 175 176 alSourceStop(this->sourceID); 177 alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); 178 alSourcei (this->sourceID, AL_LOOPING, AL_FALSE); 179 alSourcef (this->sourceID, AL_GAIN, gain); 180 181 alSourcePlay(this->sourceID); 182 183 if (unlikely(this->buffer != NULL)) 184 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 185 this->bPlay = true; 186 187 if (DEBUG_LEVEL >= 3) 188 SoundEngine::checkError("Play Source", __LINE__); 189 } 190 191 192 /** 193 * @brief Plays and loops back a SoundSource 194 */ 195 void SoundSource::loop() 196 { 197 if (this->buffer && this->retrieveSource()) 198 { 199 if (this->bPlay) 200 alSourceStop(this->sourceID); 201 202 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 203 alSourcei (this->sourceID, AL_LOOPING, AL_TRUE); 204 alSourcef (this->sourceID, AL_GAIN, 1); 205 alSourcePlay(this->sourceID); 206 207 if (DEBUG_LEVEL >= 3) 208 SoundEngine::checkError("Play Source", __LINE__); 209 this->bPlay = true; 210 } 211 } 159 212 160 213 /** 161 162 163 214 * @brief Plays and loops buffer on this Source 215 * @param buffer the buffer to play back on this Source 216 */ 164 217 void SoundSource::loop(const SoundBuffer* buffer) 165 218 { 166 if (this->buffer && this->retrieveSource()) 167 { 168 if (this->bPlay) 169 alSourceStop(this->sourceID); 170 171 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 172 alSourcei (this->sourceID, AL_LOOPING, AL_TRUE ); 173 alSourcePlay(this->sourceID); 174 175 if (DEBUG_LEVEL >= 3) 176 SoundEngine::checkError("Play LoopSource", __LINE__); 177 this->bPlay = true; 178 } 219 if (this->buffer && this->retrieveSource()) 220 { 221 if (this->bPlay) 222 alSourceStop(this->sourceID); 223 224 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 225 alSourcei (this->sourceID, AL_LOOPING, AL_TRUE); 226 alSourcef (this->sourceID, AL_GAIN, 1); 227 228 alSourcePlay(this->sourceID); 229 230 if (DEBUG_LEVEL >= 3) 231 SoundEngine::checkError("Play LoopSource", __LINE__); 232 this->bPlay = true; 233 } 179 234 } 180 235 181 236 182 237 /** 183 * @brief Stops playback of a SoundSource 184 */ 238 * @brief Plays and loops buffer on this Source with gain control 239 * @param buffer the buffer to play back on this Source 240 */ 241 void SoundSource::loop(const SoundBuffer* buffer, float gain) 242 { 243 if (this->buffer && this->retrieveSource()) 244 { 245 if (this->bPlay) 246 alSourceStop(this->sourceID); 247 248 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 249 alSourcei (this->sourceID, AL_LOOPING, AL_TRUE); 250 alSourcef (this->sourceID, AL_GAIN, gain); 251 252 alSourcePlay(this->sourceID); 253 254 if (DEBUG_LEVEL >= 3) 255 SoundEngine::checkError("Play LoopSource", __LINE__); 256 this->bPlay = true; 257 } 258 } 259 260 261 /** 262 * @brief Stops playback of a SoundSource 263 */ 185 264 void SoundSource::stop() 186 265 { 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 266 this->bPlay = false; 267 if (this->sourceID != 0) 268 { 269 this->bPlay = false; 270 if (this->sourceID != 0) 271 { 272 alSourceStop(this->sourceID); 273 if (DEBUG_LEVEL >= 3) 274 SoundEngine::checkError("StopSource", __LINE__); 275 alSourcei(this->sourceID, AL_BUFFER, 0); 276 if (!this->resident) 277 SoundEngine::getInstance()->pushALSource(this->sourceID); 278 this->sourceID = 0; 279 } 280 } 202 281 } 203 282 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 283 /** 284 * @brief Pauses Playback of a SoundSource 285 */ 286 void SoundSource::pause() 287 { 288 alSourcePause(this->sourceID); 289 if (DEBUG_LEVEL >= 3) 290 SoundEngine::checkError("Pause Source", __LINE__); 291 } 292 293 294 /** 295 * @brief Rewinds Playback of a SoundSource 296 */ 297 void SoundSource::rewind() 298 { 299 alSourceRewind(this->sourceID); 300 301 if (DEBUG_LEVEL >= 3) 302 SoundEngine::checkError("Rewind Source", __LINE__); 303 } 304 305 306 /** 307 * @brief sets the RolloffFactor of the Sound emitted from the SoundSource 308 * @param rolloffFactor The Factor described 309 * 310 * this tells openAL how fast the Sounds decay outward from the Source 311 */ 312 void SoundSource::setRolloffFactor(ALfloat rolloffFactor) 313 { 314 alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor); 315 316 if (DEBUG_LEVEL >= 3) 317 SoundEngine::checkError("Set Source Rolloff-factor", __LINE__); 318 } 319 320 321 /** 322 * @brief sets the Positional this Source should be attached to. 323 * @param sourceNode the Source this is attached to. 324 * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels. 325 */ 326 void SoundSource::setSourceNode(const PNode* sourceNode) 327 { 328 this->sourceNode = sourceNode; 329 } 330 331 /** 332 * @brief retrieve a Source. 333 */ 334 bool SoundSource::retrieveSource() 335 { 336 if (this->sourceID != 0) 337 return true; 338 else 339 { 340 SoundEngine::getInstance()->popALSource(this->sourceID); 341 if (this->sourceID != 0) 342 { 343 if (unlikely(this->sourceNode == NULL)) 344 resetSource(this->sourceID); 345 return true; 346 } 347 } 348 return false; 349 } 350 351 352 /** 353 * @brief reset an alSource to its default Values. 354 */ 355 void SoundSource::resetSource(ALuint sourceID) 356 { 357 alSource3f(sourceID, AL_POSITION, 0.0, 0.0, 0.0); 358 alSource3f(sourceID, AL_VELOCITY, 0.0, 0.0, 0.0); 359 alSource3f(sourceID, AL_DIRECTION, 0.0, 0.0, 0.0); 360 alSourcef (sourceID, AL_ROLLOFF_FACTOR, 0.0 ); 361 //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE ); 362 alSourcef (sourceID, AL_GAIN, SoundEngine::getInstance()->getEffectsVolume()); 363 } 285 364 } -
branches/atmospheric_engine/src/lib/sound/sound_source.h
r7810 r8052 29 29 void play(); 30 30 void play(const SoundBuffer* buffer); 31 void play(const SoundBuffer* buffer, float gain); 31 32 void loop(); 32 33 void loop(const SoundBuffer* buffer); 34 void loop(const SoundBuffer* buffer, float gain); 33 35 void stop(); 34 36 void pause();
Note: See TracChangeset
for help on using the changeset viewer.