Skip to content

fix(stt): capture through parec instead of sox -t pulseaudio on Linux - #27

Open
fede-ciliberti wants to merge 1 commit into
renjfk:mainfrom
fede-ciliberti:pr/parec-capture
Open

fede-ciliberti wants to merge 1 commit into
renjfk:mainfrom
fede-ciliberti:pr/parec-capture

Conversation

@fede-ciliberti

Copy link
Copy Markdown

What

On Linux, recording fails on any distro whose sox was built without the
PulseAudio format handler:

Recording error: sox FAIL formats: no handler for given file type `pulseaudio'

detectAudioBackend() returns pulseaudio whenever pactl answers, and
buildRecordArgs() then asks sox for -t pulseaudio. Debian and Ubuntu ship
sox with only libsox-fmt-alsa and libsox-fmt-base unless the optional
libsox-fmt-pulse package is installed, so the capture dies before recording
anything.

This captures through parec instead (part of pulseaudio-utils, present
wherever pactl is) and pipes its stdout into sox as raw PCM, which needs no
sox format plugin:

parec --format s16le --rate 16000 --channels 1 --latency-msec 50 | sox -t raw ... out.wav

The sox -t pulseaudio path is kept for systems that do have the handler;
parec is only preferred when the binary exists. coreaudio and the plain
sox -d paths are untouched.

Also

Stopping waits a 500ms grace period before SIGINT to parec: PulseAudio keeps
the captured audio in a server-side buffer and killing the process immediately
discards the tail of the speech (measured ~0.5-0.8s on a PipeWire system).
waitForSoxExit gets a longer budget so the pipe can flush.

Verification

  • capture verified manually on PipeWire: 3s of audio produced a 2.95s 16kHz
    mono WAV through parec | sox
  • the pulseaudio branch is now only taken when parec is absent and
    sox --help reports pulseaudio support
  • 2 new tests: isPulseLikeBackend() and the hints for the parec backend
  • npm test 22/22, npx oxlint . clean, npx oxfmt --check . clean

detectAudioBackend() assumed that a reachable PulseAudio server implies sox can
open a pulseaudio source. Stock Debian/Ubuntu sox ships only libsox-fmt-alsa and
libsox-fmt-base, so recording died with:

  Recording error: sox FAIL formats: no handler for given file type `pulseaudio'

Capture now goes through parec (pulseaudio-utils, present wherever pactl is) and
its stdout is piped into sox as raw PCM, which needs no sox format plugin. The
sox pulseaudio path stays for systems that do have the handler; parec is only
preferred when the binary exists, and the coreaudio/alsa paths are untouched.

Stopping waits a 500ms grace period before SIGINT so the server-side buffer
drains into the pipe instead of discarding the tail of the speech, and
waitForSoxExit gets a longer budget for the pipe to flush.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant