| 1 | import { api } from "./api"; |
| 2 | import { runtimeSocketURL } from "./client-context.mjs"; |
| 3 | import workletURL from "./audio-worklet.js?url&no-inline"; |
| 4 | |
| 5 | export class AudioOutput { |
| 6 | constructor(onState, onError = () => {}) { |
| 7 | this.onState = onState; |
| 8 | this.onError = onError; |
| 9 | this.active = false; |
| 10 | this.volume = 1; |
| 11 | this.generation = 0; |
| 12 | } |
| 13 | setActive(active) { |
| 14 | if (this.active === active) return; |
| 15 | this.active = active; |
| 16 | if (active) this.activate(); |
| 17 | else { |
| 18 | this.disconnect(); |
| 19 | this.context?.suspend().catch(() => {}); |
| 20 | this.onState("idle"); |
| 21 | this.onError(""); |
| 22 | } |
| 23 | } |
| 24 | setVolume(value) { |
| 25 | this.volume = value; |
| 26 | if (this.gain) this.gain.gain.value = value; |
| 27 | } |
| 28 | activate() { |
| 29 | if (this.disposed || !this.active) return; |
| 30 | try { |
| 31 | if (!this.context) { |
| 32 | this.context = new AudioContext({ latencyHint: "interactive" }); |
| 33 | this.context.onstatechange = () => { |
| 34 | if (this.disposed || !this.active) return; |
| 35 | if (this.context.state === "running") this.connect(); |
| 36 | else { this.disconnect(); this.onState("blocked"); } |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | const resume = this.context.resume(); |
| 42 | if (this.context.state !== "running") this.onState("blocked"); |
| 43 | void resume.then(() => { |
| 44 | if (this.context.state === "running") this.connect(); |
| 45 | }).catch(() => this.onState("blocked")); |
| 46 | } catch { this.onState("unsupported"); } |
| 47 | } |
| 48 | disconnect() { |
| 49 | this.generation++; |
| 50 | clearTimeout(this.retry); |
| 51 | this.opening = false; |
| 52 | const socket = this.socket; |
| 53 | this.socket = null; |
| 54 | if (socket) { socket.onclose = null; socket.close(); } |
| 55 | this.node?.port.postMessage("reset"); |
| 56 | } |
| 57 | async prepare() { |
| 58 | if (!this.preparing) { |
| 59 | this.preparing = this.context.audioWorklet.addModule(workletURL).then(() => { |
| 60 | if (this.disposed) return; |
| 61 | this.node = new AudioWorkletNode(this.context, "cage-audio", { numberOfInputs: 0, numberOfOutputs: 1, outputChannelCount: [2] }); |
| 62 | this.gain = this.context.createGain(); |
| 63 | this.gain.gain.value = this.volume; |
| 64 | this.node.connect(this.gain).connect(this.context.destination); |
| 65 | }).catch(error => { this.preparing = null; throw error; }); |
| 66 | } |
| 67 | return this.preparing; |
| 68 | } |
| 69 | async connect() { |
| 70 | if (!this.active || this.disposed || this.context.state !== "running" || this.socket || this.opening) return; |
| 71 | clearTimeout(this.retry); |
| 72 | this.opening = true; |
| 73 | const generation = this.generation; |
| 74 | const live = () => this.active && !this.disposed && generation === this.generation; |
| 75 | this.onState("connecting"); |
| 76 | const reconnect = () => { |
| 77 | if (!live()) return; |
| 78 | this.opening = false; |
| 79 | this.socket = null; |
| 80 | this.node?.port.postMessage("reset"); |
| 81 | this.onState("disconnected"); |
| 82 | this.retry = setTimeout(() => this.connect(), 2500); |
| 83 | }; |
| 84 | try { |
| 85 | await this.prepare(); |
| 86 | const format = await api("/audio"); |
| 87 | if (!live()) return; |
| 88 | if (format.format !== "s16le" || format.sample_rate !== 48000 || format.channels !== 2) throw new Error("声音格式不兼容"); |
| 89 | this.onError(""); |
| 90 | const socket = new WebSocket(runtimeSocketURL("/audio")); |
| 91 | socket.binaryType = "arraybuffer"; |
| 92 | this.socket = socket; |
| 93 | let confirmed = false; |
| 94 | socket.onmessage = ({ data }) => { |
| 95 | if (!live()) return; |
| 96 | if (typeof data === "string") { |
| 97 | try { |
| 98 | const header = JSON.parse(data); |
| 99 | confirmed = header.format === "s16le" && header.sample_rate === 48000 && header.channels === 2; |
| 100 | } catch { confirmed = false; } |
| 101 | if (!confirmed) socket.close(); |
| 102 | } else if (confirmed) { |
| 103 | this.onState("playing"); |
| 104 | this.node.port.postMessage(data, [data]); |
| 105 | } |
| 106 | }; |
| 107 | socket.onclose = reconnect; |
| 108 | socket.onerror = () => socket.close(); |
| 109 | this.opening = false; |
| 110 | } catch (error) { |
| 111 | if (live()) this.onError(error.message || "声音连接未能建立,请重试"); |
| 112 | reconnect(); |
| 113 | } |
| 114 | } |
| 115 | close() { |
| 116 | this.disposed = true; |
| 117 | this.disconnect(); |
| 118 | this.node?.disconnect(); |
| 119 | this.gain?.disconnect(); |
| 120 | this.context?.close().catch(() => {}); |
| 121 | } |
| 122 | } |