This commit is contained in:
Jade (Rose) Rowland 2024-08-17 17:11:51 -04:00
parent 9c899d5308
commit 73ee84a1fb
2 changed files with 10 additions and 19 deletions

View File

@ -4,7 +4,7 @@
// import createClock from './zyklus.mjs';
function getTime() {
const precision = 10 ** 4;
const seconds = performance.now() * .001;
return seconds
// return Math.round(seconds * precision) / precision;
@ -25,25 +25,20 @@ const sendMessage = (type, payload) => {
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', {
begin,
end,
cps,
time: num_seconds_at_cps_change + num_seconds_since_cps_change + tickdeadline,
num_cycles_at_cps_change,
time,
cycle,
});
num_ticks_since_cps_change++;

View File

@ -27,29 +27,25 @@ export class NeoCyclist {
this.worker.port.start();
this.channel = new BroadcastChannel('strudeltick');
const tickCallback = (payload) => {
const { num_cycles_at_cps_change, cps, begin, end, cycle, time } = payload;
const { cps, begin, end, cycle, time } = payload;
this.cps = cps;
this.cycle = cycle;
const currentTime = cycleToSeconds(num_cycles_at_cps_change + this.cycle, this.cps)
console.log(time, currentTime, this.cycle)
processHaps(begin, end, currentTime, num_cycles_at_cps_change);
processHaps(begin, end, time);
this.time_at_last_tick_message = this.getTime();
};
const processHaps = (begin, end, currentTime, num_cycles_at_cps_change) => {
const processHaps = (begin, end, currentTime) => {
if (this.started === false) {
return;
}
const haps = this.pattern.queryArc(begin, end, { _cps: this.cps });
haps.forEach((hap) => {
if (hap.hasOnset()) {
const target = cycleToSeconds(hap.whole.begin - num_cycles_at_cps_change, this.cps)
// const target = (hap.whole.begin - num_cycles_at_cps_change) / this.cps;
const timeUntilTrigger = cycleToSeconds(hap.whole.begin - this.cycle, this.cps);
const target = timeUntilTrigger + currentTime;
const targetTime = this.collator.calculateTimestamp(currentTime, target) + this.latency;
const duration = cycleToSeconds(hap.duration, this.cps)
const duration = cycleToSeconds(hap.duration, this.cps);
onTrigger?.(hap, 0, duration, this.cps, targetTime);
}
});
@ -94,7 +90,7 @@ export class NeoCyclist {
}
stop() {
logger('[cyclist] stop');
this.collator.reset()
this.collator.reset();
this.setStarted(false);
}
setPattern(pat, autostart = false) {