Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alexanderwallin/super-tiny-wave-decoder

📻 A super tiny WAVE parser and decoder, without any dependencies or unnecessary abstraction layers
https://github.com/alexanderwallin/super-tiny-wave-decoder

audio decoder tiny wave

Last synced: about 2 months ago
JSON representation

📻 A super tiny WAVE parser and decoder, without any dependencies or unnecessary abstraction layers

Awesome Lists containing this project

README

        

# super-tiny-wave-decoder

[![npm version](https://badge.fury.io/js/super-tiny-wave-decoder.svg)](https://badge.fury.io/js/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](https://github.com/audiocogs/aurora.js), so lots of ❤️ from here to there!

**Note:** This is terribly similar to [`wav-decoder`](https://github.com/mohayonao/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.

## 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

```sh
npm install --save super-tiny-wave-decoder
```

## Usage

```js
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!