|
1 | | -// Auto-generated stub — replace with real implementation |
| 1 | +// audio-capture-napi: cross-platform audio capture using SoX (rec) on macOS |
| 2 | +// and arecord (ALSA) on Linux. Replaces the original cpal-based native module. |
| 3 | + |
| 4 | +import { type ChildProcess, spawn, spawnSync } from 'child_process' |
| 5 | + |
| 6 | +// ─── State ─────────────────────────────────────────────────────────── |
| 7 | + |
| 8 | +let recordingProcess: ChildProcess | null = null |
| 9 | +let availabilityCache: boolean | null = null |
| 10 | + |
| 11 | +// ─── Helpers ───────────────────────────────────────────────────────── |
| 12 | + |
| 13 | +function commandExists(cmd: string): boolean { |
| 14 | + const result = spawnSync(cmd, ['--version'], { |
| 15 | + stdio: 'ignore', |
| 16 | + timeout: 3000, |
| 17 | + }) |
| 18 | + return result.error === undefined |
| 19 | +} |
| 20 | + |
| 21 | +// ─── Public API ────────────────────────────────────────────────────── |
| 22 | + |
| 23 | +/** |
| 24 | + * Check whether a supported audio recording command is available. |
| 25 | + * Returns true if `rec` (SoX) is found on macOS, or `arecord` (ALSA) on Linux. |
| 26 | + * Windows is not supported and always returns false. |
| 27 | + */ |
2 | 28 | export function isNativeAudioAvailable(): boolean { |
| 29 | + if (availabilityCache !== null) { |
| 30 | + return availabilityCache |
| 31 | + } |
| 32 | + |
| 33 | + if (process.platform === 'win32') { |
| 34 | + availabilityCache = false |
| 35 | + return false |
| 36 | + } |
| 37 | + |
| 38 | + if (process.platform === 'darwin') { |
| 39 | + // macOS: use SoX rec |
| 40 | + availabilityCache = commandExists('rec') |
| 41 | + return availabilityCache |
| 42 | + } |
| 43 | + |
| 44 | + if (process.platform === 'linux') { |
| 45 | + // Linux: prefer arecord, fall back to rec |
| 46 | + availabilityCache = commandExists('arecord') || commandExists('rec') |
| 47 | + return availabilityCache |
| 48 | + } |
| 49 | + |
| 50 | + availabilityCache = false |
3 | 51 | return false |
4 | 52 | } |
| 53 | + |
| 54 | +/** |
| 55 | + * Check whether a recording is currently in progress. |
| 56 | + */ |
5 | 57 | export function isNativeRecordingActive(): boolean { |
6 | | - return false |
| 58 | + return recordingProcess !== null && !recordingProcess.killed |
7 | 59 | } |
8 | | -export function stopNativeRecording(): void {} |
| 60 | + |
| 61 | +/** |
| 62 | + * Stop the active recording process, if any. |
| 63 | + */ |
| 64 | +export function stopNativeRecording(): void { |
| 65 | + if (recordingProcess) { |
| 66 | + const proc = recordingProcess |
| 67 | + recordingProcess = null |
| 68 | + if (!proc.killed) { |
| 69 | + proc.kill('SIGTERM') |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Start recording audio. Raw PCM data (16kHz, 16-bit signed, mono) is |
| 76 | + * streamed via the onData callback. onEnd is called when recording stops |
| 77 | + * (either from silence detection or process termination). |
| 78 | + * |
| 79 | + * Returns true if recording started successfully, false otherwise. |
| 80 | + */ |
9 | 81 | export function startNativeRecording( |
10 | | - _onData: (data: Buffer) => void, |
11 | | - _onEnd: () => void, |
| 82 | + onData: (data: Buffer) => void, |
| 83 | + onEnd: () => void, |
12 | 84 | ): boolean { |
13 | | - return false |
| 85 | + // Don't start if already recording |
| 86 | + if (isNativeRecordingActive()) { |
| 87 | + stopNativeRecording() |
| 88 | + } |
| 89 | + |
| 90 | + if (!isNativeAudioAvailable()) { |
| 91 | + return false |
| 92 | + } |
| 93 | + |
| 94 | + let child: ChildProcess |
| 95 | + |
| 96 | + if (process.platform === 'darwin' || (process.platform === 'linux' && commandExists('rec'))) { |
| 97 | + // Use SoX rec: output raw PCM 16kHz 16-bit signed mono to stdout |
| 98 | + child = spawn( |
| 99 | + 'rec', |
| 100 | + [ |
| 101 | + '-q', // quiet |
| 102 | + '--buffer', |
| 103 | + '1024', // small buffer for low latency |
| 104 | + '-t', 'raw', // raw PCM output |
| 105 | + '-r', '16000', // 16kHz sample rate |
| 106 | + '-e', 'signed', // signed integer encoding |
| 107 | + '-b', '16', // 16-bit |
| 108 | + '-c', '1', // mono |
| 109 | + '-', // output to stdout |
| 110 | + ], |
| 111 | + { stdio: ['pipe', 'pipe', 'pipe'] }, |
| 112 | + ) |
| 113 | + } else if (process.platform === 'linux' && commandExists('arecord')) { |
| 114 | + // Use arecord: output raw PCM 16kHz 16-bit signed LE mono to stdout |
| 115 | + child = spawn( |
| 116 | + 'arecord', |
| 117 | + [ |
| 118 | + '-f', 'S16_LE', // signed 16-bit little-endian |
| 119 | + '-r', '16000', // 16kHz sample rate |
| 120 | + '-c', '1', // mono |
| 121 | + '-t', 'raw', // raw PCM, no header |
| 122 | + '-q', // quiet |
| 123 | + '-', // output to stdout |
| 124 | + ], |
| 125 | + { stdio: ['pipe', 'pipe', 'pipe'] }, |
| 126 | + ) |
| 127 | + } else { |
| 128 | + return false |
| 129 | + } |
| 130 | + |
| 131 | + recordingProcess = child |
| 132 | + |
| 133 | + child.stdout?.on('data', (chunk: Buffer) => { |
| 134 | + onData(chunk) |
| 135 | + }) |
| 136 | + |
| 137 | + // Consume stderr to prevent backpressure |
| 138 | + child.stderr?.on('data', () => {}) |
| 139 | + |
| 140 | + child.on('close', () => { |
| 141 | + recordingProcess = null |
| 142 | + onEnd() |
| 143 | + }) |
| 144 | + |
| 145 | + child.on('error', () => { |
| 146 | + recordingProcess = null |
| 147 | + onEnd() |
| 148 | + }) |
| 149 | + |
| 150 | + return true |
14 | 151 | } |
0 commit comments