[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> |
---|
| 33 | #include <AL/alut.h> |
---|
| 34 | #include <vorbis/vorbisfile.h> |
---|
| 35 | |
---|
[5896] | 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "core/GameMode.h" |
---|
[5695] | 38 | #include "core/Resource.h" |
---|
[6117] | 39 | #include "core/XMLPort.h" |
---|
[6186] | 40 | #include "SoundManager.h" |
---|
[3060] | 41 | |
---|
[5738] | 42 | namespace orxonox |
---|
[3060] | 43 | { |
---|
[5896] | 44 | BaseSound::BaseSound() |
---|
[5899] | 45 | : audioSource_(0) |
---|
| 46 | , audioBuffer_(0) |
---|
[5896] | 47 | , bLoop_(false) |
---|
[6117] | 48 | , state_(Stopped) |
---|
[3060] | 49 | { |
---|
[5896] | 50 | RegisterRootObject(BaseSound); |
---|
[6117] | 51 | |
---|
[6202] | 52 | this->volume_ = 1; |
---|
| 53 | this->pitch_ = 1; |
---|
| 54 | |
---|
[6117] | 55 | if (GameMode::playsSound()) |
---|
| 56 | { |
---|
| 57 | alGenSources(1, &this->audioSource_); |
---|
| 58 | assert(this->audioSource_ != 0); |
---|
| 59 | } |
---|
[3060] | 60 | } |
---|
| 61 | |
---|
[5896] | 62 | BaseSound::~BaseSound() |
---|
[3060] | 63 | { |
---|
[5899] | 64 | this->setSource(""); |
---|
[6117] | 65 | if (GameMode::playsSound()) |
---|
| 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 | { |
---|
[6117] | 79 | if (!this->isPlaying() && GameMode::showsGraphics()) |
---|
[5896] | 80 | { |
---|
[6117] | 81 | this->state_ = Playing; |
---|
[5899] | 82 | alSourcePlay(this->audioSource_); |
---|
[3108] | 83 | |
---|
[5896] | 84 | if (alGetError() != AL_NO_ERROR) |
---|
[6117] | 85 | COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl; |
---|
[3060] | 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
[5896] | 89 | void BaseSound::stop() |
---|
| 90 | { |
---|
[6117] | 91 | this->state_ = Stopped; |
---|
| 92 | if (GameMode::playsSound()) |
---|
[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; |
---|
| 101 | if (GameMode::playsSound()) |
---|
[5899] | 102 | alSourcePause(this->audioSource_); |
---|
[3060] | 103 | } |
---|
| 104 | |
---|
[6117] | 105 | void BaseSound::setVolume(float vol) |
---|
[5896] | 106 | { |
---|
[6117] | 107 | if (vol > 1 || vol < 0) |
---|
| 108 | { |
---|
| 109 | COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl; |
---|
| 110 | vol = vol > 1 ? 1 : vol; |
---|
| 111 | vol = vol < 0 ? 0 : vol; |
---|
| 112 | } |
---|
| 113 | this->volume_ = vol; |
---|
[6186] | 114 | |
---|
| 115 | this->updateVolume(); |
---|
[3060] | 116 | } |
---|
[6184] | 117 | |
---|
[6186] | 118 | float BaseSound::getVolumeGain() |
---|
[6184] | 119 | { |
---|
[6186] | 120 | return SoundManager::getInstance().getVolume(SoundType::none); |
---|
[6184] | 121 | } |
---|
| 122 | |
---|
[6186] | 123 | void BaseSound::updateVolume(void) |
---|
[6184] | 124 | { |
---|
[6186] | 125 | if (alIsSource(this->audioSource_)) |
---|
| 126 | alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain()); |
---|
[6184] | 127 | } |
---|
[3060] | 128 | |
---|
[6117] | 129 | void BaseSound::setLooping(bool val) |
---|
[5896] | 130 | { |
---|
[6117] | 131 | this->bLoop_ = val; |
---|
| 132 | if (GameMode::playsSound()) |
---|
| 133 | alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE)); |
---|
[3060] | 134 | } |
---|
| 135 | |
---|
[6202] | 136 | void BaseSound::setPitch(float pitch) |
---|
| 137 | { |
---|
| 138 | if (pitch > 2 || pitch < 0.5) |
---|
| 139 | { |
---|
| 140 | COUT(2) << "Sound warning: pitch out of range, cropping value." << std::endl; |
---|
| 141 | pitch = pitch > 2 ? 2 : pitch; |
---|
| 142 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
| 143 | } |
---|
| 144 | this->pitch_ = pitch; |
---|
| 145 | if (GameMode::playsSound()) |
---|
| 146 | alSourcei(this->audioSource_, AL_PITCH, pitch); |
---|
| 147 | } |
---|
| 148 | |
---|
[5899] | 149 | void BaseSound::setSource(const std::string& source) |
---|
[5896] | 150 | { |
---|
[6117] | 151 | if (!GameMode::playsSound() || source == this->source_) |
---|
| 152 | { |
---|
| 153 | this->source_ = source; |
---|
[5896] | 154 | return; |
---|
[6117] | 155 | } |
---|
[5896] | 156 | |
---|
[6117] | 157 | if (this->audioBuffer_ != 0 && alIsBuffer(this->audioBuffer_)) |
---|
[3060] | 158 | { |
---|
[6117] | 159 | alSourceStop(this->audioSource_); |
---|
| 160 | // Unload old sound first |
---|
[5899] | 161 | alSourcei(this->audioSource_, AL_BUFFER, 0); |
---|
| 162 | alDeleteBuffers(1, &this->audioBuffer_); |
---|
[6117] | 163 | this->audioBuffer_ = 0; |
---|
[3060] | 164 | } |
---|
| 165 | |
---|
[6117] | 166 | this->source_ = source; |
---|
| 167 | if (source_.empty()) |
---|
| 168 | return; |
---|
| 169 | |
---|
[5899] | 170 | COUT(3) << "Sound: OpenAL ALUT: loading file " << source << std::endl; |
---|
[5695] | 171 | // Get DataStream from the resources |
---|
[5899] | 172 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source); |
---|
[5896] | 173 | if (fileInfo == NULL) |
---|
| 174 | { |
---|
[6117] | 175 | COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl; |
---|
[5896] | 176 | return; |
---|
[5695] | 177 | } |
---|
[5904] | 178 | dataStream_ = Resource::open(source); |
---|
[5695] | 179 | // Read everything into a temporary buffer |
---|
| 180 | char* buffer = new char[fileInfo->size]; |
---|
[5904] | 181 | dataStream_->read(buffer, fileInfo->size); |
---|
| 182 | dataStream_->seek(0); |
---|
[5695] | 183 | |
---|
[5899] | 184 | this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size); |
---|
[5695] | 185 | delete[] buffer; |
---|
| 186 | |
---|
[5899] | 187 | if (this->audioBuffer_ == AL_NONE) |
---|
[5896] | 188 | { |
---|
[3060] | 189 | COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
[5904] | 190 | if (source.find("ogg", 0) != std::string::npos) |
---|
| 191 | { |
---|
| 192 | COUT(2) << "Sound: Trying fallback ogg loader" << std::endl; |
---|
[6117] | 193 | this->audioBuffer_ = this->loadOggFile(); |
---|
[5904] | 194 | } |
---|
[3060] | 195 | |
---|
[5904] | 196 | if (this->audioBuffer_ == AL_NONE) |
---|
| 197 | { |
---|
| 198 | COUT(2) << "Sound: fallback ogg loader failed: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
| 199 | return; |
---|
| 200 | } |
---|
[3060] | 201 | } |
---|
| 202 | |
---|
[5899] | 203 | alSourcei(this->audioSource_, AL_BUFFER, this->audioBuffer_); |
---|
[5896] | 204 | if (alGetError() != AL_NO_ERROR) |
---|
| 205 | { |
---|
[5899] | 206 | COUT(2) << "Sound: OpenAL: Error loading sample file: " << source << 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()); |
---|
[6117] | 213 | alSourcei (this->audioSource_, AL_LOOPING, (this->bLoop_ ? AL_TRUE : AL_FALSE)); |
---|
| 214 | if (this->isPlaying() || this->isPaused()) |
---|
| 215 | alSourcePlay(this->audioSource_); |
---|
| 216 | if (this->isPaused()) |
---|
| 217 | alSourcePause(this->audioSource_); |
---|
[5896] | 218 | |
---|
[6117] | 219 | if (alGetError() != AL_NO_ERROR) |
---|
| 220 | COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl; |
---|
[3060] | 221 | } |
---|
| 222 | |
---|
[5904] | 223 | size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource) |
---|
[3060] | 224 | { |
---|
[5904] | 225 | return static_cast<Ogre::DataStream*>(datasource)->read(ptr, size * nmemb); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | int seekVorbis(void* datasource, ogg_int64_t offset, int whence) |
---|
| 229 | { |
---|
| 230 | Ogre::DataStream* stream = static_cast<Ogre::DataStream*>(datasource); |
---|
| 231 | int offset_beg = offset; |
---|
| 232 | if (whence == SEEK_CUR) |
---|
| 233 | offset_beg = stream->tell() + offset; |
---|
| 234 | else if (whence == SEEK_END) |
---|
| 235 | offset_beg = stream->size() + offset; |
---|
| 236 | else if (whence != SEEK_SET) |
---|
| 237 | return -1; |
---|
| 238 | stream->seek(offset_beg); |
---|
| 239 | return 0; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | long tellVorbis(void* datasource) |
---|
| 243 | { |
---|
| 244 | return static_cast<long>(static_cast<Ogre::DataStream*>(datasource)->tell()); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | ALuint BaseSound::loadOggFile() |
---|
| 248 | { |
---|
[6127] | 249 | char inbuffer[256*1024]; |
---|
[3060] | 250 | std::vector<char> outbuffer; |
---|
[6127] | 251 | outbuffer.reserve(80*1024*1024); |
---|
[3060] | 252 | OggVorbis_File vf; |
---|
[3075] | 253 | vorbis_info* vorbisInfo; |
---|
[3060] | 254 | int eof = false; |
---|
| 255 | int current_section; |
---|
[3075] | 256 | ALuint buffer; |
---|
| 257 | ALenum format; |
---|
[3060] | 258 | |
---|
[5904] | 259 | // Open file with custom streaming |
---|
| 260 | ov_callbacks vorbisCallbacks; |
---|
| 261 | vorbisCallbacks.read_func = &readVorbis; |
---|
| 262 | vorbisCallbacks.seek_func = &seekVorbis; |
---|
| 263 | vorbisCallbacks.tell_func = &tellVorbis; |
---|
| 264 | vorbisCallbacks.close_func = NULL; |
---|
[3060] | 265 | |
---|
[5904] | 266 | int ret = ov_open_callbacks(dataStream_.get(), &vf, NULL, 0, vorbisCallbacks); |
---|
| 267 | if (ret < 0) |
---|
[3060] | 268 | { |
---|
[3370] | 269 | COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl; |
---|
[3060] | 270 | ov_clear(&vf); |
---|
| 271 | return AL_NONE; |
---|
| 272 | } |
---|
| 273 | |
---|
[5896] | 274 | while (!eof) |
---|
[3060] | 275 | { |
---|
| 276 | long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); |
---|
| 277 | if (ret == 0) |
---|
| 278 | { |
---|
| 279 | eof = true; |
---|
| 280 | } |
---|
| 281 | else if (ret < 0) |
---|
| 282 | { |
---|
| 283 | COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; |
---|
| 284 | ov_clear(&vf); |
---|
| 285 | return AL_NONE; |
---|
| 286 | } |
---|
| 287 | else |
---|
| 288 | { |
---|
[6127] | 289 | outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + ret); |
---|
[3060] | 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
[3075] | 293 | vorbisInfo = ov_info(&vf, -1); |
---|
[5896] | 294 | if (vorbisInfo->channels == 1) |
---|
[3075] | 295 | format = AL_FORMAT_MONO16; |
---|
| 296 | else |
---|
| 297 | format = AL_FORMAT_STEREO16; |
---|
| 298 | |
---|
| 299 | alGenBuffers(1, &buffer); |
---|
| 300 | alBufferData(buffer, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate); |
---|
[3060] | 301 | ov_clear(&vf); |
---|
| 302 | |
---|
[3075] | 303 | return buffer; |
---|
[3060] | 304 | } |
---|
[6117] | 305 | } |
---|