2016-05-06 21:22:39 +00:00
|
|
|
#include "amuse/Sequencer.hpp"
|
2016-05-15 06:48:26 +00:00
|
|
|
#include "amuse/Submix.hpp"
|
|
|
|
#include "amuse/Voice.hpp"
|
|
|
|
#include "amuse/Engine.hpp"
|
2016-06-15 00:36:25 +00:00
|
|
|
#include <map>
|
2016-05-06 21:22:39 +00:00
|
|
|
|
|
|
|
namespace amuse
|
|
|
|
{
|
|
|
|
|
2016-05-15 21:56:23 +00:00
|
|
|
void Sequencer::ChannelState::_bringOutYourDead()
|
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_chanVoxs.begin(); it != m_chanVoxs.end();)
|
2016-05-15 21:56:23 +00:00
|
|
|
{
|
|
|
|
Voice* vox = it->second.get();
|
|
|
|
vox->_bringOutYourDead();
|
|
|
|
if (vox->_isRecursivelyDead())
|
|
|
|
{
|
|
|
|
it = m_chanVoxs.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
2016-05-16 02:40:18 +00:00
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_keyoffVoxs.begin(); it != m_keyoffVoxs.end();)
|
2016-05-16 02:40:18 +00:00
|
|
|
{
|
|
|
|
Voice* vox = it->get();
|
|
|
|
vox->_bringOutYourDead();
|
|
|
|
if (vox->_isRecursivelyDead())
|
|
|
|
{
|
|
|
|
it = m_keyoffVoxs.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
2018-08-25 08:34:04 +00:00
|
|
|
std::unordered_set<ObjToken<Voice>> newSet = m_keyoffVoxs;
|
|
|
|
m_keyoffVoxs = newSet;
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::_bringOutYourDead()
|
|
|
|
{
|
|
|
|
for (auto& chan : m_chanStates)
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan)
|
2017-12-07 04:06:35 +00:00
|
|
|
chan._bringOutYourDead();
|
2016-05-15 21:56:23 +00:00
|
|
|
|
|
|
|
if (!m_arrData && m_dieOnEnd && getVoiceCount() == 0)
|
|
|
|
m_state = SequencerState::Dead;
|
|
|
|
}
|
|
|
|
|
2016-05-15 06:48:26 +00:00
|
|
|
void Sequencer::_destroy()
|
|
|
|
{
|
|
|
|
Entity::_destroy();
|
2016-07-13 03:04:55 +00:00
|
|
|
if (m_studio)
|
|
|
|
m_studio.reset();
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 05:15:20 +00:00
|
|
|
Sequencer::~Sequencer()
|
|
|
|
{
|
2016-07-13 03:04:55 +00:00
|
|
|
if (m_studio)
|
|
|
|
m_studio.reset();
|
2016-05-31 05:15:20 +00:00
|
|
|
}
|
2016-05-14 22:38:37 +00:00
|
|
|
|
2018-08-25 08:34:04 +00:00
|
|
|
Sequencer::Sequencer(Engine& engine, const AudioGroup& group, GroupId groupId, const SongGroupIndex* songGroup,
|
|
|
|
SongId setupId, ObjToken<Studio> studio)
|
2016-07-14 04:54:46 +00:00
|
|
|
: Entity(engine, group, groupId), m_songGroup(songGroup), m_studio(studio)
|
2016-05-15 06:48:26 +00:00
|
|
|
{
|
2016-06-15 00:36:25 +00:00
|
|
|
auto it = m_songGroup->m_midiSetups.find(setupId);
|
|
|
|
if (it != m_songGroup->m_midiSetups.cend())
|
2018-07-14 06:06:33 +00:00
|
|
|
m_midiSetup = it->second.data();
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 08:34:04 +00:00
|
|
|
Sequencer::Sequencer(Engine& engine, const AudioGroup& group, GroupId groupId, const SFXGroupIndex* sfxGroup,
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Studio> studio)
|
2016-07-14 04:54:46 +00:00
|
|
|
: Entity(engine, group, groupId), m_sfxGroup(sfxGroup), m_studio(studio)
|
2016-06-15 00:36:25 +00:00
|
|
|
{
|
|
|
|
std::map<uint16_t, const SFXGroupIndex::SFXEntry*> sortSFX;
|
|
|
|
for (const auto& sfx : sfxGroup->m_sfxEntries)
|
2018-07-14 06:06:33 +00:00
|
|
|
sortSFX[sfx.first] = &sfx.second;
|
2016-06-15 00:36:25 +00:00
|
|
|
|
|
|
|
m_sfxMappings.reserve(sortSFX.size());
|
|
|
|
for (const auto& sfx : sortSFX)
|
|
|
|
m_sfxMappings.push_back(sfx.second);
|
|
|
|
}
|
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
Sequencer::ChannelState::~ChannelState() {}
|
2016-05-15 06:48:26 +00:00
|
|
|
|
2017-12-07 04:06:35 +00:00
|
|
|
Sequencer::ChannelState::ChannelState(Sequencer& parent, uint8_t chanId)
|
|
|
|
: m_parent(&parent), m_chanId(chanId)
|
2016-05-15 06:48:26 +00:00
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
if (m_parent->m_songGroup)
|
2016-05-15 06:48:26 +00:00
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
if (m_parent->m_midiSetup)
|
2016-06-07 03:42:51 +00:00
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
m_setup = &m_parent->m_midiSetup[chanId];
|
2016-06-15 00:36:25 +00:00
|
|
|
|
|
|
|
if (chanId == 9)
|
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
auto it = m_parent->m_songGroup->m_drumPages.find(m_setup->programNo);
|
|
|
|
if (it != m_parent->m_songGroup->m_drumPages.cend())
|
2016-07-14 19:42:54 +00:00
|
|
|
{
|
2018-07-14 06:06:33 +00:00
|
|
|
m_page = &it->second;
|
2016-07-14 19:42:54 +00:00
|
|
|
m_curProgram = m_setup->programNo;
|
|
|
|
}
|
2016-06-15 00:36:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
auto it = m_parent->m_songGroup->m_normPages.find(m_setup->programNo);
|
|
|
|
if (it != m_parent->m_songGroup->m_normPages.cend())
|
2016-07-14 19:42:54 +00:00
|
|
|
{
|
2018-07-14 06:06:33 +00:00
|
|
|
m_page = &it->second;
|
2016-07-14 19:42:54 +00:00
|
|
|
m_curProgram = m_setup->programNo;
|
|
|
|
}
|
2016-06-15 00:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_curVol = m_setup->volume / 127.f;
|
|
|
|
m_curPan = m_setup->panning / 64.f - 1.f;
|
|
|
|
m_ctrlVals[0x5b] = m_setup->reverb;
|
|
|
|
m_ctrlVals[0x5d] = m_setup->chorus;
|
2016-06-07 03:42:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-15 00:36:25 +00:00
|
|
|
if (chanId == 9)
|
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
auto it = m_parent->m_songGroup->m_drumPages.find(0);
|
|
|
|
if (it != m_parent->m_songGroup->m_drumPages.cend())
|
2018-07-14 06:06:33 +00:00
|
|
|
m_page = &it->second;
|
2016-06-15 00:36:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
auto it = m_parent->m_songGroup->m_normPages.find(0);
|
|
|
|
if (it != m_parent->m_songGroup->m_normPages.cend())
|
2018-07-14 06:06:33 +00:00
|
|
|
m_page = &it->second;
|
2016-06-15 00:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_curVol = 1.f;
|
|
|
|
m_curPan = 0.f;
|
|
|
|
m_ctrlVals[0x5b] = 0;
|
|
|
|
m_ctrlVals[0x5d] = 0;
|
2016-06-07 03:42:51 +00:00
|
|
|
}
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
2017-12-07 04:06:35 +00:00
|
|
|
else if (m_parent->m_sfxGroup)
|
2016-05-15 06:48:26 +00:00
|
|
|
{
|
2016-06-07 03:42:51 +00:00
|
|
|
m_curVol = 1.f;
|
|
|
|
m_curPan = 0.f;
|
|
|
|
m_ctrlVals[0x5b] = 0;
|
|
|
|
m_ctrlVals[0x5d] = 0;
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 01:22:18 +00:00
|
|
|
m_ctrlVals[7] = 127;
|
|
|
|
m_ctrlVals[10] = 64;
|
2016-05-19 02:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::advance(double dt)
|
|
|
|
{
|
|
|
|
if (m_state == SequencerState::Playing)
|
2017-02-27 05:24:58 +00:00
|
|
|
{
|
|
|
|
if (m_stopFadeTime)
|
|
|
|
{
|
|
|
|
float step = dt / m_stopFadeTime * m_stopFadeBeginVol;
|
|
|
|
float vol = std::max(0.f, m_curVol - step);
|
|
|
|
if (vol == 0.f)
|
|
|
|
{
|
|
|
|
m_arrData = nullptr;
|
|
|
|
m_state = SequencerState::Interactive;
|
|
|
|
allOff(true);
|
|
|
|
m_stopFadeTime = 0.f;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setVolume(vol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_volFadeTime)
|
|
|
|
{
|
2017-03-01 05:36:58 +00:00
|
|
|
float step = dt / m_volFadeTime * std::fabs(m_volFadeTarget - m_volFadeStart);
|
2017-02-27 05:24:58 +00:00
|
|
|
float vol;
|
|
|
|
if (m_curVol < m_volFadeTarget)
|
|
|
|
vol = std::min(m_volFadeTarget, m_curVol + step);
|
2018-05-25 06:37:27 +00:00
|
|
|
else
|
2017-02-27 05:24:58 +00:00
|
|
|
vol = std::max(m_volFadeTarget, m_curVol - step);
|
|
|
|
if (vol == m_volFadeTarget)
|
|
|
|
m_volFadeTime = 0.f;
|
|
|
|
else
|
|
|
|
setVolume(vol);
|
|
|
|
}
|
|
|
|
|
2016-05-19 02:19:43 +00:00
|
|
|
if (m_songState.advance(*this, dt))
|
|
|
|
{
|
|
|
|
m_arrData = nullptr;
|
|
|
|
m_state = SequencerState::Interactive;
|
|
|
|
allOff();
|
|
|
|
}
|
2017-02-27 05:24:58 +00:00
|
|
|
}
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 02:40:18 +00:00
|
|
|
size_t Sequencer::ChannelState::getVoiceCount() const
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
for (const auto& vox : m_chanVoxs)
|
|
|
|
ret += vox.second->getTotalVoices();
|
|
|
|
for (const auto& vox : m_keyoffVoxs)
|
|
|
|
ret += vox->getTotalVoices();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-15 06:48:26 +00:00
|
|
|
size_t Sequencer::getVoiceCount() const
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
for (const auto& chan : m_chanStates)
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan)
|
2017-12-07 04:06:35 +00:00
|
|
|
ret += chan.getVoiceCount();
|
2016-05-15 06:48:26 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Voice> Sequencer::ChannelState::keyOn(uint8_t note, uint8_t velocity)
|
2016-05-15 06:48:26 +00:00
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
if (m_parent->m_songGroup && !m_page)
|
2016-05-15 06:48:26 +00:00
|
|
|
return {};
|
|
|
|
|
2018-07-30 06:20:03 +00:00
|
|
|
if (m_lastVoice && m_lastVoice->isDestroyed())
|
2018-07-29 03:37:06 +00:00
|
|
|
m_lastVoice.reset();
|
|
|
|
|
2016-06-01 21:17:16 +00:00
|
|
|
/* If portamento is enabled for voice, pre-empt spawning new voices */
|
2018-07-29 03:37:06 +00:00
|
|
|
if (ObjToken<Voice> lastVoice = m_lastVoice)
|
2016-06-01 21:17:16 +00:00
|
|
|
{
|
|
|
|
uint8_t lastNote = lastVoice->getLastNote();
|
|
|
|
if (lastVoice->doPortamento(note))
|
|
|
|
{
|
|
|
|
m_chanVoxs.erase(lastNote);
|
|
|
|
m_chanVoxs[note] = lastVoice;
|
|
|
|
return lastVoice;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 05:27:39 +00:00
|
|
|
/* Ensure keyoff sent first */
|
2016-05-19 11:03:38 +00:00
|
|
|
auto keySearch = m_chanVoxs.find(note);
|
|
|
|
if (keySearch != m_chanVoxs.cend())
|
|
|
|
{
|
2018-07-29 03:37:06 +00:00
|
|
|
if (keySearch->second == m_lastVoice)
|
2016-06-01 21:17:16 +00:00
|
|
|
m_lastVoice.reset();
|
2016-05-19 11:03:38 +00:00
|
|
|
keySearch->second->keyOff();
|
|
|
|
keySearch->second->setPedal(false);
|
2018-08-25 08:34:04 +00:00
|
|
|
m_keyoffVoxs.emplace(keySearch->second);
|
2016-05-19 11:03:38 +00:00
|
|
|
m_chanVoxs.erase(keySearch);
|
|
|
|
}
|
2016-05-19 05:27:39 +00:00
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
std::list<ObjToken<Voice>>::iterator ret = m_parent->m_engine._allocateVoice(
|
2017-12-07 04:06:35 +00:00
|
|
|
m_parent->m_audioGroup, m_parent->m_groupId, NativeSampleRate, true, false, m_parent->m_studio);
|
2016-05-31 20:40:51 +00:00
|
|
|
if (*ret)
|
2016-05-15 06:48:26 +00:00
|
|
|
{
|
2018-08-10 06:19:23 +00:00
|
|
|
(*ret)->m_sequencer = m_parent;
|
2016-05-31 20:40:51 +00:00
|
|
|
m_chanVoxs[note] = *ret;
|
|
|
|
(*ret)->installCtrlValues(m_ctrlVals);
|
2016-05-28 02:28:59 +00:00
|
|
|
|
2016-06-15 00:36:25 +00:00
|
|
|
ObjectId oid;
|
2018-07-15 06:10:50 +00:00
|
|
|
bool res;
|
2017-12-07 04:06:35 +00:00
|
|
|
if (m_parent->m_songGroup)
|
2018-07-15 06:10:50 +00:00
|
|
|
{
|
2018-07-14 06:06:33 +00:00
|
|
|
oid = m_page->objId;
|
2018-07-15 06:10:50 +00:00
|
|
|
res = (*ret)->loadPageObject(oid, m_parent->m_ticksPerSec, note, velocity, m_ctrlVals[1]);
|
|
|
|
}
|
2017-12-07 04:06:35 +00:00
|
|
|
else if (m_parent->m_sfxMappings.size())
|
2016-06-15 00:36:25 +00:00
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
size_t lookupIdx = note % m_parent->m_sfxMappings.size();
|
|
|
|
const SFXGroupIndex::SFXEntry* sfxEntry = m_parent->m_sfxMappings[lookupIdx];
|
2018-08-10 06:19:23 +00:00
|
|
|
oid = sfxEntry->objId;
|
2016-06-15 00:36:25 +00:00
|
|
|
note = sfxEntry->defKey;
|
2018-08-10 06:19:23 +00:00
|
|
|
res = (*ret)->loadPageObject(oid, m_parent->m_ticksPerSec, note, velocity, m_ctrlVals[1]);
|
2016-06-15 00:36:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
|
2018-07-15 06:10:50 +00:00
|
|
|
if (!res)
|
2016-05-22 23:08:28 +00:00
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
m_parent->m_engine._destroyVoice(ret);
|
2016-05-22 23:08:28 +00:00
|
|
|
return {};
|
|
|
|
}
|
2017-12-07 04:06:35 +00:00
|
|
|
(*ret)->setVolume(m_parent->m_curVol * m_curVol);
|
2016-05-31 20:40:51 +00:00
|
|
|
(*ret)->setReverbVol(m_ctrlVals[0x5b] / 127.f);
|
2016-07-14 04:54:46 +00:00
|
|
|
(*ret)->setAuxBVol(m_ctrlVals[0x5d] / 127.f);
|
2016-05-31 20:40:51 +00:00
|
|
|
(*ret)->setPan(m_curPan);
|
|
|
|
(*ret)->setPitchWheel(m_curPitchWheel);
|
2018-08-14 08:36:02 +00:00
|
|
|
if (m_pitchWheelRange != -1)
|
|
|
|
(*ret)->setPitchWheelRange(m_pitchWheelRange, m_pitchWheelRange);
|
2016-05-16 20:20:29 +00:00
|
|
|
|
2016-05-22 23:08:28 +00:00
|
|
|
if (m_ctrlVals[64] > 64)
|
2016-05-31 20:40:51 +00:00
|
|
|
(*ret)->setPedal(true);
|
2016-06-01 21:17:16 +00:00
|
|
|
|
|
|
|
m_lastVoice = *ret;
|
2016-05-22 23:08:28 +00:00
|
|
|
}
|
2016-05-16 20:20:29 +00:00
|
|
|
|
2016-05-31 20:40:51 +00:00
|
|
|
return *ret;
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Voice> Sequencer::keyOn(uint8_t chan, uint8_t note, uint8_t velocity)
|
2016-05-15 06:48:26 +00:00
|
|
|
{
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan > 15)
|
|
|
|
return {};
|
2016-05-15 06:48:26 +00:00
|
|
|
|
2016-05-19 02:19:43 +00:00
|
|
|
if (!m_chanStates[chan])
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chan] = ChannelState(*this, chan);
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2017-12-07 04:06:35 +00:00
|
|
|
return m_chanStates[chan].keyOn(note, velocity);
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::ChannelState::keyOff(uint8_t note, uint8_t velocity)
|
|
|
|
{
|
|
|
|
auto keySearch = m_chanVoxs.find(note);
|
|
|
|
if (keySearch == m_chanVoxs.cend())
|
|
|
|
return;
|
|
|
|
|
2018-07-30 06:20:03 +00:00
|
|
|
if ((m_lastVoice && m_lastVoice->isDestroyed()) || keySearch->second == m_lastVoice)
|
2016-06-01 21:17:16 +00:00
|
|
|
m_lastVoice.reset();
|
2016-05-15 06:48:26 +00:00
|
|
|
keySearch->second->keyOff();
|
2018-08-25 08:34:04 +00:00
|
|
|
m_keyoffVoxs.emplace(keySearch->second);
|
2016-05-15 06:48:26 +00:00
|
|
|
m_chanVoxs.erase(keySearch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::keyOff(uint8_t chan, uint8_t note, uint8_t velocity)
|
|
|
|
{
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan > 15 || !m_chanStates[chan])
|
2016-05-15 06:48:26 +00:00
|
|
|
return;
|
|
|
|
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chan].keyOff(note, velocity);
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::ChannelState::setCtrlValue(uint8_t ctrl, int8_t val)
|
|
|
|
{
|
|
|
|
m_ctrlVals[ctrl] = val;
|
2016-05-16 20:20:29 +00:00
|
|
|
for (const auto& vox : m_chanVoxs)
|
2016-05-21 21:40:03 +00:00
|
|
|
vox.second->_notifyCtrlChange(ctrl, val);
|
2016-05-16 20:20:29 +00:00
|
|
|
for (const auto& vox : m_keyoffVoxs)
|
2016-05-21 21:40:03 +00:00
|
|
|
vox->_notifyCtrlChange(ctrl, val);
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2016-05-22 08:35:55 +00:00
|
|
|
switch (ctrl)
|
|
|
|
{
|
|
|
|
case 7:
|
2016-05-19 02:19:43 +00:00
|
|
|
setVolume(val / 127.f);
|
2016-05-22 08:35:55 +00:00
|
|
|
break;
|
|
|
|
case 10:
|
2016-05-19 02:19:43 +00:00
|
|
|
setPan(val / 64.f - 1.f);
|
2016-05-22 08:35:55 +00:00
|
|
|
break;
|
2018-08-14 08:36:02 +00:00
|
|
|
case 98:
|
|
|
|
// RPN LSB
|
|
|
|
m_rpn &= ~0x7f;
|
|
|
|
m_rpn |= val;
|
|
|
|
case 99:
|
|
|
|
// RPN MSB
|
|
|
|
m_rpn &= ~0x3f80;
|
|
|
|
m_rpn |= val << 7;
|
|
|
|
case 6:
|
|
|
|
if (m_rpn == 0)
|
|
|
|
m_pitchWheelRange = val;
|
|
|
|
case 96:
|
|
|
|
if (m_rpn == 0)
|
|
|
|
m_pitchWheelRange += 1;
|
|
|
|
case 97:
|
|
|
|
if (m_rpn == 0)
|
|
|
|
m_pitchWheelRange -= 1;
|
2016-07-14 04:54:46 +00:00
|
|
|
default:
|
|
|
|
break;
|
2016-05-22 08:35:55 +00:00
|
|
|
}
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sequencer::ChannelState::programChange(int8_t prog)
|
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
if (m_parent->m_songGroup)
|
2016-05-16 20:20:29 +00:00
|
|
|
{
|
2016-06-15 00:36:25 +00:00
|
|
|
if (m_chanId == 9)
|
2016-05-16 20:20:29 +00:00
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
auto it = m_parent->m_songGroup->m_drumPages.find(prog);
|
|
|
|
if (it != m_parent->m_songGroup->m_drumPages.cend())
|
2016-06-15 00:36:25 +00:00
|
|
|
{
|
2018-07-14 06:06:33 +00:00
|
|
|
m_page = &it->second;
|
2016-06-15 00:36:25 +00:00
|
|
|
m_curProgram = prog;
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
2016-06-15 00:36:25 +00:00
|
|
|
else
|
2016-05-16 20:20:29 +00:00
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
auto it = m_parent->m_songGroup->m_normPages.find(prog);
|
|
|
|
if (it != m_parent->m_songGroup->m_normPages.cend())
|
2016-06-15 00:36:25 +00:00
|
|
|
{
|
2018-07-14 06:06:33 +00:00
|
|
|
m_page = &it->second;
|
2016-06-15 00:36:25 +00:00
|
|
|
m_curProgram = prog;
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::ChannelState::nextProgram()
|
|
|
|
{
|
|
|
|
int newProg = m_curProgram;
|
|
|
|
while ((newProg += 1) <= 127)
|
|
|
|
if (programChange(newProg))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::ChannelState::prevProgram()
|
|
|
|
{
|
|
|
|
int newProg = m_curProgram;
|
|
|
|
while ((newProg -= 1) >= 0)
|
|
|
|
if (programChange(newProg))
|
|
|
|
break;
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::setCtrlValue(uint8_t chan, uint8_t ctrl, int8_t val)
|
|
|
|
{
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan > 15)
|
2016-05-15 06:48:26 +00:00
|
|
|
return;
|
|
|
|
|
2016-07-04 03:35:37 +00:00
|
|
|
if (ctrl == 0x66)
|
|
|
|
{
|
|
|
|
printf("Loop Start\n");
|
|
|
|
}
|
|
|
|
else if (ctrl == 0x67)
|
|
|
|
{
|
|
|
|
printf("Loop End\n");
|
|
|
|
}
|
|
|
|
|
2016-05-19 02:19:43 +00:00
|
|
|
if (!m_chanStates[chan])
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chan] = ChannelState(*this, chan);
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chan].setCtrlValue(ctrl, val);
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::ChannelState::setPitchWheel(float pitchWheel)
|
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
m_curPitchWheel = pitchWheel;
|
2016-05-15 06:48:26 +00:00
|
|
|
for (const auto& vox : m_chanVoxs)
|
|
|
|
vox.second->setPitchWheel(pitchWheel);
|
2016-05-16 02:40:18 +00:00
|
|
|
for (const auto& vox : m_keyoffVoxs)
|
|
|
|
vox->setPitchWheel(pitchWheel);
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::setPitchWheel(uint8_t chan, float pitchWheel)
|
|
|
|
{
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan > 15)
|
2016-05-15 06:48:26 +00:00
|
|
|
return;
|
|
|
|
|
2016-05-19 02:19:43 +00:00
|
|
|
if (!m_chanStates[chan])
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chan] = ChannelState(*this, chan);
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chan].setPitchWheel(pitchWheel);
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
void Sequencer::setTempo(double ticksPerSec) { m_ticksPerSec = ticksPerSec; }
|
2016-05-15 06:48:26 +00:00
|
|
|
|
|
|
|
void Sequencer::ChannelState::allOff()
|
|
|
|
{
|
2018-07-30 06:20:03 +00:00
|
|
|
if (m_lastVoice && m_lastVoice->isDestroyed())
|
2018-07-29 03:37:06 +00:00
|
|
|
m_lastVoice.reset();
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_chanVoxs.begin(); it != m_chanVoxs.end();)
|
2016-05-22 23:08:28 +00:00
|
|
|
{
|
2018-07-29 03:37:06 +00:00
|
|
|
if (it->second == m_lastVoice)
|
2016-06-01 21:17:16 +00:00
|
|
|
m_lastVoice.reset();
|
2016-05-22 23:08:28 +00:00
|
|
|
it->second->keyOff();
|
2018-08-25 08:34:04 +00:00
|
|
|
m_keyoffVoxs.emplace(it->second);
|
2016-05-22 23:08:28 +00:00
|
|
|
it = m_chanVoxs.erase(it);
|
|
|
|
}
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::allOff(bool now)
|
|
|
|
{
|
|
|
|
if (now)
|
|
|
|
for (auto& chan : m_chanStates)
|
2016-05-16 20:20:29 +00:00
|
|
|
{
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan)
|
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
for (const auto& vox : chan.m_chanVoxs)
|
2016-05-31 20:40:51 +00:00
|
|
|
vox.second->kill();
|
2017-12-07 04:06:35 +00:00
|
|
|
for (const auto& vox : chan.m_keyoffVoxs)
|
2016-05-31 20:40:51 +00:00
|
|
|
vox->kill();
|
2017-12-07 04:06:35 +00:00
|
|
|
chan.m_chanVoxs.clear();
|
|
|
|
chan.m_keyoffVoxs.clear();
|
2016-05-19 02:19:43 +00:00
|
|
|
}
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
2016-05-15 06:48:26 +00:00
|
|
|
else
|
|
|
|
for (auto& chan : m_chanStates)
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan)
|
2017-12-07 04:06:35 +00:00
|
|
|
chan.allOff();
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 06:17:02 +00:00
|
|
|
void Sequencer::allOff(uint8_t chan, bool now)
|
|
|
|
{
|
|
|
|
if (chan > 15 || !m_chanStates[chan])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (now)
|
|
|
|
{
|
2017-12-07 04:06:35 +00:00
|
|
|
for (const auto& vox : m_chanStates[chan].m_chanVoxs)
|
2016-05-31 20:40:51 +00:00
|
|
|
vox.second->kill();
|
2017-12-07 04:06:35 +00:00
|
|
|
for (const auto& vox : m_chanStates[chan].m_keyoffVoxs)
|
2016-05-31 20:40:51 +00:00
|
|
|
vox->kill();
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chan].m_chanVoxs.clear();
|
|
|
|
m_chanStates[chan].m_keyoffVoxs.clear();
|
2016-05-20 06:17:02 +00:00
|
|
|
}
|
|
|
|
else
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chan].allOff();
|
2016-05-20 06:17:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 21:56:23 +00:00
|
|
|
void Sequencer::ChannelState::killKeygroup(uint8_t kg, bool now)
|
|
|
|
{
|
2018-07-30 06:20:03 +00:00
|
|
|
if (m_lastVoice && m_lastVoice->isDestroyed())
|
2018-07-29 03:37:06 +00:00
|
|
|
m_lastVoice.reset();
|
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_chanVoxs.begin(); it != m_chanVoxs.end();)
|
2016-05-15 21:56:23 +00:00
|
|
|
{
|
|
|
|
Voice* vox = it->second.get();
|
|
|
|
if (vox->m_keygroup == kg)
|
|
|
|
{
|
2018-07-29 03:37:06 +00:00
|
|
|
if (it->second == m_lastVoice)
|
2016-06-01 21:17:16 +00:00
|
|
|
m_lastVoice.reset();
|
2016-05-15 21:56:23 +00:00
|
|
|
if (now)
|
|
|
|
{
|
2018-08-26 04:57:02 +00:00
|
|
|
vox->kill();
|
2016-05-15 21:56:23 +00:00
|
|
|
it = m_chanVoxs.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
vox->keyOff();
|
2018-08-25 08:34:04 +00:00
|
|
|
m_keyoffVoxs.emplace(it->second);
|
2016-05-22 23:08:28 +00:00
|
|
|
it = m_chanVoxs.erase(it);
|
|
|
|
continue;
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
2016-05-16 02:40:18 +00:00
|
|
|
|
|
|
|
if (now)
|
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = m_keyoffVoxs.begin(); it != m_keyoffVoxs.end();)
|
2016-05-16 02:40:18 +00:00
|
|
|
{
|
|
|
|
Voice* vox = it->get();
|
|
|
|
if (vox->m_keygroup == kg)
|
|
|
|
{
|
2018-08-26 04:57:02 +00:00
|
|
|
vox->kill();
|
2016-05-16 02:40:18 +00:00
|
|
|
it = m_keyoffVoxs.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::killKeygroup(uint8_t kg, bool now)
|
|
|
|
{
|
|
|
|
for (auto& chan : m_chanStates)
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan)
|
2017-12-07 04:06:35 +00:00
|
|
|
chan.killKeygroup(kg, now);
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Voice> Sequencer::ChannelState::findVoice(int vid)
|
2016-05-15 21:56:23 +00:00
|
|
|
{
|
|
|
|
for (const auto& vox : m_chanVoxs)
|
|
|
|
if (vox.second->vid() == vid)
|
|
|
|
return vox.second;
|
2016-05-16 02:40:18 +00:00
|
|
|
for (const auto& vox : m_keyoffVoxs)
|
|
|
|
if (vox->vid() == vid)
|
|
|
|
return vox;
|
2016-05-15 21:56:23 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Voice> Sequencer::findVoice(int vid)
|
2016-05-15 21:56:23 +00:00
|
|
|
{
|
|
|
|
for (auto& chan : m_chanStates)
|
|
|
|
{
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan)
|
|
|
|
{
|
2018-07-29 03:37:06 +00:00
|
|
|
ObjToken<Voice> ret = chan.findVoice(vid);
|
2016-05-19 02:19:43 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::ChannelState::sendMacroMessage(ObjectId macroId, int32_t val)
|
|
|
|
{
|
|
|
|
for (const auto& v : m_chanVoxs)
|
|
|
|
{
|
|
|
|
Voice* vox = v.second.get();
|
|
|
|
if (vox->getObjectId() == macroId)
|
|
|
|
vox->message(val);
|
|
|
|
}
|
2016-05-16 02:40:18 +00:00
|
|
|
for (const auto& v : m_keyoffVoxs)
|
|
|
|
{
|
|
|
|
Voice* vox = v.get();
|
|
|
|
if (vox->getObjectId() == macroId)
|
|
|
|
vox->message(val);
|
|
|
|
}
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::sendMacroMessage(ObjectId macroId, int32_t val)
|
|
|
|
{
|
|
|
|
for (auto& chan : m_chanStates)
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chan)
|
2017-12-07 04:06:35 +00:00
|
|
|
chan.sendMacroMessage(macroId, val);
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::playSong(const unsigned char* arrData, bool dieOnEnd)
|
|
|
|
{
|
|
|
|
m_arrData = arrData;
|
|
|
|
m_dieOnEnd = dieOnEnd;
|
2016-05-19 02:19:43 +00:00
|
|
|
m_songState.initialize(arrData);
|
2016-06-01 21:32:48 +00:00
|
|
|
setTempo(m_songState.getTempo() * 384 / 60);
|
2016-05-15 21:56:23 +00:00
|
|
|
m_state = SequencerState::Playing;
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 05:24:58 +00:00
|
|
|
void Sequencer::stopSong(float fadeTime, bool now)
|
2016-05-19 10:12:32 +00:00
|
|
|
{
|
2017-02-27 05:24:58 +00:00
|
|
|
if (fadeTime == 0.f)
|
|
|
|
{
|
|
|
|
allOff(now);
|
|
|
|
m_arrData = nullptr;
|
|
|
|
m_state = SequencerState::Interactive;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_stopFadeTime = fadeTime;
|
|
|
|
m_stopFadeBeginVol = m_curVol;
|
|
|
|
}
|
2016-05-19 10:12:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 02:40:18 +00:00
|
|
|
void Sequencer::ChannelState::setVolume(float vol)
|
|
|
|
{
|
2016-05-19 02:19:43 +00:00
|
|
|
m_curVol = vol;
|
2017-12-07 04:06:35 +00:00
|
|
|
float voxVol = m_parent->m_curVol * m_curVol;
|
2016-05-16 02:40:18 +00:00
|
|
|
for (const auto& v : m_chanVoxs)
|
|
|
|
{
|
|
|
|
Voice* vox = v.second.get();
|
2016-05-19 02:19:43 +00:00
|
|
|
vox->setVolume(voxVol);
|
2016-05-16 02:40:18 +00:00
|
|
|
}
|
|
|
|
for (const auto& v : m_keyoffVoxs)
|
|
|
|
{
|
|
|
|
Voice* vox = v.get();
|
2016-05-19 02:19:43 +00:00
|
|
|
vox->setVolume(voxVol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::ChannelState::setPan(float pan)
|
|
|
|
{
|
|
|
|
m_curPan = pan;
|
|
|
|
for (const auto& v : m_chanVoxs)
|
|
|
|
{
|
|
|
|
Voice* vox = v.second.get();
|
|
|
|
vox->setPan(m_curPan);
|
|
|
|
}
|
|
|
|
for (const auto& v : m_keyoffVoxs)
|
|
|
|
{
|
|
|
|
Voice* vox = v.get();
|
|
|
|
vox->setPan(m_curPan);
|
2016-05-16 02:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-27 05:24:58 +00:00
|
|
|
void Sequencer::setVolume(float vol, float fadeTime)
|
2016-05-16 02:40:18 +00:00
|
|
|
{
|
2017-02-27 05:24:58 +00:00
|
|
|
if (fadeTime == 0.f)
|
|
|
|
{
|
|
|
|
m_curVol = vol;
|
|
|
|
for (auto& chan : m_chanStates)
|
|
|
|
if (chan)
|
2017-12-07 04:06:35 +00:00
|
|
|
chan.setVolume(chan.m_curVol);
|
2017-02-27 05:24:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_volFadeTime = fadeTime;
|
|
|
|
m_volFadeTarget = vol;
|
|
|
|
m_volFadeStart = m_curVol;
|
|
|
|
}
|
2016-05-16 02:40:18 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 20:20:29 +00:00
|
|
|
int8_t Sequencer::getChanProgram(int8_t chanId) const
|
|
|
|
{
|
2016-07-14 19:42:54 +00:00
|
|
|
if (chanId > 15)
|
2016-05-16 20:20:29 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-07-14 19:42:54 +00:00
|
|
|
if (!m_chanStates[chanId])
|
|
|
|
{
|
|
|
|
if (!m_midiSetup)
|
|
|
|
return 0;
|
|
|
|
return m_midiSetup[chanId].programNo;
|
|
|
|
}
|
|
|
|
|
2017-12-07 04:06:35 +00:00
|
|
|
return m_chanStates[chanId].m_curProgram;
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sequencer::setChanProgram(int8_t chanId, int8_t prog)
|
|
|
|
{
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chanId > 15)
|
2016-05-16 20:20:29 +00:00
|
|
|
return false;
|
|
|
|
|
2016-05-19 02:19:43 +00:00
|
|
|
if (!m_chanStates[chanId])
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chanId] = ChannelState(*this, chanId);
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2017-12-07 04:06:35 +00:00
|
|
|
return m_chanStates[chanId].programChange(prog);
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::nextChanProgram(int8_t chanId)
|
|
|
|
{
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chanId > 15)
|
2016-05-16 20:20:29 +00:00
|
|
|
return;
|
|
|
|
|
2016-05-19 02:19:43 +00:00
|
|
|
if (!m_chanStates[chanId])
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chanId] = ChannelState(*this, chanId);
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2017-12-07 04:06:35 +00:00
|
|
|
return m_chanStates[chanId].nextProgram();
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sequencer::prevChanProgram(int8_t chanId)
|
|
|
|
{
|
2016-05-19 02:19:43 +00:00
|
|
|
if (chanId > 15)
|
2016-05-16 20:20:29 +00:00
|
|
|
return;
|
|
|
|
|
2016-05-19 02:19:43 +00:00
|
|
|
if (!m_chanStates[chanId])
|
2017-12-07 04:06:35 +00:00
|
|
|
m_chanStates[chanId] = ChannelState(*this, chanId);
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2017-12-07 04:06:35 +00:00
|
|
|
return m_chanStates[chanId].prevProgram();
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
2016-05-06 21:22:39 +00:00
|
|
|
}
|