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