super-tiny-wave-decoder

0.1.3 • Public • Published

super-tiny-wave-decoder

npm version

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

import { 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

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

getAsDataView(data) ⇒ DataView

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

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.

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 Buffer
  • offset: Number - An offset in bytes to start read the string from
  • bytes: Number - The number of bytes to read
  • encoding: String - The encoding to parse the text as

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

Returns: Object - An object containing the WAVE header info
Throws:

  • Error The passed data needs to be at least 44 bytes long
  • Error 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

getWaveDuration(header) ⇒ Number

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

decodeWaveData(header, data) ⇒ Float32Array

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 object
  • data: 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!

Package Sidebar

Install

npm i super-tiny-wave-decoder

Weekly Downloads

0

Version

0.1.3

License

ISC

Last publish

Collaborators

  • alexander_wallin