[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 | |
---|
[5896] | 72 | void BaseSound::stop() |
---|
| 73 | { |
---|
[5899] | 74 | if (alIsSource(this->audioSource_)) |
---|
| 75 | alSourceStop(this->audioSource_); |
---|
[3060] | 76 | } |
---|
| 77 | |
---|
[5896] | 78 | void BaseSound::pause() |
---|
| 79 | { |
---|
[5899] | 80 | if (alIsSource(this->audioSource_)) |
---|
| 81 | alSourcePause(this->audioSource_); |
---|
[3060] | 82 | } |
---|
| 83 | |
---|
[5896] | 84 | bool BaseSound::isPlaying() |
---|
| 85 | { |
---|
[5899] | 86 | if (alIsSource(this->audioSource_)) |
---|
[3060] | 87 | return getSourceState() == AL_PLAYING; |
---|
| 88 | return false; |
---|
| 89 | } |
---|
| 90 | |
---|
[5896] | 91 | bool BaseSound::isPaused() |
---|
| 92 | { |
---|
[5899] | 93 | if (alIsSource(this->audioSource_)) |
---|
[3060] | 94 | return getSourceState() == AL_PAUSED; |
---|
| 95 | return true; |
---|
| 96 | } |
---|
| 97 | |
---|
[5896] | 98 | bool BaseSound::isStopped() |
---|
| 99 | { |
---|
[5899] | 100 | if (alIsSource(this->audioSource_)) |
---|
[3060] | 101 | return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED; |
---|
| 102 | return true; |
---|
| 103 | } |
---|
| 104 | |
---|
[5896] | 105 | void BaseSound::setPlayOnLoad(bool val) |
---|
| 106 | { |
---|
| 107 | this->bPlayOnLoad_ = true; |
---|
| 108 | this->play(); |
---|
| 109 | } |
---|
| 110 | |
---|
[5899] | 111 | void BaseSound::setSource(const std::string& source) |
---|
[5896] | 112 | { |
---|
[5954] | 113 | if (!GameMode::playsSound()) |
---|
| 114 | { |
---|
| 115 | this->source_ = source; |
---|
[5896] | 116 | return; |
---|
[5954] | 117 | } |
---|
| 118 | |
---|
| 119 | if (this->source_ != source && alIsSource(this->audioSource_)) |
---|
[3060] | 120 | { |
---|
[5896] | 121 | // Unload sound |
---|
[5899] | 122 | alSourcei(this->audioSource_, AL_BUFFER, 0); |
---|
| 123 | alDeleteSources(1, &this->audioSource_); |
---|
| 124 | alDeleteBuffers(1, &this->audioBuffer_); |
---|
[5954] | 125 | } |
---|
| 126 | |
---|
| 127 | this->source_ = source; |
---|
| 128 | |
---|
| 129 | if(source_.empty()) |
---|
| 130 | { |
---|
[5896] | 131 | return; |
---|
[3060] | 132 | } |
---|
| 133 | |
---|
[5899] | 134 | COUT(3) << "Sound: OpenAL ALUT: loading file " << source << std::endl; |
---|
[5695] | 135 | // Get DataStream from the resources |
---|
[5899] | 136 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source); |
---|
[5896] | 137 | if (fileInfo == NULL) |
---|
| 138 | { |
---|
[5899] | 139 | COUT(2) << "Warning: Sound file '" << source << "' not found" << std::endl; |
---|
[5896] | 140 | return; |
---|
[5695] | 141 | } |
---|
[5904] | 142 | dataStream_ = Resource::open(source); |
---|
[5695] | 143 | // Read everything into a temporary buffer |
---|
| 144 | char* buffer = new char[fileInfo->size]; |
---|
[5904] | 145 | dataStream_->read(buffer, fileInfo->size); |
---|
| 146 | dataStream_->seek(0); |
---|
[5695] | 147 | |
---|
[5899] | 148 | this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size); |
---|
[5695] | 149 | delete[] buffer; |
---|
| 150 | |
---|
[5899] | 151 | if (this->audioBuffer_ == AL_NONE) |
---|
[5896] | 152 | { |
---|
[3060] | 153 | COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
[5904] | 154 | if (source.find("ogg", 0) != std::string::npos) |
---|
| 155 | { |
---|
| 156 | COUT(2) << "Sound: Trying fallback ogg loader" << std::endl; |
---|
[5946] | 157 | this->audioBuffer_ = this->loadOggFile(); |
---|
[5904] | 158 | } |
---|
[3060] | 159 | |
---|
[5904] | 160 | if (this->audioBuffer_ == AL_NONE) |
---|
| 161 | { |
---|
| 162 | COUT(2) << "Sound: fallback ogg loader failed: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
| 163 | return; |
---|
| 164 | } |
---|
[3060] | 165 | } |
---|
| 166 | |
---|
[5899] | 167 | alGenSources(1, &this->audioSource_); |
---|
| 168 | alSourcei(this->audioSource_, AL_BUFFER, this->audioBuffer_); |
---|
[5896] | 169 | if (alGetError() != AL_NO_ERROR) |
---|
| 170 | { |
---|
[5899] | 171 | COUT(2) << "Sound: OpenAL: Error loading sample file: " << source << std::endl; |
---|
[5896] | 172 | return; |
---|
[3060] | 173 | } |
---|
[5896] | 174 | |
---|
[5899] | 175 | alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0); |
---|
[5896] | 176 | |
---|
| 177 | if (this->bPlayOnLoad_) |
---|
| 178 | this->play(); |
---|
[3060] | 179 | } |
---|
| 180 | |
---|
[5896] | 181 | ALint BaseSound::getSourceState() |
---|
| 182 | { |
---|
[3060] | 183 | ALint state; |
---|
[5899] | 184 | alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state); |
---|
[3060] | 185 | return state; |
---|
| 186 | } |
---|
| 187 | |
---|
[5904] | 188 | size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource) |
---|
[3060] | 189 | { |
---|
[5904] | 190 | return static_cast<Ogre::DataStream*>(datasource)->read(ptr, size * nmemb); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | int seekVorbis(void* datasource, ogg_int64_t offset, int whence) |
---|
| 194 | { |
---|
| 195 | Ogre::DataStream* stream = static_cast<Ogre::DataStream*>(datasource); |
---|
| 196 | int offset_beg = offset; |
---|
| 197 | if (whence == SEEK_CUR) |
---|
| 198 | offset_beg = stream->tell() + offset; |
---|
| 199 | else if (whence == SEEK_END) |
---|
| 200 | offset_beg = stream->size() + offset; |
---|
| 201 | else if (whence != SEEK_SET) |
---|
| 202 | return -1; |
---|
| 203 | stream->seek(offset_beg); |
---|
| 204 | return 0; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | long tellVorbis(void* datasource) |
---|
| 208 | { |
---|
| 209 | return static_cast<long>(static_cast<Ogre::DataStream*>(datasource)->tell()); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | ALuint BaseSound::loadOggFile() |
---|
| 213 | { |
---|
[3060] | 214 | char inbuffer[4096]; |
---|
| 215 | std::vector<char> outbuffer; |
---|
| 216 | OggVorbis_File vf; |
---|
[3075] | 217 | vorbis_info* vorbisInfo; |
---|
[3060] | 218 | int eof = false; |
---|
| 219 | int current_section; |
---|
[3075] | 220 | ALuint buffer; |
---|
| 221 | ALenum format; |
---|
[3060] | 222 | |
---|
[5904] | 223 | // Open file with custom streaming |
---|
| 224 | ov_callbacks vorbisCallbacks; |
---|
| 225 | vorbisCallbacks.read_func = &readVorbis; |
---|
| 226 | vorbisCallbacks.seek_func = &seekVorbis; |
---|
| 227 | vorbisCallbacks.tell_func = &tellVorbis; |
---|
| 228 | vorbisCallbacks.close_func = NULL; |
---|
[3060] | 229 | |
---|
[5904] | 230 | int ret = ov_open_callbacks(dataStream_.get(), &vf, NULL, 0, vorbisCallbacks); |
---|
| 231 | if (ret < 0) |
---|
[3060] | 232 | { |
---|
[3370] | 233 | COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl; |
---|
[3060] | 234 | ov_clear(&vf); |
---|
| 235 | return AL_NONE; |
---|
| 236 | } |
---|
| 237 | |
---|
[5896] | 238 | while (!eof) |
---|
[3060] | 239 | { |
---|
| 240 | long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); |
---|
| 241 | if (ret == 0) |
---|
| 242 | { |
---|
| 243 | eof = true; |
---|
| 244 | } |
---|
| 245 | else if (ret < 0) |
---|
| 246 | { |
---|
| 247 | COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; |
---|
| 248 | ov_clear(&vf); |
---|
| 249 | return AL_NONE; |
---|
| 250 | } |
---|
| 251 | else |
---|
| 252 | { |
---|
| 253 | outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + sizeof(inbuffer)); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
[3075] | 257 | vorbisInfo = ov_info(&vf, -1); |
---|
[5896] | 258 | if (vorbisInfo->channels == 1) |
---|
[3075] | 259 | format = AL_FORMAT_MONO16; |
---|
| 260 | else |
---|
| 261 | format = AL_FORMAT_STEREO16; |
---|
| 262 | |
---|
| 263 | alGenBuffers(1, &buffer); |
---|
| 264 | alBufferData(buffer, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate); |
---|
[3060] | 265 | ov_clear(&vf); |
---|
| 266 | |
---|
[3075] | 267 | return buffer; |
---|
[3060] | 268 | } |
---|
[5896] | 269 | |
---|
[3060] | 270 | } // namespace: orxonox |
---|