mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-25 20:48:27 +00:00
seperated service worker zyklus and cyclist zyklus because of import constraints on service workers
This commit is contained in:
parent
e5570a6017
commit
d544bf4665
@ -1,5 +1,5 @@
|
|||||||
// eslint-disable-next-line no-undef
|
// eslint-disable-next-line no-undef
|
||||||
importScripts('./zyklus.js');
|
importScripts('./neozyklus.js');
|
||||||
|
|
||||||
function getTime() {
|
function getTime() {
|
||||||
const precision = 10 ** 4;
|
const precision = 10 ** 4;
|
||||||
|
|||||||
@ -4,7 +4,7 @@ Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/st
|
|||||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as createClock from './zyklus.js';
|
import createClock from './zyklus';
|
||||||
import { logger } from './logger.mjs';
|
import { logger } from './logger.mjs';
|
||||||
|
|
||||||
export class Cyclist {
|
export class Cyclist {
|
||||||
|
|||||||
46
packages/core/neozyklus.js
Normal file
46
packages/core/neozyklus.js
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
// used to consistently schedule events, for use in a service worker - see <https://github.com/tidalcycles/strudel/blob/main/packages/core/clockworker.mjs>
|
||||||
|
this.createClock = (
|
||||||
|
getTime,
|
||||||
|
callback, // called slightly before each cycle
|
||||||
|
duration = 0.05, // duration of each cycle
|
||||||
|
interval = 0.1, // interval between callbacks
|
||||||
|
overlap = 0.1, // overlap between callbacks
|
||||||
|
) => {
|
||||||
|
let tick = 0; // counts callbacks
|
||||||
|
let phase = 0; // next callback time
|
||||||
|
let precision = 10 ** 4; // used to round phase
|
||||||
|
let minLatency = 0.01;
|
||||||
|
const setDuration = (setter) => (duration = setter(duration));
|
||||||
|
overlap = overlap || interval / 2;
|
||||||
|
const onTick = () => {
|
||||||
|
const t = getTime();
|
||||||
|
const lookahead = t + interval + overlap; // the time window for this tick
|
||||||
|
if (phase === 0) {
|
||||||
|
phase = t + minLatency;
|
||||||
|
}
|
||||||
|
// callback as long as we're inside the lookahead
|
||||||
|
while (phase < lookahead) {
|
||||||
|
phase = Math.round(phase * precision) / precision;
|
||||||
|
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
|
||||||
|
tick++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let intervalID;
|
||||||
|
const start = () => {
|
||||||
|
clear(); // just in case start was called more than once
|
||||||
|
onTick();
|
||||||
|
intervalID = setInterval(onTick, interval * 1000);
|
||||||
|
};
|
||||||
|
const clear = () => intervalID !== undefined && clearInterval(intervalID);
|
||||||
|
const pause = () => clear();
|
||||||
|
const stop = () => {
|
||||||
|
tick = 0;
|
||||||
|
phase = 0;
|
||||||
|
clear();
|
||||||
|
};
|
||||||
|
const getPhase = () => phase;
|
||||||
|
// setCallback
|
||||||
|
return { setDuration, start, stop, pause, duration, interval, getPhase, minLatency };
|
||||||
|
};
|
||||||
@ -1,50 +1,49 @@
|
|||||||
// will move to https://github.com/felixroos/zyklus
|
// will move to https://github.com/felixroos/zyklus
|
||||||
// TODO: started flag
|
// TODO: started flag
|
||||||
//TODO: fix tests not understanding "self"
|
|
||||||
if (typeof self !== 'undefined') {
|
function createClock(
|
||||||
self.createClock = (
|
getTime,
|
||||||
getTime,
|
callback, // called slightly before each cycle
|
||||||
callback, // called slightly before each cycle
|
duration = 0.05, // duration of each cycle
|
||||||
duration = 0.05, // duration of each cycle
|
interval = 0.1, // interval between callbacks
|
||||||
interval = 0.1, // interval between callbacks
|
overlap = 0.1, // overlap between callbacks
|
||||||
overlap = 0.1, // overlap between callbacks
|
) {
|
||||||
) => {
|
let tick = 0; // counts callbacks
|
||||||
let tick = 0; // counts callbacks
|
let phase = 0; // next callback time
|
||||||
let phase = 0; // next callback time
|
let precision = 10 ** 4; // used to round phase
|
||||||
let precision = 10 ** 4; // used to round phase
|
let minLatency = 0.01;
|
||||||
let minLatency = 0.01;
|
const setDuration = (setter) => (duration = setter(duration));
|
||||||
const setDuration = (setter) => (duration = setter(duration));
|
overlap = overlap || interval / 2;
|
||||||
overlap = overlap || interval / 2;
|
const onTick = () => {
|
||||||
const onTick = () => {
|
const t = getTime();
|
||||||
const t = getTime();
|
const lookahead = t + interval + overlap; // the time window for this tick
|
||||||
const lookahead = t + interval + overlap; // the time window for this tick
|
if (phase === 0) {
|
||||||
if (phase === 0) {
|
phase = t + minLatency;
|
||||||
phase = t + minLatency;
|
}
|
||||||
}
|
// callback as long as we're inside the lookahead
|
||||||
// callback as long as we're inside the lookahead
|
while (phase < lookahead) {
|
||||||
while (phase < lookahead) {
|
phase = Math.round(phase * precision) / precision;
|
||||||
phase = Math.round(phase * precision) / precision;
|
phase >= t && callback(phase, duration, tick);
|
||||||
phase >= t && callback(phase, duration, tick, t);
|
phase < t && console.log('TOO LATE', phase); // what if latency is added from outside?
|
||||||
phase < t && console.log('TOO LATE', phase); // what if latency is added from outside?
|
phase += duration; // increment phase by duration
|
||||||
phase += duration; // increment phase by duration
|
tick++;
|
||||||
tick++;
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
let intervalID;
|
|
||||||
const start = () => {
|
|
||||||
clear(); // just in case start was called more than once
|
|
||||||
onTick();
|
|
||||||
intervalID = setInterval(onTick, interval * 1000);
|
|
||||||
};
|
|
||||||
const clear = () => intervalID !== undefined && clearInterval(intervalID);
|
|
||||||
const pause = () => clear();
|
|
||||||
const stop = () => {
|
|
||||||
tick = 0;
|
|
||||||
phase = 0;
|
|
||||||
clear();
|
|
||||||
};
|
|
||||||
const getPhase = () => phase;
|
|
||||||
// setCallback
|
|
||||||
return { setDuration, start, stop, pause, duration, interval, getPhase, minLatency };
|
|
||||||
};
|
};
|
||||||
|
let intervalID;
|
||||||
|
const start = () => {
|
||||||
|
clear(); // just in case start was called more than once
|
||||||
|
onTick();
|
||||||
|
intervalID = setInterval(onTick, interval * 1000);
|
||||||
|
};
|
||||||
|
const clear = () => intervalID !== undefined && clearInterval(intervalID);
|
||||||
|
const pause = () => clear();
|
||||||
|
const stop = () => {
|
||||||
|
tick = 0;
|
||||||
|
phase = 0;
|
||||||
|
clear();
|
||||||
|
};
|
||||||
|
const getPhase = () => phase;
|
||||||
|
// setCallback
|
||||||
|
return { setDuration, start, stop, pause, duration, interval, getPhase, minLatency };
|
||||||
}
|
}
|
||||||
|
export default createClock;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user