feat: 添加音频工作模块,支持从后端加载音频处理器

This commit is contained in:
Pine
2026-08-18 07:57:15 +08:00
parent 55d33c091b
commit 6f8ccf1176
4 changed files with 397 additions and 3 deletions
+6
View File
@@ -31,6 +31,12 @@ async function bootstrapRuntimeConfig() {
/* 浏览器开发环境或未注册命令:跳过 */
}
// AudioWorklet 兼容:打包环境(WebView2)无法从 tauri.localhost 自定义协议加载
// worklet 模块,统一改从后端真实 HTTP 服务加载(backend/static/voice-worklets/
if (!window.VOICE_WORKLETS_BASE) {
window.VOICE_WORKLETS_BASE = `${getApiBase()}/static/voice-worklets/`;
}
// 2) 后端 /api/configMQTT 地址/账号;API 基址以前端实际可达地址为准)
// 优先级保护:若构建期已用 .env.local 烘焙了 VITE_MQTT_URL
// 则后端 /api/config 不再覆盖(避免后端默认 localhost 冲掉正确地址)。
+21 -3
View File
@@ -481,9 +481,27 @@ export class S2sWsRealtimeClient extends EventTarget {
// The worklets live at the repo root, one level up from this module.
// ?v=2 busts the browser cache so the fixed (unmuted) playback worklet
// always loads instead of a stale muted copy.
const base = window.VOICE_WORKLETS_BASE || "/voice-worklets/";
await ctx.audioWorklet.addModule(base + "mic-capture.js?v=3");
await ctx.audioWorklet.addModule(base + "audio-playback.js?v=3");
// 多基址回退:打包环境优先后端 httptauri.localhost 无法加载 worklet 模块),
// 浏览器开发回退页面源 / 相对路径。
const candidates = [
window.VOICE_WORKLETS_BASE,
`${window.location.origin}/voice-worklets/`,
"/voice-worklets/",
].filter(Boolean);
let workletErr = null;
let workletOk = false;
for (const base of candidates) {
try {
await ctx.audioWorklet.addModule(base + "mic-capture.js?v=3");
await ctx.audioWorklet.addModule(base + "audio-playback.js?v=3");
workletOk = true;
break;
} catch (err) {
workletErr = err;
console.warn("[ws] worklet 加载失败,尝试备用地址:", base, err);
}
}
if (!workletOk) throw workletErr || new Error("AudioWorklet 模块加载失败");
const captureNode = new AudioWorkletNode(ctx, "mic-capture", {
numberOfInputs: 1,