FastLED 3.9.7
Loading...
Searching...
No Matches
MIDI.h
Go to the documentation of this file.
1
28#pragma once
29
30#include "midi_Defs.h"
31#include "midi_Platform.h"
32#include "midi_Settings.h"
33#include "midi_Message.h"
34
35#include "serialMIDI.h"
36
37// -----------------------------------------------------------------------------
38
39BEGIN_MIDI_NAMESPACE
40
41#define MIDI_LIBRARY_VERSION 0x050000
42#define MIDI_LIBRARY_VERSION_MAJOR 5
43#define MIDI_LIBRARY_VERSION_MINOR 0
44#define MIDI_LIBRARY_VERSION_PATCH 0
45
46
53template<class Transport, class _Settings = DefaultSettings, class _Platform = DefaultPlatform>
55{
56public:
57 typedef _Settings Settings;
58 typedef _Platform Platform;
60
61public:
62 inline MidiInterface(Transport&);
63 inline ~MidiInterface();
64
65public:
66 void begin(Channel inChannel = 1);
67
68 // -------------------------------------------------------------------------
69 // MIDI Output
70
71public:
72 inline void sendNoteOn(DataByte inNoteNumber,
73 DataByte inVelocity,
74 Channel inChannel);
75
76 inline void sendNoteOff(DataByte inNoteNumber,
77 DataByte inVelocity,
78 Channel inChannel);
79
80 inline void sendProgramChange(DataByte inProgramNumber,
81 Channel inChannel);
82
83 inline void sendControlChange(DataByte inControlNumber,
84 DataByte inControlValue,
85 Channel inChannel);
86
87 inline void sendPitchBend(int inPitchValue, Channel inChannel);
88 inline void sendPitchBend(double inPitchValue, Channel inChannel);
89
90 inline void sendPolyPressure(DataByte inNoteNumber,
91 DataByte inPressure,
92 Channel inChannel) __attribute__ ((deprecated));
93
94 inline void sendAfterTouch(DataByte inPressure,
95 Channel inChannel);
96 inline void sendAfterTouch(DataByte inNoteNumber,
97 DataByte inPressure,
98 Channel inChannel);
99
100 inline void sendSysEx(unsigned inLength,
101 const byte* inArray,
102 bool inArrayContainsBoundaries = false);
103
104 inline void sendTimeCodeQuarterFrame(DataByte inTypeNibble,
105 DataByte inValuesNibble);
106 inline void sendTimeCodeQuarterFrame(DataByte inData);
107
108 inline void sendSongPosition(unsigned inBeats);
109 inline void sendSongSelect(DataByte inSongNumber);
110 inline void sendTuneRequest();
111
112 inline void sendCommon(MidiType inType, unsigned = 0);
113
114 inline void sendClock() { sendRealTime(Clock); };
115 inline void sendStart() { sendRealTime(Start); };
116 inline void sendStop() { sendRealTime(Stop); };
117 inline void sendTick() { sendRealTime(Tick); };
118 inline void sendContinue() { sendRealTime(Continue); };
119 inline void sendActiveSensing() { sendRealTime(ActiveSensing); };
120 inline void sendSystemReset() { sendRealTime(SystemReset); };
121
122 inline void sendRealTime(MidiType inType);
123
124 inline void beginRpn(unsigned inNumber,
125 Channel inChannel);
126 inline void sendRpnValue(unsigned inValue,
127 Channel inChannel);
128 inline void sendRpnValue(byte inMsb,
129 byte inLsb,
130 Channel inChannel);
131 inline void sendRpnIncrement(byte inAmount,
132 Channel inChannel);
133 inline void sendRpnDecrement(byte inAmount,
134 Channel inChannel);
135 inline void endRpn(Channel inChannel);
136
137 inline void beginNrpn(unsigned inNumber,
138 Channel inChannel);
139 inline void sendNrpnValue(unsigned inValue,
140 Channel inChannel);
141 inline void sendNrpnValue(byte inMsb,
142 byte inLsb,
143 Channel inChannel);
144 inline void sendNrpnIncrement(byte inAmount,
145 Channel inChannel);
146 inline void sendNrpnDecrement(byte inAmount,
147 Channel inChannel);
148 inline void endNrpn(Channel inChannel);
149
150 inline void send(const MidiMessage&);
151
152public:
153 void send(MidiType inType,
154 DataByte inData1,
155 DataByte inData2,
156 Channel inChannel);
157
158 // -------------------------------------------------------------------------
159 // MIDI Input
160
161public:
162 inline bool read();
163 inline bool read(Channel inChannel);
164
165public:
166 inline MidiType getType() const;
167 inline Channel getChannel() const;
168 inline DataByte getData1() const;
169 inline DataByte getData2() const;
170 inline const byte* getSysExArray() const;
171 inline unsigned getSysExArrayLength() const;
172 inline bool check() const;
173
174public:
175 inline Channel getInputChannel() const;
176 inline void setInputChannel(Channel inChannel);
177
178public:
179 static inline MidiType getTypeFromStatusByte(byte inStatus);
180 static inline Channel getChannelFromStatusByte(byte inStatus);
181 static inline bool isChannelMessage(MidiType inType);
182
183 // -------------------------------------------------------------------------
184 // Input Callbacks
185
186public:
187 inline void setHandleMessage(void (*fptr)(const MidiMessage&)) { mMessageCallback = fptr; };
188 inline void setHandleError(ErrorCallback fptr) { mErrorCallback = fptr; }
189 inline void setHandleNoteOff(NoteOffCallback fptr) { mNoteOffCallback = fptr; }
190 inline void setHandleNoteOn(NoteOnCallback fptr) { mNoteOnCallback = fptr; }
191 inline void setHandleAfterTouchPoly(AfterTouchPolyCallback fptr) { mAfterTouchPolyCallback = fptr; }
192 inline void setHandleControlChange(ControlChangeCallback fptr) { mControlChangeCallback = fptr; }
193 inline void setHandleProgramChange(ProgramChangeCallback fptr) { mProgramChangeCallback = fptr; }
194 inline void setHandleAfterTouchChannel(AfterTouchChannelCallback fptr) { mAfterTouchChannelCallback = fptr; }
195 inline void setHandlePitchBend(PitchBendCallback fptr) { mPitchBendCallback = fptr; }
196 inline void setHandleSystemExclusive(SystemExclusiveCallback fptr) { mSystemExclusiveCallback = fptr; }
197 inline void setHandleTimeCodeQuarterFrame(TimeCodeQuarterFrameCallback fptr) { mTimeCodeQuarterFrameCallback = fptr; }
198 inline void setHandleSongPosition(SongPositionCallback fptr) { mSongPositionCallback = fptr; }
199 inline void setHandleSongSelect(SongSelectCallback fptr) { mSongSelectCallback = fptr; }
200 inline void setHandleTuneRequest(TuneRequestCallback fptr) { mTuneRequestCallback = fptr; }
201 inline void setHandleClock(ClockCallback fptr) { mClockCallback = fptr; }
202 inline void setHandleStart(StartCallback fptr) { mStartCallback = fptr; }
203 inline void setHandleTick(TickCallback fptr) { mTickCallback = fptr; }
204 inline void setHandleContinue(ContinueCallback fptr) { mContinueCallback = fptr; }
205 inline void setHandleStop(StopCallback fptr) { mStopCallback = fptr; }
206 inline void setHandleActiveSensing(ActiveSensingCallback fptr) { mActiveSensingCallback = fptr; }
207 inline void setHandleSystemReset(SystemResetCallback fptr) { mSystemResetCallback = fptr; }
208
209 inline void disconnectCallbackFromType(MidiType inType);
210
211private:
212 void launchCallback();
213
214 void (*mMessageCallback)(const MidiMessage& message) = nullptr;
215 ErrorCallback mErrorCallback = nullptr;
216 NoteOffCallback mNoteOffCallback = nullptr;
217 NoteOnCallback mNoteOnCallback = nullptr;
218 AfterTouchPolyCallback mAfterTouchPolyCallback = nullptr;
219 ControlChangeCallback mControlChangeCallback = nullptr;
220 ProgramChangeCallback mProgramChangeCallback = nullptr;
221 AfterTouchChannelCallback mAfterTouchChannelCallback = nullptr;
222 PitchBendCallback mPitchBendCallback = nullptr;
223 SystemExclusiveCallback mSystemExclusiveCallback = nullptr;
224 TimeCodeQuarterFrameCallback mTimeCodeQuarterFrameCallback = nullptr;
225 SongPositionCallback mSongPositionCallback = nullptr;
226 SongSelectCallback mSongSelectCallback = nullptr;
227 TuneRequestCallback mTuneRequestCallback = nullptr;
228 ClockCallback mClockCallback = nullptr;
229 StartCallback mStartCallback = nullptr;
230 TickCallback mTickCallback = nullptr;
231 ContinueCallback mContinueCallback = nullptr;
232 StopCallback mStopCallback = nullptr;
233 ActiveSensingCallback mActiveSensingCallback = nullptr;
234 SystemResetCallback mSystemResetCallback = nullptr;
235
236 // -------------------------------------------------------------------------
237 // MIDI Soft Thru
238
239public:
240 inline Thru::Mode getFilterMode() const;
241 inline bool getThruState() const;
242
243 inline void turnThruOn(Thru::Mode inThruFilterMode = Thru::Full);
244 inline void turnThruOff();
245 inline void setThruFilterMode(Thru::Mode inThruFilterMode);
246
247private:
248 void thruFilter(byte inChannel);
249
250 // -------------------------------------------------------------------------
251 // MIDI Parsing
252
253private:
254 bool parse();
255 inline void handleNullVelocityNoteOnAsNoteOff();
256 inline bool inputFilter(Channel inChannel);
257 inline void resetInput();
258 inline void UpdateLastSentTime();
259
260 // -------------------------------------------------------------------------
261 // Transport
262
263public:
264 Transport* getTransport() { return &mTransport; };
265
266private:
267 Transport& mTransport;
268
269 // -------------------------------------------------------------------------
270 // Internal variables
271
272private:
273 Channel mInputChannel;
274 StatusByte mRunningStatus_RX;
275 StatusByte mRunningStatus_TX;
276 byte mPendingMessage[3];
277 unsigned mPendingMessageExpectedLength;
278 unsigned mPendingMessageIndex;
279 unsigned mCurrentRpnNumber;
280 unsigned mCurrentNrpnNumber;
281 bool mThruActivated : 1;
282 Thru::Mode mThruFilterMode : 7;
283 MidiMessage mMessage;
284 unsigned long mLastMessageSentTime;
285 unsigned long mLastMessageReceivedTime;
286 unsigned long mSenderActiveSensingPeriodicity;
287 bool mReceiverActiveSensingActivated;
288 int8_t mLastError;
289
290private:
291 inline StatusByte getStatus(MidiType inType,
292 Channel inChannel) const;
293};
294
295// -----------------------------------------------------------------------------
296
297unsigned encodeSysEx(const byte* inData,
298 byte* outSysEx,
299 unsigned inLength,
300 bool inFlipHeaderBits = false);
301unsigned decodeSysEx(const byte* inSysEx,
302 byte* outData,
303 unsigned inLength,
304 bool inFlipHeaderBits = false);
305
306END_MIDI_NAMESPACE
307
308#include "MIDI.hpp"
unsigned encodeSysEx(const byte *inData, byte *outSysEx, unsigned inLength, bool inFlipHeaderBits=false)
Encode System Exclusive messages. SysEx messages are encoded to guarantee transmission of data bytes ...
Definition MIDI.cpp:46
unsigned decodeSysEx(const byte *inSysEx, byte *outData, unsigned inLength, bool inFlipHeaderBits=false)
Decode System Exclusive messages. SysEx messages are encoded to guarantee transmission of data bytes ...
Definition MIDI.cpp:87
MIDI Library for the Arduino - Inline implementations.
The main class for MIDI handling. It is templated over the type of serial port to provide abstraction...
Definition MIDI.h:55
~MidiInterface()
Destructor for MidiInterface.
Definition MIDI.hpp:59
MidiInterface(Transport &)
Constructor for MidiInterface.
Definition MIDI.hpp:34
void begin(Channel inChannel=1)
Call the begin method in the setup() function of the Arduino.
Definition MIDI.hpp:72
bool check() const
Check if a valid message is stored in the structure.
Definition MIDI.hpp:1187
unsigned getSysExArrayLength() const
Get the length of the System Exclusive array.
Definition MIDI.hpp:1180
Channel getChannel() const
Get the channel of the message stored in the structure.
Definition MIDI.hpp:1145
static Channel getChannelFromStatusByte(byte inStatus)
Returns channel in the range 1-16.
Definition MIDI.hpp:1236
static MidiType getTypeFromStatusByte(byte inStatus)
Extract an enumerated MIDI type from a status byte.
Definition MIDI.hpp:1218
DataByte getData1() const
Get the first data byte of the last received message.
Definition MIDI.hpp:1152
MidiType getType() const
Get the last received message's type.
Definition MIDI.hpp:1134
DataByte getData2() const
Get the second data byte of the last received message.
Definition MIDI.hpp:1159
const byte * getSysExArray() const
Get the System Exclusive byte array.
Definition MIDI.hpp:1169
void disconnectCallbackFromType(MidiType inType)
Detach an external function from the given type.
Definition MIDI.hpp:1262
bool read()
Read messages from the serial port using the main input channel.
Definition MIDI.hpp:704
void setInputChannel(Channel inChannel)
Set the value for the input MIDI channel.
Definition MIDI.hpp:1205
void sendAfterTouch(DataByte inPressure, Channel inChannel)
Send a MonoPhonic AfterTouch message (applies to all notes)
Definition MIDI.hpp:297
void endRpn(Channel inChannel)
Terminate an RPN frame. This will send a Null Function to deselect the currently selected RPN.
Definition MIDI.hpp:589
void sendPitchBend(int inPitchValue, Channel inChannel)
Send a Pitch Bend message using a signed integer value.
Definition MIDI.hpp:324
void sendTimeCodeQuarterFrame(DataByte inTypeNibble, DataByte inValuesNibble)
Send a MIDI Time Code Quarter Frame.
Definition MIDI.hpp:400
void endNrpn(Channel inChannel)
Terminate an NRPN frame. This will send a Null Function to deselect the currently selected NRPN.
Definition MIDI.hpp:669
void sendNoteOn(DataByte inNoteNumber, DataByte inVelocity, Channel inChannel)
Send a Note On message.
Definition MIDI.hpp:226
void sendTuneRequest()
Send a Tune Request message.
Definition MIDI.hpp:388
void sendControlChange(DataByte inControlNumber, DataByte inControlValue, Channel inChannel)
Send a Control Change message.
Definition MIDI.hpp:270
void send(const MidiMessage &)
Send a MIDI message.
Definition MIDI.hpp:118
void sendProgramChange(DataByte inProgramNumber, Channel inChannel)
Send a Program Change message.
Definition MIDI.hpp:257
void beginNrpn(unsigned inNumber, Channel inChannel)
Start a Non-Registered Parameter Number frame.
Definition MIDI.hpp:603
void beginRpn(unsigned inNumber, Channel inChannel)
Start a Registered Parameter Number frame.
Definition MIDI.hpp:523
void sendSongPosition(unsigned inBeats)
Send a Song Position Pointer message.
Definition MIDI.hpp:423
void sendPolyPressure(DataByte inNoteNumber, DataByte inPressure, Channel inChannel)
Send a Polyphonic AfterTouch message (applies to a specified note)
Definition MIDI.hpp:285
void sendSongSelect(DataByte inSongNumber)
Send a Song Select message.
Definition MIDI.hpp:430
void sendCommon(MidiType inType, unsigned=0)
Send a Common message. Common messages reset the running status.
Definition MIDI.hpp:443
void sendRpnValue(unsigned inValue, Channel inChannel)
Send a 14-bit value for the currently selected RPN number.
Definition MIDI.hpp:541
void sendRealTime(MidiType inType)
Send a Real Time (one byte) message.
Definition MIDI.hpp:492
void sendSysEx(unsigned inLength, const byte *inArray, bool inArrayContainsBoundaries=false)
Generate and send a System Exclusive frame.
Definition MIDI.hpp:357
void sendNoteOff(DataByte inNoteNumber, DataByte inVelocity, Channel inChannel)
Send a Note Off message.
Definition MIDI.hpp:245
void sendNrpnValue(unsigned inValue, Channel inChannel)
Send a 14-bit value for the currently selected NRPN number.
Definition MIDI.hpp:621
void setThruFilterMode(Thru::Mode inThruFilterMode)
Set the filter for thru mirroring.
Definition MIDI.hpp:1352
MIDI Library for the Arduino - Definitions.
MidiType
Definition midi_Defs.h:95
@ Continue
System Real Time - Continue.
Definition midi_Defs.h:117
@ Clock
System Real Time - Timing Clock.
Definition midi_Defs.h:113
@ ActiveSensing
System Real Time - Active Sensing.
Definition midi_Defs.h:120
@ SystemReset
System Real Time - System Reset.
Definition midi_Defs.h:121
@ Tick
System Real Time - Timing Tick (1 tick = 10 milliseconds)
Definition midi_Defs.h:115
@ Start
System Real Time - Start.
Definition midi_Defs.h:116
@ Stop
System Real Time - Stop.
Definition midi_Defs.h:118
MIDI Library for the Arduino - Message struct definition.
MIDI Library for the Arduino - Platform @license MIT - Copyright (c) 2015 Francois Best.
MIDI Library for the Arduino - Settings.
MIDI Library for the Arduino - Platform @license MIT - Copyright (c) 2015 Francois Best.
@ Full
Fully enabled Thru (every incoming message is sent back).
Definition midi_Defs.h:132