1.1.0 • Published 5 years ago

uttori-audio-wave v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

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.

ParamTypeDefaultDescription
listDataBufferListThe DataBufferList of the audio file to process.
overridesobjectOptions for this instance.
overrides.sizenumber16ArrayBuffer 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

ParamTypeDescription
dataBufferThe 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

ParamTypeDescription
bufferDataBufferThe 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'
ParamTypeDescription
chunkstring | BufferData 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.

ParamTypeDefaultDescription
dataobjectThe values to encode to the header chunk chunk.
data.riffstring"'RIFF'"RIFF Header, should contains the string RIFF, RF64, or BW64 in ASCII form.
data.sizenumberThis 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.formatstring"'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.

ParamTypeDescription
chunkstring | BufferData 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.

ParamTypeDefaultDescription
dataobject{}The values to encode to the fmt chunk.
data.audioFormatnumber1Format of the audio data, 1 is PCM and values other than 1 indicate some form of compression. See decodeFMT for a listing
data.channelsnumber2Mono = 1, Stereo = 2, etc.
data.sampleRatenumber441008000, 44100, 96000, etc.
data.byteRatenumber176400Sample Rate Channels Bits per Sample / 8
data.blockAlignnumber4The number of bytes for one sample including all channels. Channels * Bits per Sample / 8
data.bitsPerSamplenumber168 bits = 8, 16 bits = 16, etc.
data.extraParamSiznumber0The size of the extra paramteres to follow, or 0.
data.extraParamsnumber0Any 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.

ParamTypeDescription
chunkstring | BufferData Blob

AudioWAV.decodeLISTINFO(list) ⇒ object

Decode the LIST INFO chunks.

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

ParamTypeDescription
listDataStreamList DataStream

AudioWAV.decodeLISTadtl(list) ⇒ object

Decode the LIST adtl chunks.

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

ParamTypeDescription
listDataStreamList DataStream

AudioWAV.decodeDATA(chunk)

Decode the data (Audio Data) chunk.

Kind: static method of AudioWAV

ParamTypeDescription
chunkstring | BufferData 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.

ParamTypeDescription
chunkstring | BufferData 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

ParamTypeDefaultDescription
dataobjectThe JSON values to set in the RLND chunk.
data.devicestringAn 8 character string representing the device label. SP-404SX Wave Converter v1.01 on macOS sets this value to roifspsx.
data.unknown1number4Unknown, SP-404SX Wave Converter v1.01 on macOS sets this value to 0x04.
data.unknown2number0Unknown, SP-404SX Wave Converter v1.01 on macOS sets this value to 0x00.
data.unknown3number0Unknown, SP-404SX Wave Converter v1.01 on macOS sets this value to 0x00.
data.unknown4number0Unknown, SP-404SX Wave Converter v1.01 on macOS sets this value to 0x00.
data.sampleIndexnumber | stringThe 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

ParamTypeDescription
chunkstring | BufferData 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

ParamTypeDescription
chunkstring | BufferData 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

ParamTypeDescription
chunkstring | BufferData 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.

ParamTypeDescription
chunkstring | BufferData 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

ParamTypeDescription
chunkstring | BufferData 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

1.1.0

5 years ago

1.0.0

5 years ago