Debian Codegraph @

engine/core/audio/soundmanager.cpp

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 // Standard C++ library includes
23
24 // Platform specific includes
25
26 // 3rd party library includes
27
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "audio/effects/soundeffectmanager.h"
33 #include "vfs/vfs.h"
34 #include "util/log/logger.h"
35 #include "util/base/exception.h"
36
37 #include "soundclipmanager.h"
38 #include "soundemitter.h"
39 #include "soundmanager.h"
40
41 namespace FIFE {
42 /** Logger to use for this source file.
43 * @relates Logger
44 */
45 static Logger _log(LM_AUDIO);
46
47 SoundManager::SoundManager() :
48 m_context(0),
49 m_device(0),
50 m_muteVol(0),
51 m_volume(1.0),
52 m_maxDistance(50.0),
53 m_distanceModel(SD_DISTANCE_INVERSE_CLAMPED),
54 m_state(SM_STATE_INACTIV),
55 m_sources(),
56 m_createdSources(0),
57 m_effectManager(NULL) {
58 }
59
60 SoundManager::~SoundManager() {
61 // delete all soundemitters
62 for (std::vector<SoundEmitter*>::iterator it = m_emitterVec.begin(); it != m_emitterVec.end(); ++it) {
63 if ((*it) != NULL) {
64 delete (*it);
65 }
66 }
67 m_emitterVec.clear();
68 // delete all sources
69 alDeleteSources(m_createdSources, m_sources);
70 // delete effect manager
71 delete m_effectManager;
72
73 if (m_device) {
74 alcDestroyContext(m_context);
75 alcCloseDevice(m_device);
76 m_device = NULL;
77 }
78
79 if (alcGetError(NULL) != ALC_NO_ERROR) {
80 FL_ERR(_log, LMsg() << "error closing openal device");
81 }
82 }
83
84 void SoundManager::init() {
85 m_device = alcOpenDevice(NULL);
86
87 if (!m_device || alcGetError(m_device) != ALC_NO_ERROR) {
88 FL_ERR(_log, LMsg() << "Could not open audio device - deactivating audio module");
89 m_device = NULL;
90 return;
91 }
92
93 m_context = alcCreateContext(m_device, NULL);
94 if (!m_context || alcGetError(m_device) != ALC_NO_ERROR) {
95 FL_ERR(_log, LMsg() << "Couldn't create audio context - deactivating audio module");
96 m_device = NULL;
97 return;
98 }
99
100 alcMakeContextCurrent(m_context);
101 if (alcGetError(m_device) != ALC_NO_ERROR) {
102 FL_ERR(_log, LMsg() << "Couldn't change current audio context - deactivating audio module");
103 m_device = NULL;
104 return;
105 }
106 // create and initialize the effect manager
107 m_effectManager = new SoundEffectManager();
108 m_effectManager->init(m_device);
109
110 // set listener position
111 alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
112 ALfloat vec1[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
113 alListenerfv(AL_ORIENTATION, vec1);
114
115 // set volume
116 alListenerf(AL_GAIN, m_volume);
117
118 // create max sources
119 for (uint16_t i = 0; i < MAX_SOURCES; i++) {
120 alGenSources(1, &m_sources[i]);
121 if (alGetError() != AL_NO_ERROR) {
122 break;
123 }
124
125 m_freeSources.push(m_sources[i]);
126 m_createdSources++;
127 }
128 m_state = SM_STATE_PLAY;
129 }
130
131 bool SoundManager::isActive() const {
132 return m_state != SM_STATE_INACTIV;
133 }
134
135 ALCcontext* SoundManager::getContext() const {
136 return m_context;
137 }
138
139 void SoundManager::setVolume(float vol) {
140 m_volume = vol;
141 m_muteVol = vol;
142 if (isActive()) {
143 alListenerf(AL_GAIN, vol);
144 }
145 }
146
147 float SoundManager::getVolume() const {
148 return m_volume;
149 }
150
151 void SoundManager::mute() {
152 if (isActive()) {
153 alGetListenerf(AL_GAIN, &m_muteVol);
154 alListenerf(AL_GAIN, 0);
155 }
156 }
157
158 void SoundManager::unmute() {
159 if (isActive()) {
160 alListenerf(AL_GAIN, m_muteVol);
161 }
162 }
163
164 void SoundManager::play() {
165 m_state = SM_STATE_PLAY;
166 for (std::vector<SoundEmitter*>::iterator it = m_emitterVec.begin(); it != m_emitterVec.end(); ++it) {
167 SoundEmitter* emitter = (*it);
168 if (!emitter) {
169 continue;
170 }
171 emitter->play();
172 }
173 }
174
175 void SoundManager::pause() {
176 m_state = SM_STATE_PAUSE;
177 for (std::vector<SoundEmitter*>::iterator it = m_emitterVec.begin(); it != m_emitterVec.end(); ++it) {
178 SoundEmitter* emitter = (*it);
179 if (!emitter) {
180 continue;
181 }
182 emitter->pause();
183 }
184 }
185
186 void SoundManager::stop() {
187 m_state = SM_STATE_STOP;
188 for (std::vector<SoundEmitter*>::iterator it = m_emitterVec.begin(); it != m_emitterVec.end(); ++it) {
189 SoundEmitter* emitter = (*it);
190 if (!emitter) {
191 continue;
192 }
193 emitter->stop();
194 }
195 }
196
197 void SoundManager::rewind() {
198 for (std::vector<SoundEmitter*>::iterator it = m_emitterVec.begin(); it != m_emitterVec.end(); ++it) {
199 SoundEmitter* emitter = (*it);
200 if (!emitter) {
201 continue;
202 }
203 emitter->rewind();
204 }
205 }
206
207 void SoundManager::setDistanceModel(SoundDistanceModelType model) {
208 m_distanceModel = model;
209 if (!isActive()) {
210 return;
211 }
212
213 switch (m_distanceModel) {
214 case SD_DISTANCE_NONE:
215 alDistanceModel(AL_NONE);
216 break;
217 case SD_DISTANCE_INVERSE:
218 alDistanceModel(AL_INVERSE_DISTANCE);
219 break;
220 case SD_DISTANCE_INVERSE_CLAMPED:
221 alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
222 break;
223 case SD_DISTANCE_LINEAR:
224 alDistanceModel(AL_LINEAR_DISTANCE);
225 break;
226 case SD_DISTANCE_LINEAR_CLAMPED:
227 alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);
228 break;
229 case SD_DISTANCE_EXPONENT:
230 alDistanceModel(AL_EXPONENT_DISTANCE);
231 break;
232 case SD_DISTANCE_EXPONENT_CLAMPED:
233 alDistanceModel(AL_EXPONENT_DISTANCE_CLAMPED);
234 break;
235 default:
236 break;
237 }
238 }
239
240 SoundDistanceModelType SoundManager::getDistanceModel() const {
241 return m_distanceModel;
242 }
243
244 void SoundManager::setListenerPosition(const AudioSpaceCoordinate& position) {
245 if (isActive()) {
246 alListener3f(AL_POSITION, static_cast<ALfloat>(position.x), static_cast<ALfloat>(position.y), static_cast<ALfloat>(position.z));
247 }
248 }
249
250 AudioSpaceCoordinate SoundManager::getListenerPosition() const {
251 if (isActive()) {
252 ALfloat vec[3];
253 alGetListenerfv(AL_POSITION, vec);
254 return AudioSpaceCoordinate(vec[0], vec[1], vec[2]);
255 }
256 return AudioSpaceCoordinate();
257 }
258
259 void SoundManager::setListenerOrientation(const AudioSpaceCoordinate& orientation) {
260 if (isActive()) {
261 ALfloat vec[6] = { static_cast<ALfloat>(orientation.x), static_cast<ALfloat>(orientation.y), static_cast<ALfloat>(orientation.z),
262 0.0, 0.0, 1.0 };
263 alListenerfv(AL_ORIENTATION, vec);
264 }
265 }
266
267 AudioSpaceCoordinate SoundManager::getListenerOrientation() const {
268 if (isActive()) {
269 ALfloat vec[6];
270 alGetListenerfv(AL_ORIENTATION, vec);
271 return AudioSpaceCoordinate(vec[0], vec[1], vec[2]);
272 }
273 return AudioSpaceCoordinate();
274 }
275
276 void SoundManager::setListenerVelocity(const AudioSpaceCoordinate& velocity) {
277 if (isActive()) {
278 alListener3f(AL_VELOCITY, static_cast<ALfloat>(velocity.x), static_cast<ALfloat>(velocity.y), static_cast<ALfloat>(velocity.z));
279 }
280 }
281
282 AudioSpaceCoordinate SoundManager::getListenerVelocity() const {
283 if (isActive()) {
284 ALfloat vec[3];
285 alGetListenerfv(AL_VELOCITY, vec);
286 return AudioSpaceCoordinate(vec[0], vec[1], vec[2]);
287 }
288 return AudioSpaceCoordinate();
289 }
290
291 void SoundManager::setDopplerFactor(float factor) {
292 if (isActive()) {
293 if (factor >= 0.0) {
294 alDopplerFactor(factor);
295 }
296 }
297 }
298
299 float SoundManager::getDopplerFactor() const {
300 if (isActive()) {
301 return alGetFloat(AL_DOPPLER_FACTOR);
302 }
303 return 0.0;
304 }
305
306 void SoundManager::setListenerMaxDistance(float distance) {
307 m_maxDistance = distance;
308 }
309
310 float SoundManager::getListenerMaxDistance() const {
311 return m_maxDistance;
312 }
313
314 void SoundManager::update() {
315 if (m_state != SM_STATE_PLAY) {
316 return;
317 }
318 AudioSpaceCoordinate listenerPos = getListenerPosition();
319 double maxDistance = static_cast<double>(m_maxDistance);
320
321 // first check emitters
322 for (std::vector<SoundEmitter*>::iterator it = m_emitterVec.begin(); it != m_emitterVec.end(); ++it) {
323 SoundEmitter* emitter = (*it);
324 if (!emitter) {
325 continue;
326 }
327 emitter->setCheckDifference();
328
329 bool active = emitter->isActive();
330 bool clip = emitter->getSoundClip();
331 bool plays = !emitter->isFinished();
332 // remove active without clip or stopped
333 if (!clip || !plays) {
334 if (active) {
335 emitter->update();
336 releaseSource(emitter);
337 }
338 continue;
339 }
340
341 bool inRange = true;
342 if (emitter->isPosition()) {
343 AudioSpaceCoordinate emitterPos = emitter->getPosition();
344 double rx = listenerPos.x - emitterPos.x;
345 double ry = listenerPos.y - emitterPos.y;
346 double rz = listenerPos.z - emitterPos.z;
347 inRange = maxDistance >= Mathd::Sqrt(rx*rx + ry*ry + rz*rz);
348 }
349 // remove active not in range
350 if (!inRange) {
351 if (active) {
352 releaseSource(emitter);
353 }
354 continue;
355 }
356 if (!active && !m_freeSources.empty()) {
357 setEmitterSource(emitter);
358 }
359 }
360 // then update active
361 for (std::map<SoundEmitter*, ALuint>::iterator it = m_activeEmitters.begin(); it != m_activeEmitters.end(); ++it) {
362 it->first->update();
363 }
364 }
365
366 SoundEmitter* SoundManager::getEmitter(uint32_t emitterId) const {
367 return m_emitterVec.at(emitterId);
368 }
369
370 SoundEmitter* SoundManager::createEmitter() {
371 SoundEmitter* ptr = NULL;
372 for (uint32_t i = 0; i < m_emitterVec.size(); i++) {
373 if (m_emitterVec.at(i) == NULL) {
374 ptr = new SoundEmitter(this, i);
375 m_emitterVec.at(i) = ptr;
376 break;
377 }
378 }
379 if (!ptr) {
380 ptr = new SoundEmitter(this, m_emitterVec.size());
381 m_emitterVec.push_back(ptr);
382 }
383 return ptr;
384 }
385
386 SoundEmitter* SoundManager::createEmitter(const std::string& name) {
387 SoundEmitter* emitter = createEmitter();
388 emitter->setSoundClip(SoundClipManager::instance()->get(name));
389 return emitter;
390 }
391
392 void SoundManager::releaseEmitter(uint32_t emitterId) {
393 SoundEmitter** ptr = &m_emitterVec.at(emitterId);
394 if ((*ptr)->isActive()) {
395 releaseSource(*ptr);
396 }
397 delete *ptr;
398 *ptr = NULL;
399 }
400
401 void SoundManager::deleteEmitter(SoundEmitter* emitter) {
402 releaseEmitter(emitter->getId());
403 }
404
405 void SoundManager::setEmitterSource(SoundEmitter* emitter) {
406 std::pair<std::map<SoundEmitter*, ALuint>::iterator, bool> ret;
407 ret = m_activeEmitters.insert(std::pair<SoundEmitter*, ALuint>(emitter, m_freeSources.front()));
408 if (ret.second == false) {
409 FL_WARN(_log, LMsg() << "SoundEmitter already have an source handler");
410 }
411 emitter->setSource(m_freeSources.front());
412 m_freeSources.pop();
413 }
414
415 void SoundManager::releaseSource(SoundEmitter* emitter) {
416 if (emitter->isActive()) {
417 std::map<SoundEmitter*, ALuint>::iterator it = m_activeEmitters.find(emitter);
418 if (it != m_activeEmitters.end()) {
419 m_freeSources.push(it->second);
420 m_activeEmitters.erase(it);
421 emitter->setSource(0);
422 } else {
423 FL_WARN(_log, LMsg() << "SoundEmitter can not release source handler");
424 }
425 }
426 }
427
428 SoundEffect* SoundManager::createSoundEffect(SoundEffectType type) {
429 return m_effectManager->createSoundEffect(type);
430 }
431
432 SoundEffect* SoundManager::createSoundEffectPreset(SoundEffectPreset type) {
433 return m_effectManager->createSoundEffectPreset(type);
434 }
435
436 void SoundManager::deleteSoundEffect(SoundEffect* effect) {
437 m_effectManager->deleteSoundEffect(effect);
438 }
439
440 void SoundManager::enableSoundEffect(SoundEffect* effect) {
441 m_effectManager->enableSoundEffect(effect);
442 }
443
444 void SoundManager::disableSoundEffect(SoundEffect* effect) {
445 m_effectManager->disableSoundEffect(effect);
446 }
447
448 void SoundManager::addEmitterToSoundEffect(SoundEffect* effect, SoundEmitter* emitter) {
449 m_effectManager->addEmitterToSoundEffect(effect, emitter);
450 }
451
452 void SoundManager::removeEmitterFromSoundEffect(SoundEffect* effect, SoundEmitter* emitter) {
453 m_effectManager->removeEmitterFromSoundEffect(effect, emitter);
454 }
455
456 void SoundManager::addSoundFilterToSoundEffect(SoundEffect* effect, SoundFilter* filter) {
457 m_effectManager->addSoundFilterToSoundEffect(effect, filter);
458 }
459
460 void SoundManager::removeSoundFilterFromSoundEffect(SoundEffect* effect, SoundFilter* filter) {
461 m_effectManager->removeSoundFilterFromSoundEffect(effect, filter);
462 }
463
464 void SoundManager::activateEffect(SoundEffect* effect, SoundEmitter* emitter) {
465 m_effectManager->activateEffect(effect, emitter);
466 }
467
468 void SoundManager::deactivateEffect(SoundEffect* effect, SoundEmitter* emitter) {
469 m_effectManager->deactivateEffect(effect, emitter);
470 }
471
472 SoundFilter* SoundManager::createSoundFilter(SoundFilterType type) {
473 return m_effectManager->createSoundFilter(type);
474 }
475
476 void SoundManager::deleteSoundFilter(SoundFilter* filter) {
477 m_effectManager->deleteSoundFilter(filter);
478 }
479
480 void SoundManager::enableDirectSoundFilter(SoundFilter* filter) {
481 m_effectManager->enableDirectSoundFilter(filter);
482 }
483
484 void SoundManager::disableDirectSoundFilter(SoundFilter* filter) {
485 m_effectManager->disableDirectSoundFilter(filter);
486 }
487
488 void SoundManager::addEmitterToDirectSoundFilter(SoundFilter* filter, SoundEmitter* emitter) {
489 m_effectManager->addEmitterToDirectSoundFilter(filter, emitter);
490 }
491
492 void SoundManager::removeEmitterFromDirectSoundFilter(SoundFilter* filter, SoundEmitter* emitter) {
493 m_effectManager->removeEmitterFromDirectSoundFilter(filter, emitter);
494 }
495
496 void SoundManager::activateFilter(SoundFilter* filter, SoundEmitter* emitter) {
497 m_effectManager->activateFilter(filter, emitter);
498 }
499
500 void SoundManager::deactivateFilter(SoundFilter* filter, SoundEmitter* emitter) {
501 m_effectManager->deactivateFilter(filter, emitter);
502 }
503
504 void SoundManager::addToGroup(SoundEmitter* emitter) {
505 if (emitter->getGroup() != "") {
506 m_groups[emitter->getGroup()].push_back(emitter);
507 }
508 }
509
510 void SoundManager::removeFromGroup(SoundEmitter* emitter) {
511 std::string group = emitter->getGroup();
512 if (group == "") {
513 return;
514 }
515 EmitterGroupsIterator groupIt = m_groups.find(group);
516 if (groupIt == m_groups.end()) {
517 FL_WARN(_log, LMsg() << "SoundEmitter can not removed from unknown group");
518 return;
519 }
520 bool found = false;
521 std::vector<SoundEmitter*>::iterator emitterIt = groupIt->second.begin();
522 std::vector<SoundEmitter*>::iterator emitterEnd = groupIt->second.end();
523 while (emitterIt != emitterEnd) {
524 if ((*emitterIt) == emitter) {
525 groupIt->second.erase(emitterIt++);
526 found = true;
527 } else {
528 ++emitterIt;
529 }
530 }
531 if (!found) {
532 FL_WARN(_log, LMsg() << "SoundEmitter could not be found in the given group.");
533 return;
534 }
535 }
536
537 void SoundManager::removeGroup(const std::string& group) {
538 if (group == "") {
539 return;
540 }
541 EmitterGroupsIterator groupIt = m_groups.find(group);
542 if (groupIt == m_groups.end()) {
543 FL_WARN(_log, LMsg() << "SoundEmitter can not remove unknown group");
544 return;
545 }
546 std::vector<SoundEmitter*> emitters = groupIt->second;
547 for (std::vector<SoundEmitter*>::iterator it = emitters.begin(); it != emitters.end(); ++it) {
548 (*it)->setGroup("");
549 }
550 m_groups.erase(group);
551 }
552
553 void SoundManager::removeAllGroups() {
554 std::vector<std::string> groups;
555 for (EmitterGroupsIterator it = m_groups.begin(); it != m_groups.end(); ++it) {
556 groups.push_back(it->first);
557 }
558 for (std::vector<std::string>::iterator it = groups.begin(); it != groups.end(); ++it) {
559 removeGroup(*it);
560 }
561 m_groups.clear();
562 }
563
564 void SoundManager::play(const std::string& group) {
565 EmitterGroupsIterator groupIt = m_groups.find(group);
566 if (groupIt == m_groups.end()) {
567 FL_WARN(_log, LMsg() << "Unknown group can not played");
568 return;
569 }
570 std::vector<SoundEmitter*>::iterator emitterIt = groupIt->second.begin();
571 for (; emitterIt != groupIt->second.end(); ++emitterIt) {
572 (*emitterIt)->play();
573 }
574 }
575
576 void SoundManager::pause(const std::string& group) {
577 EmitterGroupsIterator groupIt = m_groups.find(group);
578 if (groupIt == m_groups.end()) {
579 FL_WARN(_log, LMsg() << "Unknown group can not paused");
580 return;
581 }
582 std::vector<SoundEmitter*>::iterator emitterIt = groupIt->second.begin();
583 for (; emitterIt != groupIt->second.end(); ++emitterIt) {
584 (*emitterIt)->pause();
585 }
586 }
587
588 void SoundManager::stop(const std::string& group) {
589 EmitterGroupsIterator groupIt = m_groups.find(group);
590 if (groupIt == m_groups.end()) {
591 FL_WARN(_log, LMsg() << "Unknown group can not stopped");
592 return;
593 }
594 std::vector<SoundEmitter*>::iterator emitterIt = groupIt->second.begin();
595 for (; emitterIt != groupIt->second.end(); ++emitterIt) {
596 (*emitterIt)->stop();
597 }
598 }
599
600 void SoundManager::rewind(const std::string& group) {
601 EmitterGroupsIterator groupIt = m_groups.find(group);
602 if (groupIt == m_groups.end()) {
603 FL_WARN(_log, LMsg() << "Unknown group can not rewinded");
604 return;
605 }
606 std::vector<SoundEmitter*>::iterator emitterIt = groupIt->second.begin();
607 for (; emitterIt != groupIt->second.end(); ++emitterIt) {
608 (*emitterIt)->rewind();
609 }
610 }
611
612 void SoundManager::setGain(const std::string& group, float gain) {
613 EmitterGroupsIterator groupIt = m_groups.find(group);
614 if (groupIt == m_groups.end()) {
615 FL_WARN(_log, LMsg() << "Unknown group can not set gain");
616 return;
617 }
618 std::vector<SoundEmitter*>::iterator emitterIt = groupIt->second.begin();
619 for (; emitterIt != groupIt->second.end(); ++emitterIt) {
620 (*emitterIt)->setGain(gain);
621 }
622 }
623
624 void SoundManager::setMaxGain(const std::string& group, float gain) {
625 EmitterGroupsIterator groupIt = m_groups.find(group);
626 if (groupIt == m_groups.end()) {
627 FL_WARN(_log, LMsg() << "Unknown group can not set max gain");
628 return;
629 }
630 std::vector<SoundEmitter*>::iterator emitterIt = groupIt->second.begin();
631 for (; emitterIt != groupIt->second.end(); ++emitterIt) {
632 (*emitterIt)->setMaxGain(gain);
633 }
634 }
635
636 void SoundManager::setMinGain(const std::string& group, float gain) {
637 EmitterGroupsIterator groupIt = m_groups.find(group);
638 if (groupIt == m_groups.end()) {
639 FL_WARN(_log, LMsg() << "Unknown group can not set min gain");
640 return;
641 }
642 std::vector<SoundEmitter*>::iterator emitterIt = groupIt->second.begin();
643 for (; emitterIt != groupIt->second.end(); ++emitterIt) {
644 (*emitterIt)->setMinGain(gain);
645 }
646 }
647
648 } //FIFE