diff --git a/examples/NoteOnOffEverySecEsp8266/NoteOnOffEverySecEsp8266.ino b/examples/Esp8266_NoteOnOffEverySec/Esp8266_NoteOnOffEverySec.ino similarity index 91% rename from examples/NoteOnOffEverySecEsp8266/NoteOnOffEverySecEsp8266.ino rename to examples/Esp8266_NoteOnOffEverySec/Esp8266_NoteOnOffEverySec.ino index 27aa89d..a504aea 100644 --- a/examples/NoteOnOffEverySecEsp8266/NoteOnOffEverySecEsp8266.ino +++ b/examples/Esp8266_NoteOnOffEverySec/Esp8266_NoteOnOffEverySec.ino @@ -5,8 +5,8 @@ #include "AppleMidi.h" -char ssid[] = "The Mighty Network"; // your network SSID (name) -char pass[] = "0208196700"; // your network password (use for WPA, or use as key for WEP) +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) unsigned long t0 = millis(); bool isConnected = false; @@ -91,18 +91,18 @@ void loop() // ----------------------------------------------------------------------------- // rtpMIDI session. Device connected // ----------------------------------------------------------------------------- -void OnAppleMidiConnected(char* name) { +void OnAppleMidiConnected(long unsigned int ssrc, char* name) { isConnected = true; - // Serial.print("Connected to session "); - // Serial.println(name); + Serial.print("Connected to session "); + Serial.println(name); } // ----------------------------------------------------------------------------- // rtpMIDI session. Device disconnected // ----------------------------------------------------------------------------- -void OnAppleMidiDisconnected() { +void OnAppleMidiDisconnected(long unsigned int ssrc) { isConnected = false; - // Serial.println("Disconnected"); + Serial.println("Disconnected"); } // ----------------------------------------------------------------------------- diff --git a/examples/EthernetShield_InitiateSession/EthernetShield_InitiateSession.ino b/examples/EthernetShield_InitiateSession/EthernetShield_InitiateSession.ino new file mode 100644 index 0000000..cd16c9a --- /dev/null +++ b/examples/EthernetShield_InitiateSession/EthernetShield_InitiateSession.ino @@ -0,0 +1,136 @@ + +#include +#include + +#include "AppleMidi.h" + +// Enter a MAC address for your controller below. +// Newer Ethernet shields have a MAC address printed on a sticker on the shield +byte mac[] = { + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED +}; + +unsigned long t0 = millis(); +bool isConnected = false; + +APPLEMIDI_CREATE_DEFAULT_INSTANCE(); // see definition in AppleMidi_Defs.h + +IPAddress remote(192, 168, 0, 119); // replace with remote ip + +// ----------------------------------------------------------------------------- +// +// ----------------------------------------------------------------------------- +void setup() +{ + // Serial communications and wait for port to open: + Serial.begin(115200); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + Serial.print("Getting IP address..."); + + if (Ethernet.begin(mac) == 0) { + Serial.println(); + Serial.println( "Failed DHCP, check network cable & reboot" ); + for (;;) + ; + } + + Serial.println(); + Serial.print("IP address is "); + Serial.println(Ethernet.localIP()); + + Serial.print("AppleMIDI Session "); + Serial.print(AppleMIDI.getSessionName()); + Serial.print(" with SSRC 0x"); + Serial.println(AppleMIDI.getSynchronizationSource(), HEX); + + // Create a session and wait for a remote host to connect to us + AppleMIDI.begin("test"); + + Serial.print("OK, now make an active connection to "); + Serial.println(remote); + + // This is the invite to the remote participant + AppleMIDI.invite(remote); + + AppleMIDI.OnConnected(OnAppleMidiConnected); + AppleMIDI.OnDisconnected(OnAppleMidiDisconnected); + + AppleMIDI.OnReceiveNoteOn(OnAppleMidiNoteOn); + AppleMIDI.OnReceiveNoteOff(OnAppleMidiNoteOff); + + Serial.println("Sending NoteOn/Off of note 45, every second"); +} + +// ----------------------------------------------------------------------------- +// +// ----------------------------------------------------------------------------- +void loop() +{ + // Listen to incoming notes + AppleMIDI.run(); + + // send a note every second + // (dont cáll delay(1000) as it will stall the pipeline) + if (isConnected && (millis() - t0) > 1000) + { + t0 = millis(); + // Serial.print("."); + + int note = 45; + int velocity = 55; + int channel = 1; + + AppleMIDI.noteOn(note, velocity, channel); + AppleMIDI.noteOff(note, velocity, channel); + } +} + +// ==================================================================================== +// Event handlers for incoming MIDI messages +// ==================================================================================== + +// ----------------------------------------------------------------------------- +// rtpMIDI session. Device connected +// ----------------------------------------------------------------------------- +void OnAppleMidiConnected(long unsigned int ssrc, char* name) { + isConnected = true; + Serial.print("Connected to session "); + Serial.println(name); +} + +// ----------------------------------------------------------------------------- +// rtpMIDI session. Device disconnected +// ----------------------------------------------------------------------------- +void OnAppleMidiDisconnected(long unsigned int ssrc) { + isConnected = false; + Serial.println("Disconnected"); +} + +// ----------------------------------------------------------------------------- +// +// ----------------------------------------------------------------------------- +void OnAppleMidiNoteOn(byte channel, byte note, byte velocity) { + Serial.print("Incoming NoteOn from channel:"); + Serial.print(channel); + Serial.print(" note:"); + Serial.print(note); + Serial.print(" velocity:"); + Serial.print(velocity); + Serial.println(); +} + +// ----------------------------------------------------------------------------- +// +// ----------------------------------------------------------------------------- +void OnAppleMidiNoteOff(byte channel, byte note, byte velocity) { + Serial.print("Incoming NoteOff from channel:"); + Serial.print(channel); + Serial.print(" note:"); + Serial.print(note); + Serial.print(" velocity:"); + Serial.print(velocity); + Serial.println(); +} diff --git a/examples/EthernetShield_InitiateSessions/EthernetShield_InitiateSessions.ino b/examples/EthernetShield_InitiateSessions/EthernetShield_InitiateSessions.ino new file mode 100644 index 0000000..0499fc9 --- /dev/null +++ b/examples/EthernetShield_InitiateSessions/EthernetShield_InitiateSessions.ino @@ -0,0 +1,140 @@ + +#include +#include + +#include "AppleMidi.h" + +// Enter a MAC address for your controller below. +// Newer Ethernet shields have a MAC address printed on a sticker on the shield +byte mac[] = { + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED +}; + +unsigned long t0 = millis(); +bool isConnected = false; + +APPLEMIDI_CREATE_DEFAULT_INSTANCE(); // see definition in AppleMidi_Defs.h + +IPAddress remote1(192, 168, 0, 119); // replace with remote ip +IPAddress remote2(192, 168, 0, 127); // replace with remote ip + +// ----------------------------------------------------------------------------- +// +// ----------------------------------------------------------------------------- +void setup() +{ + // Serial communications and wait for port to open: + Serial.begin(115200); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + Serial.print("Getting IP address..."); + + if (Ethernet.begin(mac) == 0) { + Serial.println(); + Serial.println( "Failed DHCP, check network cable & reboot" ); + for (;;) + ; + } + + Serial.println(); + Serial.print("IP address is "); + Serial.println(Ethernet.localIP()); + + // Create a session and wait for a remote host to connect to us + AppleMIDI.begin("Arduino"); + + Serial.print("AppleMIDI Session "); + Serial.print(AppleMIDI.getSessionName()); + Serial.print(" with SSRC 0x"); + Serial.println(AppleMIDI.getSynchronizationSource(), HEX); + + Serial.print("OK, now make an active connection to "); + Serial.print(remote1); + Serial.print(" and "); + Serial.println(remote2); + + // This is the invite to the remote participant + AppleMIDI.invite(remote1); + AppleMIDI.invite(remote2); + + AppleMIDI.OnConnected(OnAppleMidiConnected); + AppleMIDI.OnDisconnected(OnAppleMidiDisconnected); + + AppleMIDI.OnReceiveNoteOn(OnAppleMidiNoteOn); + AppleMIDI.OnReceiveNoteOff(OnAppleMidiNoteOff); + + Serial.println("Sending NoteOn/Off of note 45, every second"); +} + +// ----------------------------------------------------------------------------- +// +// ----------------------------------------------------------------------------- +void loop() +{ + // Listen to incoming notes + AppleMIDI.run(); + + // send a note every second + // (dont cáll delay(1000) as it will stall the pipeline) + if (isConnected && (millis() - t0) > 250) + { + t0 = millis(); + // Serial.print("."); + + int note = 45; + int velocity = 55; + int channel = 1; + + AppleMIDI.noteOn(note, velocity, channel); + AppleMIDI.noteOff(note, velocity, channel); + } +} + +// ==================================================================================== +// Event handlers for incoming MIDI messages +// ==================================================================================== + +// ----------------------------------------------------------------------------- +// rtpMIDI session. Device connected +// ----------------------------------------------------------------------------- +void OnAppleMidiConnected(long unsigned int ssrc, char* name) { + isConnected = true; + Serial.print("Connected to session "); + Serial.println(name); +} + +// ----------------------------------------------------------------------------- +// rtpMIDI session. Device disconnected +// ----------------------------------------------------------------------------- +void OnAppleMidiDisconnected(long unsigned int ssrc) { + isConnected = false; + Serial.println("Disconnected"); +} + +// ----------------------------------------------------------------------------- +// +// ----------------------------------------------------------------------------- +void OnAppleMidiNoteOn(byte channel, byte note, byte velocity) { + Serial.print("Incoming NoteOn from channel:"); + Serial.print(channel); + Serial.print(" note:"); + Serial.print(note); + Serial.print(" velocity:"); + Serial.print(velocity); + Serial.println(); +} + +// ----------------------------------------------------------------------------- +// +// ----------------------------------------------------------------------------- +void OnAppleMidiNoteOff(byte channel, byte note, byte velocity) { + Serial.print("Incoming NoteOff from channel:"); + Serial.print(channel); + Serial.print(" note:"); + Serial.print(note); + Serial.print(" velocity:"); + Serial.print(velocity); + Serial.println(); +} diff --git a/examples/NoteOnOffEverySec/NoteOnOffEverySec.ino b/examples/EthernetShield_NoteOnOffEverySec/EthernetShield_NoteOnOffEverySec.ino similarity index 94% rename from examples/NoteOnOffEverySec/NoteOnOffEverySec.ino rename to examples/EthernetShield_NoteOnOffEverySec/EthernetShield_NoteOnOffEverySec.ino index f60ff4a..34a6a32 100644 --- a/examples/NoteOnOffEverySec/NoteOnOffEverySec.ino +++ b/examples/EthernetShield_NoteOnOffEverySec/EthernetShield_NoteOnOffEverySec.ino @@ -89,18 +89,18 @@ void loop() // ----------------------------------------------------------------------------- // rtpMIDI session. Device connected // ----------------------------------------------------------------------------- -void OnAppleMidiConnected(char* name) { +void OnAppleMidiConnected(long unsigned int ssrc, char* name) { isConnected = true; - // Serial.print("Connected to session "); - // Serial.println(name); + Serial.print("Connected to session "); + Serial.println(name); } // ----------------------------------------------------------------------------- // rtpMIDI session. Device disconnected // ----------------------------------------------------------------------------- -void OnAppleMidiDisconnected() { +void OnAppleMidiDisconnected(long unsigned int ssrc) { isConnected = false; - // Serial.println("Disconnected"); + Serial.println("Disconnected"); } // ----------------------------------------------------------------------------- diff --git a/examples/NoteOnOffEverySecWifi/NoteOnOffEverySecWifi.ino b/examples/Wifi_NoteOnOffEverySec/Wifi_NoteOnOffEverySec.ino similarity index 94% rename from examples/NoteOnOffEverySecWifi/NoteOnOffEverySecWifi.ino rename to examples/Wifi_NoteOnOffEverySec/Wifi_NoteOnOffEverySec.ino index 4bc2777..2310cb5 100644 --- a/examples/NoteOnOffEverySecWifi/NoteOnOffEverySecWifi.ino +++ b/examples/Wifi_NoteOnOffEverySec/Wifi_NoteOnOffEverySec.ino @@ -1,4 +1,4 @@ -// Hardware: Mega 2560 R2 + Ethernet Shield +// Hardware: Mega 2560 R2 + Wifi Shield // These need to be included when using standard Ethernet #include @@ -107,18 +107,18 @@ void loop() // ----------------------------------------------------------------------------- // rtpMIDI session. Device connected // ----------------------------------------------------------------------------- -void OnAppleMidiConnected(char* name) { +void OnAppleMidiConnected(long unsigned int ssrc, char* name) { isConnected = true; - // Serial.print("Connected to session "); - // Serial.println(name); + Serial.print("Connected to session "); + Serial.println(name); } // ----------------------------------------------------------------------------- // rtpMIDI session. Device disconnected // ----------------------------------------------------------------------------- -void OnAppleMidiDisconnected() { +void OnAppleMidiDisconnected(long unsigned int ssrc) { isConnected = false; - // Serial.println("Disconnected"); + Serial.println("Disconnected"); } // ----------------------------------------------------------------------------- diff --git a/keywords.txt b/keywords.txt index 31619a5..d8dc7df 100644 --- a/keywords.txt +++ b/keywords.txt @@ -13,6 +13,9 @@ ####################################### begin KEYWORD2 +invite KEYWORD2 +getSessionName KEYWORD2 +getSynchronizationSource KEYWORD2 run KEYWORD2 noteOn KEYWORD2 noteOff KEYWORD2 @@ -32,6 +35,11 @@ Continue KEYWORD2 stop KEYWORD2 activeSensing KEYWORD2 systemReset KEYWORD2 +timeCodeQuarterFrame KEYWORD2 +sysEx KEYWORD2 +afterTouch KEYWORD2 +polyPressure KEYWORD2 +tick KEYWORD2 ####################################### # Instances (KEYWORD3) diff --git a/library.properties b/library.properties index c5faf0d..444dd57 100644 --- a/library.properties +++ b/library.properties @@ -1,4 +1,4 @@ -name=Arduino AppleMIDI +name=AppleMIDI version=1.0.0 author=lathoub maintainer=lathoub diff --git a/src/AppleMidi.h b/src/AppleMidi.h index baa5667..7b5f912 100644 --- a/src/AppleMidi.h +++ b/src/AppleMidi.h @@ -27,6 +27,8 @@ #include "utility/dissector.h" +#define MAX_SESSIONS 4 + BEGIN_APPLEMIDI_NAMESPACE class IRtpMidi @@ -50,7 +52,7 @@ class IRtpMidi class IAppleMidi : public IRtpMidi { public: - virtual void Invite(IPAddress ip, uint16_t port = CONTROL_PORT) = 0; + virtual void invite(IPAddress ip, uint16_t port = CONTROL_PORT) = 0; virtual void OnInvitation(void* sender, AppleMIDI_Invitation&) = 0; virtual void OnEndSession(void* sender, AppleMIDI_EndSession&) = 0; @@ -84,10 +86,8 @@ class AppleMidi_Class : public IAppleMidi RtpMidi _rtpMidi; RtpMidi_Clock _rtpMidiClock; - - SessionInvite_t _sessionInvite; - Accept _sessionAccept; +// Accept _sessionAccept; // SSRC, Synchronization source. // (RFC 1889) The source of a stream of RTP packets, identified by a 32-bit numeric SSRC identifier @@ -103,10 +103,11 @@ class AppleMidi_Class : public IAppleMidi // be identified as a different SSRC. uint32_t _ssrc; - // Allow for multiple sessions???? Session_t Sessions[MAX_SESSIONS]; - char SessionName[50]; + char _sessionName[50]; + + inline uint32_t createInitiatorToken(); public: // Constructor and Destructor @@ -118,12 +119,13 @@ class AppleMidi_Class : public IAppleMidi inline void begin(const char*, uint16_t port = CONTROL_PORT); inline uint32_t getSynchronizationSource() { return _ssrc; } + inline char* getSessionName() { return _sessionName; } inline void run(); // IAppleMidi - inline void Invite(IPAddress ip, uint16_t port = CONTROL_PORT); + inline void invite(IPAddress ip, uint16_t port = CONTROL_PORT); inline void OnInvitation(void* sender, AppleMIDI_Invitation&); inline void OnEndSession(void* sender, AppleMIDI_EndSession&); @@ -154,9 +156,9 @@ class AppleMidi_Class : public IAppleMidi inline void OnTuneRequest(void* sender); private: - inline void write(UdpClass&, AppleMIDI_InvitationRejected&); - inline void write(UdpClass&, AppleMIDI_InvitationAccepted&); - inline void write(UdpClass&, AppleMIDI_Syncronization&); + inline void write(UdpClass&, AppleMIDI_InvitationRejected&, IPAddress ip, uint16_t port); + inline void write(UdpClass&, AppleMIDI_InvitationAccepted&, IPAddress ip, uint16_t port); + inline void write(UdpClass&, AppleMIDI_Syncronization&, IPAddress ip, uint16_t port); inline void write(UdpClass&, AppleMIDI_Invitation&, IPAddress ip, uint16_t port); #if APPLEMIDI_BUILD_OUTPUT @@ -211,13 +213,15 @@ class AppleMidi_Class : public IAppleMidi #endif // APPLEMIDI_BUILD_OUTPUT inline int GetFreeSessionSlot(); - inline int GetSessionSlot(const uint32_t ssrc); - inline void CreateSession(const int slot, const uint32_t ssrc); - inline void CreateLocalSessionStepControl(const int slot, const uint32_t ssrc); - inline void CreateLocalSessionStepContent(const int slot, const uint32_t ssrc); - inline void CreateRemoteSessionStepControl(const int slot, const uint32_t ssrc, IPAddress ip, uint16_t port); - inline void CreateRemoteSessionStepContent(const int slot, const uint32_t ssrc, IPAddress ip, uint16_t port); + inline int GetSessionSlotUsingSSrc(const uint32_t ssrc); + inline int GetSessionSlotUsingInitiatorToken(const uint32_t initiatorToken); + inline void CreateLocalSession(const int slot, const uint32_t ssrc); + inline void CreateRemoteSession(IPAddress ip, uint16_t port); + inline void CompleteLocalSessionControl(AppleMIDI_InvitationAccepted& invitationAccepted); + inline void CompleteLocalSessionContent(AppleMIDI_InvitationAccepted& invitationAccepted); inline void DeleteSession(const uint32_t ssrc); + inline void DeleteSession(int slot); + inline void DeleteSessions(); inline void DumpSession(); @@ -236,8 +240,8 @@ class AppleMidi_Class : public IAppleMidi #if APPLEMIDI_USE_CALLBACKS public: - inline void OnConnected(void (*fptr)(char*)); - inline void OnDisconnected(void (*fptr)()); + inline void OnConnected(void(*fptr)(uint32_t, char*)); + inline void OnDisconnected(void(*fptr)(uint32_t)); inline void OnReceiveNoteOn(void (*fptr)(byte channel, byte note, byte velocity)); inline void OnReceiveNoteOff(void (*fptr)(byte channel, byte note, byte velocity)); @@ -262,8 +266,8 @@ class AppleMidi_Class : public IAppleMidi inline void launchCallback(); - void(*mConnectedCallback)(char*); - void(*mDisconnectedCallback)(); + void(*mConnectedCallback)(uint32_t, char*); + void(*mDisconnectedCallback)(uint32_t); void (*mNoteOffCallback)(byte channel, byte note, byte velocity); void (*mNoteOnCallback)(byte channel, byte note, byte velocity); diff --git a/src/AppleMidi.hpp b/src/AppleMidi.hpp index 7970d63..dfbe35c 100644 --- a/src/AppleMidi.hpp +++ b/src/AppleMidi.hpp @@ -73,7 +73,7 @@ template inline void AppleMidi_Class::begin(const char* sessionName, uint16_t port) { // - strcpy(SessionName, sessionName); + strcpy(_sessionName, sessionName); Port = port; @@ -87,19 +87,7 @@ inline void AppleMidi_Class::begin(const char* sessionName, uint16_t p _ssrc = *((uint32_t*) &buffer[0]); // Initialize Sessions - for (int i = 0; i < MAX_SESSIONS; i++) - { - Sessions[i].ssrc = 0; - //for (int j = 0; j < MAX_PARTICIPANTS_PER_SESSION; j++) - //{ - // Sessions[i].participants[j].initiatorToken = 0; - //} - } - - _sessionInvite.remotePort = 0; - _sessionInvite.lastSend = 0; - _sessionInvite.status = None; - _sessionInvite.attempts = 0; + DeleteSessions(); // open UDP socket for control messages _controlUDP.begin(Port); @@ -180,25 +168,19 @@ inline void AppleMidi_Class::run() } -/*! \brief +/*! \brief The Arduino initiates the session. */ template -inline void AppleMidi_Class::Invite(IPAddress ip, uint16_t port) +inline void AppleMidi_Class::invite(IPAddress ip, uint16_t port) { - // Ignore if an invite is already pending. - - _sessionInvite.remoteHost = ip; - _sessionInvite.remotePort = port; - _sessionInvite.lastSend = 0; - _sessionInvite.attempts = 0; - _sessionInvite.status = WaitingForControlInvitationAccepted; + CreateRemoteSession(ip, port); #if (APPLEMIDI_DEBUG) - Serial.println("Posted invitation"); + Serial.println("Queued invite"); #endif } -/*! \brief . +/*! \brief The Arduino is being invited to a session. */ template inline void AppleMidi_Class::OnInvitation(void* sender, AppleMIDI_Invitation& invitation) @@ -211,8 +193,7 @@ inline void AppleMidi_Class::OnInvitation(void* sender, AppleMIDI_Invi OnContentInvitation(sender, invitation); } - -/*! \brief . +/*! \brief The session has been ended by the remote source. */ template inline void AppleMidi_Class::OnEndSession(void* sender, AppleMIDI_EndSession& sessionEnd) @@ -232,7 +213,7 @@ inline void AppleMidi_Class::OnEndSession(void* sender, AppleMIDI_EndS DeleteSession(sessionEnd.ssrc); if (mDisconnectedCallback != 0) - mDisconnectedCallback(); + mDisconnectedCallback(sessionEnd.ssrc); } /* \brief With the receiver feedback packet, the recipient can tell the sender up to what sequence @@ -250,7 +231,7 @@ inline void AppleMidi_Class::OnReceiverFeedback(void* sender, AppleMID } -/*! \brief . +/*! \brief The invitation that we have send, has been accepted. */ template void AppleMidi_Class::OnInvitationAccepted(void* sender, AppleMIDI_InvitationAccepted& invitationAccepted) @@ -279,31 +260,7 @@ void AppleMidi_Class::OnControlInvitationAccepted(void* sender, AppleM Serial.println(); #endif - if (_sessionInvite.status != WaitingForControlInvitationAccepted) - { -#if (APPLEMIDI_DEBUG) - Serial.println("unexpected _sessionInvite.status"); -#endif - return; - } - - // Find a free slot for the session - int index = GetFreeSessionSlot(); - if (index < 0) - { -#if (APPLEMIDI_DEBUG) - Serial.println("No free slot found"); -#endif - return; - } - - CreateLocalSessionStepControl(index, invitationAccepted.ssrc); - - // Initiate next step in the invitation process - _sessionInvite.lastSend = 0; - _sessionInvite.attempts = 0; - _sessionInvite.ssrc = invitationAccepted.ssrc; - _sessionInvite.status = WaitingForContentInvitationAccepted; + CompleteLocalSessionControl(invitationAccepted); } /*! \brief . @@ -324,25 +281,10 @@ void AppleMidi_Class::OnContentInvitationAccepted(void* sender, AppleM Serial.println(); #endif - // OK - invitations worked out well - _sessionInvite.lastSend = 0; - _sessionInvite.ssrc = 0; - _sessionInvite.status = None; - - // Find a free slot for the session - int index = GetSessionSlot(invitationAccepted.ssrc); - if (index < 0) - { -#if (APPLEMIDI_DEBUG) - Serial.println("Session should exist!, but it doesnt"); -#endif - return; - } - - CreateLocalSessionStepContent(index, invitationAccepted.ssrc); + CompleteLocalSessionContent(invitationAccepted); } -/*! \brief . +/*! \brief Part 1 of being invited into a session, the Control Invitation. */ template void AppleMidi_Class::OnControlInvitation(void* sender, AppleMIDI_Invitation& invitation) @@ -362,7 +304,7 @@ void AppleMidi_Class::OnControlInvitation(void* sender, AppleMIDI_Invi // Do we know this ssrc already? // In case initiator reconnects (after a crash of some sort) - int index = GetSessionSlot(invitation.ssrc); + int index = GetSessionSlotUsingSSrc(invitation.ssrc); if (index < 0) { // No, not existing; must be a new initiator @@ -372,21 +314,21 @@ void AppleMidi_Class::OnControlInvitation(void* sender, AppleMIDI_Invi { // no free slots, we cant accept invite AppleMIDI_InvitationRejected invitationRejected(invitation.ssrc, invitation.initiatorToken, invitation.sessionName); - write(_controlUDP, invitationRejected); + write(_controlUDP, invitationRejected, _controlUDP.remoteIP(), _controlUDP.remotePort()); return; } } - // Initiate a session or a new participant in the session? - CreateRemoteSessionStepControl(index, invitation.ssrc, _controlUDP.remoteIP(), _controlUDP.remotePort()); + // Initiate a session got this ssrc + CreateLocalSession(index, invitation.ssrc); - AppleMIDI_InvitationAccepted acceptInvitation(_ssrc, invitation.initiatorToken, SessionName); - write(_controlUDP, acceptInvitation); + AppleMIDI_InvitationAccepted acceptInvitation(_ssrc, invitation.initiatorToken, getSessionName()); + write(_controlUDP, acceptInvitation, _controlUDP.remoteIP(), _controlUDP.remotePort()); #if (APPLEMIDI_DEBUG) Serial.print("< Control InvitationAccepted: peer = \""); - Serial.print(SessionName); + Serial.print(getSessionName()); Serial.print("\""); Serial.print(" ,ssrc 0x"); Serial.print(_ssrc, HEX); @@ -421,8 +363,8 @@ void AppleMidi_Class::OnContentInvitation(void* sender, AppleMIDI_Invi #endif // Find the slot, it should be there because created by control session - int index = GetSessionSlot(invitation.ssrc); - if (index < 0) + int i = GetSessionSlotUsingSSrc(invitation.ssrc); + if (i < 0) { #if (APPLEMIDI_DEBUG) Serial.print("Error - control session does not exists for "); @@ -430,17 +372,17 @@ void AppleMidi_Class::OnContentInvitation(void* sender, AppleMIDI_Invi Serial.print(". Rejecting invitation."); #endif AppleMIDI_InvitationRejected invitationRejected(invitation.ssrc, invitation.initiatorToken, invitation.sessionName); - write(_contentUDP, invitationRejected); + write(_contentUDP, invitationRejected, _contentUDP.remoteIP(), _contentUDP.remotePort()); return; } - AppleMIDI_InvitationAccepted acceptInvitation(_ssrc, invitation.initiatorToken, SessionName); - write(_contentUDP, acceptInvitation); + AppleMIDI_InvitationAccepted acceptInvitation(_ssrc, invitation.initiatorToken, getSessionName()); + write(_contentUDP, acceptInvitation, _contentUDP.remoteIP(), _contentUDP.remotePort()); #if (APPLEMIDI_DEBUG) Serial.print("< Content InvitationAccepted: peer = \""); - Serial.print(SessionName); + Serial.print(getSessionName()); Serial.print("\""); Serial.print(" ,ssrc 0x"); Serial.print(_ssrc, HEX); @@ -453,10 +395,14 @@ void AppleMidi_Class::OnContentInvitation(void* sender, AppleMIDI_Invi Serial.println(); #endif - CreateRemoteSessionStepContent(index, invitation.ssrc, _contentUDP.remoteIP(), _contentUDP.remotePort()); + Sessions[i].contentIP = _contentUDP.remoteIP(); + Sessions[i].contentPort = _contentUDP.remotePort(); + Sessions[i].invite.status = None; + Sessions[i].syncronization.enabled = true; // synchronisation can start + if (mConnectedCallback != 0) - mConnectedCallback(invitation.sessionName); + mConnectedCallback(invitation.ssrc, invitation.sessionName); } /*! \brief . @@ -509,7 +455,7 @@ void AppleMidi_Class::OnSyncronization(void* sender, AppleMIDI_Syncron // If we know this session already, ignore it. - int index = GetSessionSlot(synchronization.ssrc); + int index = GetSessionSlotUsingSSrc(synchronization.ssrc); if (index < 0) { #if (APPLEMIDI_DEBUG) @@ -556,7 +502,7 @@ void AppleMidi_Class::OnSyncronization(void* sender, AppleMIDI_Syncron } AppleMIDI_Syncronization synchronizationResponse(_ssrc, synchronization.count, synchronization.timestamps); - write(_contentUDP, synchronizationResponse); + write(_contentUDP, synchronizationResponse, _contentUDP.remoteIP(), _contentUDP.remotePort()); #if (APPLEMIDI_DEBUG) Serial.print("< Syncronization for ssrc 0x"); @@ -979,8 +925,8 @@ void AppleMidi_Class::OnTuneRequest(void* sender) //------------------------------------------------------------------------------ -/*! \brief . -*/ +/*! \brief Find a free session slot. + */ template int AppleMidi_Class::GetFreeSessionSlot() { @@ -990,10 +936,10 @@ int AppleMidi_Class::GetFreeSessionSlot() return -1; } -/*! \brief . +/*! \brief Find the slot of a session, based on the ssrc. */ template -int AppleMidi_Class::GetSessionSlot(const uint32_t ssrc) +int AppleMidi_Class::GetSessionSlotUsingSSrc(const uint32_t ssrc) { for (int i = 0; i < MAX_SESSIONS; i++) if (ssrc == Sessions[i].ssrc) @@ -1001,64 +947,136 @@ int AppleMidi_Class::GetSessionSlot(const uint32_t ssrc) return -1; } -/*! \brief . +/*! \brief Find the slot of a session, based on the ssrc. */ template -void AppleMidi_Class::CreateLocalSessionStepControl(const int index, const uint32_t ssrc) +int AppleMidi_Class::GetSessionSlotUsingInitiatorToken(const uint32_t initiatorToken) { - CreateSession(index, ssrc); - Sessions[index].seqNum = -1; - Sessions[index].initiator = Undefined; - //Sessions[index].contentIP = ip; - //Sessions[index].controlPort = port; + for (int i = 0; i < MAX_SESSIONS; i++) + if (initiatorToken == Sessions[i].invite.initiatorToken) + return i; + return -1; } /*! \brief . */ template -void AppleMidi_Class::CreateLocalSessionStepContent(const int index, const uint32_t ssrc) +void AppleMidi_Class::CompleteLocalSessionControl(AppleMIDI_InvitationAccepted& invitationAccepted) { - CreateSession(index, ssrc); - Sessions[index].initiator = Local; - //Sessions[index].contentIP = ip; - //Sessions[index].contentPort = port; + // Find slot, based on initiator token + int i = GetSessionSlotUsingInitiatorToken(invitationAccepted.initiatorToken); + if (i < 0) + { +#if (APPLEMIDI_DEBUG) + Serial.println("hmm, initiatorToken not found"); +#endif + return; + } + + // + if (Sessions[i].invite.status != WaitingForControlInvitationAccepted) + { +#if (APPLEMIDI_DEBUG) // issue warning + Serial.println("status not what expected"); +#endif + } + + // Initiate next step in the invitation process + Sessions[i].invite.lastSend = 0; + Sessions[i].invite.attempts = 0; + Sessions[i].invite.ssrc = invitationAccepted.ssrc; + Sessions[i].invite.status = SendContentInvite; } /*! \brief . */ template -void AppleMidi_Class::CreateRemoteSessionStepControl(const int index, const uint32_t ssrc, IPAddress ip, uint16_t port) +void AppleMidi_Class::CompleteLocalSessionContent(AppleMIDI_InvitationAccepted& invitationAccepted) { - CreateSession(index, ssrc); - Sessions[index].seqNum = 0; - Sessions[index].initiator = Undefined; - //Sessions[index].contentIP = ip; - //Sessions[index].controlPort = port; + // Find slot, based on initiator token + int i = GetSessionSlotUsingInitiatorToken(invitationAccepted.initiatorToken); + if (i < 0) + { +#if (APPLEMIDI_DEBUG) + Serial.println("hmm, initiatorToken not found"); +#endif + return; + } + + // + if (Sessions[i].invite.status != WaitingForContentInvitationAccepted) + { +#if (APPLEMIDI_DEBUG) // issue warning + Serial.println("status not what expected"); +#endif + } + + // Finalize invitation process + Sessions[i].ssrc = invitationAccepted.ssrc; +// strcpy(Sessions[i].name, invitationAccepted.name); + Sessions[i].invite.status = None; + Sessions[i].syncronization.enabled = true; // synchronisation can start + + if (mConnectedCallback != 0) + mConnectedCallback(Sessions[i].ssrc, invitationAccepted.name); } -/*! \brief . +/*! \brief Initialize session at slot 'index'. */ template -void AppleMidi_Class::CreateRemoteSessionStepContent(const int index, const uint32_t ssrc, IPAddress ip, uint16_t port) +void AppleMidi_Class::CreateLocalSession(const int i, const uint32_t ssrc) { - CreateSession(index, ssrc); - Sessions[index].seqNum = 1; - Sessions[index].initiator = Remote; - Sessions[index].contentIP = ip; - Sessions[index].contentPort = port; +#if (APPLEMIDI_DEBUG) + Serial.print ("New Local Session in slot "); + Serial.print (i); + Serial.print (" with SSRC "); + Serial.println(ssrc, HEX); +#endif + + Sessions[i].ssrc = ssrc; + Sessions[i].seqNum = 1; + Sessions[i].initiator = Remote; + Sessions[i].syncronization.lastTime = 0; + Sessions[i].syncronization.count = 0; + Sessions[i].syncronization.busy = false; + Sessions[i].syncronization.enabled = false; + Sessions[i].invite.status = ReceiveControlInvitation; } -/*! \brief . +/*! \brief Initialize session at slot 'index'. */ template -void AppleMidi_Class::CreateSession(const int index, const uint32_t ssrc) +void AppleMidi_Class::CreateRemoteSession(IPAddress ip, uint16_t port) { - Sessions[index].ssrc = ssrc; - Sessions[index].seqNum = 1; - Sessions[index].initiator = Undefined; - Sessions[index].syncronization.lastTime = 0; - Sessions[index].syncronization.count = 0; - Sessions[index].syncronization.busy = false; + int i = GetFreeSessionSlot(); + if (i < 0) + { +#if (APPLEMIDI_DEBUG) + Serial.println("Invite: No free slot availble, invitation cancelled."); +#endif + return; + } + +#if (APPLEMIDI_DEBUG) + Serial.print("New Remote Session in slot "); + Serial.println(i); +#endif + + Sessions[i].ssrc = -1; + Sessions[i].seqNum = 0; + Sessions[i].initiator = Local; + Sessions[i].contentIP = ip; + Sessions[i].contentPort = port + 1; + Sessions[i].syncronization.lastTime = 0; + Sessions[i].syncronization.count = 0; + Sessions[i].syncronization.busy = false; + Sessions[i].syncronization.enabled = false; + + Sessions[i].invite.remoteHost = ip; + Sessions[i].invite.remotePort = port; + Sessions[i].invite.lastSend = 0; + Sessions[i].invite.attempts = 0; + Sessions[i].invite.status = SendControlInvite; } /*! \brief . @@ -1066,13 +1084,47 @@ void AppleMidi_Class::CreateSession(const int index, const uint32_t ss template void AppleMidi_Class::DeleteSession(const uint32_t ssrc) { - int index = GetSessionSlot(ssrc); - if (index < 0) + // Find the slot first + int slot = GetSessionSlotUsingSSrc(ssrc); + if (slot < 0) return; - Sessions[index].ssrc = 0; - Sessions[index].seqNum = 0; - Sessions[index].initiator = Undefined; + DeleteSession(slot); +} + +/*! \brief . +*/ +template +void AppleMidi_Class::DeleteSession(int slot) +{ + // Then zero-ize it + Sessions[slot].ssrc = 0; + Sessions[slot].seqNum = 0; + Sessions[slot].initiator = Undefined; + Sessions[slot].invite.status = None; + Sessions[slot].syncronization.enabled = false; + +#if (APPLEMIDI_DEBUG) + Serial.print("Freeing Session slot "); + Serial.println(slot); +#endif +} + +/*! \brief . +*/ +template +void AppleMidi_Class::DeleteSessions() +{ + for (int slot = 0; slot < MAX_SESSIONS; slot++) + { + Sessions[slot].ssrc = 0; + Sessions[slot].initiator = Undefined; + Sessions[slot].invite.status = None; + Sessions[slot].invite.status = None; + Sessions[slot].invite.attempts = 0; + Sessions[slot].invite.lastSend = 0; + Sessions[slot].syncronization.enabled = false; + } } /*! \brief . @@ -1091,38 +1143,42 @@ void AppleMidi_Class::DumpSession() #endif } + +/*! \brief . +*/ +template +inline uint32_t AppleMidi_Class::createInitiatorToken() +{ + static int counter = 0; + return 0x12345000 + ++counter; +} + /*! \brief . */ template inline void AppleMidi_Class::ManageInvites() { - if (_sessionInvite.status == None) - { - // Do nothing - } - else if (_sessionInvite.status == WaitingForControlInvitationAccepted) + for (int i = 0; i < MAX_SESSIONS; i++) { - if (_sessionInvite.lastSend + 1000 < millis()) - { - if (_sessionInvite.attempts >= 10) // Max attempts - { - // Give up - _sessionInvite.lastSend = 0; - _sessionInvite.status = None; - - DeleteSession(_sessionInvite.ssrc); - - return; - } + Session_t* session = &Sessions[i]; + if (session->invite.status == None) + { + // No invitation pending + } + else if (session->invite.status == SendControlInvite) + { + // Send invitation AppleMIDI_Invitation invitation; - invitation.initiatorToken = 0x12345678; + invitation.initiatorToken = createInitiatorToken(); invitation.ssrc = _ssrc; - strcpy(invitation.sessionName, SessionName); - write(_controlUDP, invitation, _sessionInvite.remoteHost, _sessionInvite.remotePort); + strcpy(invitation.sessionName, getSessionName()); + write(_controlUDP, invitation, session->invite.remoteHost, session->invite.remotePort); - _sessionInvite.lastSend = millis(); - _sessionInvite.attempts++; + session->invite.initiatorToken = invitation.initiatorToken; + session->invite.lastSend = millis(); + session->invite.attempts = 1; + session->invite.status = WaitingForControlInvitationAccepted; #if (APPLEMIDI_DEBUG) Serial.print("< Control Invitation: peer = \""); @@ -1131,34 +1187,60 @@ inline void AppleMidi_Class::ManageInvites() Serial.print(" ,ssrc 0x"); Serial.print(invitation.ssrc, HEX); Serial.print(" ,Attempt = "); - Serial.print(_sessionInvite.attempts); + Serial.print(session->invite.attempts); Serial.print(" ,initiatorToken = 0x"); Serial.print(invitation.initiatorToken, HEX); Serial.println(); #endif - } - } - else if (_sessionInvite.status == WaitingForContentInvitationAccepted) - { - if (_sessionInvite.lastSend + 1000 < millis()) + else if (session->invite.status == WaitingForControlInvitationAccepted) { - if (_sessionInvite.attempts >= 10) // Max attempts + if (session->invite.lastSend + 1000 < millis()) { - // Give up - _sessionInvite.lastSend = 0; - _sessionInvite.status = None; + // If no response received after 1 second, send invitation again + // with a maximum of 10 times. + + if (session->invite.attempts >= 10) // Max attempts + { + DeleteSession(i); // give up + return; + } - DeleteSession(_sessionInvite.ssrc); + // Send invitation + AppleMIDI_Invitation invitation; + invitation.initiatorToken = session->invite.initiatorToken; + invitation.ssrc = _ssrc; + strcpy(invitation.sessionName, getSessionName()); + write(_controlUDP, invitation, session->invite.remoteHost, session->invite.remotePort); - return; - } + session->invite.lastSend = millis(); + session->invite.attempts++; +#if (APPLEMIDI_DEBUG) + Serial.print("< Control Invitation: peer = \""); + Serial.print(invitation.sessionName); + Serial.print("\""); + Serial.print(" ,ssrc 0x"); + Serial.print(invitation.ssrc, HEX); + Serial.print(" ,Attempt = "); + Serial.print(session->invite.attempts); + Serial.print(" ,initiatorToken = 0x"); + Serial.print(invitation.initiatorToken, HEX); + Serial.println(); +#endif + } + } + else if (session->invite.status == SendContentInvite) + { AppleMIDI_Invitation invitation; - invitation.initiatorToken = 0x12345678; + invitation.initiatorToken = session->invite.initiatorToken; invitation.ssrc = _ssrc; - strcpy(invitation.sessionName, SessionName); - write(_contentUDP, invitation, _sessionInvite.remoteHost, _sessionInvite.remotePort + 1); + strcpy(invitation.sessionName, getSessionName()); + write(_contentUDP, invitation, session->invite.remoteHost, session->invite.remotePort + 1); + + session->invite.lastSend = millis(); + session->invite.attempts = 1; + session->invite.status = WaitingForContentInvitationAccepted; #if (APPLEMIDI_DEBUG) Serial.print("< Content Invitation: peer = \""); @@ -1166,26 +1248,60 @@ inline void AppleMidi_Class::ManageInvites() Serial.print("\""); Serial.print(" ,ssrc 0x"); Serial.print(invitation.ssrc, HEX); -#if (APPLEMIDI_DEBUG_VERBOSE) + Serial.print(" ,Attempt = "); + Serial.print(session->invite.attempts); Serial.print(" ,initiatorToken = 0x"); Serial.print(invitation.initiatorToken, HEX); -#endif Serial.println(); #endif + } + else if (session->invite.status == WaitingForContentInvitationAccepted) + { + if (session->invite.lastSend + 1000 < millis()) + { + // If no response received after 1 second, send invitation again + // with a maximum of 10 times. + + if (session->invite.attempts >= 10) // Max attempts + { + DeleteSession(session->invite.ssrc); // Give up + return; + } + + AppleMIDI_Invitation invitation; + invitation.initiatorToken = session->invite.initiatorToken; + invitation.ssrc = _ssrc; + strcpy(invitation.sessionName, getSessionName()); + write(_contentUDP, invitation, session->invite.remoteHost, session->invite.remotePort + 1); - _sessionInvite.lastSend = millis(); + session->invite.lastSend = millis(); + session->invite.attempts++; + +#if (APPLEMIDI_DEBUG) + Serial.print("< Content Invitation: peer = \""); + Serial.print(invitation.sessionName); + Serial.print("\""); + Serial.print(" ,ssrc 0x"); + Serial.print(invitation.ssrc, HEX); + Serial.print(" ,Attempt = "); + Serial.print(session->invite.attempts); + Serial.print(" ,initiatorToken = 0x"); + Serial.print(invitation.initiatorToken, HEX); + Serial.println(); +#endif + } } } } -/*! \brief . +/*! \brief The initiator of the session polls if remote station is still alive. */ template inline void AppleMidi_Class::ManageTiming() { for (int i = 0; i < MAX_SESSIONS; i++) { - if (Sessions[i].initiator == Local) + if (Sessions[i].initiator == Local && Sessions[i].syncronization.enabled) { if (!Sessions[i].syncronization.busy) { @@ -1222,7 +1338,7 @@ inline void AppleMidi_Class::ManageTiming() synchronization.count = 0; AppleMIDI_Syncronization synchronizationResponse(_ssrc, synchronization.count, synchronization.timestamps); - write(_contentUDP, synchronizationResponse); + write(_contentUDP, synchronizationResponse, Sessions[i].contentIP, Sessions[i].contentPort); Sessions[i].syncronization.busy = true; @@ -1231,6 +1347,10 @@ inline void AppleMidi_Class::ManageTiming() Serial.print(synchronizationResponse.ssrc, HEX); Serial.print(", count = "); Serial.print(synchronizationResponse.count); + Serial.print(", to = "); + Serial.print(Sessions[i].contentIP); + Serial.print(" "); + Serial.print(Sessions[i].contentPort); #if (APPLEMIDI_DEBUG_VERBOSE) //Serial.print (" Timestamps = "); //Serial.print (synchronizationResponse.timestamps[0], HEX); @@ -1263,10 +1383,14 @@ inline void AppleMidi_Class::ManageTiming() //} } +// ----------------------------------------------------------------------------- +// +// ----------------------------------------------------------------------------- + template -inline void AppleMidi_Class::write(UdpClass& udp, AppleMIDI_InvitationRejected& ir) +inline void AppleMidi_Class::write(UdpClass& udp, AppleMIDI_InvitationRejected& ir, IPAddress ip, uint16_t port) { - udp.beginPacket(udp.remoteIP(), udp.remotePort()); + udp.beginPacket(ip, port); udp.write(ir.signature, sizeof(ir.signature)); udp.write(ir.command, sizeof(ir.command)); @@ -1288,9 +1412,9 @@ inline void AppleMidi_Class::write(UdpClass& udp, AppleMIDI_Invitation } template -inline void AppleMidi_Class::write(UdpClass& udp, AppleMIDI_InvitationAccepted& ia) +inline void AppleMidi_Class::write(UdpClass& udp, AppleMIDI_InvitationAccepted& ia, IPAddress ip, uint16_t port) { - udp.beginPacket(udp.remoteIP(), udp.remotePort()); + udp.beginPacket(ip, port); udp.write(ia.signature, sizeof(ia.signature)); udp.write(ia.command, sizeof(ia.command)); @@ -1312,9 +1436,9 @@ inline void AppleMidi_Class::write(UdpClass& udp, AppleMIDI_Invitation } template -inline void AppleMidi_Class::write(UdpClass& udp, AppleMIDI_Syncronization& sy) +inline void AppleMidi_Class::write(UdpClass& udp, AppleMIDI_Syncronization& sy, IPAddress ip, uint16_t port) { - udp.beginPacket(udp.remoteIP(), udp.remotePort()); + udp.beginPacket(ip, port); udp.write(sy.signature, sizeof(sy.signature)); udp.write(sy.command, sizeof(sy.command)); @@ -1819,9 +1943,12 @@ with previous versions of the library. template inline void AppleMidi_Class::sysEx(unsigned int inLength, const byte* inArray, bool inArrayContainsBoundaries) { + // USE SEND!!!!! + + _rtpMidi.sequenceNr++; // _rtpMidi.timestamp = - _rtpMidi.beginWrite(_contentUDP); +// _rtpMidi.beginWrite(_contentUDP, session.contentIP, session.contentPort); uint8_t length = inLength + 1 + ((inArrayContainsBoundaries) ? 0 : 2); _contentUDP.write(&length, 1); @@ -1994,8 +2121,8 @@ inline void AppleMidi_Class::tick() #if APPLEMIDI_USE_CALLBACKS -template inline void AppleMidi_Class::OnConnected(void(*fptr)(char*)) { mConnectedCallback = fptr; } -template inline void AppleMidi_Class::OnDisconnected(void(*fptr)()) { mDisconnectedCallback = fptr; } +template inline void AppleMidi_Class::OnConnected(void(*fptr)(uint32_t, char*)) { mConnectedCallback = fptr; } +template inline void AppleMidi_Class::OnDisconnected(void(*fptr)(uint32_t)) { mDisconnectedCallback = fptr; } template inline void AppleMidi_Class::OnReceiveNoteOff(void(*fptr)(byte channel, byte note, byte velocity)) { mNoteOffCallback = fptr; } template inline void AppleMidi_Class::OnReceiveNoteOn(void(*fptr)(byte channel, byte note, byte velocity)) { mNoteOnCallback = fptr; } diff --git a/src/utility/AppleMidi_Defs.h b/src/utility/AppleMidi_Defs.h index 2af0e59..f540626 100644 --- a/src/utility/AppleMidi_Defs.h +++ b/src/utility/AppleMidi_Defs.h @@ -298,10 +298,13 @@ enum SessionInitiator Local, }; -enum SessioInviteStatus +enum SessionInviteStatus { None, + SendControlInvite, + ReceiveControlInvitation, WaitingForControlInvitationAccepted, + SendContentInvite, WaitingForContentInvitationAccepted, }; @@ -311,15 +314,17 @@ enum SessioInviteStatus typedef struct { - SessioInviteStatus status; + SessionInviteStatus status; unsigned long lastSend; IPAddress remoteHost; uint16_t remotePort; int attempts; - uint32_t ssrc; + uint32_t ssrc; + uint32_t initiatorToken; } SessionInvite_t; typedef struct { + bool enabled; unsigned long lastTime; uint32_t count; bool busy; @@ -327,6 +332,7 @@ typedef struct { typedef struct { uint32_t ssrc; // the unique identifier +// char[50] name; unsigned short seqNum; SessionInitiator initiator; SessionSyncronization_t syncronization; @@ -334,6 +340,7 @@ typedef struct { //uint16_t controlPort; IPAddress contentIP; uint16_t contentPort; + SessionInvite_t invite; } Session_t; typedef uint32_t MIDISamplingRate; diff --git a/src/utility/AppleMidi_InvitationAccepted.h b/src/utility/AppleMidi_InvitationAccepted.h index cf91e82..f21d1f0 100644 --- a/src/utility/AppleMidi_InvitationAccepted.h +++ b/src/utility/AppleMidi_InvitationAccepted.h @@ -39,7 +39,7 @@ class AppleMIDI_InvitationAccepted { this->initiatorToken = initiatorToken; this->ssrc = ssrc; - strcpy(this->name, (const char*)name); + strcpy(this->name, static_cast(name)); } private: diff --git a/src/utility/AppleMidi_InvitationRejected.h b/src/utility/AppleMidi_InvitationRejected.h index c6f667a..4d8e9e3 100644 --- a/src/utility/AppleMidi_InvitationRejected.h +++ b/src/utility/AppleMidi_InvitationRejected.h @@ -39,7 +39,7 @@ class AppleMIDI_InvitationRejected { this->initiatorToken = initiatorToken; this->ssrc = ssrc; - strcpy(this->sessionName, (const char*)sessionName); + strcpy(this->sessionName, static_cast(sessionName)); } private: diff --git a/src/utility/AppleMidi_Settings.h b/src/utility/AppleMidi_Settings.h index 041171a..f4f1385 100644 --- a/src/utility/AppleMidi_Settings.h +++ b/src/utility/AppleMidi_Settings.h @@ -25,6 +25,7 @@ // (MIDI in, out, thru), or to 0 to disable the feature and save space. // Note that thru can work only if input and output are enabled. +//#define DEBUG #define RELEASE #if defined(RELEASE) @@ -45,7 +46,7 @@ #if defined(DEBUG_BUILD) #define APPLEMIDI_DEBUG 1 -#undef APPLEMIDI_DEBUG_VERBOSE +#undef APPLEMIDI_DEBUG_VERBOSE #endif #define APPLEMIDI_BUILD_INPUT 1 @@ -76,9 +77,6 @@ #define MIDI_SYSEX_ARRAY_SIZE 255 // Maximum size is 65535 bytes. -#define MAX_SESSIONS 10 -#define MAX_PARTICIPANTS_PER_SESSION 10 - // ----------------------------------------------------------------------------- BEGIN_APPLEMIDI_NAMESPACE diff --git a/src/utility/RtpMidi_Clock.h b/src/utility/RtpMidi_Clock.h index 61ca3e4..dec94e8 100644 --- a/src/utility/RtpMidi_Clock.h +++ b/src/utility/RtpMidi_Clock.h @@ -73,16 +73,16 @@ typedef struct RtpMidi_Clock { unsigned long ticks = millis() - startTime_; unsigned long seconds = ticks / TicksPerSecond(); - uint32_t lapse = (uint32_t)((double) seconds * clockRate_); + uint32_t lapse = (uint32_t)(static_cast(seconds) * clockRate_); return lapse; } - unsigned long Ticks() + unsigned long Ticks() const { return millis(); } - unsigned long TicksPerSecond() + unsigned long TicksPerSecond() const { return 1000; } diff --git a/src/utility/packet-apple-midi.h b/src/utility/packet-apple-midi.h index fc2fe48..3f88a85 100644 --- a/src/utility/packet-apple-midi.h +++ b/src/utility/packet-apple-midi.h @@ -58,7 +58,7 @@ Serial.println (packetSize); return NOT_ENOUGH_DATA; } - if (0 == memcmp((void*)(packetBuffer + offset), amSignature, sizeof(amSignature))) + if (0 == memcmp(static_cast(packetBuffer + offset), amSignature, sizeof(amSignature))) { offset += sizeof(amSignature); } @@ -106,7 +106,7 @@ Serial.println("Not enough data for Invitation"); return NOT_ENOUGH_DATA; } - else if (0 == memcmp((void*)(packetBuffer + offset), amInvitationAccepted, sizeof(amInvitationAccepted))) + else if (0 == memcmp(static_cast(packetBuffer + offset), amInvitationAccepted, sizeof(amInvitationAccepted))) { offset += sizeof(amInvitationAccepted); @@ -138,11 +138,11 @@ Serial.println("Not enough data for AcceptInvitation"); return NOT_ENOUGH_DATA; } - else if (0 == memcmp((void*)(packetBuffer + offset), amInvitationRejected, sizeof(amInvitationRejected))) + else if (0 == memcmp(static_cast(packetBuffer + offset), amInvitationRejected, sizeof(amInvitationRejected))) { offset += sizeof(amInvitationRejected); } - else if (0 == memcmp((void*)(packetBuffer + offset), amSyncronization, sizeof(amSyncronization))) + else if (0 == memcmp(static_cast(packetBuffer + offset), amSyncronization, sizeof(amSyncronization))) { offset += sizeof(amSyncronization); @@ -168,7 +168,7 @@ Serial.println("Not enough data for Syncronization"); #endif return NOT_ENOUGH_DATA; } - else if (0 == memcmp((void*)(packetBuffer + offset), amReceiverFeedback, sizeof(amReceiverFeedback))) + else if (0 == memcmp(static_cast(packetBuffer + offset), amReceiverFeedback, sizeof(amReceiverFeedback))) { offset += sizeof(amReceiverFeedback); @@ -191,7 +191,7 @@ Serial.println("Not enough data for ReceiverFeedback"); #endif return NOT_ENOUGH_DATA; } - else if (0 == memcmp((void*)(packetBuffer + offset), amBitrateReceiveLimit, sizeof(amBitrateReceiveLimit))) + else if (0 == memcmp(static_cast(packetBuffer + offset), amBitrateReceiveLimit, sizeof(amBitrateReceiveLimit))) { offset += sizeof(amBitrateReceiveLimit); @@ -213,7 +213,7 @@ Serial.println("Not enough data for BitrateReceiveLimit"); #endif return NOT_ENOUGH_DATA; } - else if (0 == memcmp((void*)(packetBuffer + offset), amEndSession, sizeof(amEndSession))) + else if (0 == memcmp(static_cast(packetBuffer + offset), amEndSession, sizeof(amEndSession))) { offset += sizeof(amEndSession); diff --git a/src/utility/packet-rtp-midi.h b/src/utility/packet-rtp-midi.h index 51d8c53..a48e0b9 100644 --- a/src/utility/packet-rtp-midi.h +++ b/src/utility/packet-rtp-midi.h @@ -1196,7 +1196,7 @@ Serial.println (cmd_count); if ( (cmd_count) || (flags & RTP_MIDI_CS_FLAG_Z) ) { /* Decode a delta-time - if 0 is returned something went wrong */ - int consumed = decodetime((IRtpMidi*) appleMidi, packetBuffer, offset, cmd_len); + int consumed = decodetime(appleMidi, packetBuffer, offset, cmd_len); if ( -1 == consumed ) { #ifdef APPLEMIDI_DEBUG Serial.print ("ReportedBoundsError 1"); @@ -1214,8 +1214,8 @@ Serial.print ("ReportedBoundsError 1"); if (cmd_len) { /* Decode a MIDI-command - if 0 is returned something went wrong */ - int consumed = decodemidi((IRtpMidi*) appleMidi, packetBuffer, cmd_count, offset, cmd_len, &runningstatus, &rsoffset); - if ( -1 == consumed ) { + int consumed = decodemidi(appleMidi, packetBuffer, cmd_count, offset, cmd_len, &runningstatus, &rsoffset); + if (-1 == consumed) { #ifdef APPLEMIDI_DEBUG Serial.print ("ReportedBoundsError 2"); #endif @@ -1256,7 +1256,7 @@ Serial.println("journal section"); /* do we have system journal? */ if ( flags & RTP_MIDI_JS_FLAG_Y ) { /* first we need to get the flags & length from the system-journal */ - int consumed = decode_system_journal((IRtpMidi*) appleMidi, packetBuffer, offset); + int consumed = decode_system_journal(appleMidi, packetBuffer, offset); if ( -1 == consumed ) { #ifdef APPLEMIDI_DEBUG @@ -1279,7 +1279,7 @@ Serial.print ("ReportedBoundsError 3"); Serial.print("Processing channel journal: "); Serial.println(i); #endif - int consumed = decode_channel_journal( (IRtpMidi*)appleMidi, packetBuffer, offset); + int consumed = decode_channel_journal(appleMidi, packetBuffer, offset); #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.print("Consumed by channel journal ("); @@ -1305,12 +1305,12 @@ Serial.println("ReportedBoundsError 4"); return offset; } -private: +//private: /* * Here the system-journal is decoded. */ static int - decode_system_journal(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) + decode_system_journal(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_system_journal"); @@ -1383,7 +1383,7 @@ Serial.println("decode_system_journal"); * Here a channel-journal is decoded. */ static int - decode_channel_journal(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_channel_journal(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { uint32_t chanflags; uint16_t chanjourlen; int consumed = 0; @@ -1520,7 +1520,7 @@ Serial.println("decode_system_journal"); * external decoders. */ static int - decodemidi(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte *runningstatus, unsigned int *rsoffset ) + decodemidi(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte *runningstatus, unsigned int *rsoffset ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decodemidi"); @@ -1585,7 +1585,8 @@ Serial.println("RealTime"); if ( octet < 0xf0 ) { *rsoffset = offset ; *runningstatus = octet; - } else { + } + else { /* system-realtime-commands maintain the current running-status * other system-commands clear the running-status, since we * already handled realtime, we can reset it here */ @@ -1598,32 +1599,39 @@ Serial.println("RealTime"); offset++; } - /* non-system MIDI-commands encode the command in the high nibble and the channel * in the low nibble - so we will take care of those cases next */ - if ( octet < 0xf0 ) { - switch ( octet >> 4 ) { + if (octet < 0xf0) { +#ifdef APPLEMIDI_DEBUG_VERBOSE + Serial.print("MIDI command "); + Serial.println(octet >> 4); +#endif + switch (octet >> 4) { case RTP_MIDI_STATUS_CHANNEL_NOTE_OFF: - ext_consumed = decode_note_off(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs ); + ext_consumed = decode_note_off(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs); break; case RTP_MIDI_STATUS_CHANNEL_NOTE_ON: - ext_consumed = decode_note_on(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs ); + ext_consumed = decode_note_on(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs); break; case RTP_MIDI_STATUS_CHANNEL_POLYPHONIC_KEY_PRESSURE: - ext_consumed = decode_poly_pressure(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs ); + ext_consumed = decode_poly_pressure(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs); break; + + // If these below calls are uncommented, the ESP8266 crashes when the execution comes here + // even if the functions are NOT called. Works fine on ARDUINO + case RTP_MIDI_STATUS_CHANNEL_CONTROL_CHANGE: - ext_consumed = decode_control_change(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs ); + ext_consumed = decode_control_change(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs); break; case RTP_MIDI_STATUS_CHANNEL_PROGRAM_CHANGE: - ext_consumed = decode_program_change(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs ); + ext_consumed = decode_program_change(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs); break; case RTP_MIDI_STATUS_CHANNEL_CHANNEL_PRESSURE: - ext_consumed = decode_channel_pressure(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs ); + ext_consumed = decode_channel_pressure(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs); break; case RTP_MIDI_STATUS_CHANNEL_PITCH_BEND_CHANGE: - ext_consumed = decode_pitch_bend_change(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs ); + ext_consumed = decode_pitch_bend_change(rtpMidi, packetBuffer, cmd_count, offset, cmd_len, octet, *rsoffset, using_rs); break; default: ext_consumed = -1; @@ -1631,15 +1639,15 @@ Serial.println("RealTime"); Serial.println("UnknownA"); #endif } - + /* external decoder informed us of error -> pass this through */ - if ( ext_consumed < 0 ) { + if (ext_consumed < 0) { #ifdef APPLEMIDI_DEBUG -Serial.println("errora"); + Serial.println("errora"); #endif return ext_consumed; } - + return consumed + ext_consumed; } @@ -1693,7 +1701,7 @@ Serial.println("errora"); * This decodes the delta-time before a MIDI-command */ static int - decodetime(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int cmd_len) + decodetime(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int cmd_len) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decodetime"); @@ -1723,7 +1731,7 @@ Serial.println("decodetime"); * Here a Note-Off command is decoded. */ static int - decode_note_off(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { + decode_note_off(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_note_off"); @@ -1801,8 +1809,8 @@ Serial.println("aborted MIDI-command 2"); * Here a Note-On command is decoded. */ static int - decode_note_on(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, uint8_t status, unsigned int rsoffset, bool using_rs ) { - + decode_note_on(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, uint8_t status, unsigned int rsoffset, bool using_rs ) + { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_note_on"); #endif @@ -1881,7 +1889,7 @@ Serial.println("aborted MIDI-command 2"); * Here polyphonic aftertouch is decoded. */ static int - decode_poly_pressure(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { + decode_poly_pressure(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_poly_pressure"); @@ -1957,7 +1965,7 @@ Serial.println("aborted MIDI-command"); * Here channel aftertouch is decoded. */ static int - decode_channel_pressure(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { + decode_channel_pressure(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_channel_pressure"); @@ -2012,7 +2020,7 @@ Serial.println("aborted MIDI-command"); * Here pitch-bend is decoded. */ static int - decode_pitch_bend_change(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { + decode_pitch_bend_change(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_pitch_bend_change"); @@ -2095,7 +2103,7 @@ Serial.println("aborted MIDI-command"); * Here program_change is decoded. */ static int - decode_program_change(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { + decode_program_change(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_program_change"); @@ -2151,7 +2159,7 @@ Serial.println("aborted MIDI-command"); * Here control change is decoded. */ static int - decode_control_change(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { + decode_control_change(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len, byte status, unsigned int rsoffset, bool using_rs ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_control_change"); @@ -2243,7 +2251,7 @@ Serial.println("decode_sysex_common_nrt_sd_hdr"); * Here a Sysex-Common Non-Realtime Sample Dump Packet command is decoded. */ static unsigned int - decode_sysex_common_nrt_sd_packet(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_sd_packet(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_sd_packet"); @@ -2257,7 +2265,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime Sample Dump Request command is decoded. */ static unsigned int - decode_sysex_common_nrt_sd_req(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_sd_req(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_sd_req"); @@ -2270,7 +2278,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime Sample Dump Extension command is decoded. */ static unsigned int - decode_sysex_common_nrt_sd_ext(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_sd_ext(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_sd_ext"); @@ -2284,7 +2292,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime General Information command is decoded. */ static unsigned int - decode_sysex_common_nrt_gi(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_gi(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_gi"); @@ -2297,7 +2305,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime File Dump command is decoded. */ static unsigned int - decode_sysex_common_nrt_fd(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_fd(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_fd"); @@ -2311,7 +2319,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * As the code-points do not overlap, both RT and NRT are decoded here... */ static unsigned int - decode_sysex_common_tuning(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_tuning(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_tuning"); @@ -2324,7 +2332,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime General MIDI command is decoded. */ static unsigned int - decode_sysex_common_nrt_gm(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_gm(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_gm"); @@ -2337,7 +2345,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime Downloadable Sounds command is decoded. */ static unsigned int - decode_sysex_common_nrt_dls(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_dls(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_dls"); @@ -2350,7 +2358,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime End Of File command is decoded. */ static unsigned int - decode_sysex_common_nrt_eof(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_eof(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_eof"); @@ -2363,7 +2371,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime Wait command is decoded. */ static unsigned int - decode_sysex_common_nrt_wait(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_wait(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_wait"); @@ -2376,7 +2384,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime Cancel command is decoded. */ static unsigned int - decode_sysex_common_nrt_cancel(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_cancel(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_cancel"); @@ -2389,7 +2397,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime NAK command is decoded. */ static unsigned int - decode_sysex_common_nrt_nak(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_nak(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_nak"); @@ -2402,7 +2410,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime ACK command is decoded. */ static unsigned int - decode_sysex_common_nrt_ack(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_ack(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_ack"); @@ -2416,7 +2424,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * as the codepoints are the same, we decode both realtime and non-realtime here. */ static unsigned int - decode_sysex_common_nrt_mtc(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt_mtc(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt_mtc"); @@ -2430,7 +2438,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * As the codepoints are the same, we decode both realtime and non-realtime here. */ static unsigned int - decode_sysex_common_rt_mtc_cue(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_rt_mtc_cue(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_rt_mtc_cue"); @@ -2443,7 +2451,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Non-Realtime command is decoded. */ static unsigned int - decode_sysex_common_nrt(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_nrt(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_nrt"); @@ -2456,7 +2464,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Realtime MIDI Time Code command is decoded. */ static unsigned int - decode_sysex_common_rt_mtc(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_rt_mtc(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_rt_mtc"); @@ -2469,7 +2477,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Realtime MIDI Show Control command is decoded. */ static unsigned int - decode_sysex_common_rt_sc(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_rt_sc(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_rt_sc"); @@ -2482,7 +2490,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Realtime Notation Information command is decoded. */ static unsigned int - decode_sysex_common_rt_ni(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_rt_ni(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_rt_ni"); @@ -2495,7 +2503,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Realtime Device Control command is decoded. */ static unsigned int - decode_sysex_common_rt_dc(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_rt_dc(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_rt_dc"); @@ -2508,7 +2516,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Realtime MIDI Machine Control command is decoded. */ static unsigned int - decode_sysex_common_rt_mmc_command(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_rt_mmc_command(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_rt_mmc_command"); @@ -2521,7 +2529,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Realtime MIDI Machine Control response is decoded. */ static unsigned int - decode_sysex_common_rt_mmc_response(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_rt_mmc_response(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_rt_mmc_response"); @@ -2534,7 +2542,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * Here a Sysex-Common Realtime command is decoded. */ static unsigned int - decode_sysex_common_rt(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_rt(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_rt"); @@ -2549,7 +2557,7 @@ Serial.println("decode_sysex_common_nrt_sd_packet"); * We don't know what this data encodes, so we just dump it. */ static unsigned int - decode_sysex_common_educational(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { + decode_sysex_common_educational(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_common_educational"); @@ -2569,7 +2577,7 @@ Serial.println("decode_sysex_common_educational"); * We don't know what this data encodes, so we just dump it. */ static unsigned int - decode_sysex_common_manufacturer(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len, unsigned int manu_code) { + decode_sysex_common_manufacturer(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int data_len, unsigned int manu_code) { int consumed = 0; @@ -2589,7 +2597,7 @@ Serial.println("decode_sysex_common_educational"); * Here a Sysex-Start command is decoded. */ static unsigned int - decode_sysex_start(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { + decode_sysex_start(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_start"); @@ -2689,7 +2697,7 @@ Serial.println("decode_sysex_start"); * Here the MIDI-Time-Code (MTC) Quarter Frame command is decoded. */ static int - decode_mtc_quarter_frame(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { + decode_mtc_quarter_frame(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_mtc_quarter_frame"); @@ -2742,7 +2750,7 @@ Serial.println("aborted MIDI-command"); * Here the Song Position Pointer command is decoded. */ static int - decode_song_position_pointer(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { + decode_song_position_pointer(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_song_position_pointer"); @@ -2802,7 +2810,7 @@ Serial.println("aborted MIDI-command"); * Here a Song-Select command is decoded. */ static int - decode_song_select(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { + decode_song_select(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_song_select"); @@ -2841,7 +2849,7 @@ Serial.println("aborted MIDI-command"); * Here the undefined common-command 0xf4 is decoded. */ static int - decode_undefined_f4(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { + decode_undefined_f4(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_undefined_f4"); @@ -2854,7 +2862,7 @@ Serial.println("aborted MIDI-command"); * Here the undefined common-command 0xf5 is decoded. */ static int - decode_undefined_f5(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { + decode_undefined_f5(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_undefined_f5"); @@ -2867,7 +2875,7 @@ Serial.println("aborted MIDI-command"); * Here a Tune-Request command is decoded. */ static int - decode_tune_request(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len) { + decode_tune_request(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_tune_request"); @@ -2882,7 +2890,7 @@ Serial.println("aborted MIDI-command"); * Here a Sysex-End command is decoded - in RTP-MIDI this has a special semantic, it either starts a segmented Sysex-frame or a Sysex-Cancel */ static int - decode_sysex_end(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { + decode_sysex_end(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int cmd_count, unsigned int offset, unsigned int cmd_len ) { #ifdef APPLEMIDI_DEBUG_VERBOSE Serial.println("decode_sysex_end"); @@ -2928,7 +2936,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter Q of the channel-journal is decoded. */ static int - decode_sj_chapter_q(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset ) { + decode_sj_chapter_q(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset ) { uint8_t header; unsigned int start_offset = offset; int len = 1; @@ -2960,7 +2968,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter F of the channel-journal is decoded. */ static int - decode_sj_chapter_f(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_sj_chapter_f(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { uint8_t header; unsigned int start_offset = offset; int len = 1; @@ -2992,7 +3000,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter X of the channel-journal is decoded. */ static int - decode_sj_chapter_x(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int max_length) { + decode_sj_chapter_x(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset, unsigned int max_length) { uint8_t header; uint8_t octet; unsigned int consumed = 0; @@ -3067,7 +3075,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter D of the channel-journal is decoded. */ static int - decode_sj_chapter_d(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_sj_chapter_d(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { int header; unsigned int start_offset = offset; int ext_consumed; @@ -3137,7 +3145,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter D F4-field of the system-journal is decoded. */ static int - decode_sj_chapter_d_f4(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_sj_chapter_d_f4(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { int start_offset = offset; uint16_t f4flags; uint16_t f4length; @@ -3184,7 +3192,7 @@ Serial.println("aborted MIDI-command"); } static int - decode_sj_chapter_d_f5(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_sj_chapter_d_f5(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { unsigned int start_offset = offset; uint16_t f5flags; uint16_t f5length; @@ -3237,7 +3245,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter D F9-field of the system-journal is decoded. */ static int - decode_sj_chapter_d_f9(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_sj_chapter_d_f9(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { unsigned int start_offset = offset; uint8_t f9flags; uint8_t f9length; @@ -3273,7 +3281,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter D FD-field of the system-journal is decoded. */ static int - decode_sj_chapter_d_fd(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_sj_chapter_d_fd(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { unsigned int start_offset = offset; uint8_t fdflags; uint8_t fdlength; @@ -3309,7 +3317,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter c of the channel-journal is decoded. */ static int - decode_cj_chapter_c(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_cj_chapter_c(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { unsigned int start_offset = offset; uint8_t octet; int count; @@ -3336,7 +3344,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter m of the channel-journal is decoded, possibly the most complex part of the RTP-MIDI stuff ;-) */ static int - decode_cj_chapter_m(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_cj_chapter_m(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { uint16_t header; uint8_t logitemheader; int length; @@ -3420,7 +3428,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter n of the channel-journal is decoded. */ static int - decode_cj_chapter_n(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_cj_chapter_n(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { unsigned int start_offset = offset; uint16_t header; uint8_t note; @@ -3476,7 +3484,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter e of the channel-journal is decoded. */ static int - decode_cj_chapter_e(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_cj_chapter_e(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { unsigned int start_offset = offset; uint8_t header; uint8_t note; @@ -3505,7 +3513,7 @@ Serial.println("aborted MIDI-command"); * Here the chapter a of the channel-journal is decoded. */ static int - decode_cj_chapter_a(IRtpMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { + decode_cj_chapter_a(IAppleMidi* rtpMidi, unsigned char* packetBuffer, unsigned int offset) { unsigned int start_offset = offset; uint8_t header; uint8_t note; @@ -3529,8 +3537,6 @@ Serial.println("aborted MIDI-command"); return offset-start_offset; } - - }; END_APPLEMIDI_NAMESPACE diff --git a/src/utility/packet-rtp.h b/src/utility/packet-rtp.h index a62e6bd..c727bae 100644 --- a/src/utility/packet-rtp.h +++ b/src/utility/packet-rtp.h @@ -101,8 +101,13 @@ Serial.println(packetSize); static int rtp_info_current = 0; struct _rtp_info *rtp_info; +#ifdef APPLEMIDI_DEBUG_VERBOSE + Serial.print("current: "); + Serial.println(rtp_info_current); +#endif + rtp_info_current++; - if (rtp_info_current==4) { + if (rtp_info_current == 4) { rtp_info_current=0; } rtp_info = &rtp_info_arr[rtp_info_current]; @@ -150,7 +155,7 @@ Serial.print ("Seq Number: "); Serial.println(seq_num, HEX); Serial.print ("Timestamp : "); Serial.println(timestamp, HEX); -Serial.print ("Sync Src : "); +Serial.print("Sync Src : "); Serial.println(sync_src, HEX); #endif