Skip to content

Using AppleMIDI Callbacks

lathoub edited this page Feb 12, 2021 · 5 revisions

AppleMIDI callbacks

AppleMIDI callback work in the same way as MIDI Callbacks.

Default AppleMIDI callbacks

Similar to MIDI callbacks, AppleMIDI has its own specific callbacks.

  • setHandleConnected
  • setHandleDisconnected

Example:

#define SerialMon Serial
#define APPLEMIDI_DEBUG SerialMon
#include <AppleMIDI.h>

  AppleMIDI.setHandleConnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc, const char* name) {
    isConnected++;
    DBG(F("Connected to session"), ssrc, name);
  });
  AppleMIDI.setHandleDisconnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc) {
    isConnected--;
    DBG(F("Disconnected"), ssrc);
  });

Extended AppleMIDI Callbacks

Define USE_EXT_CALLBACKS before including #include <AppleMIDI.h> (Extended callbacks need to be explicitly defined, to avoid using too much memory)

The following callback are available (the name is self-explanatory):

  • setHandleException
  • setHandleReceivedRtp
  • setHandleStartReceivedMidi
  • setHandleReceivedMidi
  • setHandleEndReceivedMidi
  • setHandleSentRtp
  • setHandleSentRtpMidi

Example:

#define USE_EXT_CALLBACKS
#define SerialMon Serial
#define APPLEMIDI_DEBUG SerialMon
#include <AppleMIDI.h>

  AppleMIDI.setHandleSentRtp([](const APPLEMIDI_NAMESPACE::Rtp_t & rtp) {
    //  DBG(F("an rtpMessage has been sent with sequenceNr"), rtp.sequenceNr);
  });

Full example here

Exceptions

enum Exception : uint8_t
{
    BufferFullException,
    ParseException,
    UnexpectedParseException,
    TooManyParticipantsException,
    ComputerNotInDirectory,
    NotAcceptingAnyone,
    UnexpectedInviteException,
    ParticipantNotFoundException,
    ListenerTimeOutException,
    MaxAttemptsException,
    NoResponseFromConnectionRequestException,
    SendPacketsDropped,
    ReceivedPacketsDropped,
    UdpBeginPacketFailed,
};

Example:

...
AppleMIDI.setHandleException(OnAppleMidiException);
...

void OnAppleMidiException(const APPLEMIDI_NAMESPACE::ssrc_t& ssrc, const APPLEMIDI_NAMESPACE::Exception& e, const int32_t value ) {
  switch (e)
  {
    case APPLEMIDI_NAMESPACE::Exception::BufferFullException:
      DBG(F("*** BufferFullException"));
      break;
    case APPLEMIDI_NAMESPACE::Exception::ParseException:
      DBG(F("*** ParseException"));
      break;
    case APPLEMIDI_NAMESPACE::Exception::TooManyParticipantsException:
      DBG(F("*** TooManyParticipantsException"));
      break;
    case APPLEMIDI_NAMESPACE::Exception::UnexpectedInviteException:
      DBG(F("*** UnexpectedInviteException"));
      break;
    case APPLEMIDI_NAMESPACE::Exception::ParticipantNotFoundException:
      DBG(F("*** ParticipantNotFoundException"), value);
      break;
    case APPLEMIDI_NAMESPACE::Exception::ComputerNotInDirectory:
      DBG(F("*** ComputerNotInDirectory"), value);
      break;
    case APPLEMIDI_NAMESPACE::Exception::NotAcceptingAnyone:
      DBG(F("*** NotAcceptingAnyone"), value);
      break;
    case APPLEMIDI_NAMESPACE::Exception::ListenerTimeOutException:
      DBG(F("*** ListenerTimeOutException"));
      break;
    case APPLEMIDI_NAMESPACE::Exception::MaxAttemptsException:
      DBG(F("*** MaxAttemptsException"));
      break;
    case APPLEMIDI_NAMESPACE::Exception::NoResponseFromConnectionRequestException:
      DBG(F("***:yyy did't respond to the connection request. Check the address and port, and any firewall or router settings. (time)"));
      break;
    case APPLEMIDI_NAMESPACE::Exception::SendPacketsDropped:
      DBG(F("*** SendPacketsDropped"), value);
      break;
    case APPLEMIDI_NAMESPACE::Exception::ReceivedPacketsDropped:
      DBG(F("******************************************** ReceivedPacketsDropped"), value);
      break;
    case APPLEMIDI_NAMESPACE::Exception::UdpBeginPacketFailed:
      DBG(F("*** UdpBeginPacketFailed"), value);
      break;
  }
}