sync mostly working
This commit is contained in:
parent
aa36d25b9a
commit
619ca5385d
1 changed files with 29 additions and 16 deletions
|
|
@ -24,7 +24,7 @@ export class NeoCyclist {
|
||||||
this.time_dif;
|
this.time_dif;
|
||||||
|
|
||||||
this.channel = new BroadcastChannel('strudeltick');
|
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
|
let weight = 0; // the amount of weight that is applied to the current average when averaging a new time dif
|
||||||
const maxWeight = 20;
|
const maxWeight = 20;
|
||||||
const precision = 10 ** 3; //round off time diff to prevent accumulating outliers
|
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.
|
// 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
|
// 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.
|
// in order to schedule events consistently.
|
||||||
const setTimeReference = (num_seconds_since_cps_change, tickdeadline) => {
|
const setTimeReference = (time, workertime) => {
|
||||||
const time_dif = getTime() - num_seconds_since_cps_change + tickdeadline;
|
const time_dif = workertime - time;
|
||||||
// const time_dif = workertime - time;
|
if (worker_time_dif === 0) {
|
||||||
if (this.worker_time_dif == null) {
|
worker_time_dif = time_dif;
|
||||||
this.worker_time_dif = time_dif;
|
|
||||||
} else {
|
} else {
|
||||||
const w = 1; //weight of new time diff;
|
const w = 1; //weight of new time diff;
|
||||||
const new_dif =
|
const new_dif = Math.round(((worker_time_dif * weight + time_dif * w) / (weight + w)) * precision) / precision;
|
||||||
Math.round(((this.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
|
// reset the weight so the clock recovers faster from an audio context freeze/dropout if it happens
|
||||||
weight = 4;
|
weight = 4;
|
||||||
}
|
}
|
||||||
this.worker_time_dif = new_dif;
|
worker_time_dif = new_dif;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const tickCallback = (payload) => {
|
const tickCallback = (payload) => {
|
||||||
|
const workertime = payload.time;
|
||||||
const time = this.getTime();
|
const time = this.getTime();
|
||||||
|
|
||||||
let {
|
let {
|
||||||
|
cycle,
|
||||||
|
phase,
|
||||||
num_cycles_at_cps_change,
|
num_cycles_at_cps_change,
|
||||||
cps,
|
cps,
|
||||||
num_seconds_at_cps_change,
|
num_seconds_at_cps_change,
|
||||||
|
|
@ -67,24 +68,37 @@ export class NeoCyclist {
|
||||||
// const tickdeadline = phase - payload.time;
|
// const tickdeadline = phase - payload.time;
|
||||||
const lastTick = time + tickdeadline;
|
const lastTick = time + tickdeadline;
|
||||||
const secondsSinceLastTick = time - lastTick - duration;
|
const secondsSinceLastTick = time - lastTick - duration;
|
||||||
|
// console.log(phase - payload.time);
|
||||||
|
// console.log({ num_seconds_since_cps_change });
|
||||||
if (this.time_dif == null) {
|
if (this.time_dif == null) {
|
||||||
this.time_dif = getTime() - num_seconds_since_cps_change + tickdeadline;
|
this.time_dif = getTime() - num_seconds_since_cps_change + tickdeadline;
|
||||||
}
|
}
|
||||||
setTimeReference(num_seconds_since_cps_change, tickdeadline);
|
setTimeReference(time, workertime);
|
||||||
this.cps = cps;
|
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 = begin + secondsSinceLastTick * cps;
|
||||||
// this.cycle = payload.cycle;
|
//set the weight of average time diff and processs haps
|
||||||
weight = Math.min(weight + 1, maxWeight);
|
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();
|
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) {
|
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) => {
|
||||||
|
|
@ -139,7 +153,6 @@ export class NeoCyclist {
|
||||||
}
|
}
|
||||||
stop() {
|
stop() {
|
||||||
this.time_dif = null;
|
this.time_dif = null;
|
||||||
this.worker_time_dif = null;
|
|
||||||
logger('[cyclist] stop');
|
logger('[cyclist] stop');
|
||||||
this.setStarted(false);
|
this.setStarted(false);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue