wavefile
Read & write wave files with 4, 8, 16, 24, 32 & 64-bit data.
Copyright (c) 2017 Rafael da Silva Rocha.
https://github.com/rochars/wavefile
Install
npm install wavefile
See it in action
https://tr2099.github.io/
Hit "Load in player" to generate wave files.
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
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());
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.
let wav = new Wavefile(fs.readFileSync("file.wav"));
wav.toBitDepth("24");
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 PCM:
wav.fromIMAADPCM();
The decoder assumes a 256 block align. To use 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 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 PCM:
wav.fromMuLaw();
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.dataChunkId);
console.log(wav.dataChunkSize);
console.log(wav.samples);
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 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.