From 117f7fa4b58ef9fb598cd7034c096cf4e43b27b5 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sat, 17 Aug 2024 17:26:46 -0400 Subject: [PATCH] cleaning up --- packages/core/neocyclist.mjs | 10 +++++----- packages/core/util.mjs | 10 ++++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index 8d5fadd2..fe4cb0e2 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -16,7 +16,7 @@ export class NeoCyclist { this.time_at_last_tick_message = 0; // the clock of the worker and the audio context clock can drift apart over time // aditionally, the message time of the worker pinging the callback to process haps can be inconsistent. - // we need to keep a rolling weighted average of the time difference between the worker clock and audio context clock + // we need to keep a rolling average of the time difference between the worker clock and audio context clock // in order to schedule events consistently. this.collator = new ClockCollator({ getTargetClockTime: getTime }); this.onToggle = onToggle; @@ -30,8 +30,9 @@ export class NeoCyclist { const { cps, begin, end, cycle, time } = payload; this.cps = cps; this.cycle = cycle; - processHaps(begin, end, time); - this.time_at_last_tick_message = this.getTime(); + const currentTime = this.collator.calculateOffset(time) + time; + processHaps(begin, end, currentTime); + this.time_at_last_tick_message = currentTime; }; const processHaps = (begin, end, currentTime) => { @@ -43,8 +44,7 @@ export class NeoCyclist { haps.forEach((hap) => { if (hap.hasOnset()) { const timeUntilTrigger = cycleToSeconds(hap.whole.begin - this.cycle, this.cps); - const target = timeUntilTrigger + currentTime; - const targetTime = this.collator.calculateTimestamp(currentTime, target) + this.latency; + const targetTime = timeUntilTrigger + currentTime + this.latency const duration = cycleToSeconds(hap.duration, this.cps); onTrigger?.(hap, 0, duration, this.cps, targetTime); } diff --git a/packages/core/util.mjs b/packages/core/util.mjs index 08bcdbce..f41128bc 100644 --- a/packages/core/util.mjs +++ b/packages/core/util.mjs @@ -390,8 +390,7 @@ export class ClockCollator { this.timeAtPrevOffsetSample = null; }; } - - calculateTimestamp(currentTime, targetTime) { + calculateOffset(currentTime) { const targetClockTime = this.getTargetClockTime(); const diffBetweenTimeSamples = targetClockTime - this.timeAtPrevOffsetSample; const newOffsetTime = targetClockTime - currentTime; @@ -419,8 +418,11 @@ export class ClockCollator { } } - const timestamp = this.offsetTime + targetTime; - return timestamp; + return this.offsetTime; + } + + calculateTimestamp(currentTime, targetTime) { + return this.calculateOffset(currentTime) + targetTime } }