uttori-audio-wave

Utility for reading & wrtiing WAV audio files.

Usage no npm install needed!

<script type="module">
  import uttoriAudioWave from 'https://cdn.skypack.dev/uttori-audio-wave';
</script>

README

view on npm npm module downloads Build Status Dependency Status Coverage Status

Uttori AudioWAV

Utility for reading, parsing and basic encoding for Waveform Audio File Format (WAVE / WAV) files.

Install

npm install --save uttori-audio-wave

Example

In this example we convert a valid 16 bit, 44.1kHz Wave file to be used with an SP-404SX by adding the appropriate header.

const fs = require('fs');
const { AudioWAV } = require('uttori-audio-wave');

// Read in a WAV file with AudioWAV
const data = fs.readFileSync('./test/assets/input.wav');
const input = AudioWAV.fromFile(data);
const { chunks } = input;

// Remove the header, we will make a new one with our new size.
chunks.splice(0, 1);

// Remove any existing RLND chunks, should be after `fmt `
const roland_index = chunks.findIndex((chunk) => chunk.type === 'roland');
if (roland_index > 0) {
  chunks.splice(roland_index, 1);
}

// Create a RLND chunk and set the pad to J12
const rlnd = AudioWAV.encodeRLND({ device: 'roifspsx', sampleIndex: 'J12' });

// Add the new RLND after the format chunk
const index = chunks.findIndex((chunk) => chunk.type === 'format');
chunks.splice(index + 1, 0, { type: 'roland', chunk: rlnd });

// Calculate the total size, include `WAVE` text (4 bytes)
const size = chunks.reduce((total, chunk) => {
  total += chunk.chunk.length;
  return total;
}, 4);

// Build the binary data
const header = AudioWAV.encodeHeader({ size });
const parts = chunks.reduce((arr, chunk) => {
  arr.push(Buffer.from(chunk.chunk));
  return arr;
}, [header]);
const output = Buffer.concat(parts);

// Write file, *.WAV as that is what the offical software uses.
fs.writeFileSync('./test/assets/output.WAV', output);

API Reference

AudioWAV

AudioWAV - WAVE Audio Utility

The WAVE file format is a subset of Microsoft's RIFF specification for the storage of multimedia files.

Kind: global class

new AudioWAV(list, [overrides])

Creates a new AudioWAV.

Param Type Default Description
list DataBufferList The DataBufferList of the audio file to process.
[overrides] object Options for this instance.
[overrides.size] number 16 ArrayBuffer byteLength for the underlying binary parsing.

Example (AudioWAV)

const data = fs.readFileSync('./audio.wav');
const file = AudioWAV.fromFile(data);
console.log('Chunks:', file.chunks);

audioWAV.parse()

Parse the WAV file, decoding the supported chunks.

Kind: instance method of AudioWAV

audioWAV.decodeChunk() ⇒ string

Decodes the chunk type, and attempts to parse that chunk if supported. Supported Chunk Types: IHDR, PLTE, IDAT, IEND, tRNS, pHYs

Chunk Structure: Length: 4 bytes Type: 4 bytes (IHDR, PLTE, IDAT, IEND, etc.) Chunk: {length} bytes CRC: 4 bytes

Kind: instance method of AudioWAV
Returns: string - Chunk Type
Throws:

  • Error Invalid Chunk Length when less than 0

See: Chunk Layout

AudioWAV.fromFile(data) ⇒ AudioWAV

Creates a new AudioWAV from file data.

Kind: static method of AudioWAV
Returns: AudioWAV - the new AudioWAV instance for the provided file data

Param Type Description
data Buffer The data of the image to process.

AudioWAV.fromBuffer(buffer) ⇒ AudioWAV

Creates a new AudioWAV from a DataBuffer.

Kind: static method of AudioWAV
Returns: AudioWAV - the new AudioWAV instance for the provided DataBuffer

Param Type Description
buffer DataBuffer The DataBuffer of the image to process.

AudioWAV.decodeHeader(chunk) ⇒ object

Decodes and validates WAV Header.

Signature (Decimal): [82, 73, 70, 70, ..., ..., ..., ..., 87, 65, 86, 69] Signature (Hexadecimal): [52, 49, 46, 46, ..., ..., ..., ..., 57, 41, 56, 45] Signature (ASCII): [R, I, F, F, ..., ..., ..., ..., W, A, V, E]

Kind: static method of AudioWAV
Returns: object - - The decoded values.
Throws:

  • Error Invalid WAV header, expected 'WAVE'
Param Type Description
chunk string | Buffer Data Blob

AudioWAV.encodeHeader(data) ⇒ Buffer

Enocdes JSON values to a valid Wave Header chunk Buffer.

Kind: static method of AudioWAV
Returns: Buffer - - The newley encoded fmt chunk.

Param Type Default Description
data object The values to encode to the header chunk chunk.
[data.riff] string "'RIFF'" RIFF Header, should contains the string RIFF, RF64, or BW64 in ASCII form.
data.size number This is the size of the entire file in bytes minus 8 bytes for the 2 fields not included in this count. RF64 sets this to -1 = 0xFFFFFFFF as it doesn't use this to support larger sizes in the DS64 chunk.
[data.format] string "'WAVE'" Contains the string WAVE in ASCII form

AudioWAV.decodeFMT(chunk) ⇒ object

Decode the FMT (Format) chunk. Should be the first chunk in the data stream.

Audio Format: 2 bytes Channels: 2 bytes Sample Rate: 4 bytes Byte Rate: 4 bytes Block Align: 2 bytes Bits per Sample 2 bytes [Extra Param Size] 2 bytes [Extra Params] n bytes

Kind: static method of AudioWAV
Returns: object - - The decoded values.

Param Type Description
chunk string | Buffer Data Blob

AudioWAV.encodeFMT([data]) ⇒ Buffer

Enocdes JSON values to a valid fmt chunk Buffer.

Defaults are set to Red Book Compact Disc Digital Audio (CDDA or CD-DA) / Audio CD standards.

RF64 specific fields are currently unsupported.

Kind: static method of AudioWAV
Returns: Buffer - - The newley encoded fmt chunk.

Param Type Default Description
[data] object {} The values to encode to the fmt chunk.
[data.audioFormat] number 1 Format of the audio data, 1 is PCM and values other than 1 indicate some form of compression. See decodeFMT for a listing
[data.channels] number 2 Mono = 1, Stereo = 2, etc.
[data.sampleRate] number 44100 8000, 44100, 96000, etc.
[data.byteRate] number 176400 Sample Rate * Channels * Bits per Sample / 8
[data.blockAlign] number 4 The number of bytes for one sample including all channels. Channels * Bits per Sample / 8
[data.bitsPerSample] number 16 8 bits = 8, 16 bits = 16, etc.
[data.extraParamSiz] number 0 The size of the extra paramteres to follow, or 0.
[data.extraParams] number 0 Any extra data to encode.

AudioWAV.decodeLIST(chunk) ⇒ object

Decode the LIST (LIST Information) chunk.

A LIST chunk defines a list of sub-chunks and has the following format.

Kind: static method of AudioWAV
Returns: object - - The decoded values.

Param Type Description
chunk string | Buffer Data Blob

AudioWAV.decodeLISTINFO(list) ⇒ object

Decode the LIST INFO chunks.

Kind: static method of AudioWAV
Returns: object - - The parsed list.

Param Type Description
list DataStream List DataStream

AudioWAV.decodeLISTadtl(list) ⇒ object

Decode the LIST adtl chunks.

Kind: static method of AudioWAV
Returns: object - - The parsed list.

Param Type Description
list DataStream List DataStream

AudioWAV.decodeDATA(chunk)

Decode the data (Audio Data) chunk.

Kind: static method of AudioWAV

Param Type Description
chunk string | Buffer Data Blob

AudioWAV.decodeRLND(chunk) ⇒ object

Decode the RLND (Roland) chunk.

Useful for use on SP-404 / SP-404SX / SP-404A samplers, perhaps others.

This chunk is sized and padded with zeros to ensure that the the sample data starts exactly at offset 512.

Kind: static method of AudioWAV
Returns: object - - The decoded values.

Param Type Description
chunk string | Buffer Data Blob

AudioWAV.encodeRLND(data) ⇒ Buffer

Enocdes JSON values to a valid RLND (Roland) chunk Buffer.

Useful for use on SP-404 / SP-404SX / SP-404A samplers, perhaps others.

The unknown value may be an unsigned 32bit integer.

This chunk is sized and padded with zeros to ensure that the the sample data starts exactly at offset 512.

Kind: static method of AudioWAV
Returns: Buffer - - The new RLND chunk.
See: SP-404SX Support Page

Param Type Default Description
data object The JSON values to set in the RLND chunk.
data.device string An 8 character string representing the device label. SP-404SX Wave Converter v1.01 on macOS sets this value to roifspsx.
[data.unknown1] number 4 Unknown, SP-404SX Wave Converter v1.01 on macOS sets this value to 0x04.
[data.unknown2] number 0 Unknown, SP-404SX Wave Converter v1.01 on macOS sets this value to 0x00.
[data.unknown3] number 0 Unknown, SP-404SX Wave Converter v1.01 on macOS sets this value to 0x00.
[data.unknown4] number 0 Unknown, SP-404SX Wave Converter v1.01 on macOS sets this value to 0x00.
data.sampleIndex number | string The pad the sample plays on, between 0 and 119 as a number or the pad label, A1 - J12. Only the SP404SX (device === roifspsx) provided values can be converted from string corrently, and if it is not found it will defailt to 0 / A1.

AudioWAV.decodeJUNK(chunk)

Decode the JUNK (Padding) chunk.

To align RIFF chunks to certain boundaries (i.e. 2048 bytes for CD-ROMs) the RIFF specification includes a JUNK chunk. The contents are to be skipped when reading. When writing RIFFs, JUNK chunks should not have an odd Size.

Kind: static method of AudioWAV

Param Type Description
chunk string | Buffer Data Blob

AudioWAV.decodeBEXT(chunk) ⇒ object

Decode the bext (Broadcast Wave Format (BWF) Broadcast Extension) chunk.

Kind: static method of AudioWAV
Returns: object - - The decoded values.
See

Param Type Description
chunk string | Buffer Data Blob

AudioWAV.decodeCue(chunk) ⇒ object

Decode the 'cue ' (Cue Points) chunk.

A cue chunk specifies one or more sample offsets which are often used to mark noteworthy sections of audio. For example, the beginning and end of a verse in a song may have cue points to make them easier to find. The cue chunk is optional and if included, a single cue chunk should specify all cue points for the "WAVE" chunk. No more than one cue chunk is allowed in a "WAVE" chunk.

Kind: static method of AudioWAV
Returns: object - - The decoded values.
See: Cue Chunk

Param Type Description
chunk string | Buffer Data Blob

AudioWAV.decodeResU(chunk) ⇒ object

Decode the 'ResU' chunk, a ZIP compressed JSON Data containg Time Signature, Tempo and other data for Logic Pro X.

Kind: static method of AudioWAV
Returns: object - - The decoded values.

Param Type Description
chunk string | Buffer Data Blob

AudioWAV.decodeDS64(chunk) ⇒ object

DataSize 64 Parsing

Kind: static method of AudioWAV
Returns: object - - The decoded values.
See: RF64: An extended File Format for Audio

Param Type Description
chunk string | Buffer Data Blob

Tests

To run the test suite, first install the dependencies, then run npm test:

npm install
npm test
DEBUG=Uttori* npm test

Contributors

Thanks

License