Sequencer: Eliminate magic values where applicable

Many of them are just representative of the sizes of an array, so we can
just query the array instead.
This commit is contained in:
Lioncash 2019-09-10 19:48:29 -04:00
parent 6c07ec907a
commit f50d67eff0
1 changed files with 38 additions and 19 deletions

View File

@ -246,11 +246,13 @@ ObjToken<Voice> Sequencer::ChannelState::keyOn(uint8_t note, uint8_t velocity) {
} }
ObjToken<Voice> Sequencer::keyOn(uint8_t chan, uint8_t note, uint8_t velocity) { ObjToken<Voice> Sequencer::keyOn(uint8_t chan, uint8_t note, uint8_t velocity) {
if (chan > 15) if (chan >= m_chanStates.size()) {
return {}; return {};
}
if (!m_chanStates[chan]) if (!m_chanStates[chan]) {
m_chanStates[chan] = ChannelState(*this, chan); m_chanStates[chan] = ChannelState(*this, chan);
}
return m_chanStates[chan].keyOn(note, velocity); return m_chanStates[chan].keyOn(note, velocity);
} }
@ -268,8 +270,9 @@ void Sequencer::ChannelState::keyOff(uint8_t note, uint8_t velocity) {
} }
void Sequencer::keyOff(uint8_t chan, uint8_t note, uint8_t velocity) { void Sequencer::keyOff(uint8_t chan, uint8_t note, uint8_t velocity) {
if (chan > 15 || !m_chanStates[chan]) if (chan >= m_chanStates.size() || !m_chanStates[chan]) {
return; return;
}
m_chanStates[chan].keyOff(note, velocity); m_chanStates[chan].keyOff(note, velocity);
} }
@ -351,8 +354,9 @@ void Sequencer::ChannelState::prevProgram() {
} }
void Sequencer::setCtrlValue(uint8_t chan, uint8_t ctrl, int8_t val) { void Sequencer::setCtrlValue(uint8_t chan, uint8_t ctrl, int8_t val) {
if (chan > 15) if (chan >= m_chanStates.size()) {
return; return;
}
if (ctrl == 0x66) { if (ctrl == 0x66) {
fmt::print(fmt("Loop Start\n")); fmt::print(fmt("Loop Start\n"));
@ -360,8 +364,9 @@ void Sequencer::setCtrlValue(uint8_t chan, uint8_t ctrl, int8_t val) {
fmt::print(fmt("Loop End\n")); fmt::print(fmt("Loop End\n"));
} }
if (!m_chanStates[chan]) if (!m_chanStates[chan]) {
m_chanStates[chan] = ChannelState(*this, chan); m_chanStates[chan] = ChannelState(*this, chan);
}
m_chanStates[chan].setCtrlValue(ctrl, val); m_chanStates[chan].setCtrlValue(ctrl, val);
} }
@ -375,11 +380,13 @@ void Sequencer::ChannelState::setPitchWheel(float pitchWheel) {
} }
void Sequencer::setPitchWheel(uint8_t chan, float pitchWheel) { void Sequencer::setPitchWheel(uint8_t chan, float pitchWheel) {
if (chan > 15) if (chan >= m_chanStates.size()) {
return; return;
}
if (!m_chanStates[chan]) if (!m_chanStates[chan]) {
m_chanStates[chan] = ChannelState(*this, chan); m_chanStates[chan] = ChannelState(*this, chan);
}
m_chanStates[chan].setPitchWheel(pitchWheel); m_chanStates[chan].setPitchWheel(pitchWheel);
} }
@ -422,19 +429,23 @@ void Sequencer::allOff(bool now) {
} }
void Sequencer::allOff(uint8_t chan, bool now) { void Sequencer::allOff(uint8_t chan, bool now) {
if (chan > 15 || !m_chanStates[chan]) if (chan >= m_chanStates.size() || !m_chanStates[chan]) {
return; return;
}
if (now) { if (now) {
for (const auto& vox : m_chanStates[chan].m_chanVoxs) for (const auto& vox : m_chanStates[chan].m_chanVoxs) {
vox.second->kill(); vox.second->kill();
for (const auto& vox : m_chanStates[chan].m_keyoffVoxs) }
for (const auto& vox : m_chanStates[chan].m_keyoffVoxs) {
vox->kill(); vox->kill();
}
m_chanStates[chan].m_chanVoxs.clear(); m_chanStates[chan].m_chanVoxs.clear();
m_chanStates[chan].m_keyoffVoxs.clear(); m_chanStates[chan].m_keyoffVoxs.clear();
} else } else {
m_chanStates[chan].allOff(); m_chanStates[chan].allOff();
} }
}
void Sequencer::ChannelState::killKeygroup(uint8_t kg, bool now) { void Sequencer::ChannelState::killKeygroup(uint8_t kg, bool now) {
if (m_lastVoice && m_lastVoice->isDestroyed()) if (m_lastVoice && m_lastVoice->isDestroyed())
@ -575,12 +586,14 @@ void Sequencer::setVolume(float vol, float fadeTime) {
} }
int8_t Sequencer::getChanProgram(int8_t chanId) const { int8_t Sequencer::getChanProgram(int8_t chanId) const {
if (chanId > 15) if (static_cast<size_t>(chanId) >= m_chanStates.size()) {
return 0; return 0;
}
if (!m_chanStates[chanId]) { if (!m_chanStates[chanId]) {
if (!m_midiSetup) if (!m_midiSetup) {
return 0; return 0;
}
return m_midiSetup[chanId].programNo; return m_midiSetup[chanId].programNo;
} }
@ -588,31 +601,37 @@ int8_t Sequencer::getChanProgram(int8_t chanId) const {
} }
bool Sequencer::setChanProgram(int8_t chanId, int8_t prog) { bool Sequencer::setChanProgram(int8_t chanId, int8_t prog) {
if (chanId > 15) if (static_cast<size_t>(chanId) >= m_chanStates.size()) {
return false; return false;
}
if (!m_chanStates[chanId]) if (!m_chanStates[chanId]) {
m_chanStates[chanId] = ChannelState(*this, chanId); m_chanStates[chanId] = ChannelState(*this, chanId);
}
return m_chanStates[chanId].programChange(prog); return m_chanStates[chanId].programChange(prog);
} }
void Sequencer::nextChanProgram(int8_t chanId) { void Sequencer::nextChanProgram(int8_t chanId) {
if (chanId > 15) if (static_cast<size_t>(chanId) >= m_chanStates.size()) {
return; return;
}
if (!m_chanStates[chanId]) if (!m_chanStates[chanId]) {
m_chanStates[chanId] = ChannelState(*this, chanId); m_chanStates[chanId] = ChannelState(*this, chanId);
}
return m_chanStates[chanId].nextProgram(); return m_chanStates[chanId].nextProgram();
} }
void Sequencer::prevChanProgram(int8_t chanId) { void Sequencer::prevChanProgram(int8_t chanId) {
if (chanId > 15) if (static_cast<size_t>(chanId) >= m_chanStates.size()) {
return; return;
}
if (!m_chanStates[chanId]) if (!m_chanStates[chanId]) {
m_chanStates[chanId] = ChannelState(*this, chanId); m_chanStates[chanId] = ChannelState(*this, chanId);
}
return m_chanStates[chanId].prevProgram(); return m_chanStates[chanId].prevProgram();
} }