-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathRPN_NRPN.ino
208 lines (181 loc) · 5.52 KB
/
RPN_NRPN.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#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.
If you'd like to go further, have a look at this thread:
https://github.com/FortySevenEffects/arduino_midi_library/issues/60
*/
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_NAMESPACE::DataIncrement:
if (mSelected)
{
Value& currentValue = getCurrentValue();
currentValue += inValue;
return true;
}
break;
case MIDI_NAMESPACE::DataDecrement:
if (mSelected)
{
Value& currentValue = getCurrentValue();
currentValue -= inValue;
return true;
}
break;
case MIDI_NAMESPACE::DataEntryMSB:
if (mSelected)
{
Value& currentValue = getCurrentValue();
currentValue.mMsb = inValue;
currentValue.mLsb = 0;
return true;
}
break;
case MIDI_NAMESPACE::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_NAMESPACE::RPNMSB, MIDI_NAMESPACE::RPNLSB> RpnParser;
typedef ParameterNumberParser<NrpnState, MIDI_NAMESPACE::NRPNMSB, MIDI_NAMESPACE::NRPNLSB> NrpnParser;
struct ChannelSetup
{
inline ChannelSetup()
: mRpnParser(mRpnState)
, mNrpnParser(mNrpnState)
{
}
inline void reset()
{
mRpnParser.reset();
mNrpnParser.reset();
}
inline void setup()
{
mRpnState.enable(MIDI_NAMESPACE::RPN::PitchBendSensitivity);
mRpnState.enable(MIDI_NAMESPACE::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_NAMESPACE::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_NAMESPACE::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_NAMESPACE::Channel channel = 1;
const byte semitones = 12;
const byte cents = 42;
MIDI.beginRpn(MIDI_NAMESPACE::RPN::PitchBendSensitivity, channel);
MIDI.sendRpnValue(semitones, cents, channel);
MIDI.endRpn(channel);
}
}