cleaning up

This commit is contained in:
Jade (Rose) Rowland 2024-08-17 17:26:46 -04:00
parent 73ee84a1fb
commit 117f7fa4b5
2 changed files with 11 additions and 9 deletions

View File

@ -16,7 +16,7 @@ export class NeoCyclist {
this.time_at_last_tick_message = 0; this.time_at_last_tick_message = 0;
// the clock of the worker and the audio context clock can drift apart over time // 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. // 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. // in order to schedule events consistently.
this.collator = new ClockCollator({ getTargetClockTime: getTime }); this.collator = new ClockCollator({ getTargetClockTime: getTime });
this.onToggle = onToggle; this.onToggle = onToggle;
@ -30,8 +30,9 @@ export class NeoCyclist {
const { cps, begin, end, cycle, time } = payload; const { cps, begin, end, cycle, time } = payload;
this.cps = cps; this.cps = cps;
this.cycle = cycle; this.cycle = cycle;
processHaps(begin, end, time); const currentTime = this.collator.calculateOffset(time) + time;
this.time_at_last_tick_message = this.getTime(); processHaps(begin, end, currentTime);
this.time_at_last_tick_message = currentTime;
}; };
const processHaps = (begin, end, currentTime) => { const processHaps = (begin, end, currentTime) => {
@ -43,8 +44,7 @@ export class NeoCyclist {
haps.forEach((hap) => { haps.forEach((hap) => {
if (hap.hasOnset()) { if (hap.hasOnset()) {
const timeUntilTrigger = cycleToSeconds(hap.whole.begin - this.cycle, this.cps); const timeUntilTrigger = cycleToSeconds(hap.whole.begin - this.cycle, this.cps);
const target = timeUntilTrigger + currentTime; const targetTime = timeUntilTrigger + currentTime + this.latency
const targetTime = this.collator.calculateTimestamp(currentTime, target) + this.latency;
const duration = cycleToSeconds(hap.duration, this.cps); const duration = cycleToSeconds(hap.duration, this.cps);
onTrigger?.(hap, 0, duration, this.cps, targetTime); onTrigger?.(hap, 0, duration, this.cps, targetTime);
} }

View File

@ -390,8 +390,7 @@ export class ClockCollator {
this.timeAtPrevOffsetSample = null; this.timeAtPrevOffsetSample = null;
}; };
} }
calculateOffset(currentTime) {
calculateTimestamp(currentTime, targetTime) {
const targetClockTime = this.getTargetClockTime(); const targetClockTime = this.getTargetClockTime();
const diffBetweenTimeSamples = targetClockTime - this.timeAtPrevOffsetSample; const diffBetweenTimeSamples = targetClockTime - this.timeAtPrevOffsetSample;
const newOffsetTime = targetClockTime - currentTime; const newOffsetTime = targetClockTime - currentTime;
@ -419,8 +418,11 @@ export class ClockCollator {
} }
} }
const timestamp = this.offsetTime + targetTime; return this.offsetTime;
return timestamp; }
calculateTimestamp(currentTime, targetTime) {
return this.calculateOffset(currentTime) + targetTime
} }
} }