205 lines
6.8 KiB
TypeScript
205 lines
6.8 KiB
TypeScript
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<MicConfig>;
|
|
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));
|
|
}
|
|
|
|
/**
|
|
* The configuration a capture of the temples' own microphones arms them
|
|
* with: `mics` of 2 is one microphone per temple (the front one), 4 is both
|
|
* of each. The front end and direction detection follow Microphone setup
|
|
* (the Microphones app), except that both of a temple's microphones only
|
|
* come through the codec front end. Always raw PCM16 at 16 kHz, which is
|
|
* what the firmware streams and transcription takes.
|
|
*/
|
|
export function micArrayCaptureConfig(mics: 2 | 4): MicConfig {
|
|
const base = loadMicConfig();
|
|
return {
|
|
...base,
|
|
source: mics === 4 ? "codec" : base.source,
|
|
channelMask: mics === 4 ? 0b11 : 0b01,
|
|
codec: "raw",
|
|
format: "pcm16",
|
|
sampleRateHz: 16_000,
|
|
armHardware: true,
|
|
};
|
|
}
|
|
|
|
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<typeof setInterval> | 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();
|