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!
Note: This is terribly similar to wav-decoder, which I found after making this. There are some minor differences in the API, but if you're fine with the one in wav-decoder, you should probably use that one.
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.
npm install --save super-tiny-wave-decoderimport { getWaveHeader, decodeWaveData } from 'super-tiny-wave-decoder'
// Read a wave file somehow
const waveContents = readAudioFileAsAnArrayBufferSomehow('sound.wav')
const waveData = new Uint8Array(waveContents)
// Get WAVE header
const header = getWaveHeader(waveData)
// Get a chunk of 4096 bytes starting after the header and
// decode that chunk
const audioDataChunk = waveData.slice(44, 44 + 4096)
const decodedAudio = decodeWaveData(audioDataChunk, header)
// decodedAudio is now a Float32Array ready to be used as an
// audio buffer- 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
Wraps and returns a typed array or ArrayBuffer in a DataView
Returns: DataView - A DataView wrapping the passed data
Throws:
TypeErrorThe passed data needs to be of a supported type
Params
data:Mixed- A DataView, ArrayBuffer, TypedArray or node Buffer
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
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:
ErrorThe passed data needs to be at least 44 bytes longErrorThe 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
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
Decodes a chunk of wave audio data
Returns: Float32Array - A Float32Array containing decoded audio
Throws:
ErrorThe 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
Do npm run to see what's available, and don't be shy sending a pull request!