Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V.3.0.0rc #98

Merged
merged 68 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
e12eb30
debug text update (very minor update)
lathoub Dec 21, 2020
28a84a7
debug update
lathoub Dec 21, 2020
9d47f31
deprecate Error callback, replace with Exception callback
lathoub Dec 21, 2020
1db81ad
removed error callback, replace with Exception
lathoub Dec 21, 2020
4e5cba4
NULL => nullptr
lathoub Dec 21, 2020
a7917d1
Update library.properties
lathoub Dec 21, 2020
80624d7
Update ESP32_Callbacks.ino
lathoub Dec 21, 2020
38a07cb
advance callbacks, added sendRtp
lathoub Dec 21, 2020
4120625
extented callbacks now optional to save weight
lathoub Dec 22, 2020
7bf2d0d
minimum memory usage example
lathoub Dec 22, 2020
397eab2
renamed EthernetShield to AVR for AVR type Arduino's
lathoub Dec 22, 2020
e29094d
min memory setup, #defines for add functions
lathoub Dec 22, 2020
a874521
Update README.md
lathoub Dec 22, 2020
2777a99
Update README.md
lathoub Dec 23, 2020
cd5a8fd
Update README.md
lathoub Dec 23, 2020
d835296
further weight reduction #define ONE_PARTICIPANT
lathoub Dec 23, 2020
2c6b047
Merge branch 'v.3.0.0rc' of https://github.com/lathoub/Arduino-AppleM…
lathoub Dec 23, 2020
cf1ba1a
Update README.md
lathoub Dec 23, 2020
f425163
furtner memory reduction
lathoub Dec 23, 2020
2824421
Merge branch 'v.3.0.0rc' of https://github.com/lathoub/Arduino-AppleM…
lathoub Dec 23, 2020
952c31d
Update README.md
lathoub Dec 23, 2020
8654ca3
Update README.md
lathoub Dec 23, 2020
8a138d1
Update README.md
lathoub Dec 23, 2020
777a698
Update AVR_NoteOnOffEverySec.ino
lathoub Dec 23, 2020
3d98cf9
Merge branch 'v.3.0.0rc' of https://github.com/lathoub/Arduino-AppleM…
lathoub Dec 23, 2020
df64cae
added Directory
lathoub Dec 24, 2020
b4a0b24
joined USE_EXT_CALLBACKS, LATENCY_CALCULATION
lathoub Dec 24, 2020
68c9082
Update AppleMIDI.h
lathoub Dec 24, 2020
88850b3
fixed crash when session name is too long (> MaxSessionName)
lathoub Dec 28, 2020
095e6d4
Create ESP32_NoteOnOffEverySec.ino
lathoub Dec 28, 2020
2f6d510
get* as const functions
lathoub Dec 28, 2020
613b767
Update README.md
lathoub Dec 28, 2020
4526bfb
Update AppleMIDI.h
lathoub Dec 28, 2020
d69f41d
Merge branch 'v.3.0.0rc' of https://github.com/lathoub/Arduino-AppleM…
lathoub Dec 28, 2020
9303945
removed extented callback from wESP32 demo
lathoub Dec 28, 2020
49e5121
added sentRtpMidiCallback, renamed send -> sent
lathoub Dec 28, 2020
2467d2d
added ssrc to ReceiveMidi calls (private functions)
lathoub Dec 28, 2020
5e3678d
receiverFeedback test is < not !=
lathoub Dec 29, 2020
ce7e8a7
receivedRtpCallback with ssrc, disconnect with ssrc
lathoub Dec 29, 2020
296717c
Update .travis.yml
lathoub Dec 29, 2020
8117dd3
Update README.md
lathoub Dec 29, 2020
9294a50
Update README.md
lathoub Dec 29, 2020
299004c
Update .travis.yml
lathoub Dec 29, 2020
2739ce6
Update .travis.yml
lathoub Dec 29, 2020
3c95ee1
Update .travis.yml
lathoub Dec 29, 2020
4f7f9ab
Update .travis.yml
lathoub Dec 29, 2020
5aa8c69
update examples
lathoub Dec 29, 2020
9f08a63
Merge branch 'v.3.0.0rc' of https://github.com/lathoub/Arduino-AppleM…
lathoub Dec 29, 2020
9c6d4c3
Update .travis.yml
lathoub Dec 29, 2020
33be4a4
fixing examples
lathoub Dec 29, 2020
ba3a300
Merge branch 'v.3.0.0rc' of https://github.com/lathoub/Arduino-AppleM…
lathoub Dec 29, 2020
8936cd7
Update .travis.yml
lathoub Dec 29, 2020
08366e5
Update .travis.yml
lathoub Dec 29, 2020
ca50b41
Update .travis.yml
lathoub Dec 29, 2020
e069ffb
Update .travis.yml
lathoub Dec 29, 2020
0fc91fd
Update .travis.yml
lathoub Dec 29, 2020
122f778
Create AVR_E3_NoteOnOffEverySec.ino
lathoub Dec 29, 2020
4e6f680
Merge branch 'v.3.0.0rc' of https://github.com/lathoub/Arduino-AppleM…
lathoub Dec 29, 2020
c0a99a5
Update .travis.yml
lathoub Dec 30, 2020
09f5f16
Update wESP32_NoteOnOffEverySec.ino
lathoub Dec 30, 2020
608ddd1
Merge branch 'v.3.0.0rc' of https://github.com/lathoub/Arduino-AppleM…
lathoub Dec 30, 2020
79cbccf
Update .travis.yml
lathoub Dec 30, 2020
ebf3cb5
Update README.md
lathoub Dec 30, 2020
2bb78db
Update README.md
lathoub Dec 30, 2020
c9a3e81
Update README.md
lathoub Dec 30, 2020
24e92b8
Update README.md
lathoub Dec 30, 2020
299286b
Update README.md
lathoub Dec 30, 2020
4dd5446
Update README.md
lathoub Dec 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
extented callbacks now optional to save weight
  • Loading branch information
lathoub committed Dec 22, 2020
commit 4120625540af35caf74745e2bc8fcd268fed0b5a
82 changes: 82 additions & 0 deletions examples/EthernetShield_Callbacks/EthernetShield_Callbacks.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <Ethernet.h>

#define DISCARD_SESSION_NAME
#define NO_LATENCY_CALCULATION
#define NO_EXT_CALLBACKS
#define SerialMon Serial
#define APPLEMIDI_DEBUG SerialMon
#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 t1 = millis();
bool isConnected = false;

APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE();

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void setup()
{
DBG_SETUP(115200);
DBG("Booting");

if (Ethernet.begin(mac) == 0) {
DBG(F("Failed DHCP, check network cable & reboot"));
for (;;);
}

DBG(F("OK, now make sure you an rtpMIDI session that is Enabled"));
DBG(F("Add device named Arduino with Host"), Ethernet.localIP(), "Port", AppleMIDI.getPort(), "(Name", AppleMIDI.getName(), ")");
DBG(F("Select and then press the Connect button"));
DBG(F("Then open a MIDI listener and monitor incoming notes"));

MIDI.begin();

// Stay informed on connection status
AppleMIDI.setHandleConnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc, const char* name) {
isConnected = true;
DBG(F("Connected to session"), name);
});
AppleMIDI.setHandleDisconnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc) {
isConnected = false;
DBG(F("Disconnected"));
});

MIDI.setHandleNoteOn([](byte channel, byte note, byte velocity) {
DBG(F("NoteOn"), note);
});
MIDI.setHandleNoteOff([](byte channel, byte note, byte velocity) {
DBG(F("NoteOff"), note);
});

DBG(F("Sending MIDI messages every second"));
}

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void loop()
{
// Listen to incoming notes
MIDI.read();

// send a note every second
// (dont cáll delay(1000) as it will stall the pipeline)
if (isConnected && (millis() - t1) > 1000)
{
t1 = millis();

byte note = random(1, 127);
byte velocity = 55;
byte channel = 1;

MIDI.sendNoteOn(note, velocity, channel);
// MIDI.sendNoteOff(note, velocity, channel);
}
}
17 changes: 11 additions & 6 deletions src/AppleMIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ class AppleMIDISession

void setHandleConnected(void (*fptr)(const ssrc_t&, const char*)) { _connectedCallback = fptr; }
void setHandleDisconnected(void (*fptr)(const ssrc_t&)) { _disconnectedCallback = fptr; }
void setHandleException(void (*fptr)(const ssrc_t&, const Exception&, const int32_t value)) { _exceptionCallback = fptr; }
#ifdef USE_EXT_CALLBACKS
void setHandleException(void (*fptr)(const ssrc_t&, const Exception&, const int32_t value)) { _exceptionCallback = fptr; }
void setHandleReceivedRtp(void (*fptr)(const Rtp_t&, const int32_t&)) { _receivedRtpCallback = fptr; }
void setHandleStartReceivedMidi(void (*fptr)(const ssrc_t&)) { _startReceivedMidiByteCallback = fptr; }
void setHandleReceivedMidi(void (*fptr)(const ssrc_t&, byte)) { _receivedMidiByteCallback = fptr; }
void setHandleEndReceivedMidi(void (*fptr)(const ssrc_t&)) { _endReceivedMidiByteCallback = fptr; }
void setHandleSendRtp(void (*fptr)(const Rtp_t&)) { _sendRtpCallback = fptr; }
#endif

const char* getName() { return this->localName; };
const uint16_t getPort() { return this->port; };
Expand All @@ -74,6 +76,7 @@ class AppleMIDISession
void sendEndSession();

public:
// Override default thruActivated
static const bool thruActivated = false;

void begin()
Expand All @@ -91,7 +94,6 @@ class AppleMIDISession
// this is our SSRC
//
// NOTE: Arduino random only goes to INT32_MAX (not UINT32_MAX)

this->ssrc = random(1, INT32_MAX) * 2;

controlPort.begin(port);
Expand Down Expand Up @@ -151,8 +153,10 @@ class AppleMIDISession
}
else
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, BufferFullException, 0);
#endif
}
}

Expand Down Expand Up @@ -219,14 +223,15 @@ class AppleMIDISession
rtpMIDIParser<UdpClass, Settings, Platform> _rtpMIDIParser;

connectedCallback _connectedCallback = nullptr;
disconnectedCallback _disconnectedCallback = nullptr;
#ifdef USE_EXT_CALLBACKS
startReceivedMidiByteCallback _startReceivedMidiByteCallback = nullptr;
receivedMidiByteCallback _receivedMidiByteCallback = nullptr;
endReceivedMidiByteCallback _endReceivedMidiByteCallback = nullptr;
receivedRtpCallback _receivedRtpCallback = nullptr;
disconnectedCallback _disconnectedCallback = nullptr;
exceptionCallback _exceptionCallback = nullptr;
sendRtpCallback _sendRtpCallback = nullptr;

exceptionCallback _exceptionCallback = nullptr;
#endif
// buffer for incoming and outgoing MIDI messages
MidiBuffer_t inMidiBuffer;
MidiBuffer_t outMidiBuffer;
Expand All @@ -237,7 +242,7 @@ class AppleMIDISession
char localName[DefaultSettings::MaxSessionNameLen + 1];
uint16_t port = DEFAULT_CONTROL_PORT;
Deque<Participant<Settings>, Settings::MaxNumberOfParticipants> participants;
int32_t latencyAdjustment = 0;
// int32_t latencyAdjustment = 0; // not used

private:
void readControlPackets();
Expand Down
83 changes: 46 additions & 37 deletions src/AppleMIDI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ void AppleMIDISession<UdpClass, Settings, Platform>::parseControlPackets()
auto retVal = _appleMIDIParser.parse(controlBuffer, amPortType::Control);
if (retVal == parserReturn::UnexpectedData)
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, ParseException, 0);

#endif
controlBuffer.pop_front();
}
}
Expand Down Expand Up @@ -81,9 +82,10 @@ void AppleMIDISession<UdpClass, Settings, Platform>::parseDataPackets()
|| retVal2 == parserReturn::NotSureGiveMeMoreData)
break; // one or the other buffer does not have enough data

#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, UnexpectedParseException, 0);

#endif
dataBuffer.pop_front();
}
}
Expand Down Expand Up @@ -114,9 +116,10 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedControlInvitation(A
{
writeInvitation(controlPort, controlPort.remoteIP(), controlPort.remotePort(), invitation, amInvitationRejected);

#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, TooManyParticipantsException, 0);

#endif
return;
}

Expand All @@ -143,9 +146,10 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedDataInvitation(Appl
{
writeInvitation(dataPort, dataPort.remoteIP(), dataPort.remotePort(), invitation, amInvitationRejected);

#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, ParticipantNotFoundException, invitation.ssrc);
#endif
return;
}

Expand Down Expand Up @@ -259,6 +263,11 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedSynchronization(App
auto participant = getParticipantBySSRC(synchronization.ssrc);
if (nullptr == participant)
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, ParticipantNotFoundException, synchronization.ssrc);
#endif

return;
}

Expand Down Expand Up @@ -313,19 +322,6 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedSynchronization(App
#ifdef LATENCY_CALCULATION
// each party can estimate the offset between the two clocks using the following formula
participant->offsetEstimate = (uint32_t)(((synchronization.timestamps[2] + synchronization.timestamps[0]) / 2) - synchronization.timestamps[1]);
/*
uint64_t remoteAverage = ((synchronization.timestamps[2] + synchronization.timestamps[0]) / 2);
uint64_t localAverage = synchronization.timestamps[1];

static uint64_t oldRemoteAverage = 0;
static uint64_t oldLocalAverage = 0;

uint64_t r = (remoteAverage - oldRemoteAverage);
uint64_t l = (localAverage - oldLocalAverage);

oldRemoteAverage = remoteAverage;
oldLocalAverage = localAverage;
*/
#endif
break;
}
Expand All @@ -348,18 +344,20 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedReceiverFeedback(Ap
// Here is where you would correct if packets are dropped (send them again)

auto participant = getParticipantBySSRC(receiverFeedback.ssrc);
if (nullptr != participant) {
if (participant->sendSequenceNr != receiverFeedback.sequenceNr)
{
Serial.print("ERRORRORORORORORORRROROROROROROR");

if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, SendPacketsDropped, participant->sendSequenceNr - receiverFeedback.sequenceNr);
if (nullptr == participant) {
#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, ParticipantNotFoundException, receiverFeedback.ssrc);
#endif
return;
}

Serial.print(participant->sendSequenceNr);
Serial.print(" ");
Serial.println(receiverFeedback.sequenceNr);
}
if (participant->sendSequenceNr != receiverFeedback.sequenceNr)
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, SendPacketsDropped, participant->sendSequenceNr - receiverFeedback.sequenceNr);
#endif
}
}

Expand Down Expand Up @@ -516,16 +514,19 @@ void AppleMIDISession<UdpClass, Settings, Platform>::writeRtpMidiBuffer(Particip
// have an option to not transmit messages with future timestamps, to accommodate hardware not
// prepared to defer rendering the messages until the proper time.)
//
rtp.timestamp = (Settings::TimestampRtpPackets) ? htonl(rtpMidiClock.Now()) : 0;
rtp.timestamp = (Settings::TimestampRtpPackets) ? rtpMidiClock.Now() : 0;

// increment the sequenceNr
participant->sendSequenceNr++;

rtp.sequenceNr = participant->sendSequenceNr;

#ifdef USE_EXT_CALLBACKS
if (_sendRtpCallback)
_sendRtpCallback(rtp);
#endif

rtp.timestamp = htonl(rtp.timestamp);
rtp.ssrc = htonl(rtp.ssrc);
rtp.sequenceNr = htons(rtp.sequenceNr);

Expand Down Expand Up @@ -606,9 +607,10 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronizationListen
// otherwise the responder may assume that the initiator has died and terminate the session.
if (now - participant->lastSyncExchangeTime > Settings::CK_MaxTimeOut)
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, ListenerTimeOutException, 0);

#endif
sendEndSession(participant);

participants.erase(i);
Expand Down Expand Up @@ -672,9 +674,10 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronizationInitia
{
if (participant->synchronizationCount > DefaultSettings::MaxSynchronizationCK0Attempts)
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, MaxAttemptsException, 0);

#endif
// After too many attempts, stop.
sendEndSession(participant);

Expand Down Expand Up @@ -731,9 +734,10 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSessionInvites()
{
if (participant->connectionAttempts >= DefaultSettings::MaxSessionInvitesAttempts)
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, NoResponseFromConnectionRequestException, 0);

#endif
// After too many attempts, stop.
sendEndSession(participant);

Expand Down Expand Up @@ -869,19 +873,18 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedRtp(const Rtp_t& rt

if (participant->receiveSequenceNr + 1 != rtp.sequenceNr) {

#ifdef USE_EXT_CALLBACKS
if (nullptr != _exceptionCallback)
_exceptionCallback(ssrc, ReceivedPacketsDropped, participant->receiveSequenceNr + 1 - rtp.sequenceNr);

Serial.print("___ERROR____");
Serial.print(participant->receiveSequenceNr);
Serial.print(" ");
Serial.println(rtp.sequenceNr);
#endif
}

participant->receiveSequenceNr = rtp.sequenceNr;

#ifdef USE_EXT_CALLBACKS
if (nullptr != _receivedRtpCallback)
_receivedRtpCallback(rtp, latency);
#endif
}
else
{
Expand All @@ -892,24 +895,30 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedRtp(const Rtp_t& rt
template <class UdpClass, class Settings, class Platform>
void AppleMIDISession<UdpClass, Settings, Platform>::StartReceivedMidi()
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _startReceivedMidiByteCallback)
_startReceivedMidiByteCallback(0);
#endif
}

template <class UdpClass, class Settings, class Platform>
void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedMidi(byte value)
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _receivedMidiByteCallback)
_receivedMidiByteCallback(0, value);
#endif

inMidiBuffer.push_back(value);
}

template <class UdpClass, class Settings, class Platform>
void AppleMIDISession<UdpClass, Settings, Platform>::EndReceivedMidi()
{
#ifdef USE_EXT_CALLBACKS
if (nullptr != _endReceivedMidiByteCallback)
_endReceivedMidiByteCallback(0);
#endif
}

END_APPLEMIDI_NAMESPACE
Loading