| [4744] | 1 | /* | 
|---|
| [8495] | 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: ... | 
|---|
| [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 |   } | 
|---|
| [7299] | 88 |  | 
|---|
| [7317] | 89 |  | 
|---|
| [8350] | 90 |   /** | 
|---|
 | 91 |   * @brief compares two Sources with each other. | 
|---|
 | 92 |   * @param source the Source to compare against this One. | 
|---|
 | 93 |   * Two Sources are the same, if the PNodes match, and the Sound Played are the same. | 
|---|
 | 94 |   * The alSource must not match, because no two Sources can have the same alSource. | 
|---|
 | 95 |   */ | 
|---|
 | 96 |   bool SoundSource::operator==(const SoundSource& source) | 
|---|
 | 97 |   { | 
|---|
 | 98 |     return (this->buffer == source.buffer && | 
|---|
 | 99 |             this->bPlay == source.bPlay && | 
|---|
 | 100 |             this->sourceNode == source.sourceNode); | 
|---|
 | 101 |   } | 
|---|
| [7299] | 102 |  | 
|---|
| [7317] | 103 |  | 
|---|
| [8350] | 104 |   /** | 
|---|
 | 105 |   * @brief deletes a SoundSource | 
|---|
 | 106 |   */ | 
|---|
 | 107 |   SoundSource::~SoundSource() | 
|---|
 | 108 |   { | 
|---|
 | 109 |     this->stop(); | 
|---|
 | 110 |     if (this->sourceID != 0) | 
|---|
 | 111 |       SoundEngine::getInstance()->pushALSource(this->sourceID); | 
|---|
 | 112 |   } | 
|---|
| [4320] | 113 |  | 
|---|
| [7317] | 114 |  | 
|---|
| [8350] | 115 |   /** | 
|---|
 | 116 |   * @brief Plays back a SoundSource | 
|---|
 | 117 |   */ | 
|---|
 | 118 |   void SoundSource::play() | 
|---|
 | 119 |   { | 
|---|
 | 120 |     if (this->buffer && this->retrieveSource()) | 
|---|
 | 121 |     { | 
|---|
 | 122 |       if (this->bPlay) | 
|---|
 | 123 |         alSourceStop(this->sourceID); | 
|---|
| [7318] | 124 |  | 
|---|
| [8350] | 125 |       alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); | 
|---|
 | 126 |       alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE); | 
|---|
 | 127 |       alSourcef (this->sourceID, AL_GAIN, 1); | 
|---|
 | 128 |       alSourcePlay(this->sourceID); | 
|---|
| [4320] | 129 |  | 
|---|
| [8350] | 130 |       if (DEBUG_LEVEL >= 3) | 
|---|
 | 131 |         SoundEngine::checkError("Play Source", __LINE__); | 
|---|
 | 132 |       this->bPlay = true; | 
|---|
 | 133 |     } | 
|---|
 | 134 |   } | 
|---|
| [7317] | 135 |  | 
|---|
| [5930] | 136 |  | 
|---|
| [8350] | 137 |   /** | 
|---|
 | 138 |   * @brief Plays back buffer on this Source | 
|---|
 | 139 |   * @param buffer the buffer to play back on this Source | 
|---|
 | 140 |   */ | 
|---|
 | 141 |   void SoundSource::play(const SoundBuffer* buffer) | 
|---|
 | 142 |   { | 
|---|
 | 143 |     if (!this->retrieveSource()) | 
|---|
 | 144 |     { | 
|---|
 | 145 |       PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n"); | 
|---|
 | 146 |       return; | 
|---|
 | 147 |     } | 
|---|
| [5386] | 148 |  | 
|---|
| [8350] | 149 |     alSourceStop(this->sourceID); | 
|---|
 | 150 |     alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); | 
|---|
 | 151 |     alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE); | 
|---|
 | 152 |     alSourcef (this->sourceID, AL_GAIN, 1); | 
|---|
| [7290] | 153 |  | 
|---|
| [8350] | 154 |     alSourcePlay(this->sourceID); | 
|---|
| [1853] | 155 |  | 
|---|
| [8350] | 156 |     if (unlikely(this->buffer != NULL)) | 
|---|
 | 157 |       alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); | 
|---|
 | 158 |     this->bPlay = true; | 
|---|
| [7317] | 159 |  | 
|---|
| [8350] | 160 |     if (DEBUG_LEVEL >= 3) | 
|---|
 | 161 |       SoundEngine::checkError("Play Source", __LINE__); | 
|---|
 | 162 |   } | 
|---|
| [7810] | 163 |  | 
|---|
 | 164 |  | 
|---|
| [8350] | 165 |   /** | 
|---|
 | 166 |   * @brief Plays back buffer on this Source with gain | 
|---|
 | 167 |   * @param buffer the buffer to play back on this Source | 
|---|
 | 168 |   */ | 
|---|
 | 169 |   void SoundSource::play(const SoundBuffer* buffer, float gain) | 
|---|
 | 170 |   { | 
|---|
 | 171 |     if (!this->retrieveSource()) | 
|---|
 | 172 |     { | 
|---|
 | 173 |       PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n"); | 
|---|
 | 174 |       return; | 
|---|
 | 175 |     } | 
|---|
| [7810] | 176 |  | 
|---|
| [8350] | 177 |     alSourceStop(this->sourceID); | 
|---|
 | 178 |     alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); | 
|---|
 | 179 |     alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE); | 
|---|
 | 180 |     alSourcef (this->sourceID, AL_GAIN, gain); | 
|---|
| [7810] | 181 |  | 
|---|
| [8350] | 182 |     alSourcePlay(this->sourceID); | 
|---|
| [1853] | 183 |  | 
|---|
| [8350] | 184 |     if (unlikely(this->buffer != NULL)) | 
|---|
 | 185 |       alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); | 
|---|
 | 186 |     this->bPlay = true; | 
|---|
| [5386] | 187 |  | 
|---|
| [8350] | 188 |     if (DEBUG_LEVEL >= 3) | 
|---|
 | 189 |       SoundEngine::checkError("Play Source", __LINE__); | 
|---|
 | 190 |   } | 
|---|
| [8495] | 191 |    | 
|---|
 | 192 |    /** | 
|---|
 | 193 |    * @brief Plays back buffer on this Source with gain and looping possibility | 
|---|
 | 194 |    * @param buffer the buffer to play back on this Source | 
|---|
 | 195 |     */ | 
|---|
 | 196 |   void SoundSource::play(const SoundBuffer* buffer, float gain, bool loop) | 
|---|
| [8350] | 197 |   { | 
|---|
| [8495] | 198 |     if (!this->retrieveSource()) | 
|---|
| [8350] | 199 |     { | 
|---|
| [8495] | 200 |       PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n"); | 
|---|
 | 201 |       return; | 
|---|
| [8350] | 202 |     } | 
|---|
| [7290] | 203 |  | 
|---|
| [8495] | 204 |     alSourceStop(this->sourceID); | 
|---|
 | 205 |     alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); | 
|---|
 | 206 |      | 
|---|
 | 207 |     if (loop) | 
|---|
 | 208 |         alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE); | 
|---|
 | 209 |     else | 
|---|
 | 210 |         alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE); | 
|---|
 | 211 |      | 
|---|
 | 212 |     alSourcef (this->sourceID, AL_GAIN, gain); | 
|---|
| [5386] | 213 |  | 
|---|
| [8495] | 214 |     alSourcePlay(this->sourceID); | 
|---|
| [7317] | 215 |  | 
|---|
| [8495] | 216 |     if (unlikely(this->buffer != NULL)) | 
|---|
| [8350] | 217 |       alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); | 
|---|
| [8495] | 218 |     this->bPlay = true; | 
|---|
| [8255] | 219 |  | 
|---|
| [8495] | 220 |     if (DEBUG_LEVEL >= 3) | 
|---|
 | 221 |       SoundEngine::checkError("Play Source", __LINE__); | 
|---|
 | 222 |   }  | 
|---|
| [8255] | 223 |  | 
|---|
 | 224 |  | 
|---|
| [8350] | 225 |   /** | 
|---|
 | 226 |   * @brief Stops playback of a SoundSource | 
|---|
 | 227 |   */ | 
|---|
 | 228 |   void SoundSource::stop() | 
|---|
 | 229 |   { | 
|---|
 | 230 |     this->bPlay = false; | 
|---|
 | 231 |     if (this->sourceID != 0) | 
|---|
 | 232 |     { | 
|---|
 | 233 |       this->bPlay = false; | 
|---|
 | 234 |       if (this->sourceID != 0) | 
|---|
 | 235 |       { | 
|---|
 | 236 |         alSourceStop(this->sourceID); | 
|---|
 | 237 |         if (DEBUG_LEVEL >= 3) | 
|---|
 | 238 |           SoundEngine::checkError("StopSource", __LINE__); | 
|---|
 | 239 |         alSourcei(this->sourceID, AL_BUFFER, 0); | 
|---|
 | 240 |         if (!this->resident) | 
|---|
 | 241 |           SoundEngine::getInstance()->pushALSource(this->sourceID); | 
|---|
 | 242 |         this->sourceID = 0; | 
|---|
 | 243 |       } | 
|---|
 | 244 |     } | 
|---|
 | 245 |   } | 
|---|
| [8255] | 246 |  | 
|---|
| [8350] | 247 |   /** | 
|---|
 | 248 |   * @brief Pauses Playback of a SoundSource | 
|---|
 | 249 |   */ | 
|---|
 | 250 |   void SoundSource::pause() | 
|---|
 | 251 |   { | 
|---|
 | 252 |     alSourcePause(this->sourceID); | 
|---|
 | 253 |     if (DEBUG_LEVEL >= 3) | 
|---|
 | 254 |       SoundEngine::checkError("Pause Source", __LINE__); | 
|---|
 | 255 |   } | 
|---|
| [8255] | 256 |  | 
|---|
 | 257 |  | 
|---|
| [8350] | 258 |   /** | 
|---|
 | 259 |   * @brief Rewinds Playback of a SoundSource | 
|---|
 | 260 |   */ | 
|---|
 | 261 |   void SoundSource::rewind() | 
|---|
 | 262 |   { | 
|---|
 | 263 |     alSourceRewind(this->sourceID); | 
|---|
| [8255] | 264 |  | 
|---|
| [8350] | 265 |     if (DEBUG_LEVEL >= 3) | 
|---|
 | 266 |       SoundEngine::checkError("Rewind Source", __LINE__); | 
|---|
 | 267 |   } | 
|---|
| [8255] | 268 |  | 
|---|
 | 269 |  | 
|---|
| [8350] | 270 |   /** | 
|---|
 | 271 |   * @brief sets the RolloffFactor of the Sound emitted from the SoundSource | 
|---|
 | 272 |   * @param rolloffFactor The Factor described | 
|---|
 | 273 |   * | 
|---|
 | 274 |   * this tells openAL how fast the Sounds decay outward from the Source | 
|---|
 | 275 |   */ | 
|---|
 | 276 |   void SoundSource::setRolloffFactor(ALfloat rolloffFactor) | 
|---|
 | 277 |   { | 
|---|
 | 278 |     alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor); | 
|---|
| [8255] | 279 |  | 
|---|
| [8350] | 280 |     if (DEBUG_LEVEL >= 3) | 
|---|
 | 281 |       SoundEngine::checkError("Set Source Rolloff-factor", __LINE__); | 
|---|
 | 282 |   } | 
|---|
 | 283 |  | 
|---|
 | 284 |  | 
|---|
 | 285 |   /** | 
|---|
 | 286 |   * @brief sets the Positional this Source should be attached to. | 
|---|
 | 287 |   * @param sourceNode the Source this is attached to. | 
|---|
 | 288 |   * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels. | 
|---|
 | 289 |   */ | 
|---|
 | 290 |   void SoundSource::setSourceNode(const PNode* sourceNode) | 
|---|
 | 291 |   { | 
|---|
 | 292 |     this->sourceNode = sourceNode; | 
|---|
 | 293 |   } | 
|---|
 | 294 |  | 
|---|
 | 295 |   /** | 
|---|
 | 296 |   * @brief retrieve a Source. | 
|---|
 | 297 |   */ | 
|---|
 | 298 |   bool SoundSource::retrieveSource() | 
|---|
 | 299 |   { | 
|---|
 | 300 |     if (this->sourceID != 0) | 
|---|
 | 301 |       return true; | 
|---|
 | 302 |     else | 
|---|
 | 303 |     { | 
|---|
 | 304 |       SoundEngine::getInstance()->popALSource(this->sourceID); | 
|---|
 | 305 |       if (this->sourceID != 0) | 
|---|
 | 306 |       { | 
|---|
 | 307 |         if (unlikely(this->sourceNode == NULL)) | 
|---|
 | 308 |           resetSource(this->sourceID); | 
|---|
 | 309 |         return true; | 
|---|
 | 310 |       } | 
|---|
 | 311 |     } | 
|---|
 | 312 |     return false; | 
|---|
 | 313 |   } | 
|---|
 | 314 |  | 
|---|
 | 315 |  | 
|---|
 | 316 |   /** | 
|---|
 | 317 |   * @brief reset an alSource to its default Values. | 
|---|
 | 318 |   */ | 
|---|
 | 319 |   void SoundSource::resetSource(ALuint sourceID) | 
|---|
 | 320 |   { | 
|---|
 | 321 |     alSource3f(sourceID, AL_POSITION,        0.0, 0.0, 0.0); | 
|---|
 | 322 |     alSource3f(sourceID, AL_VELOCITY,        0.0, 0.0, 0.0); | 
|---|
 | 323 |     alSource3f(sourceID, AL_DIRECTION,       0.0, 0.0, 0.0); | 
|---|
 | 324 |     alSourcef (sourceID, AL_ROLLOFF_FACTOR,  0.0          ); | 
|---|
 | 325 |     //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE      ); | 
|---|
 | 326 |     alSourcef (sourceID, AL_GAIN,            SoundEngine::getInstance()->getEffectsVolume()); | 
|---|
 | 327 |   } | 
|---|
 | 328 |  | 
|---|
 | 329 |  | 
|---|
 | 330 |   /** | 
|---|
 | 331 |   * @brief Fades in a Source over a time period | 
|---|
 | 332 |   * @param duration time perios to fade in | 
|---|
 | 333 |   */ | 
|---|
 | 334 |   void SoundSource::fadein(const SoundBuffer* buffer, ALfloat duration) | 
|---|
 | 335 |   { | 
|---|
 | 336 |     //if (this->buffer && this->retrieveSource()) | 
|---|
 | 337 |     //{ | 
|---|
 | 338 |  | 
|---|
 | 339 |     for (ALfloat n = 0.0; n < 1.0; n+=.01) | 
|---|
 | 340 |     { | 
|---|
 | 341 |       alSourcef(this->sourceID, AL_GAIN, n); | 
|---|
 | 342 |       // sleep(1); | 
|---|
 | 343 |     } | 
|---|
 | 344 |  | 
|---|
| [8495] | 345 |     //  alSourcePlay(this->sourceID); | 
|---|
| [8350] | 346 |  | 
|---|
| [8495] | 347 |     //  if (DEBUG_LEVEL >= 3) | 
|---|
 | 348 |     //    SoundEngine::checkError("Loop Source", __LINE__); | 
|---|
 | 349 |     //  this->bPlay = true; | 
|---|
| [8350] | 350 |     //} | 
|---|
 | 351 |  | 
|---|
 | 352 |   } | 
|---|
 | 353 |  | 
|---|
 | 354 |  | 
|---|
| [7317] | 355 | } | 
|---|