Compare commits

...

1 Commits

Author SHA1 Message Date
Felix Roos 1f2202e6a1 fix clock timing problems after system sleep deprivation 2026-10-12 23:02:04 +02:00
2 changed files with 20 additions and 3 deletions
+10 -1
View File
@@ -55,6 +55,7 @@ export function parseControlsFromHap(hap, cps) {
return controls;
}
let timeOrigin = performance.timeOrigin;
export async function oscOutput(hap, targetTime, cps = 1) {
const ws = await connect();
@@ -63,7 +64,15 @@ export async function oscOutput(hap, targetTime, cps = 1) {
logger('[osc] clockbridge not ready');
return;
}
const ts = performance.timeOrigin + timeMs;
// when the system sleeps while the browser is open, performance.now() doesnt tick
// .. which means performance.timeOrigin + performance.now() doesnt land in the present
const deprivation = timeOrigin + performance.now() - Date.now();
if (Math.abs(deprivation) > 20) {
console.log(`[osc] snap with ${(deprivation / 1000).toFixed(2)}s deprivation`);
// shift time after microsleep
timeOrigin -= deprivation;
}
const ts = timeOrigin + timeMs;
const controls = parseControlsFromHap(hap, cps);
const keyvals = Object.entries(controls).flat();
+10 -2
View File
@@ -25,8 +25,16 @@ export class ClockBridge {
}
const offset = performanceTime - contextTime * 1000; // clock offset in ms
const dt = performanceTime - (this.lastTime ?? performanceTime); // delta time since last hap
const decay = 1 / 10000; // how fast offset changes have an effect
this.p = expDecay(this.p ?? offset, offset, decay, dt); // smooth clock offset
const deprivation = offset - this.p;
// when the system sleeps, the audio and system clock get jumbled
// so we dont smooth if the jump is too high, to snap the offset
if (Math.abs(deprivation) > 20) {
console.log(`[ClockBridge] snap with ${(deprivation / 1000).toFixed(2)}s deprivation`);
this.p = offset;
} else {
const decay = 1 / 10000; // how fast offset changes have an effect
this.p = expDecay(this.p ?? offset, offset, decay, dt); // smooth clock offset
}
this.lastTime = performanceTime;
return this.p;
}