1+ #ifndef MIDIHELPERS_h
2+ #define MIDIHELPERS_h
3+
4+ #define USE_EXT_CALLBACKS // as from example => required for MIDI callbacks
5+ #include < AppleMIDI.h> // https://github.com/lathoub/Arduino-AppleMIDI-Library
6+
7+ using namespace APPLEMIDI_NAMESPACE ;
8+
9+ /*
10+ class Utilities for creating a generic pointer to AppleMIDISession and MidiInterface
11+ from a WiFiUDP or EThernetUDP (template) and be able to manipulate it as a generic instance
12+ and manipulating the pointer generically
13+ */
14+
15+ class MidiClient {
16+ public:
17+ virtual void read () = 0;
18+ virtual ~MidiClient () {}
19+ virtual void setHandleConnected (void (*fptr)(const ssrc_t &, const char *))= 0;
20+ virtual void setHandleDisconnected (void (*fptr)(const ssrc_t &)) = 0;
21+ virtual void setHandleException (void (*fptr)(const ssrc_t &, const Exception &, const int32_t value))=0;
22+ virtual const char *getName () = 0;
23+ virtual const uint16_t getPort () = 0;
24+ virtual void begin () = 0;
25+ // all methods you need to be wrapped below
26+ virtual void sendNoteOn (byte note, byte velocity, byte channel) = 0;
27+ virtual void sendNoteOff (byte note, byte velocity, byte channel) = 0;
28+ // etc...
29+
30+ };
31+
32+ template <typename UdpType>
33+ class AppleMidiWithInterfaceWrapper : public MidiClient {
34+ public:
35+ AppleMIDISession<UdpType>* session;
36+ MidiInterface<AppleMIDISession<UdpType>, AppleMIDISettings>* midi;
37+
38+ AppleMidiWithInterfaceWrapper<UdpType>(const char * sessionName, uint16_t port) {
39+ session = new AppleMIDISession<UdpType>(sessionName, port);
40+ midi = new MidiInterface<AppleMIDISession<UdpType>, AppleMIDISettings>(*session);
41+ }
42+
43+ virtual void begin (){
44+ session->begin ();
45+ }
46+
47+ virtual const char *getName (){
48+ return session->getName ();
49+ }
50+
51+ virtual const uint16_t getPort (){
52+ return session->getPort ();
53+ }
54+
55+ virtual void setHandleConnected (void (*fptr)(const ssrc_t &, const char *)){
56+ session->setHandleConnected (fptr);
57+ }
58+
59+ virtual void setHandleDisconnected (void (*fptr)(const ssrc_t &)){
60+ session->setHandleDisconnected (fptr);
61+ }
62+
63+ virtual void setHandleException (void (*fptr)(const ssrc_t &, const Exception &, const int32_t value)){
64+ session->setHandleException (fptr);
65+ }
66+
67+
68+ void read () override {
69+ midi->read ();
70+ }
71+
72+ void sendNoteOn (byte note, byte velocity, byte channel) override {
73+ midi->sendNoteOn (note, velocity, channel);
74+ }
75+
76+ void sendNoteOff (byte note, byte velocity, byte channel) override {
77+ midi->sendNoteOff (note, velocity, channel);
78+ }
79+
80+
81+ ~AppleMidiWithInterfaceWrapper () {
82+ delete midi;
83+ delete session;
84+ }
85+ };
86+
87+ #endif
0 commit comments