Arduino MIDI Library  Version 4.3.1
Go to the documentation of this file.
28 #pragma once
30 #include "midi_Defs.h"
31 #include "midi_Settings.h"
32 #include "midi_Message.h"
34 // -----------------------------------------------------------------------------
44 template<class SerialPort, class _Settings = DefaultSettings>
46 {
47 public:
48  typedef _Settings Settings;
50 public:
51  inline MidiInterface(SerialPort& inSerial);
52  inline ~MidiInterface();
54 public:
55  void begin(Channel inChannel = 1);
57  // -------------------------------------------------------------------------
58  // MIDI Output
60 public:
61  inline void sendNoteOn(DataByte inNoteNumber,
62  DataByte inVelocity,
63  Channel inChannel);
65  inline void sendNoteOff(DataByte inNoteNumber,
66  DataByte inVelocity,
67  Channel inChannel);
69  inline void sendProgramChange(DataByte inProgramNumber,
70  Channel inChannel);
72  inline void sendControlChange(DataByte inControlNumber,
73  DataByte inControlValue,
74  Channel inChannel);
76  inline void sendPitchBend(int inPitchValue, Channel inChannel);
77  inline void sendPitchBend(double inPitchValue, Channel inChannel);
79  inline void sendPolyPressure(DataByte inNoteNumber,
80  DataByte inPressure,
81  Channel inChannel) __attribute__ ((deprecated));
83  inline void sendAfterTouch(DataByte inPressure,
84  Channel inChannel);
85  inline void sendAfterTouch(DataByte inNoteNumber,
86  DataByte inPressure,
87  Channel inChannel);
89  inline void sendSysEx(unsigned inLength,
90  const byte* inArray,
91  bool inArrayContainsBoundaries = false);
93  inline void sendTimeCodeQuarterFrame(DataByte inTypeNibble,
94  DataByte inValuesNibble);
95  inline void sendTimeCodeQuarterFrame(DataByte inData);
97  inline void sendSongPosition(unsigned inBeats);
98  inline void sendSongSelect(DataByte inSongNumber);
99  inline void sendTuneRequest();
100  inline void sendRealTime(MidiType inType);
102  inline void beginRpn(unsigned inNumber,
103  Channel inChannel);
104  inline void sendRpnValue(unsigned inValue,
105  Channel inChannel);
106  inline void sendRpnValue(byte inMsb,
107  byte inLsb,
108  Channel inChannel);
109  inline void sendRpnIncrement(byte inAmount,
110  Channel inChannel);
111  inline void sendRpnDecrement(byte inAmount,
112  Channel inChannel);
113  inline void endRpn(Channel inChannel);
115  inline void beginNrpn(unsigned inNumber,
116  Channel inChannel);
117  inline void sendNrpnValue(unsigned inValue,
118  Channel inChannel);
119  inline void sendNrpnValue(byte inMsb,
120  byte inLsb,
121  Channel inChannel);
122  inline void sendNrpnIncrement(byte inAmount,
123  Channel inChannel);
124  inline void sendNrpnDecrement(byte inAmount,
125  Channel inChannel);
126  inline void endNrpn(Channel inChannel);
128 public:
129  void send(MidiType inType,
130  DataByte inData1,
131  DataByte inData2,
132  Channel inChannel);
134  // -------------------------------------------------------------------------
135  // MIDI Input
137 public:
138  inline bool read();
139  inline bool read(Channel inChannel);
141 public:
142  inline MidiType getType() const;
143  inline Channel getChannel() const;
144  inline DataByte getData1() const;
145  inline DataByte getData2() const;
146  inline const byte* getSysExArray() const;
147  inline unsigned getSysExArrayLength() const;
148  inline bool check() const;
150 public:
151  inline Channel getInputChannel() const;
152  inline void setInputChannel(Channel inChannel);
154 public:
155  static inline MidiType getTypeFromStatusByte(byte inStatus);
156  static inline Channel getChannelFromStatusByte(byte inStatus);
157  static inline bool isChannelMessage(MidiType inType);
159  // -------------------------------------------------------------------------
160  // Input Callbacks
162 public:
163  inline void setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity));
164  inline void setHandleNoteOn(void (*fptr)(byte channel, byte note, byte velocity));
165  inline void setHandleAfterTouchPoly(void (*fptr)(byte channel, byte note, byte pressure));
166  inline void setHandleControlChange(void (*fptr)(byte channel, byte number, byte value));
167  inline void setHandleProgramChange(void (*fptr)(byte channel, byte number));
168  inline void setHandleAfterTouchChannel(void (*fptr)(byte channel, byte pressure));
169  inline void setHandlePitchBend(void (*fptr)(byte channel, int bend));
170  inline void setHandleSystemExclusive(void (*fptr)(byte * array, unsigned size));
171  inline void setHandleTimeCodeQuarterFrame(void (*fptr)(byte data));
172  inline void setHandleSongPosition(void (*fptr)(unsigned beats));
173  inline void setHandleSongSelect(void (*fptr)(byte songnumber));
174  inline void setHandleTuneRequest(void (*fptr)(void));
175  inline void setHandleClock(void (*fptr)(void));
176  inline void setHandleStart(void (*fptr)(void));
177  inline void setHandleContinue(void (*fptr)(void));
178  inline void setHandleStop(void (*fptr)(void));
179  inline void setHandleActiveSensing(void (*fptr)(void));
180  inline void setHandleSystemReset(void (*fptr)(void));
182  inline void disconnectCallbackFromType(MidiType inType);
184 private:
185  void launchCallback();
187  void (*mNoteOffCallback)(byte channel, byte note, byte velocity);
188  void (*mNoteOnCallback)(byte channel, byte note, byte velocity);
189  void (*mAfterTouchPolyCallback)(byte channel, byte note, byte velocity);
190  void (*mControlChangeCallback)(byte channel, byte, byte);
191  void (*mProgramChangeCallback)(byte channel, byte);
192  void (*mAfterTouchChannelCallback)(byte channel, byte);
193  void (*mPitchBendCallback)(byte channel, int);
194  void (*mSystemExclusiveCallback)(byte * array, unsigned size);
195  void (*mTimeCodeQuarterFrameCallback)(byte data);
196  void (*mSongPositionCallback)(unsigned beats);
197  void (*mSongSelectCallback)(byte songnumber);
198  void (*mTuneRequestCallback)(void);
199  void (*mClockCallback)(void);
200  void (*mStartCallback)(void);
201  void (*mContinueCallback)(void);
202  void (*mStopCallback)(void);
203  void (*mActiveSensingCallback)(void);
204  void (*mSystemResetCallback)(void);
206  // -------------------------------------------------------------------------
207  // MIDI Soft Thru
209 public:
210  inline Thru::Mode getFilterMode() const;
211  inline bool getThruState() const;
213  inline void turnThruOn(Thru::Mode inThruFilterMode = Thru::Full);
214  inline void turnThruOff();
215  inline void setThruFilterMode(Thru::Mode inThruFilterMode);
217 private:
218  void thruFilter(byte inChannel);
220 private:
221  bool parse();
222  inline void handleNullVelocityNoteOnAsNoteOff();
223  inline bool inputFilter(Channel inChannel);
224  inline void resetInput();
226 private:
229 private:
230  SerialPort& mSerial;
232 private:
233  Channel mInputChannel;
234  StatusByte mRunningStatus_RX;
235  StatusByte mRunningStatus_TX;
236  byte mPendingMessage[3];
237  unsigned mPendingMessageExpectedLenght;
238  unsigned mPendingMessageIndex;
239  unsigned mCurrentRpnNumber;
240  unsigned mCurrentNrpnNumber;
241  bool mThruActivated : 1;
242  Thru::Mode mThruFilterMode : 7;
243  MidiMessage mMessage;
246 private:
247  inline StatusByte getStatus(MidiType inType,
248  Channel inChannel) const;
249 };
251 // -----------------------------------------------------------------------------
253 unsigned encodeSysEx(const byte* inData, byte* outSysEx, unsigned inLenght);
254 unsigned decodeSysEx(const byte* inSysEx, byte* outData, unsigned inLenght);
258 #include "MIDI.hpp"
void setHandleProgramChange(void(*fptr)(byte channel, byte number))
Definition: MIDI.hpp:1144
void disconnectCallbackFromType(MidiType inType)
Detach an external function from the given type.
Definition: MIDI.hpp:1166
void setHandleNoteOn(void(*fptr)(byte channel, byte note, byte velocity))
Definition: MIDI.hpp:1141
void sendSongSelect(DataByte inSongNumber)
Send a Song Select message.
Definition: MIDI.hpp:418
void setThruFilterMode(Thru::Mode inThruFilterMode)
Set the filter for thru mirroring.
Definition: MIDI.hpp:1252
void sendSongPosition(unsigned inBeats)
Send a Song Position Pointer message.
Definition: MIDI.hpp:404
void turnThruOn(Thru::Mode inThruFilterMode=Thru::Full)
Definition: MIDI.hpp:1271
void beginRpn(unsigned inNumber, Channel inChannel)
Start a Registered Parameter Number frame.
Definition: MIDI.hpp:462
void sendProgramChange(DataByte inProgramNumber, Channel inChannel)
Send a Program Change message.
Definition: MIDI.hpp:225
MidiType getType() const
Get the last received message&#39;s type.
Definition: MIDI.hpp:1010
DataByte getData2() const
Get the second data byte of the last received message.
Definition: MIDI.hpp:1035
byte StatusByte
Definition: midi_Defs.h:57
MIDI Library for the Arduino - Settings.
Definition: midi_Defs.h:93
Channel getChannel() const
Get the channel of the message stored in the structure.
Definition: MIDI.hpp:1021
void setHandleSystemExclusive(void(*fptr)(byte *array, unsigned size))
Definition: MIDI.hpp:1147
void setHandleSongSelect(void(*fptr)(byte songnumber))
Definition: MIDI.hpp:1150
unsigned getSysExArrayLength() const
Get the lenght of the System Exclusive array.
Definition: MIDI.hpp:1056
bool read()
Read messages from the serial port using the main input channel.
Definition: MIDI.hpp:643
DataByte getData1() const
Get the first data byte of the last received message.
Definition: MIDI.hpp:1028
void setHandleClock(void(*fptr)(void))
Definition: MIDI.hpp:1152
void beginNrpn(unsigned inNumber, Channel inChannel)
Start a Non-Registered Parameter Number frame.
Definition: MIDI.hpp:542
void begin(Channel inChannel=1)
Call the begin method in the setup() function of the Arduino.
Definition: MIDI.hpp:84
void sendRealTime(MidiType inType)
Send a Real Time (one byte) message.
Definition: MIDI.hpp:436
static Channel getChannelFromStatusByte(byte inStatus)
Returns channel in the range 1-16.
Definition: MIDI.hpp:1117
void setHandleStart(void(*fptr)(void))
Definition: MIDI.hpp:1153
byte Channel
Definition: midi_Defs.h:59
void endRpn(Channel inChannel)
Terminate an RPN frame. This will send a Null Function to deselect the currently selected RPN...
Definition: MIDI.hpp:528
void setInputChannel(Channel inChannel)
Set the value for the input MIDI channel.
Definition: MIDI.hpp:1081
void setHandleSystemReset(void(*fptr)(void))
Definition: MIDI.hpp:1157
bool getThruState() const
Definition: MIDI.hpp:1265
Channel getInputChannel() const
Definition: MIDI.hpp:1071
void endNrpn(Channel inChannel)
Terminate an NRPN frame. This will send a Null Function to deselect the currently selected NRPN...
Definition: MIDI.hpp:608
enum __attribute__((deprecated)) MidiFilterMode
Definition: midi_Defs.h:105
void turnThruOff()
Definition: MIDI.hpp:1278
void sendAfterTouch(DataByte inPressure, Channel inChannel)
Send a MonoPhonic AfterTouch message (applies to all notes)
Definition: MIDI.hpp:265
void sendPolyPressure(DataByte inNoteNumber, DataByte inPressure, Channel inChannel) __attribute__((deprecated))
Send a Polyphonic AfterTouch message (applies to a specified note)
Definition: MIDI.hpp:253
void sendRpnDecrement(byte inAmount, Channel inChannel)
Definition: MIDI.hpp:517
void sendNrpnValue(unsigned inValue, Channel inChannel)
Send a 14-bit value for the currently selected NRPN number.
Definition: MIDI.hpp:560
void sendControlChange(DataByte inControlNumber, DataByte inControlValue, Channel inChannel)
Send a Control Change message.
Definition: MIDI.hpp:238
void sendTimeCodeQuarterFrame(DataByte inTypeNibble, DataByte inValuesNibble)
Send a MIDI Time Code Quarter Frame.
Definition: MIDI.hpp:375
void sendNrpnDecrement(byte inAmount, Channel inChannel)
Definition: MIDI.hpp:597
void setHandleSongPosition(void(*fptr)(unsigned beats))
Definition: MIDI.hpp:1149
MIDI Library for the Arduino - Inline implementations.
Definition: midi_Defs.h:65
unsigned encodeSysEx(const byte *inData, byte *outSysEx, unsigned inLenght)
Encode System Exclusive messages. SysEx messages are encoded to guarantee transmission of data bytes ...
Definition: MIDI.cpp:45
_Settings Settings
Definition: MIDI.h:48
Thru::Mode getFilterMode() const
Definition: MIDI.hpp:1259
void sendPitchBend(int inPitchValue, Channel inChannel)
Send a Pitch Bend message using a signed integer value.
Definition: MIDI.hpp:292
static MidiType getTypeFromStatusByte(byte inStatus)
Extract an enumerated MIDI type from a status byte.
Definition: MIDI.hpp:1094
void setHandleActiveSensing(void(*fptr)(void))
Definition: MIDI.hpp:1156
MidiInterface(SerialPort &inSerial)
Constructor for MidiInterface.
Definition: MIDI.hpp:34
MIDI Library for the Arduino - Message struct definition.
static bool isChannelMessage(MidiType inType)
Definition: MIDI.hpp:1123
byte DataByte
Definition: midi_Defs.h:58
uint8_t byte
Definition: midi_Defs.h:36
void setHandleTimeCodeQuarterFrame(void(*fptr)(byte data))
Definition: MIDI.hpp:1148
void sendTuneRequest()
Send a Tune Request message.
Definition: MIDI.hpp:358
void setHandleContinue(void(*fptr)(void))
Definition: MIDI.hpp:1154
void sendNrpnIncrement(byte inAmount, Channel inChannel)
Definition: MIDI.hpp:587
MIDI Library for the Arduino - Definitions.
void sendRpnValue(unsigned inValue, Channel inChannel)
Send a 14-bit value for the currently selected RPN number.
Definition: MIDI.hpp:480
unsigned decodeSysEx(const byte *inSysEx, byte *outData, unsigned inLenght)
Decode System Exclusive messages. SysEx messages are encoded to guarantee transmission of data bytes ...
Definition: MIDI.cpp:82
void send(MidiType inType, DataByte inData1, DataByte inData2, Channel inChannel)
Generate and send a MIDI message from the values given.
Definition: MIDI.hpp:133
void sendSysEx(unsigned inLength, const byte *inArray, bool inArrayContainsBoundaries=false)
Generate and send a System Exclusive frame.
Definition: MIDI.hpp:325
void setHandleNoteOff(void(*fptr)(byte channel, byte note, byte velocity))
Definition: MIDI.hpp:1140
bool check() const
Check if a valid message is stored in the structure.
Definition: MIDI.hpp:1063
void setHandlePitchBend(void(*fptr)(byte channel, int bend))
Definition: MIDI.hpp:1146
void setHandleStop(void(*fptr)(void))
Definition: MIDI.hpp:1155
void sendNoteOn(DataByte inNoteNumber, DataByte inVelocity, Channel inChannel)
Send a Note On message.
Definition: MIDI.hpp:194
const byte * getSysExArray() const
Get the System Exclusive byte array.
Definition: MIDI.hpp:1045
Fully enabled Thru (every incoming message is sent back).
Definition: midi_Defs.h:96
void setHandleTuneRequest(void(*fptr)(void))
Definition: MIDI.hpp:1151
Destructor for MidiInterface.
Definition: MIDI.hpp:71
void setHandleControlChange(void(*fptr)(byte channel, byte number, byte value))
Definition: MIDI.hpp:1143
void sendNoteOff(DataByte inNoteNumber, DataByte inVelocity, Channel inChannel)
Send a Note Off message.
Definition: MIDI.hpp:213
The main class for MIDI handling. It is templated over the type of serial port to provide abstraction...
Definition: MIDI.h:45
void setHandleAfterTouchChannel(void(*fptr)(byte channel, byte pressure))
Definition: MIDI.hpp:1145
void sendRpnIncrement(byte inAmount, Channel inChannel)
Definition: MIDI.hpp:507
void setHandleAfterTouchPoly(void(*fptr)(byte channel, byte note, byte pressure))
Definition: MIDI.hpp:1142