Skip to content

Commit

Permalink
Add sync detail in text messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-heasly committed Apr 24, 2024
1 parent 17cb52d commit 2d79d71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
33 changes: 23 additions & 10 deletions Source/UDPEventsPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ void UDPEventsPlugin::run()
textEvent.type = 2;
textEvent.clientSeconds = *((double *)(messageBuffer + 1));
textEvent.textLength = udpNToHS(*((uint16 *)(messageBuffer + 9)));
String text = String::fromUTF8(messageBuffer + 11, textEvent.textLength);
textEvent.text = text + "@" + String(textEvent.clientSeconds);
textEvent.text = String::fromUTF8(messageBuffer + 11, textEvent.textLength);

// Enqueue this to be handled below, on the main thread, in process().
{
Expand Down Expand Up @@ -264,7 +263,6 @@ void UDPEventsPlugin::process(AudioBuffer<float> &buffer)
while (!softEventQueue.empty())
{
const SoftEvent &softEvent = softEventQueue.front();
softEventQueue.pop();
if (softEvent.type == 1)
{
// This is a TTL message.
Expand All @@ -275,18 +273,19 @@ void UDPEventsPlugin::process(AudioBuffer<float> &buffer)
if (syncComplete)
{
// The working sync has seen both a real and a soft event.
// Add it to the sync history and start a new sync going forward.
// Record it as an event, add it to the sync history, and start a new sync going forward.
addEventForSyncEstimate(workingSync);
syncEstimates.push_back(workingSync);
workingSync.clear();
}
}
else
{
// This is a soft TTL event to add to the selected stream.
// We'll add it, if we can find a previous sync estimate.
int64 sampleNumber = softSampleNumber(softEvent.clientSeconds, stream->getSampleRate());
if (sampleNumber)
{
// We'll only add it if we can find a previous sync estimate.
TTLEventPtr ttlEvent = TTLEvent::createTTLEvent(ttlChannel,
sampleNumber,
softEvent.lineNumber,
Expand All @@ -298,18 +297,22 @@ void UDPEventsPlugin::process(AudioBuffer<float> &buffer)
else if (softEvent.type == 2)
{
// This is a Text message to add to the selected stream.
// We'll add it, if we can find a previous sync estimate.
int64 sampleNumber = softSampleNumber(softEvent.clientSeconds, stream->getSampleRate());
if (sampleNumber)
{
// We'll only add it if we can find a previous sync estimate.
// For now, Open Ephys ignores the sampleNumber we pass for text events.
// But we can still do our best effort!
// Currently Open Ephys persists text events with low, per-block timing precision.
// Append high-precision timing info to the message for later reconstruction.
String messageText = softEvent.text + "@" + String(softEvent.clientSeconds) + "=" + String(sampleNumber);
TextEventPtr textEvent = TextEvent::createTextEvent(getMessageChannel(),
sampleNumber,
softEvent.text);
messageText);
addEvent(textEvent, 0);
}
}

// Pop invokes destructor of message (and allocated text!) -- so wait until we're done.
softEventQueue.pop();
}
}
}
Expand All @@ -333,6 +336,15 @@ int64 UDPEventsPlugin::softSampleNumber(double softSecs, float localSampleRate)
return 0;
}

void UDPEventsPlugin::addEventForSyncEstimate(struct SyncEstimate syncEstimate)
{
String text = "UDP Events sync on line " + String(syncLine + 1) + "@" + String(syncEstimate.syncSoftSecs) + "=" + String(syncEstimate.syncLocalSampleNumber);
TextEventPtr textEvent = TextEvent::createTextEvent(getMessageChannel(),
syncEstimate.syncLocalSampleNumber,
text);
addEvent(textEvent, 0);
}

void UDPEventsPlugin::handleTTLEvent(TTLEventPtr event)
{
if (event->getLine() == syncLine)
Expand All @@ -346,7 +358,8 @@ void UDPEventsPlugin::handleTTLEvent(TTLEventPtr event)
if (completed)
{
// The working sync has seen both a real and a soft event.
// Add it to the sync history and start a new sync going forward.
// Record it as an event, add it to the sync history, and start a new sync going forward.
addEventForSyncEstimate(workingSync);
syncEstimates.push_back(workingSync);
workingSync.clear();
}
Expand Down
3 changes: 3 additions & 0 deletions Source/UDPEventsPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class UDPEventsPlugin : public GenericProcessor, public Thread
SyncEstimate workingSync;
std::list<SyncEstimate> syncEstimates;

/** Add a text event to represent a completed sync estimate. */
void addEventForSyncEstimate(struct SyncEstimate syncEstimate);

/** Convert a soft timestamp to the nearest local sample number using the most relevant sync estimate. */
int64 softSampleNumber(double softSecs, float localSampleRate);
};
Expand Down

0 comments on commit 2d79d71

Please sign in to comment.