Control Surface new-input
MIDI Control Surface library for Arduino
MIDI_Sender.ipp
Go to the documentation of this file.
1 #include "MIDI_Sender.hpp"
2 #include <AH/Containers/CRTP.hpp>
3 
5 
6 template <class Derived>
8  uint8_t d1, uint8_t d2,
9  Cable cable) {
10  send(ChannelMessage(m, c, d1, d2, cable));
11 }
12 
13 template <class Derived>
15  uint8_t d1, Cable cable) {
16  send(ChannelMessage(m, c, d1, 0, cable));
17 }
18 
19 template <class Derived>
20 void MIDI_Sender<Derived>::sendNoteOn(MIDIAddress address, uint8_t velocity) {
21  if (address)
22  CRTP(Derived).sendChannelMessageImpl({
24  address.getChannel(),
25  address.getAddress(),
26  uint8_t(velocity & 0x7F),
27  address.getCableNumber(),
28  });
29 }
30 template <class Derived>
31 void MIDI_Sender<Derived>::sendNoteOff(MIDIAddress address, uint8_t velocity) {
32  if (address)
33  CRTP(Derived).sendChannelMessageImpl({
35  address.getChannel(),
36  address.getAddress(),
37  uint8_t(velocity & 0x7F),
38  address.getCableNumber(),
39  });
40 }
41 template <class Derived>
43  uint8_t pressure) {
44  if (address)
45  CRTP(Derived).sendChannelMessageImpl({
47  address.getChannel(),
48  address.getAddress(),
49  uint8_t(pressure & 0x7F),
50  address.getCableNumber(),
51  });
52 }
53 template <class Derived>
55  uint8_t value) {
56  if (address)
57  CRTP(Derived).sendChannelMessageImpl({
59  address.getChannel(),
60  address.getAddress(),
61  uint8_t(value & 0x7F),
62  address.getCableNumber(),
63  });
64 }
65 template <class Derived>
67  uint8_t value) {
68  if (address)
69  CRTP(Derived).sendChannelMessageImpl({
71  address.getChannel(),
72  uint8_t(value & 0x7F),
73  uint8_t(0x00),
74  address.getCableNumber(),
75  });
76 }
77 template <class Derived>
79  if (address)
80  CRTP(Derived).sendChannelMessageImpl({
82  address.getChannel(),
83  address.getAddress(),
84  uint8_t(0x00),
85  address.getCableNumber(),
86  });
87 }
88 template <class Derived>
90  uint8_t pressure) {
91  if (address)
92  CRTP(Derived).sendChannelMessageImpl({
94  address.getChannel(),
95  uint8_t(pressure & 0x7F),
96  uint8_t(0x00),
97  address.getCableNumber(),
98  });
99 }
100 template <class Derived>
102  uint16_t value) {
103  if (address)
104  CRTP(Derived).sendChannelMessageImpl({
106  address.getChannel(),
107  uint8_t((value >> 0) & 0x7F),
108  uint8_t((value >> 7) & 0x7F),
109  address.getCableNumber(),
110  });
111 }
112 template <class Derived>
114  if (message.length > 0)
115  CRTP(Derived).sendSysExImpl(message);
116 }
117 
118 template <class Derived>
120  if (message.isValid())
121  CRTP(Derived).sendRealTimeImpl(message);
122 }
123 
124 template <class Derived>
126  if (message.hasValidChannelMessageHeader()) {
127  message.sanitize();
128  CRTP(Derived).sendChannelMessageImpl(message);
129  }
130 }
131 
132 template <class Derived>
134  if (message.hasValidSystemCommonHeader()) {
135  message.sanitize();
136  CRTP(Derived).sendSysCommonImpl(message);
137  }
138 }
139 
140 template <class Derived>
141 template <uint16_t N>
142 void MIDI_Sender<Derived>::sendSysEx(const uint8_t (&sysexdata)[N],
143  Cable cable) {
144  send(SysExMessage(sysexdata, N, cable));
145 }
146 template <class Derived>
147 void MIDI_Sender<Derived>::sendSysEx(const uint8_t *data, uint16_t length,
148  Cable cable) {
149  send(SysExMessage(data, length, cable));
150 }
151 
152 template <class Derived>
154  send(SysCommonMessage(m, cable));
155 }
156 template <class Derived>
158  Cable cable) {
159  send(SysCommonMessage(m, data1, cable));
160 }
161 template <class Derived>
163  uint8_t data2, Cable cable) {
164  send(SysCommonMessage(m, data1, data2, cable));
165 }
166 
167 template <class Derived>
170 }
171 template <class Derived>
173  uint8_t values, Cable cable) {
174  sendMTCQuarterFrame((messageType << 4) | values, cable);
175 }
176 template <class Derived>
179  msg.setData14bit(spp);
180  send(msg);
181 }
182 template <class Derived>
183 void MIDI_Sender<Derived>::sendSongSelect(uint8_t song, Cable cable) {
185 }
186 template <class Derived>
189 }
190 
191 template <class Derived>
193  send(RealTimeMessage(rt, cable));
194 }
195 template <class Derived>
196 void MIDI_Sender<Derived>::sendRealTime(uint8_t rt, Cable cable) {
197  send(RealTimeMessage(rt, cable));
198 }
199 
200 template <class Derived>
202  sendRealTime(MIDIMessageType::TIMING_CLOCK, cable);
203 }
204 template <class Derived>
206  sendRealTime(MIDIMessageType::START, cable);
207 }
208 template <class Derived>
210  sendRealTime(MIDIMessageType::CONTINUE, cable);
211 }
212 template <class Derived>
214  sendRealTime(MIDIMessageType::STOP, cable);
215 }
216 template <class Derived>
218  sendRealTime(MIDIMessageType::ACTIVE_SENSING, cable);
219 }
220 template <class Derived>
222  sendRealTime(MIDIMessageType::SYSTEM_RESET, cable);
223 }
224 
225 template <class Derived>
227  CRTP(Derived).sendNowImpl();
228 }
229 
230 template <class Derived>
231 void MIDI_Sender<Derived>::sendKP(MIDIAddress address, uint8_t pressure) {
232  sendKeyPressure(address, pressure);
233 }
234 template <class Derived>
235 void MIDI_Sender<Derived>::sendCC(MIDIAddress address, uint8_t value) {
236  sendControlChange(address, value);
237 }
238 template <class Derived>
240  sendProgramChange(address);
241 }
242 template <class Derived>
243 void MIDI_Sender<Derived>::sendPC(MIDIChannelCable address, uint8_t value) {
244  sendProgramChange(address, value);
245 }
246 template <class Derived>
247 void MIDI_Sender<Derived>::sendCP(MIDIChannelCable address, uint8_t pressure) {
248  sendChannelPressure(address, pressure);
249 }
250 template <class Derived>
251 void MIDI_Sender<Derived>::sendPB(MIDIChannelCable address, uint16_t value) {
252  sendPitchBend(address, value);
253 }
254 
#define CRTP(Derived)
Helper for the Curiously Recurring Template Pattern.
Definition: CRTP.hpp:4
MIDIMessageType
All possible MIDI status byte values (without channel).
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
A type-safe class for MIDI USB Cable numbers.
Definition: Cable.hpp:13
A type-safe class for MIDI channels.
Definition: Channel.hpp:13
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
constexpr uint8_t getAddress() const
Get the address [0, 127].
constexpr Channel getChannel() const
Get the channel [CHANNEL_1, CHANNEL_16].
constexpr Cable getCableNumber() const
Get the cable number [CABLE_1, CABLE_16].
A class for saving a MIDI channel and cable number.
Definition: MIDIAddress.hpp:24
constexpr Channel getChannel() const
Get the channel [1, 16].
Definition: MIDIAddress.hpp:44
constexpr Cable getCableNumber() const
Get the cable number [CABLE_1, CABLE_16].
Definition: MIDIAddress.hpp:49
void sendNow()
Causes all buffered messages to be sent immediately.
void sendSysEx(const uint8_t(&sysexdata)[N], Cable cable=CABLE_1)
Send a MIDI System Exclusive message.
void sendChannelPressure(MIDIChannelCable address, uint8_t pressure)
Send a MIDI Channel Pressure event.
Definition: MIDI_Sender.ipp:89
void sendPB(MIDIChannelCable address, uint16_t value)
Send a MIDI Pitch Bend event.
void sendStart(Cable cable=CABLE_1)
Send a MIDI Start message.
void sendRealTime(MIDIMessageType rt, Cable cable=CABLE_1)
Send a MIDI Real-Time message.
void sendChannelMessage(MIDIMessageType m, Channel c, uint8_t d1, uint8_t d2, Cable cable=CABLE_1)
Send a 3-byte MIDI Channel Voice message.
Definition: MIDI_Sender.ipp:7
void sendKP(MIDIAddress address, uint8_t pressure)
Send a MIDI Key Pressure event.
void sendMTCQuarterFrame(uint8_t data, Cable cable=CABLE_1)
Send a MIDI Time Code Quarter Frame.
void sendSysCommon(MIDIMessageType m, Cable cable=CABLE_1)
Send a MIDI System Common message.
void sendProgramChange(MIDIAddress address)
Send a MIDI Program Change event.
Definition: MIDI_Sender.ipp:78
void sendSystemReset(Cable cable=CABLE_1)
Send a MIDI System Reset message.
void send(ChannelMessage message)
Send a MIDI Channel Voice message.
void sendCP(MIDIChannelCable address, uint8_t pressure)
Send a MIDI Channel Pressure event.
void sendActiveSensing(Cable cable=CABLE_1)
Send a MIDI Active Sensing message.
void sendCC(MIDIAddress address, uint8_t value)
Send a MIDI Control Change event.
void sendStop(Cable cable=CABLE_1)
Send a MIDI Stop message.
void sendKeyPressure(MIDIAddress address, uint8_t pressure)
Send a MIDI Key Pressure event.
Definition: MIDI_Sender.ipp:42
void sendControlChange(MIDIAddress address, uint8_t value)
Send a MIDI Control Change event.
Definition: MIDI_Sender.ipp:54
void sendSongPositionPointer(uint16_t spp, Cable cable=CABLE_1)
Send a MIDI Song Position Pointer message.
void sendSongSelect(uint8_t song, Cable cable=CABLE_1)
Send a MIDI Song Select message.
void sendPC(MIDIAddress address)
Send a MIDI Program Change event.
void sendNoteOn(MIDIAddress address, uint8_t velocity)
Send a MIDI Note On event.
Definition: MIDI_Sender.ipp:20
void sendNoteOff(MIDIAddress address, uint8_t velocity)
Send a MIDI Note Off event.
Definition: MIDI_Sender.ipp:31
void sendTuneRequest(Cable cable=CABLE_1)
Send a MIDI Tune Request.
void sendTimingClock(Cable cable=CABLE_1)
Send a MIDI Timing Clock message.
void sendPitchBend(MIDIChannelCable address, uint16_t value)
Send a MIDI Pitch Bend event.
void sendContinue(Cable cable=CABLE_1)
Send a MIDI Continue message.
bool hasValidChannelMessageHeader() const
Check whether the header is a valid header for a channel message.
void sanitize()
Make sure that the status byte has the most significant bit set and the data bytes have the most sign...
void setData14bit(uint16_t data)
If Data 1 and Data 2 represent a single 14-bit number, you can use this method to set that number.
bool hasValidSystemCommonHeader() const
Check whether the header is a valid header for a System Common message.
bool isValid() const
Check whether the header is a valid header for a Real-Time message.