@senad-d/micme 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/README.md +29 -20
- package/SECURITY.md +5 -3
- package/micme.example.json +31 -0
- package/micme.schema.json +85 -0
- package/package.json +5 -3
- package/scripts/check-package-contents.mjs +4 -1
- package/scripts/doctor.mjs +61 -29
- package/src/audio.ts +539 -34
- package/src/config.ts +136 -85
- package/src/constants.ts +3 -1
- package/src/extension.ts +20 -7
- package/src/settings.ts +48 -43
- package/src/streaming.ts +229 -34
- package/src/types.ts +3 -2
- package/.env.example +0 -75
package/src/audio.ts
CHANGED
|
@@ -1,35 +1,551 @@
|
|
|
1
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { sliceByColumn, visibleWidth, type Component } from "@earendil-works/pi-tui";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
import { AUDIO_VALIDATION_TIMEOUT_MS } from "./constants.ts";
|
|
4
5
|
import { env, envFlag, getAudioFilter, getMinMaxVolumeDb, getRecordSampleRate, getTranscribeSampleRate } from "./config.ts";
|
|
5
6
|
import { findExecutable, formatRunExit, replacePlaceholders, runProcess, shellCommand, shellQuote } from "./processes.ts";
|
|
6
7
|
import type { AudioDeviceCandidate, AudioDiagnostics, CommandSpec, RunResult } from "./types.ts";
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
type DeviceBackend = "avfoundation" | "pulse" | "dshow" | "unsupported";
|
|
10
|
+
type DeviceKind = "audio" | "video";
|
|
11
|
+
|
|
12
|
+
type ListedDevice = {
|
|
13
|
+
id?: string;
|
|
14
|
+
name: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type ParsedDeviceInventory = {
|
|
18
|
+
audio: ListedDevice[];
|
|
19
|
+
video: ListedDevice[];
|
|
20
|
+
sawDeviceSection: boolean;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type DeviceBackendInfo =
|
|
24
|
+
| {
|
|
25
|
+
backend: Exclude<DeviceBackend, "unsupported">;
|
|
26
|
+
sourceLabel: string;
|
|
27
|
+
args: string[];
|
|
28
|
+
includeVideo: boolean;
|
|
29
|
+
}
|
|
30
|
+
| {
|
|
31
|
+
backend: "unsupported";
|
|
32
|
+
sourceLabel: string;
|
|
33
|
+
includeVideo: false;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
type DevicePanelOptions = {
|
|
37
|
+
sourceLabel: string;
|
|
38
|
+
backend: DeviceBackend;
|
|
39
|
+
audio?: ListedDevice[];
|
|
40
|
+
video?: ListedDevice[];
|
|
41
|
+
includeVideo?: boolean;
|
|
42
|
+
warning?: string;
|
|
43
|
+
errorLines?: string[];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
type DevicePanelStyle = {
|
|
47
|
+
plain: boolean;
|
|
48
|
+
separator: string;
|
|
49
|
+
audioLabel: string;
|
|
50
|
+
videoLabel: string;
|
|
51
|
+
warningLabel: string;
|
|
52
|
+
errorLabel: string;
|
|
53
|
+
ellipsis: string;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const DEVICE_SCAN_TIMEOUT_MS = 8_000;
|
|
57
|
+
const DEVICE_PANEL_MESSAGE_TYPE = "micme-devices";
|
|
58
|
+
const DEVICE_PANEL_DEFAULT_WIDTH = 92;
|
|
59
|
+
const DEVICE_PANEL_MAX_WIDTH = 100;
|
|
60
|
+
const DEVICE_PANEL_MIN_WIDTH = 36;
|
|
61
|
+
|
|
62
|
+
let deviceMessageRendererRegistered = false;
|
|
63
|
+
|
|
64
|
+
type DevicePanelMessage = {
|
|
65
|
+
customType: string;
|
|
66
|
+
content: string;
|
|
67
|
+
display: boolean;
|
|
68
|
+
details?: unknown;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
type DeviceMessageRendererOptions = {
|
|
72
|
+
expanded?: boolean;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
type DeviceMessageApi = {
|
|
76
|
+
registerMessageRenderer?: (
|
|
77
|
+
customType: string,
|
|
78
|
+
renderer: (message: DevicePanelMessage, options: DeviceMessageRendererOptions, theme: ExtensionContext["ui"]["theme"]) => Component,
|
|
79
|
+
) => void;
|
|
80
|
+
sendMessage?: (message: DevicePanelMessage, options?: { triggerTurn?: boolean; deliverAs?: "steer" | "followUp" | "nextTurn" }) => void;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export function registerDeviceMessageRenderer(pi: ExtensionAPI) {
|
|
84
|
+
const api = pi as ExtensionAPI & DeviceMessageApi;
|
|
85
|
+
if (!api.registerMessageRenderer) return;
|
|
86
|
+
api.registerMessageRenderer(DEVICE_PANEL_MESSAGE_TYPE, (message) => new DevicePanelMessageComponent(readDevicePanelOptions(message.details)));
|
|
87
|
+
deviceMessageRendererRegistered = true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function listAudioDevices(ctx: ExtensionContext, pi?: ExtensionAPI) {
|
|
91
|
+
const backend = getCurrentDeviceBackend();
|
|
92
|
+
if (backend.backend === "unsupported") {
|
|
93
|
+
await showDevicePanel(ctx, pi, {
|
|
94
|
+
sourceLabel: backend.sourceLabel,
|
|
95
|
+
backend: backend.backend,
|
|
96
|
+
errorLines: ["Device listing is not implemented for this platform"],
|
|
97
|
+
});
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
9
101
|
const ffmpeg = findExecutable(["ffmpeg"]);
|
|
10
102
|
if (!ffmpeg) {
|
|
11
|
-
ctx
|
|
103
|
+
await showDevicePanel(ctx, pi, {
|
|
104
|
+
sourceLabel: backend.sourceLabel,
|
|
105
|
+
backend: backend.backend,
|
|
106
|
+
errorLines: ["ffmpeg not found", "Install ffmpeg or configure a custom recorder before listing devices."],
|
|
107
|
+
});
|
|
12
108
|
return;
|
|
13
109
|
}
|
|
14
110
|
|
|
15
|
-
let title = "Micme audio devices";
|
|
16
111
|
let result: RunResult;
|
|
17
|
-
|
|
18
|
-
result = await runProcess(ffmpeg,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
title = "Micme Windows DirectShow devices";
|
|
26
|
-
} else {
|
|
27
|
-
ctx.ui.notify(`Device listing is not implemented for ${process.platform}.`, "warning");
|
|
112
|
+
try {
|
|
113
|
+
result = await runProcess(ffmpeg, backend.args, DEVICE_SCAN_TIMEOUT_MS);
|
|
114
|
+
} catch {
|
|
115
|
+
await showDevicePanel(ctx, pi, {
|
|
116
|
+
sourceLabel: backend.sourceLabel,
|
|
117
|
+
backend: backend.backend,
|
|
118
|
+
errorLines: ["Device scan failed", "Could not start ffmpeg to list capture devices."],
|
|
119
|
+
});
|
|
28
120
|
return;
|
|
29
121
|
}
|
|
30
122
|
|
|
31
|
-
|
|
32
|
-
|
|
123
|
+
await showDevicePanel(ctx, pi, buildDevicePanelFromRun(backend, result));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function getCurrentDeviceBackend(): DeviceBackendInfo {
|
|
127
|
+
if (process.platform === "darwin") {
|
|
128
|
+
return {
|
|
129
|
+
backend: "avfoundation",
|
|
130
|
+
sourceLabel: "macOS · AVF",
|
|
131
|
+
args: ["-hide_banner", "-f", "avfoundation", "-list_devices", "true", "-i", ""],
|
|
132
|
+
includeVideo: true,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (process.platform === "linux") {
|
|
137
|
+
return {
|
|
138
|
+
backend: "pulse",
|
|
139
|
+
sourceLabel: "Linux · Pulse",
|
|
140
|
+
args: ["-hide_banner", "-sources", "pulse"],
|
|
141
|
+
includeVideo: false,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (process.platform === "win32") {
|
|
146
|
+
return {
|
|
147
|
+
backend: "dshow",
|
|
148
|
+
sourceLabel: "Windows · DShow",
|
|
149
|
+
args: ["-hide_banner", "-list_devices", "true", "-f", "dshow", "-i", "dummy"],
|
|
150
|
+
includeVideo: true,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return { backend: "unsupported", sourceLabel: `${process.platform} · unsupported`, includeVideo: false };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function buildDevicePanelFromRun(backend: Extract<DeviceBackendInfo, { args: string[] }>, result: RunResult): DevicePanelOptions {
|
|
158
|
+
const output = combineRunOutput(result);
|
|
159
|
+
const parsed = parseDeviceInventory(backend.backend, output);
|
|
160
|
+
const hasDevices = parsed.audio.length > 0 || parsed.video.length > 0;
|
|
161
|
+
|
|
162
|
+
if (result.timedOut) {
|
|
163
|
+
return {
|
|
164
|
+
sourceLabel: backend.sourceLabel,
|
|
165
|
+
backend: backend.backend,
|
|
166
|
+
errorLines: ["Device scan timed out", "FFmpeg did not return device information within 8s."],
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (!hasDevices && backend.backend === "avfoundation" && hasMacosPermissionIssue(output)) {
|
|
171
|
+
return {
|
|
172
|
+
sourceLabel: backend.sourceLabel,
|
|
173
|
+
backend: backend.backend,
|
|
174
|
+
errorLines: ["macOS microphone permission may be blocked", "Allow microphone access for the terminal app, then try again."],
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (!hasDevices && result.code !== 0) {
|
|
179
|
+
return {
|
|
180
|
+
sourceLabel: backend.sourceLabel,
|
|
181
|
+
backend: backend.backend,
|
|
182
|
+
audio: parsed.audio,
|
|
183
|
+
video: parsed.video,
|
|
184
|
+
includeVideo: backend.includeVideo,
|
|
185
|
+
errorLines: [getNoParseableDeviceMessage(backend.backend)],
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
sourceLabel: backend.sourceLabel,
|
|
191
|
+
backend: backend.backend,
|
|
192
|
+
audio: parsed.audio,
|
|
193
|
+
video: parsed.video,
|
|
194
|
+
includeVideo: backend.includeVideo,
|
|
195
|
+
warning: getDeviceScanWarning(backend.backend, result, output, hasDevices),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function combineRunOutput(result: RunResult) {
|
|
200
|
+
return `${result.stdout || ""}\n${result.stderr || ""}`;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function parseDeviceInventory(backend: Exclude<DeviceBackend, "unsupported">, output: string): ParsedDeviceInventory {
|
|
204
|
+
if (backend === "avfoundation") return parseAvfoundationDevices(output);
|
|
205
|
+
if (backend === "pulse") return parsePulseAudioDevices(output);
|
|
206
|
+
return parseDirectShowDevices(output);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function parseAvfoundationDevices(output: string): ParsedDeviceInventory {
|
|
210
|
+
const inventory: ParsedDeviceInventory = { audio: [], video: [], sawDeviceSection: false };
|
|
211
|
+
let section: DeviceKind | undefined;
|
|
212
|
+
|
|
213
|
+
for (const line of output.split(/\r?\n/)) {
|
|
214
|
+
if (line.includes("AVFoundation video devices:")) {
|
|
215
|
+
section = "video";
|
|
216
|
+
inventory.sawDeviceSection = true;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (line.includes("AVFoundation audio devices:")) {
|
|
220
|
+
section = "audio";
|
|
221
|
+
inventory.sawDeviceSection = true;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (!section) continue;
|
|
225
|
+
|
|
226
|
+
const match = line.match(/\[(\d+)\]\s+(.+)$/);
|
|
227
|
+
if (!match) continue;
|
|
228
|
+
const id = match[1] ?? "";
|
|
229
|
+
const name = (match[2] ?? "").trim();
|
|
230
|
+
if (!name) continue;
|
|
231
|
+
inventory[section].push({ id, name });
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return inventory;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function parsePulseAudioDevices(output: string): ParsedDeviceInventory {
|
|
238
|
+
const inventory: ParsedDeviceInventory = { audio: [], video: [], sawDeviceSection: false };
|
|
239
|
+
let inSources = false;
|
|
240
|
+
|
|
241
|
+
for (const rawLine of output.split(/\r?\n/)) {
|
|
242
|
+
const line = stripFfmpegLogPrefix(rawLine).trim();
|
|
243
|
+
if (/Auto-detected sources for pulse|sources for pulse/i.test(line)) {
|
|
244
|
+
inSources = true;
|
|
245
|
+
inventory.sawDeviceSection = true;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (!inSources || !line || /^(Cannot|Error|Failed|Unknown)\b/i.test(line)) continue;
|
|
249
|
+
|
|
250
|
+
const sourceLine = line.replace(/^\*\s*/, "");
|
|
251
|
+
const match = sourceLine.match(/^([^\s\[]+)(?:\s+\[(.+)\])?$/);
|
|
252
|
+
if (!match) continue;
|
|
253
|
+
const id = (match[1] ?? "").trim();
|
|
254
|
+
if (!id || id.includes(":")) continue;
|
|
255
|
+
let name = (match[2] ?? "").trim();
|
|
256
|
+
if (id === "default" && (!name || /^default$/i.test(name))) name = "PulseAudio default source";
|
|
257
|
+
inventory.audio.push({ id, name });
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return inventory;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function parseDirectShowDevices(output: string): ParsedDeviceInventory {
|
|
264
|
+
const inventory: ParsedDeviceInventory = { audio: [], video: [], sawDeviceSection: false };
|
|
265
|
+
let section: DeviceKind | undefined;
|
|
266
|
+
|
|
267
|
+
for (const rawLine of output.split(/\r?\n/)) {
|
|
268
|
+
const line = stripFfmpegLogPrefix(rawLine).trim();
|
|
269
|
+
if (/DirectShow video devices/i.test(line)) {
|
|
270
|
+
section = "video";
|
|
271
|
+
inventory.sawDeviceSection = true;
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (/DirectShow audio devices/i.test(line)) {
|
|
275
|
+
section = "audio";
|
|
276
|
+
inventory.sawDeviceSection = true;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (!section || /Alternative name/i.test(line)) continue;
|
|
280
|
+
|
|
281
|
+
const match = line.match(/^"(.+)"$/) ?? line.match(/"([^"]+)"/);
|
|
282
|
+
const name = (match?.[1] ?? "").trim();
|
|
283
|
+
if (!name) continue;
|
|
284
|
+
inventory[section].push({ name });
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return inventory;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function stripFfmpegLogPrefix(line: string) {
|
|
291
|
+
return line.replace(/^(?:\[[^\]]+\]\s*)+/, "");
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function hasMacosPermissionIssue(output: string) {
|
|
295
|
+
return /(?:not authorized|authorization|privacy|permission|operation not permitted|microphone access|access.*microphone|microphone.*blocked)/i.test(output);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function getDeviceScanWarning(backend: Exclude<DeviceBackend, "unsupported">, result: RunResult, output: string, hasDevices: boolean) {
|
|
299
|
+
if (!hasDevices) return undefined;
|
|
300
|
+
if (backend === "avfoundation" && hasAvfoundationIoWarning(output)) return "AVFoundation I/O warning after scan";
|
|
301
|
+
if (backend === "dshow" && /Immediate exit requested/i.test(output)) return undefined;
|
|
302
|
+
if (result.code !== 0 || result.signal) return `${getBackendLongName(backend)} warning after scan`;
|
|
303
|
+
if (hasFfmpegWarningAfterScan(output)) return `${getBackendLongName(backend)} warning after scan`;
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function hasAvfoundationIoWarning(output: string) {
|
|
308
|
+
return /(?:Input\/output error|I\/O error|Error opening input|Error opening input file|Error opening input files)/i.test(output);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function hasFfmpegWarningAfterScan(output: string) {
|
|
312
|
+
return /\b(?:warning|error|failed|cannot|unable)\b/i.test(output);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function getNoParseableDeviceMessage(backend: Exclude<DeviceBackend, "unsupported">) {
|
|
316
|
+
if (backend === "avfoundation") return "No parseable AVFoundation device output";
|
|
317
|
+
if (backend === "pulse") return "No parseable PulseAudio source output";
|
|
318
|
+
return "No parseable DirectShow device output";
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function getBackendLongName(backend: Exclude<DeviceBackend, "unsupported">) {
|
|
322
|
+
if (backend === "avfoundation") return "AVFoundation";
|
|
323
|
+
if (backend === "pulse") return "PulseAudio";
|
|
324
|
+
return "DirectShow";
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async function showDevicePanel(ctx: ExtensionContext, pi: ExtensionAPI | undefined, options: DevicePanelOptions) {
|
|
328
|
+
if (sendDevicePanelMessage(pi, options)) return;
|
|
329
|
+
ctx.ui.setWidget(DEVICE_PANEL_MESSAGE_TYPE, renderDevicePanel(options).split("\n"));
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function sendDevicePanelMessage(pi: ExtensionAPI | undefined, options: DevicePanelOptions) {
|
|
333
|
+
const api = pi as (ExtensionAPI & DeviceMessageApi) | undefined;
|
|
334
|
+
if (!deviceMessageRendererRegistered || !api?.sendMessage) return false;
|
|
335
|
+
api.sendMessage({ customType: DEVICE_PANEL_MESSAGE_TYPE, content: "Micme devices", display: true, details: options });
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
class DevicePanelMessageComponent implements Component {
|
|
340
|
+
constructor(private readonly options: DevicePanelOptions) {}
|
|
341
|
+
|
|
342
|
+
render(width: number): string[] {
|
|
343
|
+
return renderDevicePanel(this.options, Math.min(width, DEVICE_PANEL_MAX_WIDTH)).split("\n");
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
invalidate(): void {}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function readDevicePanelOptions(value: unknown): DevicePanelOptions {
|
|
350
|
+
if (!isDevicePanelOptions(value)) {
|
|
351
|
+
return {
|
|
352
|
+
sourceLabel: "unknown · unsupported",
|
|
353
|
+
backend: "unsupported",
|
|
354
|
+
errorLines: ["Could not render Micme devices panel"],
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
return value;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function isDevicePanelOptions(value: unknown): value is DevicePanelOptions {
|
|
361
|
+
if (!value || typeof value !== "object") return false;
|
|
362
|
+
const candidate = value as DevicePanelOptions;
|
|
363
|
+
return typeof candidate.sourceLabel === "string" && isDeviceBackend(candidate.backend);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function isDeviceBackend(value: unknown): value is DeviceBackend {
|
|
367
|
+
return value === "avfoundation" || value === "pulse" || value === "dshow" || value === "unsupported";
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export function renderDevicePanel(options: DevicePanelOptions, width = getDevicePanelWidth()) {
|
|
371
|
+
const style = getDevicePanelStyle();
|
|
372
|
+
const panelWidth = Math.max(2, Math.floor(width));
|
|
373
|
+
const lines = [buildDevicePanelHeader(panelWidth, options.sourceLabel, style)];
|
|
374
|
+
const hasInventory = options.audio !== undefined || options.video !== undefined || !options.errorLines?.length;
|
|
375
|
+
|
|
376
|
+
if (hasInventory) {
|
|
377
|
+
lines.push(buildDeviceRow("audio", options.audio ?? [], style, panelWidth));
|
|
378
|
+
if (options.includeVideo) lines.push(buildDeviceRow("video", options.video ?? [], style, panelWidth));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (options.warning) lines.push(buildStatusRow("warning", options.warning, style, panelWidth));
|
|
382
|
+
for (const [index, line] of (options.errorLines ?? []).entries()) {
|
|
383
|
+
lines.push(buildErrorRow(line, index === 0, style, panelWidth));
|
|
384
|
+
}
|
|
385
|
+
lines.push(buildDevicePanelFooter(panelWidth, style));
|
|
386
|
+
|
|
387
|
+
return lines.filter(Boolean).join("\n");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function getDevicePanelWidth() {
|
|
391
|
+
const columns = process.stdout.columns;
|
|
392
|
+
if (Number.isFinite(columns) && columns > 0) {
|
|
393
|
+
const capped = Math.min(columns, DEVICE_PANEL_MAX_WIDTH);
|
|
394
|
+
return capped < DEVICE_PANEL_MIN_WIDTH ? Math.max(2, capped) : capped;
|
|
395
|
+
}
|
|
396
|
+
return DEVICE_PANEL_DEFAULT_WIDTH;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function getDevicePanelStyle(): DevicePanelStyle {
|
|
400
|
+
const plain = shouldUsePlainDevicePanel();
|
|
401
|
+
return {
|
|
402
|
+
plain,
|
|
403
|
+
separator: plain ? " | " : " │ ",
|
|
404
|
+
audioLabel: plain ? "audio" : "🎙",
|
|
405
|
+
videoLabel: plain ? "video" : "🎥",
|
|
406
|
+
warningLabel: plain ? "warn" : "⚠",
|
|
407
|
+
errorLabel: plain ? "error" : "✕",
|
|
408
|
+
ellipsis: plain ? "..." : "…",
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function shouldUsePlainDevicePanel() {
|
|
413
|
+
const locale = [process.env.LC_ALL, process.env.LC_CTYPE, process.env.LANG].filter(Boolean).join(" ");
|
|
414
|
+
return envFlag("MICME_PLAIN") || envFlag("MICME_ASCII") || envFlag("MICME_NO_UNICODE") || process.env.TERM === "dumb" || (locale.length > 0 && !/utf-?8/i.test(locale));
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function formatSourceLabel(sourceLabel: string, style: DevicePanelStyle) {
|
|
418
|
+
return style.plain ? sourceLabel.replace(/\s*·\s*/g, " - ") : sourceLabel;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function buildDevicePanelHeader(width: number, sourceLabel: string, style: DevicePanelStyle) {
|
|
422
|
+
const title = "Micme devices";
|
|
423
|
+
if (width < 18) return fitLine(style.plain ? `+-- ${title}` : `╭─ ${title}`, width, style);
|
|
424
|
+
|
|
425
|
+
const overhead = style.plain ? 10 : 8;
|
|
426
|
+
const availableTextWidth = Math.max(1, width - overhead - 1);
|
|
427
|
+
const sourceBudget = Math.max(4, Math.min(visibleWidth(sourceLabel), Math.floor(availableTextWidth * 0.45)));
|
|
428
|
+
let source = ellipsize(formatSourceLabel(sourceLabel, style), sourceBudget, style);
|
|
429
|
+
let titleText = ellipsize(title, Math.max(4, availableTextWidth - visibleWidth(source)), style);
|
|
430
|
+
if (visibleWidth(titleText) + visibleWidth(source) > availableTextWidth) {
|
|
431
|
+
source = ellipsize(source, Math.max(1, availableTextWidth - visibleWidth(titleText)), style);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const fillerWidth = Math.max(1, width - overhead - visibleWidth(titleText) - visibleWidth(source));
|
|
435
|
+
const filler = (style.plain ? "-" : "─").repeat(fillerWidth);
|
|
436
|
+
return style.plain ? `+-- ${titleText} ${filler} ${source} --+` : `╭─ ${titleText} ${filler} ${source} ─╮`;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function buildDevicePanelFooter(width: number, style: DevicePanelStyle) {
|
|
440
|
+
if (width < 2) return style.plain ? "+" : "╰";
|
|
441
|
+
const horizontal = (style.plain ? "-" : "─").repeat(Math.max(0, width - 2));
|
|
442
|
+
return style.plain ? `+${horizontal}+` : `╰${horizontal}╯`;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function buildDeviceRow(kind: DeviceKind, devices: ListedDevice[], style: DevicePanelStyle, width: number) {
|
|
446
|
+
const prefix = style.plain ? ` ${kind.padEnd(5)} ${devices.length}` : ` ${kind === "audio" ? style.audioLabel : style.videoLabel} ${devices.length}`;
|
|
447
|
+
if (devices.length === 0) return fitLine(`${prefix}${style.separator}no ${kind} devices found`, width, style);
|
|
448
|
+
|
|
449
|
+
const displayNames = compactDeviceNames(devices);
|
|
450
|
+
let row = prefix;
|
|
451
|
+
let shown = 0;
|
|
452
|
+
|
|
453
|
+
for (let index = 0; index < devices.length; index++) {
|
|
454
|
+
const remainingAfter = devices.length - index - 1;
|
|
455
|
+
const overflow = remainingAfter > 0 ? `${style.separator}+${remainingAfter} more` : "";
|
|
456
|
+
const chip = formatDeviceChip(devices[index]!, displayNames[index]!);
|
|
457
|
+
const candidate = `${row}${style.separator}${chip}${overflow}`;
|
|
458
|
+
if (visibleWidth(candidate) <= width) {
|
|
459
|
+
row = `${row}${style.separator}${chip}`;
|
|
460
|
+
shown = index + 1;
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const availableForChip = width - visibleWidth(`${row}${style.separator}${overflow}`);
|
|
465
|
+
if (availableForChip >= getMinimumDeviceChipWidth(devices[index]!)) {
|
|
466
|
+
row = `${row}${style.separator}${truncateDeviceChip(devices[index]!, displayNames[index]!, availableForChip, style)}`;
|
|
467
|
+
shown = index + 1;
|
|
468
|
+
}
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const hidden = devices.length - shown;
|
|
473
|
+
if (hidden > 0) {
|
|
474
|
+
const overflow = `${style.separator}+${hidden} more`;
|
|
475
|
+
if (visibleWidth(`${row}${overflow}`) <= width) row = `${row}${overflow}`;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return fitLine(row, width, style);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function buildStatusRow(kind: "warning", message: string, style: DevicePanelStyle, width: number) {
|
|
482
|
+
if (style.plain) return fitLine(` ${style.warningLabel.padEnd(7)}${style.separator}${message}`, width, style);
|
|
483
|
+
return fitLine(` ${kind === "warning" ? style.warningLabel : "✓"} ${message}`, width, style);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function buildErrorRow(message: string, primary: boolean, style: DevicePanelStyle, width: number) {
|
|
487
|
+
if (style.plain) return fitLine(` ${(primary ? style.errorLabel : "").padEnd(7)}${style.separator}${message}`, width, style);
|
|
488
|
+
return fitLine(primary ? ` ${style.errorLabel} ${message}` : ` ${message}`, width, style);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function compactDeviceNames(devices: ListedDevice[]) {
|
|
492
|
+
const originals = devices.map((device) => device.name || (device.id ? "" : "unknown"));
|
|
493
|
+
const proposed = originals.map((name) => (name ? compactLowValueDevicePrefix(name) : ""));
|
|
494
|
+
const counts = new Map<string, number>();
|
|
495
|
+
for (const name of proposed) {
|
|
496
|
+
if (name) counts.set(name.toLowerCase(), (counts.get(name.toLowerCase()) ?? 0) + 1);
|
|
497
|
+
}
|
|
498
|
+
return devices.map((device, index) => {
|
|
499
|
+
const compacted = proposed[index] || originals[index] || "";
|
|
500
|
+
if (compacted !== device.name && compacted && counts.get(compacted.toLowerCase()) === 1) return compacted;
|
|
501
|
+
return originals[index] || "";
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function compactLowValueDevicePrefix(name: string) {
|
|
506
|
+
return name
|
|
507
|
+
.replace(/^Microsoft\s+Teams\s+Audio$/i, "Teams Audio")
|
|
508
|
+
.replace(/^Microsoft\s+Teams\b/i, "Teams")
|
|
509
|
+
.replace(/^Microsoft\s+/i, "")
|
|
510
|
+
.trim();
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function formatDeviceChip(device: ListedDevice, displayName: string) {
|
|
514
|
+
const id = device.id?.trim();
|
|
515
|
+
if (!id) return displayName;
|
|
516
|
+
return displayName ? `${id} ${displayName}` : id;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function truncateDeviceChip(device: ListedDevice, displayName: string, width: number, style: DevicePanelStyle) {
|
|
520
|
+
const id = device.id?.trim();
|
|
521
|
+
if (!id) return ellipsize(displayName, width, style);
|
|
522
|
+
if (visibleWidth(id) >= width) return ellipsize(id, width, style);
|
|
523
|
+
const prefix = `${id} `;
|
|
524
|
+
const nameWidth = width - visibleWidth(prefix);
|
|
525
|
+
if (nameWidth <= 0) return id;
|
|
526
|
+
return `${prefix}${ellipsize(displayName, nameWidth, style)}`;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function getMinimumDeviceChipWidth(device: ListedDevice) {
|
|
530
|
+
const id = device.id?.trim();
|
|
531
|
+
if (id) return device.name ? visibleWidth(id) + 2 : visibleWidth(id);
|
|
532
|
+
return Math.min(4, Math.max(1, visibleWidth(device.name || "unknown")));
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function fitLine(line: string, width: number, style: DevicePanelStyle) {
|
|
536
|
+
return visibleWidth(line) <= width ? line : ellipsize(line, width, style);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function ellipsize(value: string, width: number, style: DevicePanelStyle) {
|
|
540
|
+
if (width <= 0) return "";
|
|
541
|
+
if (visibleWidth(value) <= width) return value;
|
|
542
|
+
const ellipsisWidth = visibleWidth(style.ellipsis);
|
|
543
|
+
if (width <= ellipsisWidth) return sliceByColumn(style.ellipsis, 0, width) || ".".repeat(width);
|
|
544
|
+
let truncated = sliceByColumn(value, 0, width - ellipsisWidth).trimEnd();
|
|
545
|
+
while (truncated && visibleWidth(`${truncated}${style.ellipsis}`) > width) {
|
|
546
|
+
truncated = sliceByColumn(truncated, 0, Math.max(0, visibleWidth(truncated) - 1)).trimEnd();
|
|
547
|
+
}
|
|
548
|
+
return `${truncated || sliceByColumn(value, 0, Math.max(0, width - ellipsisWidth))}${style.ellipsis}`;
|
|
33
549
|
}
|
|
34
550
|
|
|
35
551
|
export async function discoverAudioDevices(): Promise<AudioDeviceCandidate[]> {
|
|
@@ -37,7 +553,7 @@ export async function discoverAudioDevices(): Promise<AudioDeviceCandidate[]> {
|
|
|
37
553
|
if (!ffmpeg) return [];
|
|
38
554
|
|
|
39
555
|
if (process.platform === "darwin") {
|
|
40
|
-
const result = await runProcess(ffmpeg, ["-hide_banner", "-f", "avfoundation", "-list_devices", "true", "-i", ""],
|
|
556
|
+
const result = await runProcess(ffmpeg, ["-hide_banner", "-f", "avfoundation", "-list_devices", "true", "-i", ""], DEVICE_SCAN_TIMEOUT_MS);
|
|
41
557
|
return parseAvfoundationAudioDevices(`${result.stdout}\n${result.stderr}`);
|
|
42
558
|
}
|
|
43
559
|
|
|
@@ -53,22 +569,11 @@ export async function discoverAudioDevices(): Promise<AudioDeviceCandidate[]> {
|
|
|
53
569
|
}
|
|
54
570
|
|
|
55
571
|
export function parseAvfoundationAudioDevices(output: string): AudioDeviceCandidate[] {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
if (inAudioDevices && line.includes("Error opening input")) break;
|
|
64
|
-
if (!inAudioDevices) continue;
|
|
65
|
-
const match = line.match(/\[(\d+)\]\s+(.+)$/);
|
|
66
|
-
if (!match) continue;
|
|
67
|
-
const value = match[1] ?? "";
|
|
68
|
-
const name = (match[2] ?? "").trim();
|
|
69
|
-
devices.push({ label: `${value}: ${name}`, value, description: name });
|
|
70
|
-
}
|
|
71
|
-
return devices;
|
|
572
|
+
return parseAvfoundationDevices(output).audio.map((device) => ({
|
|
573
|
+
label: `${device.id ?? ""}: ${device.name}`,
|
|
574
|
+
value: device.id ?? "",
|
|
575
|
+
description: device.name,
|
|
576
|
+
}));
|
|
72
577
|
}
|
|
73
578
|
|
|
74
579
|
export async function prepareAudioForTranscription(inputPath: string, tempDir: string) {
|