Open History to see the last commit
1/** iOS lists connected inputs only after recording-session setup. */
2const MICROPHONE_KINDS = ["wired", "bluetooth", "builtIn", "carPlay"] as const;
3
4export type MicrophoneKind = (typeof MICROPHONE_KINDS)[number];
5
6export interface MicrophoneDevice {
7 readonly kind: MicrophoneKind;
8 /** iOS's port identifier, stable across reconnects of the same device. */
9 readonly uid: string;
10 readonly name: string;
11}
12
13export type MicrophoneEntry = { readonly kind: MicrophoneKind } | MicrophoneDevice;
14
15interface MicrophoneInput {
16 readonly uid: string;
17 readonly name: string;
18 readonly type: string;
19}
20
21// A car without CarPlay connects as a Bluetooth headset, so it is a Bluetooth device.
22const KIND_BY_PORT_TYPE: Readonly<Record<string, MicrophoneKind>> = {
23 MicrophoneWired: "wired",
24 USBAudio: "wired",
25 LineIn: "wired",
26 BluetoothHFP: "bluetooth",
27 BluetoothLE: "bluetooth",
28 MicrophoneBuiltIn: "builtIn",
29 CarAudio: "carPlay",
30};
31
32const DEVICE_KINDS: ReadonlySet<MicrophoneKind> = new Set(["wired", "bluetooth"]);
33
34const rank = (kind: MicrophoneKind) => MICROPHONE_KINDS.indexOf(kind);
35
36export function isMicrophoneKind(value: unknown): value is MicrophoneKind {
37 return (MICROPHONE_KINDS as ReadonlyArray<unknown>).includes(value);
38}
39
40export function isMicrophoneDevice(entry: MicrophoneEntry): entry is MicrophoneDevice {
41 return "uid" in entry;
42}
43
44export function microphoneEntryKey(entry: MicrophoneEntry): string {
45 return isMicrophoneDevice(entry) ? entry.uid : `kind:${entry.kind}`;
46}
47
48export function microphoneOrder(
49 saved: ReadonlyArray<MicrophoneEntry> = [],
50): ReadonlyArray<MicrophoneEntry> {
51 const order: MicrophoneEntry[] = [];
52 const keys = new Set<string>();
53 for (const entry of saved) {
54 const row =
55 isMicrophoneDevice(entry) && DEVICE_KINDS.has(entry.kind) ? entry : { kind: entry.kind };
56 const key = microphoneEntryKey(row);
57 if (keys.has(key)) continue;
58 keys.add(key);
59 order.push(row);
60 }
61 for (const kind of MICROPHONE_KINDS) {
62 if (keys.has(`kind:${kind}`)) continue;
63 const lastDevice = order.map((entry) => entry.kind).lastIndexOf(kind);
64 const below = order.findIndex(
65 (entry) => !isMicrophoneDevice(entry) && rank(entry.kind) > rank(kind),
66 );
67 order.splice(lastDevice !== -1 ? lastDevice + 1 : below !== -1 ? below : order.length, 0, {
68 kind,
69 });
70 }
71 return order;
72}
73
74export function rememberMicrophones(
75 order: ReadonlyArray<MicrophoneEntry>,
76 inputs: ReadonlyArray<MicrophoneInput>,
77): ReadonlyArray<MicrophoneEntry> {
78 let next: MicrophoneEntry[] | null = null;
79 for (const input of inputs) {
80 const kind = KIND_BY_PORT_TYPE[input.type];
81 if (!kind || !DEVICE_KINDS.has(kind)) continue;
82 const current = next ?? order;
83 const index = current.findIndex((entry) => microphoneEntryKey(entry) === input.uid);
84 const existing = current[index];
85 if (existing && isMicrophoneDevice(existing) && existing.name === input.name) continue;
86 next ??= [...order];
87 const device = { kind, uid: input.uid, name: input.name };
88 if (index !== -1) {
89 next[index] = device;
90 continue;
91 }
92 const kindRow = next.findIndex((entry) => microphoneEntryKey(entry) === `kind:${kind}`);
93 next.splice(kindRow === -1 ? next.length : kindRow, 0, device);
94 }
95 return next ?? order;
96}
97
98/** The connected input of the first entry that has one, or null to leave the route to iOS. */
99export function preferredMicrophoneInput<Input extends MicrophoneInput>(
100 inputs: ReadonlyArray<Input>,
101 order: ReadonlyArray<MicrophoneEntry>,
102): Input | null {
103 const listed = new Set(order.filter(isMicrophoneDevice).map((device) => device.uid));
104 for (const entry of order) {
105 const input = inputs.find((candidate) =>
106 isMicrophoneDevice(entry)
107 ? candidate.uid === entry.uid
108 : KIND_BY_PORT_TYPE[candidate.type] === entry.kind && !listed.has(candidate.uid),
109 );
110 if (input) return input;
111 }
112 return null;
113}