2016-05-03 01:16:26 +00:00
|
|
|
#include "amuse/Engine.hpp"
|
|
|
|
#include "amuse/Voice.hpp"
|
2016-05-07 22:10:57 +00:00
|
|
|
#include "amuse/Submix.hpp"
|
2016-05-03 01:16:26 +00:00
|
|
|
#include "amuse/Sequencer.hpp"
|
|
|
|
#include "amuse/IBackendVoice.hpp"
|
|
|
|
#include "amuse/IBackendVoiceAllocator.hpp"
|
|
|
|
#include "amuse/AudioGroupData.hpp"
|
|
|
|
#include "amuse/AudioGroup.hpp"
|
2016-05-11 04:48:08 +00:00
|
|
|
#include "amuse/Common.hpp"
|
2016-05-03 01:16:26 +00:00
|
|
|
|
|
|
|
namespace amuse
|
|
|
|
{
|
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
static const float FullLevels[8] = {1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f};
|
|
|
|
|
2016-05-16 22:13:24 +00:00
|
|
|
Engine::~Engine()
|
|
|
|
{
|
2017-02-15 06:01:39 +00:00
|
|
|
m_backend.setCallbackInterface(nullptr);
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Sequencer>& seq : m_activeSequencers)
|
2016-05-31 21:40:24 +00:00
|
|
|
if (!seq->m_destroyed)
|
|
|
|
seq->_destroy();
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Emitter>& emitter : m_activeEmitters)
|
2016-05-16 22:13:24 +00:00
|
|
|
emitter->_destroy();
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Voice>& vox : m_activeVoices)
|
2016-05-16 22:13:24 +00:00
|
|
|
vox->_destroy();
|
|
|
|
}
|
2016-05-14 22:38:37 +00:00
|
|
|
|
2016-05-22 08:35:55 +00:00
|
|
|
Engine::Engine(IBackendVoiceAllocator& backend, AmplitudeMode ampMode)
|
2016-07-14 04:54:46 +00:00
|
|
|
: m_backend(backend), m_ampMode(ampMode), m_defaultStudio(_allocateStudio(true))
|
2016-05-19 10:12:32 +00:00
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
m_defaultStudio->getAuxA().makeReverbStd(0.5f, 0.8f, 3.0f, 0.5f, 0.1f);
|
|
|
|
m_defaultStudio->getAuxB().makeChorus(15, 0, 500);
|
|
|
|
m_defaultStudioReady = true;
|
2017-02-15 06:01:39 +00:00
|
|
|
m_backend.setCallbackInterface(this);
|
2016-05-20 06:17:02 +00:00
|
|
|
m_midiReader = backend.allocateMIDIReader(*this);
|
2016-05-19 10:12:32 +00:00
|
|
|
}
|
2016-05-03 01:16:26 +00:00
|
|
|
|
2016-05-15 21:56:23 +00:00
|
|
|
std::pair<AudioGroup*, const SongGroupIndex*> Engine::_findSongGroup(int groupId) const
|
|
|
|
{
|
|
|
|
for (const auto& pair : m_audioGroups)
|
|
|
|
{
|
|
|
|
const SongGroupIndex* ret = pair.second->getProj().getSongGroupIndex(groupId);
|
|
|
|
if (ret)
|
|
|
|
return {pair.second.get(), ret};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<AudioGroup*, const SFXGroupIndex*> Engine::_findSFXGroup(int groupId) const
|
|
|
|
{
|
|
|
|
for (const auto& pair : m_audioGroups)
|
|
|
|
{
|
|
|
|
const SFXGroupIndex* ret = pair.second->getProj().getSFXGroupIndex(groupId);
|
|
|
|
if (ret)
|
|
|
|
return {pair.second.get(), ret};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
std::list<ObjToken<Voice>>::iterator Engine::_allocateVoice(const AudioGroup& group, int groupId,
|
|
|
|
double sampleRate, bool dynamicPitch, bool emitter,
|
|
|
|
ObjToken<Studio> studio)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
auto it =
|
2018-07-29 03:37:06 +00:00
|
|
|
m_activeVoices.emplace(m_activeVoices.end(), MakeObj<Voice>(*this, group, groupId, m_nextVid++, emitter, studio));
|
2016-07-14 04:54:46 +00:00
|
|
|
m_activeVoices.back()->m_backendVoice = m_backend.allocateVoice(*m_activeVoices.back(), sampleRate, dynamicPitch);
|
2018-07-29 03:37:06 +00:00
|
|
|
m_activeVoices.back()->m_backendVoice->setChannelLevels(studio->getMaster().m_backendSubmix.get(), FullLevels, false);
|
|
|
|
m_activeVoices.back()->m_backendVoice->setChannelLevels(studio->getAuxA().m_backendSubmix.get(), FullLevels, false);
|
|
|
|
m_activeVoices.back()->m_backendVoice->setChannelLevels(studio->getAuxB().m_backendSubmix.get(), FullLevels, false);
|
2016-05-31 20:40:51 +00:00
|
|
|
return it;
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
std::list<ObjToken<Sequencer>>::iterator Engine::_allocateSequencer(const AudioGroup& group, int groupId,
|
|
|
|
int setupId, ObjToken<Studio> studio)
|
2016-05-15 21:56:23 +00:00
|
|
|
{
|
|
|
|
const SongGroupIndex* songGroup = group.getProj().getSongGroupIndex(groupId);
|
2016-06-15 00:36:25 +00:00
|
|
|
if (songGroup)
|
|
|
|
{
|
2018-07-30 06:20:03 +00:00
|
|
|
amuse::ObjToken<Sequencer> tok = MakeObj<Sequencer>(*this, group, groupId, songGroup, setupId, studio);
|
|
|
|
auto it = m_activeSequencers.emplace(m_activeSequencers.end(), tok);
|
2016-06-15 00:36:25 +00:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
const SFXGroupIndex* sfxGroup = group.getProj().getSFXGroupIndex(groupId);
|
|
|
|
if (sfxGroup)
|
|
|
|
{
|
2018-07-30 06:20:03 +00:00
|
|
|
amuse::ObjToken<Sequencer> tok = MakeObj<Sequencer>(*this, group, groupId, sfxGroup, studio);
|
|
|
|
auto it = m_activeSequencers.emplace(m_activeSequencers.end(), tok);
|
2016-06-15 00:36:25 +00:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return {};
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Studio> Engine::_allocateStudio(bool mainOut)
|
2016-05-07 22:10:57 +00:00
|
|
|
{
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Studio> ret = MakeObj<Studio>(*this, mainOut);
|
2016-07-14 04:54:46 +00:00
|
|
|
ret->m_master.m_backendSubmix = m_backend.allocateSubmix(ret->m_master, mainOut, 0);
|
|
|
|
ret->m_auxA.m_backendSubmix = m_backend.allocateSubmix(ret->m_auxA, mainOut, 1);
|
|
|
|
ret->m_auxB.m_backendSubmix = m_backend.allocateSubmix(ret->m_auxB, mainOut, 2);
|
|
|
|
return ret;
|
2016-05-07 22:10:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
std::list<ObjToken<Voice>>::iterator Engine::_destroyVoice(std::list<ObjToken<Voice>>::iterator it)
|
2016-05-06 21:22:39 +00:00
|
|
|
{
|
2016-05-31 20:40:51 +00:00
|
|
|
assert(this == &(*it)->getEngine());
|
|
|
|
if ((*it)->m_destroyed)
|
2016-05-16 22:13:24 +00:00
|
|
|
return m_activeVoices.begin();
|
2016-05-31 20:40:51 +00:00
|
|
|
(*it)->_destroy();
|
|
|
|
return m_activeVoices.erase(it);
|
2016-05-06 21:22:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
std::list<ObjToken<Sequencer>>::iterator
|
|
|
|
Engine::_destroySequencer(std::list<ObjToken<Sequencer>>::iterator it)
|
2016-05-15 21:56:23 +00:00
|
|
|
{
|
2016-05-31 20:40:51 +00:00
|
|
|
assert(this == &(*it)->getEngine());
|
|
|
|
if ((*it)->m_destroyed)
|
2016-05-16 22:13:24 +00:00
|
|
|
return m_activeSequencers.begin();
|
2016-05-31 20:40:51 +00:00
|
|
|
(*it)->_destroy();
|
|
|
|
return m_activeSequencers.erase(it);
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 04:46:39 +00:00
|
|
|
void Engine::_bringOutYourDead()
|
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_activeEmitters.begin(); it != m_activeEmitters.end();)
|
2016-05-14 06:33:21 +00:00
|
|
|
{
|
|
|
|
Emitter* emitter = it->get();
|
2016-05-16 20:20:29 +00:00
|
|
|
if (emitter->getVoice()->_isRecursivelyDead())
|
2016-05-14 06:33:21 +00:00
|
|
|
{
|
|
|
|
emitter->_destroy();
|
|
|
|
it = m_activeEmitters.erase(it);
|
2016-05-14 06:40:50 +00:00
|
|
|
continue;
|
2016-05-14 06:33:21 +00:00
|
|
|
}
|
2016-05-14 06:40:50 +00:00
|
|
|
++it;
|
2016-05-14 06:33:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_activeVoices.begin(); it != m_activeVoices.end();)
|
2016-05-14 04:46:39 +00:00
|
|
|
{
|
|
|
|
Voice* vox = it->get();
|
|
|
|
vox->_bringOutYourDead();
|
2016-05-16 20:20:29 +00:00
|
|
|
if (vox->_isRecursivelyDead())
|
2016-05-14 04:46:39 +00:00
|
|
|
{
|
2016-05-31 20:40:51 +00:00
|
|
|
it = _destroyVoice(it);
|
2016-05-14 04:46:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
2016-05-15 21:56:23 +00:00
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_activeSequencers.begin(); it != m_activeSequencers.end();)
|
2016-05-15 21:56:23 +00:00
|
|
|
{
|
|
|
|
Sequencer* seq = it->get();
|
|
|
|
seq->_bringOutYourDead();
|
|
|
|
if (seq->m_state == SequencerState::Dead)
|
|
|
|
{
|
2016-05-31 20:40:51 +00:00
|
|
|
it = _destroySequencer(it);
|
2016-05-15 21:56:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
2016-05-14 04:46:39 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 06:01:39 +00:00
|
|
|
void Engine::_on5MsInterval(IBackendVoiceAllocator& engine, double dt)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2017-09-19 03:59:20 +00:00
|
|
|
m_channelSet = engine.getAvailableSet();
|
2016-05-20 06:17:02 +00:00
|
|
|
if (m_midiReader)
|
2016-05-20 22:56:25 +00:00
|
|
|
m_midiReader->pumpReader(dt);
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Sequencer>& seq : m_activeSequencers)
|
2016-05-19 02:19:43 +00:00
|
|
|
seq->advance(dt);
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Emitter>& emitter : m_activeEmitters)
|
2017-09-19 03:59:20 +00:00
|
|
|
emitter->_update();
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Listener>& listener : m_activeListeners)
|
2017-09-20 09:22:46 +00:00
|
|
|
listener->m_dirty = false;
|
2016-05-19 10:12:32 +00:00
|
|
|
}
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2017-02-15 06:01:39 +00:00
|
|
|
void Engine::_onPumpCycleComplete(IBackendVoiceAllocator& engine)
|
2016-05-19 10:12:32 +00:00
|
|
|
{
|
2016-05-14 04:46:39 +00:00
|
|
|
_bringOutYourDead();
|
|
|
|
|
|
|
|
/* Determine lowest available free vid */
|
2016-05-04 05:19:13 +00:00
|
|
|
int maxVid = -1;
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Voice>& vox : m_activeVoices)
|
2016-05-14 04:46:39 +00:00
|
|
|
maxVid = std::max(maxVid, vox->maxVid());
|
2016-05-04 05:19:13 +00:00
|
|
|
m_nextVid = maxVid + 1;
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
AudioGroup* Engine::_addAudioGroup(const AudioGroupData& data, std::unique_ptr<AudioGroup>&& grp)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-07 22:10:57 +00:00
|
|
|
AudioGroup* ret = grp.get();
|
2016-05-15 21:56:23 +00:00
|
|
|
m_audioGroups.emplace(std::make_pair(&data, std::move(grp)));
|
2016-05-11 04:48:08 +00:00
|
|
|
|
|
|
|
/* setup SFX index for contained objects */
|
2016-05-15 21:56:23 +00:00
|
|
|
for (const auto& grp : ret->getProj().sfxGroups())
|
2016-05-11 04:48:08 +00:00
|
|
|
{
|
2018-07-26 03:41:48 +00:00
|
|
|
const SFXGroupIndex& sfxGroup = *grp.second;
|
2016-05-11 04:48:08 +00:00
|
|
|
m_sfxLookup.reserve(m_sfxLookup.size() + sfxGroup.m_sfxEntries.size());
|
2016-05-15 21:56:23 +00:00
|
|
|
for (const auto& ent : sfxGroup.m_sfxEntries)
|
2018-07-14 06:06:33 +00:00
|
|
|
m_sfxLookup[ent.first] = std::make_tuple(ret, grp.first, &ent.second);
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 22:10:57 +00:00
|
|
|
return ret;
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
/** Add GameCube audio group data pointers to engine; must remain resident! */
|
|
|
|
const AudioGroup* Engine::addAudioGroup(const AudioGroupData& data)
|
|
|
|
{
|
|
|
|
removeAudioGroup(data);
|
2018-07-16 07:41:15 +00:00
|
|
|
return _addAudioGroup(data, std::make_unique<AudioGroup>(data));
|
2016-05-28 02:28:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:16:26 +00:00
|
|
|
/** Remove audio group from engine */
|
2016-05-15 21:56:23 +00:00
|
|
|
void Engine::removeAudioGroup(const AudioGroupData& data)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-15 21:56:23 +00:00
|
|
|
auto search = m_audioGroups.find(&data);
|
|
|
|
if (search == m_audioGroups.cend())
|
2016-05-11 04:48:08 +00:00
|
|
|
return;
|
|
|
|
AudioGroup* grp = search->second.get();
|
|
|
|
|
|
|
|
/* Destroy runtime entities within group */
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_activeVoices.begin(); it != m_activeVoices.end();)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-14 04:46:39 +00:00
|
|
|
Voice* vox = it->get();
|
2016-05-15 21:56:23 +00:00
|
|
|
if (&vox->getAudioGroup() == grp)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-14 04:46:39 +00:00
|
|
|
vox->_destroy();
|
2016-05-03 01:16:26 +00:00
|
|
|
it = m_activeVoices.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_activeEmitters.begin(); it != m_activeEmitters.end();)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-14 04:46:39 +00:00
|
|
|
Emitter* emitter = it->get();
|
2016-05-15 21:56:23 +00:00
|
|
|
if (&emitter->getAudioGroup() == grp)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-14 04:46:39 +00:00
|
|
|
emitter->_destroy();
|
2016-05-03 01:16:26 +00:00
|
|
|
it = m_activeEmitters.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_activeSequencers.begin(); it != m_activeSequencers.end();)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-14 04:46:39 +00:00
|
|
|
Sequencer* seq = it->get();
|
2016-05-15 21:56:23 +00:00
|
|
|
if (&seq->getAudioGroup() == grp)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-14 04:46:39 +00:00
|
|
|
seq->_destroy();
|
2016-05-03 01:16:26 +00:00
|
|
|
it = m_activeSequencers.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:48:08 +00:00
|
|
|
/* teardown SFX index for contained objects */
|
|
|
|
for (const auto& pair : grp->getProj().sfxGroups())
|
|
|
|
{
|
2018-07-26 03:41:48 +00:00
|
|
|
const SFXGroupIndex& sfxGroup = *pair.second;
|
2016-05-11 04:48:08 +00:00
|
|
|
for (const auto& pair : sfxGroup.m_sfxEntries)
|
|
|
|
m_sfxLookup.erase(pair.first);
|
|
|
|
}
|
|
|
|
|
2016-05-15 21:56:23 +00:00
|
|
|
m_audioGroups.erase(search);
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 03:04:55 +00:00
|
|
|
/** Create new Studio within engine */
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Studio> Engine::addStudio(bool mainOut) { return _allocateStudio(mainOut); }
|
2016-05-07 22:10:57 +00:00
|
|
|
|
2016-05-03 01:16:26 +00:00
|
|
|
/** Start soundFX playing from loaded audio groups */
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Voice> Engine::fxStart(int sfxId, float vol, float pan, ObjToken<Studio> smx)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-11 04:48:08 +00:00
|
|
|
auto search = m_sfxLookup.find(sfxId);
|
|
|
|
if (search == m_sfxLookup.end())
|
2018-07-28 04:34:29 +00:00
|
|
|
return {};
|
2016-05-11 04:48:08 +00:00
|
|
|
|
2016-05-15 21:56:23 +00:00
|
|
|
AudioGroup* grp = std::get<0>(search->second);
|
2016-05-16 20:20:29 +00:00
|
|
|
const SFXGroupIndex::SFXEntry* entry = std::get<2>(search->second);
|
2016-05-03 01:16:26 +00:00
|
|
|
if (!grp)
|
2018-07-28 04:34:29 +00:00
|
|
|
return {};
|
2016-05-03 01:16:26 +00:00
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
std::list<ObjToken<Voice>>::iterator ret =
|
2017-06-18 02:48:52 +00:00
|
|
|
_allocateVoice(*grp, std::get<1>(search->second), NativeSampleRate, true, false, smx);
|
2016-05-28 02:28:59 +00:00
|
|
|
|
2018-07-15 06:10:50 +00:00
|
|
|
if (!(*ret)->loadMacroObject(entry->macro.id, 0, 1000.f, entry->defKey, entry->defVel, 0))
|
2016-05-14 06:33:21 +00:00
|
|
|
{
|
2016-05-31 20:40:51 +00:00
|
|
|
_destroyVoice(ret);
|
2016-05-14 06:33:21 +00:00
|
|
|
return {};
|
|
|
|
}
|
2016-05-28 02:28:59 +00:00
|
|
|
|
2016-05-31 20:40:51 +00:00
|
|
|
(*ret)->setVolume(vol);
|
|
|
|
(*ret)->setPan(pan);
|
|
|
|
return *ret;
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 04:34:29 +00:00
|
|
|
/** Start SoundMacro node playing directly (for editor use) */
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Voice> Engine::macroStart(const AudioGroup* group, SoundMacroId id, uint8_t key, uint8_t vel,
|
|
|
|
uint8_t mod, ObjToken<Studio> smx)
|
2018-07-28 04:34:29 +00:00
|
|
|
{
|
|
|
|
if (!group)
|
|
|
|
return {};
|
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
std::list<ObjToken<Voice>>::iterator ret =
|
2018-07-28 04:34:29 +00:00
|
|
|
_allocateVoice(*group, {}, NativeSampleRate, true, false, smx);
|
|
|
|
|
|
|
|
if (!(*ret)->loadMacroObject(id, 0, 1000.f, key, vel, mod))
|
|
|
|
{
|
|
|
|
_destroyVoice(ret);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:20:03 +00:00
|
|
|
(*ret)->setVolume(1.f);
|
|
|
|
(*ret)->setPan(0.f);
|
2018-07-28 04:34:29 +00:00
|
|
|
return *ret;
|
|
|
|
}
|
|
|
|
|
2018-07-31 08:04:43 +00:00
|
|
|
/** Start SoundMacro object playing directly (for editor use) */
|
|
|
|
ObjToken<Voice> Engine::macroStart(const AudioGroup* group, const SoundMacro* macro, uint8_t key,
|
|
|
|
uint8_t vel, uint8_t mod, ObjToken<Studio> smx)
|
|
|
|
{
|
|
|
|
if (!group)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::list<ObjToken<Voice>>::iterator ret =
|
|
|
|
_allocateVoice(*group, {}, NativeSampleRate, true, false, smx);
|
|
|
|
|
|
|
|
if (!(*ret)->loadMacroObject(macro, 0, 1000.f, key, vel, mod))
|
|
|
|
{
|
|
|
|
_destroyVoice(ret);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
(*ret)->setVolume(1.f);
|
|
|
|
(*ret)->setPan(0.f);
|
|
|
|
return *ret;
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:16:26 +00:00
|
|
|
/** Start soundFX playing from loaded audio groups, attach to positional emitter */
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Emitter> Engine::addEmitter(const float* pos, const float* dir, float maxDist, float falloff,
|
|
|
|
int sfxId, float minVol, float maxVol, bool doppler, ObjToken<Studio> smx)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-11 04:48:08 +00:00
|
|
|
auto search = m_sfxLookup.find(sfxId);
|
|
|
|
if (search == m_sfxLookup.end())
|
2018-07-28 04:34:29 +00:00
|
|
|
return {};
|
2016-05-11 04:48:08 +00:00
|
|
|
|
2016-05-15 21:56:23 +00:00
|
|
|
AudioGroup* grp = std::get<0>(search->second);
|
2016-05-16 20:20:29 +00:00
|
|
|
const SFXGroupIndex::SFXEntry* entry = std::get<2>(search->second);
|
2016-05-03 01:16:26 +00:00
|
|
|
if (!grp)
|
2018-07-28 04:34:29 +00:00
|
|
|
return {};
|
2016-05-03 01:16:26 +00:00
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
std::list<ObjToken<Voice>>::iterator vox =
|
2017-06-18 02:48:52 +00:00
|
|
|
_allocateVoice(*grp, std::get<1>(search->second), NativeSampleRate, true, true, smx);
|
2016-05-28 02:28:59 +00:00
|
|
|
|
2018-07-15 06:10:50 +00:00
|
|
|
if (!(*vox)->loadMacroObject(entry->macro, 0, 1000.f, entry->defKey, entry->defVel, 0))
|
2016-05-14 06:33:21 +00:00
|
|
|
{
|
2017-09-20 09:22:46 +00:00
|
|
|
_destroyVoice(vox);
|
2016-05-14 06:33:21 +00:00
|
|
|
return {};
|
|
|
|
}
|
2016-05-28 02:28:59 +00:00
|
|
|
|
2017-11-28 02:44:35 +00:00
|
|
|
auto emitIt = m_activeEmitters.emplace(m_activeEmitters.end(),
|
2018-07-29 03:37:06 +00:00
|
|
|
MakeObj<Emitter>(*this, *grp, *vox, maxDist, minVol, falloff, doppler));
|
2017-11-28 02:44:35 +00:00
|
|
|
Emitter& ret = *(*emitIt);
|
|
|
|
|
2017-09-20 09:22:46 +00:00
|
|
|
ret.getVoice()->setPan(entry->panning);
|
2017-09-19 03:59:20 +00:00
|
|
|
ret.setVectors(pos, dir);
|
2016-05-03 05:16:37 +00:00
|
|
|
ret.setMaxVol(maxVol);
|
|
|
|
|
2016-05-31 20:40:51 +00:00
|
|
|
return *emitIt;
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 03:59:20 +00:00
|
|
|
/** Build listener and add to engine's listener list */
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Listener> Engine::addListener(const float* pos, const float* dir, const float* heading, const float* up,
|
|
|
|
float frontDiff, float backDiff, float soundSpeed, float volume)
|
2017-09-19 03:59:20 +00:00
|
|
|
{
|
|
|
|
auto listenerIt = m_activeListeners.emplace(m_activeListeners.end(),
|
2018-07-29 03:37:06 +00:00
|
|
|
MakeObj<Listener>(volume, frontDiff, backDiff, soundSpeed));
|
2017-09-19 03:59:20 +00:00
|
|
|
Listener& ret = *(*listenerIt);
|
|
|
|
ret.setVectors(pos, dir, heading, up);
|
|
|
|
return *listenerIt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Remove listener from engine's listener list */
|
|
|
|
void Engine::removeListener(Listener* listener)
|
|
|
|
{
|
|
|
|
for (auto it = m_activeListeners.begin() ; it != m_activeListeners.end() ; ++it)
|
|
|
|
{
|
|
|
|
if (it->get() == listener)
|
|
|
|
{
|
|
|
|
m_activeListeners.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:16:26 +00:00
|
|
|
/** Start song playing from loaded audio groups */
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Sequencer> Engine::seqPlay(int groupId, int songId, const unsigned char* arrData, ObjToken<Studio> smx)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-15 21:56:23 +00:00
|
|
|
std::pair<AudioGroup*, const SongGroupIndex*> songGrp = _findSongGroup(groupId);
|
2016-06-15 00:36:25 +00:00
|
|
|
if (songGrp.second)
|
|
|
|
{
|
2018-07-29 03:37:06 +00:00
|
|
|
std::list<ObjToken<Sequencer>>::iterator ret = _allocateSequencer(*songGrp.first, groupId, songId, smx);
|
2016-06-15 00:36:25 +00:00
|
|
|
if (!*ret)
|
|
|
|
return {};
|
2016-05-15 21:56:23 +00:00
|
|
|
|
2016-06-15 00:36:25 +00:00
|
|
|
if (arrData)
|
|
|
|
(*ret)->playSong(arrData);
|
|
|
|
return *ret;
|
|
|
|
}
|
2016-05-15 21:56:23 +00:00
|
|
|
|
2016-06-15 00:36:25 +00:00
|
|
|
std::pair<AudioGroup*, const SFXGroupIndex*> sfxGrp = _findSFXGroup(groupId);
|
|
|
|
if (sfxGrp.second)
|
|
|
|
{
|
2018-07-29 03:37:06 +00:00
|
|
|
std::list<ObjToken<Sequencer>>::iterator ret = _allocateSequencer(*sfxGrp.first, groupId, songId, smx);
|
2016-06-15 00:36:25 +00:00
|
|
|
if (!*ret)
|
|
|
|
return {};
|
|
|
|
return *ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 06:16:00 +00:00
|
|
|
/** Set total volume of engine */
|
|
|
|
void Engine::setVolume(float vol)
|
|
|
|
{
|
2017-02-06 03:21:38 +00:00
|
|
|
m_masterVolume = vol;
|
2016-07-14 06:16:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:16:37 +00:00
|
|
|
/** Find voice from VoiceId */
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Voice> Engine::findVoice(int vid)
|
2016-05-03 05:16:37 +00:00
|
|
|
{
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Voice>& vox : m_activeVoices)
|
2016-05-14 06:33:21 +00:00
|
|
|
{
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Voice> ret = vox->_findVoice(vid, vox);
|
2016-05-14 06:33:21 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-05-15 21:56:23 +00:00
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Sequencer>& seq : m_activeSequencers)
|
2016-05-15 21:56:23 +00:00
|
|
|
{
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Voice> ret = seq->findVoice(vid);
|
2016-05-15 21:56:23 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-14 04:46:39 +00:00
|
|
|
return {};
|
2016-05-03 05:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 21:22:39 +00:00
|
|
|
/** Stop all voices in `kg`, stops immediately (no KeyOff) when `flag` set */
|
2016-05-14 04:46:39 +00:00
|
|
|
void Engine::killKeygroup(uint8_t kg, bool now)
|
2016-05-06 21:22:39 +00:00
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_activeVoices.begin(); it != m_activeVoices.end();)
|
2016-05-06 21:22:39 +00:00
|
|
|
{
|
2016-05-14 04:46:39 +00:00
|
|
|
Voice* vox = it->get();
|
|
|
|
if (vox->m_keygroup == kg)
|
2016-05-06 21:22:39 +00:00
|
|
|
{
|
2016-05-14 04:46:39 +00:00
|
|
|
if (now)
|
2016-05-06 21:22:39 +00:00
|
|
|
{
|
2016-05-31 20:40:51 +00:00
|
|
|
it = _destroyVoice(it);
|
2016-05-06 21:22:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-14 04:46:39 +00:00
|
|
|
vox->keyOff();
|
2016-05-06 21:22:39 +00:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
2016-05-15 21:56:23 +00:00
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Sequencer>& seq : m_activeSequencers)
|
2016-05-15 21:56:23 +00:00
|
|
|
seq->killKeygroup(kg, now);
|
2016-05-06 21:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Send all voices using `macroId` the message `val` */
|
|
|
|
void Engine::sendMacroMessage(ObjectId macroId, int32_t val)
|
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_activeVoices.begin(); it != m_activeVoices.end(); ++it)
|
2016-05-14 04:46:39 +00:00
|
|
|
{
|
|
|
|
Voice* vox = it->get();
|
|
|
|
if (vox->getObjectId() == macroId)
|
|
|
|
vox->message(val);
|
|
|
|
}
|
2016-05-15 21:56:23 +00:00
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
for (ObjToken<Sequencer>& seq : m_activeSequencers)
|
2016-05-15 21:56:23 +00:00
|
|
|
seq->sendMacroMessage(macroId, val);
|
2016-05-06 21:22:39 +00:00
|
|
|
}
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|