| 1 | /* | 
|---|
| 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 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND | 
|---|
| 17 |  | 
|---|
| 18 | #include "sound_source.h" | 
|---|
| 19 | #include "sound_engine.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "alincl.h" | 
|---|
| 22 | #include "compiler.h" | 
|---|
| 23 |  | 
|---|
| 24 | namespace OrxSound | 
|---|
| 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 |         } | 
|---|
| 389 |  | 
|---|
| 390 |  | 
|---|
| 391 | } | 
|---|