Changeset 8255 in orxonox.OLD for trunk/src/lib/sound
- Timestamp:
- Jun 8, 2006, 3:44:12 PM (19 years ago)
- Location:
- trunk/src/lib/sound
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/sound/sound_source.cc
r7810 r8255 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 159 160 /** 161 * @brief Plays and loops buffer on this Source 162 * @param buffer the buffer to play back on this Source 163 */ 164 void SoundSource::loop(const SoundBuffer* buffer) 165 { 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 } 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 179 391 } 180 181 182 /**183 * @brief Stops playback of a SoundSource184 */185 void SoundSource::stop()186 {187 this->bPlay = false;188 if (this->sourceID != 0)189 {190 this->bPlay = false;191 if (this->sourceID != 0)192 {193 alSourceStop(this->sourceID);194 if (DEBUG_LEVEL >= 3)195 SoundEngine::checkError("StopSource", __LINE__);196 alSourcei(this->sourceID, AL_BUFFER, 0);197 if (!this->resident)198 SoundEngine::getInstance()->pushALSource(this->sourceID);199 this->sourceID = 0;200 }201 }202 }203 204 /**205 * @brief Pauses Playback of a SoundSource206 */207 void SoundSource::pause()208 {209 alSourcePause(this->sourceID);210 if (DEBUG_LEVEL >= 3)211 SoundEngine::checkError("Pause Source", __LINE__);212 }213 214 215 /**216 * @brief Rewinds Playback of a SoundSource217 */218 void SoundSource::rewind()219 {220 alSourceRewind(this->sourceID);221 222 if (DEBUG_LEVEL >= 3)223 SoundEngine::checkError("Rewind Source", __LINE__);224 }225 226 227 /**228 * @brief sets the RolloffFactor of the Sound emitted from the SoundSource229 * @param rolloffFactor The Factor described230 *231 * this tells openAL how fast the Sounds decay outward from the Source232 */233 void SoundSource::setRolloffFactor(ALfloat rolloffFactor)234 {235 alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);236 237 if (DEBUG_LEVEL >= 3)238 SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);239 }240 241 242 /**243 * @brief sets the Positional this Source should be attached to.244 * @param sourceNode the Source this is attached to.245 * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels.246 */247 void SoundSource::setSourceNode(const PNode* sourceNode)248 {249 this->sourceNode = sourceNode;250 }251 252 /**253 * @brief retrieve a Source.254 */255 bool SoundSource::retrieveSource()256 {257 if (this->sourceID != 0)258 return true;259 else260 {261 SoundEngine::getInstance()->popALSource(this->sourceID);262 if (this->sourceID != 0)263 {264 if (unlikely(this->sourceNode == NULL))265 resetSource(this->sourceID);266 return true;267 }268 }269 return false;270 }271 272 273 /**274 * @brief reset an alSource to its default Values.275 */276 void SoundSource::resetSource(ALuint sourceID)277 {278 alSource3f(sourceID, AL_POSITION, 0.0, 0.0, 0.0);279 alSource3f(sourceID, AL_VELOCITY, 0.0, 0.0, 0.0);280 alSource3f(sourceID, AL_DIRECTION, 0.0, 0.0, 0.0);281 alSourcef (sourceID, AL_ROLLOFF_FACTOR, 0.0 );282 //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE );283 alSourcef (sourceID, AL_GAIN, SoundEngine::getInstance()->getEffectsVolume());284 }285 } -
trunk/src/lib/sound/sound_source.h
r7810 r8255 1 1 /*! 2 3 2 * @file sound_source.h 3 * @brief Definition of the SoundSource. 4 4 */ 5 5 … … 14 14 namespace OrxSound 15 15 { 16 class SoundBuffer; 17 //! A class that represents a SoundSource 18 class SoundSource : public BaseObject 19 { 20 public: 21 SoundSource(const PNode* sourceNode = NULL, const SoundBuffer* buffer = NULL); 22 SoundSource(const SoundSource& source); 23 SoundSource& operator=(const SoundSource& source); 24 bool operator==(const SoundSource& source); 25 26 virtual ~SoundSource(); 27 28 // user interaction 29 void play(); 30 void play(const SoundBuffer* buffer); 31 void loop(); 32 void loop(const SoundBuffer* buffer); 33 void stop(); 34 void pause(); 35 void rewind(); 36 37 // development functions 38 /** @returns The ID of this Source */ 39 inline ALuint getID() const { return this->sourceID; }; 40 /** @returns true, if the Source is Playing */ 41 inline bool isPlaying() const { return this->bPlay; }; 42 void setSourceNode(const PNode* sourceNode); 43 /** @returns the SoundBuffer of this Source */ 44 inline const SoundBuffer* getBuffer() const { return this->buffer; }; 45 /** @returns the SourceNode of this Source */ 46 inline const PNode* getNode() const { return this->sourceNode; }; 47 /** @param resident if the Source is Resident */ 48 inline void setResident(bool resident) { this->resident = resident; }; 49 /** @returns true if the alSource is Resident */ 50 inline bool isResident() const { return this->resident; }; 51 52 void setRolloffFactor(ALfloat rolloffFactor); 53 54 static void resetSource(ALuint sourceID); 55 56 private: 57 bool retrieveSource(); 58 59 private: 60 bool bPlay; //!< If the Source is Playing. 61 bool resident; //!< If the alSource should be resident (if true, the alSource will be returned on deletion). 62 ALuint sourceID; //!< The ID of the Source 63 const SoundBuffer* buffer; //!< The buffer to play in this source. 64 const PNode* sourceNode; //!< The SourceNode representing the position/velocity... of this source. 65 }; 16 class SoundBuffer; 17 //! A class that represents a SoundSource 18 class SoundSource : public BaseObject 19 { 20 public: 21 SoundSource(const PNode* sourceNode = NULL, const SoundBuffer* buffer = NULL); 22 SoundSource(const SoundSource& source); 23 SoundSource& operator=(const SoundSource& source); 24 bool operator==(const SoundSource& source); 25 26 virtual ~SoundSource(); 27 28 // user interaction 29 void play(); 30 void play(const SoundBuffer* buffer); 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); 35 void stop(); 36 void pause(); 37 void rewind(); 38 void fadein(const SoundBuffer* buffer, ALfloat duration); 39 40 41 // development functions 42 /** @returns The ID of this Source */ 43 inline ALuint getID() const { return this->sourceID; }; 44 /** @returns true, if the Source is Playing */ 45 inline bool isPlaying() const { return this->bPlay; }; 46 void setSourceNode(const PNode* sourceNode); 47 /** @returns the SoundBuffer of this Source */ 48 inline const SoundBuffer* getBuffer() const { return this->buffer; }; 49 /** @returns the SourceNode of this Source */ 50 inline const PNode* getNode() const { return this->sourceNode; }; 51 /** @param resident if the Source is Resident */ 52 inline void setResident(bool resident) { this->resident = resident; }; 53 /** @returns true if the alSource is Resident */ 54 inline bool isResident() const { return this->resident; }; 55 56 void setRolloffFactor(ALfloat rolloffFactor); 57 58 static void resetSource(ALuint sourceID); 59 60 private: 61 bool retrieveSource(); 62 63 private: 64 bool bPlay; //!< If the Source is Playing. 65 bool resident; //!< If the alSource should be resident (if true, the alSource will be returned on deletion). 66 ALuint sourceID; //!< The ID of the Source 67 const SoundBuffer* buffer; //!< The buffer to play in this source. 68 const PNode* sourceNode; //!< The SourceNode representing the position/velocity... of this source. 69 }; 66 70 } 67 71 #endif /* _SOUND_SOURCE_H */
Note: See TracChangeset
for help on using the changeset viewer.