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
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
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>
119#if !DISABLE_PIPES
123 template <class MIDIInterface_t>
124 static void handleStall(MIDIInterface_t *self);
126#endif
127
128 private:
130
131 private:
133};
134
135template <class MIDIInterface_t>
137#if DISABLE_PIPES
138 MIDIReadEvent event = self->read();
141 event = self->read();
142 }
143#else
144 MIDIReadEvent event = self->read();
146 return;
147 if (self->getStaller() == self)
148 self->unstall(self);
149 int16_t size_rem = 512 * 3 / 4; // Don't keep on reading for too long
150 bool chunked = false; // Whether there's an unterminated SysEx chunk
154 size_rem -= self->getSysExMessage().length;
155 chunked = true;
156 } else if (event == MIDIReadEvent::SYSEX_MESSAGE) {
157 size_rem -= self->getSysExMessage().length;
158 chunked = false;
159 } else {
160 size_rem -= 3;
161 }
162 if (size_rem < 0)
163 break;
164 event = self->read();
165 }
166 if (chunked)
167 self->stall(self);
168#endif
169 // TODO: add logic to detect MIDI messages such as (N)RPN that span over
170 // multiple channel voice messages and that shouldn't be interrupted.
171 // For short messages such as (N)RPN, I suggest waiting with a timeout.
172}
173
174template <class MIDIInterface_t>
177 switch (event) {
179 self->onChannelMessage(self->getChannelMessage());
180 break;
181 case MIDIReadEvent::SYSEX_CHUNK: // fallthrough
183 self->onSysExMessage(self->getSysExMessage());
184 break;
186 self->onSysCommonMessage(self->getSysCommonMessage());
187 break;
189 self->onRealTimeMessage(self->getRealTimeMessage());
190 break;
191 case MIDIReadEvent::NO_MESSAGE: break; // LCOV_EXCL_LINE
192 default: break; // LCOV_EXCL_LINE
193 }
194}
195
196#if !DISABLE_PIPES
197template <class MIDIInterface_t>
199 const char *staller_name = self->getStallerName();
200 DEBUGFN(F("Handling stall. Cause: ") << staller_name);
201 self->unstall(self);
202
203 unsigned long startTime = millis();
204 while (millis() - startTime < SYSEX_CHUNK_TIMEOUT) {
205 MIDIReadEvent event = self->read();
208 startTime = millis(); // reset timeout
210 return;
211 }
212 DEBUGFN(F("Warning: Unable to un-stall pipes. Cause: ") << staller_name);
213 static_cast<void>(staller_name);
214}
215#endif
216
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.
Array< T, N > copyAs(const Array< U, N > &src)
Copy an Array to an Array of a different type.
#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.