From 41d363a9e1c6a05c6ddc7a4ed8139b71abde51d6 Mon Sep 17 00:00:00 2001 From: pschatzmann Date: Sun, 28 Sep 2025 15:44:22 +0200 Subject: [PATCH 1/7] Using templates to support multile networking stacks --- examples/ble-receive/ble-receive.ino | 7 +- examples/ble-send/ble-send.ino | 4 +- examples/serialbt-send/serialbt-send.ino | 1 + src/AppleMidiServer.cpp | 149 ---------------- src/AppleMidiServer.h | 215 +++++++++++++++++------ src/MidiIpServer.h | 11 +- src/MidiStreamIn.h | 13 +- src/MidiStreamOut.h | 14 +- src/MidiUdp.cpp | 50 ------ src/MidiUdp.h | 59 +++++-- src/MidiUdpServer.h | 3 +- 11 files changed, 224 insertions(+), 302 deletions(-) delete mode 100644 src/AppleMidiServer.cpp delete mode 100644 src/MidiUdp.cpp diff --git a/examples/ble-receive/ble-receive.ino b/examples/ble-receive/ble-receive.ino index 263ad5b..43c2e16 100644 --- a/examples/ble-receive/ble-receive.ino +++ b/examples/ble-receive/ble-receive.ino @@ -7,8 +7,8 @@ * @copyright Copyright (c) 2021 * */ -#include +#include MidiCallbackAction action; MidiBleServer ble("MidiServer", &action); @@ -27,8 +27,7 @@ void setup() { Serial.begin(115200); action.setCallbacks(onNoteOn, onNoteOff); - ble.begin(action); + ble.begin(); } -void loop() { -} +void loop() {} diff --git a/examples/ble-send/ble-send.ino b/examples/ble-send/ble-send.ino index 8327bf6..e749f16 100644 --- a/examples/ble-send/ble-send.ino +++ b/examples/ble-send/ble-send.ino @@ -11,10 +11,12 @@ #include MidiBleServer ble("MidiServer"); +int note = 30; +uint16_t amplitude = 100; // 0 to 128 void setup() { Serial.begin(115200); - ble.begin(action); + ble.begin(); ble.setDefaultSendingChannel(0); } diff --git a/examples/serialbt-send/serialbt-send.ino b/examples/serialbt-send/serialbt-send.ino index 684b6ad..de47632 100644 --- a/examples/serialbt-send/serialbt-send.ino +++ b/examples/serialbt-send/serialbt-send.ino @@ -7,6 +7,7 @@ * */ #include "Midi.h" +#include "BluetoothSerial.h" BluetoothSerial SerialBT; MidiStreamOut out(SerialBT); diff --git a/src/AppleMidiServer.cpp b/src/AppleMidiServer.cpp deleted file mode 100644 index 67e6a07..0000000 --- a/src/AppleMidiServer.cpp +++ /dev/null @@ -1,149 +0,0 @@ -#include "AppleMidiServer.h" -#if TCP_ACTIVE - -namespace midi { - -AppleMidiServer *SelfAppleMidi = nullptr; - -/// Starts the listening -bool AppleMidiServer :: begin(int control_port){ - MIDI_LOGI( __PRETTY_FUNCTION__); - if (WiFi.status() != WL_CONNECTED){ - MIDI_LOGE("WIFI is not connected"); - return false; - } - setupLogger(); - setupMDns(control_port); - MIDI_LOGI("MIDI using port: %d", control_port); - applemidi_init((apple_midi_cb_t) applemidi_callback_midi_message_received, (apple_midi_cb_t) applemidi_if_send_udp_datagram); - - // setup udp - udpControl.begin(control_port); // control port - udpData.begin(control_port+1); // data port - return true; -} - - -/// Starts a session with the indicated address -bool AppleMidiServer :: begin(IPAddress adress, int control_port, int data_port_opt){ - MIDI_LOGI( __PRETTY_FUNCTION__); - if (WiFi.status() != WL_CONNECTED){ - MIDI_LOGE("WIFI is not connected"); - return false; - } - setupLogger(); - setupMDns(control_port); - applemidi_init((apple_midi_cb_t) applemidi_callback_midi_message_received, (apple_midi_cb_t) applemidi_if_send_udp_datagram); - int data_port = data_port_opt > 0 ? data_port_opt : control_port+1; - MIDI_LOGI("MIDI using address: %s port: %d",toStr(adress), control_port); - // listen for udp on port - udpControl.begin(control_port); - udpData.begin(data_port); - int status = applemidi_start_session(data_port, (uint8_t*) &adress, control_port); - - return status>=0; -} - -const char* AppleMidiServer :: toStr(IPAddress &adress){ - static char networ_str[20]; - //adress.toString() is not supported on all environemnts - sprintf(networ_str,"%u.%u.%u.%u",adress[0],adress[1],adress[2],adress[3]); - return networ_str; -} - -void AppleMidiServer :: end(){ - MIDI_LOGI( __PRETTY_FUNCTION__); - udpControl.stop(); - udpData.stop(); -} - -/// process input from the control and the data port -bool AppleMidiServer :: tick(uint32_t timestamp){ - bool active = false; - - // process control port - int packetSize = udpControl.parsePacket(); - remote_port = udpControl.remotePort(); - if (packetSize>0){ - // We got some control data - IPAddress remote_address = udpControl.remoteIP(); - int len = udpControl.read(rx_buffer, MIDI_BUFFER_SIZE); - MIDI_LOGD("control: %d -> %d ",remote_port, len); - applemidi_parse_udp_datagram((uint8_t*)&remote_address, remote_port, rx_buffer, len, false); - active = true; - } - - // process data port - packetSize = udpData.parsePacket(); - if (packetSize>0){ - int remote_port = udpData.remotePort(); - IPAddress remote_address = udpData.remoteIP(); - int len = udpData.read(rx_buffer, MIDI_BUFFER_SIZE); - MIDI_LOGD("data: %d -> %d",remote_port, len); - applemidi_parse_udp_datagram((uint8_t*) &remote_address, remote_port, rx_buffer, len, true); - active = true; - } - return active; -} - -/// MidiCommon implementation -void AppleMidiServer :: writeData(MidiMessage *msg, int len){ - MIDI_LOGI( __PRETTY_FUNCTION__); - applemidi_send_message(remote_port, (uint8_t*) msg, len*sizeof(MidiMessage)); -} - -/// Setup MDNS apple-midi service -void AppleMidiServer :: setupMDns(int port) { -#if MDNS_ACTIVE - if(MDNS.begin(dns_name)) { - MDNS.addService("_apple-midi", "_udp", port); - } else { - MIDI_LOGE("Error starting mDNS"); - } -#else - MIDI_LOGW("MDNS has been deactivated"); -#endif -} - -/// Activate apple midi debug messages -void AppleMidiServer :: setupLogger() { - switch(MidiLogLevel){ - case MidiInfo: - applemidi_set_debug_level(2); - break; - case MidiDebug: - applemidi_set_debug_level(3); - break; - case MidiWarning: - applemidi_set_debug_level(1); - break; - case MidiError: - applemidi_set_debug_level(1); - break; - } -} - -/// Callback method to parse midi message -void AppleMidiServer :: applemidi_callback_midi_message_received(uint8_t port, uint32_t timestamp, uint8_t midi_status, uint8_t *remaining_message, size_t len, size_t continued_sysex_pos) { - MIDI_LOGI("applemidi_callback_midi_message_received: port=%d", port); - // the parser expects a midi message with the status and the parameters - uint8_t message[len+1]; - message[0]=midi_status; - memmove(message+1,remaining_message, len); - SelfAppleMidi->apple_event_handler.parse(message, len+1); -} - -/// Callback method to send UDP message with the help of the Arduino API -int32_t AppleMidiServer :: applemidi_if_send_udp_datagram(uint8_t *ip_addr, uint16_t port, uint8_t *tx_data, size_t tx_len){ - MIDI_LOGD( "applemidi_if_send_udp_datagram: port=%d", port); - IPAddress *p_adr = (IPAddress *) ip_addr; - WiFiUDP *p_udp = port == SelfAppleMidi->remote_port ? &(SelfAppleMidi->udpControl) : &(SelfAppleMidi->udpData); - p_udp->beginPacket(*p_adr, port); - int32_t result = p_udp->write(tx_data, tx_len); - p_udp->endPacket(); - return result; -} - -} - -#endif \ No newline at end of file diff --git a/src/AppleMidiServer.h b/src/AppleMidiServer.h index 438b83d..8927e60 100644 --- a/src/AppleMidiServer.h +++ b/src/AppleMidiServer.h @@ -2,11 +2,12 @@ #include "ConfigMidi.h" #if TCP_ACTIVE #include +#include + #include "MidiAction.h" #include "MidiCommon.h" #include "MidiLogger.h" #include "apple-midi/applemidi.h" -#include #if MDNS_ACTIVE #include #endif @@ -15,75 +16,177 @@ namespace midi { -class AppleMidiServer; -extern AppleMidiServer *SelfAppleMidi; typedef void* apple_midi_cb_t; /***************************************************/ -/*! \class AppleMidiServer +/*! \class AppleMidiServer \brief A Sender and Receiver which supports Apple Midi using the implementation from midibox. Apple midi uses UDP on a control and a data port. https://github.com/midibox/esp32-idf-applemidi - + by Phil Schatzmann */ /***************************************************/ +template +class AppleMidiServer : public MidiCommon { + public: + AppleMidiServer() {} + + AppleMidiServer(MidiAction* action, int midiPort = -1) { + apple_event_handler.begin(action, midiPort); + } + + void setName(const char* name) { dns_name = name; } + + bool begin(int control_port = APPLEMIDI_DEFAULT_PORT) { + _instance = this; + MIDI_LOGI(__PRETTY_FUNCTION__); + setupLogger(); + setupMDns(control_port); + MIDI_LOGI("MIDI using port: %d", control_port); + applemidi_init((apple_midi_cb_t)applemidi_callback_midi_message_received, + (apple_midi_cb_t)applemidi_if_send_udp_datagram); + udpControl.begin(control_port); // control port + udpData.begin(control_port + 1); // data port + return true; + } + + bool begin(IPAddress adress, int control_port = APPLEMIDI_DEFAULT_PORT, + int data_port_opt = -1) { + _instance = this; + MIDI_LOGI(__PRETTY_FUNCTION__); + setupLogger(); + setupMDns(control_port); + applemidi_init((apple_midi_cb_t)applemidi_callback_midi_message_received, + (apple_midi_cb_t)applemidi_if_send_udp_datagram); + int data_port = data_port_opt > 0 ? data_port_opt : control_port + 1; + MIDI_LOGI("MIDI using address: %s port: %d", toStr(adress), control_port); + udpControl.begin(control_port); + udpData.begin(data_port); + int status = + applemidi_start_session(data_port, (uint8_t*)&adress, control_port); + return status >= 0; + } + + void end() { + MIDI_LOGI(__PRETTY_FUNCTION__); + udpControl.stop(); + udpData.stop(); + } -class AppleMidiServer : public MidiCommon { - public: - AppleMidiServer() { - SelfAppleMidi = this; - }; - - AppleMidiServer(MidiAction *action, int midiPort=-1){ - SelfAppleMidi = this; - apple_event_handler.begin(action, midiPort); - } - - /// Defines the dns name - void setName(const char* name){ - dns_name = name; - } - - /// Starts the listening - bool begin(int control_port=APPLEMIDI_DEFAULT_PORT); - /// Starts a session with the indicated address - bool begin(IPAddress adress, int control_port=APPLEMIDI_DEFAULT_PORT, int data_port_opt=-1); - /// Closes the connections - void end(); - /// Processing logic to be executed in loop - bool loop() { - return tick(millis()); - } - - protected: - MidiParser apple_event_handler; - WiFiUDP udpControl; - WiFiUDP udpData; - uint8_t rx_buffer[MIDI_BUFFER_SIZE]; - int remote_port; - bool is_setup = false; - const char* dns_name = "AppleMidi"; - - /// process input from the control and the data port - virtual bool tick(uint32_t timestamp); - /// MidiCommon implementation - virtual void writeData(MidiMessage *msg, int len); - /// Setup MDNS apple-midi service - virtual void setupMDns(int port); - /// provides the network address as string - const char* toStr(IPAddress &adress); - /// Activate apple midi debug messages - void setupLogger(); - /// Callback method to parse midi message - static void applemidi_callback_midi_message_received(uint8_t port, uint32_t timestamp, uint8_t midi_status, uint8_t *remaining_message, size_t len, size_t continued_sysex_pos); - /// Callback method to send UDP message with the help of the Arduino API - static int32_t applemidi_if_send_udp_datagram(uint8_t *ip_addr, uint16_t port, uint8_t *tx_data, size_t tx_len); + bool loop() { return tick(millis()); } + protected: + MidiParser apple_event_handler; + UDPClass udpControl; + UDPClass udpData; + uint8_t rx_buffer[MIDI_BUFFER_SIZE]; + int remote_port; + bool is_setup = false; + const char* dns_name = "AppleMidi"; + static AppleMidiServer* _instance; + virtual bool tick(uint32_t timestamp) { + bool active = false; + int packetSize = udpControl.parsePacket(); + remote_port = udpControl.remotePort(); + if (packetSize > 0) { + IPAddress remote_address = udpControl.remoteIP(); + int len = udpControl.read(rx_buffer, MIDI_BUFFER_SIZE); + MIDI_LOGD("control: %d -> %d ", remote_port, len); + applemidi_parse_udp_datagram((uint8_t*)&remote_address, remote_port, + rx_buffer, len, false); + active = true; + } + packetSize = udpData.parsePacket(); + if (packetSize > 0) { + int remote_port = udpData.remotePort(); + IPAddress remote_address = udpData.remoteIP(); + int len = udpData.read(rx_buffer, MIDI_BUFFER_SIZE); + MIDI_LOGD("data: %d -> %d", remote_port, len); + applemidi_parse_udp_datagram((uint8_t*)&remote_address, remote_port, + rx_buffer, len, true); + active = true; + } + return active; + } + + virtual void writeData(MidiMessage* msg, int len) { + MIDI_LOGI(__PRETTY_FUNCTION__); + applemidi_send_message(remote_port, (uint8_t*)msg, + len * sizeof(MidiMessage)); + } + + virtual void setupMDns(int port) { +#if MDNS_ACTIVE + if (MDNS.begin(dns_name)) { + MDNS.addService("_apple-midi", "_udp", port); + } else { + MIDI_LOGE("Error starting mDNS"); + } +#else + MIDI_LOGW("MDNS has been deactivated"); +#endif + } + + const char* toStr(IPAddress& adress) { + static char networ_str[20]; + sprintf(networ_str, "%u.%u.%u.%u", adress[0], adress[1], adress[2], + adress[3]); + return networ_str; + } + + void setupLogger() { + switch (MidiLogLevel) { + case MidiInfo: + applemidi_set_debug_level(2); + break; + case MidiDebug: + applemidi_set_debug_level(3); + break; + case MidiWarning: + applemidi_set_debug_level(1); + break; + case MidiError: + applemidi_set_debug_level(1); + break; + } + } + + static void applemidi_callback_midi_message_received( + uint8_t port, uint32_t timestamp, uint8_t midi_status, + uint8_t* remaining_message, size_t len, size_t continued_sysex_pos) { + MIDI_LOGI("applemidi_callback_midi_message_received: port=%d", port); + if (_instance) { + uint8_t message[len + 1]; + message[0] = midi_status; + memmove(message + 1, remaining_message, len); + _instance->apple_event_handler.parse(message, len + 1); + } + } + + static int32_t applemidi_if_send_udp_datagram(uint8_t* ip_addr, uint16_t port, + uint8_t* tx_data, + size_t tx_len) { + MIDI_LOGD("applemidi_if_send_udp_datagram: port=%d", port); + if (_instance) { + IPAddress* p_adr = (IPAddress*)ip_addr; + UDPClass* p_udp = port == _instance->remote_port + ? &(_instance->udpControl) + : &(_instance->udpData); + p_udp->beginPacket(*p_adr, port); + int32_t result = p_udp->write(tx_data, tx_len); + p_udp->endPacket(); + return result; + } + return 0; + } }; -} +template +AppleMidiServer* AppleMidiServer::_instance = nullptr; + +} // namespace midi #endif \ No newline at end of file diff --git a/src/MidiIpServer.h b/src/MidiIpServer.h index 7088068..d553dd1 100644 --- a/src/MidiIpServer.h +++ b/src/MidiIpServer.h @@ -17,7 +17,7 @@ namespace midi { by Phil Schatzmann */ /***************************************************/ - +template class MidiIpServer : public MidiServer { public: MidiIpServer(MidiAction *action) : MidiServer(action){ @@ -32,11 +32,6 @@ class MidiIpServer : public MidiServer { bool begin(int serverPort=5008){ MIDI_LOGI( __PRETTY_FUNCTION__); - if (WiFi.status() != WL_CONNECTED){ - MIDI_LOGE("WiFi not connected"); - return false; - } - if (p_wifi_server==nullptr){ p_wifi_server = new WiFiServer(serverPort); } @@ -70,8 +65,8 @@ class MidiIpServer : public MidiServer { } protected: - WiFiServer *p_wifi_server = nullptr; - WiFiClient client; + ServerClass *p_wifi_server = nullptr; + ClientClass client; }; diff --git a/src/MidiStreamIn.h b/src/MidiStreamIn.h index 56d65c3..d1c893e 100644 --- a/src/MidiStreamIn.h +++ b/src/MidiStreamIn.h @@ -26,6 +26,7 @@ namespace midi { /***************************************************/ class MidiStreamIn : public MidiCommon { public: + MidiStreamIn() = default; /// Default Constructor MidiStreamIn(Stream &stream, MidiAction &action); /// Constructor to implement you custom MidiParser @@ -33,13 +34,10 @@ class MidiStreamIn : public MidiCommon { /// Destructor ~MidiStreamIn(); // Parse/Process the next midi message - bool loop(); - + bool loop(); + /// Call setup when created with empty constructor + void setup(Stream *stream, MidiParser *handler, bool releaseHandler); protected: - friend class MidiServer; - friend class MidiIpServer; - friend class MidiUdpServer; - int getLastStatusPos(uint8_t *buffer, int endPos); Stream *pStream = nullptr; MidiParser *pHandler = nullptr; @@ -47,9 +45,6 @@ class MidiStreamIn : public MidiCommon { uint8_t buffer[BUFFER_LEN]; int startPos = 0; - MidiStreamIn() = default; - - void setup(Stream *stream, MidiParser *handler, bool releaseHandler); }; } // namespace diff --git a/src/MidiStreamOut.h b/src/MidiStreamOut.h index a80b2e6..0a58e3b 100644 --- a/src/MidiStreamOut.h +++ b/src/MidiStreamOut.h @@ -16,19 +16,13 @@ namespace midi { /***************************************************/ class MidiStreamOut : public MidiCommon { public: + MidiStreamOut() = default; /// Default Constructor MidiStreamOut(Print &stream); - - protected: - friend class MidiServer; - friend class MidiIpServer; - friend class MidiUdpServer; - - MidiStreamOut() = default; - - virtual void writeData(MidiMessage *pMsg, int len); - + /// Call setup when created with empty constructor virtual void setup(Print *stream); + virtual void writeData(MidiMessage *pMsg, int len); + protected: Print *pStream; uint8_t buffer[4]; diff --git a/src/MidiUdp.cpp b/src/MidiUdp.cpp deleted file mode 100644 index f879a4a..0000000 --- a/src/MidiUdp.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include -#if MIDI_ACTIVE && TCP_ACTIVE - -#include "MidiLogger.h" - -namespace midi { - -const char* APP_MidiUdp = "MidiUdp"; - -MidiUdp :: MidiUdp(char* addessStr,int targetPort) { - this->isValidHostFlag = WiFi.hostByName(addessStr, targetUdpAddress); - if (!this->isValidHostFlag){ - MIDI_LOGE( "x%x, Could not resolve host %s ", __func__, addessStr); - } - this->targetPort = targetPort; -} - -MidiUdp :: MidiUdp(IPAddress address,int targetPort) { - this->targetUdpAddress = address; - this->targetPort = targetPort; -} - -bool MidiUdp :: isValidHost() { - return this->isValidHostFlag; -} - -void MidiUdp :: setTargetPort(int port){ - targetPort = port; -} - -size_t MidiUdp :: write(const uint8_t * buffer, size_t size ) { - size_t result = 0; - if (this->beginPacket(targetUdpAddress, targetPort) == 1){ - result = WiFiUDP::write(buffer, size); - if (result>0){ - bool packetOk = this->endPacket(); - MIDI_LOGD( "x%x, Number of bytes have %s been sent out: %d ", __func__, packetOk?"":"not", result); - } - //this->flush(); - } else { - MIDI_LOGD( "x%x, beginPacked has failed ", __func__); - this->isValidHostFlag = false; - } - return result; -} - -} // stk - -#endif - diff --git a/src/MidiUdp.h b/src/MidiUdp.h index a1fac7d..e97c929 100644 --- a/src/MidiUdp.h +++ b/src/MidiUdp.h @@ -5,12 +5,13 @@ #include #include #include +#include "MidiLogger.h" namespace midi { /***************************************************/ /*! \class MidiUdp - \brief Simple UDP wrapper class which sends all + \brief Simple UDP wrapper class which sends all packages to the same destination. This is useful when the API asks for a stream @@ -21,20 +22,50 @@ namespace midi { */ /***************************************************/ -class MidiUdp : public WiFiUDP { // EthernetUDP { - public: - MidiUdp(char* targetUdpAddressStr,int targetPort); - MidiUdp(IPAddress targetUdpAddress,int targetPort); - size_t write(const uint8_t * buffer,size_t size ); - bool isValidHost(); - void setTargetPort(int port); - - protected: - IPAddress targetUdpAddress; - int targetPort; - bool isValidHostFlag; +template +class MidiUdp : public UDPClass { // EthernetUDP { + + public: + MidiUdp(char* addessStr, int targetPort) { + this->isValidHostFlag = WiFi.hostByName(addessStr, targetUdpAddress); + if (!this->isValidHostFlag) { + MIDI_LOGE("x%x, Could not resolve host %s ", __func__, addessStr); + } + this->targetPort = targetPort; + } + + MidiUdp(IPAddress address, int targetPort) { + this->targetUdpAddress = address; + this->targetPort = targetPort; + } + + size_t write(const uint8_t* buffer, size_t size) { + size_t result = 0; + if (this->beginPacket(targetUdpAddress, targetPort) == 1) { + result = WiFiUDP::write(buffer, size); + if (result > 0) { + bool packetOk = this->endPacket(); + MIDI_LOGD("x%x, Number of bytes have %s been sent out: %d ", __func__, + packetOk ? "" : "not", result); + } + // this->flush(); + } else { + MIDI_LOGD("x%x, beginPacked has failed ", __func__); + this->isValidHostFlag = false; + } + return result; + } + + bool isValidHost() { return this->isValidHostFlag; } + + void setTargetPort(int port) { targetPort = port; } + + protected: + IPAddress targetUdpAddress; + int targetPort; + bool isValidHostFlag; }; -} +} // namespace midi #endif \ No newline at end of file diff --git a/src/MidiUdpServer.h b/src/MidiUdpServer.h index 5425c53..a463114 100644 --- a/src/MidiUdpServer.h +++ b/src/MidiUdpServer.h @@ -18,6 +18,7 @@ namespace midi { */ /***************************************************/ +template class MidiUdpServer : public MidiServer { public: MidiUdpServer(MidiAction *action):MidiServer(action){ @@ -63,7 +64,7 @@ class MidiUdpServer : public MidiServer { } protected: - MidiUdp *udp=nullptr; + MidiUdp *udp=nullptr; bool is_connected = false; int remote_port = 0; From b2115331298ed533a4855b0036cd963f51a33f37 Mon Sep 17 00:00:00 2001 From: pschatzmann Date: Sun, 28 Sep 2025 15:46:06 +0200 Subject: [PATCH 2/7] AppleMidiServer: define _instance in constructor --- src/AppleMidiServer.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/AppleMidiServer.h b/src/AppleMidiServer.h index 8927e60..0923193 100644 --- a/src/AppleMidiServer.h +++ b/src/AppleMidiServer.h @@ -32,16 +32,18 @@ typedef void* apple_midi_cb_t; template class AppleMidiServer : public MidiCommon { public: - AppleMidiServer() {} + AppleMidiServer() { + _instance = this; + }; AppleMidiServer(MidiAction* action, int midiPort = -1) { + _instance = this; apple_event_handler.begin(action, midiPort); } void setName(const char* name) { dns_name = name; } bool begin(int control_port = APPLEMIDI_DEFAULT_PORT) { - _instance = this; MIDI_LOGI(__PRETTY_FUNCTION__); setupLogger(); setupMDns(control_port); From b5b34cda3769d9dd3ad921d648a5a7ccbb16721a Mon Sep 17 00:00:00 2001 From: pschatzmann Date: Sun, 28 Sep 2025 15:55:26 +0200 Subject: [PATCH 3/7] Readme --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index a999437..eafb790 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,21 @@ MidiLogLevel = MidiDebug; // or MidiInfo, MidiWarning, MidiError All the midi classes are defined using the midi namespace. If you include Midi.h the using namespace is already defined. However, if you include the individual class specific header files you need to add a using namespace midi; in your sketch. +### Class Templates / Alternatives Networking Stacks + +The following classes support class templates for selecting the networking stack: + +- ```AppleMidiServer``` +- ```MidiIpserver``` +- ```MidiUdp``` +- ```MidiUdpServer``` + +By default the WiFi classes are used. If you want to use it with Ethernet please select the corresponding clases: + +- ```AppleMidiServer``` +- ```MidiIpserver``` +- ```MidiUdp``` +- ```MidiUdpServer``` ### Installation in Arduino From 346876646aebbaab34ed368e7b817999e325e2d7 Mon Sep 17 00:00:00 2001 From: pschatzmann Date: Sun, 28 Sep 2025 15:59:11 +0200 Subject: [PATCH 4/7] doxygen --- docs/html/_apple_midi_server_8h_source.html | 259 +++++++++++++----- docs/html/_midi_common_8h_source.html | 47 ++-- docs/html/_midi_ip_server_8h_source.html | 93 +++---- docs/html/_midi_server_8h_source.html | 6 +- docs/html/_midi_stream_in_8h_source.html | 46 ++-- docs/html/_midi_stream_out_8h_source.html | 31 +-- docs/html/_midi_udp_8h_source.html | 79 ++++-- docs/html/_midi_udp_server_8h_source.html | 115 ++++---- ...assmidi_1_1_apple_midi_server-members.html | 58 ++-- .../html/classmidi_1_1_apple_midi_server.html | 150 +++++----- docs/html/classmidi_1_1_apple_midi_server.png | Bin 604 -> 724 bytes ...classmidi_1_1_midi_ble_client-members.html | 55 ++-- docs/html/classmidi_1_1_midi_ble_client.html | 3 + ...classmidi_1_1_midi_ble_server-members.html | 59 ++-- docs/html/classmidi_1_1_midi_ble_server.html | 3 + .../classmidi_1_1_midi_common-members.html | 49 ++-- docs/html/classmidi_1_1_midi_common.html | 19 +- docs/html/classmidi_1_1_midi_common.png | Bin 2336 -> 3574 bytes .../classmidi_1_1_midi_ip_server-members.html | 75 ++--- docs/html/classmidi_1_1_midi_ip_server.html | 50 ++-- docs/html/classmidi_1_1_midi_ip_server.png | Bin 754 -> 1223 bytes .../classmidi_1_1_midi_server-members.html | 59 ++-- docs/html/classmidi_1_1_midi_server.html | 9 +- docs/html/classmidi_1_1_midi_server.png | Bin 976 -> 1828 bytes .../classmidi_1_1_midi_stream_in-members.html | 18 +- docs/html/classmidi_1_1_midi_stream_in.html | 23 +- ...classmidi_1_1_midi_stream_out-members.html | 16 +- docs/html/classmidi_1_1_midi_stream_out.html | 29 +- docs/html/classmidi_1_1_midi_udp-members.html | 20 +- docs/html/classmidi_1_1_midi_udp.html | 50 ++-- docs/html/classmidi_1_1_midi_udp.png | Bin 397 -> 582 bytes ...classmidi_1_1_midi_udp_server-members.html | 75 ++--- docs/html/classmidi_1_1_midi_udp_server.html | 48 ++-- docs/html/classmidi_1_1_midi_udp_server.png | Bin 774 -> 986 bytes docs/html/functions.html | 43 +-- docs/html/functions_func.html | 43 +-- docs/html/hierarchy.html | 9 +- docs/html/index.html | 16 ++ docs/html/menudata.js | 4 - docs/html/search/all_0.js | 6 +- docs/html/search/all_1.js | 2 +- docs/html/search/all_2.js | 4 +- docs/html/search/all_3.js | 2 +- docs/html/search/all_4.js | 3 +- docs/html/search/all_5.js | 3 +- docs/html/search/all_6.js | 20 +- docs/html/search/all_7.js | 20 +- docs/html/search/all_8.js | 5 +- docs/html/search/all_9.js | 6 +- docs/html/search/all_a.js | 5 +- docs/html/search/all_b.js | 5 +- docs/html/search/all_c.js | 8 +- docs/html/search/all_d.js | 3 +- docs/html/search/classes_0.js | 2 +- docs/html/search/classes_1.js | 35 +-- docs/html/search/functions_0.js | 4 +- docs/html/search/functions_1.js | 2 +- docs/html/search/functions_2.js | 4 +- docs/html/search/functions_3.js | 2 +- docs/html/search/functions_4.js | 3 +- docs/html/search/functions_5.js | 3 +- docs/html/search/functions_6.js | 7 +- docs/html/search/functions_7.js | 8 +- docs/html/search/functions_8.js | 5 +- docs/html/search/functions_9.js | 6 +- docs/html/search/functions_a.js | 5 +- docs/html/search/functions_b.js | 5 +- docs/html/search/functions_c.js | 8 +- docs/html/search/functions_d.js | 3 +- docs/html/search/pages_0.js | 2 +- docs/html/search/searchdata.js | 4 +- 71 files changed, 967 insertions(+), 892 deletions(-) diff --git a/docs/html/_apple_midi_server_8h_source.html b/docs/html/_apple_midi_server_8h_source.html index dcaf9c7..cadd9d2 100644 --- a/docs/html/_apple_midi_server_8h_source.html +++ b/docs/html/_apple_midi_server_8h_source.html @@ -73,86 +73,193 @@
2 #include "ConfigMidi.h"
3 #if TCP_ACTIVE
4 #include <WiFi.h>
-
5 #include "MidiAction.h"
-
6 #include "MidiCommon.h"
-
7 #include "MidiLogger.h"
-
8 #include "apple-midi/applemidi.h"
-
9 #include <WiFiUdp.h>
-
10 #if MDNS_ACTIVE
-
11 #include <ESPmDNS.h>
-
12 #endif
-
13 
-
14 #define MIDI_BUFFER_SIZE 80
-
15 
-
16 namespace midi {
-
17 
-
18 class AppleMidiServer;
-
19 extern AppleMidiServer *SelfAppleMidi;
-
20 typedef void* apple_midi_cb_t;
-
21 
-
22 /***************************************************/
-
32 /***************************************************/
-
33 
-
34 class AppleMidiServer : public MidiCommon {
-
35  public:
-
36  AppleMidiServer() {
-
37  SelfAppleMidi = this;
-
38  };
-
39 
-
40  AppleMidiServer(MidiAction *action, int midiPort=-1){
-
41  SelfAppleMidi = this;
-
42  apple_event_handler.begin(action, midiPort);
-
43  }
-
44 
-
46  void setName(const char* name){
-
47  dns_name = name;
-
48  }
-
49 
-
51  bool begin(int control_port=APPLEMIDI_DEFAULT_PORT);
-
53  bool begin(IPAddress adress, int control_port=APPLEMIDI_DEFAULT_PORT, int data_port_opt=-1);
-
55  void end();
-
57  bool loop() {
-
58  return tick(millis());
-
59  }
-
60 
-
61  protected:
-
62  MidiParser apple_event_handler;
-
63  WiFiUDP udpControl;
-
64  WiFiUDP udpData;
-
65  uint8_t rx_buffer[MIDI_BUFFER_SIZE];
-
66  int remote_port;
-
67  bool is_setup = false;
-
68  const char* dns_name = "AppleMidi";
-
69 
-
71  virtual bool tick(uint32_t timestamp);
-
73  virtual void writeData(MidiMessage *msg, int len);
-
75  virtual void setupMDns(int port);
-
77  const char* toStr(IPAddress &adress);
-
79  void setupLogger();
-
81  static void applemidi_callback_midi_message_received(uint8_t port, uint32_t timestamp, uint8_t midi_status, uint8_t *remaining_message, size_t len, size_t continued_sysex_pos);
-
83  static int32_t applemidi_if_send_udp_datagram(uint8_t *ip_addr, uint16_t port, uint8_t *tx_data, size_t tx_len);
-
84 
-
85 
-
86 };
-
87 
-
88 }
-
89 #endif
-
A Sender and Receiver which supports Apple Midi using the implementation from midibox....
Definition: AppleMidiServer.h:34
-
bool loop()
Processing logic to be executed in loop.
Definition: AppleMidiServer.h:57
-
virtual void setupMDns(int port)
Setup MDNS apple-midi service.
Definition: AppleMidiServer.cpp:96
-
virtual bool tick(uint32_t timestamp)
process input from the control and the data port
Definition: AppleMidiServer.cpp:61
-
static void applemidi_callback_midi_message_received(uint8_t port, uint32_t timestamp, uint8_t midi_status, uint8_t *remaining_message, size_t len, size_t continued_sysex_pos)
Callback method to parse midi message.
Definition: AppleMidiServer.cpp:127
-
void setupLogger()
Activate apple midi debug messages.
Definition: AppleMidiServer.cpp:109
-
const char * toStr(IPAddress &adress)
provides the network address as string
Definition: AppleMidiServer.cpp:47
-
void end()
Closes the connections.
Definition: AppleMidiServer.cpp:54
-
bool begin(int control_port=APPLEMIDI_DEFAULT_PORT)
Starts the listening.
Definition: AppleMidiServer.cpp:9
-
void setName(const char *name)
Defines the dns name.
Definition: AppleMidiServer.h:46
-
static int32_t applemidi_if_send_udp_datagram(uint8_t *ip_addr, uint16_t port, uint8_t *tx_data, size_t tx_len)
Callback method to send UDP message with the help of the Arduino API.
Definition: AppleMidiServer.cpp:137
-
virtual void writeData(MidiMessage *msg, int len)
MidiCommon implementation.
Definition: AppleMidiServer.cpp:90
+
5 #include <WiFiUdp.h>
+
6 
+
7 #include "MidiAction.h"
+
8 #include "MidiCommon.h"
+
9 #include "MidiLogger.h"
+
10 #include "apple-midi/applemidi.h"
+
11 #if MDNS_ACTIVE
+
12 #include <ESPmDNS.h>
+
13 #endif
+
14 
+
15 #define MIDI_BUFFER_SIZE 80
+
16 
+
17 namespace midi {
+
18 
+
19 typedef void* apple_midi_cb_t;
+
20 
+
21 /***************************************************/
+
31 /***************************************************/
+
32 template <class UDPClass = WiFiUDP>
+
33 class AppleMidiServer : public MidiCommon {
+
34  public:
+
35  AppleMidiServer() {
+
36  _instance = this;
+
37  };
+
38 
+
39  AppleMidiServer(MidiAction* action, int midiPort = -1) {
+
40  _instance = this;
+
41  apple_event_handler.begin(action, midiPort);
+
42  }
+
43 
+
44  void setName(const char* name) { dns_name = name; }
+
45 
+
46  bool begin(int control_port = APPLEMIDI_DEFAULT_PORT) {
+
47  MIDI_LOGI(__PRETTY_FUNCTION__);
+
48  setupLogger();
+
49  setupMDns(control_port);
+
50  MIDI_LOGI("MIDI using port: %d", control_port);
+
51  applemidi_init((apple_midi_cb_t)applemidi_callback_midi_message_received,
+
52  (apple_midi_cb_t)applemidi_if_send_udp_datagram);
+
53  udpControl.begin(control_port); // control port
+
54  udpData.begin(control_port + 1); // data port
+
55  return true;
+
56  }
+
57 
+
58  bool begin(IPAddress adress, int control_port = APPLEMIDI_DEFAULT_PORT,
+
59  int data_port_opt = -1) {
+
60  _instance = this;
+
61  MIDI_LOGI(__PRETTY_FUNCTION__);
+
62  setupLogger();
+
63  setupMDns(control_port);
+
64  applemidi_init((apple_midi_cb_t)applemidi_callback_midi_message_received,
+
65  (apple_midi_cb_t)applemidi_if_send_udp_datagram);
+
66  int data_port = data_port_opt > 0 ? data_port_opt : control_port + 1;
+
67  MIDI_LOGI("MIDI using address: %s port: %d", toStr(adress), control_port);
+
68  udpControl.begin(control_port);
+
69  udpData.begin(data_port);
+
70  int status =
+
71  applemidi_start_session(data_port, (uint8_t*)&adress, control_port);
+
72  return status >= 0;
+
73  }
+
74 
+
75  void end() {
+
76  MIDI_LOGI(__PRETTY_FUNCTION__);
+
77  udpControl.stop();
+
78  udpData.stop();
+
79  }
+
80 
+
81  bool loop() { return tick(millis()); }
+
82 
+
83  protected:
+
84  MidiParser apple_event_handler;
+
85  UDPClass udpControl;
+
86  UDPClass udpData;
+
87  uint8_t rx_buffer[MIDI_BUFFER_SIZE];
+
88  int remote_port;
+
89  bool is_setup = false;
+
90  const char* dns_name = "AppleMidi";
+
91  static AppleMidiServer<UDPClass>* _instance;
+
92 
+
93  virtual bool tick(uint32_t timestamp) {
+
94  bool active = false;
+
95  int packetSize = udpControl.parsePacket();
+
96  remote_port = udpControl.remotePort();
+
97  if (packetSize > 0) {
+
98  IPAddress remote_address = udpControl.remoteIP();
+
99  int len = udpControl.read(rx_buffer, MIDI_BUFFER_SIZE);
+
100  MIDI_LOGD("control: %d -> %d ", remote_port, len);
+
101  applemidi_parse_udp_datagram((uint8_t*)&remote_address, remote_port,
+
102  rx_buffer, len, false);
+
103  active = true;
+
104  }
+
105  packetSize = udpData.parsePacket();
+
106  if (packetSize > 0) {
+
107  int remote_port = udpData.remotePort();
+
108  IPAddress remote_address = udpData.remoteIP();
+
109  int len = udpData.read(rx_buffer, MIDI_BUFFER_SIZE);
+
110  MIDI_LOGD("data: %d -> %d", remote_port, len);
+
111  applemidi_parse_udp_datagram((uint8_t*)&remote_address, remote_port,
+
112  rx_buffer, len, true);
+
113  active = true;
+
114  }
+
115  return active;
+
116  }
+
117 
+
118  virtual void writeData(MidiMessage* msg, int len) {
+
119  MIDI_LOGI(__PRETTY_FUNCTION__);
+
120  applemidi_send_message(remote_port, (uint8_t*)msg,
+
121  len * sizeof(MidiMessage));
+
122  }
+
123 
+
124  virtual void setupMDns(int port) {
+
125 #if MDNS_ACTIVE
+
126  if (MDNS.begin(dns_name)) {
+
127  MDNS.addService("_apple-midi", "_udp", port);
+
128  } else {
+
129  MIDI_LOGE("Error starting mDNS");
+
130  }
+
131 #else
+
132  MIDI_LOGW("MDNS has been deactivated");
+
133 #endif
+
134  }
+
135 
+
136  const char* toStr(IPAddress& adress) {
+
137  static char networ_str[20];
+
138  sprintf(networ_str, "%u.%u.%u.%u", adress[0], adress[1], adress[2],
+
139  adress[3]);
+
140  return networ_str;
+
141  }
+
142 
+
143  void setupLogger() {
+
144  switch (MidiLogLevel) {
+
145  case MidiInfo:
+
146  applemidi_set_debug_level(2);
+
147  break;
+
148  case MidiDebug:
+
149  applemidi_set_debug_level(3);
+
150  break;
+
151  case MidiWarning:
+
152  applemidi_set_debug_level(1);
+
153  break;
+
154  case MidiError:
+
155  applemidi_set_debug_level(1);
+
156  break;
+
157  }
+
158  }
+
159 
+
160  static void applemidi_callback_midi_message_received(
+
161  uint8_t port, uint32_t timestamp, uint8_t midi_status,
+
162  uint8_t* remaining_message, size_t len, size_t continued_sysex_pos) {
+
163  MIDI_LOGI("applemidi_callback_midi_message_received: port=%d", port);
+
164  if (_instance) {
+
165  uint8_t message[len + 1];
+
166  message[0] = midi_status;
+
167  memmove(message + 1, remaining_message, len);
+
168  _instance->apple_event_handler.parse(message, len + 1);
+
169  }
+
170  }
+
171 
+
172  static int32_t applemidi_if_send_udp_datagram(uint8_t* ip_addr, uint16_t port,
+
173  uint8_t* tx_data,
+
174  size_t tx_len) {
+
175  MIDI_LOGD("applemidi_if_send_udp_datagram: port=%d", port);
+
176  if (_instance) {
+
177  IPAddress* p_adr = (IPAddress*)ip_addr;
+
178  UDPClass* p_udp = port == _instance->remote_port
+
179  ? &(_instance->udpControl)
+
180  : &(_instance->udpData);
+
181  p_udp->beginPacket(*p_adr, port);
+
182  int32_t result = p_udp->write(tx_data, tx_len);
+
183  p_udp->endPacket();
+
184  return result;
+
185  }
+
186  return 0;
+
187  }
+
188 };
+
189 
+
190 template <class UDPClass>
+ +
192 
+
193 } // namespace midi
+
194 #endif
+
A Sender and Receiver which supports Apple Midi using the implementation from midibox....
Definition: AppleMidiServer.h:33
Abstract class for a MidiAction.
Definition: MidiAction.h:15
The common methods provided by all Arduino Midi subclasses which can be used to generate Midi message...
Definition: MidiCommon.h:75
A simple Midi Parser which calls the corresponding events. It supports Midi and BLE Midi messages....
Definition: MidiParser.h:29
void begin(MidiAction *MidiAction, int filter_channel=-1)
Assigns the MidiAction and optinally defines a midi channel.
Definition: MidiParser.cpp:14
+
void parse(uint8_t *msg, uint8_t len)
Parse a string into midi messages.
Definition: MidiParser.cpp:25
The content of the midi message: timestamp, status, arg1 and arg2.
Definition: MidiCommon.h:48
diff --git a/docs/html/_midi_common_8h_source.html b/docs/html/_midi_common_8h_source.html index 2680c8f..06ed16c 100644 --- a/docs/html/_midi_common_8h_source.html +++ b/docs/html/_midi_common_8h_source.html @@ -171,40 +171,41 @@
132  void setConnectionStatus(ConnectionStatus status) {connectionStatus=status; }
133  void updateTimestamp(MidiMessage *pMsg);
134  virtual void writeData(MidiMessage *msg, int len);
-
135 
-
136  ConnectionStatus connectionStatus;
-
137  MidiAction *pMidiAction;
-
138  MidiMessage outMessage;
-
139  int receivingChannel = -1;
-
140  uint8_t sendingChannel = 0;
-
141  uint8_t timestampLow;
-
142  uint8_t timestampHigh;
-
143  char *name;
-
144 };
-
145 
+
135  uint8_t getChannel(uint8_t ch);
+
136 
+
137  ConnectionStatus connectionStatus;
+
138  MidiAction *pMidiAction;
+
139  MidiMessage outMessage;
+
140  int receivingChannel = -1;
+
141  uint8_t sendingChannel = 0;
+
142  uint8_t timestampLow;
+
143  uint8_t timestampHigh;
+
144  char *name;
+
145 };
146 
-
147 } // namespace
-
148 
-
149 #endif
+
147 
+
148 } // namespace
+
149 
+
150 #endif
Abstract class for a MidiAction.
Definition: MidiAction.h:15
The common methods provided by all Arduino Midi subclasses which can be used to generate Midi message...
Definition: MidiCommon.h:75
-
virtual void polyPressure(uint8_t valuePar, int8_t channel=-1)
Sends a polyPressure MIDI command to the output.
Definition: MidiCommon.cpp:68
+
virtual void polyPressure(uint8_t valuePar, int8_t channel=-1)
Sends a polyPressure MIDI command to the output.
Definition: MidiCommon.cpp:69
virtual void setFilterReceivingChannel(int channel)
Activates a filter on receiving messages to the indicated channel.
Definition: MidiCommon.cpp:19
-
virtual void localControl(bool active, int8_t channel=-1)
Sends a localControl MIDI command to the output.
Definition: MidiCommon.cpp:90
+
virtual void localControl(bool active, int8_t channel=-1)
Sends a localControl MIDI command to the output.
Definition: MidiCommon.cpp:93
virtual void setDefaultSendingChannel(int8_t channel)
Sets the default channel for the sending commands.
Definition: MidiCommon.cpp:15
-
static uint8_t frequencyToNote(float freq)
Converts a Frequency (in Hz) to a MIDI note.
Definition: MidiCommon.cpp:106
+
static uint8_t frequencyToNote(float freq)
Converts a Frequency (in Hz) to a MIDI note.
Definition: MidiCommon.cpp:110
virtual void channelPressure(uint8_t value, int8_t channel=-1)
Sends a channelPressure MIDI command to the output.
Definition: MidiCommon.cpp:61
MidiCommon()
Default Constructor.
Definition: MidiCommon.cpp:5
-
virtual void resetAllControllers(int8_t channel=-1)
Sends a resetAllControllers MIDI command to the output.
Definition: MidiCommon.cpp:86
+
virtual void resetAllControllers(int8_t channel=-1)
Sends a resetAllControllers MIDI command to the output.
Definition: MidiCommon.cpp:89
virtual void noteOff(uint8_t note, uint8_t velocity, int8_t channel=-1)
Sends a noteOff MIDI command to the output.
Definition: MidiCommon.cpp:45
virtual void noteOn(uint8_t note, uint8_t velocity, int8_t channel=-1)
Sends a noteOn MIDI command to the output.
Definition: MidiCommon.cpp:37
-
static float noteToFrequency(uint8_t note)
Converts a MIDI note to a frequency in Hz.
Definition: MidiCommon.cpp:101
-
virtual void controlChange(uint8_t msg, uint8_t value, int8_t channel=-1)
Sends a control change MIDI command to the output.
Definition: MidiCommon.cpp:94
-
void write(MidiMessage *msg, int len)
write multiple MidiMessage objects to final output
Definition: MidiCommon.cpp:113
+
static float noteToFrequency(uint8_t note)
Converts a MIDI note to a frequency in Hz.
Definition: MidiCommon.cpp:105
+
virtual void controlChange(uint8_t msg, uint8_t value, int8_t channel=-1)
Sends a control change MIDI command to the output.
Definition: MidiCommon.cpp:97
+
void write(MidiMessage *msg, int len)
write multiple MidiMessage objects to final output
Definition: MidiCommon.cpp:117
virtual ConnectionStatus getConnectionStatus()
Determines the connection status.
Definition: MidiCommon.h:126
virtual void pitchBend(uint16_t value, int8_t channel=-1)
Sends a pitchBend MIDI command to the output.
Definition: MidiCommon.cpp:53
-
virtual void allNotesOff(int8_t channel=-1)
Sends a allNotesOff MIDI command to the output.
Definition: MidiCommon.cpp:82
-
virtual void programChange(uint8_t program, int8_t channel=-1)
Sends a programChange MIDI command to the output.
Definition: MidiCommon.cpp:75
+
virtual void allNotesOff(int8_t channel=-1)
Sends a allNotesOff MIDI command to the output.
Definition: MidiCommon.cpp:85
+
virtual void programChange(uint8_t program, int8_t channel=-1)
Sends a programChange MIDI command to the output.
Definition: MidiCommon.cpp:77
virtual void setMidiAction(MidiAction &MidiAction)
Defines the voice which is used in inbound processing.
Definition: MidiCommon.cpp:11
The content of the midi message: timestamp, status, arg1 and arg2.
Definition: MidiCommon.h:48
diff --git a/docs/html/_midi_ip_server_8h_source.html b/docs/html/_midi_ip_server_8h_source.html index f22dec4..c558f21 100644 --- a/docs/html/_midi_ip_server_8h_source.html +++ b/docs/html/_midi_ip_server_8h_source.html @@ -82,7 +82,7 @@
11 
12 /***************************************************/
19 /***************************************************/
-
20 
+
20 template <class ServerClass = WiFiServer, class ClientClass = WiFiClient>
21 class MidiIpServer : public MidiServer {
22  public:
23  MidiIpServer(MidiAction *action) : MidiServer(action){
@@ -97,58 +97,55 @@
32  bool begin(int serverPort=5008){
33  MIDI_LOGI( __PRETTY_FUNCTION__);
34 
-
35  if (WiFi.status() != WL_CONNECTED){
-
36  MIDI_LOGE("WiFi not connected");
-
37  return false;
-
38  }
-
39 
-
40  if (p_wifi_server==nullptr){
-
41  p_wifi_server = new WiFiServer(serverPort);
-
42  }
-
43  p_wifi_server->begin();
-
44  MIDI_LOGI("server started on port %d", serverPort);
-
45  return true;
-
46  }
-
47 
-
48  void end() {
-
49  if (p_wifi_server!=nullptr){
-
50  delete p_wifi_server;
-
51  p_wifi_server = nullptr;
-
52  }
-
53  }
-
54 
-
55  void loop() {
-
56  if (p_wifi_server!=nullptr){
-
57  if (!client.connected()){
-
58  client = p_wifi_server->accept();
-
59  if (client.connected()){
-
60  MIDI_LOGI("MidiIpServer->connected");
-
61  in.setup(&client, new MidiParser(p_action), true);
-
62  out.setup(&client);
-
63  out.resetAllControllers();
-
64  }
-
65  } else {
-
66  MIDI_LOGD("MidiIpServer::loop");
-
67  in.loop();
-
68  }
-
69  }
-
70  }
-
71 
-
72  protected:
-
73  WiFiServer *p_wifi_server = nullptr;
-
74  WiFiClient client;
+
35  if (p_wifi_server==nullptr){
+
36  p_wifi_server = new WiFiServer(serverPort);
+
37  }
+
38  p_wifi_server->begin();
+
39  MIDI_LOGI("server started on port %d", serverPort);
+
40  return true;
+
41  }
+
42 
+
43  void end() {
+
44  if (p_wifi_server!=nullptr){
+
45  delete p_wifi_server;
+
46  p_wifi_server = nullptr;
+
47  }
+
48  }
+
49 
+
50  void loop() {
+
51  if (p_wifi_server!=nullptr){
+
52  if (!client.connected()){
+
53  client = p_wifi_server->accept();
+
54  if (client.connected()){
+
55  MIDI_LOGI("MidiIpServer->connected");
+
56  in.setup(&client, new MidiParser(p_action), true);
+
57  out.setup(&client);
+
58  out.resetAllControllers();
+
59  }
+
60  } else {
+
61  MIDI_LOGD("MidiIpServer::loop");
+
62  in.loop();
+
63  }
+
64  }
+
65  }
+
66 
+
67  protected:
+
68  ServerClass *p_wifi_server = nullptr;
+
69  ClientClass client;
+
70 
+
71 };
+
72 
+
73 
+
74 }
75 
-
76 };
-
77 
-
78 
-
79 }
-
80 
-
81 #endif
+
76 #endif
Abstract class for a MidiAction.
Definition: MidiAction.h:15
-
virtual void resetAllControllers(int8_t channel=-1)
Sends a resetAllControllers MIDI command to the output.
Definition: MidiCommon.cpp:86
+
virtual void resetAllControllers(int8_t channel=-1)
Sends a resetAllControllers MIDI command to the output.
Definition: MidiCommon.cpp:89
A simple IP Server which which receives and creates MIDI messages.
Definition: MidiIpServer.h:21
A simple Midi Parser which calls the corresponding events. It supports Midi and BLE Midi messages....
Definition: MidiParser.h:29
A simple Serial Server which which receives and creates MIDI messages.
Definition: MidiServer.h:20
+
void setup(Stream *stream, MidiParser *handler, bool releaseHandler)
Call setup when created with empty constructor.
Definition: MidiStreamIn.cpp:25
+
virtual void setup(Print *stream)
Call setup when created with empty constructor.
Definition: MidiStreamOut.cpp:12