Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion src/AppleMIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class AppleMIDISession
this->port = port;
#ifdef KEEP_SESSION_NAME
strncpy(this->localName, sessionName, Settings::MaxSessionNameLen);
this->localName[Settings::MaxSessionNameLen] = '\0';
#endif

#ifdef ONE_PARTICIPANT
Expand Down Expand Up @@ -117,6 +118,7 @@ class AppleMIDISession
AppleMIDISession &setName(const char *sessionName)
{
strncpy(this->localName, sessionName, Settings::MaxSessionNameLen);
this->localName[Settings::MaxSessionNameLen] = '\0';
return *this;
};
#else
Expand Down Expand Up @@ -170,7 +172,7 @@ class AppleMIDISession
// this is our SSRC
//
// NOTE: Arduino random only goes to INT32_MAX (not UINT32_MAX)
this->ssrc = random(1, INT32_MAX) * 2;
this->ssrc = random(1, INT32_MAX / 2) * 2;

controlPort.begin(port);
dataPort.begin(port + 1);
Expand Down
61 changes: 41 additions & 20 deletions src/AppleMIDI.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "AppleMIDI_Namespace.h"
#include <string.h>

BEGIN_APPLEMIDI_NAMESPACE

Expand All @@ -17,8 +18,7 @@ size_t AppleMIDISession<UdpClass, Settings, Platform>::readControlPackets()
auto bytesRead = controlPort.read(packetBuffer, bytesToRead);
packetSize -= bytesRead;

for (auto i = 0; i < bytesRead; i++)
controlBuffer.push_back(packetBuffer[i]);
controlBuffer.push_back(packetBuffer, bytesRead);
}

return controlBuffer.size();
Expand Down Expand Up @@ -65,8 +65,7 @@ size_t AppleMIDISession<UdpClass, Settings, Platform>::readDataPackets()
auto bytesRead = dataPort.read(packetBuffer, bytesToRead);
packetSize -= bytesRead;

for (auto i = 0; i < bytesRead; i++)
dataBuffer.push_back(packetBuffer[i]);
dataBuffer.push_back(packetBuffer, bytesRead);
}

return dataBuffer.size();
Expand Down Expand Up @@ -149,6 +148,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::ReceivedControlInvitation(A
participant.lastSyncExchangeTime = now;
#ifdef KEEP_SESSION_NAME
strncpy(participant.sessionName, invitation.sessionName, Settings::MaxSessionNameLen);
participant.sessionName[Settings::MaxSessionNameLen] = '\0';
#endif

#ifdef KEEP_SESSION_NAME
Expand Down Expand Up @@ -690,10 +690,12 @@ void AppleMIDISession<UdpClass, Settings, Platform>::writeRtpMidiBuffer(Particip
return;
}

// write rtp header
dataPort.write((uint8_t *)&rtp, sizeof(rtp));
// Write RTP + rtpMIDI in a single packet to reduce overhead.
uint8_t packet[sizeof(Rtp) + 2 + Settings::MaxBufferSize];
size_t offset = 0;
memcpy(packet + offset, &rtp, sizeof(rtp));
offset += sizeof(rtp);

// Write rtpMIDI section
RtpMIDI_t rtpMidi;

// 0 1 2 3
Expand All @@ -711,21 +713,21 @@ void AppleMIDISession<UdpClass, Settings, Platform>::writeRtpMidiBuffer(Particip
{ // Short header
rtpMidi.flags |= (uint8_t)bufferLen;
rtpMidi.flags &= ~RTP_MIDI_CS_FLAG_B; // short header, clear B-FLAG
dataPort.write(rtpMidi.flags);
packet[offset++] = rtpMidi.flags;
}
else
{ // Long header
rtpMidi.flags |= (uint8_t)(bufferLen >> 8);
rtpMidi.flags |= RTP_MIDI_CS_FLAG_B; // set B-FLAG for long header
dataPort.write(rtpMidi.flags);
dataPort.write((uint8_t)(bufferLen));
packet[offset++] = rtpMidi.flags;
packet[offset++] = (uint8_t)(bufferLen);
}

// write out the MIDI Section
for (size_t i = 0; i < bufferLen; i++)
dataPort.write(outMidiBuffer[i]);
offset += outMidiBuffer.copy_out(packet + offset, bufferLen);

// *No* journal section (Not supported)
dataPort.write(packet, offset);

dataPort.endPacket();
dataPort.flush();
Expand All @@ -743,19 +745,28 @@ template <class UdpClass, class Settings, class Platform>
void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronization()
{
#ifndef ONE_PARTICIPANT
for (size_t i = 0; i < participants.size(); i++)
for (size_t i = 0; i < participants.size();)
#endif
{
#ifndef ONE_PARTICIPANT
auto pParticipant = &participants[i];
if (pParticipant->ssrc == 0) continue;
if (pParticipant->ssrc == 0)
{
i++;
continue;
}
#else
auto pParticipant = &participant;
if (pParticipant->ssrc == 0) return;
#endif
#ifdef APPLEMIDI_INITIATOR
if (pParticipant->invitationStatus != Connected)
{
#ifndef ONE_PARTICIPANT
i++;
#endif
continue;
}

// Only for Initiators that are Connected
if (pParticipant->kind == Listener)
Expand All @@ -772,6 +783,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronization()
sendEndSession(pParticipant);
#ifndef ONE_PARTICIPANT
participants.erase(i);
continue;
#else
participant.ssrc = 0;
#endif
Expand All @@ -783,6 +795,9 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSynchronization()
(pParticipant->synchronizing) ? manageSynchronizationInitiatorInvites(i)
: manageSynchronizationInitiatorHeartBeat(pParticipant);
}
#endif
#ifndef ONE_PARTICIPANT
i++;
#endif
}
}
Expand Down Expand Up @@ -882,7 +897,7 @@ template <class UdpClass, class Settings, class Platform>
void AppleMIDISession<UdpClass, Settings, Platform>::manageSessionInvites()
{
#ifndef ONE_PARTICIPANT
for (auto i = 0; i < participants.size(); i++)
for (auto i = 0; i < participants.size();)
#endif
{
#ifndef ONE_PARTICIPANT
Expand All @@ -893,7 +908,10 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSessionInvites()

if (pParticipant->kind == Listener)
#ifndef ONE_PARTICIPANT
{
i++;
continue;
}
#else
return;
#endif
Expand All @@ -911,7 +929,10 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSessionInvites()

if (pParticipant->invitationStatus == Connected)
#ifndef ONE_PARTICIPANT
{
i++;
continue;
}
#else
return;
#endif
Expand All @@ -930,12 +951,9 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSessionInvites()

#ifndef ONE_PARTICIPANT
participants.erase(i);
#else
participant.ssrc = 0;
#endif
#ifndef ONE_PARTICIPANT
continue;
#else
participant.ssrc = 0;
return;
#endif
}
Expand Down Expand Up @@ -964,6 +982,9 @@ void AppleMIDISession<UdpClass, Settings, Platform>::manageSessionInvites()
pParticipant->invitationStatus = AwaitingDataInvitationAccepted;
}
}
#ifndef ONE_PARTICIPANT
i++;
#endif
}
}

Expand Down Expand Up @@ -1054,7 +1075,7 @@ void AppleMIDISession<UdpClass, Settings, Platform>::sendEndSession()
participants.pop_front();
}
#else
if (participant.src != 0)
if (participant.ssrc != 0)
{
sendEndSession(&participant);
participant.ssrc = 0;
Expand Down
62 changes: 62 additions & 0 deletions src/utility/Deque.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <string.h>

BEGIN_APPLEMIDI_NAMESPACE

template<typename T, size_t Size>
Expand All @@ -25,6 +27,8 @@ class Deque {
const T & back() const;
void push_front(const T &);
void push_back(const T &);
size_t push_back(const T *, size_t);
size_t copy_out(T *, size_t) const;
void pop_front();
void pop_back();

Expand Down Expand Up @@ -124,6 +128,64 @@ void Deque<T, Size>::push_back(const T &value)
}
}

template<typename T, size_t Size>
size_t Deque<T, Size>::push_back(const T *values, size_t count)
{
if (values == nullptr || count == 0)
return 0;

const size_t available = free();
if (available == 0)
return 0;

const size_t toWrite = (count < available) ? count : available;

if (empty())
_tail = _head;

size_t first = toWrite;
if (_head + first > Size)
first = Size - _head;

memcpy(&_data[_head], values, first * sizeof(T));
_head = (_head + first) % Size;

const size_t remaining = toWrite - first;
if (remaining > 0)
{
memcpy(&_data[_head], values + first, remaining * sizeof(T));
_head = (_head + remaining) % Size;
}

return toWrite;
}

template<typename T, size_t Size>
size_t Deque<T, Size>::copy_out(T *dest, size_t count) const
{
if (dest == nullptr || count == 0)
return 0;

const size_t available = size();
if (available == 0)
return 0;

const size_t toCopy = (count < available) ? count : available;
const size_t start = (size_t)_tail;

size_t first = toCopy;
if (start + first > Size)
first = Size - start;

memcpy(dest, &_data[start], first * sizeof(T));

const size_t remaining = toCopy - first;
if (remaining > 0)
memcpy(dest + first, &_data[0], remaining * sizeof(T));

return toCopy;
}

template<typename T, size_t Size>
void Deque<T, Size>::pop_front() {
if (empty()) // if empty, do nothing.
Expand Down