wavefile-creator
Copyright (c) 2019 Rafael da Silva Rocha.
https://github.com/rochars/wavefile-creator
Create, read and write wav files.
Install
npm install wavefile-creator
Use
Node
const wavefileCreator = ;let wav = ;
or
const WaveFileCreator = WaveFileCreator;let wav = ;
or
;let wav = ;
Node.js Example
const WaveFileCreator = WaveFileCreator; // Load a wav file buffer as a WaveFileCreator objectlet wav = ;wav; // Check some of the file propertiesconsole;console;console; // toBuffer() return a wav file buffer// ready to be written to disk...let theFile = wav; // ...or to be loaded by another WaveFileCreator objectlet wav2 = theFile;
Browser
Use the wavefile-creator.js file in the dist folder:
Or load it from the jsDelivr CDN:
Or load it from unpkg:
Create wave files from scratch
Use the fromScratch(numChannels, sampleRate, bitDepth, samples)
method.
Mono:
let wav = ; // Create a mono wave file, 44.1 kHz, 32-bit and 4 sampleswav;fs;
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.
// Stereo, 48 kHz, 8-bit, de-interleaved samples// WaveFile interleave the samples automaticallywav;fs;
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
API
The WaveFileCreator methods
/** * Set up the WaveFileCreator object from a byte buffer. * @param * @param * @throws * @throws * @throws */WaveFileCreator {} /** * Return a byte buffer representig the WaveFileCreator object as a .wav file. * The return value of this method can be written straight to disk. * @return */WaveFileCreator {} /** * Set up the WaveFileCreator object based on the arguments passed. * Existing chunks are reset. * @param * (Integer numbers: 1 for mono, 2 stereo and so on). * @param * Integer numbers like 8000, 44100, 48000, 96000, 192000. * @param * One of '4', '8', '8a', '8m', '16', '24', '32', '32f', '64' * or any value between '8' and '32' (like '12'). * @param {!Array<number>|!Array<!Array<number>>|!TypedArray} samples * The samples. Must be in the correct range according to the bit depth. * @param * as RIFX with {'container': 'RIFX'} * @throws */ {}
The WaveFileCreator properties
/** * The container identifier. * "RIFF", "RIFX" and "RF64" are supported. * @type */WaveFileCreatorcontainer = '';/** * @type */WaveFileCreatorchunkSize = 0;/** * The format. * Always 'WAVE'. * @type */WaveFileCreatorformat = '';/** * The data of the "fmt" chunk. * @type {!Object<string, *>} */WaveFileCreatorfmt = /** @type */ chunkId: '' /** @type */ chunkSize: 0 /** @type */ audioFormat: 0 /** @type */ numChannels: 0 /** @type */ sampleRate: 0 /** @type */ byteRate: 0 /** @type */ blockAlign: 0 /** @type */ bitsPerSample: 0 /** @type */ cbSize: 0 /** @type */ validBitsPerSample: 0 /** @type */ dwChannelMask: 0 /** * 4 32-bit values representing a 128-bit ID * @type */ subformat: ;/** * The data of the "fact" chunk. * @type {!Object<string, *>} */WaveFileCreatorfact = /** @type */ chunkId: '' /** @type */ chunkSize: 0 /** @type */ dwSampleLength: 0;/** * The data of the "cue " chunk. * @type {!Object<string, *>} */WaveFileCreatorcue = /** @type */ chunkId: '' /** @type */ chunkSize: 0 /** @type */ dwCuePoints: 0 /** @type {!Array<!Object>} */ points: ;/** * The data of the "smpl" chunk. * @type {!Object<string, *>} */WaveFileCreatorsmpl = /** @type */ chunkId: '' /** @type */ chunkSize: 0 /** @type */ dwManufacturer: 0 /** @type */ dwProduct: 0 /** @type */ dwSamplePeriod: 0 /** @type */ dwMIDIUnityNote: 0 /** @type */ dwMIDIPitchFraction: 0 /** @type */ dwSMPTEFormat: 0 /** @type */ dwSMPTEOffset: 0 /** @type */ dwNumSampleLoops: 0 /** @type */ dwSamplerData: 0 /** @type {!Array<!Object>} */ loops: ;/** * The data of the "bext" chunk. * @type {!Object<string, *>} */WaveFileCreatorbext = /** @type */ chunkId: '' /** @type */ chunkSize: 0 /** @type */ description: '' //256 /** @type */ originator: '' //32 /** @type */ originatorReference: '' //32 /** @type */ originationDate: '' //10 /** @type */ originationTime: '' //8 /** * 2 32-bit values, timeReference high and low * @type */ timeReference: 0 0 /** @type */ version: 0 //WORD /** @type */ UMID: '' // 64 chars /** @type */ loudnessValue: 0 //WORD /** @type */ loudnessRange: 0 //WORD /** @type */ maxTruePeakLevel: 0 //WORD /** @type */ maxMomentaryLoudness: 0 //WORD /** @type */ maxShortTermLoudness: 0 //WORD /** @type */ reserved: '' //180 /** @type */ codingHistory: '' // string, unlimited;/** * The data of the "ds64" chunk. * Used only with RF64 files. * @type {!Object<string, *>} */WaveFileCreatords64 = /** @type */ chunkId: '' /** @type */ chunkSize: 0 /** @type */ riffSizeHigh: 0 // DWORD /** @type */ riffSizeLow: 0 // DWORD /** @type */ dataSizeHigh: 0 // DWORD /** @type */ dataSizeLow: 0 // DWORD /** @type */ originationTime: 0 // DWORD /** @type */ sampleCountHigh: 0 // DWORD /** @type */ sampleCountLow: 0 // DWORD /** @type */ //"tableLength": 0, // DWORD /** @type */ //"table": [];/** * The data of the "data" chunk. * @type {!Object<string, *>} */WaveFileCreatordata = /** @type */ chunkId: '' /** @type */ chunkSize: 0 /** @type */ samples: 0;/** * The data of the "LIST" chunks. * Each item in this list look like this: * { * chunkId: '', * chunkSize: 0, * format: '', * subChunks: [] * } * @type {!Array<!Object>} */WaveFileCreatorLIST = ;/** * The data of the "junk" chunk. * @type {!Object<string, *>} */WaveFileCreatorjunk = /** @type */ chunkId: '' /** @type */ chunkSize: 0 /** @type */ chunkData: ;/** * The bit depth code according to the samples. * @type */WaveFileCreatorbitDepth = '';
Cue points
Items in cue.points are objects like this:
/** @type */ dwName: 0 // a cue point ID /** @type */ dwPosition: 0 /** @type */ fccChunk: 0 /** @type */ dwChunkStart: 0 /** @type */ dwBlockStart: 0 /** @type */ dwSampleOffset: 0
Sample loops
Items in smpl.loops are objects like this:
/** @type */ dwName: '' // a cue point ID /** @type */ dwType: 0 /** @type */ dwStart: 0 /** @type */ dwEnd: 0 /** @type */ dwFraction: 0 /** @type */ dwPlayCount: 0
LIST chunk
"LIST" chunk data is stored as follows:
/** * An array of the "LIST" chunks present in the file. * @type {!Array<!Object>} */WaveFileCreatorLIST = ;
Items in WaveFileCreator.LIST are objects like this:
/** @type */ chunkId: '' // always 'LIST' /** @type */ chunkSize: 0 /** @type */ format: '' // 'adtl' or 'INFO' /** @type {!Array<!Object>} */ 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-creator can read "LIST" chunks of format "INFO" and "adtl".
For "LIST" chunks with the "INFO" format, "subChunks" will be an array of objects like this:
/** @type */ chunkId: '' // some RIFF tag /** @type */ chunkSize 0 /** @type */ value: ''
Where "chunkId" may be any RIFF tag:
https://sno.phy.queensu.ca/~phil/exiftool/TagNames/RIFF.html#Info
Contributing to wavefile-creator
wavefile-creator 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-creator 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, also 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
LICENSE
Copyright (c) 2019 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.