Debian Codegraph @

engine/core/audio/soundmanager.h

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the FIFE team *
3 * http://www.fifengine.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #ifndef FIFE_SOUNDMANAGER_H
23 #define FIFE_SOUNDMANAGER_H
24
25 // Standard C++ library includes
26 #include <queue>
27
28 // Platform specific includes
29
30 // 3rd party library includes
31
32 // FIFE includes
33 // These includes are split up in two parts, separated by one empty line
34 // First block: files included from the FIFE root src directory
35 // Second block: files included from the same folder
36 #include "model/metamodel/modelcoords.h"
37 #include "util/base/singleton.h"
38
39 #include "fife_openal.h"
40 #include "soundconfig.h"
41
42 namespace FIFE {
43
44 /** Distance model from OpenAL
45 */
46 enum SoundDistanceModelType {
47 SD_DISTANCE_NONE,
48 SD_DISTANCE_INVERSE,
49 SD_DISTANCE_INVERSE_CLAMPED,
50 SD_DISTANCE_LINEAR,
51 SD_DISTANCE_LINEAR_CLAMPED,
52 SD_DISTANCE_EXPONENT,
53 SD_DISTANCE_EXPONENT_CLAMPED
54 };
55
56 /** SoundManager state
57 */
58 enum SoundManagerState {
59 SM_STATE_INACTIV,
60 SM_STATE_PLAY,
61 SM_STATE_PAUSE,
62 SM_STATE_STOP
63 };
64
65 class SoundEffectManager;
66 class SoundEffect;
67 class SoundFilter;
68 class SoundEmitter;
69
70 class SoundManager : public DynamicSingleton<SoundManager> {
71 public:
72
73 SoundManager();
74
75 ~SoundManager();
76
77 /** Initializes the audio system
78 */
79 void init();
80
81 /** Returns true if audio module is active
82 */
83 bool isActive() const;
84
85 /** Returns an openAL context
86 */
87 ALCcontext* getContext() const;
88
89 /** Sets the Master Volume
90 *
91 * @param vol The volume value. 0=silence ... 1.0=normal loudness.
92 */
93 void setVolume(float vol);
94
95 /** Return the Master Volume
96 */
97 float getVolume() const;
98
99 /** Mute
100 */
101 void mute();
102
103 /** Unmutes to volume before mute() was called.
104 */
105 void unmute();
106
107 /** Plays all SoundEmitters.
108 */
109 void play();
110
111 /** Pauses all SoundEmitters.
112 */
113 void pause();
114
115 /** Stops all SoundEmitters.
116 */
117 void stop();
118
119 /** Rewinds all SoundEmitters.
120 */
121 void rewind();
122
123 /** Sets the distance model.
124 */
125 void setDistanceModel(SoundDistanceModelType model);
126
127 /** Return the distance mode.
128 */
129 SoundDistanceModelType getDistanceModel() const;
130
131 /** Sets the position of the listener (alter ego).
132 */
133 void setListenerPosition(const AudioSpaceCoordinate& position);
134
135 /** Return the position of the listener (alter ego).
136 */
137 AudioSpaceCoordinate getListenerPosition() const;
138
139 /** Sets the orientation of the listener (alter ego).
140 */
141 void setListenerOrientation(const AudioSpaceCoordinate& orientation);
142
143 /** Return the orientation of the listener (alter ego).
144 */
145 AudioSpaceCoordinate getListenerOrientation() const;
146
147 /** Sets the velocity of the listener (alter ego).
148 */
149 void setListenerVelocity(const AudioSpaceCoordinate& velocity);
150
151 /** Return the velocity of the listener (alter ego).
152 */
153 AudioSpaceCoordinate getListenerVelocity() const;
154
155 /** Sets factor for doppler effect.
156 */
157 void setDopplerFactor(float factor);
158
159 /** Return factor for doppler effect.
160 */
161 float getDopplerFactor() const;
162
163 /** Sets the maximal listener distance.
164 * If it is larger the emitter turns off, or on if it is smaller.
165 */
166 void setListenerMaxDistance(float distance);
167
168 /** Return the maximal listener distance.
169 */
170 float getListenerMaxDistance() const;
171
172 /** Called once a frame and updates the sound objects.
173 */
174 void update();
175
176 /** Returns a pointer to an emitter-instance given by emitterId
177 *
178 * @param emitterId The id of the Emitter
179 *
180 */
181 SoundEmitter* getEmitter(uint32_t emitterId) const;
182
183 /** Returns a pointer to an allocated emitter-instance
184 */
185 SoundEmitter* createEmitter();
186
187 /** Returns a pointer to an allocated emitter-instance
188 *
189 * @param name The name of the SoundClip.
190 */
191 SoundEmitter* createEmitter(const std::string& name);
192
193 /** Release an emitter-instance given by emitter-id
194 *
195 * @param emitterId The id of the Emitter.
196 */
197 void releaseEmitter(uint32_t emitterId);
198
199 /** Release given emitter
200 *
201 * @param emitter The emitter-instance to destroy.
202 */
203 void deleteEmitter(SoundEmitter* emitter);
204
205 /** Release the source handle
206 *
207 * @param emitter The emitter-instance.
208 */
209 void releaseSource(SoundEmitter* emitter);
210
211 /** Creates SoundEffect of the specific type.
212 * @param type See SoundEffectType
213 */
214 SoundEffect* createSoundEffect(SoundEffectType type);
215
216 /** Creates EaxReverb SoundEffect and loads the specific preset type.
217 * @param type See SoundEffectPreset
218 */
219 SoundEffect* createSoundEffectPreset(SoundEffectPreset type);
220
221 /** Deletes given SoundEffect.
222 * Deactivates for SoundEmitters are automatically done.
223 */
224 void deleteSoundEffect(SoundEffect* effect);
225
226 /** Enables given SoundEffect.
227 */
228 void enableSoundEffect(SoundEffect* effect);
229
230 /** Disables given SoundEffect.
231 */
232 void disableSoundEffect(SoundEffect* effect);
233
234 /** Adds given SoundEmitter to the specific SoundEffect.
235 * Note: A SoundEmitter can only have a limited number of effects, usually 4.
236 */
237 void addEmitterToSoundEffect(SoundEffect* effect, SoundEmitter* emitter);
238
239 /** Removes given SoundEmitter from the specific SoundEffect.
240 */
241 void removeEmitterFromSoundEffect(SoundEffect* effect, SoundEmitter* emitter);
242
243 /** Adds given SoundFilter to the SoundEffect.
244 * Note: A SoundEffect can only have one SoundFilter.
245 */
246 void addSoundFilterToSoundEffect(SoundEffect* effect, SoundFilter* filter);
247
248 /** Removes given SoundFilter from the SoundEffect.
249 */
250 void removeSoundFilterFromSoundEffect(SoundEffect* effect, SoundFilter* filter);
251
252 /** Internal function to do the OpenAL calls to activate the SoundEffect for the SoundEmitter.
253 */
254 void activateEffect(SoundEffect* effect, SoundEmitter* emitter);
255
256 /** Internal function to do the OpenAL calls to deactivate the SoundEffect for the SoundEmitter.
257 */
258 void deactivateEffect(SoundEffect* effect, SoundEmitter* emitter);
259
260 /** Creates SoundFilter of the specific type.
261 * @param type See SoundFilterType
262 */
263 SoundFilter* createSoundFilter(SoundFilterType type);
264
265 /** Deletes given SoundFilter.
266 * Deactivates for SoundEmitters and Effects are automatically done.
267 */
268 void deleteSoundFilter(SoundFilter* filter);
269
270 /** Enables given direct SoundFilter. Only SoundFilters for SoundEmitters are meant.
271 */
272 void enableDirectSoundFilter(SoundFilter* filter);
273
274 /** Disables given SoundFilter. Only SoundFilters for SoundEmitters are meant.
275 */
276 void disableDirectSoundFilter(SoundFilter* filter);
277
278 /** Adds given SoundEmitter to the specific direct SoundFilter
279 * Note: A SoundEmitter can only have one direct filter.
280 */
281 void addEmitterToDirectSoundFilter(SoundFilter* filter, SoundEmitter* emitter);
282
283 /** Removes given SoundEmitter from the specific direct SoundFilter.
284 */
285 void removeEmitterFromDirectSoundFilter(SoundFilter* filter, SoundEmitter* emitter);
286
287 /** Internal function to do the OpenAL calls to activate the SoundFilter for the SoundEmitter.
288 */
289 void activateFilter(SoundFilter* filter, SoundEmitter* emitter);
290
291 /** Internal function to do the OpenAL calls to deactivate the SoundFilter for the SoundEmitter.
292 */
293 void deactivateFilter(SoundFilter* filter, SoundEmitter* emitter);
294
295 /** Adds the emitter to group.
296 * Called from the emitter after a setGroup() call.
297 *
298 * @param emitter The emitter-instance.
299 */
300 void addToGroup(SoundEmitter* emitter);
301
302 /** Removes the emitter from group.
303 * Called from the emitter if new group is different as the old.
304 *
305 * @param emitter The emitter-instance.
306 */
307 void removeFromGroup(SoundEmitter* emitter);
308
309 /** Remove group and resets the group for the affected emitters.
310 *
311 * @param group The group name.
312 */
313 void removeGroup(const std::string& group);
314
315 /** Remove all groups and resets the group of affected emitters.
316 */
317 void removeAllGroups();
318
319 /** Plays all emitters of the group.
320 *
321 * @param group The group name.
322 */
323 void play(const std::string& group);
324
325 /** Pauses all emitters of the group.
326 *
327 * @param group The group name.
328 */
329 void pause(const std::string& group);
330
331 /** Stops all emitters of the group.
332 *
333 * @param group The group name.
334 */
335 void stop(const std::string& group);
336
337 /** Rewinds all emitters of the group.
338 *
339 * @param group The group name.
340 */
341 void rewind(const std::string& group);
342
343 /** Sets gain for all emitters of the group.
344 *
345 * @param group The group name.
346 * @param gain The gain value.
347 */
348 void setGain(const std::string& group, float gain);
349
350 /** Sets max gain for all emitters of the group.
351 *
352 * @param group The group name.
353 * @param gain The max gain value.
354 */
355 void setMaxGain(const std::string& group, float gain);
356
357 /** Sets min gain for all emitters of the group.
358 *
359 * @param group The group name.
360 * @param gain The min gain value.
361 */
362 void setMinGain(const std::string& group, float gain);
363
364 private:
365 typedef std::map<std::string, std::vector<SoundEmitter*> > EmitterGroups;
366 typedef EmitterGroups::iterator EmitterGroupsIterator;
367
368 /** Sets the source handle
369 *
370 * @param emitter The Emitter pointer.
371 */
372 void setEmitterSource(SoundEmitter* emitter);
373
374 //! emitter-vector, holds all emitters
375 std::vector<SoundEmitter*> m_emitterVec;
376 //! OpenAL context
377 ALCcontext* m_context;
378 //! OpenAL device
379 ALCdevice* m_device;
380 //! volume before mute() was called
381 float m_muteVol;
382 //! volume to support setVolume-calls before initialization
383 float m_volume;
384 //! distance that removes a active Emitter
385 float m_maxDistance;
386 //! Selected distance model
387 SoundDistanceModelType m_distanceModel;
388 //! State of the SoundManager
389 SoundManagerState m_state;
390
391 //! Holds handles for sources
392 ALuint m_sources[MAX_SOURCES];
393 //! Maximal created sources, can be different to MAX_SOURCES
394 uint16_t m_createdSources;
395 //! Holds free handles for sources
396 std::queue<ALuint> m_freeSources;
397 //! Map that holds active Emitters together with the used source handle
398 std::map<SoundEmitter*, ALuint> m_activeEmitters;
399
400 SoundEffectManager* m_effectManager;
401
402 //! A map that holds the groups together with the appended emitters.
403 EmitterGroups m_groups;
404 };
405 }
406 #endif