more options for zyklus api:

+ allow setting custom interval functions
+ allow disabling phase rounding
This commit is contained in:
Felix Roos 2024-03-21 22:38:20 +01:00
parent 10bd668033
commit dc977b292d

View File

@ -7,6 +7,9 @@ function createClock(
duration = 0.05, // duration of each cycle
interval = 0.1, // interval between callbacks
overlap = 0.1, // overlap between callbacks
setInterval = globalThis.setInterval,
clearInterval = globalThis.clearInterval,
round = true,
) {
let tick = 0; // counts callbacks
let phase = 0; // next callback time
@ -22,7 +25,7 @@ function createClock(
}
// callback as long as we're inside the lookahead
while (phase < lookahead) {
phase = Math.round(phase * precision) / precision;
phase = round ? Math.round(phase * precision) / precision : phase;
phase >= t && callback(phase, duration, tick, t);
phase < t && console.log('TOO LATE', phase); // what if latency is added from outside?
phase += duration; // increment phase by duration
@ -35,7 +38,10 @@ function createClock(
onTick();
intervalID = setInterval(onTick, interval * 1000);
};
const clear = () => intervalID !== undefined && clearInterval(intervalID);
const clear = () => {
intervalID !== undefined && clearInterval(intervalID);
intervalID = undefined;
};
const pause = () => clear();
const stop = () => {
tick = 0;