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 <cassert> |
---|
32 | #include <vector> |
---|
33 | #include <al.h> |
---|
34 | |
---|
35 | #include "core/CoreIncludes.h" |
---|
36 | #include "core/GameMode.h" |
---|
37 | #include "core/Resource.h" |
---|
38 | #include "core/XMLPort.h" |
---|
39 | #include "SoundBuffer.h" |
---|
40 | #include "SoundManager.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | BaseSound::BaseSound() |
---|
45 | : audioSource_(0) |
---|
46 | , bPooling_(false) |
---|
47 | , volume_(1.0) |
---|
48 | , bLoop_(false) |
---|
49 | , state_(Stopped) |
---|
50 | , pitch_ (1.0) |
---|
51 | { |
---|
52 | RegisterRootObject(BaseSound); |
---|
53 | |
---|
54 | if (GameMode::playsSound()) |
---|
55 | { |
---|
56 | alGenSources(1, &this->audioSource_); |
---|
57 | assert(this->audioSource_ != 0); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | BaseSound::~BaseSound() |
---|
62 | { |
---|
63 | this->setSource(""); |
---|
64 | if (GameMode::playsSound()) |
---|
65 | alDeleteSources(1, &this->audioSource_); |
---|
66 | } |
---|
67 | |
---|
68 | void BaseSound::XMLPortExtern(Element& xmlelement, XMLPort::Mode mode) |
---|
69 | { |
---|
70 | XMLPortParam(BaseSound, "volume", setVolume, getVolume, xmlelement, mode); |
---|
71 | XMLPortParam(BaseSound, "loop", setLooping, getLooping, xmlelement, mode); |
---|
72 | XMLPortParam(BaseSound, "play", setPlaying, isPlaying, xmlelement, mode); |
---|
73 | XMLPortParam(BaseSound, "source", setSource, getSource, xmlelement, mode); |
---|
74 | } |
---|
75 | |
---|
76 | void BaseSound::play() |
---|
77 | { |
---|
78 | this->state_ = Playing; |
---|
79 | if (GameMode::playsSound() && this->getSourceState() != AL_PLAYING) |
---|
80 | { |
---|
81 | alSourcePlay(this->audioSource_); |
---|
82 | |
---|
83 | if (int error = alGetError()) |
---|
84 | COUT(2) << "Sound: Error playing sound: " << error << std::endl; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | void BaseSound::stop() |
---|
89 | { |
---|
90 | this->state_ = Stopped; |
---|
91 | if (GameMode::playsSound()) |
---|
92 | alSourceStop(this->audioSource_); |
---|
93 | } |
---|
94 | |
---|
95 | void BaseSound::pause() |
---|
96 | { |
---|
97 | if (this->isStopped()) |
---|
98 | return; |
---|
99 | this->state_ = Paused; |
---|
100 | if (GameMode::playsSound()) |
---|
101 | alSourcePause(this->audioSource_); |
---|
102 | } |
---|
103 | |
---|
104 | ALint BaseSound::getSourceState() const |
---|
105 | { |
---|
106 | if (GameMode::playsSound()) |
---|
107 | { |
---|
108 | ALint state; |
---|
109 | alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state); |
---|
110 | return state; |
---|
111 | } |
---|
112 | else |
---|
113 | return AL_INITIAL; |
---|
114 | } |
---|
115 | |
---|
116 | void BaseSound::setVolume(float vol) |
---|
117 | { |
---|
118 | if (vol > 1 || vol < 0) |
---|
119 | { |
---|
120 | COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl; |
---|
121 | vol = vol > 1 ? 1 : vol; |
---|
122 | vol = vol < 0 ? 0 : vol; |
---|
123 | } |
---|
124 | this->volume_ = vol; |
---|
125 | |
---|
126 | this->updateVolume(); |
---|
127 | } |
---|
128 | |
---|
129 | float BaseSound::getVolumeGain() |
---|
130 | { |
---|
131 | return SoundManager::getInstance().getVolume(SoundType::none); |
---|
132 | } |
---|
133 | |
---|
134 | void BaseSound::updateVolume(void) |
---|
135 | { |
---|
136 | if (alIsSource(this->audioSource_)) |
---|
137 | { |
---|
138 | alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain()); |
---|
139 | if (int error = alGetError()) |
---|
140 | COUT(2) << "Sound: Error setting volume: " << error << std::endl; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | void BaseSound::setLooping(bool val) |
---|
145 | { |
---|
146 | this->bLoop_ = val; |
---|
147 | if (GameMode::playsSound()) |
---|
148 | alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE)); |
---|
149 | } |
---|
150 | |
---|
151 | void BaseSound::setPitch(float pitch) |
---|
152 | { |
---|
153 | if (pitch > 2 || pitch < 0.5) |
---|
154 | { |
---|
155 | COUT(2) << "Sound warning: pitch out of range, cropping value." << std::endl; |
---|
156 | pitch = pitch > 2 ? 2 : pitch; |
---|
157 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
158 | } |
---|
159 | this->pitch_ = pitch; |
---|
160 | if (GameMode::playsSound()) |
---|
161 | { |
---|
162 | if (int error = alGetError()) |
---|
163 | COUT(2) << "Sound: Error setting pitch: " << error << std::endl; |
---|
164 | alSourcef(this->audioSource_, AL_PITCH, pitch); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | void BaseSound::setSource(const std::string& source) |
---|
169 | { |
---|
170 | if (!GameMode::playsSound() || source == this->source_) |
---|
171 | { |
---|
172 | this->source_ = source; |
---|
173 | return; |
---|
174 | } |
---|
175 | |
---|
176 | if (this->soundBuffer_ != NULL) |
---|
177 | { |
---|
178 | alSourceStop(this->audioSource_); |
---|
179 | // Unload old sound first |
---|
180 | alSourcei(this->audioSource_, AL_BUFFER, 0); |
---|
181 | SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_); |
---|
182 | this->soundBuffer_.reset(); |
---|
183 | } |
---|
184 | |
---|
185 | this->source_ = source; |
---|
186 | if (source_.empty()) |
---|
187 | return; |
---|
188 | |
---|
189 | // Get DataStream from the resources |
---|
190 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source); |
---|
191 | if (fileInfo == NULL) |
---|
192 | { |
---|
193 | COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl; |
---|
194 | return; |
---|
195 | } |
---|
196 | |
---|
197 | this->soundBuffer_ = SoundManager::getInstance().getSoundBuffer(fileInfo); |
---|
198 | if (this->soundBuffer_ == NULL) |
---|
199 | return; |
---|
200 | |
---|
201 | alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer()); |
---|
202 | if (alGetError() != AL_NO_ERROR) |
---|
203 | { |
---|
204 | COUT(2) << "Sound: OpenAL: Error loading sample file: " << source << std::endl; |
---|
205 | return; |
---|
206 | } |
---|
207 | |
---|
208 | alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0); |
---|
209 | this->updateVolume(); |
---|
210 | this->setPitch(this->getPitch()); |
---|
211 | this->setLooping(getLooping()); |
---|
212 | if (this->isPlaying() || this->isPaused()) |
---|
213 | { |
---|
214 | alSourcePlay(this->audioSource_); |
---|
215 | if (int error = alGetError()) |
---|
216 | COUT(2) << "Sound: Error playing sound: " << error << std::endl; |
---|
217 | } |
---|
218 | if (this->isPaused()) |
---|
219 | alSourcePause(this->audioSource_); |
---|
220 | } |
---|
221 | } |
---|