Changeset 8480 in orxonox.OLD for branches/atmospheric_engine/src/lib/sound
- Timestamp:
- Jun 15, 2006, 4:57:51 PM (18 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
r8255 r8480 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 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 } 212 213 /** 214 * @brief Plays and loops buffer on this Source 215 * @param buffer the buffer to play back on this Source 216 */ 217 void SoundSource::loop(const SoundBuffer* buffer) 218 { 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("Loop Source", __LINE__); 232 this->bPlay = true; 233 } 234 } 235 236 237 /** 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("Loop Source", __LINE__); 256 this->bPlay = true; 257 } 258 } 259 260 261 /** 262 * @brief Stops playback of a SoundSource 263 */ 264 void SoundSource::stop() 265 { 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 } 281 } 282 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 } 364 365 366 /** 367 * @brief Fades in a Source over a time period 368 * @param duration time perios to fade in 369 */ 370 void SoundSource::fadein(const SoundBuffer* buffer, ALfloat duration) 371 { 372 //if (this->buffer && this->retrieveSource()) 373 //{ 374 375 for (ALfloat n = 0.0; n < 1.0; n+=.01) 376 { 377 alSourcef(this->sourceID, AL_GAIN, n); 378 // sleep(1); 379 } 380 381 // alSourcePlay(this->sourceID); 382 383 // if (DEBUG_LEVEL >= 3) 384 // SoundEngine::checkError("Loop Source", __LINE__); 385 // this->bPlay = true; 386 //} 387 388 } 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 * @brief Plays back buffer on this Source with gain and looping possibility 193 * @param buffer the buffer to play back on this Source 194 */ 195 void SoundSource::play(const SoundBuffer* buffer, float gain, bool loop) 196 { 197 if (!this->retrieveSource()) 198 { 199 PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n"); 200 return; 201 } 202 203 alSourceStop(this->sourceID); 204 alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); 205 206 if (loop) 207 alSourcei (this->sourceID, AL_LOOPING, AL_TRUE); 208 else 209 alSourcei (this->sourceID, AL_LOOPING, AL_FALSE); 210 211 alSourcef (this->sourceID, AL_GAIN, gain); 212 213 alSourcePlay(this->sourceID); 214 215 if (unlikely(this->buffer != NULL)) 216 alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); 217 this->bPlay = true; 218 219 if (DEBUG_LEVEL >= 3) 220 SoundEngine::checkError("Play Source", __LINE__); 221 } 222 223 224 /** 225 * @brief Stops playback of a SoundSource 226 */ 227 void SoundSource::stop() 228 { 229 this->bPlay = false; 230 if (this->sourceID != 0) 231 { 232 this->bPlay = false; 233 if (this->sourceID != 0) 234 { 235 alSourceStop(this->sourceID); 236 if (DEBUG_LEVEL >= 3) 237 SoundEngine::checkError("StopSource", __LINE__); 238 alSourcei(this->sourceID, AL_BUFFER, 0); 239 if (!this->resident) 240 SoundEngine::getInstance()->pushALSource(this->sourceID); 241 this->sourceID = 0; 242 } 243 } 244 } 245 246 /** 247 * @brief Pauses Playback of a SoundSource 248 */ 249 void SoundSource::pause() 250 { 251 alSourcePause(this->sourceID); 252 if (DEBUG_LEVEL >= 3) 253 SoundEngine::checkError("Pause Source", __LINE__); 254 } 255 256 257 /** 258 * @brief Rewinds Playback of a SoundSource 259 */ 260 void SoundSource::rewind() 261 { 262 alSourceRewind(this->sourceID); 263 264 if (DEBUG_LEVEL >= 3) 265 SoundEngine::checkError("Rewind Source", __LINE__); 266 } 267 268 269 /** 270 * @brief sets the RolloffFactor of the Sound emitted from the SoundSource 271 * @param rolloffFactor The Factor described 272 * 273 * this tells openAL how fast the Sounds decay outward from the Source 274 */ 275 void SoundSource::setRolloffFactor(ALfloat rolloffFactor) 276 { 277 alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor); 278 279 if (DEBUG_LEVEL >= 3) 280 SoundEngine::checkError("Set Source Rolloff-factor", __LINE__); 281 } 282 283 284 /** 285 * @brief sets the Positional this Source should be attached to. 286 * @param sourceNode the Source this is attached to. 287 * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels. 288 */ 289 void SoundSource::setSourceNode(const PNode* sourceNode) 290 { 291 this->sourceNode = sourceNode; 292 } 293 294 /** 295 * @brief retrieve a Source. 296 */ 297 bool SoundSource::retrieveSource() 298 { 299 if (this->sourceID != 0) 300 return true; 301 else 302 { 303 SoundEngine::getInstance()->popALSource(this->sourceID); 304 if (this->sourceID != 0) 305 { 306 if (unlikely(this->sourceNode == NULL)) 307 resetSource(this->sourceID); 308 return true; 309 } 310 } 311 return false; 312 } 313 314 315 /** 316 * @brief reset an alSource to its default Values. 317 */ 318 void SoundSource::resetSource(ALuint sourceID) 319 { 320 alSource3f(sourceID, AL_POSITION, 0.0, 0.0, 0.0); 321 alSource3f(sourceID, AL_VELOCITY, 0.0, 0.0, 0.0); 322 alSource3f(sourceID, AL_DIRECTION, 0.0, 0.0, 0.0); 323 alSourcef (sourceID, AL_ROLLOFF_FACTOR, 0.0 ); 324 //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE ); 325 alSourcef (sourceID, AL_GAIN, SoundEngine::getInstance()->getEffectsVolume()); 326 } 327 328 329 /** 330 * @brief Fades in a Source over a time period 331 * @param duration time perios to fade in 332 */ 333 void SoundSource::fadein(const SoundBuffer* buffer, ALfloat duration) 334 { 335 //if (this->buffer && this->retrieveSource()) 336 //{ 337 338 for (ALfloat n = 0.0; n < 1.0; n+=.01) 339 { 340 alSourcef(this->sourceID, AL_GAIN, n); 341 // sleep(1); 342 } 343 344 // alSourcePlay(this->sourceID); 345 346 // if (DEBUG_LEVEL >= 3) 347 // SoundEngine::checkError("Loop Source", __LINE__); 348 // this->bPlay = true; 349 //} 350 351 } 389 352 390 353 -
branches/atmospheric_engine/src/lib/sound/sound_source.h
r8255 r8480 30 30 void play(const SoundBuffer* buffer); 31 31 void play(const SoundBuffer* buffer, float gain); 32 void loop(); 33 void loop(const SoundBuffer* buffer); 34 void loop(const SoundBuffer* buffer, float gain); 32 void play(const SoundBuffer* buffer, float gain, bool loop); 33 35 34 void stop(); 36 35 void pause();
Note: See TracChangeset
for help on using the changeset viewer.