| 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 "model/structures/instance.h" |
| 33 |
#include "util/log/logger.h" |
| 34 |
#include "util/base/exception.h" |
| 35 |
|
| 36 |
#include "actionaudio.h" |
| 37 |
#include "soundmanager.h" |
| 38 |
#include "soundemitter.h" |
| 39 |
#include "soundsource.h" |
| 40 |
|
| 41 |
namespace FIFE { |
| 42 |
static Logger _log(LM_AUDIO); |
| 43 |
|
| 44 |
class SoundChangeListener : public InstanceChangeListener { |
| 45 |
public: |
| 46 |
SoundChangeListener(SoundSource* source) { |
| 47 |
m_source = source; |
| 48 |
} |
| 49 |
virtual ~SoundChangeListener() {} |
| 50 |
|
| 51 |
virtual void onInstanceChanged(Instance* instance, InstanceChangeInfo info) { |
| 52 |
if ((info & ICHANGE_LOC) == ICHANGE_LOC) { |
| 53 |
m_source->setPosition(); |
| 54 |
} else if ((info & ICHANGE_ROTATION) == ICHANGE_ROTATION) { |
| 55 |
m_source->setDirection(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
private: |
| 60 |
SoundSource* m_source; |
| 61 |
}; |
| 62 |
|
| 63 |
SoundSource::SoundSource(Instance* instance) : |
| 64 |
m_instance(instance), |
| 65 |
m_audio(NULL) { |
| 66 |
|
| 67 |
m_emitter = SoundManager::instance()->createEmitter(); |
| 68 |
m_listener = new SoundChangeListener(this); |
| 69 |
m_instance->addChangeListener(m_listener); |
| 70 |
|
| 71 |
// inital data |
| 72 |
setPosition(); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
SoundSource::~SoundSource() { |
| 77 |
m_instance->removeChangeListener(m_listener); |
| 78 |
delete m_listener; |
| 79 |
SoundManager::instance()->releaseEmitter(m_emitter->getId()); |
| 80 |
} |
| 81 |
|
| 82 |
void SoundSource::setActionAudio(ActionAudio* audio) { |
| 83 |
if (audio != m_audio) { |
| 84 |
if (m_audio) { |
| 85 |
m_emitter->stop(); |
| 86 |
} |
| 87 |
|
| 88 |
m_audio = audio; |
| 89 |
if (m_audio) { |
| 90 |
updateSoundEmitter(); |
| 91 |
m_emitter->play(); |
| 92 |
} else { |
| 93 |
m_emitter->reset(); |
| 94 |
} |
| 95 |
} else if (audio && !m_emitter->isLooping()) { |
| 96 |
m_emitter->rewind(); |
| 97 |
m_emitter->play(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
ActionAudio* SoundSource::getActionAudio() const { |
| 102 |
return m_audio; |
| 103 |
} |
| 104 |
|
| 105 |
void SoundSource::setPosition() { |
| 106 |
if (m_audio) { |
| 107 |
m_emitter->setPosition(m_instance->getLocationRef().getMapCoordinates()); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void SoundSource::setDirection() { |
| 112 |
if (m_audio && m_audio->isDirection()) { |
| 113 |
m_emitter->setDirection(m_instance->getFacingLocation().getMapCoordinates()); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
void SoundSource::updateSoundEmitter() { |
| 118 |
m_emitter->setGain(m_audio->getGain()); |
| 119 |
m_emitter->setMaxGain(m_audio->getMaxGain()); |
| 120 |
m_emitter->setMinGain(m_audio->getMinGain()); |
| 121 |
m_emitter->setReferenceDistance(m_audio->getReferenceDistance()); |
| 122 |
m_emitter->setMaxDistance(m_audio->getMaxDistance()); |
| 123 |
m_emitter->setRolloff(m_audio->getRolloff()); |
| 124 |
m_emitter->setPitch(m_audio->getPitch()); |
| 125 |
m_emitter->setConeInnerAngle(m_audio->getConeInnerAngle()); |
| 126 |
m_emitter->setConeOuterAngle(m_audio->getConeOuterAngle()); |
| 127 |
m_emitter->setConeOuterGain(m_audio->getConeOuterGain()); |
| 128 |
m_emitter->setVelocity(m_audio->getVelocity()); |
| 129 |
m_emitter->setLooping(m_audio->isLooping()); |
| 130 |
m_emitter->setRelativePositioning(m_audio->isRelativePositioning()); |
| 131 |
m_emitter->setPosition(m_instance->getLocationRef().getMapCoordinates()); |
| 132 |
m_emitter->setSoundClip(m_audio->getSoundFileName()); |
| 133 |
m_emitter->setGroup(m_audio->getGroupName()); |
| 134 |
} |
| 135 |
|
| 136 |
} |