Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
MIDI_Interface.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "MIDI_Pipes.hpp"
4#include "MIDI_Sender.hpp"
5#include "MIDI_Staller.hpp"
7#include <Def/Def.hpp>
8#include <Def/MIDIAddress.hpp>
10
12
13constexpr auto MIDI_BAUD = 31250;
14
15class MIDI_Callbacks;
16
21 public MIDI_Sender<MIDI_Interface>,
22 public AH::Updatable<MIDI_Interface>,
23 protected MIDIStaller {
24 protected:
25 MIDI_Interface() = default;
27
28 public:
30 virtual ~MIDI_Interface();
31
33 void begin() override {}
35 void update() override = 0;
36
39
41 void setAsDefault();
48 static MIDI_Interface *getDefault();
49
51
54
58 void setCallbacks(MIDI_Callbacks *cb) { this->callbacks = cb; }
63
65
66 protected:
67 friend class MIDI_Sender<MIDI_Interface>;
73 virtual void sendSysExImpl(SysExMessage) = 0;
77 virtual void sendNowImpl() = 0;
78
79 public:
80#if DISABLE_PIPES
81 virtual MIDIReadEvent read() = 0;
82 virtual ChannelMessage getChannelMessage() const = 0;
83 virtual SysCommonMessage getSysCommonMessage() const = 0;
84 virtual RealTimeMessage getRealTimeMessage() const = 0;
85 virtual SysExMessage getSysExMessage() const = 0;
86#endif
87
88 protected:
89#if !DISABLE_PIPES
91 void sinkMIDIfromPipe(ChannelMessage msg) override { send(msg); }
93 void sinkMIDIfromPipe(SysExMessage msg) override { send(msg); }
95 void sinkMIDIfromPipe(SysCommonMessage msg) override { send(msg); }
97 void sinkMIDIfromPipe(RealTimeMessage msg) override { send(msg); }
98#endif
99
100 protected:
102 void onChannelMessage(ChannelMessage message);
104 void onSysExMessage(SysExMessage message);
111
112 public:
114 template <class MIDIInterface_t>
115 static void updateIncoming(MIDIInterface_t *iface);
117 template <class MIDIInterface_t>
118 static void dispatchIncoming(MIDIInterface_t *iface, MIDIReadEvent event);
122 template <class MIDIInterface_t>
123 static void handleStall(MIDIInterface_t *self);
125
126 private:
128
129 private:
131};
132
133template <class MIDIInterface_t>
134void MIDI_Interface::updateIncoming(MIDIInterface_t *self) {
135#if DISABLE_PIPES
136 MIDIReadEvent event = self->read();
137 while (event != MIDIReadEvent::NO_MESSAGE) {
138 dispatchIncoming(self, event);
139 event = self->read();
140 }
141#else
142 MIDIReadEvent event = self->read();
143 if (event == MIDIReadEvent::NO_MESSAGE)
144 return;
145 if (self->getStaller() == self)
146 self->unstall(self);
147 int16_t size_rem = 512 * 3 / 4; // Don't keep on reading for too long
148 bool chunked = false; // Whether there's an unterminated SysEx chunk
149 while (event != MIDIReadEvent::NO_MESSAGE) {
150 dispatchIncoming(self, event);
151 if (event == MIDIReadEvent::SYSEX_CHUNK) {
152 size_rem -= self->getSysExMessage().length;
153 chunked = true;
154 } else if (event == MIDIReadEvent::SYSEX_MESSAGE) {
155 size_rem -= self->getSysExMessage().length;
156 chunked = false;
157 } else {
158 size_rem -= 3;
159 }
160 if (size_rem < 0)
161 break;
162 event = self->read();
163 }
164 if (chunked)
165 self->stall(self);
166#endif
167 // TODO: add logic to detect MIDI messages such as (N)RPN that span over
168 // multiple channel voice messages and that shouldn't be interrupted.
169 // For short messages such as (N)RPN, I suggest waiting with a timeout.
170}
171
172template <class MIDIInterface_t>
173void MIDI_Interface::dispatchIncoming(MIDIInterface_t *self,
174 MIDIReadEvent event) {
175 switch (event) {
177 self->onChannelMessage(self->getChannelMessage());
178 break;
179 case MIDIReadEvent::SYSEX_CHUNK: // fallthrough
181 self->onSysExMessage(self->getSysExMessage());
182 break;
184 self->onSysCommonMessage(self->getSysCommonMessage());
185 break;
187 self->onRealTimeMessage(self->getRealTimeMessage());
188 break;
189 case MIDIReadEvent::NO_MESSAGE: break; // LCOV_EXCL_LINE
190 default: break; // LCOV_EXCL_LINE
191 }
192}
193
194#if !DISABLE_PIPES
195template <class MIDIInterface_t>
196void MIDI_Interface::handleStall(MIDIInterface_t *self) {
197 const char *staller_name = self->getStallerName();
198 DEBUGFN(F("Handling stall. Cause: ") << staller_name);
199 self->unstall(self);
200
201 unsigned long startTime = millis();
202 while (millis() - startTime < SYSEX_CHUNK_TIMEOUT) {
203 MIDIReadEvent event = self->read();
204 dispatchIncoming(self, event);
205 if (event == MIDIReadEvent::SYSEX_CHUNK)
206 startTime = millis(); // reset timeout
207 else if (event == MIDIReadEvent::SYSEX_MESSAGE)
208 return;
209 }
210 DEBUGFN(F("Warning: Unable to un-stall pipes. Cause: ") << staller_name);
211 static_cast<void>(staller_name);
212}
213#endif
214
MIDIReadEvent
Values returned by the MIDI reading functions.
@ CHANNEL_MESSAGE
A MIDI Channel message was received.
@ SYSEX_CHUNK
An incomplete System Exclusive message.
@ SYSCOMMON_MESSAGE
A MIDI System Common message was received.
@ NO_MESSAGE
No new messages were received.
@ SYSEX_MESSAGE
A MIDI System Exclusive message was received.
@ REALTIME_MESSAGE
A MIDI Real-Time message was received.
constexpr auto MIDI_BAUD
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
constexpr unsigned long SYSEX_CHUNK_TIMEOUT
Timeout in milliseconds to wait for a SysEx chunk to complete.
A super class for object that have to be updated regularly.
A class for callbacks from MIDI input.
An abstract class for MIDI interfaces.
void onSysCommonMessage(SysCommonMessage message)
Call the System Common message callback and send the message to the sink pipe.
virtual void sendSysCommonImpl(SysCommonMessage)=0
Low-level function for sending a MIDI system common message.
MIDI_Interface()=default
virtual void sendRealTimeImpl(RealTimeMessage)=0
Low-level function for sending a MIDI real-time message.
virtual void sendNowImpl()=0
Low-level function for sending any buffered outgoing MIDI messages.
void sinkMIDIfromPipe(SysCommonMessage msg) override
Accept an incoming MIDI System Common message from the source pipe.
virtual ~MIDI_Interface()
Destructor.
virtual void sendChannelMessageImpl(ChannelMessage)=0
Low-level function for sending a MIDI channel voice message.
virtual void sendSysExImpl(SysExMessage)=0
Low-level function for sending a system exclusive MIDI message.
void sinkMIDIfromPipe(RealTimeMessage msg) override
Accept an incoming MIDI Real-Time message from the source pipe.
virtual void handleStall()=0
Call back that should finish any MIDI messages that are in progress, and un-stall the pipe or MIDI so...
void begin() override
Initialize the MIDI Interface.
static void dispatchIncoming(MIDIInterface_t *iface, MIDIReadEvent event)
Dispatch the given type of MIDI message from the given interface.
void setAsDefault()
Set this MIDI interface as the default interface.
void sinkMIDIfromPipe(ChannelMessage msg) override
Accept an incoming MIDI Channel message from the source pipe.
MIDI_Callbacks * callbacks
void onSysExMessage(SysExMessage message)
Call the SysEx message callback and send the message to the sink pipe.
void onRealTimeMessage(RealTimeMessage message)
Call the real-time message callback and send the message to the sink pipe.
static MIDI_Interface * DefaultMIDI_Interface
static void updateIncoming(MIDIInterface_t *iface)
Read, parse and dispatch incoming MIDI messages on the given interface.
void setCallbacks(MIDI_Callbacks *cb)
Set the callbacks that will be called when a MIDI message is received.
void onChannelMessage(ChannelMessage message)
Call the channel message callback and send the message to the sink pipe.
static MIDI_Interface * getDefault()
Return the default MIDI interface.
void sinkMIDIfromPipe(SysExMessage msg) override
Accept an incoming MIDI System Exclusive message from the source pipe.
void update() override=0
Read the MIDI interface and call the callback if a message was received.
MIDI_Interface(MIDI_Interface &&)=default
Statically polymorphic template for classes that send MIDI messages.
void send(ChannelMessage message)
Send a MIDI Channel Voice message.
#define DEBUGFN(x)
Print an expression and its function (function name and line number) to the debug output if debugging...
Definition Debug.hpp:115
Struct that can cause a MIDI_Pipe to be stalled.
virtual void handleStall()=0
Call back that should finish any MIDI messages that are in progress, and un-stall the pipe or MIDI so...
A struct that is both a TrueMIDI_Sink and a TrueMIDI_Source.