wav-file-decoder
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

wav-file-decoder

A simple decoder for WAV audio files.

NPM package: wav-file-decoder
Online demo: www.source-code.biz/snippets/typescript/wavFileDecoder
Examples of how to use it: github.com/chdh/wav-file-decoder/tree/main/test/src
Compagnion package: wav-file-encoder

API

Test if a file is a WAV file

function isWavFile (fileData: ArrayBufferView | ArrayBuffer) : boolean
  • fileData: An ArrayBufferView (e.g. Uint8Array or Node.js Buffer) or an ArrayBuffer that contains the raw data bytes of a WAV file.
  • Return value: true if the file looks like a valid and supported WAV file.

Decode a WAV file

function decodeWavFile (fileData: ArrayBufferView | ArrayBuffer) : AudioData
  • fileData: An ArrayBufferView (e.g. Uint8Array or Node.js Buffer) or an ArrayBuffer that contains the raw data bytes of a WAV file.
  • Return value: A data structure containing the decoded WAV file data.
interface AudioData {
  channelData:      Float32Array[]; // arrays containing the audio samples (PCM data), one array per channel
  sampleRate:       number;         // sample rate (samples per second)
  numberOfChannels: number;         // number of channels, same as channelData.length
  audioEncoding:    AudioEncoding;  // audio encoding in the WAV file (int or float)
  bitsPerSample:    number;         // number of bits per sample in the WAV file
  wavFileTypeName:  string;         // combination of audioEncoding and bitsPerSample, e.g. "int16" or "float32"
}
enum AudioEncoding {
  pcmInt,                           // 0 = PCM integer
  pcmFloat                          // 1 = PCM float
}

The audio sample values in the channelData arrays are within the range -1 to +1.
An exception is thrown when the passed file is not a WAV file or when the format of the WAV file is not supported.

Package Sidebar

Install

npm i wav-file-decoder

Weekly Downloads

6

Version

1.0.3

License

MIT

Unpacked Size

14.5 kB

Total Files

5

Last publish

Collaborators

  • chdh