Files
webspeak3/web/src/aec3.ts
T
MoepchiandClaude Opus 5.5 eba1e1af92 Web: smaller review fixes (A10-A19, S10, S11, lint)
- A10: drop labels of unimplemented shortcuts, localize "Ctrl", never
  fire shortcuts (Ctrl+D!) while typing in a field
- A11: in-memory localStorage stand-in when storage is blocked instead
  of a blank page
- A12: invite link falls back to selecting the text without clipboard API
- A13: AEC3 script honors BASE_URL, failed loads are retried
- A14: shared downloadBlob helper, object URLs revoked with a delay
- A15: remaining hardcoded German/English strings moved to the locales
- A16: broken soundpack files show an error instead of failing silently
- A17: connect buttons no longer pass the click event as overrides
- A18: design base persisted from the resolved theme
- A19: no audio send() while the socket is still connecting
- S10: privilege keys are no longer persisted in localStorage
- S11: share links can carry the token in the fragment (#token=)
- oxlint: 0 warnings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
2026-09-27 19:20:47 +00:00

66 lines
2.2 KiB
TypeScript

// Lazy loader for Google WebRTC's AEC3 (compiled to WASM via
// @ennuicastr/webrtcaec3.js), an opt-in, stronger alternative to the browser's
// native echo cancellation for speaker+mic (no headset) setups where native
// AEC often isn't enough (github.com/Moepchi/webspeak3 issue #2). Loaded as a
// plain <script> tag rather than bundled: the module's own glue code resolves
// its .wasm file next to its own <script src>, by a name it decides itself -
// see scripts/sync-aec3-assets.mjs for how that file gets there.
export interface Aec3Instance {
free(): void;
analyze(data: Float32Array[]): void;
processSize(data: Float32Array[]): number;
process(out: Float32Array[], data: Float32Array[]): void;
}
interface Aec3Module {
AEC3: new (sampleRate: number, renderChannels: number, captureChannels: number) => Aec3Instance;
}
declare global {
interface Window {
WebRtcAec3?: () => Promise<Aec3Module>;
}
}
let modulePromise: Promise<Aec3Module> | null = null;
function loadScript(src: string): Promise<void> {
return new Promise((resolve, reject) => {
const el = document.createElement("script");
el.src = src;
el.onload = () => resolve();
el.onerror = () => reject(new Error(`Failed to load ${src}`));
document.head.appendChild(el);
});
}
function loadModule(): Promise<Aec3Module> {
if (!modulePromise) {
// BASE_URL keeps sub-path deployments (VITE_BASE_PATH) working; a failed
// load is forgotten so the next attempt retries instead of reusing it.
modulePromise = loadScript(`${import.meta.env.BASE_URL}vendor/webrtcaec3.js`)
.then(() => {
if (!window.WebRtcAec3) throw new Error("AEC3 script loaded but did not register window.WebRtcAec3");
return window.WebRtcAec3();
})
.catch((err) => {
modulePromise = null;
throw err;
});
}
return modulePromise;
}
/** Creates a fresh AEC3 instance. `renderChannels` must match the channel
* count passed to `analyze()`, `captureChannels` the count passed to
* `process()`. */
export async function createAec3(
sampleRate: number,
renderChannels: number,
captureChannels: number,
): Promise<Aec3Instance> {
const mod = await loadModule();
return new mod.AEC3(sampleRate, renderChannels, captureChannels);
}