<commit_before>#include <MIDI.h>
#include "utility.h"

MIDI_CREATE_DEFAULT_INSTANCE();

/* Listen to RPN & NRPN messages on all channels

The complexity of this example resides in the fact that keeping a state
of all the 16384 * 2 RPN/NRPN values would not fit in memory.
As we're only interested in a few of them, we use a separate state map.
*/

template<class State, byte MsbSelectCCNumber, byte LsbSelectCCNumber>
class ParameterNumberParser
{
public:
    ParameterNumberParser(State& inState)
        : mState(inState)
    {
    }

public:
    inline void reset()
    {
        mState.reset();
        mSelected = false;
        mCurrentNumber = 0;
    }

public:
    bool parseControlChange(byte inNumber, byte inValue)
    {
        switch (inNumber)
        {
            case MsbSelectCCNumber:
                mCurrentNumber.mMsb = inValue;
                break;
            case LsbSelectCCNumber:
                if (inValue == 0x7f && mCurrentNumber.mMsb == 0x7f)
                {
                    // End of Null Function, disable parser.
                    mSelected = false;
                }
                else
                {
                    mCurrentNumber.mLsb = inValue;
                    mSelected = mState.has(mCurrentNumber.as14bits());
                }
                break;

            case midi::DataIncrement:
                if (mSelected)
                {
                    Value& currentValue = getCurrentValue();
                    currentValue += inValue;
                    return true;
                }
                break;
            case midi::DataDecrement:
                if (mSelected)
                {
                    Value& currentValue = getCurrentValue();
                    currentValue -= inValue;
                    return true;
                }
                break;

            case midi::DataEntry:
                if (mSelected)
                {
                    Value& currentValue = getCurrentValue();
                    currentValue.mMsb = inValue;
                    currentValue.mLsb = 0;
                    return true;
                }
                break;
            case midi::DataEntryLSB:
                if (mSelected)
                {
                    Value& currentValue = getCurrentValue();
                    currentValue.mLsb = inValue;
                    return true;
                }
                break;

            default:
                // Not part of the RPN/NRPN workflow, ignoring.
                break;
        }
        return false;
    }

public:
    inline Value& getCurrentValue()
    {
        return mState.get(mCurrentNumber.as14bits());
    }
    inline const Value& getCurrentValue() const
    {
        return mState.get(mCurrentNumber.as14bits());
    }

public:
    State& mState;
    bool mSelected;
    Value mCurrentNumber;
};

// --

typedef State<2> RpnState;  // We'll listen to 2 RPN
typedef State<4> NrpnState; // and 4 NRPN
typedef ParameterNumberParser<RpnState,  midi::RPNMSB,  midi::RPN>  RpnParser;
typedef ParameterNumberParser<NrpnState, midi::NRPNMSB, midi::NRPN> NrpnParser;

struct ChannelSetup
{
    inline ChannelSetup()
        : mRpnParser(mRpnState)
        , mNrpnParser(mNrpnState)
    {
    }

    inline void reset()
    {
        mRpnParser.reset();
        mNrpnParser.reset();
    }
    inline void setup()
    {
        mRpnState.enable(midi::RPN::PitchBendSensitivity);
        mRpnState.enable(midi::RPN::ModulationDepthRange);

        // Enable a few random NRPNs
        mNrpnState.enable(12);
        mNrpnState.enable(42);
        mNrpnState.enable(1234);
        mNrpnState.enable(1176);
    }

    RpnState    mRpnState;
    NrpnState   mNrpnState;
    RpnParser   mRpnParser;
    NrpnParser  mNrpnParser;
};

ChannelSetup sChannelSetup[16];

// --

void handleControlChange(byte inChannel, byte inNumber, byte inValue)
{
    ChannelSetup& channel = sChannelSetup[inChannel];

    if (channel.mRpnParser.parseControlChange(inNumber, inValue))
    {
        const Value& value    = channel.mRpnParser.getCurrentValue();
        const unsigned number = channel.mRpnParser.mCurrentNumber.as14bits();

        if (number == midi::RPN::PitchBendSensitivity)
        {
            // Here, we use the LSB and MSB separately as they have different meaning.
            const byte semitones    = value.mMsb;
            const byte cents        = value.mLsb;
        }
        else if (number == midi::RPN::ModulationDepthRange)
        {
            // But here, we want the full 14 bit value.
            const unsigned range = value.as14bits();
        }
    }
    else if (channel.mRpnParser.parseControlChange(inNumber, inValue))
    {
        // You get the idea..
    }
}

// --

void setup()
{
    for (int i = 0; i < 16; ++i)
    {
        ChannelSetup& channel = sChannelSetup[i];
        channel.reset();
        channel.setup();
    }
    MIDI.setHandleControlChange(handleControlChange);
    MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
    MIDI.read();
}<commit_msg>Added example of RPN sequence.<commit_after>#include <MIDI.h>
#include "utility.h"

MIDI_CREATE_DEFAULT_INSTANCE();

/* Listen to RPN & NRPN messages on all channels

The complexity of this example resides in the fact that keeping a state
of all the 16384 * 2 RPN/NRPN values would not fit in memory.
As we're only interested in a few of them, we use a separate state map.
*/

template<class State, byte MsbSelectCCNumber, byte LsbSelectCCNumber>
class ParameterNumberParser
{
public:
    ParameterNumberParser(State& inState)
        : mState(inState)
    {
    }

public:
    inline void reset()
    {
        mState.reset();
        mSelected = false;
        mCurrentNumber = 0;
    }

public:
    bool parseControlChange(byte inNumber, byte inValue)
    {
        switch (inNumber)
        {
            case MsbSelectCCNumber:
                mCurrentNumber.mMsb = inValue;
                break;
            case LsbSelectCCNumber:
                if (inValue == 0x7f && mCurrentNumber.mMsb == 0x7f)
                {
                    // End of Null Function, disable parser.
                    mSelected = false;
                }
                else
                {
                    mCurrentNumber.mLsb = inValue;
                    mSelected = mState.has(mCurrentNumber.as14bits());
                }
                break;

            case midi::DataIncrement:
                if (mSelected)
                {
                    Value& currentValue = getCurrentValue();
                    currentValue += inValue;
                    return true;
                }
                break;
            case midi::DataDecrement:
                if (mSelected)
                {
                    Value& currentValue = getCurrentValue();
                    currentValue -= inValue;
                    return true;
                }
                break;

            case midi::DataEntry:
                if (mSelected)
                {
                    Value& currentValue = getCurrentValue();
                    currentValue.mMsb = inValue;
                    currentValue.mLsb = 0;
                    return true;
                }
                break;
            case midi::DataEntryLSB:
                if (mSelected)
                {
                    Value& currentValue = getCurrentValue();
                    currentValue.mLsb = inValue;
                    return true;
                }
                break;

            default:
                // Not part of the RPN/NRPN workflow, ignoring.
                break;
        }
        return false;
    }

public:
    inline Value& getCurrentValue()
    {
        return mState.get(mCurrentNumber.as14bits());
    }
    inline const Value& getCurrentValue() const
    {
        return mState.get(mCurrentNumber.as14bits());
    }

public:
    State& mState;
    bool mSelected;
    Value mCurrentNumber;
};

// --

typedef State<2> RpnState;  // We'll listen to 2 RPN
typedef State<4> NrpnState; // and 4 NRPN
typedef ParameterNumberParser<RpnState,  midi::RPNMSB,  midi::RPN>  RpnParser;
typedef ParameterNumberParser<NrpnState, midi::NRPNMSB, midi::NRPN> NrpnParser;

struct ChannelSetup
{
    inline ChannelSetup()
        : mRpnParser(mRpnState)
        , mNrpnParser(mNrpnState)
    {
    }

    inline void reset()
    {
        mRpnParser.reset();
        mNrpnParser.reset();
    }
    inline void setup()
    {
        mRpnState.enable(midi::RPN::PitchBendSensitivity);
        mRpnState.enable(midi::RPN::ModulationDepthRange);

        // Enable a few random NRPNs
        mNrpnState.enable(12);
        mNrpnState.enable(42);
        mNrpnState.enable(1234);
        mNrpnState.enable(1176);
    }

    RpnState    mRpnState;
    NrpnState   mNrpnState;
    RpnParser   mRpnParser;
    NrpnParser  mNrpnParser;
};

ChannelSetup sChannelSetup[16];

// --

void handleControlChange(byte inChannel, byte inNumber, byte inValue)
{
    ChannelSetup& channel = sChannelSetup[inChannel];

    if (channel.mRpnParser.parseControlChange(inNumber, inValue))
    {
        const Value& value    = channel.mRpnParser.getCurrentValue();
        const unsigned number = channel.mRpnParser.mCurrentNumber.as14bits();

        if (number == midi::RPN::PitchBendSensitivity)
        {
            // Here, we use the LSB and MSB separately as they have different meaning.
            const byte semitones    = value.mMsb;
            const byte cents        = value.mLsb;
        }
        else if (number == midi::RPN::ModulationDepthRange)
        {
            // But here, we want the full 14 bit value.
            const unsigned range = value.as14bits();
        }
    }
    else if (channel.mRpnParser.parseControlChange(inNumber, inValue))
    {
        // You get the idea..
    }
}

// --

void setup()
{
    for (int i = 0; i < 16; ++i)
    {
        ChannelSetup& channel = sChannelSetup[i];
        channel.reset();
        channel.setup();
    }
    MIDI.setHandleControlChange(handleControlChange);
    MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
    MIDI.read();

    // Send a RPN sequence (Pitch Bend sensitivity) on channel 1
    {
        const midi::Channel channel = 1;
        const byte semitones = 12;
        const byte cents     = 42;
        MIDI.startRpn(midi::PitchBendSensitivity, channel);
        MIDI.sendRpnValue(semitones, cents, channel);
        MIDI.endRpn(channel);
    }
}
{'fraction_non_alphanumeric': 0.08893129770992367, 'fraction_numerical': 0.00916030534351145, 'mean_word_length': 1.6493933265925178, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4032753', 'n_tokens_mistral': 3195, 'n_tokens_neox': 2990, 'n_words': 706}