scheduler is now finally tight

This commit is contained in:
Felix Roos 2022-08-23 17:55:10 +02:00
parent ec60fe42be
commit 9059c1b850
2 changed files with 23 additions and 21 deletions

View File

@ -6,7 +6,6 @@ This program is free software: you can redistribute it and/or modify it under th
import { ClockWorker } from './clockworker.mjs'; import { ClockWorker } from './clockworker.mjs';
// TODO: make pause work with origin.
// find out why setPattern takes so long // find out why setPattern takes so long
// reimplement setCps // reimplement setCps
@ -14,28 +13,30 @@ export class Scheduler {
worker; worker;
pattern; pattern;
started = false; started = false;
origin;
phase = 0; phase = 0;
cps = 1; cps = 1;
getTime; getTime;
constructor({ interval, onTrigger, onError, latency = 0.1, getTime }) { lastTime;
constructor({ interval, onTrigger, onError, latency = 0.2, getTime }) {
this.worker = new ClockWorker((_, interval) => { this.worker = new ClockWorker((_, interval) => {
try { try {
// first, calculate queryArc, where // goals:
// - first query should start from zero // - first query should start from zero
// - next query must start where the other left off // - next query must start where the other left off
// - queries must be synced to the interval clock => no drifting // - queries must be synced to the interval clock => no drifting
const begin = this.phase; // begin where we left off last time || 0
const time = getTime(); const time = getTime();
this.origin = this.origin ?? time; // capture timestamp of first tick if (!this.lastTime) {
const runTime = time - this.origin; // how long the scheduler is running since start this.lastTime = time;
const cps = this.cps; // remember cps used to calculate the current slice return;
// TODO: find a way to implement cps without jumps.. }
const end = (runTime + interval) * cps; const passed = time - this.lastTime; // how much time passed since the last callback?
// console.log('runTime', runTime); this.lastTime = time;
const begin = this.phase; // begin where we left off last time || 0
const end = this.phase + passed * this.cps;
this.phase = end; // remember where we left off fro next query this.phase = end; // remember where we left off fro next query
const haps = this.pattern.queryArc(begin, end); // get haps const haps = this.pattern.queryArc(begin, end); // get haps
const t = getTime(); // need new timestamp after query (can take a few ms) // const t = getTime(); // need new timestamp after query (can take a few ms)
// schedule each hap // schedule each hap
haps.forEach((hap) => { haps.forEach((hap) => {
if (typeof hap.value?.cps === 'number') { if (typeof hap.value?.cps === 'number') {
@ -45,16 +46,17 @@ export class Scheduler {
if (!hap.part.begin.equals(hap.whole.begin)) { if (!hap.part.begin.equals(hap.whole.begin)) {
return; return;
} }
// calculate absolute time for this hap, .whole.begin is relative to 0, so we need to add the origin const scheduled = time + (hap.whole.begin - begin) / this.cps + latency - passed; // this took me ages...
const scheduledTime = hap.whole.begin / cps + latency + this.origin; const duration = hap.duration / this.cps;
// deadline = time in s until the event should be scheduled const now = getTime();
const deadline = scheduledTime - t; const deadline = scheduled - now;
if (deadline < 0) { // TODO: could still use a deadline based approach by measuring the time it takes for the query
if (scheduled < now) {
console.warn(`deadline ${deadline.toFixed(2)} is below zero! latency ${latency}s, interval ${interval}s`); console.warn(`deadline ${deadline.toFixed(2)} is below zero! latency ${latency}s, interval ${interval}s`);
return; return;
} }
// TODO: use legato / duration of objectified value // TODO: use legato / duration of objectified value
const duration = hap.duration / this.cps;
onTrigger?.(hap, deadline, duration); onTrigger?.(hap, deadline, duration);
}); });
} catch (err) { } catch (err) {
@ -75,13 +77,13 @@ export class Scheduler {
console.log('pause'); console.log('pause');
this.worker.stop(); this.worker.stop();
this.phase = 0; this.phase = 0;
delete this.origin; delete this.lastTime;
this.started = false; this.started = false;
} }
stop() { stop() {
console.log('stop'); console.log('stop');
this.phase = 0; this.phase = 0;
delete this.origin; delete this.lastTime;
this.worker.stop(); this.worker.stop();
this.started = false; this.started = false;
} }

View File

@ -62,7 +62,7 @@ stack(
.echoWith(4,.125,(x,n)=>x.gain(.15*1/(n+1))) // echo notes .echoWith(4,.125,(x,n)=>x.gain(.15*1/(n+1))) // echo notes
//.hush() //.hush()
) )
.fast(2/3)`; .cps(2/3)`;
// await prebake(); // await prebake();