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