wavefile
Copyright (c) 2017-2018 Rafael da Silva Rocha.
https://github.com/rochars/wavefile
wavefile is a JavaScript module to work with .wav files.
- Works out of the box in the browser
- Works out of the box in Node
- Works out of the box with TypeScript
- Works with huge wave files
- Create wave files from scratch
- Read and write tags on .wav files
- Set and delete cue points and their labels
- Encode/decode files as ADPCM, A-Law and μ-Law
- Turn RIFF files to RIFX and RIFX files to RIFF
- Create or edit BWF metadata ("bext" chunk)
- Change the bit depth of the audio
- Just 7 dependencies, all MIT-licensed
- Less than 10kb minified + compressed, less than 30kb minified
- Made with Closure Compiler in mind (works great with others, too)
And more.
Table of Contents
Install
npm install wavefile
Use
ES6
import WaveFile from wavefile.js:
import WaveFile from 'wavefile.js';
let wav = new WaveFile();
Node
Require WaveFile from wavefile:
const WaveFile = require('wavefile');
let wav = new WaveFile();
This also works:
const WaveFile = require('wavefile').default;
let wav = new WaveFile();
Browser
Use the compiled file in the /dist folder:
<script src="wavefile.min.js"></script>
<script>
var WaveFile = new WaveFile();
</script>
Or get it from the jsDelivr CDN:
<script src="https://cdn.jsdelivr.net/npm/wavefile"></script>
Or get it from unpkg:
<script src="https://unpkg.com/wavefile"></script>
Or as a module from jspm:
<script type="module">
import WaveFile from 'https://dev.jspm.io/wavefile';
console.log(new WaveFile());
</script>
Browser Compatibility
wavefile supports all browsers that are ES5-compliant (IE8 and below are not supported).
See it in action
With wavefile you can change the bit depth and compression type of wav files on the fly before loading them in a browser player. This example uses wavefile and wavesurfer to create a browser player that supports mu-Law, A-Law, IMA ADPCM and all other formats supported by wavefile:
https://rochars.github.io/wavefile/example
var wav = new WaveFile(ADPCMFileBuffer);
wav.fromIMAADPCM();
var wavDataURI = wav.toDataURI();
Check out wavesurfer:
https://github.com/katspaugh/wavesurfer.js
Example
const WaveFile = require('wavefile');
let wav = new WaveFile(buffer);
console.log(wav.container);
console.log(wav.chunkSize);
console.log(wav.fmt.chunkId);
let wavBuffer = wav.toBuffer();
let wavDataURI = wav.toDataURI();
Operation Manual
Create wave files from scratch
You must inform the number of channels, the sample rate, the bit depth and the samples (in this order).
Mono:
let wav = new WaveFile();
wav.fromScratch(1, 44100, '32', [0, -2147483648, 2147483647, 4]);
fs.writeFileSync(path, wav.toBuffer());
Stereo:
Samples can be informed interleaved or de-interleaved. If they are de-interleaved, WaveFile will interleave them. In this example they are de-interleaved.
wav.fromScratch(2, 48000, '8', [
[0, -2, 4, 3],
[0, -1, 4, 3]
]);
fs.writeFileSync(path, wav.toBuffer());
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
You can also use any bit depth between "8" and "53", like "11", "12", "17", "20" and so on.
A word on bit depth
Resolutions other than 4-bit, 8-bit, 16-bit, 24-bit, 32-bit (integer), 32-bit (fp) and 64-bit (fp) are implemented as WAVE_FORMAT_EXTENSIBLE and may not be supported by some players.
Add RIFF tags to files
You can create (or overwrite) tags on files with the WaveFile.setTag() method.
wav.setTag("ICMT", "some comments");
To get the value of a tag (if it exists), use WaveFile.getTag():
console.log(wav.getTag("ICMT"));
You can delete a tag with WaveFile.deleteTag():
wav.deleteTag("ICMT");
Add cue points to files
You can create cue points using the WaveFile.setCuePoint() method. The method takes time in milliseconds, a text label and creates a cue point in the corresponding position of the file:
wav.setCuePoint(1750, "some label for the cue point");
To delete a cue point use WaveFile.deleteCuePoint() informing the index of the point. Points are ordered according to their position. The first point is indexed as 1.
wav.deleteCuePoint(1);
Mind that creating or deleting cue points will change the index of other points if they exist.
RIFX
wavefile can handle existing RIFX files and create RIFX files from scratch. Files created from scratch will default to RIFF; to create a file as RIFX you must define the container:
wav.fromScratch(1, 48000, '16', [0, 1, -32768, 32767], {"container": "RIFX"});
RIFX to RIFF and RIFF to RIFX:
wav.toRIFX();
wav.toRIFF();
IMA-ADPCM
16-bit 8000 Hz mono wave files can be compressed as IMA-ADPCM:
wav.toIMAADPCM();
IMA-ADPCM files compressed with wavefile will have a block align of 256 bytes.
If the audio is not 16-bit it will be converted to 16-bit before compressing. Compressing audio with sample rate different from 8000 Hz or more than one channel is not supported and will throw errors.
To decode 4-bit IMA-ADPCM as 16-bit linear PCM:
wav.fromIMAADPCM();
Decoding always result in 16-bit audio. To decode to another bit depth:
wav.fromIMAADPCM("24");
A-Law
16-bit wave files (mono or stereo) can be encoded as A-Law:
wav.toALaw();
If the audio is not 16-bit it will be converted to 16-bit before compressing.
To decode 8-bit A-Law as 16-bit linear PCM:
wav.fromALaw();
Decoding always result in 16-bit audio. To decode to another bit depth:
wav.fromALaw("24");
mu-Law
16-bit wave files (mono or stereo) can be encoded as mu-Law:
wav.toMuLaw();
If the audio is not 16-bit it will be converted to 16-bit before compressing.
To decode 8-bit mu-Law as 16-bit linear PCM:
wav.fromMuLaw();
Decoding always result in 16-bit audio. To decode to another bit depth:
wav.fromMuLaw("24");
Change the bit depth
You can change the bit depth of the audio with the toBitDepth(bitDepth) method.
let wav = new WaveFile(fs.readFileSync("32bit-file.wav"));
wav.toBitDepth("24");
fs.writeFileSync("24bit-file.wav", wav.toBuffer());
Add BWF metadata
To add BWF data to a file you can use the bext property:
let wav = new WaveFile(fs.readFileSync("32bit-file.wav"));
wav.bext.originator = "wavefile";
fs.writeFileSync("32bit-file-with-bext.wav", wav.toBuffer());
By default wavefile will not insert a "bext" chunk in new files or in files that do not already have a "bext" chunk unless a property of WaveFile.bext is changed from it's default value. See below the full list of properties in WaveFile.bext.
RF64
wavefile have limited support of RF64 files. It possible to read (at least some) RF64 files, but changing the bit depth or applying compression to the samples will result in a RIFF file.
API
To create a WaveFile object:
WaveFile(bytes=null);
The WaveFile methods
WaveFile.fromScratch(numChannels, sampleRate, bitDepth, samples, options={}) {}
WaveFile.fromBuffer(bytes) {}
WaveFile.toBuffer() {}
WaveFile.fromBase64(base64String) {}
WaveFile.toBase64() {}
WaveFile.toDataURI() {}
WaveFile.fromDataURI(dataURI) {}
WaveFile.toRIFF() {}
WaveFile.toRIFX() {}
WaveFile.toBitDepth(bitDepth, changeResolution=true) {}
WaveFile.toIMAADPCM() {}
WaveFile.fromIMAADPCM(bitDepth="16") {}
WaveFile.toALaw() {}
WaveFile.fromALaw(bitDepth="16") {}
WaveFile.toMuLaw() {}
WaveFile.fromMuLaw(bitDepth="16") {}
WaveFile.setTag(tag, value) {}
WaveFile.getTag(tag) {}
WaveFile.deleteTag(tag) {}
WaveFile.setCuePoint(position, labl="") {}
WaveFile.deleteCuePoint(index) {}
WaveFile.updateLabel(pointIndex, label) {}
The WaveFile properties
WaveFile.container = "";
WaveFile.chunkSize = 0;
WaveFile.format = "";
WaveFile.fmt = {
"chunkId": "",
"chunkSize": 0,
"audioFormat": 0,
"numChannels": 0,
"sampleRate": 0,
"byteRate": 0,
"blockAlign": 0,
"bitsPerSample": 0,
"cbSize": 0,
"validBitsPerSample": 0,
"dwChannelMask": 0,
"subformat": []
};
WaveFile.fact = {
"chunkId": "",
"chunkSize": 0,
"dwSampleLength": 0
};
WaveFile.cue = {
"chunkId": "",
"chunkSize": 0,
"dwCuePoints": 0,
"points": [],
};
WaveFile.smpl = {
"chunkId": "",
"chunkSize": 0,
"dwManufacturer": 0,
"dwProduct": 0,
"dwSamplePeriod": 0,
"dwMIDIUnityNote": 0,
"dwMIDIPitchFraction": 0,
"dwSMPTEFormat": 0,
"dwSMPTEOffset": 0,
"dwNumSampleLoops": 0,
"dwSamplerData": 0,
"loops": [],
};
WaveFile.bext = {
"chunkId": "",
"chunkSize": 0,
"description": "",
"originator": "",
"originatorReference": "",
"originationDate": "",
"originationTime": "",
"timeReference": [0, 0],
"version": 0,
"UMID": "",
"loudnessValue": 0,
"loudnessRange": 0,
"maxTruePeakLevel": 0,
"maxMomentaryLoudness": 0,
"maxShortTermLoudness": 0,
"reserved": "",
"codingHistory": ""
};
WaveFile.ds64 = {
"chunkId": "",
"chunkSize": 0,
"riffSizeHigh": 0,
"riffSizeLow": 0,
"dataSizeHigh": 0,
"dataSizeLow": 0,
"originationTime": 0,
"sampleCountHigh": 0,
"sampleCountLow": 0,
};
WaveFile.data = {
"chunkId": "",
"chunkSize": 0,
"samples": []
};
WaveFile.LIST = [];
WaveFile.junk = {
"chunkId": "",
"chunkSize": 0,
"chunkData": []
};
WaveFile.bitDepth = "";
Cue points
Items in cue.points are objects that look like this:
{
"dwName": 0,
"dwPosition": 0,
"fccChunk": 0,
"dwChunkStart": 0,
"dwBlockStart": 0,
"dwSampleOffset": 0,
}
Sample loops
Items in smpl.loops are objects that look like this:
{
"dwName": "",
"dwType": 0,
"dwStart": 0,
"dwEnd": 0,
"dwFraction": 0,
"dwPlayCount": 0,
}
LIST chunk
"LIST" chunk data is stored as follows:
WaveFile.LIST = [];
WaveFile.LIST is an array of objects with this signature:
{
"chunkId": "",
"chunkSize": 0,
"format": "",
"subChunks": []
};
Where "subChunks" are the subChunks of the "LIST" chunk. A single file may have many "LIST" chunks as long as their formats ("INFO", "adtl", etc) are not the same. wavefile can read and write "LIST" chunks of format "INFO" and "adtl".
For "LIST" chunks with the "INFO" format, "subChunks" will be an array of objects with this signature:
{
"chunkId": ""
"chunkSize" 0,
"value": ""
}
Where "chunkId" may be any RIFF tag:
https://sno.phy.queensu.ca/~phil/exiftool/TagNames/RIFF.html#Info
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)
Distribution
This library is a ES6 module also distributed as a CommonJS module, UMD module and a compiled script for browsers. It works out of the box in Node when installed with npm install wavefile
.
If you are using this lib in a browser:
You may load both wavefile.umd.js and wavefile.min.js in the browser with <script>
tags. Ideally you should use wavefile.min.js. You can load it via the https://unpkg.com and https://www.jsdelivr.com/ CDNs:
unpkg:
<script src="https://unpkg.com/wavefile"></script>
jsDelivr:
<script src="https://cdn.jsdelivr.net/npm/wavefile"></script>
If you are using this lib as a dependency:
-
The CommonJS is the dist file used by Node. It is served in the "main" field of package.json. It includes all the sources but no dependencies. Dependencies will be imported from the node_modules folder. This is the source you are running when you npm install wavefile.
-
The UMD module is compatible with Node, AMD and browsers. It is served in the "browser" field of package.json. It includes all dependencies. This file is not compiled/minified as it may be used by module bundlers. Compilation/minification should be up to the bundler consuming this file.
-
The compiled dist is browser-only and should be the one served by CDNs. It includes all the dependencies. It is used in the "unpkg" and "jsdelivr" fields of package.json.
-
The ES6 dist is wavefile.js, served as "es2015" in package.json. It includes all the dependencies. It is not compiled/minified.
-
./index.js is served as "module" in package.json. It should be used by systems that support ES modules and are aware of Node's module path resolution (a module bundler, for instance). This should be the entry point for bundlers in most cases - this will avoid code duplication in the case of shared dependencies (as opposed to using "browser" as the entry point).
If your module bundler is using "browser" as the entry point your dist should work the same but will be a larger file.
Contributing to wavefile
wavefile welcomes all contributions from anyone willing to work in good faith with other contributors and the community. No contribution is too small and all contributions are valued.
See CONTRIBUTING.md for details.
Style guide
wavefile code should follow the Google JavaScript Style Guide:
https://google.github.io/styleguide/jsguide.html
Code of conduct
This project is bound by a Code of Conduct: The Contributor Covenant, version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
References
Papers
https://tech.ebu.ch/docs/tech/tech3285.pdf
https://tech.ebu.ch/docs/tech/tech3306-2009.pdf
http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
https://www.loc.gov/preservation/digital/formats/fdd/fdd000356.shtml
http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/riffmci.pdf
https://sites.google.com/site/musicgapi/technical-documents/wav-file-format
http://www.neurophys.wisc.edu/auditory/riff-format.txt
https://sno.phy.queensu.ca/~phil/exiftool/TagNames/RIFF.html#Info
Software
https://github.com/erikd/libsndfile
https://gist.github.com/hackNightly/3776503
https://github.com/chirlu/sox/blob/master/src/wav.c
Other
https://developercertificate.org/
https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
https://google.github.io/styleguide/jsguide.html
Legal
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.