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 | this->buffer_ = loadOggFile(filename); |
---|
151 | } |
---|
152 | |
---|
153 | if(this->buffer_ == AL_NONE) |
---|
154 | return false; |
---|
155 | } |
---|
156 | |
---|
157 | alGenSources(1, &this->source_); |
---|
158 | alSourcei(this->source_, AL_BUFFER, this->buffer_); |
---|
159 | if(alGetError() != AL_NO_ERROR) { |
---|
160 | COUT(2) << "Sound: OpenAL: Error loading sample file" << std::endl; |
---|
161 | return false; |
---|
162 | } |
---|
163 | return true; |
---|
164 | } |
---|
165 | |
---|
166 | ALint SoundBase::getSourceState() { |
---|
167 | ALint state; |
---|
168 | alGetSourcei(this->source_, AL_SOURCE_STATE, &state); |
---|
169 | return state; |
---|
170 | } |
---|
171 | |
---|
172 | ALuint SoundBase::loadOggFile(std::string filename) |
---|
173 | { |
---|
174 | COUT(2) << "Sound: Trying fallback ogg loader"; |
---|
175 | |
---|
176 | char inbuffer[4096]; |
---|
177 | std::vector<char> outbuffer; |
---|
178 | OggVorbis_File vf; |
---|
179 | int eof = false; |
---|
180 | int current_section; |
---|
181 | |
---|
182 | FILE* f = fopen(filename.c_str(), "rb"); |
---|
183 | |
---|
184 | if(ov_open(f, &vf, NULL, 0) < 0) |
---|
185 | { |
---|
186 | COUT(2) << "Sound: libvorbisfile: File seems not to be an Ogg Vorbis bitstream" << std::endl; |
---|
187 | ov_clear(&vf); |
---|
188 | return AL_NONE; |
---|
189 | } |
---|
190 | |
---|
191 | while(!eof) |
---|
192 | { |
---|
193 | long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); |
---|
194 | if (ret == 0) |
---|
195 | { |
---|
196 | eof = true; |
---|
197 | } |
---|
198 | else if (ret < 0) |
---|
199 | { |
---|
200 | COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; |
---|
201 | ov_clear(&vf); |
---|
202 | return AL_NONE; |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + sizeof(inbuffer)); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | ov_clear(&vf); |
---|
211 | |
---|
212 | return alutCreateBufferFromFileImage(&outbuffer, outbuffer.size()); |
---|
213 | } |
---|
214 | } // namespace: orxonox |
---|