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