playing around

This commit is contained in:
Jade (Rose) Rowland 2024-01-08 23:34:12 -05:00
parent b52cb198b5
commit 6eec4277c1

View File

@ -16,9 +16,20 @@ export class Cyclist {
this.lastBegin = 0; // query begin of last tick
this.lastEnd = 0; // query end of last tick
this.getTime = getTime; // get absolute time
this.num_cycles_since_last_cps_change = 0;
this.num_cycles_at_cps_change = 0;
this.onToggle = onToggle;
this.latency = latency; // fixed trigger time offset
this.broadcast = new BroadcastChannel('strudel_clock');
this.nextCycleStartTime = 0;
this.broadcast.onmessage = (event) => {
const data = event.data;
const { cps, sendTime, phase, nextCycleStartTime, cycle } = data;
this.cps = cps;
const now = Date.now();
const messageLatency = now - sendTime;
console.log({ messageLatency });
this.nextCycleStartTime = now + nextCycleStartTime - messageLatency;
};
this.clock = createClock(
getTime,
// called slightly before each cycle
@ -27,23 +38,25 @@ export class Cyclist {
this.origin = phase;
}
if (this.num_ticks_since_cps_change === 0) {
this.num_cycles_since_last_cps_change = this.lastEnd;
this.num_cycles_at_cps_change = this.lastEnd;
}
this.num_ticks_since_cps_change++;
try {
const time = getTime();
const begin = this.lastEnd;
this.lastBegin = begin;
console.log();
//convert ticks to cycles, so you can query the pattern for events
const eventLength = duration * this.cps;
const end = this.num_cycles_since_last_cps_change + this.num_ticks_since_cps_change * eventLength;
const num_cycles_since_cps_change = this.num_ticks_since_cps_change * eventLength;
const end = this.num_cycles_at_cps_change + num_cycles_since_cps_change;
this.lastEnd = end;
// query the pattern for events
const haps = this.pattern.queryArc(begin, end);
const tickdeadline = phase - time; // time left until the phase is a whole number
this.lastTick = time + tickdeadline;
haps.forEach((hap) => {
@ -53,6 +66,16 @@ export class Cyclist {
onTrigger?.(hap, deadline, duration, this.cps);
}
});
console.log(1 - (num_cycles_since_cps_change % 1));
if (tick % 1 === 0) {
// this.broadcast.postMessage({
// cps: this.cps,
// sendTime: Date.now(),
// phase,
// nextCycleStartTime: (1 - (num_cycles_since_cps_change % 1)) * this.cps * 1000,
// cycle: num_cycles_since_cps_change,
// });
}
} catch (e) {
logger(`[cyclist] error: ${e.message}`);
onError?.(e);
@ -70,14 +93,27 @@ export class Cyclist {
this.onToggle?.(v);
}
start() {
this.num_ticks_since_cps_change = 0;
this.num_cycles_since_last_cps_change = 0;
if (!this.pattern) {
throw new Error('Scheduler: no pattern set! call .setPattern first.');
}
logger('[cyclist] start');
this.clock.start();
this.setStarted(true);
const date = Date.now();
let wait = this.nextCycleStartTime - date;
wait = Math.max(0, wait);
console.log({ wait });
this.broadcast.postMessage({
type: 'request_start',
});
this.broadcast.onmessage;
setTimeout(() => {
this.num_ticks_since_cps_change = 0;
this.num_cycles_at_cps_change = 0;
if (!this.pattern) {
throw new Error('Scheduler: no pattern set! call .setPattern first.');
}
logger('[cyclist] start');
this.clock.start();
this.setStarted(true);
}, wait);
}
pause() {
logger('[cyclist] pause');