Skip to content
94 changes: 83 additions & 11 deletions Source/Plugins/NetworkEvents/NetworkEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,22 @@ void NetworkEvents::restartConnection()

void NetworkEvents::createEventChannels()
{
EventChannel* chan = new EventChannel(EventChannel::TEXT, 1, MAX_MESSAGE_LENGTH, CoreServices::getGlobalSampleRate(), this);
float sampleRate = CoreServices::getGlobalSampleRate();
EventChannel* chan = new EventChannel(EventChannel::TEXT, 1, MAX_MESSAGE_LENGTH, sampleRate, this);
chan->setName("Network messages");
chan->setDescription("Messages received through the network events module");
chan->setIdentifier("external.network.rawData");
chan->addEventMetaData(new MetaDataDescriptor(MetaDataDescriptor::INT64, 1, "Software timestamp",
"OS high resolution timer count when the event was received", "timestamp.software"));
eventChannelArray.add(chan);
messageChannel = chan;

EventChannel* TTLchan = new EventChannel(EventChannel::TTL, 8, 1, sampleRate, this);
TTLchan->setName("Network Events output");
TTLchan->setDescription("Triggers whenever \"TTL\" is received on the port.");
TTLchan->setIdentifier("external.network.ttl");
eventChannelArray.add(TTLchan);
TTLChannel = TTLchan;
}


Expand All @@ -102,11 +110,11 @@ AudioProcessorEditor* NetworkEvents::createEditor ()
}


void NetworkEvents::postTimestamppedStringToMidiBuffer (const StringTS& s)
void NetworkEvents::postTimestamppedStringToMidiBuffer (const StringTS& s, juce::int64 timestamp)
{
MetaDataValueArray md;
md.add(new MetaDataValue(MetaDataDescriptor::INT64, 1, &s.timestamp));
TextEventPtr event = TextEvent::createTextEvent(messageChannel, CoreServices::getGlobalTimestamp(), s.str, md);
TextEventPtr event = TextEvent::createTextEvent(messageChannel, timestamp, s.str, md);
addEvent(messageChannel, event, 0);
}

Expand Down Expand Up @@ -181,8 +189,8 @@ String NetworkEvents::handleSpecialMessages(const String& s)
if (CoreServices::getRecordingStatus())
{
CoreServices::setRecordingStatus (false);
return String ("StoppedRecording");
}
}
return String("StoppedRecording");
}
else if (cmd.compareIgnoreCase ("IsAcquiring") == 0)
{
Expand Down Expand Up @@ -212,21 +220,85 @@ String NetworkEvents::handleSpecialMessages(const String& s)
status += CoreServices::RecordNode::getExperimentNumber();
return status;
}
else if (cmd.compareIgnoreCase ("TTL") == 0)
{
// Default to channel 0 and off (if no optional info sent)
int channel = 0;
bool onOff = 0;
/** Set optional parameters (name/value pairs)*/
String params = s.substring(cmd.length()).trim();
StringPairArray dict = parseNetworkMessage(params);

StringArray keys = dict.getAllKeys();
for (int i = 0; i < keys.size(); ++i)
{
String key = keys[i];
int value = dict[key].getIntValue();

if (key.compareIgnoreCase("Channel") == 0)
{
// Make sure in range
if (value <= 8 && value >= 1)
{
channel = value - 1;
}
else
{
return "InvalidChannel";
}
}
else if (key.compareIgnoreCase("on") == 0)
{
onOff = value;
}
}
{
ScopedLock TTLlock(TTLqueueLock);
if (CoreServices::getAcquisitionStatus())
{
TTLQueue.push({ onOff, channel });
}
}



return "TTLHandled: Channel=" + String(channel + 1) + " on=" + String(onOff);
}

return String ("NotHandled");
}

void NetworkEvents::triggerTTLEvent(StringTTL TTLmsg, juce::int64 timestamp)
{
juce::uint8 ttlData = TTLmsg.onOff << TTLmsg.eventChannel;
TTLEventPtr event = TTLEvent::createTTLEvent(TTLChannel, timestamp, &ttlData, sizeof(juce::uint8), TTLmsg.eventChannel);
addEvent(TTLChannel, event, 0);
}


void NetworkEvents::process (AudioSampleBuffer& buffer)
{
setTimestampAndSamples(CoreServices::getGlobalTimestamp(),0);
juce::int64 timestamp = CoreServices::getGlobalTimestamp();
setTimestampAndSamples(timestamp,0);

ScopedLock lock(queueLock);
while (! networkMessagesQueue.empty())
{
const StringTS& msg = networkMessagesQueue.front();
postTimestamppedStringToMidiBuffer (msg);
networkMessagesQueue.pop();
ScopedLock lock(queueLock);
while (!networkMessagesQueue.empty())
{
const StringTS& msg = networkMessagesQueue.front();
postTimestamppedStringToMidiBuffer(msg, timestamp);
networkMessagesQueue.pop();
}
}

{
ScopedLock TTLlock(TTLqueueLock);
while (!TTLQueue.empty())
{
const StringTTL& TTLmsg = TTLQueue.front();
triggerTTLEvent(TTLmsg, timestamp);
TTLQueue.pop();
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion Source/Plugins/NetworkEvents/NetworkEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class NetworkEvents : public GenericProcessor
int64 timestamp;
};

struct StringTTL
{
bool onOff;
int eventChannel;
};

class ZMQContext
{
public:
Expand Down Expand Up @@ -142,7 +148,7 @@ class NetworkEvents : public GenericProcessor
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Responder);
};

void postTimestamppedStringToMidiBuffer(const StringTS& s);
void postTimestamppedStringToMidiBuffer(const StringTS& s, juce::int64 timestamp);

String handleSpecialMessages(const String& s);

Expand All @@ -164,8 +170,14 @@ class NetworkEvents : public GenericProcessor

std::queue<StringTS> networkMessagesQueue;
CriticalSection queueLock;

std::queue<StringTTL> TTLQueue;
CriticalSection TTLqueueLock;

const EventChannel* messageChannel{ nullptr };
const EventChannel* TTLChannel{ nullptr };

void triggerTTLEvent(StringTTL TTLmsg, juce::int64 timestamp);

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NetworkEvents);
};
Expand Down