mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-22 11:08:35 +00:00
fix scheduler again.. add comments
This commit is contained in:
parent
275931969e
commit
ec60fe42be
@ -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 = 1 / 10; // query span, use powers of 2 to get better float accuracy
|
interval = 1 / 20; // 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;
|
||||||
|
|||||||
@ -6,45 +6,52 @@ 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
|
||||||
|
// reimplement setCps
|
||||||
|
|
||||||
export class Scheduler {
|
export class Scheduler {
|
||||||
worker;
|
worker;
|
||||||
pattern;
|
pattern;
|
||||||
started = false;
|
started = false;
|
||||||
|
origin;
|
||||||
phase = 0;
|
phase = 0;
|
||||||
cps = 1;
|
cps = 1;
|
||||||
lastTime;
|
getTime;
|
||||||
error = 0;
|
|
||||||
constructor({ interval, onTrigger, onError, latency = 0.1, getTime }) {
|
constructor({ interval, onTrigger, onError, latency = 0.1, getTime }) {
|
||||||
this.worker = new ClockWorker((_, interval) => {
|
this.worker = new ClockWorker((_, interval) => {
|
||||||
try {
|
try {
|
||||||
const begin = this.phase;
|
// first, calculate queryArc, where
|
||||||
const end = this.phase + interval * this.cps;
|
// - first query should start from zero
|
||||||
this.phase = end;
|
// - next query must start where the other left off
|
||||||
const haps = this.pattern.queryArc(begin, end);
|
// - queries must be synced to the interval clock => no drifting
|
||||||
// this.log(begin, end, haps);
|
const begin = this.phase; // begin where we left off last time || 0
|
||||||
// measure time between last and current callback and calculate deviation from extected interval
|
const time = getTime();
|
||||||
const time = getTime?.();
|
this.origin = this.origin ?? time; // capture timestamp of first tick
|
||||||
if (time && this.lastTime) {
|
const runTime = time - this.origin; // how long the scheduler is running since start
|
||||||
const diff = time - this.lastTime;
|
const cps = this.cps; // remember cps used to calculate the current slice
|
||||||
this.error = diff - interval;
|
// TODO: find a way to implement cps without jumps..
|
||||||
}
|
const end = (runTime + interval) * cps;
|
||||||
this.lastTime = time;
|
// console.log('runTime', runTime);
|
||||||
|
this.phase = end; // remember where we left off fro next query
|
||||||
|
const haps = this.pattern.queryArc(begin, end); // get haps
|
||||||
|
const t = getTime(); // need new timestamp after query (can take a few ms)
|
||||||
|
// schedule each hap
|
||||||
haps.forEach((hap) => {
|
haps.forEach((hap) => {
|
||||||
if (typeof hap.value?.cps === 'number') {
|
if (typeof hap.value?.cps === 'number') {
|
||||||
this.setCps(hap.value?.cps);
|
this.setCps(hap.value?.cps);
|
||||||
}
|
}
|
||||||
|
// skip haps without onset
|
||||||
if (!hap.part.begin.equals(hap.whole.begin)) {
|
if (!hap.part.begin.equals(hap.whole.begin)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// console.log('error', this.error);
|
// calculate absolute time for this hap, .whole.begin is relative to 0, so we need to add the origin
|
||||||
const deadline = (hap.whole.begin - begin) / this.cps + latency - this.error;
|
const scheduledTime = hap.whole.begin / cps + latency + this.origin;
|
||||||
// const deadline = hap.whole.begin - begin + latency; // - error;
|
// deadline = time in s until the event should be scheduled
|
||||||
|
const deadline = scheduledTime - t;
|
||||||
if (deadline < 0) {
|
if (deadline < 0) {
|
||||||
console.warn(
|
console.warn(`deadline ${deadline.toFixed(2)} is below zero! latency ${latency}s, interval ${interval}s`);
|
||||||
`deadline ${deadline.toFixed(
|
return;
|
||||||
2,
|
|
||||||
)} is below zero! latency ${latency}s, interval ${interval}s, error ${this.error.toFixed(2)}s`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
// 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;
|
||||||
@ -57,6 +64,7 @@ export class Scheduler {
|
|||||||
}, interval);
|
}, interval);
|
||||||
}
|
}
|
||||||
start() {
|
start() {
|
||||||
|
console.log('start');
|
||||||
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.');
|
||||||
}
|
}
|
||||||
@ -64,15 +72,21 @@ export class Scheduler {
|
|||||||
this.started = true;
|
this.started = true;
|
||||||
}
|
}
|
||||||
pause() {
|
pause() {
|
||||||
|
console.log('pause');
|
||||||
this.worker.stop();
|
this.worker.stop();
|
||||||
|
this.phase = 0;
|
||||||
|
delete this.origin;
|
||||||
this.started = false;
|
this.started = false;
|
||||||
}
|
}
|
||||||
stop() {
|
stop() {
|
||||||
|
console.log('stop');
|
||||||
this.phase = 0;
|
this.phase = 0;
|
||||||
|
delete this.origin;
|
||||||
this.worker.stop();
|
this.worker.stop();
|
||||||
this.started = false;
|
this.started = false;
|
||||||
}
|
}
|
||||||
setPattern(pat) {
|
setPattern(pat) {
|
||||||
|
console.log('set pattern!');
|
||||||
this.pattern = pat;
|
this.pattern = pat;
|
||||||
}
|
}
|
||||||
setCps(cps = 1) {
|
setCps(cps = 1) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user