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_); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | void SoundBase::stop() { |
---|
103 | if(alIsSource(this->source_)) { |
---|
104 | alSourceStop(this->source_); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | void SoundBase::pause() { |
---|
109 | if(alIsSource(this->source_)) { |
---|
110 | alSourcePause(this->source_); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | bool SoundBase::isPlaying() { |
---|
115 | if(alIsSource(this->source_)) { |
---|
116 | return getSourceState() == AL_PLAYING; |
---|
117 | } |
---|
118 | return false; |
---|
119 | } |
---|
120 | |
---|
121 | bool SoundBase::isPaused() { |
---|
122 | if(alIsSource(this->source_)) { |
---|
123 | return getSourceState() == AL_PAUSED; |
---|
124 | } |
---|
125 | return true; |
---|
126 | } |
---|
127 | |
---|
128 | bool SoundBase::isStopped() { |
---|
129 | if(alIsSource(this->source_)) { |
---|
130 | return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED; |
---|
131 | } |
---|
132 | return true; |
---|
133 | } |
---|
134 | |
---|
135 | bool SoundBase::loadFile(std::string filename) { |
---|
136 | filename = Core::getMediaPathString() + "/audio/" + filename; |
---|
137 | |
---|
138 | if(!SoundBase::soundmanager_s->isSoundAvailable()) |
---|
139 | { |
---|
140 | COUT(3) << "Sound: not available, skipping " << filename << std::endl; |
---|
141 | return false; |
---|
142 | } |
---|
143 | |
---|
144 | COUT(3) << "Sound: OpenAL ALUT: loading file " << filename << std::endl; |
---|
145 | this->buffer_ = alutCreateBufferFromFile(filename.c_str()); |
---|
146 | if(this->buffer_ == AL_NONE) { |
---|
147 | COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
148 | if(filename.find("ogg", 0) != std::string::npos) |
---|
149 | { |
---|
150 | COUT(2) << "Sound: Trying fallback ogg loader" << std::endl; |
---|
151 | this->buffer_ = loadOggFile(filename); |
---|
152 | } |
---|
153 | |
---|
154 | if(this->buffer_ == AL_NONE) |
---|
155 | { |
---|
156 | COUT(2) << "Sound: fallback ogg loader failed: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
157 | return false; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | alGenSources(1, &this->source_); |
---|
162 | alSourcei(this->source_, AL_BUFFER, this->buffer_); |
---|
163 | // ALenum |
---|
164 | if(alGetError() != AL_NO_ERROR) { |
---|
165 | COUT(2) << "Sound: OpenAL: Error loading sample file: " << filename << std::endl; |
---|
166 | return false; |
---|
167 | } |
---|
168 | return true; |
---|
169 | } |
---|
170 | |
---|
171 | ALint SoundBase::getSourceState() { |
---|
172 | ALint state; |
---|
173 | alGetSourcei(this->source_, AL_SOURCE_STATE, &state); |
---|
174 | return state; |
---|
175 | } |
---|
176 | |
---|
177 | ALuint SoundBase::loadOggFile(std::string filename) |
---|
178 | { |
---|
179 | char inbuffer[4096]; |
---|
180 | std::vector<char> outbuffer; |
---|
181 | OggVorbis_File vf; |
---|
182 | vorbis_info* vorbisInfo; |
---|
183 | int eof = false; |
---|
184 | int current_section; |
---|
185 | ALuint buffer; |
---|
186 | ALenum format; |
---|
187 | |
---|
188 | FILE* f = fopen(filename.c_str(), "rb"); |
---|
189 | |
---|
190 | if(ov_open(f, &vf, NULL, 0) < 0) |
---|
191 | { |
---|
192 | COUT(2) << "Sound: libvorbisfile: File seems not to be an Ogg Vorbis bitstream" << std::endl; |
---|
193 | ov_clear(&vf); |
---|
194 | return AL_NONE; |
---|
195 | } |
---|
196 | |
---|
197 | while(!eof) |
---|
198 | { |
---|
199 | long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); |
---|
200 | if (ret == 0) |
---|
201 | { |
---|
202 | eof = true; |
---|
203 | } |
---|
204 | else if (ret < 0) |
---|
205 | { |
---|
206 | COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; |
---|
207 | ov_clear(&vf); |
---|
208 | return AL_NONE; |
---|
209 | } |
---|
210 | else |
---|
211 | { |
---|
212 | outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + sizeof(inbuffer)); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | vorbisInfo = ov_info(&vf, -1); |
---|
217 | if(vorbisInfo->channels == 1) |
---|
218 | format = AL_FORMAT_MONO16; |
---|
219 | else |
---|
220 | format = AL_FORMAT_STEREO16; |
---|
221 | |
---|
222 | alGenBuffers(1, &buffer); |
---|
223 | alBufferData(buffer, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate); |
---|
224 | ov_clear(&vf); |
---|
225 | |
---|
226 | return buffer; |
---|
227 | } |
---|
228 | } // namespace: orxonox |
---|