Merge pull request #874 from daslyfe/clock_drift

Bug Fix #119: Clock drift
This commit is contained in:
Felix Roos 2023-12-27 18:38:09 +01:00 committed by GitHub
commit 624affe092
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,13 +11,14 @@ export class Cyclist {
constructor({ interval, onTrigger, onToggle, onError, getTime, latency = 0.1 }) { constructor({ interval, onTrigger, onToggle, onError, getTime, latency = 0.1 }) {
this.started = false; this.started = false;
this.cps = 1; this.cps = 1;
this.num_ticks_since_cps_change = 0;
this.lastTick = 0; // absolute time when last tick (clock callback) happened this.lastTick = 0; // absolute time when last tick (clock callback) happened
this.lastBegin = 0; // query begin of last tick this.lastBegin = 0; // query begin of last tick
this.lastEnd = 0; // query end of last tick this.lastEnd = 0; // query end of last tick
this.getTime = getTime; // get absolute time this.getTime = getTime; // get absolute time
this.num_cycles_since_last_cps_change = 0;
this.onToggle = onToggle; this.onToggle = onToggle;
this.latency = latency; // fixed trigger time offset this.latency = latency; // fixed trigger time offset
const round = (x) => Math.round(x * 1000) / 1000;
this.clock = createClock( this.clock = createClock(
getTime, getTime,
// called slightly before each cycle // called slightly before each cycle
@ -25,14 +26,24 @@ export class Cyclist {
if (tick === 0) { if (tick === 0) {
this.origin = phase; this.origin = phase;
} }
if (this.num_ticks_since_cps_change === 0) {
this.num_cycles_since_last_cps_change = this.lastEnd;
}
this.num_ticks_since_cps_change++;
try { try {
const time = getTime(); const time = getTime();
const begin = this.lastEnd; const begin = this.lastEnd;
this.lastBegin = begin; this.lastBegin = begin;
const end = round(begin + duration * this.cps);
//convert ticks to cycles, so you can query the pattern for events
const eventLength = duration * this.cps;
const end = this.num_cycles_since_last_cps_change + this.num_ticks_since_cps_change * eventLength;
this.lastEnd = end; this.lastEnd = end;
// query the pattern for events
const haps = this.pattern.queryArc(begin, end); const haps = this.pattern.queryArc(begin, end);
const tickdeadline = phase - time; // time left till phase begins
const tickdeadline = phase - time; // time left until the phase is a whole number
this.lastTick = time + tickdeadline; this.lastTick = time + tickdeadline;
haps.forEach((hap) => { haps.forEach((hap) => {
@ -59,6 +70,8 @@ export class Cyclist {
this.onToggle?.(v); this.onToggle?.(v);
} }
start() { start() {
this.num_ticks_since_cps_change = 0;
this.num_cycles_since_last_cps_change = 0;
if (!this.pattern) { if (!this.pattern) {
throw new Error('Scheduler: no pattern set! call .setPattern first.'); throw new Error('Scheduler: no pattern set! call .setPattern first.');
} }
@ -84,7 +97,11 @@ export class Cyclist {
} }
} }
setCps(cps = 1) { setCps(cps = 1) {
if (this.cps === cps) {
return;
}
this.cps = cps; this.cps = cps;
this.num_ticks_since_cps_change = 0;
} }
log(begin, end, haps) { log(begin, end, haps) {
const onsets = haps.filter((h) => h.hasOnset()); const onsets = haps.filter((h) => h.hasOnset());