[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: |
---|
| 23 | * Erwin 'vaiursch' Herrsche |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | #include <vector> |
---|
| 29 | #include <AL/alut.h> |
---|
| 30 | #include <vorbis/vorbisfile.h> |
---|
| 31 | |
---|
| 32 | #include "orxonox/objects/worldentities/WorldEntity.h" |
---|
| 33 | #include "util/Math.h" |
---|
| 34 | #include "SoundBase.h" |
---|
| 35 | #include "SoundManager.h" |
---|
| 36 | #include "core/Core.h" |
---|
| 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
| 40 | SoundManager* SoundBase::soundmanager_s = NULL; |
---|
| 41 | |
---|
| 42 | SoundBase::SoundBase(WorldEntity* entity) |
---|
| 43 | { |
---|
| 44 | if(SoundBase::soundmanager_s == NULL) |
---|
| 45 | { |
---|
| 46 | SoundBase::soundmanager_s = new SoundManager(); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | this->source_ = 0; |
---|
| 50 | this->buffer_ = 0; |
---|
| 51 | this->entity_ = entity; |
---|
| 52 | |
---|
| 53 | SoundBase::soundmanager_s->addSound(this); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | SoundBase::~SoundBase() |
---|
| 57 | { |
---|
| 58 | alSourcei(this->source_, AL_BUFFER, 0); |
---|
| 59 | alDeleteSources(1, &this->source_); |
---|
| 60 | alDeleteBuffers(1, &this->buffer_); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void SoundBase::attachToEntity(WorldEntity* entity) |
---|
| 64 | { |
---|
| 65 | this->entity_ = entity; |
---|
| 66 | this->update(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void SoundBase::update() { |
---|
| 70 | if(this->entity_ != NULL && alIsSource(this->source_)) { |
---|
| 71 | Vector3 pos = this->entity_->getPosition(); |
---|
| 72 | alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z); |
---|
| 73 | ALenum error = alGetError(); |
---|
| 74 | if(error == AL_INVALID_VALUE) |
---|
| 75 | COUT(2) << "Sound: OpenAL: Invalid sound position" << std::endl; |
---|
| 76 | |
---|
| 77 | Vector3 vel = this->entity_->getVelocity(); |
---|
| 78 | alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z); |
---|
| 79 | error = alGetError(); |
---|
| 80 | if(error == AL_INVALID_VALUE) |
---|
| 81 | COUT(2) << "Sound: OpenAL: Invalid sound velocity" << std::endl; |
---|
| 82 | |
---|
| 83 | Quaternion orient = this->entity_->getOrientation(); |
---|
| 84 | Vector3 at = orient.zAxis(); |
---|
| 85 | alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z); |
---|
| 86 | error = alGetError(); |
---|
| 87 | if(error == AL_INVALID_VALUE) |
---|
| 88 | COUT(2) << "Sound: OpenAL: Invalid sound direction" << std::endl; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void SoundBase::play(bool loop) { |
---|
| 93 | if(alIsSource(this->source_)) { |
---|
| 94 | if(loop) |
---|
| 95 | alSourcei(this->source_, AL_LOOPING, AL_TRUE); |
---|
| 96 | else |
---|
| 97 | alSourcei(this->source_, AL_LOOPING, AL_FALSE); |
---|
| 98 | alSourcePlay(this->source_); |
---|
[3108] | 99 | |
---|
| 100 | if(alGetError() != AL_NO_ERROR) |
---|
| 101 | { |
---|
| 102 | COUT(2) << "Sound: OpenAL: Error playin sound " << this->source_ << std::endl; |
---|
| 103 | } |
---|
[3060] | 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void SoundBase::stop() { |
---|
| 108 | if(alIsSource(this->source_)) { |
---|
| 109 | alSourceStop(this->source_); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | void SoundBase::pause() { |
---|
| 114 | if(alIsSource(this->source_)) { |
---|
| 115 | alSourcePause(this->source_); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | bool SoundBase::isPlaying() { |
---|
| 120 | if(alIsSource(this->source_)) { |
---|
| 121 | return getSourceState() == AL_PLAYING; |
---|
| 122 | } |
---|
| 123 | return false; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | bool SoundBase::isPaused() { |
---|
| 127 | if(alIsSource(this->source_)) { |
---|
| 128 | return getSourceState() == AL_PAUSED; |
---|
| 129 | } |
---|
| 130 | return true; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | bool SoundBase::isStopped() { |
---|
| 134 | if(alIsSource(this->source_)) { |
---|
| 135 | return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED; |
---|
| 136 | } |
---|
| 137 | return true; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | bool SoundBase::loadFile(std::string filename) { |
---|
| 141 | filename = Core::getMediaPathString() + "/audio/" + filename; |
---|
| 142 | |
---|
| 143 | if(!SoundBase::soundmanager_s->isSoundAvailable()) |
---|
| 144 | { |
---|
| 145 | COUT(3) << "Sound: not available, skipping " << filename << std::endl; |
---|
| 146 | return false; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | COUT(3) << "Sound: OpenAL ALUT: loading file " << filename << std::endl; |
---|
| 150 | this->buffer_ = alutCreateBufferFromFile(filename.c_str()); |
---|
| 151 | if(this->buffer_ == AL_NONE) { |
---|
| 152 | COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
| 153 | if(filename.find("ogg", 0) != std::string::npos) |
---|
| 154 | { |
---|
| 155 | COUT(2) << "Sound: Trying fallback ogg loader" << std::endl; |
---|
| 156 | this->buffer_ = loadOggFile(filename); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | if(this->buffer_ == AL_NONE) |
---|
| 160 | { |
---|
| 161 | COUT(2) << "Sound: fallback ogg loader failed: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
| 162 | return false; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | alGenSources(1, &this->source_); |
---|
| 167 | alSourcei(this->source_, AL_BUFFER, this->buffer_); |
---|
| 168 | if(alGetError() != AL_NO_ERROR) { |
---|
| 169 | COUT(2) << "Sound: OpenAL: Error loading sample file: " << filename << std::endl; |
---|
| 170 | return false; |
---|
| 171 | } |
---|
| 172 | return true; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | ALint SoundBase::getSourceState() { |
---|
| 176 | ALint state; |
---|
| 177 | alGetSourcei(this->source_, AL_SOURCE_STATE, &state); |
---|
| 178 | return state; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | ALuint SoundBase::loadOggFile(std::string filename) |
---|
| 182 | { |
---|
| 183 | char inbuffer[4096]; |
---|
| 184 | std::vector<char> outbuffer; |
---|
| 185 | OggVorbis_File vf; |
---|
[3075] | 186 | vorbis_info* vorbisInfo; |
---|
[3060] | 187 | int eof = false; |
---|
| 188 | int current_section; |
---|
[3075] | 189 | ALuint buffer; |
---|
| 190 | ALenum format; |
---|
[3060] | 191 | |
---|
| 192 | FILE* f = fopen(filename.c_str(), "rb"); |
---|
| 193 | |
---|
| 194 | if(ov_open(f, &vf, NULL, 0) < 0) |
---|
| 195 | { |
---|
| 196 | COUT(2) << "Sound: libvorbisfile: File seems not to be an Ogg Vorbis bitstream" << std::endl; |
---|
| 197 | ov_clear(&vf); |
---|
| 198 | return AL_NONE; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | while(!eof) |
---|
| 202 | { |
---|
| 203 | long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); |
---|
| 204 | if (ret == 0) |
---|
| 205 | { |
---|
| 206 | eof = true; |
---|
| 207 | } |
---|
| 208 | else if (ret < 0) |
---|
| 209 | { |
---|
| 210 | COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; |
---|
| 211 | ov_clear(&vf); |
---|
| 212 | return AL_NONE; |
---|
| 213 | } |
---|
| 214 | else |
---|
| 215 | { |
---|
| 216 | outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + sizeof(inbuffer)); |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
[3075] | 220 | vorbisInfo = ov_info(&vf, -1); |
---|
| 221 | if(vorbisInfo->channels == 1) |
---|
| 222 | format = AL_FORMAT_MONO16; |
---|
| 223 | else |
---|
| 224 | format = AL_FORMAT_STEREO16; |
---|
| 225 | |
---|
| 226 | alGenBuffers(1, &buffer); |
---|
| 227 | alBufferData(buffer, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate); |
---|
[3060] | 228 | ov_clear(&vf); |
---|
| 229 | |
---|
[3075] | 230 | return buffer; |
---|
[3060] | 231 | } |
---|
| 232 | } // namespace: orxonox |
---|