working
This commit is contained in:
parent
9c899d5308
commit
73ee84a1fb
2 changed files with 10 additions and 19 deletions
|
|
@ -4,7 +4,7 @@
|
||||||
// import createClock from './zyklus.mjs';
|
// import createClock from './zyklus.mjs';
|
||||||
|
|
||||||
function getTime() {
|
function getTime() {
|
||||||
const precision = 10 ** 4;
|
|
||||||
const seconds = performance.now() * .001;
|
const seconds = performance.now() * .001;
|
||||||
return seconds
|
return seconds
|
||||||
// return Math.round(seconds * precision) / precision;
|
// return Math.round(seconds * precision) / precision;
|
||||||
|
|
@ -25,25 +25,20 @@ const sendMessage = (type, payload) => {
|
||||||
|
|
||||||
const sendTick = (phase, duration, tick, time) => {
|
const sendTick = (phase, duration, tick, time) => {
|
||||||
const num_seconds_since_cps_change = num_ticks_since_cps_change * duration;
|
const num_seconds_since_cps_change = num_ticks_since_cps_change * duration;
|
||||||
|
|
||||||
const tickdeadline = phase - time;
|
const tickdeadline = phase - time;
|
||||||
const lastTick = time + tickdeadline;
|
const lastTick = time + tickdeadline;
|
||||||
const num_cycles_since_cps_change = num_seconds_since_cps_change * cps;
|
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 begin = num_cycles_at_cps_change + num_cycles_since_cps_change;
|
||||||
const secondsSinceLastTick = time - lastTick - duration;
|
const secondsSinceLastTick = time - lastTick - duration;
|
||||||
|
|
||||||
const eventLength = duration * cps;
|
const eventLength = duration * cps;
|
||||||
const end = begin + eventLength;
|
const end = begin + eventLength;
|
||||||
|
|
||||||
const cycle = begin + secondsSinceLastTick * cps;
|
const cycle = begin + secondsSinceLastTick * cps;
|
||||||
|
|
||||||
sendMessage('tick', {
|
sendMessage('tick', {
|
||||||
begin,
|
begin,
|
||||||
end,
|
end,
|
||||||
cps,
|
cps,
|
||||||
time: num_seconds_at_cps_change + num_seconds_since_cps_change + tickdeadline,
|
time,
|
||||||
num_cycles_at_cps_change,
|
|
||||||
cycle,
|
cycle,
|
||||||
});
|
});
|
||||||
num_ticks_since_cps_change++;
|
num_ticks_since_cps_change++;
|
||||||
|
|
|
||||||
|
|
@ -27,29 +27,25 @@ export class NeoCyclist {
|
||||||
this.worker.port.start();
|
this.worker.port.start();
|
||||||
this.channel = new BroadcastChannel('strudeltick');
|
this.channel = new BroadcastChannel('strudeltick');
|
||||||
const tickCallback = (payload) => {
|
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.cps = cps;
|
||||||
this.cycle = cycle;
|
this.cycle = cycle;
|
||||||
const currentTime = cycleToSeconds(num_cycles_at_cps_change + this.cycle, this.cps)
|
processHaps(begin, end, time);
|
||||||
console.log(time, currentTime, this.cycle)
|
|
||||||
processHaps(begin, end, currentTime, num_cycles_at_cps_change);
|
|
||||||
this.time_at_last_tick_message = this.getTime();
|
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) {
|
if (this.started === false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const haps = this.pattern.queryArc(begin, end, { _cps: this.cps });
|
const haps = this.pattern.queryArc(begin, end, { _cps: this.cps });
|
||||||
|
|
||||||
haps.forEach((hap) => {
|
haps.forEach((hap) => {
|
||||||
if (hap.hasOnset()) {
|
if (hap.hasOnset()) {
|
||||||
const target = cycleToSeconds(hap.whole.begin - num_cycles_at_cps_change, this.cps)
|
const timeUntilTrigger = cycleToSeconds(hap.whole.begin - this.cycle, this.cps);
|
||||||
// const target = (hap.whole.begin - num_cycles_at_cps_change) / this.cps;
|
const target = timeUntilTrigger + currentTime;
|
||||||
const targetTime = this.collator.calculateTimestamp(currentTime, target) + this.latency;
|
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);
|
onTrigger?.(hap, 0, duration, this.cps, targetTime);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -94,7 +90,7 @@ export class NeoCyclist {
|
||||||
}
|
}
|
||||||
stop() {
|
stop() {
|
||||||
logger('[cyclist] stop');
|
logger('[cyclist] stop');
|
||||||
this.collator.reset()
|
this.collator.reset();
|
||||||
this.setStarted(false);
|
this.setStarted(false);
|
||||||
}
|
}
|
||||||
setPattern(pat, autostart = false) {
|
setPattern(pat, autostart = false) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue