Move the methods of MidiState to their own file
This commit is contained in:
parent
17bfcf2567
commit
1fe8932851
3 changed files with 141 additions and 83 deletions
|
|
@ -6,6 +6,7 @@ set (SFIZZ_SOURCES
|
||||||
sfizz/Region.cpp
|
sfizz/Region.cpp
|
||||||
sfizz/Voice.cpp
|
sfizz/Voice.cpp
|
||||||
sfizz/ScopedFTZ.cpp
|
sfizz/ScopedFTZ.cpp
|
||||||
|
sfizz/MidiState.cpp
|
||||||
sfizz/SfzHelpers.cpp
|
sfizz/SfzHelpers.cpp
|
||||||
sfizz/FloatEnvelopes.cpp
|
sfizz/FloatEnvelopes.cpp
|
||||||
)
|
)
|
||||||
|
|
|
||||||
94
src/sfizz/MidiState.cpp
Normal file
94
src/sfizz/MidiState.cpp
Normal file
|
|
@ -0,0 +1,94 @@
|
||||||
|
#include "MidiState.h"
|
||||||
|
#include "Debug.h"
|
||||||
|
|
||||||
|
sfz::MidiState::MidiState()
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void sfz::MidiState::noteOnEvent(int channel, int noteNumber, uint8_t velocity) noexcept
|
||||||
|
{
|
||||||
|
ASSERT(channel >= 1 && channel <= 16);
|
||||||
|
ASSERT(noteNumber >= 0 && noteNumber <= 127);
|
||||||
|
ASSERT(velocity >= 0 && velocity <= 127);
|
||||||
|
channel = translateSfzChannelToMidi(channel);
|
||||||
|
if (noteNumber >= 0 && noteNumber < 128) {
|
||||||
|
lastNoteVelocities[channel][noteNumber] = velocity;
|
||||||
|
noteOnTimes[channel][noteNumber] = std::chrono::steady_clock::now();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float sfz::MidiState::getNoteDuration(int channel, int noteNumber) const
|
||||||
|
{
|
||||||
|
ASSERT(channel >= 1 && channel <= 16);
|
||||||
|
ASSERT(noteNumber >= 0 && noteNumber <= 127);
|
||||||
|
channel = translateSfzChannelToMidi(channel);
|
||||||
|
if (noteNumber >= 0 && noteNumber < 128) {
|
||||||
|
const auto noteOffTime = std::chrono::steady_clock::now();
|
||||||
|
const auto duration = std::chrono::duration_cast<std::chrono::duration<float>>(noteOffTime - noteOnTimes[channel][noteNumber]);
|
||||||
|
return duration.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t sfz::MidiState::getNoteVelocity(int channel, int noteNumber) const noexcept
|
||||||
|
{
|
||||||
|
ASSERT(channel >= 1 && channel <= 16);
|
||||||
|
ASSERT(noteNumber >= 0 && noteNumber <= 127);
|
||||||
|
channel = translateSfzChannelToMidi(channel);
|
||||||
|
return lastNoteVelocities[channel][noteNumber];
|
||||||
|
}
|
||||||
|
|
||||||
|
void sfz::MidiState::pitchBendEvent(int channel, int pitchBendValue) noexcept
|
||||||
|
{
|
||||||
|
ASSERT(channel >= 1 && channel <= 16);
|
||||||
|
ASSERT(pitchBendValue >= -8192 && pitchBendValue <= 8192);
|
||||||
|
channel = translateSfzChannelToMidi(channel);
|
||||||
|
pitchBends[channel] = pitchBendValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sfz::MidiState::getPitchBend(int channel) const noexcept
|
||||||
|
{
|
||||||
|
ASSERT(channel >= 1 && channel <= 16);
|
||||||
|
channel = translateSfzChannelToMidi(channel);
|
||||||
|
return pitchBends[channel];
|
||||||
|
}
|
||||||
|
|
||||||
|
void sfz::MidiState::ccEvent(int channel, int ccNumber, uint8_t ccValue) noexcept
|
||||||
|
{
|
||||||
|
ASSERT(channel >= 1 && channel <= 16);
|
||||||
|
ASSERT(ccNumber >= 0 && ccNumber <= 127);
|
||||||
|
ASSERT(ccValue >= 0 && ccValue <= 127);
|
||||||
|
channel = translateSfzChannelToMidi(channel);
|
||||||
|
cc[channel][ccNumber] = ccValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t sfz::MidiState::getCCValue(int channel, int ccNumber) const noexcept
|
||||||
|
{
|
||||||
|
ASSERT(channel >= 1 && channel <= 16);
|
||||||
|
ASSERT(ccNumber >= 0 && ccNumber <= 127);
|
||||||
|
channel = translateSfzChannelToMidi(channel);
|
||||||
|
return cc[channel][ccNumber];
|
||||||
|
}
|
||||||
|
|
||||||
|
const sfz::CCValueArray& sfz::MidiState::getCCArray(int channel) const noexcept
|
||||||
|
{
|
||||||
|
ASSERT(channel >= 1 && channel <= 16);
|
||||||
|
channel = translateSfzChannelToMidi(channel);
|
||||||
|
return cc[channel];
|
||||||
|
}
|
||||||
|
|
||||||
|
void sfz::MidiState::reset() noexcept
|
||||||
|
{
|
||||||
|
for (auto& channelArray: lastNoteVelocities)
|
||||||
|
for (auto& velocity: channelArray)
|
||||||
|
velocity = 0;
|
||||||
|
|
||||||
|
for (auto& channelArray: cc)
|
||||||
|
for (auto& ccValue: channelArray)
|
||||||
|
ccValue = 0;
|
||||||
|
|
||||||
|
for (auto& bendValue: pitchBends)
|
||||||
|
bendValue = 0;
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include "SfzHelpers.h"
|
#include "SfzHelpers.h"
|
||||||
#include "Debug.h"
|
|
||||||
|
|
||||||
namespace sfz
|
namespace sfz
|
||||||
{
|
{
|
||||||
|
|
@ -15,10 +14,7 @@ namespace sfz
|
||||||
class MidiState
|
class MidiState
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MidiState() noexcept
|
MidiState();
|
||||||
{
|
|
||||||
reset();
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Update the state after a note on event
|
* @brief Update the state after a note on event
|
||||||
*
|
*
|
||||||
|
|
@ -26,17 +22,7 @@ public:
|
||||||
* @param noteNumber
|
* @param noteNumber
|
||||||
* @param velocity
|
* @param velocity
|
||||||
*/
|
*/
|
||||||
void noteOnEvent(int channel, int noteNumber, uint8_t velocity) noexcept
|
void noteOnEvent(int channel, int noteNumber, uint8_t velocity) noexcept;
|
||||||
{
|
|
||||||
ASSERT(channel >= 1 && channel <= 16);
|
|
||||||
ASSERT(noteNumber >= 0 && noteNumber <= 127);
|
|
||||||
ASSERT(velocity >= 0 && velocity <= 127);
|
|
||||||
channel = translateSfzChannelToMidi(channel);
|
|
||||||
if (noteNumber >= 0 && noteNumber < 128) {
|
|
||||||
lastNoteVelocities[channel][noteNumber] = velocity;
|
|
||||||
noteOnTimes[channel][noteNumber] = std::chrono::steady_clock::now();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Register a note off and get the note duration
|
* @brief Register a note off and get the note duration
|
||||||
|
|
@ -45,19 +31,7 @@ public:
|
||||||
* @param noteNumber
|
* @param noteNumber
|
||||||
* @return float
|
* @return float
|
||||||
*/
|
*/
|
||||||
float getNoteDuration(int channel, int noteNumber) const
|
float getNoteDuration(int channel, int noteNumber) const;
|
||||||
{
|
|
||||||
ASSERT(channel >= 1 && channel <= 16);
|
|
||||||
ASSERT(noteNumber >= 0 && noteNumber <= 127);
|
|
||||||
channel = translateSfzChannelToMidi(channel);
|
|
||||||
if (noteNumber >= 0 && noteNumber < 128) {
|
|
||||||
const auto noteOffTime = std::chrono::steady_clock::now();
|
|
||||||
const auto duration = std::chrono::duration_cast<std::chrono::duration<float>>(noteOffTime - noteOnTimes[channel][noteNumber]);
|
|
||||||
return duration.count();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the note on velocity for a given note
|
* @brief Get the note on velocity for a given note
|
||||||
|
|
@ -66,66 +40,55 @@ public:
|
||||||
* @param noteNumber
|
* @param noteNumber
|
||||||
* @return uint8_t
|
* @return uint8_t
|
||||||
*/
|
*/
|
||||||
uint8_t getNoteVelocity(int channel, int noteNumber) const noexcept
|
uint8_t getNoteVelocity(int channel, int noteNumber) const noexcept;
|
||||||
{
|
|
||||||
ASSERT(channel >= 1 && channel <= 16);
|
|
||||||
ASSERT(noteNumber >= 0 && noteNumber <= 127);
|
|
||||||
channel = translateSfzChannelToMidi(channel);
|
|
||||||
return lastNoteVelocities[channel][noteNumber];
|
|
||||||
}
|
|
||||||
|
|
||||||
void pitchBendEvent(int channel, int pitchBendValue) noexcept
|
/**
|
||||||
{
|
* @brief Register a pitch bend event
|
||||||
ASSERT(channel >= 1 && channel <= 16);
|
*
|
||||||
ASSERT(pitchBendValue >= -8192 && pitchBendValue <= 8192);
|
* @param channel
|
||||||
channel = translateSfzChannelToMidi(channel);
|
* @param pitchBendValue
|
||||||
pitchBends[channel] = pitchBendValue;
|
*/
|
||||||
}
|
void pitchBendEvent(int channel, int pitchBendValue) noexcept;
|
||||||
|
|
||||||
int getPitchBend(int channel) const noexcept
|
/**
|
||||||
{
|
* @brief Get the pitch bend status on a channel
|
||||||
ASSERT(channel >= 1 && channel <= 16);
|
*
|
||||||
channel = translateSfzChannelToMidi(channel);
|
* @param channel
|
||||||
return pitchBends[channel];
|
* @return int
|
||||||
}
|
*/
|
||||||
|
int getPitchBend(int channel) const noexcept;
|
||||||
|
|
||||||
void ccEvent(int channel, int ccNumber, uint8_t ccValue) noexcept
|
/**
|
||||||
{
|
* @brief Register a CC event
|
||||||
ASSERT(channel >= 1 && channel <= 16);
|
*
|
||||||
ASSERT(ccNumber >= 0 && ccNumber <= 127);
|
* @param channel
|
||||||
ASSERT(ccValue >= 0 && ccValue <= 127);
|
* @param ccNumber
|
||||||
channel = translateSfzChannelToMidi(channel);
|
* @param ccValue
|
||||||
cc[channel][ccNumber] = ccValue;
|
*/
|
||||||
}
|
void ccEvent(int channel, int ccNumber, uint8_t ccValue) noexcept;
|
||||||
|
|
||||||
uint8_t getCCValue(int channel, int ccNumber) const noexcept
|
/**
|
||||||
{
|
* @brief Get the CC value for a specific channel and cc number
|
||||||
ASSERT(channel >= 1 && channel <= 16);
|
*
|
||||||
ASSERT(ccNumber >= 0 && ccNumber <= 127);
|
* @param channel
|
||||||
channel = translateSfzChannelToMidi(channel);
|
* @param ccNumber
|
||||||
return cc[channel][ccNumber];
|
* @return uint8_t
|
||||||
}
|
*/
|
||||||
|
uint8_t getCCValue(int channel, int ccNumber) const noexcept;
|
||||||
|
|
||||||
const CCValueArray& getCCArray(int channel) const noexcept
|
/**
|
||||||
{
|
* @brief Get the full CC status for a specific channel
|
||||||
ASSERT(channel >= 1 && channel <= 16);
|
*
|
||||||
channel = translateSfzChannelToMidi(channel);
|
* @param channel
|
||||||
return cc[channel];
|
* @return const CCValueArray&
|
||||||
}
|
*/
|
||||||
|
const CCValueArray& getCCArray(int channel) const noexcept;
|
||||||
|
|
||||||
void reset() noexcept
|
/**
|
||||||
{
|
* @brief Reset the midi state (does not impact the last note on time)
|
||||||
for (auto& channelArray: lastNoteVelocities)
|
*
|
||||||
for (auto& velocity: channelArray)
|
*/
|
||||||
velocity = 0;
|
void reset() noexcept;
|
||||||
|
|
||||||
for (auto& channelArray: cc)
|
|
||||||
for (auto& ccValue: channelArray)
|
|
||||||
ccValue = 0;
|
|
||||||
|
|
||||||
for (auto& bendValue: pitchBends)
|
|
||||||
bendValue = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template<class T>
|
template<class T>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue