[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) |
---|
[5896] | 48 | , bLoop_(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; |
---|
[6117] | 59 | } |
---|
[3060] | 60 | } |
---|
| 61 | |
---|
[5896] | 62 | BaseSound::~BaseSound() |
---|
[3060] | 63 | { |
---|
[5899] | 64 | this->setSource(""); |
---|
[6260] | 65 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[6117] | 66 | alDeleteSources(1, &this->audioSource_); |
---|
[3060] | 67 | } |
---|
| 68 | |
---|
[6117] | 69 | void BaseSound::XMLPortExtern(Element& xmlelement, XMLPort::Mode mode) |
---|
| 70 | { |
---|
| 71 | XMLPortParam(BaseSound, "volume", setVolume, getVolume, xmlelement, mode); |
---|
| 72 | XMLPortParam(BaseSound, "loop", setLooping, getLooping, xmlelement, mode); |
---|
[6188] | 73 | XMLPortParam(BaseSound, "play", setPlaying, isPlaying, xmlelement, mode); |
---|
[6117] | 74 | XMLPortParam(BaseSound, "source", setSource, getSource, xmlelement, mode); |
---|
| 75 | } |
---|
| 76 | |
---|
[5896] | 77 | void BaseSound::play() |
---|
[3060] | 78 | { |
---|
[6255] | 79 | this->state_ = Playing; |
---|
[6260] | 80 | if (GameMode::playsSound() && alIsSource(this->audioSource_) && this->getSourceState() != AL_PLAYING) |
---|
[5896] | 81 | { |
---|
[5899] | 82 | alSourcePlay(this->audioSource_); |
---|
[3108] | 83 | |
---|
[6203] | 84 | if (int error = alGetError()) |
---|
| 85 | COUT(2) << "Sound: Error playing sound: " << error << std::endl; |
---|
[3060] | 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
[5896] | 89 | void BaseSound::stop() |
---|
| 90 | { |
---|
[6117] | 91 | this->state_ = Stopped; |
---|
[6260] | 92 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[5899] | 93 | alSourceStop(this->audioSource_); |
---|
[3060] | 94 | } |
---|
| 95 | |
---|
[5896] | 96 | void BaseSound::pause() |
---|
| 97 | { |
---|
[6117] | 98 | if (this->isStopped()) |
---|
| 99 | return; |
---|
| 100 | this->state_ = Paused; |
---|
[6260] | 101 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[5899] | 102 | alSourcePause(this->audioSource_); |
---|
[3060] | 103 | } |
---|
| 104 | |
---|
[6255] | 105 | ALint BaseSound::getSourceState() const |
---|
| 106 | { |
---|
[6260] | 107 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[6255] | 108 | { |
---|
| 109 | ALint state; |
---|
| 110 | alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state); |
---|
| 111 | return state; |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | return AL_INITIAL; |
---|
| 115 | } |
---|
| 116 | |
---|
[6117] | 117 | void BaseSound::setVolume(float vol) |
---|
[5896] | 118 | { |
---|
[6117] | 119 | if (vol > 1 || vol < 0) |
---|
| 120 | { |
---|
| 121 | COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl; |
---|
| 122 | vol = vol > 1 ? 1 : vol; |
---|
| 123 | vol = vol < 0 ? 0 : vol; |
---|
| 124 | } |
---|
| 125 | this->volume_ = vol; |
---|
[6186] | 126 | |
---|
| 127 | this->updateVolume(); |
---|
[3060] | 128 | } |
---|
[6184] | 129 | |
---|
[6186] | 130 | float BaseSound::getVolumeGain() |
---|
[6184] | 131 | { |
---|
[6186] | 132 | return SoundManager::getInstance().getVolume(SoundType::none); |
---|
[6184] | 133 | } |
---|
| 134 | |
---|
[6186] | 135 | void BaseSound::updateVolume(void) |
---|
[6184] | 136 | { |
---|
[6186] | 137 | if (alIsSource(this->audioSource_)) |
---|
[6204] | 138 | { |
---|
[6186] | 139 | alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain()); |
---|
[6204] | 140 | if (int error = alGetError()) |
---|
| 141 | COUT(2) << "Sound: Error setting volume: " << error << std::endl; |
---|
| 142 | } |
---|
[6184] | 143 | } |
---|
[3060] | 144 | |
---|
[6117] | 145 | void BaseSound::setLooping(bool val) |
---|
[5896] | 146 | { |
---|
[6117] | 147 | this->bLoop_ = val; |
---|
[6260] | 148 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[6117] | 149 | alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE)); |
---|
[3060] | 150 | } |
---|
| 151 | |
---|
[6202] | 152 | void BaseSound::setPitch(float pitch) |
---|
| 153 | { |
---|
| 154 | if (pitch > 2 || pitch < 0.5) |
---|
| 155 | { |
---|
| 156 | COUT(2) << "Sound warning: pitch out of range, cropping value." << std::endl; |
---|
| 157 | pitch = pitch > 2 ? 2 : pitch; |
---|
| 158 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
| 159 | } |
---|
| 160 | this->pitch_ = pitch; |
---|
[6260] | 161 | if (GameMode::playsSound() && alIsSource(this->audioSource_)) |
---|
[6204] | 162 | { |
---|
| 163 | if (int error = alGetError()) |
---|
| 164 | COUT(2) << "Sound: Error setting pitch: " << error << std::endl; |
---|
[6205] | 165 | alSourcef(this->audioSource_, AL_PITCH, pitch); |
---|
[6204] | 166 | } |
---|
[6202] | 167 | } |
---|
| 168 | |
---|
[5899] | 169 | void BaseSound::setSource(const std::string& source) |
---|
[5896] | 170 | { |
---|
[6117] | 171 | if (!GameMode::playsSound() || source == this->source_) |
---|
| 172 | { |
---|
| 173 | this->source_ = source; |
---|
[5896] | 174 | return; |
---|
[6117] | 175 | } |
---|
[5896] | 176 | |
---|
[6203] | 177 | if (this->soundBuffer_ != NULL) |
---|
[3060] | 178 | { |
---|
[6260] | 179 | if (alIsSource(this->audioSource_)) |
---|
| 180 | { |
---|
| 181 | alSourceStop(this->audioSource_); |
---|
| 182 | // Unload old buffer first |
---|
| 183 | alSourcei(this->audioSource_, AL_BUFFER, 0); |
---|
| 184 | } |
---|
[6254] | 185 | SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_); |
---|
[6203] | 186 | this->soundBuffer_.reset(); |
---|
[3060] | 187 | } |
---|
| 188 | |
---|
[6117] | 189 | this->source_ = source; |
---|
[6260] | 190 | if (source_.empty() || !alIsSource(this->audioSource_)) |
---|
[6117] | 191 | return; |
---|
| 192 | |
---|
[5695] | 193 | // Get DataStream from the resources |
---|
[5899] | 194 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source); |
---|
[5896] | 195 | if (fileInfo == NULL) |
---|
| 196 | { |
---|
[6117] | 197 | COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl; |
---|
[5896] | 198 | return; |
---|
[5695] | 199 | } |
---|
| 200 | |
---|
[6203] | 201 | this->soundBuffer_ = SoundManager::getInstance().getSoundBuffer(fileInfo); |
---|
| 202 | if (this->soundBuffer_ == NULL) |
---|
| 203 | return; |
---|
[5695] | 204 | |
---|
[6203] | 205 | alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer()); |
---|
[6260] | 206 | if (ALuint error = alGetError()) |
---|
[5896] | 207 | { |
---|
[6260] | 208 | COUT(1) << "Sound Error: Could not load file \"" << source << "\": " << SoundManager::getALErrorString << std::endl; |
---|
[5896] | 209 | return; |
---|
[3060] | 210 | } |
---|
[5896] | 211 | |
---|
[5899] | 212 | alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0); |
---|
[6186] | 213 | this->updateVolume(); |
---|
[6202] | 214 | this->setPitch(this->getPitch()); |
---|
[6204] | 215 | this->setLooping(getLooping()); |
---|
[6117] | 216 | if (this->isPlaying() || this->isPaused()) |
---|
[6234] | 217 | { |
---|
| 218 | alSourcePlay(this->audioSource_); |
---|
| 219 | if (int error = alGetError()) |
---|
| 220 | COUT(2) << "Sound: Error playing sound: " << error << std::endl; |
---|
| 221 | } |
---|
[6117] | 222 | if (this->isPaused()) |
---|
[6234] | 223 | alSourcePause(this->audioSource_); |
---|
[3060] | 224 | } |
---|
[6117] | 225 | } |
---|