From 7d2110d75fb991309add5f73cf36bcf31b110bd7 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Thu, 8 Aug 2024 20:35:12 -0400 Subject: [PATCH] clock fail strategy --- packages/desktopbridge/oscbridge.mjs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/desktopbridge/oscbridge.mjs b/packages/desktopbridge/oscbridge.mjs index cfeaaf64..91df3075 100644 --- a/packages/desktopbridge/oscbridge.mjs +++ b/packages/desktopbridge/oscbridge.mjs @@ -2,6 +2,12 @@ import { parseNumeral, Pattern, getEventOffsetMs } from '@strudel/core'; import { Invoke } from './utils.mjs'; let offsetTime; +let weight = 1; +let timeAtPrevOffsetSample; +let rollingOffsetTime; + + +// let prevTime = 0; Pattern.prototype.osc = function () { return this.onTrigger(async (time, hap, currentTime, cps = 1, targetTime) => { hap.ensureObjectValue(); @@ -14,12 +20,22 @@ Pattern.prototype.osc = function () { const params = []; // console.log(time, currentTime) + const unixTimeSecs = Date.now() / 1000; + const newOffsetTime = unixTimeSecs - currentTime; - if (offsetTime == null) { - const unixTimeSecs = Date.now() / 1000; - offsetTime = unixTimeSecs - currentTime; + if (unixTimeSecs - timeAtPrevOffsetSample > 2) { + timeAtPrevOffsetSample = unixTimeSecs; + rollingOffsetTime = newOffsetTime; } + + + //account for the js clock freezing or resetting for some reason + if (offsetTime == null || Math.abs(rollingOffsetTime - offsetTime) > .1 + ) { + offsetTime = newOffsetTime; + } + // prevTime = currentTime; const timestamp = offsetTime + targetTime // const timestamp = unixTimeSecs + (targetTime - currentTime)