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