[3060] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
[5896] | 23 | * Erwin 'vaiursch' Herrsche |
---|
[3060] | 24 | * Co-authors: |
---|
[5896] | 25 | * Reto Grieder |
---|
[3060] | 26 | * |
---|
| 27 | */ |
---|
[3196] | 28 | |
---|
[5896] | 29 | #include "BaseSound.h" |
---|
[3196] | 30 | |
---|
[6117] | 31 | #include <cassert> |
---|
[3060] | 32 | #include <vector> |
---|
[6203] | 33 | #include <al.h> |
---|
[3060] | 34 | |
---|
[5896] | 35 | #include "core/CoreIncludes.h" |
---|
| 36 | #include "core/GameMode.h" |
---|
[5695] | 37 | #include "core/Resource.h" |
---|
[6117] | 38 | #include "core/XMLPort.h" |
---|
[6203] | 39 | #include "SoundBuffer.h" |
---|
[6186] | 40 | #include "SoundManager.h" |
---|
[3060] | 41 | |
---|
[5738] | 42 | namespace orxonox |
---|
[3060] | 43 | { |
---|
[5896] | 44 | BaseSound::BaseSound() |
---|
[5899] | 45 | : audioSource_(0) |
---|
[6254] | 46 | , bPooling_(false) |
---|
[6203] | 47 | , volume_(1.0) |
---|
[6307] | 48 | , bLooping_(false) |
---|
[6117] | 49 | , state_(Stopped) |
---|
[6222] | 50 | , pitch_ (1.0) |
---|
[3060] | 51 | { |
---|
[5896] | 52 | RegisterRootObject(BaseSound); |
---|
[6117] | 53 | |
---|
| 54 | if (GameMode::playsSound()) |
---|
| 55 | { |
---|
| 56 | alGenSources(1, &this->audioSource_); |
---|
[6260] | 57 | if (!alIsSource(this->audioSource_)) |
---|
| 58 | COUT(1) << "Sound: Source generation failed: " << SoundManager::getALErrorString(alGetError()) << std::endl; |
---|
[6271] | 59 | |
---|
| 60 | if (alIsSource(this->audioSource_)) |
---|
| 61 | { |
---|
| 62 | alSourcei(this->audioSource_, AL_REFERENCE_DISTANCE, 20); |
---|
[6285] | 63 | alSourcei(this->audioSource_, AL_MAX_DISTANCE, 10000); |
---|
[6271] | 64 | } |
---|
[6117] | 65 | } |
---|
[3060] | 66 | } |
---|
| 67 | |
---|
[5896] | 68 | BaseSound::~BaseSound() |
---|
[3060] | 69 | { |
---|
[6270] | 70 | this->setSource(std::string()); |
---|
[6260] | 71 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[6117] | 72 | alDeleteSources(1, &this->audioSource_); |
---|
[3060] | 73 | } |
---|
| 74 | |
---|
[6117] | 75 | void BaseSound::XMLPortExtern(Element& xmlelement, XMLPort::Mode mode) |
---|
| 76 | { |
---|
| 77 | XMLPortParam(BaseSound, "volume", setVolume, getVolume, xmlelement, mode); |
---|
| 78 | XMLPortParam(BaseSound, "loop", setLooping, getLooping, xmlelement, mode); |
---|
[6188] | 79 | XMLPortParam(BaseSound, "play", setPlaying, isPlaying, xmlelement, mode); |
---|
[6117] | 80 | XMLPortParam(BaseSound, "source", setSource, getSource, xmlelement, mode); |
---|
| 81 | } |
---|
| 82 | |
---|
[5896] | 83 | void BaseSound::play() |
---|
[3060] | 84 | { |
---|
[6255] | 85 | this->state_ = Playing; |
---|
[6260] | 86 | if (GameMode::playsSound() && alIsSource(this->audioSource_) && this->getSourceState() != AL_PLAYING) |
---|
[5896] | 87 | { |
---|
[5899] | 88 | alSourcePlay(this->audioSource_); |
---|
[3108] | 89 | |
---|
[6203] | 90 | if (int error = alGetError()) |
---|
| 91 | COUT(2) << "Sound: Error playing sound: " << error << std::endl; |
---|
[3060] | 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
[5896] | 95 | void BaseSound::stop() |
---|
| 96 | { |
---|
[6117] | 97 | this->state_ = Stopped; |
---|
[6260] | 98 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[5899] | 99 | alSourceStop(this->audioSource_); |
---|
[3060] | 100 | } |
---|
| 101 | |
---|
[5896] | 102 | void BaseSound::pause() |
---|
| 103 | { |
---|
[6117] | 104 | if (this->isStopped()) |
---|
| 105 | return; |
---|
| 106 | this->state_ = Paused; |
---|
[6260] | 107 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[5899] | 108 | alSourcePause(this->audioSource_); |
---|
[3060] | 109 | } |
---|
| 110 | |
---|
[6255] | 111 | ALint BaseSound::getSourceState() const |
---|
| 112 | { |
---|
[6260] | 113 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[6255] | 114 | { |
---|
| 115 | ALint state; |
---|
| 116 | alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state); |
---|
| 117 | return state; |
---|
| 118 | } |
---|
| 119 | else |
---|
| 120 | return AL_INITIAL; |
---|
| 121 | } |
---|
| 122 | |
---|
[6117] | 123 | void BaseSound::setVolume(float vol) |
---|
[5896] | 124 | { |
---|
[6117] | 125 | if (vol > 1 || vol < 0) |
---|
| 126 | { |
---|
| 127 | COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl; |
---|
| 128 | vol = vol > 1 ? 1 : vol; |
---|
| 129 | vol = vol < 0 ? 0 : vol; |
---|
| 130 | } |
---|
| 131 | this->volume_ = vol; |
---|
[6186] | 132 | |
---|
| 133 | this->updateVolume(); |
---|
[3060] | 134 | } |
---|
[6184] | 135 | |
---|
[6186] | 136 | float BaseSound::getVolumeGain() |
---|
[6184] | 137 | { |
---|
[6186] | 138 | return SoundManager::getInstance().getVolume(SoundType::none); |
---|
[6184] | 139 | } |
---|
| 140 | |
---|
[6186] | 141 | void BaseSound::updateVolume(void) |
---|
[6184] | 142 | { |
---|
[6186] | 143 | if (alIsSource(this->audioSource_)) |
---|
[6204] | 144 | { |
---|
[6186] | 145 | alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain()); |
---|
[6204] | 146 | if (int error = alGetError()) |
---|
| 147 | COUT(2) << "Sound: Error setting volume: " << error << std::endl; |
---|
| 148 | } |
---|
[6184] | 149 | } |
---|
[3060] | 150 | |
---|
[6117] | 151 | void BaseSound::setLooping(bool val) |
---|
[5896] | 152 | { |
---|
[6307] | 153 | this->bLooping_ = val; |
---|
[6260] | 154 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[6117] | 155 | alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE)); |
---|
[3060] | 156 | } |
---|
| 157 | |
---|
[6202] | 158 | void BaseSound::setPitch(float pitch) |
---|
| 159 | { |
---|
| 160 | if (pitch > 2 || pitch < 0.5) |
---|
| 161 | { |
---|
| 162 | COUT(2) << "Sound warning: pitch out of range, cropping value." << std::endl; |
---|
| 163 | pitch = pitch > 2 ? 2 : pitch; |
---|
| 164 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
| 165 | } |
---|
| 166 | this->pitch_ = pitch; |
---|
[6260] | 167 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[6204] | 168 | { |
---|
| 169 | if (int error = alGetError()) |
---|
| 170 | COUT(2) << "Sound: Error setting pitch: " << error << std::endl; |
---|
[6205] | 171 | alSourcef(this->audioSource_, AL_PITCH, pitch); |
---|
[6204] | 172 | } |
---|
[6202] | 173 | } |
---|
| 174 | |
---|
[5899] | 175 | void BaseSound::setSource(const std::string& source) |
---|
[5896] | 176 | { |
---|
[6117] | 177 | if (!GameMode::playsSound() || source == this->source_) |
---|
| 178 | { |
---|
| 179 | this->source_ = source; |
---|
[5896] | 180 | return; |
---|
[6117] | 181 | } |
---|
[5896] | 182 | |
---|
[6203] | 183 | if (this->soundBuffer_ != NULL) |
---|
[3060] | 184 | { |
---|
[6260] | 185 | if (alIsSource(this->audioSource_)) |
---|
| 186 | { |
---|
| 187 | alSourceStop(this->audioSource_); |
---|
| 188 | // Unload old buffer first |
---|
| 189 | alSourcei(this->audioSource_, AL_BUFFER, 0); |
---|
| 190 | } |
---|
[6254] | 191 | SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_); |
---|
[6203] | 192 | this->soundBuffer_.reset(); |
---|
[3060] | 193 | } |
---|
| 194 | |
---|
[6117] | 195 | this->source_ = source; |
---|
[6260] | 196 | if (source_.empty() || !alIsSource(this->audioSource_)) |
---|
[6117] | 197 | return; |
---|
| 198 | |
---|
[6270] | 199 | this->soundBuffer_ = SoundManager::getInstance().getSoundBuffer(this->source_); |
---|
[6203] | 200 | if (this->soundBuffer_ == NULL) |
---|
| 201 | return; |
---|
[5695] | 202 | |
---|
[6203] | 203 | alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer()); |
---|
[6260] | 204 | if (ALuint error = alGetError()) |
---|
[5896] | 205 | { |
---|
[6307] | 206 | COUT(1) << "Sound Error: Could not load file \"" << source << "\": " << SoundManager::getALErrorString(error) << std::endl; |
---|
[5896] | 207 | return; |
---|
[3060] | 208 | } |
---|
[5896] | 209 | |
---|
[5899] | 210 | alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0); |
---|
[6186] | 211 | this->updateVolume(); |
---|
[6202] | 212 | this->setPitch(this->getPitch()); |
---|
[6204] | 213 | this->setLooping(getLooping()); |
---|
[6117] | 214 | if (this->isPlaying() || this->isPaused()) |
---|
[6234] | 215 | { |
---|
| 216 | alSourcePlay(this->audioSource_); |
---|
| 217 | if (int error = alGetError()) |
---|
| 218 | COUT(2) << "Sound: Error playing sound: " << error << std::endl; |
---|
| 219 | } |
---|
[6117] | 220 | if (this->isPaused()) |
---|
[6234] | 221 | alSourcePause(this->audioSource_); |
---|
[3060] | 222 | } |
---|
[6117] | 223 | } |
---|