import { getStringSetting, setStringSetting } from "../../native/settings-store"; import { useMicControlSetting } from "../../ui/dashboard-settings"; import { addMicStatusListener, micControlSession, sendMicControlRecord } from "../../native/mic-control-link"; import { MIC_LEASE_MS, decodeMicStatus, defaultMicConfig, encodeMicControl, templeActive, type MicConfig, type MicOp, type MicSide, type MicStatus, } from "./mic-protocol"; /** * Host-side controller for the CFW mic_control array: sends the same config * to both temples (each temple is its own endpoint and its own 2-mic array), * tracks per-temple field-104 status, and keeps the 90 s streaming lease * renewed while capture is armed. The feature is part of the required * firmware revision, so the only gate is the developer setting: * micControlSupported() is false and apply() refuses to arm when it is off. */ const CONFIG_SETTING_KEY = "microphones.array-config"; const LEASE_RENEW_MS = Math.floor(MIC_LEASE_MS / 3); export type MicArrayStatuses = { left: MicStatus | null; right: MicStatus | null }; /** * True when the "Use microphone control" developer setting is on. With the * setting off, Faceclaw behaves exactly as it would on firmware without the * mic-control channel (one mixed stream). */ export function micControlSupported(): boolean { return useMicControlSetting.get(); } export function loadMicConfig(): MicConfig { const raw = getStringSetting(CONFIG_SETTING_KEY, ""); if (!raw) return defaultMicConfig(); try { const parsed = JSON.parse(raw) as Partial; const defaults = defaultMicConfig(); return { ...defaults, ...parsed, hostMics: { ...defaults.hostMics, ...parsed.hostMics }, }; } catch { return defaultMicConfig(); } } export function saveMicConfig(config: MicConfig): void { setStringSetting(CONFIG_SETTING_KEY, JSON.stringify(config)); } class MicArrayController { private readonly statusListeners = new Set<(statuses: MicArrayStatuses) => void>(); private statuses: MicArrayStatuses = { left: null, right: null }; // Holds the native listener proxy, which must stay referenced or it gets GC'd. private unsubscribeStatus: (() => void) | null = null; private statusSession: unknown = null; private leaseTimer: ReturnType | null = null; private armed = false; getStatuses(): MicArrayStatuses { return this.statuses; } onStatus(listener: (statuses: MicArrayStatuses) => void): () => void { this.statusListeners.add(listener); listener(this.statuses); return () => this.statusListeners.delete(listener); } /** * True when both temples report the same active configuration; only then * should cross-temple bearing fusion be trusted. */ arrayConsistent(): boolean { const { left, right } = this.statuses; if (!left || !right) return false; return ( left.active && right.active && left.effectiveRateHz === right.effectiveRateHz && left.codec === right.codec && left.format === right.format ); } /** * Push the config to the temples. Each active temple streams its full * front+rear pair (per-mic selection happens host-side after the split); * a temple whose mics are all toggled off gets STOP instead so its stream * doesn't spend BLE bandwidth. */ apply(config: MicConfig): boolean { if (config.armHardware && !micControlSupported()) return false; const leftActive = templeActive(config, "left"); const rightActive = templeActive(config, "right"); if (leftActive || rightActive) { if (!this.send("configure", config, "configure", rightActive, leftActive)) return false; } if (!leftActive || !rightActive) { this.send("stop", undefined, "stop-disabled-temple", !rightActive, !leftActive); } saveMicConfig(config); this.armed = config.armHardware && (leftActive || rightActive); this.syncLeaseTimer(); return true; } /** * Re-send CONFIGURE to one temple whose stream went quiet (a lost write, or * a temple that restarted on its own), leaving the other temple's stream * untouched. */ reconfigureTemple(config: MicConfig, side: MicSide): boolean { if (!templeActive(config, side)) return false; if (config.armHardware && !micControlSupported()) return false; return this.send("configure", config, `configure-${side}`, side === "right", side === "left"); } query(): boolean { return this.send("query", undefined, "query"); } stopStreaming(): void { this.armed = false; this.syncLeaseTimer(); this.send("stop", undefined, "stop"); } private renewLease(): void { if (!this.armed) return; this.send("renew", undefined, "renew"); } private send( op: MicOp, config: MicConfig | undefined, label: string, rightTemple = true, leftTemple = true, ): boolean { const session = micControlSession(); if (!session) return false; try { this.ensureStatusListener(session); return sendMicControlRecord(session, encodeMicControl(op, config), label, rightTemple, leftTemple); } catch (error) { console.warn(`mic control ${label} failed: ${error}`); return false; } } private ensureStatusListener(session: unknown): void { if (this.unsubscribeStatus && this.statusSession === session) return; this.unsubscribeStatus?.(); this.statusSession = session; this.unsubscribeStatus = addMicStatusListener(session, (body, arm) => { const status = decodeMicStatus(body); if (!status) return; const side: MicSide = arm === "L" ? "left" : "right"; this.statuses = { ...this.statuses, [side]: { ...status, side } }; this.statusListeners.forEach((listener) => listener(this.statuses)); }); } private syncLeaseTimer(): void { if (this.armed && !this.leaseTimer) { this.leaseTimer = setInterval(() => this.renewLease(), LEASE_RENEW_MS); } else if (!this.armed && this.leaseTimer) { clearInterval(this.leaseTimer); this.leaseTimer = null; } } } export const micArrayController = new MicArrayController();