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