Skip to content

Commit 4901278

Browse files
committed
Cleaned up some logic and added checks for queue
1 parent affd667 commit 4901278

2 files changed

Lines changed: 51 additions & 53 deletions

File tree

Source/Plugins/NetworkEvents/NetworkEvents.cpp

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ void NetworkEvents::restartConnection()
109109

110110
void NetworkEvents::createEventChannels()
111111
{
112-
EventChannel* chan = new EventChannel(EventChannel::TEXT, 1, MAX_MESSAGE_LENGTH, CoreServices::getGlobalSampleRate(), this);
112+
juce::int64 timestamp = CoreServices::getGlobalSampleRate();
113+
EventChannel* chan = new EventChannel(EventChannel::TEXT, 1, MAX_MESSAGE_LENGTH, timestamp, this);
113114
chan->setName("Network messages");
114115
chan->setDescription("Messages received through the network events module");
115116
chan->setIdentifier("external.network.rawData");
@@ -118,10 +119,10 @@ void NetworkEvents::createEventChannels()
118119
eventChannelArray.add(chan);
119120
messageChannel = chan;
120121

121-
EventChannel* TTLchan = new EventChannel(EventChannel::TTL, 8, 1, CoreServices::getGlobalSampleRate(), this);
122+
EventChannel* TTLchan = new EventChannel(EventChannel::TTL, 8, 1, timestamp, this);
122123
TTLchan->setName("Network Events output");
123124
TTLchan->setDescription("Triggers whenever \"TTL\" is received on the port.");
124-
TTLchan->setIdentifier("network.ttl.event");
125+
TTLchan->setIdentifier("external.network.ttl");
125126
eventChannelArray.add(TTLchan);
126127
TTLChannel = TTLchan;
127128
}
@@ -135,11 +136,11 @@ AudioProcessorEditor* NetworkEvents::createEditor ()
135136
}
136137

137138

138-
void NetworkEvents::postTimestamppedStringToMidiBuffer (const StringTS& s)
139+
void NetworkEvents::postTimestamppedStringToMidiBuffer (const StringTS& s, juce::int64 timestamp)
139140
{
140141
MetaDataValueArray md;
141142
md.add(new MetaDataValue(MetaDataDescriptor::INT64, 1, &s.timestamp));
142-
TextEventPtr event = TextEvent::createTextEvent(messageChannel, CoreServices::getGlobalTimestamp(), s.str, md);
143+
TextEventPtr event = TextEvent::createTextEvent(messageChannel, timestamp, s.str, md);
143144
addEvent(messageChannel, event, 0);
144145
}
145146

@@ -245,44 +246,50 @@ String NetworkEvents::handleSpecialMessages(const String& s)
245246
status += CoreServices::RecordNode::getExperimentNumber();
246247
return status;
247248
}
248-
else if (cmd.compareIgnoreCase("TTL") == 0)
249+
else if (cmd.compareIgnoreCase ("TTL") == 0)
249250
{
250251
// Default to chan 0 and off
251-
int currEventChannel = 0;
252+
int channel = 0;
252253
bool onOff = false;
253254
/** First set optional parameters (name/value pairs)*/
254-
if (s.contains("="))
255+
String params = s.substring(cmd.length()).trim();
256+
StringPairArray dict = parseNetworkMessage(params);
257+
258+
StringArray keys = dict.getAllKeys();
259+
for (int i = 0; i < keys.size(); ++i)
255260
{
256-
String params = s.substring(cmd.length()).trim();
257-
StringPairArray dict = parseNetworkMessage(params);
261+
String key = keys[i];
262+
int value = dict[key].getIntValue();
258263

259-
StringArray keys = dict.getAllKeys();
260-
for (int i = 0; i < keys.size(); ++i)
264+
if (key.compareIgnoreCase("Channel") == 0)
261265
{
262-
String key = keys[i];
263-
String value = dict[key];
264-
265-
if (key.compareIgnoreCase("EventChannel") == 0)
266+
//Make sure in range
267+
if (value <= 8 && value >= 0)
266268
{
267-
currEventChannel = value.getIntValue();
269+
channel = value;
268270
}
269-
else if (key.compareIgnoreCase("onOff") == 0)
271+
else
270272
{
271-
if (value.compareIgnoreCase("on") == 0)
272-
{
273-
onOff = true;
274-
}
275-
else
276-
{
277-
onOff = false;
278-
}
273+
//Throw some kind of error here
279274
}
280275
}
276+
else if (key.compareIgnoreCase("on") == 0)
281277
{
282-
ScopedLock TTLlock(TTLqueueLock);
283-
TTLQueue.push({ onOff, currEventChannel });
278+
onOff = value;
284279
}
285280
}
281+
{
282+
ScopedLock TTLlock(TTLqueueLock);
283+
if (CoreServices::getAcquisitionStatus())
284+
{
285+
TTLQueue.push({ onOff, channel });
286+
}
287+
else
288+
{
289+
//Throw some error
290+
}
291+
}
292+
286293

287294

288295
return "TTL Handled";
@@ -291,45 +298,36 @@ String NetworkEvents::handleSpecialMessages(const String& s)
291298
return String ("NotHandled");
292299
}
293300

294-
void NetworkEvents::triggerEvent(StringTTL TTLmsg)
301+
void NetworkEvents::triggerEvent(StringTTL TTLmsg, juce::int64 timestamp)
295302
{
296-
if (TTLmsg.onOff)
297-
{
298-
juce::uint8 ttlDataOn = 1 << TTLmsg.eventChannel;
299-
int currEventChannel = TTLmsg.eventChannel;
300-
TTLEventPtr eventOn = TTLEvent::createTTLEvent(TTLChannel, CoreServices::getGlobalTimestamp(), &ttlDataOn, sizeof(juce::uint8), currEventChannel);
301-
std::cout << "adding true event from ne" << std::endl;
302-
addEvent(TTLChannel, eventOn, 0);
303-
}
304-
else
305-
{
306-
juce::uint8 ttlDataOff = 0;
307-
int currEventChannel = TTLmsg.eventChannel;
308-
TTLEventPtr eventOff = TTLEvent::createTTLEvent(TTLChannel, CoreServices::getGlobalTimestamp(), &ttlDataOff, sizeof(juce::uint8), currEventChannel);
309-
std::cout << "adding false event from ne" << std::endl;
310-
addEvent(TTLChannel, eventOff, 0 );
311-
}
303+
juce::uint8 ttlData = TTLmsg.onOff << TTLmsg.eventChannel;
304+
TTLEventPtr event = TTLEvent::createTTLEvent(TTLChannel, timestamp, &ttlData, sizeof(juce::uint8), TTLmsg.eventChannel);
305+
std::cout << "adding event from ne" << std::endl;
306+
addEvent(TTLChannel, event, 0);
312307
}
313308

314309

315310
void NetworkEvents::process (AudioSampleBuffer& buffer)
316311
{
317312
setTimestampAndSamples(CoreServices::getGlobalTimestamp(),0);
313+
juce::int64 timestamp = CoreServices::getGlobalTimestamp();
318314

319315
ScopedLock lock(queueLock);
320316
while (! networkMessagesQueue.empty())
321317
{
322318
const StringTS& msg = networkMessagesQueue.front();
323-
postTimestamppedStringToMidiBuffer (msg);
319+
postTimestamppedStringToMidiBuffer (msg, timestamp);
324320
networkMessagesQueue.pop();
325321
}
326322

327-
ScopedLock TTLlock(TTLqueueLock);
328-
while (!TTLQueue.empty())
329323
{
330-
const StringTTL& TTLmsg = TTLQueue.front();
331-
triggerEvent(TTLmsg);
332-
TTLQueue.pop();
324+
ScopedLock TTLlock(TTLqueueLock);
325+
while (!TTLQueue.empty())
326+
{
327+
const StringTTL& TTLmsg = TTLQueue.front();
328+
triggerEvent(TTLmsg, timestamp);
329+
TTLQueue.pop();
330+
}
333331
}
334332
}
335333

Source/Plugins/NetworkEvents/NetworkEvents.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class NetworkEvents : public GenericProcessor
152152
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Responder);
153153
};
154154

155-
void postTimestamppedStringToMidiBuffer(const StringTS& s);
155+
void postTimestamppedStringToMidiBuffer(const StringTS& s, juce::int64 timestamp);
156156

157157
String handleSpecialMessages(const String& s);
158158

@@ -191,7 +191,7 @@ class NetworkEvents : public GenericProcessor
191191
const EventChannel* messageChannel{ nullptr };
192192
const EventChannel* TTLChannel{ nullptr };
193193

194-
void triggerEvent(StringTTL TTLmsg);
194+
void triggerEvent(StringTTL TTLmsg, juce::int64 timestamp);
195195

196196
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NetworkEvents);
197197
};

0 commit comments

Comments
 (0)