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