Arduino MIDI Library  Version 4.3
MIDI.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include "midi_Defs.h"
32 #include "midi_Settings.h"
33 #include "midi_Message.h"
34 
35 // -----------------------------------------------------------------------------
36 
38 
45 template<class SerialPort, class _Settings = DefaultSettings>
47 {
48 public:
49  typedef _Settings Settings;
50 
51 public:
52  inline MidiInterface(SerialPort& inSerial);
53  inline ~MidiInterface();
54 
55 public:
56  void begin(Channel inChannel = 1);
57 
58  // -------------------------------------------------------------------------
59  // MIDI Output
60 
61 public:
62  inline void sendNoteOn(DataByte inNoteNumber,
63  DataByte inVelocity,
64  Channel inChannel);
65 
66  inline void sendNoteOff(DataByte inNoteNumber,
67  DataByte inVelocity,
68  Channel inChannel);
69 
70  inline void sendProgramChange(DataByte inProgramNumber,
71  Channel inChannel);
72 
73  inline void sendControlChange(DataByte inControlNumber,
74  DataByte inControlValue,
75  Channel inChannel);
76 
77  inline void sendPitchBend(int inPitchValue, Channel inChannel);
78  inline void sendPitchBend(double inPitchValue, Channel inChannel);
79 
80  inline void sendPolyPressure(DataByte inNoteNumber,
81  DataByte inPressure,
82  Channel inChannel) __attribute__ ((deprecated));
83 
84  inline void sendAfterTouch(DataByte inPressure,
85  Channel inChannel);
86  inline void sendAfterTouch(DataByte inNoteNumber,
87  DataByte inPressure,
88  Channel inChannel);
89 
90  inline void sendSysEx(unsigned inLength,
91  const byte* inArray,
92  bool inArrayContainsBoundaries = false);
93 
94  inline void sendTimeCodeQuarterFrame(DataByte inTypeNibble,
95  DataByte inValuesNibble);
96  inline void sendTimeCodeQuarterFrame(DataByte inData);
97 
98  inline void sendSongPosition(unsigned inBeats);
99  inline void sendSongSelect(DataByte inSongNumber);
100  inline void sendTuneRequest();
101  inline void sendRealTime(MidiType inType);
102 
103  inline void beginRpn(unsigned inNumber,
104  Channel inChannel);
105  inline void sendRpnValue(unsigned inValue,
106  Channel inChannel);
107  inline void sendRpnValue(byte inMsb,
108  byte inLsb,
109  Channel inChannel);
110  inline void sendRpnIncrement(byte inAmount,
111  Channel inChannel);
112  inline void sendRpnDecrement(byte inAmount,
113  Channel inChannel);
114  inline void endRpn(Channel inChannel);
115 
116  inline void beginNrpn(unsigned inNumber,
117  Channel inChannel);
118  inline void sendNrpnValue(unsigned inValue,
119  Channel inChannel);
120  inline void sendNrpnValue(byte inMsb,
121  byte inLsb,
122  Channel inChannel);
123  inline void sendNrpnIncrement(byte inAmount,
124  Channel inChannel);
125  inline void sendNrpnDecrement(byte inAmount,
126  Channel inChannel);
127  inline void endNrpn(Channel inChannel);
128 
129 public:
130  void send(MidiType inType,
131  DataByte inData1,
132  DataByte inData2,
133  Channel inChannel);
134 
135  // -------------------------------------------------------------------------
136  // MIDI Input
137 
138 public:
139  inline bool read();
140  inline bool read(Channel inChannel);
141 
142 public:
143  inline MidiType getType() const;
144  inline Channel getChannel() const;
145  inline DataByte getData1() const;
146  inline DataByte getData2() const;
147  inline const byte* getSysExArray() const;
148  inline unsigned getSysExArrayLength() const;
149  inline bool check() const;
150 
151 public:
152  inline Channel getInputChannel() const;
153  inline void setInputChannel(Channel inChannel);
154 
155 public:
156  static inline MidiType getTypeFromStatusByte(byte inStatus);
157  static inline Channel getChannelFromStatusByte(byte inStatus);
158  static inline bool isChannelMessage(MidiType inType);
159 
160  // -------------------------------------------------------------------------
161  // Input Callbacks
162 
163 public:
164  inline void setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity));
165  inline void setHandleNoteOn(void (*fptr)(byte channel, byte note, byte velocity));
166  inline void setHandleAfterTouchPoly(void (*fptr)(byte channel, byte note, byte pressure));
167  inline void setHandleControlChange(void (*fptr)(byte channel, byte number, byte value));
168  inline void setHandleProgramChange(void (*fptr)(byte channel, byte number));
169  inline void setHandleAfterTouchChannel(void (*fptr)(byte channel, byte pressure));
170  inline void setHandlePitchBend(void (*fptr)(byte channel, int bend));
171  inline void setHandleSystemExclusive(void (*fptr)(byte * array, unsigned size));
172  inline void setHandleTimeCodeQuarterFrame(void (*fptr)(byte data));
173  inline void setHandleSongPosition(void (*fptr)(unsigned beats));
174  inline void setHandleSongSelect(void (*fptr)(byte songnumber));
175  inline void setHandleTuneRequest(void (*fptr)(void));
176  inline void setHandleClock(void (*fptr)(void));
177  inline void setHandleStart(void (*fptr)(void));
178  inline void setHandleContinue(void (*fptr)(void));
179  inline void setHandleStop(void (*fptr)(void));
180  inline void setHandleActiveSensing(void (*fptr)(void));
181  inline void setHandleSystemReset(void (*fptr)(void));
182 
183  inline void disconnectCallbackFromType(MidiType inType);
184 
185 private:
186  void launchCallback();
187 
188  void (*mNoteOffCallback)(byte channel, byte note, byte velocity);
189  void (*mNoteOnCallback)(byte channel, byte note, byte velocity);
190  void (*mAfterTouchPolyCallback)(byte channel, byte note, byte velocity);
191  void (*mControlChangeCallback)(byte channel, byte, byte);
192  void (*mProgramChangeCallback)(byte channel, byte);
193  void (*mAfterTouchChannelCallback)(byte channel, byte);
194  void (*mPitchBendCallback)(byte channel, int);
195  void (*mSystemExclusiveCallback)(byte * array, unsigned size);
196  void (*mTimeCodeQuarterFrameCallback)(byte data);
197  void (*mSongPositionCallback)(unsigned beats);
198  void (*mSongSelectCallback)(byte songnumber);
199  void (*mTuneRequestCallback)(void);
200  void (*mClockCallback)(void);
201  void (*mStartCallback)(void);
202  void (*mContinueCallback)(void);
203  void (*mStopCallback)(void);
204  void (*mActiveSensingCallback)(void);
205  void (*mSystemResetCallback)(void);
206 
207  // -------------------------------------------------------------------------
208  // MIDI Soft Thru
209 
210 public:
211  inline Thru::Mode getFilterMode() const;
212  inline bool getThruState() const;
213 
214  inline void turnThruOn(Thru::Mode inThruFilterMode = Thru::Full);
215  inline void turnThruOff();
216  inline void setThruFilterMode(Thru::Mode inThruFilterMode);
217 
218 private:
219  void thruFilter(byte inChannel);
220 
221 private:
222  bool parse();
223  inline void handleNullVelocityNoteOnAsNoteOff();
224  inline bool inputFilter(Channel inChannel);
225  inline void resetInput();
226 
227 private:
229 
230 private:
231  SerialPort& mSerial;
232 
233 private:
234  Channel mInputChannel;
235  StatusByte mRunningStatus_RX;
236  StatusByte mRunningStatus_TX;
237  byte mPendingMessage[3];
238  unsigned mPendingMessageExpectedLenght;
239  unsigned mPendingMessageIndex;
240  unsigned mCurrentRpnNumber;
241  unsigned mCurrentNrpnNumber;
242  bool mThruActivated : 1;
243  Thru::Mode mThruFilterMode : 7;
244  MidiMessage mMessage;
245 
246 
247 private:
248  inline StatusByte getStatus(MidiType inType,
249  Channel inChannel) const;
250 };
251 
252 // -----------------------------------------------------------------------------
253 
254 unsigned encodeSysEx(const byte* inData, byte* outSysEx, unsigned inLenght);
255 unsigned decodeSysEx(const byte* inSysEx, byte* outData, unsigned inLenght);
256 
258 
259 #include "MIDI.hpp"
void setHandleProgramChange(void(*fptr)(byte channel, byte number))
Definition: MIDI.hpp:1145
void disconnectCallbackFromType(MidiType inType)
Detach an external function from the given type.
Definition: MIDI.hpp:1167
void setHandleNoteOn(void(*fptr)(byte channel, byte note, byte velocity))
Definition: MIDI.hpp:1142
void sendSongSelect(DataByte inSongNumber)
Send a Song Select message.
Definition: MIDI.hpp:419
void setThruFilterMode(Thru::Mode inThruFilterMode)
Set the filter for thru mirroring.
Definition: MIDI.hpp:1253
void sendSongPosition(unsigned inBeats)
Send a Song Position Pointer message.
Definition: MIDI.hpp:405
void turnThruOn(Thru::Mode inThruFilterMode=Thru::Full)
Definition: MIDI.hpp:1272
void beginRpn(unsigned inNumber, Channel inChannel)
Start a Registered Parameter Number frame.
Definition: MIDI.hpp:463
void sendProgramChange(DataByte inProgramNumber, Channel inChannel)
Send a Program Change message.
Definition: MIDI.hpp:226
MidiType getType() const
Get the last received message&#39;s type.
Definition: MIDI.hpp:1011
DataByte getData2() const
Get the second data byte of the last received message.
Definition: MIDI.hpp:1036
byte StatusByte
Definition: midi_Defs.h:58
MIDI Library for the Arduino - Settings.
#define BEGIN_MIDI_NAMESPACE
Mode
Definition: midi_Defs.h:94
Channel getChannel() const
Get the channel of the message stored in the structure.
Definition: MIDI.hpp:1022
void setHandleSystemExclusive(void(*fptr)(byte *array, unsigned size))
Definition: MIDI.hpp:1148
#define END_MIDI_NAMESPACE
void setHandleSongSelect(void(*fptr)(byte songnumber))
Definition: MIDI.hpp:1151
unsigned getSysExArrayLength() const
Get the lenght of the System Exclusive array.
Definition: MIDI.hpp:1057
bool read()
Read messages from the serial port using the main input channel.
Definition: MIDI.hpp:644
DataByte getData1() const
Get the first data byte of the last received message.
Definition: MIDI.hpp:1029
void setHandleClock(void(*fptr)(void))
Definition: MIDI.hpp:1153
void beginNrpn(unsigned inNumber, Channel inChannel)
Start a Non-Registered Parameter Number frame.
Definition: MIDI.hpp:543
void begin(Channel inChannel=1)
Call the begin method in the setup() function of the Arduino.
Definition: MIDI.hpp:85
void sendRealTime(MidiType inType)
Send a Real Time (one byte) message.
Definition: MIDI.hpp:437
static Channel getChannelFromStatusByte(byte inStatus)
Returns channel in the range 1-16.
Definition: MIDI.hpp:1118
void setHandleStart(void(*fptr)(void))
Definition: MIDI.hpp:1154
byte Channel
Definition: midi_Defs.h:60
void endRpn(Channel inChannel)
Terminate an RPN frame. This will send a Null Function to deselect the currently selected RPN...
Definition: MIDI.hpp:529
void setInputChannel(Channel inChannel)
Set the value for the input MIDI channel.
Definition: MIDI.hpp:1082
void setHandleSystemReset(void(*fptr)(void))
Definition: MIDI.hpp:1158
bool getThruState() const
Definition: MIDI.hpp:1266
Channel getInputChannel() const
Definition: MIDI.hpp:1072
void endNrpn(Channel inChannel)
Terminate an NRPN frame. This will send a Null Function to deselect the currently selected NRPN...
Definition: MIDI.hpp:609
enum __attribute__((deprecated)) MidiFilterMode
Definition: midi_Defs.h:106
void turnThruOff()
Definition: MIDI.hpp:1279
void sendAfterTouch(DataByte inPressure, Channel inChannel)
Send a MonoPhonic AfterTouch message (applies to all notes)
Definition: MIDI.hpp:266
void sendPolyPressure(DataByte inNoteNumber, DataByte inPressure, Channel inChannel) __attribute__((deprecated))
Send a Polyphonic AfterTouch message (applies to a specified note)
Definition: MIDI.hpp:254
void sendRpnDecrement(byte inAmount, Channel inChannel)
Definition: MIDI.hpp:518
void sendNrpnValue(unsigned inValue, Channel inChannel)
Send a 14-bit value for the currently selected NRPN number.
Definition: MIDI.hpp:561
void sendControlChange(DataByte inControlNumber, DataByte inControlValue, Channel inChannel)
Send a Control Change message.
Definition: MIDI.hpp:239
void sendTimeCodeQuarterFrame(DataByte inTypeNibble, DataByte inValuesNibble)
Send a MIDI Time Code Quarter Frame.
Definition: MIDI.hpp:376
void sendNrpnDecrement(byte inAmount, Channel inChannel)
Definition: MIDI.hpp:598
void setHandleSongPosition(void(*fptr)(unsigned beats))
Definition: MIDI.hpp:1150
MIDI Library for the Arduino - Inline implementations.
MidiType
Definition: midi_Defs.h:66
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:46
_Settings Settings
Definition: MIDI.h:49
Thru::Mode getFilterMode() const
Definition: MIDI.hpp:1260
void sendPitchBend(int inPitchValue, Channel inChannel)
Send a Pitch Bend message using a signed integer value.
Definition: MIDI.hpp:293
static MidiType getTypeFromStatusByte(byte inStatus)
Extract an enumerated MIDI type from a status byte.
Definition: MIDI.hpp:1095
void setHandleActiveSensing(void(*fptr)(void))
Definition: MIDI.hpp:1157
MidiInterface(SerialPort &inSerial)
Constructor for MidiInterface.
Definition: MIDI.hpp:35
MIDI Library for the Arduino - Message struct definition.
static bool isChannelMessage(MidiType inType)
Definition: MIDI.hpp:1124
byte DataByte
Definition: midi_Defs.h:59
uint8_t byte
Definition: midi_Defs.h:37
void setHandleTimeCodeQuarterFrame(void(*fptr)(byte data))
Definition: MIDI.hpp:1149
void sendTuneRequest()
Send a Tune Request message.
Definition: MIDI.hpp:359
void setHandleContinue(void(*fptr)(void))
Definition: MIDI.hpp:1155
void sendNrpnIncrement(byte inAmount, Channel inChannel)
Definition: MIDI.hpp:588
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:481
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:83
void send(MidiType inType, DataByte inData1, DataByte inData2, Channel inChannel)
Generate and send a MIDI message from the values given.
Definition: MIDI.hpp:134
void sendSysEx(unsigned inLength, const byte *inArray, bool inArrayContainsBoundaries=false)
Generate and send a System Exclusive frame.
Definition: MIDI.hpp:326
void setHandleNoteOff(void(*fptr)(byte channel, byte note, byte velocity))
Definition: MIDI.hpp:1141
bool check() const
Check if a valid message is stored in the structure.
Definition: MIDI.hpp:1064
void setHandlePitchBend(void(*fptr)(byte channel, int bend))
Definition: MIDI.hpp:1147
void setHandleStop(void(*fptr)(void))
Definition: MIDI.hpp:1156
void sendNoteOn(DataByte inNoteNumber, DataByte inVelocity, Channel inChannel)
Send a Note On message.
Definition: MIDI.hpp:195
const byte * getSysExArray() const
Get the System Exclusive byte array.
Definition: MIDI.hpp:1046
Fully enabled Thru (every incoming message is sent back).
Definition: midi_Defs.h:97
void setHandleTuneRequest(void(*fptr)(void))
Definition: MIDI.hpp:1152
~MidiInterface()
Destructor for MidiInterface.
Definition: MIDI.hpp:72
void setHandleControlChange(void(*fptr)(byte channel, byte number, byte value))
Definition: MIDI.hpp:1144
void sendNoteOff(DataByte inNoteNumber, DataByte inVelocity, Channel inChannel)
Send a Note Off message.
Definition: MIDI.hpp:214
The main class for MIDI handling. It is templated over the type of serial port to provide abstraction...
Definition: MIDI.h:46
void setHandleAfterTouchChannel(void(*fptr)(byte channel, byte pressure))
Definition: MIDI.hpp:1146
void sendRpnIncrement(byte inAmount, Channel inChannel)
Definition: MIDI.hpp:508
void setHandleAfterTouchPoly(void(*fptr)(byte channel, byte note, byte pressure))
Definition: MIDI.hpp:1143