Skip to content

Commit b1c686a

Browse files
authored
Merge pull request #1001 from penfold42/udpfixes
UDP midi fixes, MIDIThru2, MIDIThruBlockSpam
2 parents bc7803b + d55bbdf commit b1c686a

File tree

6 files changed

+156
-26
lines changed

6 files changed

+156
-26
lines changed

src/config.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,29 @@ void CConfig::Load (void)
103103
}
104104
}
105105

106+
const char *pMIDIThru2 = m_Properties.GetString ("MIDIThru2");
107+
if (pMIDIThru2)
108+
{
109+
std::string Arg (pMIDIThru2);
110+
111+
size_t nPos = Arg.find (',');
112+
if (nPos != std::string::npos)
113+
{
114+
m_MIDIThru2In = Arg.substr (0, nPos);
115+
m_MIDIThru2Out = Arg.substr (nPos+1);
116+
117+
if ( m_MIDIThru2In.empty ()
118+
|| m_MIDIThru2Out.empty ())
119+
{
120+
m_MIDIThru2In.clear ();
121+
m_MIDIThru2Out.clear ();
122+
}
123+
}
124+
}
125+
126+
m_bMIDIThruIgnoreClock = m_Properties.GetNumber ("MIDIThruIgnoreClock", 0) != 0;
127+
m_bMIDIThruIgnoreActiveSensing = m_Properties.GetNumber ("MIDIThruIgnoreActiveSensing", 0) != 0;
128+
106129
m_bMIDIRXProgramChange = m_Properties.GetNumber ("MIDIRXProgramChange", 1) != 0;
107130
m_bIgnoreAllNotesOff = m_Properties.GetNumber ("IgnoreAllNotesOff", 0) != 0;
108131
m_bMIDIAutoVoiceDumpOnPC = m_Properties.GetNumber ("MIDIAutoVoiceDumpOnPC", 0) != 0;
@@ -213,6 +236,8 @@ void CConfig::Load (void)
213236
if (const u8 *pIP = m_Properties.GetIPAddress("NetworkDNSServer")) m_INetworkDNSServer.Set (pIP);
214237
m_bNetworkFTPEnabled = m_Properties.GetNumber("NetworkFTPEnabled", 0) != 0;
215238
if (const u8 *pIP = m_Properties.GetIPAddress ("NetworkSyslogServerIPAddress")) m_INetworkSyslogServerIPAddress.Set (pIP);
239+
m_bUDPMIDIEnabled = m_Properties.GetNumber("UDPMIDIEnabled", 0) != 0;
240+
if (const u8 *pIP = m_Properties.GetIPAddress("UDPMIDIIPAddress")) m_IUDPMIDIIPAddress.Set (pIP);
216241

217242
m_nMasterVolume = m_Properties.GetNumber ("MasterVolume", 64);
218243
}
@@ -329,6 +354,26 @@ const char *CConfig::GetMIDIThruOut (void) const
329354
return m_MIDIThruOut.c_str ();
330355
}
331356

357+
const char *CConfig::GetMIDIThru2In (void) const
358+
{
359+
return m_MIDIThru2In.c_str ();
360+
}
361+
362+
const char *CConfig::GetMIDIThru2Out (void) const
363+
{
364+
return m_MIDIThru2Out.c_str ();
365+
}
366+
367+
bool CConfig::GetMIDIThruIgnoreClock (void) const
368+
{
369+
return m_bMIDIThruIgnoreClock;
370+
}
371+
372+
bool CConfig::GetMIDIThruIgnoreActiveSensing (void) const
373+
{
374+
return m_bMIDIThruIgnoreActiveSensing;
375+
}
376+
332377
bool CConfig::GetMIDIRXProgramChange (void) const
333378
{
334379
return m_bMIDIRXProgramChange;
@@ -793,3 +838,13 @@ bool CConfig::GetNetworkFTPEnabled (void) const
793838
{
794839
return m_bNetworkFTPEnabled;
795840
}
841+
842+
bool CConfig::GetUDPMIDIEnabled (void) const
843+
{
844+
return m_bUDPMIDIEnabled;
845+
}
846+
847+
const CIPAddress& CConfig::GetUDPMIDIIPAddress (void) const
848+
{
849+
return m_IUDPMIDIIPAddress;
850+
}

src/config.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ class CConfig // Configuration for MiniDexed
124124
unsigned GetMIDIBaudRate (void) const;
125125
const char *GetMIDIThruIn (void) const; // "" if not specified
126126
const char *GetMIDIThruOut (void) const; // "" if not specified
127+
const char *GetMIDIThru2In (void) const; // "" if not specified
128+
const char *GetMIDIThru2Out (void) const; // "" if not specified
129+
bool GetMIDIThruIgnoreClock (void) const; // false if not specified
130+
bool GetMIDIThruIgnoreActiveSensing (void) const; // false if not specified
127131
bool GetMIDIRXProgramChange (void) const; // true if not specified
128132
bool GetIgnoreAllNotesOff (void) const;
129133
bool GetMIDIAutoVoiceDumpOnPC (void) const; // false if not specified
@@ -254,6 +258,8 @@ class CConfig // Configuration for MiniDexed
254258
bool GetSyslogEnabled (void) const;
255259
const CIPAddress& GetNetworkSyslogServerIPAddress (void) const;
256260
bool GetNetworkFTPEnabled (void) const;
261+
bool GetUDPMIDIEnabled (void) const;
262+
const CIPAddress& GetUDPMIDIIPAddress (void) const;
257263

258264
private:
259265
CPropertiesFatFsFile m_Properties;
@@ -276,6 +282,10 @@ class CConfig // Configuration for MiniDexed
276282
unsigned m_nMIDIBaudRate;
277283
std::string m_MIDIThruIn;
278284
std::string m_MIDIThruOut;
285+
std::string m_MIDIThru2In;
286+
std::string m_MIDIThru2Out;
287+
bool m_bMIDIThruIgnoreClock;
288+
bool m_bMIDIThruIgnoreActiveSensing;
279289
bool m_bMIDIRXProgramChange;
280290
bool m_bIgnoreAllNotesOff;
281291
bool m_bMIDIAutoVoiceDumpOnPC;
@@ -384,6 +394,8 @@ class CConfig // Configuration for MiniDexed
384394
bool m_bSyslogEnabled;
385395
CIPAddress m_INetworkSyslogServerIPAddress;
386396
bool m_bNetworkFTPEnabled;
397+
bool m_bUDPMIDIEnabled;
398+
CIPAddress m_IUDPMIDIIPAddress;
387399
};
388400

389401
#endif

src/mididevice.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,42 @@ void CMIDIDevice::MIDIMessageHandler (const u8 *pMessage, size_t nLength, unsign
190190
*/
191191

192192
// Handle MIDI Thru
193-
if (m_DeviceName.compare (m_pConfig->GetMIDIThruIn ()) == 0)
193+
bool canThru = TRUE;
194+
if (nLength == 1)
194195
{
195-
TDeviceMap::const_iterator Iterator;
196+
if ((pMessage[0] == MIDI_TIMING_CLOCK) && m_pConfig->GetMIDIThruIgnoreClock())
197+
{
198+
canThru = FALSE;
199+
}
200+
if ((pMessage[0] == MIDI_ACTIVE_SENSING) && m_pConfig->GetMIDIThruIgnoreActiveSensing())
201+
{
202+
canThru = FALSE;
203+
}
204+
}
196205

197-
Iterator = s_DeviceMap.find (m_pConfig->GetMIDIThruOut ());
198-
if (Iterator != s_DeviceMap.end ())
206+
if (canThru)
207+
{
208+
if (m_DeviceName.compare (m_pConfig->GetMIDIThruIn ()) == 0)
199209
{
200-
Iterator->second->Send (pMessage, nLength, nCable);
210+
TDeviceMap::const_iterator Iterator;
211+
212+
Iterator = s_DeviceMap.find (m_pConfig->GetMIDIThruOut ());
213+
if (Iterator != s_DeviceMap.end ())
214+
{
215+
Iterator->second->Send (pMessage, nLength, nCable);
216+
}
217+
}
218+
219+
// Handle MIDI Thru 2
220+
if (m_DeviceName.compare (m_pConfig->GetMIDIThru2In ()) == 0)
221+
{
222+
TDeviceMap::const_iterator Iterator;
223+
224+
Iterator = s_DeviceMap.find (m_pConfig->GetMIDIThru2Out ());
225+
if (Iterator != s_DeviceMap.end ())
226+
{
227+
Iterator->second->Send (pMessage, nLength, nCable);
228+
}
201229
}
202230
}
203231

src/net/applemidi.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,8 @@ bool CAppleMIDIParticipant::SendMIDIToHost(const u8* pData, size_t nSize)
921921
return false;
922922
}
923923

924-
LOGDBG("Successfully sent %zu bytes of MIDI data", nSize);
924+
#ifdef APPLEMIDI_DEBUG
925+
LOGDBG("Successfully sent %u bytes of MIDI data", nSize);
926+
#endif
925927
return true;
926-
}
928+
}

src/udpmididevice.cpp

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,47 @@ boolean CUDPMIDIDevice::Initialize (void)
5757
}
5858
else
5959
LOGNOTE("RTP Listener initialized");
60-
m_pUDPMIDIReceiver = new CUDPMIDIReceiver(this);
61-
if (!m_pUDPMIDIReceiver->Initialize())
60+
61+
if (m_pConfig->GetUDPMIDIEnabled())
6262
{
63-
LOGERR("Failed to init UDP MIDI receiver");
64-
delete m_pUDPMIDIReceiver;
65-
m_pUDPMIDIReceiver = nullptr;
63+
m_pUDPMIDIReceiver = new CUDPMIDIReceiver(this);
64+
if (!m_pUDPMIDIReceiver->Initialize())
65+
{
66+
LOGERR("Failed to init UDP MIDI receiver");
67+
delete m_pUDPMIDIReceiver;
68+
m_pUDPMIDIReceiver = nullptr;
69+
}
70+
else
71+
LOGNOTE("UDP MIDI receiver initialized");
72+
73+
// UDP MIDI send socket setup (default: broadcast 255.255.255.255:1999)
74+
m_UDPDestAddress.Set(0xFFFFFFFF); // Broadcast by default
75+
m_UDPDestPort = 1999;
76+
if (m_pConfig->GetUDPMIDIIPAddress().IsSet())
77+
{
78+
m_UDPDestAddress.Set( m_pConfig->GetUDPMIDIIPAddress() );
79+
}
80+
CString IPAddressString;
81+
m_UDPDestAddress.Format(&IPAddressString);
82+
83+
// address 0.0.0.0 disables transmit
84+
if (!m_UDPDestAddress.IsNull())
85+
{
86+
CNetSubSystem* pNet = CNetSubSystem::Get();
87+
m_pUDPSendSocket = new CSocket(pNet, IPPROTO_UDP);
88+
m_pUDPSendSocket->Connect(m_UDPDestAddress, m_UDPDestPort);
89+
m_pUDPSendSocket->SetOptionBroadcast(TRUE);
90+
91+
LOGNOTE("UDP MIDI sender initialized. target is %s",
92+
(const char*)IPAddressString);
93+
}
94+
else
95+
LOGNOTE("UDP MIDI sender disabled. target was %s",
96+
(const char*)IPAddressString);
97+
6698
}
6799
else
68-
LOGNOTE("UDP MIDI receiver initialized");
69-
70-
// UDP MIDI send socket setup (default: broadcast 255.255.255.255:1999)
71-
CNetSubSystem* pNet = CNetSubSystem::Get();
72-
m_pUDPSendSocket = new CSocket(pNet, IPPROTO_UDP);
73-
m_UDPDestAddress.Set(0xFFFFFFFF); // Broadcast by default
74-
m_UDPDestPort = 1999;
100+
LOGNOTE("UDP MIDI is disabled in configuration");
75101

76102
return true;
77103
}
@@ -85,11 +111,13 @@ void CUDPMIDIDevice::OnAppleMIDIDataReceived(const u8* pData, size_t nSize)
85111

86112
void CUDPMIDIDevice::OnAppleMIDIConnect(const CIPAddress* pIPAddress, const char* pName)
87113
{
114+
m_bIsAppleMIDIConnected = true;
88115
LOGNOTE("RTP Device connected");
89116
}
90117

91118
void CUDPMIDIDevice::OnAppleMIDIDisconnect(const CIPAddress* pIPAddress, const char* pName)
92119
{
120+
m_bIsAppleMIDIConnected = false;
93121
LOGNOTE("RTP Device disconnected");
94122
}
95123

@@ -100,17 +128,21 @@ void CUDPMIDIDevice::OnUDPMIDIDataReceived(const u8* pData, size_t nSize)
100128

101129
void CUDPMIDIDevice::Send(const u8 *pMessage, size_t nLength, unsigned nCable)
102130
{
103-
bool sentRTP = false;
104-
if (m_pAppleMIDIParticipant && m_pAppleMIDIParticipant->SendMIDIToHost(pMessage, nLength)) {
105-
sentRTP = true;
106-
LOGNOTE("Sent %zu bytes to RTP-MIDI host", nLength);
131+
if (m_pAppleMIDIParticipant && m_bIsAppleMIDIConnected) {
132+
bool res = m_pAppleMIDIParticipant->SendMIDIToHost(pMessage, nLength);
133+
if (!res) {
134+
LOGERR("Failed to send %u bytes to RTP-MIDI host", (unsigned long) nLength);
135+
} else {
136+
// LOGDBG("Sent %u bytes to RTP-MIDI host", (unsigned long) nLength);
137+
}
107138
}
108-
if (!sentRTP && m_pUDPSendSocket) {
139+
140+
if (m_pUDPSendSocket) {
109141
int res = m_pUDPSendSocket->SendTo(pMessage, nLength, 0, m_UDPDestAddress, m_UDPDestPort);
110142
if (res < 0) {
111-
LOGERR("Failed to send %zu bytes to UDP MIDI host", nLength);
143+
LOGERR("Failed to send %u bytes to UDP MIDI host", (unsigned long) nLength);
112144
} else {
113-
LOGNOTE("Sent %zu bytes to UDP MIDI host (broadcast)", nLength);
145+
// LOGDBG("Sent %u bytes to UDP MIDI host", (unsigned long) nLength);
114146
}
115147
}
116148
}

src/udpmididevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class CUDPMIDIDevice : CAppleMIDIHandler, CUDPMIDIHandler, public CMIDIDevice
5151
CConfig *m_pConfig;
5252
CBcmRandomNumberGenerator m_Random;
5353
CAppleMIDIParticipant* m_pAppleMIDIParticipant; // AppleMIDI participant instance
54+
bool m_bIsAppleMIDIConnected = false;
5455
CUDPMIDIReceiver* m_pUDPMIDIReceiver;
5556
CSocket* m_pUDPSendSocket = nullptr;
5657
CIPAddress m_UDPDestAddress;

0 commit comments

Comments
 (0)