|
| 1 | +#include <Ethernet.h> |
| 2 | + |
| 3 | +#define USE_EXT_CALLBACKS |
| 4 | +#define SerialMon Serial |
| 5 | +#define APPLEMIDI_DEBUG SerialMon |
| 6 | +#include <AppleMIDI.h> |
| 7 | + |
| 8 | +// Enter a MAC address for your controller below. |
| 9 | +// Newer Ethernet shields have a MAC address printed on a sticker on the shield |
| 10 | +byte mac[] = { |
| 11 | + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED |
| 12 | +}; |
| 13 | + |
| 14 | +unsigned long t1 = millis(); |
| 15 | +int8_t isConnected = 0; |
| 16 | + |
| 17 | +APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE(); |
| 18 | + |
| 19 | +void OnAppleMidiException(const APPLEMIDI_NAMESPACE::ssrc_t&, const APPLEMIDI_NAMESPACE::Exception&, const int32_t); |
| 20 | + |
| 21 | +// ----------------------------------------------------------------------------- |
| 22 | +// |
| 23 | +// ----------------------------------------------------------------------------- |
| 24 | +void setup() |
| 25 | +{ |
| 26 | + DBG_SETUP(115200); |
| 27 | + DBG("Das Booting"); |
| 28 | + |
| 29 | + if (Ethernet.begin(mac) == 0) { |
| 30 | + DBG(F("Failed DHCP, check network cable & reboot")); |
| 31 | + for (;;); |
| 32 | + } |
| 33 | + |
| 34 | + DBG(F("OK, now make sure you an rtpMIDI session that is Enabled")); |
| 35 | + DBG(F("Add device named Arduino with Host"), Ethernet.localIP(), "Port", AppleMIDI.getPort(), "(Name", AppleMIDI.getName(), ")"); |
| 36 | + DBG(F("Select and then press the Connect button")); |
| 37 | + DBG(F("Then open a MIDI listener and monitor incoming notes")); |
| 38 | + |
| 39 | + MIDI.begin(); |
| 40 | + |
| 41 | + // Normal callbacks - always available |
| 42 | + // Stay informed on connection status |
| 43 | + AppleMIDI.setHandleConnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc, const char* name) { |
| 44 | + isConnected++; |
| 45 | + DBG(F("Connected to session"), ssrc, name); |
| 46 | + }); |
| 47 | + AppleMIDI.setHandleDisconnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc) { |
| 48 | + isConnected--; |
| 49 | + DBG(F("Disconnected"), ssrc); |
| 50 | + }); |
| 51 | + |
| 52 | + // Extended callback, only available when defining USE_EXT_CALLBACKS |
| 53 | + AppleMIDI.setHandleSentRtp([](const APPLEMIDI_NAMESPACE::Rtp_t & rtp) { |
| 54 | + DBG(F("an rtpMessage has been sent with sequenceNr"), rtp.sequenceNr); |
| 55 | + }); |
| 56 | + AppleMIDI.setHandleSentRtpMidi([](const APPLEMIDI_NAMESPACE::RtpMIDI_t& rtpMidi) { |
| 57 | + DBG(F("an rtpMidiMessage has been sent"), rtpMidi.flags); |
| 58 | + }); |
| 59 | + AppleMIDI.setHandleReceivedRtp([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc, const APPLEMIDI_NAMESPACE::Rtp_t & rtp, const int32_t& latency) { |
| 60 | + DBG(F("setHandleReceivedRtp"), ssrc, rtp.sequenceNr , "with", latency, "ms latency"); |
| 61 | + }); |
| 62 | + AppleMIDI.setHandleStartReceivedMidi([](const APPLEMIDI_NAMESPACE::ssrc_t& ssrc) { |
| 63 | + DBG(F("setHandleStartReceivedMidi from SSRC"), ssrc); |
| 64 | + }); |
| 65 | + AppleMIDI.setHandleReceivedMidi([](const APPLEMIDI_NAMESPACE::ssrc_t& ssrc, byte value) { |
| 66 | + DBG(F("setHandleReceivedMidi from SSRC"), ssrc, ", value:", value); |
| 67 | + }); |
| 68 | + AppleMIDI.setHandleEndReceivedMidi([](const APPLEMIDI_NAMESPACE::ssrc_t& ssrc) { |
| 69 | + DBG(F("setHandleEndReceivedMidi from SSRC"), ssrc); |
| 70 | + }); |
| 71 | + AppleMIDI.setHandleException(OnAppleMidiException); |
| 72 | + |
| 73 | + MIDI.setHandleNoteOn([](byte channel, byte note, byte velocity) { |
| 74 | + DBG(F("NoteOn"), note); |
| 75 | + }); |
| 76 | + MIDI.setHandleNoteOff([](byte channel, byte note, byte velocity) { |
| 77 | + DBG(F("NoteOff"), note); |
| 78 | + }); |
| 79 | + |
| 80 | + DBG(F("Sending MIDI messages every second")); |
| 81 | +} |
| 82 | + |
| 83 | +// ----------------------------------------------------------------------------- |
| 84 | +// |
| 85 | +// ----------------------------------------------------------------------------- |
| 86 | +void loop() |
| 87 | +{ |
| 88 | + // Listen to incoming notes |
| 89 | + MIDI.read(); |
| 90 | + |
| 91 | + // send a note every second |
| 92 | + // (dont cáll delay(1000) as it will stall the pipeline) |
| 93 | + if ((isConnected > 0) && (millis() - t1) > 1000) |
| 94 | + { |
| 95 | + t1 = millis(); |
| 96 | + |
| 97 | + byte note = random(1, 127); |
| 98 | + byte velocity = 55; |
| 99 | + byte channel = 1; |
| 100 | + |
| 101 | + DBG(F("\nsendNoteOn"), note, velocity, channel); |
| 102 | + MIDI.sendNoteOn(note, velocity, channel); |
| 103 | + //MIDI.sendNoteOff(note, velocity, channel); |
| 104 | + |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// ----------------------------------------------------------------------------- |
| 109 | +// |
| 110 | +// ----------------------------------------------------------------------------- |
| 111 | +void OnAppleMidiException(const APPLEMIDI_NAMESPACE::ssrc_t& ssrc, const APPLEMIDI_NAMESPACE::Exception& e, const int32_t value ) { |
| 112 | + switch (e) |
| 113 | + { |
| 114 | + case APPLEMIDI_NAMESPACE::Exception::BufferFullException: |
| 115 | + DBG(F("*** BufferFullException")); |
| 116 | + break; |
| 117 | + case APPLEMIDI_NAMESPACE::Exception::ParseException: |
| 118 | + DBG(F("*** ParseException")); |
| 119 | + break; |
| 120 | + case APPLEMIDI_NAMESPACE::Exception::TooManyParticipantsException: |
| 121 | + DBG(F("*** TooManyParticipantsException")); |
| 122 | + break; |
| 123 | + case APPLEMIDI_NAMESPACE::Exception::UnexpectedInviteException: |
| 124 | + DBG(F("*** UnexpectedInviteException")); |
| 125 | + break; |
| 126 | + case APPLEMIDI_NAMESPACE::Exception::ParticipantNotFoundException: |
| 127 | + DBG(F("*** ParticipantNotFoundException"), value); |
| 128 | + break; |
| 129 | + case APPLEMIDI_NAMESPACE::Exception::ComputerNotInDirectory: |
| 130 | + DBG(F("*** ComputerNotInDirectory"), value); |
| 131 | + break; |
| 132 | + case APPLEMIDI_NAMESPACE::Exception::NotAcceptingAnyone: |
| 133 | + DBG(F("*** NotAcceptingAnyone"), value); |
| 134 | + break; |
| 135 | + case APPLEMIDI_NAMESPACE::Exception::ListenerTimeOutException: |
| 136 | + DBG(F("*** ListenerTimeOutException")); |
| 137 | + break; |
| 138 | + case APPLEMIDI_NAMESPACE::Exception::MaxAttemptsException: |
| 139 | + DBG(F("*** MaxAttemptsException")); |
| 140 | + break; |
| 141 | + case APPLEMIDI_NAMESPACE::Exception::NoResponseFromConnectionRequestException: |
| 142 | + DBG(F("***:yyy did't respond to the connection request. Check the address and port, and any firewall or router settings. (time)")); |
| 143 | + break; |
| 144 | + case APPLEMIDI_NAMESPACE::Exception::SendPacketsDropped: |
| 145 | + DBG(F("*** SendPacketsDropped"), value); |
| 146 | + break; |
| 147 | + case APPLEMIDI_NAMESPACE::Exception::ReceivedPacketsDropped: |
| 148 | + DBG(F("*** ReceivedPacketsDropped"), value); |
| 149 | + break; |
| 150 | + } |
| 151 | +} |
0 commit comments