[4744] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5386] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 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" |
---|
[1853] | 23 | |
---|
[7460] | 24 | namespace OrxSound |
---|
[5386] | 25 | { |
---|
[7460] | 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"); |
---|
[1856] | 32 | |
---|
[7460] | 33 | // adding the Source to the SourcesList of the SoundEngine |
---|
| 34 | this->buffer = buffer; |
---|
| 35 | this->sourceNode = sourceNode; |
---|
| 36 | this->resident = false; |
---|
[5386] | 37 | |
---|
[7460] | 38 | this->sourceID = 0; |
---|
| 39 | this->bPlay = false; |
---|
| 40 | } |
---|
[5386] | 41 | |
---|
[7317] | 42 | |
---|
[7460] | 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"); |
---|
[7299] | 53 | |
---|
[7460] | 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; |
---|
[7299] | 58 | |
---|
[7460] | 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; |
---|
[7299] | 67 | } |
---|
| 68 | |
---|
[7317] | 69 | |
---|
[7460] | 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; |
---|
[7299] | 81 | |
---|
[7460] | 82 | if (source.bPlay) |
---|
| 83 | this->play(); |
---|
| 84 | else |
---|
| 85 | this->stop(); |
---|
| 86 | } |
---|
[7299] | 87 | |
---|
[7317] | 88 | |
---|
[7460] | 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 | } |
---|
[7299] | 101 | |
---|
[7317] | 102 | |
---|
[7460] | 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 | } |
---|
[4320] | 112 | |
---|
[7317] | 113 | |
---|
[7460] | 114 | /** |
---|
| 115 | * @brief Plays back a SoundSource |
---|
| 116 | */ |
---|
| 117 | void SoundSource::play() |
---|
[7317] | 118 | { |
---|
[7460] | 119 | if (this->buffer && this->retrieveSource()) |
---|
| 120 | { |
---|
| 121 | if (this->bPlay) |
---|
| 122 | alSourceStop(this->sourceID); |
---|
| 123 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
---|
[7318] | 124 | alSourcePlay(this->sourceID); |
---|
| 125 | |
---|
[7729] | 126 | if (DEBUG_LEVEL >= 3) |
---|
[7460] | 127 | SoundEngine::checkError("Play Source", __LINE__); |
---|
| 128 | this->bPlay = true; |
---|
| 129 | } |
---|
[7317] | 130 | } |
---|
[4320] | 131 | |
---|
[7317] | 132 | |
---|
[7460] | 133 | /** |
---|
| 134 | * @brief Plays back buffer on this Source |
---|
| 135 | * @param buffer the buffer to play back on this Source |
---|
| 136 | */ |
---|
| 137 | void SoundSource::play(const SoundBuffer* buffer) |
---|
[7291] | 138 | { |
---|
[7460] | 139 | if (!this->retrieveSource()) |
---|
| 140 | { |
---|
| 141 | PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n"); |
---|
| 142 | return; |
---|
| 143 | } |
---|
[5930] | 144 | |
---|
[7460] | 145 | alSourceStop(this->sourceID); |
---|
| 146 | alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); |
---|
| 147 | alSourcePlay(this->sourceID); |
---|
[5386] | 148 | |
---|
[7460] | 149 | if (unlikely(this->buffer != NULL)) |
---|
| 150 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
---|
| 151 | this->bPlay = true; |
---|
[7290] | 152 | |
---|
[7729] | 153 | if (DEBUG_LEVEL >= 3) |
---|
[7460] | 154 | SoundEngine::checkError("Play Source", __LINE__); |
---|
[8122] | 155 | |
---|
[7460] | 156 | } |
---|
[1853] | 157 | |
---|
[7317] | 158 | |
---|
[7460] | 159 | /** |
---|
| 160 | * @brief Stops playback of a SoundSource |
---|
| 161 | */ |
---|
| 162 | void SoundSource::stop() |
---|
[7291] | 163 | { |
---|
[7460] | 164 | this->bPlay = false; |
---|
| 165 | if (this->sourceID != 0) |
---|
| 166 | { |
---|
| 167 | alSourceStop(this->sourceID); |
---|
[7729] | 168 | if (DEBUG_LEVEL >= 3) |
---|
[7460] | 169 | SoundEngine::checkError("StopSource", __LINE__); |
---|
| 170 | alSourcei(this->sourceID, AL_BUFFER, 0); |
---|
| 171 | if (!this->resident) |
---|
| 172 | SoundEngine::getInstance()->pushALSource(this->sourceID); |
---|
| 173 | this->sourceID = 0; |
---|
| 174 | } |
---|
[7291] | 175 | } |
---|
[1853] | 176 | |
---|
[7317] | 177 | |
---|
[7460] | 178 | /** |
---|
| 179 | * @brief Pauses Playback of a SoundSource |
---|
| 180 | */ |
---|
| 181 | void SoundSource::pause() |
---|
| 182 | { |
---|
| 183 | alSourcePause(this->sourceID); |
---|
[7729] | 184 | if (DEBUG_LEVEL >= 3) |
---|
[7460] | 185 | SoundEngine::checkError("Pause Source", __LINE__); |
---|
| 186 | } |
---|
[5386] | 187 | |
---|
[7317] | 188 | |
---|
[7460] | 189 | /** |
---|
| 190 | * @brief Rewinds Playback of a SoundSource |
---|
| 191 | */ |
---|
| 192 | void SoundSource::rewind() |
---|
| 193 | { |
---|
| 194 | alSourceRewind(this->sourceID); |
---|
[7290] | 195 | |
---|
[7729] | 196 | if (DEBUG_LEVEL >= 3) |
---|
[7460] | 197 | SoundEngine::checkError("Rewind Source", __LINE__); |
---|
| 198 | } |
---|
[5386] | 199 | |
---|
[7317] | 200 | |
---|
[7460] | 201 | /** |
---|
| 202 | * @brief sets the RolloffFactor of the Sound emitted from the SoundSource |
---|
| 203 | * @param rolloffFactor The Factor described |
---|
| 204 | * |
---|
| 205 | * this tells openAL how fast the Sounds decay outward from the Source |
---|
| 206 | */ |
---|
| 207 | void SoundSource::setRolloffFactor(ALfloat rolloffFactor) |
---|
| 208 | { |
---|
| 209 | alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor); |
---|
[7290] | 210 | |
---|
[7729] | 211 | if (DEBUG_LEVEL >= 3) |
---|
[7460] | 212 | SoundEngine::checkError("Set Source Rolloff-factor", __LINE__); |
---|
| 213 | } |
---|
[5386] | 214 | |
---|
[7317] | 215 | |
---|
[7460] | 216 | /** |
---|
| 217 | * @brief sets the Positional this Source should be attached to. |
---|
| 218 | * @param sourceNode the Source this is attached to. |
---|
| 219 | * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels. |
---|
| 220 | */ |
---|
| 221 | void SoundSource::setSourceNode(const PNode* sourceNode) |
---|
| 222 | { |
---|
| 223 | this->sourceNode = sourceNode; |
---|
| 224 | } |
---|
[7317] | 225 | |
---|
[7460] | 226 | /** |
---|
| 227 | * @brief retrieve a Source. |
---|
| 228 | */ |
---|
| 229 | bool SoundSource::retrieveSource() |
---|
[7317] | 230 | { |
---|
| 231 | if (this->sourceID != 0) |
---|
[7460] | 232 | return true; |
---|
| 233 | else |
---|
[7317] | 234 | { |
---|
[7460] | 235 | SoundEngine::getInstance()->popALSource(this->sourceID); |
---|
| 236 | if (this->sourceID != 0) |
---|
| 237 | { |
---|
| 238 | if (unlikely(this->sourceNode == NULL)) |
---|
| 239 | resetSource(this->sourceID); |
---|
| 240 | return true; |
---|
| 241 | } |
---|
[7317] | 242 | } |
---|
[7460] | 243 | return false; |
---|
[7317] | 244 | } |
---|
| 245 | |
---|
| 246 | |
---|
[7460] | 247 | /** |
---|
| 248 | * @brief reset an alSource to its default Values. |
---|
| 249 | */ |
---|
| 250 | void SoundSource::resetSource(ALuint sourceID) |
---|
| 251 | { |
---|
| 252 | alSource3f(sourceID, AL_POSITION, 0.0, 0.0, 0.0); |
---|
| 253 | alSource3f(sourceID, AL_VELOCITY, 0.0, 0.0, 0.0); |
---|
| 254 | alSource3f(sourceID, AL_DIRECTION, 0.0, 0.0, 0.0); |
---|
| 255 | alSourcef (sourceID, AL_ROLLOFF_FACTOR, 0.0 ); |
---|
| 256 | //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE ); |
---|
| 257 | alSourcef (sourceID, AL_GAIN, SoundEngine::getInstance()->getEffectsVolume()); |
---|
| 258 | } |
---|
[7317] | 259 | } |
---|