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 <AL/alut.h> |
---|
29 | #include <vorbis/vorbisfile.h> |
---|
30 | |
---|
31 | #include "orxonox/objects/worldentities/WorldEntity.h" |
---|
32 | #include "util/Math.h" |
---|
33 | #include "SoundBase.h" |
---|
34 | #include "SoundManager.h" |
---|
35 | |
---|
36 | namespace orxonox |
---|
37 | { |
---|
38 | SoundManager* SoundBase::soundmanager_s = NULL; |
---|
39 | |
---|
40 | SoundBase::SoundBase(WorldEntity* entity) |
---|
41 | { |
---|
42 | if(SoundBase::soundmanager_s == NULL) |
---|
43 | { |
---|
44 | SoundBase::soundmanager_s = new SoundManager(); |
---|
45 | } |
---|
46 | |
---|
47 | this->source_ = 0; |
---|
48 | this->buffer_ = 0; |
---|
49 | this->entity_ = entity; |
---|
50 | |
---|
51 | SoundBase::soundmanager_s->addSound(this); |
---|
52 | } |
---|
53 | |
---|
54 | SoundBase::~SoundBase() |
---|
55 | { |
---|
56 | alSourcei(this->source_, AL_BUFFER, 0); |
---|
57 | alDeleteSources(1, &this->source_); |
---|
58 | alDeleteBuffers(1, &this->buffer_); |
---|
59 | } |
---|
60 | |
---|
61 | void SoundBase::attachToEntity(WorldEntity* entity) |
---|
62 | { |
---|
63 | this->entity_ = entity; |
---|
64 | this->update(); |
---|
65 | } |
---|
66 | |
---|
67 | void SoundBase::update() { |
---|
68 | if(this->entity_ != NULL && alIsSource(this->source_)) { |
---|
69 | Vector3 pos = this->entity_->getPosition(); |
---|
70 | alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z); |
---|
71 | ALenum error = alGetError(); |
---|
72 | if(error == AL_INVALID_VALUE) |
---|
73 | COUT(2) << "OpenAL: Invalid sound position" << std::endl; |
---|
74 | |
---|
75 | Vector3 vel = this->entity_->getVelocity(); |
---|
76 | alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z); |
---|
77 | error = alGetError(); |
---|
78 | if(error == AL_INVALID_VALUE) |
---|
79 | COUT(2) << "OpenAL: Invalid sound position" << std::endl; |
---|
80 | |
---|
81 | Quaternion orient = this->entity_->getOrientation(); |
---|
82 | Vector3 at = orient.zAxis(); |
---|
83 | alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z); |
---|
84 | error = alGetError(); |
---|
85 | if(error == AL_INVALID_VALUE) |
---|
86 | COUT(2) << "OpenAL: Invalid sound position" << std::endl; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | void SoundBase::play(bool loop) { |
---|
91 | if(alIsSource(this->source_)) { |
---|
92 | if(loop) |
---|
93 | alSourcei(this->source_, AL_LOOPING, AL_TRUE); |
---|
94 | else |
---|
95 | alSourcei(this->source_, AL_LOOPING, AL_FALSE); |
---|
96 | alSourcePlay(this->source_); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | void SoundBase::stop() { |
---|
101 | if(alIsSource(this->source_)) { |
---|
102 | alSourceStop(this->source_); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | void SoundBase::pause() { |
---|
107 | if(alIsSource(this->source_)) { |
---|
108 | alSourcePause(this->source_); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | bool SoundBase::isPlaying() { |
---|
113 | if(alIsSource(this->source_)) { |
---|
114 | return getSourceState() == AL_PLAYING; |
---|
115 | } |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | bool SoundBase::isPaused() { |
---|
120 | if(alIsSource(this->source_)) { |
---|
121 | return getSourceState() == AL_PAUSED; |
---|
122 | } |
---|
123 | return true; |
---|
124 | } |
---|
125 | |
---|
126 | bool SoundBase::isStopped() { |
---|
127 | if(alIsSource(this->source_)) { |
---|
128 | return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED; |
---|
129 | } |
---|
130 | return true; |
---|
131 | } |
---|
132 | |
---|
133 | bool SoundBase::loadFile(std::string filename) { |
---|
134 | filename = Core::getMediaPathString() + "/audio/" + filename; |
---|
135 | COUT(3) << "OpenAL ALUT: loading file " << filename << std::endl; |
---|
136 | this->buffer_ = alutCreateBufferFromFile(filename.c_str()); |
---|
137 | if(this->buffer_ == AL_NONE) { |
---|
138 | COUT(2) << "OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
139 | if(filename.find("ogg", 0) != std::string::npos) |
---|
140 | { |
---|
141 | this->buffer_ = loadOggFile(filename); |
---|
142 | } |
---|
143 | |
---|
144 | if(this->buffer_ == AL_NONE) |
---|
145 | return false; |
---|
146 | } |
---|
147 | |
---|
148 | alGenSources(1, &this->source_); |
---|
149 | alSourcei(this->source_, AL_BUFFER, this->buffer_); |
---|
150 | if(alGetError() != AL_NO_ERROR) { |
---|
151 | COUT(2) << "OpenAL: Error loading sample file" << std::endl; |
---|
152 | return false; |
---|
153 | } |
---|
154 | return true; |
---|
155 | } |
---|
156 | |
---|
157 | ALint SoundBase::getSourceState() { |
---|
158 | ALint state; |
---|
159 | alGetSourcei(this->source_, AL_SOURCE_STATE, &state); |
---|
160 | return state; |
---|
161 | } |
---|
162 | |
---|
163 | ALuint SoundBase::loadOggFile(std::string filename) |
---|
164 | { |
---|
165 | // just a dummy |
---|
166 | return AL_NONE; |
---|
167 | } |
---|
168 | } // namespace: orxonox |
---|