From aa36d25b9a97742e8790ba696fbfd32d8d433fb5 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sat, 23 Mar 2024 14:45:35 -0400 Subject: [PATCH 1/7] still kinda working --- packages/core/clockworker.js | 30 +++++++++++++-- packages/core/neocyclist.mjs | 73 ++++++++++++++++++++---------------- website/src/repl/Repl.jsx | 2 +- 3 files changed, 69 insertions(+), 36 deletions(-) diff --git a/packages/core/clockworker.js b/packages/core/clockworker.js index c5442422..e6165bd5 100644 --- a/packages/core/clockworker.js +++ b/packages/core/clockworker.js @@ -12,6 +12,7 @@ function getTime() { let num_cycles_at_cps_change = 0; let num_ticks_since_cps_change = 0; +let num_seconds_at_cps_change = 0; let cps = 0.5; // {id: {started: boolean}} const clients = new Map(); @@ -22,14 +23,36 @@ const sendMessage = (type, payload) => { channel.postMessage({ type, payload }); }; +//phase, num_cycles_at_cps_change, cps, seconds_at_cps_change + const sendTick = (phase, duration, tick, time) => { + const num_seconds_since_cps_change = num_ticks_since_cps_change * duration; + + const tickdeadline = phase - time; + const lastTick = time + tickdeadline; + const num_cycles_since_cps_change = num_seconds_since_cps_change * cps; + + const begin = num_cycles_at_cps_change + num_cycles_since_cps_change; + const secondsSinceLastTick = time - lastTick - duration; + + const eventLength = duration * cps; + const end = begin + eventLength; + + const cycle = begin + secondsSinceLastTick * cps; + sendMessage('tick', { phase, - duration, time, + begin, + end, cps, + tickdeadline, num_cycles_at_cps_change, + num_seconds_at_cps_change, + duration, num_ticks_since_cps_change, + num_seconds_since_cps_change, + cycle, }); num_ticks_since_cps_change++; }; @@ -71,7 +94,9 @@ const processMessage = (message) => { switch (type) { case 'cpschange': { if (payload.cps !== cps) { - num_cycles_at_cps_change = num_cycles_at_cps_change + num_ticks_since_cps_change * duration * cps; + const num_seconds_since_cps_change = num_ticks_since_cps_change * duration; + num_cycles_at_cps_change = num_cycles_at_cps_change + num_seconds_since_cps_change * cps; + num_seconds_at_cps_change = num_seconds_at_cps_change + num_seconds_since_cps_change; cps = payload.cps; num_ticks_since_cps_change = 0; } @@ -97,7 +122,6 @@ this.onconnect = function (e) { const port = e.ports[0]; port.addEventListener('message', function (e) { - console.log(e.data); processMessage(e.data); }); port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter. diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index 1ffb6fda..f49df0f3 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -21,9 +21,10 @@ export class NeoCyclist { this.worker = new SharedWorker(new URL('./clockworker.js', import.meta.url)); this.worker.port.start(); + this.time_dif; this.channel = new BroadcastChannel('strudeltick'); - let worker_time_dif = 0; // time difference between audio context clock and worker clock + this.worker_time_dif; // time difference between audio context clock and worker clock let weight = 0; // the amount of weight that is applied to the current average when averaging a new time dif const maxWeight = 20; const precision = 10 ** 3; //round off time diff to prevent accumulating outliers @@ -32,63 +33,69 @@ export class NeoCyclist { // aditionally, the message time of the worker pinging the callback to process haps can be inconsistent. // we need to keep a rolling weighted average of the time difference between the worker clock and audio context clock // in order to schedule events consistently. - const setTimeReference = (time, workertime) => { - const time_dif = workertime - time; - if (worker_time_dif === 0) { - worker_time_dif = time_dif; + const setTimeReference = (num_seconds_since_cps_change, tickdeadline) => { + const time_dif = getTime() - num_seconds_since_cps_change + tickdeadline; + // const time_dif = workertime - time; + if (this.worker_time_dif == null) { + this.worker_time_dif = time_dif; } else { const w = 1; //weight of new time diff; - const new_dif = Math.round(((worker_time_dif * weight + time_dif * w) / (weight + w)) * precision) / precision; + const new_dif = + Math.round(((this.worker_time_dif * weight + time_dif * w) / (weight + w)) * precision) / precision; - if (new_dif != worker_time_dif) { + if (new_dif != this.worker_time_dif) { // reset the weight so the clock recovers faster from an audio context freeze/dropout if it happens weight = 4; } - worker_time_dif = new_dif; + this.worker_time_dif = new_dif; } }; - const getTickDeadline = (phase, time) => { - return phase - time - worker_time_dif; - }; - const tickCallback = (payload) => { - const workertime = payload.time; const time = this.getTime(); - const { duration, phase, num_ticks_since_cps_change, num_cycles_at_cps_change, cps } = payload; - setTimeReference(time, workertime); - this.cps = cps; - - //calculate begin and end - const eventLength = duration * cps; - const num_cycles_since_cps_change = num_ticks_since_cps_change * eventLength; - const begin = num_cycles_at_cps_change + num_cycles_since_cps_change; - const tickdeadline = getTickDeadline(phase, time); - const end = begin + eventLength; - - //calculate current cycle + let { + num_cycles_at_cps_change, + cps, + num_seconds_at_cps_change, + duration, + num_seconds_since_cps_change, + begin, + end, + tickdeadline, + } = payload; + // const tickdeadline = phase - payload.time; const lastTick = time + tickdeadline; const secondsSinceLastTick = time - lastTick - duration; - this.cycle = begin + secondsSinceLastTick * cps; - //set the weight of average time diff and processs haps + if (this.time_dif == null) { + this.time_dif = getTime() - num_seconds_since_cps_change + tickdeadline; + } + setTimeReference(num_seconds_since_cps_change, tickdeadline); + this.cps = cps; + + this.cycle = begin + secondsSinceLastTick * cps; + // this.cycle = payload.cycle; weight = Math.min(weight + 1, maxWeight); - processHaps(begin, end, tickdeadline); + processHaps(begin, end, num_cycles_at_cps_change, num_seconds_at_cps_change); this.time_at_last_tick_message = this.getTime(); }; - const processHaps = (begin, end, tickdeadline) => { + const processHaps = (begin, end, num_cycles_at_cps_change, seconds_at_cps_change) => { if (this.started === false) { return; } const haps = this.pattern.queryArc(begin, end, { _cps: this.cps }); haps.forEach((hap) => { - if (hap.part.begin.equals(hap.whole.begin)) { - const deadline = (hap.whole.begin - begin) / this.cps + tickdeadline + this.latency; + if (hap.hasOnset()) { + const targetTime = + (hap.whole.begin - num_cycles_at_cps_change) / this.cps + + seconds_at_cps_change + + this.latency + + this.time_dif; const duration = hap.duration / this.cps; - onTrigger?.(hap, deadline, duration, this.cps); + onTrigger?.(hap, 0, duration, this.cps, targetTime); } }); }; @@ -131,6 +138,8 @@ export class NeoCyclist { this.setStarted(true); } stop() { + this.time_dif = null; + this.worker_time_dif = null; logger('[cyclist] stop'); this.setStarted(false); } diff --git a/website/src/repl/Repl.jsx b/website/src/repl/Repl.jsx index f30fc418..786aad66 100644 --- a/website/src/repl/Repl.jsx +++ b/website/src/repl/Repl.jsx @@ -73,7 +73,7 @@ export function Repl({ embedded = false }) { }); }; const editor = new StrudelMirror({ - sync: false, + sync: true, defaultOutput: webaudioOutput, getTime: () => getAudioContext().currentTime, setInterval, From 619ca5385de604223a2b5dd49843502187e7d785 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sat, 23 Mar 2024 14:52:44 -0400 Subject: [PATCH 2/7] sync mostly working --- packages/core/neocyclist.mjs | 45 +++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index f49df0f3..6f7b3e1d 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -24,7 +24,7 @@ export class NeoCyclist { this.time_dif; this.channel = new BroadcastChannel('strudeltick'); - this.worker_time_dif; // time difference between audio context clock and worker clock + let worker_time_dif = 0; // time difference between audio context clock and worker clock let weight = 0; // the amount of weight that is applied to the current average when averaging a new time dif const maxWeight = 20; const precision = 10 ** 3; //round off time diff to prevent accumulating outliers @@ -33,28 +33,29 @@ export class NeoCyclist { // aditionally, the message time of the worker pinging the callback to process haps can be inconsistent. // we need to keep a rolling weighted average of the time difference between the worker clock and audio context clock // in order to schedule events consistently. - const setTimeReference = (num_seconds_since_cps_change, tickdeadline) => { - const time_dif = getTime() - num_seconds_since_cps_change + tickdeadline; - // const time_dif = workertime - time; - if (this.worker_time_dif == null) { - this.worker_time_dif = time_dif; + const setTimeReference = (time, workertime) => { + const time_dif = workertime - time; + if (worker_time_dif === 0) { + worker_time_dif = time_dif; } else { const w = 1; //weight of new time diff; - const new_dif = - Math.round(((this.worker_time_dif * weight + time_dif * w) / (weight + w)) * precision) / precision; + const new_dif = Math.round(((worker_time_dif * weight + time_dif * w) / (weight + w)) * precision) / precision; - if (new_dif != this.worker_time_dif) { + if (new_dif != worker_time_dif) { // reset the weight so the clock recovers faster from an audio context freeze/dropout if it happens weight = 4; } - this.worker_time_dif = new_dif; + worker_time_dif = new_dif; } }; const tickCallback = (payload) => { + const workertime = payload.time; const time = this.getTime(); let { + cycle, + phase, num_cycles_at_cps_change, cps, num_seconds_at_cps_change, @@ -67,24 +68,37 @@ export class NeoCyclist { // const tickdeadline = phase - payload.time; const lastTick = time + tickdeadline; const secondsSinceLastTick = time - lastTick - duration; - + // console.log(phase - payload.time); + // console.log({ num_seconds_since_cps_change }); if (this.time_dif == null) { this.time_dif = getTime() - num_seconds_since_cps_change + tickdeadline; } - setTimeReference(num_seconds_since_cps_change, tickdeadline); + setTimeReference(time, workertime); this.cps = cps; + //calculate begin and end + // const eventLength = duration * cps; + + // const num_cycles_since_cps_change = num_seconds_since_cps_change * this.cps; + // const begin = num_cycles_at_cps_change + num_cycles_since_cps_change; + + // const end = begin + eventLength; + + //calculate current cycle + this.cycle = begin + secondsSinceLastTick * cps; - // this.cycle = payload.cycle; + //set the weight of average time diff and processs haps weight = Math.min(weight + 1, maxWeight); - processHaps(begin, end, num_cycles_at_cps_change, num_seconds_at_cps_change); + + processHaps(begin, end, phase, num_cycles_at_cps_change, num_seconds_at_cps_change); this.time_at_last_tick_message = this.getTime(); }; - const processHaps = (begin, end, num_cycles_at_cps_change, seconds_at_cps_change) => { + const processHaps = (begin, end, phase, num_cycles_at_cps_change, seconds_at_cps_change) => { if (this.started === false) { return; } + const haps = this.pattern.queryArc(begin, end, { _cps: this.cps }); haps.forEach((hap) => { @@ -139,7 +153,6 @@ export class NeoCyclist { } stop() { this.time_dif = null; - this.worker_time_dif = null; logger('[cyclist] stop'); this.setStarted(false); } From bc917dcf8bd702f82a2bd7ce86491ece04d93bb6 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sat, 23 Mar 2024 14:53:07 -0400 Subject: [PATCH 3/7] sync false --- website/src/repl/Repl.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/repl/Repl.jsx b/website/src/repl/Repl.jsx index 786aad66..f30fc418 100644 --- a/website/src/repl/Repl.jsx +++ b/website/src/repl/Repl.jsx @@ -73,7 +73,7 @@ export function Repl({ embedded = false }) { }); }; const editor = new StrudelMirror({ - sync: true, + sync: false, defaultOutput: webaudioOutput, getTime: () => getAudioContext().currentTime, setInterval, From f00a08ef9e49e207d32100bd99871c8fce794f6b Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sat, 23 Mar 2024 16:18:16 -0400 Subject: [PATCH 4/7] working --- packages/core/neocyclist.mjs | 46 ++++++++++++------------------------ website/src/repl/Repl.jsx | 2 +- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index 6f7b3e1d..91e0828e 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -18,13 +18,12 @@ export class NeoCyclist { this.latency = 0.1; // fixed trigger time offset this.cycle = 0; this.id = Math.round(Date.now() * Math.random()); - + this.worker_time_dif; this.worker = new SharedWorker(new URL('./clockworker.js', import.meta.url)); this.worker.port.start(); this.time_dif; this.channel = new BroadcastChannel('strudeltick'); - let worker_time_dif = 0; // time difference between audio context clock and worker clock let weight = 0; // the amount of weight that is applied to the current average when averaging a new time dif const maxWeight = 20; const precision = 10 ** 3; //round off time diff to prevent accumulating outliers @@ -33,24 +32,25 @@ export class NeoCyclist { // aditionally, the message time of the worker pinging the callback to process haps can be inconsistent. // we need to keep a rolling weighted average of the time difference between the worker clock and audio context clock // in order to schedule events consistently. - const setTimeReference = (time, workertime) => { - const time_dif = workertime - time; - if (worker_time_dif === 0) { - worker_time_dif = time_dif; + const setTimeReference = (num_seconds_at_cps_change, num_seconds_since_cps_change, tickdeadline) => { + const time_dif = getTime() - (num_seconds_at_cps_change + num_seconds_since_cps_change) + tickdeadline; + if (this.worker_time_dif == null) { + this.worker_time_dif = time_dif; } else { const w = 1; //weight of new time diff; - const new_dif = Math.round(((worker_time_dif * weight + time_dif * w) / (weight + w)) * precision) / precision; + const new_dif = + Math.round(((this.worker_time_dif * weight + time_dif * w) / (weight + w)) * precision) / precision; - if (new_dif != worker_time_dif) { + if (new_dif != this.worker_time_dif) { // reset the weight so the clock recovers faster from an audio context freeze/dropout if it happens weight = 4; } - worker_time_dif = new_dif; + this.worker_time_dif = new_dif; } + weight = Math.min(weight + 1, maxWeight); }; const tickCallback = (payload) => { - const workertime = payload.time; const time = this.getTime(); let { @@ -65,30 +65,13 @@ export class NeoCyclist { end, tickdeadline, } = payload; - // const tickdeadline = phase - payload.time; - const lastTick = time + tickdeadline; - const secondsSinceLastTick = time - lastTick - duration; - // console.log(phase - payload.time); - // console.log({ num_seconds_since_cps_change }); - if (this.time_dif == null) { - this.time_dif = getTime() - num_seconds_since_cps_change + tickdeadline; - } - setTimeReference(time, workertime); this.cps = cps; - //calculate begin and end - // const eventLength = duration * cps; - - // const num_cycles_since_cps_change = num_seconds_since_cps_change * this.cps; - // const begin = num_cycles_at_cps_change + num_cycles_since_cps_change; - - // const end = begin + eventLength; - - //calculate current cycle + setTimeReference(num_seconds_at_cps_change, num_seconds_since_cps_change, tickdeadline); + const lastTick = time + tickdeadline; + const secondsSinceLastTick = time - lastTick - duration; this.cycle = begin + secondsSinceLastTick * cps; - //set the weight of average time diff and processs haps - weight = Math.min(weight + 1, maxWeight); processHaps(begin, end, phase, num_cycles_at_cps_change, num_seconds_at_cps_change); this.time_at_last_tick_message = this.getTime(); @@ -107,7 +90,7 @@ export class NeoCyclist { (hap.whole.begin - num_cycles_at_cps_change) / this.cps + seconds_at_cps_change + this.latency + - this.time_dif; + this.worker_time_dif; const duration = hap.duration / this.cps; onTrigger?.(hap, 0, duration, this.cps, targetTime); } @@ -152,6 +135,7 @@ export class NeoCyclist { this.setStarted(true); } stop() { + this.worker_time_dif = null; this.time_dif = null; logger('[cyclist] stop'); this.setStarted(false); diff --git a/website/src/repl/Repl.jsx b/website/src/repl/Repl.jsx index f30fc418..786aad66 100644 --- a/website/src/repl/Repl.jsx +++ b/website/src/repl/Repl.jsx @@ -73,7 +73,7 @@ export function Repl({ embedded = false }) { }); }; const editor = new StrudelMirror({ - sync: false, + sync: true, defaultOutput: webaudioOutput, getTime: () => getAudioContext().currentTime, setInterval, From bd16b403e3270f40cc4107d5f7ed60c30e2fc96f Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sat, 23 Mar 2024 16:28:37 -0400 Subject: [PATCH 5/7] build working properly --- packages/core/clockworker.js | 52 +++++++++++++++++++++++++++++++++--- packages/core/neocyclist.mjs | 1 - packages/core/neozyklus.js | 46 ------------------------------- 3 files changed, 49 insertions(+), 50 deletions(-) delete mode 100644 packages/core/neozyklus.js diff --git a/packages/core/clockworker.js b/packages/core/clockworker.js index e6165bd5..f524e439 100644 --- a/packages/core/clockworker.js +++ b/packages/core/clockworker.js @@ -1,5 +1,4 @@ // eslint-disable-next-line no-undef -importScripts('./neozyklus.js'); // TODO: swap below line with above one when firefox supports esm imports in service workers // see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker?retiredLocale=de#browser_compatibility // import createClock from './zyklus.mjs'; @@ -58,7 +57,7 @@ const sendTick = (phase, duration, tick, time) => { }; //create clock method from zyklus -const clock = this.createClock(getTime, sendTick, duration); +const clock = createClock(getTime, sendTick, duration); let started = false; const startClock = (id) => { @@ -117,7 +116,7 @@ const processMessage = (message) => { } }; -this.onconnect = function (e) { +self.onconnect = function (e) { // the incoming port const port = e.ports[0]; @@ -126,3 +125,50 @@ this.onconnect = function (e) { }); port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter. }; + +// used to consistently schedule events, for use in a service worker - see +function 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 }; +} diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index 91e0828e..3ca40178 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -54,7 +54,6 @@ export class NeoCyclist { const time = this.getTime(); let { - cycle, phase, num_cycles_at_cps_change, cps, diff --git a/packages/core/neozyklus.js b/packages/core/neozyklus.js deleted file mode 100644 index 9ec4e775..00000000 --- a/packages/core/neozyklus.js +++ /dev/null @@ -1,46 +0,0 @@ -// used to consistently schedule events, for use in a service worker - see -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 }; -}; From 86c945114d5d3fa3598ad0d9666cb9d00138dd69 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sat, 23 Mar 2024 16:35:48 -0400 Subject: [PATCH 6/7] removed unessecary variable --- packages/core/neocyclist.mjs | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index 3ca40178..ebffed97 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -21,7 +21,6 @@ export class NeoCyclist { this.worker_time_dif; this.worker = new SharedWorker(new URL('./clockworker.js', import.meta.url)); this.worker.port.start(); - this.time_dif; this.channel = new BroadcastChannel('strudeltick'); let weight = 0; // the amount of weight that is applied to the current average when averaging a new time dif @@ -135,7 +134,6 @@ export class NeoCyclist { } stop() { this.worker_time_dif = null; - this.time_dif = null; logger('[cyclist] stop'); this.setStarted(false); } From 8e2407cfa9d871ac3ed51df46ffbc2c6c20ab3dc Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sat, 23 Mar 2024 16:45:34 -0400 Subject: [PATCH 7/7] cleanup --- packages/core/clockworker.js | 6 ------ packages/core/neocyclist.mjs | 15 +++++---------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/packages/core/clockworker.js b/packages/core/clockworker.js index f524e439..4c33de29 100644 --- a/packages/core/clockworker.js +++ b/packages/core/clockworker.js @@ -22,8 +22,6 @@ const sendMessage = (type, payload) => { channel.postMessage({ type, payload }); }; -//phase, num_cycles_at_cps_change, cps, seconds_at_cps_change - const sendTick = (phase, duration, tick, time) => { const num_seconds_since_cps_change = num_ticks_since_cps_change * duration; @@ -40,16 +38,12 @@ const sendTick = (phase, duration, tick, time) => { const cycle = begin + secondsSinceLastTick * cps; sendMessage('tick', { - phase, - time, begin, end, cps, tickdeadline, num_cycles_at_cps_change, num_seconds_at_cps_change, - duration, - num_ticks_since_cps_change, num_seconds_since_cps_change, cycle, }); diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index ebffed97..278ddc8a 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -50,32 +50,27 @@ export class NeoCyclist { }; const tickCallback = (payload) => { - const time = this.getTime(); - - let { - phase, + const { num_cycles_at_cps_change, cps, num_seconds_at_cps_change, - duration, num_seconds_since_cps_change, begin, end, tickdeadline, + cycle, } = payload; this.cps = cps; + this.cycle = cycle; setTimeReference(num_seconds_at_cps_change, num_seconds_since_cps_change, tickdeadline); - const lastTick = time + tickdeadline; - const secondsSinceLastTick = time - lastTick - duration; - this.cycle = begin + secondsSinceLastTick * cps; + processHaps(begin, end, num_cycles_at_cps_change, num_seconds_at_cps_change); - processHaps(begin, end, phase, num_cycles_at_cps_change, num_seconds_at_cps_change); this.time_at_last_tick_message = this.getTime(); }; - const processHaps = (begin, end, phase, num_cycles_at_cps_change, seconds_at_cps_change) => { + const processHaps = (begin, end, num_cycles_at_cps_change, seconds_at_cps_change) => { if (this.started === false) { return; }