Skip to content

the ultimate vocal synthesizer - sing, speak, beatbox, and more

Rust

The voice runs at 48 kHz whatever the device rate: a 192 kHz device starved the worklet (no sound)

Jameson Hodge6259e880f5d0
diff --git a/src/audio/AudioEngine.ts b/src/audio/AudioEngine.ts
index 0090867..dc1f9cd 100644
--- a/src/audio/AudioEngine.ts
+++ b/src/audio/AudioEngine.ts
@@ -58,7 +58,10 @@ export class AudioEngine {
await this.ctx.resume();
return;
}
- const ctx = new AudioContext({ latencyHint: "interactive" });
+ // 48 kHz whatever the device runs at (the browser resamples): the airway's cost grows with
+ // the square of its rate, and a 192 kHz device took ~3x the CPU of 48 kHz and starved the
+ // worklet (load 120-150%, no sound; issue #33)
+ const ctx = new AudioContext({ latencyHint: "interactive", sampleRate: 48000 });
this.ctx = ctx;
this.sampleRate = ctx.sampleRate;
await ctx.audioWorklet.addModule(workletUrl);
diff --git a/tools/e2e.ts b/tools/e2e.ts
index e93fc09..4d6a7c4 100644
--- a/tools/e2e.ts
+++ b/tools/e2e.ts
@@ -221,10 +221,36 @@ async function run(viewport: { width: number; height: number }, tag: string) {
await page.close();
}
+// the voice runs at 48 kHz on a 192 kHz device: at the device's rate it took ~3x the CPU and
+// starved the worklet (issue #33). The patched constructor plays a device whose default is 192 kHz.
+{
+ const page = await browser.newPage({ viewport: { width: 1280, height: 900 } });
+ page.on("pageerror", (e) => errors.push(`[device-rate] pageerror: ${e.message}`));
+ await page.addInitScript(() => {
+ const Device = window.AudioContext;
+ window.AudioContext = class extends Device {
+ constructor(o: AudioContextOptions = {}) {
+ super({ sampleRate: 192000, ...o });
+ }
+ };
+ });
+ await page.goto(url, { waitUntil: "networkidle" });
+ await page.getByRole("radio", { name: "Play" }).click();
+ await page.getByRole("button", { name: /breathe in and start/i }).click();
+ await page.waitForTimeout(1500);
+ const rate = await page.evaluate(() => {
+ // @ts-expect-error debug handle
+ return window.__chimeraEngine?.ctx?.sampleRate ?? -1;
+ });
+ logs.push(`[device-rate] a 192 kHz device runs the voice at ${rate} Hz`);
+ if (rate !== 48000) errors.push(`[device-rate] the voice ran at ${rate} Hz on a 192 kHz device (want 48000)`);
+ await page.close();
+}
+
await run({ width: 1440, height: 980 }, "desktop");
await run({ width: 390, height: 844 }, "mobile");
await browser.close();
console.log("console errors:", errors.length ? "\n" + errors.join("\n") : "none");
-console.log(logs.filter((l) => /load|warn|transcription|creature|held|back to|beatbox|silence|sing|octave/i.test(l)).slice(0, 40).join("\n"));
+console.log(logs.filter((l) => /load|rate|warn|transcription|creature|held|back to|beatbox|silence|sing|octave/i.test(l)).slice(0, 40).join("\n"));
// a run that collected errors fails, so CI and agents can't read it as a pass
if (errors.length) process.exit(1);