scheduler error correction via getTime
This commit is contained in:
parent
e72c47869a
commit
2172d1a767
2 changed files with 12 additions and 3 deletions
|
|
@ -12,7 +12,7 @@ const createWorker = (func) => new Worker(urlifyFunction(func));
|
||||||
// this is just a setInterval with a counter, running in a worker
|
// this is just a setInterval with a counter, running in a worker
|
||||||
export class ClockWorker {
|
export class ClockWorker {
|
||||||
worker;
|
worker;
|
||||||
interval = 0.1; // query span
|
interval = 1 / 32; // query span, use powers of 2 to get better float accuracy
|
||||||
tick = 0;
|
tick = 0;
|
||||||
constructor(callback, interval = this.interval) {
|
constructor(callback, interval = this.interval) {
|
||||||
this.interval = interval;
|
this.interval = interval;
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,18 @@ export class Scheduler {
|
||||||
started = false;
|
started = false;
|
||||||
phase = 0;
|
phase = 0;
|
||||||
cps = 1;
|
cps = 1;
|
||||||
constructor({ interval = 0.1, onTrigger, onError, latency = 0.1 }) {
|
lastTime;
|
||||||
|
constructor({ interval, onTrigger, onError, latency = 0.1, getTime }) {
|
||||||
this.worker = new ClockWorker((_, interval) => {
|
this.worker = new ClockWorker((_, interval) => {
|
||||||
try {
|
try {
|
||||||
|
let error = 0;
|
||||||
|
// measure time between last and current callback and calculate deviation from extected interval
|
||||||
|
const time = getTime?.();
|
||||||
|
if (time && this.lastTime) {
|
||||||
|
error = time - this.lastTime - interval; // how off is the callback? positive = too late
|
||||||
|
// console.log('ms error', error * 1000);
|
||||||
|
}
|
||||||
|
this.lastTime = time;
|
||||||
const begin = this.phase;
|
const begin = this.phase;
|
||||||
const end = this.phase + interval * this.cps;
|
const end = this.phase + interval * this.cps;
|
||||||
this.phase = end;
|
this.phase = end;
|
||||||
|
|
@ -26,7 +35,7 @@ export class Scheduler {
|
||||||
if (!hap.part.begin.equals(hap.whole.begin)) {
|
if (!hap.part.begin.equals(hap.whole.begin)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const deadline = (hap.whole.begin - begin) / this.cps + latency;
|
const deadline = (hap.whole.begin - begin) / this.cps + latency - error;
|
||||||
// TODO: use legato / duration of objectified value
|
// TODO: use legato / duration of objectified value
|
||||||
const duration = hap.duration / this.cps;
|
const duration = hap.duration / this.cps;
|
||||||
onTrigger?.(hap, deadline, duration);
|
onTrigger?.(hap, deadline, duration);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue