@senad-d/micme 0.3.7 → 0.3.8

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/audio.ts +55 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@senad-d/micme",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "type": "module",
5
5
  "description": "Local voice dictation for Pi coding prompts. Press Alt+M, speak, review, send.",
6
6
  "license": "MIT",
@@ -77,8 +77,8 @@
77
77
  "image": "https://raw.githubusercontent.com/senad-d/micme/main/img/demo.gif"
78
78
  },
79
79
  "peerDependencies": {
80
- "@earendil-works/pi-coding-agent": ">=0.80.7 <0.81.0",
81
- "@earendil-works/pi-tui": ">=0.80.7 <0.81.0"
80
+ "@earendil-works/pi-coding-agent": "*",
81
+ "@earendil-works/pi-tui": "*"
82
82
  },
83
83
  "devDependencies": {
84
84
  "@earendil-works/pi-coding-agent": "0.80.7",
package/src/audio.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
  import { sliceByColumn, visibleWidth, type Component } from "@earendil-works/pi-tui";
3
+ import { spawnSync } from "node:child_process";
3
4
  import { dirname, join } from "node:path";
4
5
  import { AUDIO_VALIDATION_TIMEOUT_MS, MAX_CAPTURED_OUTPUT_CHARS } from "./constants.ts";
5
6
  import { env, envFlag, getAudioFilter, getAvfoundationDropLateFrames, getAvfoundationInputSampleRate, getMinMaxVolumeDb, getRecordMeter, getRecordSampleRate, getRecordSync, getTranscribeSampleRate } from "./config.ts";
@@ -63,6 +64,7 @@ const RECORD_TIMING_SYNC_FILTER = "aresample=async=1:first_pts=0";
63
64
  const AVFOUNDATION_DEVICE_PREFIX_PATTERN = /^\[(\d+)]\s+/;
64
65
  const DIRECT_SHOW_FULL_DEVICE_NAME_PATTERN = /^"(.+)"$/;
65
66
  const DIRECT_SHOW_DEVICE_NAME_PATTERN = /"([^"]+)"/;
67
+ const DIRECT_SHOW_DEVICE_KIND_PATTERN = /^"([^"]+)"\s*\((audio|video)\)$/i;
66
68
  const FFMPEG_WARNING_WORDS = new Set(["warning", "error", "failed", "cannot", "unable"]);
67
69
  const PULSE_SOURCE_LINE_PATTERN = /^([^\s[]+)(?:\s+\[(.+)\])?$/;
68
70
 
@@ -273,27 +275,60 @@ export function parseDirectShowDevices(output: string): ParsedDeviceInventory {
273
275
 
274
276
  for (const rawLine of output.split(/\r?\n/)) {
275
277
  const line = stripFfmpegLogPrefix(rawLine).trim();
276
- if (/DirectShow video devices/i.test(line)) {
277
- section = "video";
278
- inventory.sawDeviceSection = true;
279
- continue;
280
- }
281
- if (/DirectShow audio devices/i.test(line)) {
282
- section = "audio";
278
+ const nextSection = parseDirectShowSection(line);
279
+ if (nextSection) {
280
+ section = nextSection;
283
281
  inventory.sawDeviceSection = true;
284
282
  continue;
285
283
  }
286
- if (!section || /Alternative name/i.test(line)) continue;
287
284
 
288
- const match = DIRECT_SHOW_FULL_DEVICE_NAME_PATTERN.exec(line) ?? DIRECT_SHOW_DEVICE_NAME_PATTERN.exec(line);
289
- const name = sanitizeDeviceField(match?.[1] ?? "");
290
- if (!name) continue;
291
- inventory[section].push({ name });
285
+ const device = parseDirectShowDevice(line, section);
286
+ if (device) inventory[device.kind].push({ name: device.name });
292
287
  }
293
288
 
294
289
  return inventory;
295
290
  }
296
291
 
292
+ function parseDirectShowSection(line: string): DeviceKind | undefined {
293
+ if (/DirectShow video devices/i.test(line)) return "video";
294
+ if (/DirectShow audio devices/i.test(line)) return "audio";
295
+ return undefined;
296
+ }
297
+
298
+ function parseDirectShowDevice(line: string, section: DeviceKind | undefined): { kind: DeviceKind; name: string } | undefined {
299
+ if (/Alternative name/i.test(line)) return undefined;
300
+
301
+ const kindMatch = DIRECT_SHOW_DEVICE_KIND_PATTERN.exec(line);
302
+ if (kindMatch) {
303
+ const name = sanitizeDeviceField(kindMatch[1] ?? "");
304
+ const kind = (kindMatch[2] ?? "").toLowerCase() === "video" ? "video" : "audio";
305
+ return name ? { kind, name } : undefined;
306
+ }
307
+
308
+ if (!section) return undefined;
309
+ const match = DIRECT_SHOW_FULL_DEVICE_NAME_PATTERN.exec(line) ?? DIRECT_SHOW_DEVICE_NAME_PATTERN.exec(line);
310
+ const name = sanitizeDeviceField(match?.[1] ?? "");
311
+ return name ? { kind: section, name } : undefined;
312
+ }
313
+
314
+ function findDefaultDshowAudioDevice(): string | undefined {
315
+ if (process.platform !== "win32") return undefined;
316
+ const ffmpeg = findExecutable(["ffmpeg"]);
317
+ if (!ffmpeg) return undefined;
318
+ try {
319
+ const result = spawnSync(ffmpeg, ["-hide_banner", "-list_devices", "true", "-f", "dshow", "-i", "dummy"], {
320
+ encoding: "utf8",
321
+ timeout: DEVICE_SCAN_TIMEOUT_MS,
322
+ windowsHide: true,
323
+ });
324
+ if (result.error || result.signal) return undefined;
325
+ const inventory = parseDirectShowDevices(`${result.stdout ?? ""}\n${result.stderr ?? ""}`);
326
+ return inventory.audio[0]?.name;
327
+ } catch {
328
+ return undefined;
329
+ }
330
+ }
331
+
297
332
  function stripFfmpegLogPrefix(line: string) {
298
333
  return line.replace(/^(?:\[[^\]]+\]\s*)+/, "");
299
334
  }
@@ -623,6 +658,11 @@ export async function discoverAudioDevices(): Promise<AudioDeviceCandidate[]> {
623
658
  }
624
659
 
625
660
  if (process.platform === "win32") {
661
+ const result = await runProcess(ffmpeg, ["-hide_banner", "-list_devices", "true", "-f", "dshow", "-i", "dummy"], DEVICE_SCAN_TIMEOUT_MS);
662
+ const parsed = parseDirectShowDevices(`${result.stdout ?? ""}\n${result.stderr ?? ""}`);
663
+ if (parsed.audio.length > 0) {
664
+ return parsed.audio.map((device) => ({ label: device.name, value: device.name, description: "DirectShow audio input" }));
665
+ }
626
666
  return [{ label: "default", value: "default", description: "DirectShow default audio input" }];
627
667
  }
628
668
 
@@ -700,7 +740,7 @@ export async function validateRecordedAudio(audioPath: string, signal?: AbortSig
700
740
  throw new Error(
701
741
  `Micme recorded almost-silent audio (max ${formatVolumeDb(diagnostics.maxVolumeDb)} dB; threshold ${minimumMaxVolumeDb.toFixed(1)} dB). ` +
702
742
  "Whisper often hallucinates phrases like 'Thank you very much' from silence. " +
703
- "Run /micme devices and set MICME_AUDIO_DEVICE to the real microphone, or set MICME_VALIDATE_AUDIO=0 to bypass this check.",
743
+ `Run /micme devices and set ${process.platform === "win32" ? "MICME_DSHOW_AUDIO_DEVICE" : "MICME_AUDIO_DEVICE"} to the real microphone, or set MICME_VALIDATE_AUDIO=0 to bypass this check.`,
704
744
  );
705
745
  }
706
746
 
@@ -765,7 +805,8 @@ export function buildRecorderCommand(audioPath: string, tempDir = dirname(audioP
765
805
  }
766
806
 
767
807
  if (process.platform === "win32") {
768
- const input = `audio=${env("MICME_DSHOW_AUDIO_DEVICE") || "default"}`;
808
+ const deviceName = env("MICME_DSHOW_AUDIO_DEVICE") || findDefaultDshowAudioDevice() || "default";
809
+ const input = `audio=${deviceName}`;
769
810
  const args = buildFfmpegRecorderArgs("dshow", input, audioPath, recordSampleRate, { meter: recordMeter, audioFilters: timingFilters });
770
811
  return { command: ffmpeg, args, display: `${ffmpeg} ${args.map(shellQuote).join(" ")}`, meterFromStdout: recordMeter, stopInput: "q\n", signalStopExitCodes: [255] };
771
812
  }