Node.js Audio Recorder
node-audiorecorder is a Node.js module designed for recording audio, delivering a 16-bit signed-integer linear pulse modulation WAV stream. It acts as a wrapper around external system utilities like SoX ('rec' or 'sox' commands) or ALSA's 'arecord' utility, which must be installed and available in the system's PATH. This package is currently at version 3.0.0 and offers robust control over recording parameters such as sample rate, bit depth, channels, encoding, and silence detection. Its primary differentiator is providing a programmatic interface to common command-line audio recording tools, making it suitable for applications requiring real-time audio input processing or storage. While its release cadence appears sporadic based on recent releases, the latest major version indicates ongoing maintenance and improvements.
Common errors
-
Error: spawn sox ENOENT
cause The 'sox' command (or 'rec' which is part of SoX) is not found in the system's PATH.fixEnsure SoX is installed on your operating system and its executable directory is included in your system's PATH environment variable. -
Error: spawn arecord ENOENT
cause The 'arecord' command is not found in the system's PATH, or ALSA utilities are not installed.fixEnsure ALSA utilities are installed (e.g., `sudo apt-get install alsa-utils` on Debian/Ubuntu) and 'arecord' is available in your PATH. Consider using `program: 'rec'` if SoX is installed. -
Warning: 'silence' ignored for program 'arecord'.
cause The `silence`, `thresholdStart`, `thresholdStop`, and `keepSilence` options are only available when using `rec` or `sox` programs, not `arecord`.fixSwitch `options.program` to `rec` or `sox` if you require silence detection features. If using `arecord`, you will need to implement silence detection logic separately.
Warnings
- breaking This module is a wrapper around system utilities (SoX/arecord). These external tools are not bundled and must be manually installed and present in the system's PATH for node-audiorecorder to function.
- gotcha On Windows 10, using SoX version 14.4.2 or later can lead to issues with continuous recording that does not stop correctly.
- gotcha Incorrect or unavailable audio device selection can prevent recording or result in silence. The `device` option might need to be explicitly set.
Install
-
npm install node-audiorecorder -
yarn add node-audiorecorder -
pnpm add node-audiorecorder
Imports
- AudioRecorder
import AudioRecorder from 'node-audiorecorder'
const AudioRecorder = require('node-audiorecorder') - options object
import { options } from 'node-audiorecorder'const options = { /* ... */ }
Quickstart
const AudioRecorder = require('node-audiorecorder');
const fs = require('fs');
const path = require('path');
// IMPORTANT: Ensure SoX or Arecord is installed on your system
// For Linux: sudo apt-get install sox libsox-fmt-all
// For MacOS: brew install sox
// For Windows: Download binaries from sox.sourceforge.net
const outputFilePath = path.join(__dirname, 'output.wav');
const options = {
program: `rec`, // Try `arecord` if on Linux and `rec` doesn't work
device: null, // Set e.g. `hw:1,0` if you have multiple devices
bits: 16,
channels: 1,
encoding: `signed-integer`,
format: `S16_LE`,
rate: 16000,
type: `wav`,
silence: 5, // Stop recording after 5 seconds of silence
thresholdStart: 0.5,
thresholdStop: 0.5,
keepSilence: true
};
const logger = console; // Optional: for debugging
const audioRecorder = new AudioRecorder(options, logger);
// Create a write stream to save the audio to a file
const fileStream = fs.createWriteStream(outputFilePath, { encoding: 'binary' });
// Pipe the audio stream to the file
audioRecorder.stream().pipe(fileStream);
console.log('Starting audio recording... (will stop after 10 seconds or 5s silence)');
audioRecorder.start();
// Stop recording after 10 seconds for demonstration
setTimeout(() => {
audioRecorder.stop();
console.log(`Recording stopped. Audio saved to ${outputFilePath}`);
fileStream.end();
}, 10000);
// Handle errors from the recording process
audioRecorder.on('error', (err) => {
console.error('Recording error:', err);
});