@yeasoft/wav
v1.1.0
Published
Stream classes for Microsoft WAVE audio files
Downloads
63
Readme
node-wav
This module offers streams to help work with Microsoft WAVE files. This module is basically a fork of the original module node-wav, which has not been further developed for many years and whose codebase is outdated due to former compatibility requirements.
The most important thing: this module has no external dependencies.
Installation
Install through npm:
npm install @yeasoft/wavExample
Here's how you would play a standard PCM WAVE file out of the speakers using
node-wav and node-speaker:
const fs = require('node:fs');
const Speaker = require('speaker');
const { WavReader } = require('@yeasoft/wav');
const file = fs.createReadStream('track01.wav');
const reader = new WavReader();
// the "format" event gets emitted at the end of the WAVE header
reader.on('format', format => {
// the WAVE header is stripped from the output of the reader
reader.pipe(new Speaker(format));
});
// pipe the WAVE file to the Reader instance
file.pipe(reader);API
The module exports the follwing three classes that can be used to read and write WAVE files:
Additionally, the following deprecated compatibility aliases are also exported in order to provide a drop in replacement of node-wav:
WavReader(options)
The WavReader class accepts a WAV audio file written to it and outputs the raw
audio data with the WAV header stripped (most of the time, PCM audio data will
be output, depending on the audioFormat property).
A "format" event gets emitted after the WAV header has been parsed.
Options
Currently there are no specific options of the WavReader class but the options
object is also passed verbatim to the base Transform class allowing to influence
the overall behaviour of the class.
WavWriter(options)
The WavWriter class accepts raw audio data written to it (only PCM audio data is
currently supported), and outputs a WAV file with a valid WAVE header at the
beginning specifying the formatting information of the audio stream.
Note that there's an interesting problem, because the WAVE header also
specifies the total byte length of the audio data in the file, and there's no
way that we can know this ahead of time. Therefore the WAVE header will contain
a byte-length if 0 initially, which most WAVE decoders will know means to
just read until EOF.
Optionally, if you are in a situation where you can seek back to the beginning
of the destination of the WAVE file (like writing to a regular file, for
example), then you may listen for the "header" event which will be emitted
after all the data has been written, and you can go back and rewrite the new
header with proper audio byte length into the beginning of the destination
(though if your destination is a regular file, you should use the the
WavFileWriter class instead).
Options
The following options can be specified on creation of a WavWriter object:
format: Format of the audio data. See the official WAVE format specification (default:0x0001)channels: Number of channels (default:2)sampleRate: Sample rate (default:44100)bitDepth: Bits per sample (default:16)bigEndian: Iftrue, a big endian wave file will be generated (default:false)dataLength: The expected size of the "data" portion of the WAVE file (default: maximum valid length for a WAVE file)
The options object is also passed verbatim to the base Transform class allowing
to influence the overall behaviour of the class.
WavFileWriter(path, options)
The WavFileWriter class is, essentially, a combination of fs.createWriteStream() and
the above WavWriter() class, except it automatically corrects the header after the file
is written. Options are passed to both WavWriter() and fs.createWriteStream().
Example usage with mic:
const mic = require('mic'); // requires arecord or sox, see https://www.npmjs.com/package/mic
const { WavFileWriter } = require('@yeasoft/wav');
const micInstance = mic({
rate: '16000',
channels: '1',
debug: true
});
const micInputStream = micInstance.getAudioStream();
const outputFileStream = new WavFileWriter('./test.wav', {
sampleRate: 16000,
channels: 1
});
micInputStream.pipe(outputFileStream);
micInstance.start();
setTimeout(function() {
micInstance.stop();
}, 5000);Options
The following options can be specified on creation of a WavFileWriter object:
fixHeaderOn: Specifies when to fix the header of the WAVE file after having written the whole audio data:none: Do not fix the header. This can be done later using thefixHeadermethodclose: Fix the header automatically on "close" eventheader: Fix the header automatically on "header" event (compatibility withnode-wav)
ignoreFixHeaderError: Iftrue, errors that occur when fixing the headers are silently ignored and no "error" event is emitted (default:false)
The options object is also passed verbatim to the base WavWriter class allowing
to influence the overall behaviour of the class.
