-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathAppleMIDI.h
354 lines (285 loc) · 12.5 KB
/
AppleMIDI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#pragma once
// https://developer.apple.com/library/archive/documentation/Audio/Conceptual/MIDINetworkDriverProtocol/MIDI/MIDI.html
#include <MIDI.h>
using namespace MIDI_NAMESPACE;
#include <IPAddress.h>
#include "AppleMIDI_Debug.h"
#include "AppleMIDI_PlatformBegin.h"
#include "AppleMIDI_Defs.h"
#include "AppleMIDI_Settings.h"
#include "rtp_Defs.h"
#include "rtpMIDI_Defs.h"
#include "rtpMIDI_Clock.h"
#include "AppleMIDI_Participant.h"
#include "AppleMIDI_Parser.h"
#include "rtpMIDI_Parser.h"
#include "AppleMIDI_Namespace.h"
BEGIN_APPLEMIDI_NAMESPACE
static unsigned long now;
struct AppleMIDISettings : public MIDI_NAMESPACE::DefaultSettings
{
// Packet based protocols prefer the entire message to be parsed
// as a whole.
static const bool Use1ByteParsing = false;
};
template <class UdpClass, class _Settings = DefaultSettings, class _Platform = DefaultPlatform>
class AppleMIDISession
{
typedef _Settings Settings;
typedef _Platform Platform;
// Allow these internal classes access to our private members
// to avoid access by the .ino to internal messages
friend class AppleMIDIParser<UdpClass, Settings, Platform>;
friend class rtpMIDIParser<UdpClass, Settings, Platform>;
public:
AppleMIDISession(const char *sessionName, const uint16_t port = DEFAULT_CONTROL_PORT)
{
this->port = port;
#ifdef KEEP_SESSION_NAME
strncpy(this->localName, sessionName, DefaultSettings::MaxSessionNameLen);
#endif
};
virtual ~AppleMIDISession()
{
};
void setHandleConnected (void (*fptr)(const ssrc_t&, const char*)) { _connectedCallback = fptr; }
void setHandleDisconnected (void (*fptr)(const ssrc_t&)) { _disconnectedCallback = fptr; }
#ifdef USE_EXT_CALLBACKS
void setHandleException (void (*fptr)(const ssrc_t&, const Exception&, const int32_t value)) { _exceptionCallback = fptr; }
void setHandleReceivedRtp (void (*fptr)(const ssrc_t&, const Rtp_t&, const int32_t&)) { _receivedRtpCallback = fptr; }
void setHandleStartReceivedMidi (void (*fptr)(const ssrc_t&)) { _startReceivedMidiByteCallback = fptr; }
void setHandleReceivedMidi (void (*fptr)(const ssrc_t&, byte)) { _receivedMidiByteCallback = fptr; }
void setHandleEndReceivedMidi (void (*fptr)(const ssrc_t&)) { _endReceivedMidiByteCallback = fptr; }
void setHandleSentRtp (void (*fptr)(const Rtp_t&)) { _sentRtpCallback = fptr; }
void setHandleSentRtpMidi (void (*fptr)(const RtpMIDI_t&)) { _sentRtpMidiCallback = fptr; }
#endif
#ifdef KEEP_SESSION_NAME
const char* getName() const { return this->localName; };
void setName(const char *sessionName) { strncpy(this->localName, sessionName, DefaultSettings::MaxSessionNameLen); };
#else
const char* getName() const { return nullptr; };
void setName(const char *sessionName) { };
#endif
const uint16_t getPort() const { return this->port; };
const ssrc_t getSynchronizationSource() const { return this->ssrc; };
#ifdef APPLEMIDI_INITIATOR
bool sendInvite(IPAddress ip, uint16_t port = DEFAULT_CONTROL_PORT);
#endif
void sendEndSession();
public:
// Override default thruActivated. Must be false for all packet based messages
static const bool thruActivated = false;
#ifdef USE_DIRECTORY
Deque<IPAddress, Settings::MaxNumberOfComputersInDirectory> directory;
WhoCanConnectToMe whoCanConnectToMe = Anyone;
#endif
void begin()
{
_appleMIDIParser.session = this;
_rtpMIDIParser.session = this;
// analogRead(0) is not available on all platforms. The use of millis()
// as it preceded by network calls, so timing is variable and usable
// for the random generator.
randomSeed(millis());
// Each stream is distinguished by a unique SSRC value and has a unique sequence
// number and RTP timestamp space.
// this is our SSRC
//
// NOTE: Arduino random only goes to INT32_MAX (not UINT32_MAX)
this->ssrc = random(1, INT32_MAX) * 2;
controlPort.begin(port);
dataPort.begin(port + 1);
rtpMidiClock.Init(rtpMidiClock.Now(), MIDI_SAMPLING_RATE_DEFAULT);
}
void end()
{
controlPort.stop();
dataPort.stop();
}
bool beginTransmission(MIDI_NAMESPACE::MidiType)
{
// All MIDI commands queued up in the same cycle (during 1 loop execution)
// are send in a single MIDI packet
// (The actual sending happen in the available() method, called at the start of the
// event loop() method.
//
// http://www.rfc-editor.org/rfc/rfc4696.txt
//
// 4.1. Queuing and Coding Incoming MIDI Data
// ...
// More sophisticated sending algorithms
// [GRAME] improve efficiency by coding small groups of commands into a
// single packet, at the expense of increasing the sender queuing
// latency.
//
if (!outMidiBuffer.empty())
{
// Check if there is still room for more - like for 3 bytes or so)
if ((outMidiBuffer.size() + 1 + 3) > outMidiBuffer.max_size())
writeRtpMidiToAllParticipants();
else
outMidiBuffer.push_back(0x00); // zero timestamp
}
// We can't start the writing process here, as we do not know the length
// of what we are to send (The RtpMidi protocol start with writing the
// length of the buffer). So we'll copy to a buffer in the 'write' method,
// and actually serialize for real in the endTransmission method
#ifndef ONE_PARTICIPANT
return (dataPort.remoteIP() != (IPAddress)INADDR_NONE && participants.size() > 0);
#else
return (dataPort.remoteIP() != (IPAddress)INADDR_NONE && participant.ssrc != 0);
#endif
};
void write(byte byte)
{
// do we still have place in the buffer for 1 more character?
if ((outMidiBuffer.size()) + 2 > outMidiBuffer.max_size())
{
// buffer is almost full, only 1 more character
if (MIDI_NAMESPACE::MidiType::SystemExclusive == outMidiBuffer.front())
{
// Add Sysex at the end of this partial SysEx (in the last availble slot) ...
outMidiBuffer.push_back(MIDI_NAMESPACE::MidiType::SystemExclusiveStart);
writeRtpMidiToAllParticipants();
// and start again with a fresh continuation of
// a next SysEx block.
outMidiBuffer.clear();
outMidiBuffer.push_back(MIDI_NAMESPACE::MidiType::SystemExclusiveEnd);
}
else
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, BufferFullException, 0);
#endif
}
}
// store in local buffer, as we do *not* know the length of the message prior to sending
outMidiBuffer.push_back(byte);
};
void endTransmission()
{
};
// first things MIDI.read() calls in this method
// MIDI-read() must be called at the start of loop()
unsigned available()
{
now = millis();
#ifdef APPLEMIDI_INITIATOR
manageSessionInvites();
#endif
// All MIDI commands queued up in the same cycle (during 1 loop execution)
// are send in a single MIDI packet
if (outMidiBuffer.size() > 0)
writeRtpMidiToAllParticipants();
// assert(outMidiBuffer.size() == 0); // must be empty
if (inMidiBuffer.size() > 0)
return inMidiBuffer.size();
if (readDataPackets()) // from socket into dataBuffer
parseDataPackets(); // from dataBuffer into inMidiBuffer
if (readControlPackets()) // from socket into controlBuffer
parseControlPackets(); // from controlBuffer to AppleMIDI
manageReceiverFeedback();
manageSynchronization();
return inMidiBuffer.size();
};
byte read()
{
auto byte = inMidiBuffer.front();
inMidiBuffer.pop_front();
return byte;
};
protected:
UdpClass controlPort;
UdpClass dataPort;
private:
RtpBuffer_t controlBuffer;
RtpBuffer_t dataBuffer;
byte packetBuffer[Settings::UdpTxPacketMaxSize];
AppleMIDIParser<UdpClass, Settings, Platform> _appleMIDIParser;
rtpMIDIParser<UdpClass, Settings, Platform> _rtpMIDIParser;
connectedCallback _connectedCallback = nullptr;
disconnectedCallback _disconnectedCallback = nullptr;
#ifdef USE_EXT_CALLBACKS
startReceivedMidiByteCallback _startReceivedMidiByteCallback = nullptr;
receivedMidiByteCallback _receivedMidiByteCallback = nullptr;
endReceivedMidiByteCallback _endReceivedMidiByteCallback = nullptr;
receivedRtpCallback _receivedRtpCallback = nullptr;
sentRtpCallback _sentRtpCallback = nullptr;
sentRtpMidiCallback _sentRtpMidiCallback = nullptr;
exceptionCallback _exceptionCallback = nullptr;
#endif
// buffer for incoming and outgoing MIDI messages
MidiBuffer_t inMidiBuffer;
MidiBuffer_t outMidiBuffer;
rtpMidi_Clock rtpMidiClock;
ssrc_t ssrc = 0;
uint16_t port = DEFAULT_CONTROL_PORT;
#ifdef ONE_PARTICIPANT
Participant<Settings> participant;
#else
Deque<Participant<Settings>, Settings::MaxNumberOfParticipants> participants;
#endif
#ifdef KEEP_SESSION_NAME
char localName[DefaultSettings::MaxSessionNameLen + 1];
#endif
private:
size_t readControlPackets();
size_t readDataPackets();
void parseControlPackets();
void parseDataPackets();
void ReceivedInvitation (AppleMIDI_Invitation_t &, const amPortType &);
void ReceivedControlInvitation (AppleMIDI_Invitation_t &);
void ReceivedDataInvitation (AppleMIDI_Invitation_t &);
void ReceivedSynchronization (AppleMIDI_Synchronization_t &);
void ReceivedReceiverFeedback (AppleMIDI_ReceiverFeedback_t &);
void ReceivedEndSession (AppleMIDI_EndSession_t &);
void ReceivedBitrateReceiveLimit (AppleMIDI_BitrateReceiveLimit &);
void ReceivedInvitationAccepted (AppleMIDI_InvitationAccepted_t &, const amPortType &);
void ReceivedControlInvitationAccepted(AppleMIDI_InvitationAccepted_t &);
void ReceivedDataInvitationAccepted (AppleMIDI_InvitationAccepted_t &);
void ReceivedInvitationRejected (AppleMIDI_InvitationRejected_t &);
// rtpMIDI callback from parser
void ReceivedRtp(const Rtp_t &);
void StartReceivedMidi();
void ReceivedMidi(byte data);
void EndReceivedMidi();
// Helpers
void writeInvitation (UdpClass &, const IPAddress &, const uint16_t &, AppleMIDI_Invitation_t &, const byte *command);
void writeReceiverFeedback(const IPAddress &, const uint16_t &, AppleMIDI_ReceiverFeedback_t &);
void writeSynchronization (const IPAddress &, const uint16_t &, AppleMIDI_Synchronization_t &);
void writeEndSession (const IPAddress &, const uint16_t &, AppleMIDI_EndSession_t &);
void sendEndSession(Participant<Settings>*);
void writeRtpMidiToAllParticipants();
void writeRtpMidiBuffer(Participant<Settings>*);
void manageReceiverFeedback();
void manageSessionInvites();
void manageSynchronization();
void manageSynchronizationInitiator();
void manageSynchronizationInitiatorHeartBeat(Participant<Settings>*);
void manageSynchronizationInitiatorInvites(size_t);
void sendSynchronization(Participant<Settings>*);
#ifndef ONE_PARTICIPANT
Participant<Settings>* getParticipantBySSRC(const ssrc_t&);
Participant<Settings>* getParticipantByInitiatorToken(const uint32_t& initiatorToken);
#endif
#ifdef USE_DIRECTORY
bool IsComputerInDirectory(IPAddress) const;
#endif
};
END_APPLEMIDI_NAMESPACE
#include "AppleMIDI.hpp"
#define APPLEMIDI_CREATE_INSTANCE(Type, Name, SessionName, Port) \
APPLEMIDI_NAMESPACE::AppleMIDISession<Type> Apple##Name(SessionName, Port); \
MIDI_NAMESPACE::MidiInterface<APPLEMIDI_NAMESPACE::AppleMIDISession<Type>, APPLEMIDI_NAMESPACE::AppleMIDISettings> Name((APPLEMIDI_NAMESPACE::AppleMIDISession<Type>&)Apple##Name);
#if defined(ARDUINO_ARCH_ESP32) || defined(ESP32)
#define APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE() \
APPLEMIDI_CREATE_INSTANCE(WiFiUDP, MIDI, "AppleMIDI-ESP32", DEFAULT_CONTROL_PORT);
#elif defined(ESP8266)
#define APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE() \
APPLEMIDI_CREATE_INSTANCE(WiFiUDP, MIDI, "AppleMIDI-ESP8266", DEFAULT_CONTROL_PORT);
#else
#define APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE() \
APPLEMIDI_CREATE_INSTANCE(EthernetUDP, MIDI, "AppleMIDI-Arduino", DEFAULT_CONTROL_PORT);
#endif
#include "AppleMIDI_PlatformEnd.h"