2016-05-03 01:16:26 +00:00
|
|
|
#include "amuse/Emitter.hpp"
|
2019-08-25 10:28:43 +00:00
|
|
|
|
|
|
|
#include "amuse/Engine.hpp"
|
2017-09-19 03:59:20 +00:00
|
|
|
#include "amuse/Listener.hpp"
|
2016-05-06 05:19:19 +00:00
|
|
|
#include "amuse/Voice.hpp"
|
2016-05-03 01:16:26 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
namespace amuse {
|
2019-09-08 00:19:47 +00:00
|
|
|
static constexpr Vector3f Delta(const Vector3f& a, const Vector3f& b) {
|
|
|
|
return {a[0] - b[0], a[1] - b[1], a[2] - b[2]};
|
2017-09-19 03:59:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 00:19:47 +00:00
|
|
|
Emitter::~Emitter() = default;
|
2016-05-14 22:38:37 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
Emitter::Emitter(Engine& engine, const AudioGroup& group, ObjToken<Voice> vox, float maxDist, float minVol,
|
|
|
|
float falloff, bool doppler)
|
|
|
|
: Entity(engine, group, vox->getGroupId(), vox->getObjectId())
|
|
|
|
, m_vox(vox)
|
|
|
|
, m_maxDist(maxDist)
|
2019-09-07 10:21:07 +00:00
|
|
|
, m_minVol(std::clamp(minVol, 0.f, 1.f))
|
|
|
|
, m_falloff(std::clamp(falloff, -1.f, 1.f))
|
2018-12-08 05:20:09 +00:00
|
|
|
, m_doppler(doppler) {}
|
|
|
|
|
|
|
|
void Emitter::_destroy() {
|
|
|
|
Entity::_destroy();
|
|
|
|
m_vox->kill();
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
float Emitter::_attenuationCurve(float dist) const {
|
|
|
|
if (dist > m_maxDist)
|
|
|
|
return 0.f;
|
|
|
|
float t = dist / m_maxDist;
|
|
|
|
if (m_falloff >= 0.f) {
|
|
|
|
return 1.f - (m_falloff * t * t + (1.f - m_falloff) * t);
|
|
|
|
} else {
|
|
|
|
float omt = 1.f - t;
|
|
|
|
return 1.f - ((1.f + m_falloff) * t - (1.f - omt * omt) * m_falloff);
|
|
|
|
}
|
2016-05-14 06:33:21 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
void Emitter::_update() {
|
|
|
|
if (!m_dirty) {
|
|
|
|
/* Ensure that all listeners are also not dirty */
|
|
|
|
bool dirty = false;
|
|
|
|
for (auto& listener : m_engine.m_activeListeners) {
|
|
|
|
if (listener->m_dirty) {
|
|
|
|
dirty = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-19 03:59:20 +00:00
|
|
|
}
|
2018-12-08 05:20:09 +00:00
|
|
|
if (!dirty)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-08 00:19:47 +00:00
|
|
|
std::array<float, 8> coefs{};
|
2018-12-08 05:20:09 +00:00
|
|
|
double avgDopplerRatio = 0.0;
|
|
|
|
|
|
|
|
for (auto& listener : m_engine.m_activeListeners) {
|
2019-09-08 00:19:47 +00:00
|
|
|
const Vector3f listenerToEmitter = Delta(m_pos, listener->m_pos);
|
2019-09-07 10:21:07 +00:00
|
|
|
const float dist = Length(listenerToEmitter);
|
|
|
|
const float panDist = Dot(listenerToEmitter, listener->m_right);
|
|
|
|
const float frontPan = std::clamp(panDist / listener->m_frontDiff, -1.f, 1.f);
|
|
|
|
const float backPan = std::clamp(panDist / listener->m_backDiff, -1.f, 1.f);
|
|
|
|
const float spanDist = -Dot(listenerToEmitter, listener->m_heading);
|
|
|
|
const float span =
|
|
|
|
std::clamp(spanDist > 0.f ? spanDist / listener->m_backDiff : spanDist / listener->m_frontDiff, -1.f, 1.f);
|
2018-12-08 05:20:09 +00:00
|
|
|
|
|
|
|
/* Calculate attenuation */
|
|
|
|
float att = _attenuationCurve(dist);
|
|
|
|
att = (m_maxVol - m_minVol) * att + m_minVol;
|
2020-09-28 05:07:01 +00:00
|
|
|
att = m_attCache.getVolume(att, false);
|
2018-12-08 05:20:09 +00:00
|
|
|
if (att > FLT_EPSILON) {
|
|
|
|
/* Apply pan law */
|
2019-09-08 00:19:47 +00:00
|
|
|
const std::array<float, 8> thisCoefs = m_vox->_panLaw(frontPan, backPan, span);
|
2018-12-08 05:20:09 +00:00
|
|
|
|
|
|
|
/* Take maximum coefficient across listeners */
|
2019-09-08 00:19:47 +00:00
|
|
|
for (size_t i = 0; i < coefs.size(); ++i) {
|
2018-12-08 05:20:09 +00:00
|
|
|
coefs[i] = std::max(coefs[i], thisCoefs[i] * att * listener->m_volume);
|
2019-09-08 00:19:47 +00:00
|
|
|
}
|
2017-09-20 09:22:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
/* Calculate doppler */
|
|
|
|
if (m_doppler) {
|
|
|
|
/* Positive values indicate emitter and listener closing in */
|
2019-09-08 00:19:47 +00:00
|
|
|
const Vector3f dirDelta = Delta(m_dir, listener->m_dir);
|
|
|
|
const Vector3f posDelta = Normalize(Delta(listener->m_pos, m_pos));
|
|
|
|
const float deltaSpeed = Dot(dirDelta, posDelta);
|
|
|
|
if (listener->m_soundSpeed != 0.f) {
|
2018-12-08 05:20:09 +00:00
|
|
|
avgDopplerRatio += 1.0 + deltaSpeed / listener->m_soundSpeed;
|
2019-09-08 00:19:47 +00:00
|
|
|
} else {
|
2018-12-08 05:20:09 +00:00
|
|
|
avgDopplerRatio += 1.0;
|
2019-09-08 00:19:47 +00:00
|
|
|
}
|
2017-09-19 03:59:20 +00:00
|
|
|
}
|
2018-12-08 05:20:09 +00:00
|
|
|
}
|
2017-09-19 03:59:20 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
if (m_engine.m_activeListeners.size() != 0) {
|
|
|
|
m_vox->setChannelCoefs(coefs);
|
|
|
|
if (m_doppler) {
|
|
|
|
m_vox->m_dopplerRatio = avgDopplerRatio / float(m_engine.m_activeListeners.size());
|
|
|
|
m_vox->m_pitchDirty = true;
|
2017-09-19 03:59:20 +00:00
|
|
|
}
|
2018-12-08 05:20:09 +00:00
|
|
|
}
|
2017-09-20 09:22:46 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
m_dirty = false;
|
2017-09-19 03:59:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
void Emitter::setVectors(const float* pos, const float* dir) {
|
2019-09-08 00:19:47 +00:00
|
|
|
for (size_t i = 0; i < m_pos.size(); ++i) {
|
2020-09-27 21:36:18 +00:00
|
|
|
if (!std::isnan(pos[i])) {
|
|
|
|
m_pos[i] = pos[i];
|
|
|
|
} else {
|
|
|
|
m_pos[i] = 0.f;
|
|
|
|
}
|
|
|
|
if (!std::isnan(dir[i])) {
|
|
|
|
m_dir[i] = dir[i];
|
|
|
|
} else {
|
|
|
|
m_dir[i] = 0.f;
|
|
|
|
}
|
2018-12-08 05:20:09 +00:00
|
|
|
}
|
|
|
|
m_dirty = true;
|
2017-09-19 03:59:20 +00:00
|
|
|
}
|
2016-05-09 07:22:58 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
} // namespace amuse
|