super-tiny-wave-decoder
A super tiny WAVE parser and decoder, without any dependencies or unnecessary abstraction layers.
Most of the logic was directly ported from parts of aurora.js, so lots of ❤️ from here to there!
Why?
Sometimes I just want to decode WAVE audio. Sometimes I even just want to parse a WAVE header. All libraries I have found have either too much other stuff I don't need, too abstraced APIs (I don't need it to make the HTTP request for me) or is specifically targeted towards either Node or the web browser.
This library is just a utility that parses WAVE headers and decodes WAVE audio. Nothing more, nothing less.
Installation
npm install --save super-tiny-wave-decoder
Usage
// Read a wave file somehowconst waveContents = const waveData = waveContents // Get WAVE headerconst header = // Get a chunk of 4096 bytes starting after the header and// decode that chunkconst audioDataChunk = waveDataconst decodedAudio = // decodedAudio is now a Float32Array ready to be used as an// audio buffer
API
Functions
- getAsDataView(data) ⇒
DataView
Wraps and returns a typed array or ArrayBuffer in a DataView
- getString(data, offset, bytes, encoding) ⇒
String
Returns a string read from some data.
This code is directly ported and rewritten from aurora.js's stream.coffee. I have no idea how it works! Might use a library for this if it doesn't work properly.
- getWaveHeader(data) ⇒
Object
Parses a chunk of wave data and tries to extract all wave header info from it.
You can see an overview of the WAVE header format here: http://www.topherlee.com/software/pcm-tut-wavformat.html
- getWaveDuration(header) ⇒
Number
Returns the duration of some wave audio given its header info
- decodeWaveData(header, data) ⇒
Float32Array
Decodes a chunk of wave audio data
DataView
getAsDataView(data) ⇒ Wraps and returns a typed array or ArrayBuffer in a DataView
Returns: DataView
- A DataView wrapping the passed data
Throws:
TypeError
The passed data needs to be of a supported type
Params
data
:Mixed
- A DataView, ArrayBuffer, TypedArray or node Buffer
String
getString(data, offset, bytes, encoding) ⇒ Returns a string read from some data.
This code is directly ported and rewritten from aurora.js's stream.coffee. I have no idea how it works! Might use a library for this if it doesn't work properly.
Returns: String
- A string
Link: https://github.com/audiocogs/aurora.js/blob/master/src/core/stream.coffee#L303
Params
data
:Mixed
- A DataView, ArrayBuffer, TypedArray or node Bufferoffset
:Number
- An offset in bytes to start read the string frombytes
:Number
- The number of bytes to readencoding
:String
- The encoding to parse the text as
Object
getWaveHeader(data) ⇒ Parses a chunk of wave data and tries to extract all wave header info from it.
You can see an overview of the WAVE header format here: http://www.topherlee.com/software/pcm-tut-wavformat.html
Returns: Object
- An object containing the WAVE header info
Throws:
Error
The passed data needs to be at least 44 bytes longError
The passed data needs to match the WAVE header spec
Params
data
:Mixed
- A DataView, ArrayBuffer, TypedArray or node Buffer containing WAVE audio data, more specifically the beginning of a WAVE audio file
Number
getWaveDuration(header) ⇒ Returns the duration of some wave audio given its header info
Returns: Number
- The duration of the WAVE audio in seconds
Params
header
:Object
- A WAVE header object
Float32Array
decodeWaveData(header, data) ⇒ Decodes a chunk of wave audio data
Returns: Float32Array
- A Float32Array containing decoded audio
Throws:
Error
The bit depth passed in header must be 8, 16, 24, 32 or 64
Params
header
:Object
- A WAVE header objectdata
:Mixed
- A DataView, ArrayBuffer, TypedArray or node Buffer containing WAVE audio data
Contributing
Do npm run
to see what's available, and don't be shy sending a pull request!