Node.js Audio Recorder

3.0.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the AudioRecorder, start a recording session, and pipe the output stream to a WAV file. It includes necessary external dependency setup comments and error handling.

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);
});

view raw JSON →