the ultimate vocal synthesizer - sing, speak, beatbox, and more
Rust
Slow devices: a 32 kHz rebuild that a browser holds until a gesture no longer stalls (silent for good, state never sent)
Jameson Hodgefa6349cf8826
diff --git a/src/audio/AudioEngine.ts b/src/audio/AudioEngine.tsindex 89388d8..b6a1ace 100644--- a/src/audio/AudioEngine.ts+++ b/src/audio/AudioEngine.ts@@ -25,6 +25,8 @@ const AVERAGE_S = 2;/** a reading counts as no more than this: a stall (a hidden tab, a laptop asleep, a busy moment)* mustn't alone make a fast device look slow, so it takes ~1 s of overload at least to fall back */const MOST = 1.5;+/** how long a start or rebuild waits for the context to run */+const RESUME_WAIT_MS = 3000;/** the first moments' load is the wasm compiling, not the device's speed */const WARM_UP_MS = 2000;@@ -196,14 +198,13 @@ export class AudioEngine {console.error("worklet:", m.message);}};- await resumed;+ // a rebuild comes from no gesture, and a browser may hold the new context until there is one+ // (its resume() then doesn't settle until then): the next tap or key resumes it, and nothing+ // waits on it for long, so the rebuild still finishes (the state is sent, the old song stops)+ const resume = () => void ctx.resume().catch(() => {});+ for (const ev of ["pointerdown", "keydown"]) window.addEventListener(ev, resume, { once: true, capture: true });// a second chance where the first resume was refused (the result is what callers see)- await ctx.resume();- if (ctx.state !== "running") {- // a rebuild comes from no gesture, and some browsers wait for one: the next tap or key- const resume = () => void ctx.resume().catch(() => {});- for (const ev of ["pointerdown", "keydown"]) window.addEventListener(ev, resume, { once: true, capture: true });- }+ await Promise.race([resumed.then(() => ctx.resume()), new Promise((r) => setTimeout(r, RESUME_WAIT_MS))]);}/**diff --git a/tools/e2e.ts b/tools/e2e.tsindex 7cc4a9b..ace4784 100644--- a/tools/e2e.ts+++ b/tools/e2e.ts@@ -356,18 +356,37 @@ async function run(viewport: { width: number; height: number }, tag: string) {// a device too slow for 48 kHz falls back to 32 kHz and keeps sounding; one too slow for that says// so (issues #86, #113). The patched node adds to the load the worklet reports: at 48 kHz only-// (it keeps up at 32), or at every rate.-for (const slowAt of ["48000", "any"]) {+// (it keeps up at 32), or at every rate. "-gesture": the browser holds the 32 kHz context, built+// without a gesture, until there is one (its resume() doesn't settle till then): the rebuild must+// still finish, and the next tap must bring the sound back.+for (const slowAt of ["48000", "any", "48000-gesture"]) {const tag = `slow-${slowAt}`;const page = await browser.newPage({ viewport: { width: 1280, height: 900 } });page.on("pageerror", (e) => errors.push(`[${tag}] pageerror: ${e.message}`));await page.addInitScript((slowAt) => {+ if (slowAt.endsWith("-gesture")) {+ let gestured = false;+ window.addEventListener("pointerdown", () => (gestured = true), { capture: true });+ const Device = window.AudioContext, resume = Device.prototype.resume;+ window.AudioContext = class extends Device {+ constructor(o?: AudioContextOptions) {+ super(o);+ // (the autoplay switch starts a context after its constructor: hold it at each start)+ // (only a tap after it counts: the one that started the audio came before)+ if (this.sampleRate === 32000) gestured = false;+ if (this.sampleRate === 32000) this.addEventListener("statechange", () => !gestured && this.state === "running" && void this.suspend());+ }+ override resume(): Promise<void> {+ return this.sampleRate !== 32000 || gestured ? resume.call(this) : new Promise<void>(() => {});+ }+ };+ }const Node = window.AudioWorkletNode;const onmessage = Object.getOwnPropertyDescriptor(MessagePort.prototype, "onmessage")!;window.AudioWorkletNode = class extends Node {constructor(ctx: BaseAudioContext, name: string, o?: AudioWorkletNodeOptions) {super(ctx, name, o);- const port = this.port, slow = slowAt === "any" || String(ctx.sampleRate) === slowAt;+ const port = this.port, slow = slowAt === "any" || String(ctx.sampleRate) === slowAt.split("-")[0];let h: ((e: MessageEvent) => void) | null = null;Object.defineProperty(port, "onmessage", {get: () => h,@@ -389,6 +408,20 @@ for (const slowAt of ["48000", "any"]) {const e = window.__chimeraEngine;return { rate: e?.ctx?.sampleRate ?? -1, rms: e?.latest?.t?.outRms ?? -1 };});+ if (slowAt.endsWith("-gesture")) {+ const held = await page.evaluate(() => {+ // @ts-expect-error debug handle+ return window.__chimeraEngine?.ctx?.state;+ });+ await page.locator(".wordmark").click();+ await page.waitForTimeout(1500);+ state.rms = await page.evaluate(() => {+ // @ts-expect-error debug handle+ return window.__chimeraEngine?.latest?.t?.outRms ?? -1;+ });+ logs.push(`[${tag}] the 32 kHz context was ${held} until a tap`);+ if (held !== "suspended") errors.push(`[${tag}] the held context was ${held} before the tap (the test's patch didn't hold it)`);+ }const warned = await page.getByText(/This device can't keep up/).isVisible();const badge = await page.getByText(/· 32 kHz/).isVisible();logs.push(`[${tag}] slow device: runs at ${state.rate} Hz, rms ${state.rms.toFixed(4)}, 32 kHz shown ${badge}, warning ${warned}`);