wavefile
Read & write wave files with 4, 8, 16, 24, 32 & 64-bit data.
Copyright (c) 2017-2018 Rafael da Silva Rocha.
https://github.com/rochars/wavefile
About
wavefile is a module to work with wav files. It is partly inspired by SoX and intended to run in both Node.js and the browser.
With wavefile you can:
- Create wav files from scratch
- Read existing wav files
- Encode/decode files as ADPCM, A-Law and mu-Law
- Read/write the data in a wav file header
- Turn RIFF files to RIFX and RIFX files to RIFF
- Edit BWF metada ("bext" chunk)
And more.
wavefile is extensively tested and contains samples of all supported formats. Please note that some formats (like 8-bit A-Law and 64-bit floating point) are not widely supported and may not load in every player.
Install
npm install wavefile
See it in action
https://tr2099.github.io/
Hit "Load in player" to generate wave files.
This website uses wavefile to create the files. The effects are provided by other libraries.
Some bit depths may not be supported by your browser, like 32-bit floating point or 64-bit floating point.
Use
let fs = require("fs");
let Wavefile = require("wavefile");
let wav = new Wavefile(fs.readFileSync("file.wav"));
console.log(wav.chunkId);
console.log(wav.chunkSize);
console.log(wav.fmtChunkId);
fs.writeFileSync(path, wav.toBuffer());
Create wave files from scratch
You must inform the number of channels, the sample rate, the bit depth and the samples (in this order). The samples are an array of numbers. The array may be multidimensional if there is more than one channel.
Possible values for the bit depth are:
"4" - 4-bit IMA-ADPCM
"8" - 8-bit
"8a" - 8-bit A-Law
"8m" - 8-bit mu-Law
"16" - 16-bit
"24" - 24-bit
"32" - 32-bit
"32f" - 32-bit floating point
"64" - 64-bit floating point
let wav = new WaveFile();
wav.fromScratch(1, 44100, '32', [0, -2147483648, 2147483647, 4]);
fs.writeFileSync(path, wav.toBuffer());
wav.fromScratch(2, 48000, '8', [
[0, -2, 4, 3],
[0, -1, 4, 3]
]);
wav.interleave();
fs.writeFileSync(path, wav.toBuffer());
wav.fromScratch(1, 44100, '32', samples, {"container": "RIFX"});
fs.writeFileSync(path, wav.toBuffer());
Interleave and de-interleave stereo samples
wav.interleave();
wav.deInterleave();
RIFF to RIFX and RIFX to RIFF
wav.toRIFX();
wav.toRIFF();
IMA-ADPCM
16-bit 8000 Hz wave files can be compressed as IMA-ADPCM:
wav.toIMAADPCM();
To decode 4-bit IMA-ADPCM as 16-bit linear PCM:
wav.fromIMAADPCM();
IMA-ADPCM files compressed with wavefile will have a block align of 256 bytes.
wavefile also assumes a block align of 256 bytes when decoding IMA-ADPCM files, but you can also decode files with a different block align:
wav.fromIMAADPCM(1024);
A-Law
16-bit wave files can be encoded as A-Law:
wav.toALaw();
To decode 8-bit A-Law as 16-bit linear PCM:
wav.fromALaw();
mu-Law
16-bit wave files can be encoded as mu-Law:
wav.toMuLaw();
To decode 8-bit mu-Law as 16-bit linear PCM:
wav.fromMuLaw();
Change the bit depth
Currently there is no dithering when changing the bit depth.
If the samples are stereo they need to be interleaved before changing the bit depth.
Notice that you can't change to and from 4-bit ADPCM, 8-bit A-Law and 8-bit mu-Law. To encode/decode files as ADPCM, A-Law and mu-Law you must use the toIMAADPCM(), fromIMAADPCM(), toALaw(), fromALaw(), toMuLaw() and fromMuLaw() methods. Only 16-bit samples can be encoded, and decoding always result in 16-bit samples.
let wav = new Wavefile(fs.readFileSync("file.wav"));
wav.toBitDepth("24");
The properties
let wav = new Wavefile(fs.readFileSync("file.wav"));
console.log(wav.chunkId);
console.log(wav.chunkSize);
console.log(wav.format);
console.log(wav.fmtChunkId);
console.log(wav.fmtChunkSize);
console.log(wav.audioFormat);
console.log(wav.numChannels);
console.log(wav.sampleRate);
console.log(wav.byteRate);
console.log(wav.blockAlign);
console.log(wav.bitsPerSample);
console.log(wav.cbSize);
console.log(wav.validBitsPerSample);
console.log(wav.factChunkId);
console.log(wav.factChunkSize);
console.log(wav.dwSampleLength);
console.log(wav.bextChunkFields);
console.log(wav.dataChunkId);
console.log(wav.dataChunkSize);
console.log(wav.samples);
BWF data
BWF data ("bext" chunk) is stored in the bextChunkFields property in human-readable form.
wav.bextChunkFields = {
"description": "",
"originator": "",
"originatorReference": "",
"originationDate": "",
"originationTime": "",
"timeReference": "",
"version": "",
"UMID": "",
"loudnessValue": "",
"loudnessRange": "",
"maxTruePeakLevel": "",
"maxMomentaryLoudness": "",
"maxShortTermLoudness": "",
"reserved": "",
"codingHistory": ""
};
The samples
Range:
- 0 to 255 for 8-bit
- -32768 to 32767 for 16-bit
- -8388608 to 8388607 for 24-bit
- -2147483648 to 2147483647 for 32-bit
- -1.0 to 1.0 for 32-bit (float)
- -1.0 to 1.0 for 64-bit (float)
LICENSE
Copyright (c) 2017-2018 Rafael da Silva Rocha.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.