Skip to content

Commit c96e39c

Browse files
committed
Ensure synchronizer can follow non-consecutive Harp timestamps
1 parent 99b4afe commit c96e39c

2 files changed

Lines changed: 62 additions & 22 deletions

File tree

Source/Processors/Synchronizer/Synchronizer.cpp

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ bool HarpDecoder::decodeBarcode(HarpBarcode& barcode, double expectedSampleRate)
5858
}
5959
}
6060

61-
for (auto bit : bits)
62-
std::cout << bit ? 1 : 0;
63-
std::cout << std::endl;
61+
//for (auto bit : bits)
62+
// std::cout << bit ? 1 : 0;
63+
//std::cout << std::endl;
6464

6565
LOGD ("Bits decoded: ", bitIndex);
6666

@@ -117,13 +117,21 @@ bool HarpDecoder::validateBitTiming(const HarpBarcode& barcode)
117117
LOGD ("Validating bit timing.");
118118
// Check overall duration
119119
if (std::abs(barcode.actualDuration - EXPECTED_BARCODE_DURATION_MS) > 5.0)
120+
{
121+
LOGD ("Overall duration more than 5 ms off.");
120122
return false;
123+
}
124+
121125

122126
// Check individual bit durations
123127
for (int i = 0; i < TOTAL_BITS; i++)
124128
{
125129
if (std::abs(barcode.bitDurations[i] - EXPECTED_BIT_DURATION_MS) > BIT_TOLERANCE_MS)
130+
{
131+
LOGD ("Failed tolerance for bit ", i);
126132
return false;
133+
}
134+
127135
}
128136

129137
LOGD ("OK.");
@@ -189,6 +197,7 @@ void SyncStream::reset (String mainStreamKey)
189197
lastEventState = true;
190198
consecutiveValidBarcodes = 0;
191199
expectedNextStartSample = -1;
200+
numDecodingAttempts = 0;
192201
isHarpStream = false;
193202
harpDetectionActive = true;
194203
baselineMatchingBarcode = HarpBarcode();
@@ -281,6 +290,7 @@ double SyncStream::getSyncAccuracy()
281290
LOGD ("latestGlobalSyncTime: ", latestGlobalSyncTime);
282291
LOGD ("globalStartTime: ", globalStartTime);
283292
LOGD ("actualSampleRate: ", actualSampleRate);
293+
LOGD ("baselineMatchingPulse.globalTimestamp: ", baselineMatchingPulse.globalTimestamp);
284294

285295
// NEW CALCULATION:
286296
double estimatedGlobalTime = double(latestSyncSampleNumber - baselineMatchingPulse.localSampleNumber)
@@ -492,6 +502,8 @@ bool SyncStream::comparePulses(const SyncPulse& pulse1, const SyncPulse& pulse2)
492502
void SyncStream::processHarpEvent(int64 sampleNumber, bool state)
493503
{
494504

505+
//LOGD ("currentBarcode.barcodeEvents.size: ", currentBarcode.barcodeEvents.size());
506+
495507
// if more than 0.5 s has elapsed since last event, log previous barcode
496508
if (currentBarcode.barcodeEvents.size() > 0)
497509
{
@@ -505,6 +517,8 @@ void SyncStream::processHarpEvent(int64 sampleNumber, bool state)
505517
}
506518
}
507519

520+
//LOGD ("currentBarcode.localStartSample: ", (currentBarcode.localStartSample));
521+
508522
if (currentBarcode.localStartSample == 0)
509523
{
510524
LOGD ("STARTING NEW BARCODE COLLECTION.");
@@ -546,23 +560,20 @@ bool SyncStream::validateBarcodeStructure(const HarpBarcode& barcode)
546560

547561
bool SyncStream::validateBarcodeTimestamp(const HarpBarcode& barcode)
548562
{
549-
// Check timestamp is reasonable (not too far from system time)
550-
int64 systemTimeSeconds = Time::currentTimeMillis() / 1000;
551-
int64 decodedTime = barcode.encodedTime;
552-
553-
if (std::abs(int64(decodedTime) - systemTimeSeconds) > HarpDecoder::MAX_REASONABLE_TIME_DIFF_S)
554-
{
555-
return false;
556-
}
557-
563+
558564
// Check monotonic increase if we have previous barcodes
559-
if (!completedBarcodes.empty())
565+
if (completedBarcodes.size() >= 2)
560566
{
561-
auto& lastBarcode = completedBarcodes.back();
567+
auto& lastBarcode = completedBarcodes[completedBarcodes.size() - 2];
562568
if (barcode.encodedTime != lastBarcode.encodedTime + 1)
563569
{
570+
LOGD ("Non-monotonic increase");
564571
return false; // Should increment by 1 second
565572
}
573+
else
574+
{
575+
LOGD ("Expected barcode found!");
576+
}
566577
}
567578

568579
return true;
@@ -581,20 +592,43 @@ void SyncStream::attemptBarcodeDecoding()
581592
if (completedBarcodes.size() == 0)
582593
{
583594
LOGD (" No completed barcodes yet.");
595+
numDecodingAttempts += 1;
584596
return;
585597
}
598+
else
599+
{
600+
LOGD (" Completed barcodes: ", completedBarcodes.size());
601+
}
586602

587603
// decode last barcode
588604
if (harpDecoder.decodeBarcode(completedBarcodes.back(), expectedSampleRate))
589605
{
590606
LOGD ("Successful decoding...setting isHarpStream to true.");
591607
isHarpStream = true;
592608
isSynchronized = true;
609+
610+
611+
// Validate timing
612+
if (!validateBarcodeTimestamp(completedBarcodes.back()))
613+
{
614+
LOGD ("Non-consecutive barcodes, clearing completed barcodes");
615+
completedBarcodes.clear();
616+
}
593617
}
594618
else
595619
{
596-
LOGD ("Unsuccessful decoding...setting harpDetectionActive to false.")
597-
harpDetectionActive = false;
620+
LOGD ("Unsuccessful decoding");
621+
622+
if (completedBarcodes.size() == 0)
623+
{
624+
LOGD ("No completed barcodes, setting Harp detection to false");
625+
harpDetectionActive = false;
626+
}
627+
else
628+
{
629+
LOGD ("Previous completed barcodes, keeping Harp detection active");
630+
}
631+
598632
}
599633
}
600634

@@ -746,8 +780,8 @@ void Synchronizer::addEvent (String streamKey,
746780

747781
const ScopedLock sl (synchronizerLock);
748782

749-
if (streamCount == 1 || sampleNumber < 1000)
750-
return;
783+
//if (streamCount == 1 || sampleNumber < 1000)
784+
// return;
751785

752786
if (streams[streamKey]->syncLine == ttlLine)
753787
{
@@ -761,9 +795,10 @@ double Synchronizer::convertSampleNumberToTimestamp (String streamKey, int64 sam
761795
{
762796

763797
// Use standard pulse baseline for conversion
764-
return double(sampleNumber - streams[streamKey]->baselineMatchingPulse.localSampleNumber)
765-
/ streams[streamKey]->actualSampleRate
766-
+ streams[streamKey]->baselineMatchingPulse.globalTimestamp;
798+
return double (sampleNumber - streams[streamKey]->baselineMatchingPulse.localSampleNumber)
799+
/ streams[streamKey]->actualSampleRate
800+
+ streams[streamKey]->baselineMatchingPulse.globalTimestamp;
801+
;
767802
}
768803
else
769804
{
@@ -867,7 +902,8 @@ void Synchronizer::hiResTimerCallback()
867902
{
868903
if (stream->harpDetectionActive)
869904
{
870-
stream->attemptBarcodeDecoding();
905+
if (stream->numDecodingAttempts < 5)
906+
stream->attemptBarcodeDecoding();
871907
}
872908
}
873909

Source/Processors/Synchronizer/Synchronizer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ class SyncStream
233233

234234
/** true if Harp detection is currently active */
235235
bool harpDetectionActive = true;
236+
237+
/** Number of Harp barcode decoding attempts made */
238+
int numDecodingAttempts = 0;
236239

237240
/** Baseline matching barcode for Harp synchronization */
238241
HarpBarcode baselineMatchingBarcode;
@@ -291,6 +294,7 @@ class SyncStream
291294
int64 barcodeStartTime = -1; // System time when current barcode collection started
292295
int64 lastEventSample = -1;
293296
bool lastEventState = true; // Default HIGH
297+
294298

295299
// Detection state
296300
int consecutiveValidBarcodes = 0;

0 commit comments

Comments
 (0)