(
onData: (chunk: Buffer) => void,
onEnd: () => void,
options?: { silenceDetection?: boolean },
)
| 333 | let nativeRecordingActive = false |
| 334 | |
| 335 | export async function startRecording( |
| 336 | onData: (chunk: Buffer) => void, |
| 337 | onEnd: () => void, |
| 338 | options?: { silenceDetection?: boolean }, |
| 339 | ): Promise<boolean> { |
| 340 | logForDebugging(`[voice] startRecording called, platform=${process.platform}`) |
| 341 | |
| 342 | // Try native audio module first (macOS, Linux, Windows via cpal) |
| 343 | const napi = await loadAudioNapi() |
| 344 | const nativeAvailable = |
| 345 | napi.isNativeAudioAvailable() && |
| 346 | (process.platform !== 'linux' || (await linuxHasAlsaCards())) |
| 347 | const useSilenceDetection = options?.silenceDetection !== false |
| 348 | if (nativeAvailable) { |
| 349 | // Ensure any previous recording is fully stopped |
| 350 | if (nativeRecordingActive || napi.isNativeRecordingActive()) { |
| 351 | napi.stopNativeRecording() |
| 352 | nativeRecordingActive = false |
| 353 | } |
| 354 | const started = napi.startNativeRecording( |
| 355 | (data: Buffer) => { |
| 356 | onData(data) |
| 357 | }, |
| 358 | () => { |
| 359 | if (useSilenceDetection) { |
| 360 | nativeRecordingActive = false |
| 361 | onEnd() |
| 362 | } |
| 363 | // In push-to-talk mode, ignore the native module's silence-triggered |
| 364 | // onEnd. Recording continues until the caller explicitly calls |
| 365 | // stopRecording() (e.g. when the user presses Ctrl+X). |
| 366 | }, |
| 367 | ) |
| 368 | if (started) { |
| 369 | nativeRecordingActive = true |
| 370 | return true |
| 371 | } |
| 372 | // Native recording failed — fall through to platform fallbacks |
| 373 | } |
| 374 | |
| 375 | // Windows has no supported fallback |
| 376 | if (process.platform === 'win32') { |
| 377 | logForDebugging('[voice] Windows native recording unavailable, no fallback') |
| 378 | return false |
| 379 | } |
| 380 | |
| 381 | // On Linux, try arecord (ALSA utils) before SoX. Consult the probe so |
| 382 | // backend selection matches checkRecordingAvailability() — otherwise |
| 383 | // on headless Linux with both alsa-utils and SoX, the availability |
| 384 | // check falls through to SoX (probe.ok=false, not WSL) but this path |
| 385 | // would still pick broken arecord. Probe is memoized; zero latency. |
| 386 | if ( |
| 387 | process.platform === 'linux' && |
| 388 | hasCommand('arecord') && |
| 389 | (await probeArecord()).ok |
| 390 | ) { |
| 391 | return startArecordRecording(onData, onEnd) |
| 392 | } |
no test coverage detected