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