scheduler error correction failed attempt
This commit is contained in:
parent
e37e0f189a
commit
275931969e
3 changed files with 26 additions and 12 deletions
|
|
@ -12,7 +12,7 @@ const createWorker = (func) => new Worker(urlifyFunction(func));
|
||||||
// this is just a setInterval with a counter, running in a worker
|
// this is just a setInterval with a counter, running in a worker
|
||||||
export class ClockWorker {
|
export class ClockWorker {
|
||||||
worker;
|
worker;
|
||||||
interval = 1 / 32; // query span, use powers of 2 to get better float accuracy
|
interval = 1 / 10; // query span, use powers of 2 to get better float accuracy
|
||||||
tick = 0;
|
tick = 0;
|
||||||
constructor(callback, interval = this.interval) {
|
constructor(callback, interval = this.interval) {
|
||||||
this.interval = interval;
|
this.interval = interval;
|
||||||
|
|
|
||||||
|
|
@ -13,21 +13,22 @@ export class Scheduler {
|
||||||
phase = 0;
|
phase = 0;
|
||||||
cps = 1;
|
cps = 1;
|
||||||
lastTime;
|
lastTime;
|
||||||
|
error = 0;
|
||||||
constructor({ interval, onTrigger, onError, latency = 0.1, getTime }) {
|
constructor({ interval, onTrigger, onError, latency = 0.1, getTime }) {
|
||||||
this.worker = new ClockWorker((_, interval) => {
|
this.worker = new ClockWorker((_, interval) => {
|
||||||
try {
|
try {
|
||||||
let error = 0;
|
|
||||||
// measure time between last and current callback and calculate deviation from extected interval
|
|
||||||
const time = getTime?.();
|
|
||||||
if (time && this.lastTime) {
|
|
||||||
error = time - this.lastTime - interval; // how off is the callback? positive = too late
|
|
||||||
// console.log('ms error', error * 1000);
|
|
||||||
}
|
|
||||||
this.lastTime = time;
|
|
||||||
const begin = this.phase;
|
const begin = this.phase;
|
||||||
const end = this.phase + interval * this.cps;
|
const end = this.phase + interval * this.cps;
|
||||||
this.phase = end;
|
this.phase = end;
|
||||||
const haps = this.pattern.queryArc(begin, end);
|
const haps = this.pattern.queryArc(begin, end);
|
||||||
|
// this.log(begin, end, haps);
|
||||||
|
// measure time between last and current callback and calculate deviation from extected interval
|
||||||
|
const time = getTime?.();
|
||||||
|
if (time && this.lastTime) {
|
||||||
|
const diff = time - this.lastTime;
|
||||||
|
this.error = diff - interval;
|
||||||
|
}
|
||||||
|
this.lastTime = time;
|
||||||
haps.forEach((hap) => {
|
haps.forEach((hap) => {
|
||||||
if (typeof hap.value?.cps === 'number') {
|
if (typeof hap.value?.cps === 'number') {
|
||||||
this.setCps(hap.value?.cps);
|
this.setCps(hap.value?.cps);
|
||||||
|
|
@ -35,7 +36,16 @@ export class Scheduler {
|
||||||
if (!hap.part.begin.equals(hap.whole.begin)) {
|
if (!hap.part.begin.equals(hap.whole.begin)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const deadline = (hap.whole.begin - begin) / this.cps + latency - error;
|
// console.log('error', this.error);
|
||||||
|
const deadline = (hap.whole.begin - begin) / this.cps + latency - this.error;
|
||||||
|
// const deadline = hap.whole.begin - begin + latency; // - error;
|
||||||
|
if (deadline < 0) {
|
||||||
|
console.warn(
|
||||||
|
`deadline ${deadline.toFixed(
|
||||||
|
2,
|
||||||
|
)} is below zero! latency ${latency}s, interval ${interval}s, error ${this.error.toFixed(2)}s`,
|
||||||
|
);
|
||||||
|
}
|
||||||
// TODO: use legato / duration of objectified value
|
// TODO: use legato / duration of objectified value
|
||||||
const duration = hap.duration / this.cps;
|
const duration = hap.duration / this.cps;
|
||||||
onTrigger?.(hap, deadline, duration);
|
onTrigger?.(hap, deadline, duration);
|
||||||
|
|
@ -68,4 +78,8 @@ export class Scheduler {
|
||||||
setCps(cps = 1) {
|
setCps(cps = 1) {
|
||||||
this.cps = cps;
|
this.cps = cps;
|
||||||
}
|
}
|
||||||
|
log(begin, end, haps) {
|
||||||
|
const onsets = haps.filter((h) => h.hasOnset());
|
||||||
|
console.log(`${begin.toFixed(4)} - ${end.toFixed(4)} ${Array(onsets.length).fill('I').join('')}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ stack(
|
||||||
.superimpose(x=>x.add(.04)) // add second, slightly detuned voice
|
.superimpose(x=>x.add(.04)) // add second, slightly detuned voice
|
||||||
.add(perlin.range(0,.5)) // random pitch variation
|
.add(perlin.range(0,.5)) // random pitch variation
|
||||||
.n() // wrap in "n"
|
.n() // wrap in "n"
|
||||||
.s('sawtooth') // waveform
|
.s('square') // waveform
|
||||||
.gain(.16) // turn down
|
.gain(.16) // turn down
|
||||||
.cutoff(500) // fixed cutoff
|
.cutoff(500) // fixed cutoff
|
||||||
.attack(1) // slowly fade in
|
.attack(1) // slowly fade in
|
||||||
|
|
@ -67,9 +67,9 @@ stack(
|
||||||
// await prebake();
|
// await prebake();
|
||||||
|
|
||||||
const ctx = getAudioContext();
|
const ctx = getAudioContext();
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [code, setCode] = useState(defaultTune);
|
const [code, setCode] = useState(defaultTune);
|
||||||
|
// const [code, setCode] = useState(`"c3".note().slow(2)`);
|
||||||
const { scheduler, evaluate, schedulerError, evalError, isDirty } = useStrudel({
|
const { scheduler, evaluate, schedulerError, evalError, isDirty } = useStrudel({
|
||||||
code,
|
code,
|
||||||
defaultOutput: webaudioOutput,
|
defaultOutput: webaudioOutput,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue