wav-reader-ts
v1.0.6
Published
more complete promise-based wav decoder
Downloads
41
Maintainers
Readme
wav-decoder-ts
promise-based wav decoder
Installation
npm install wav-decoder-ts
API
decode(src: ArrayBuffer, [opts: object]): Promise<{data: AudioData, format: Format}>
- if provide an instance of
Buffer
, it is converted toArrayBuffer
likeUint8Array.from(src).buffer
implicitly. opts.symmetric
decode to symmetrical values
- if provide an instance of
decode.sync(src: ArrayBuffer, [opts: object]): {data: AudioData, format: Format}
- synchronous version
Returns
interface AudioData {
sampleRate: number;
channelData: Float32Array[];
}
interface Format {
formatId: number;
floatingPoint: boolean;
numberOfChannels: number;
sampleRate: number;
byteRate: number;
blockSize: number;
bitDepth: number;
}
Usage
const fs = require("fs");
const WavDecoder = require("wav-decoder");
const readFile = (filepath) => {
return new Promise((resolve, reject) => {
fs.readFile(filepath, (err, buffer) => {
if (err) {
return reject(err);
}
return resolve(buffer);
});
});
};
readFile("foobar.wav").then((buffer) => {
return WavDecoder.decode(buffer);
}).then(function(wavData) {
console.log(wavData.data.sampleRate);
console.log(wavData.format.bitDepth);
console.log(wavData.data.channelData[0]); // Float32Array
console.log(wavData.data.channelData[1]); // Float32Array
});
License
MIT