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