@uttori/audio-wave
v2.1.0
Published
Utility for reading & wrtiing WAV audio files.
Downloads
9
Maintainers
Readme
Uttori AudioWAV
Utility for reading, parsing and basic encoding for Waveform Audio File Format (WAVE / WAV) files. Check out the demo to explore the chunks for a given WAV file in the browser.
AudioWAV supports parsing the following WAVE chunks:
acid
- ACID Loop Chunkbext
- Broadcast Wave Format (BWF) Broadcast Extension Chunkcue
- Cue Points Chunkdata
- Audio Data ChunkDISP
- Display Chunkds64
- DataSize 64 Chunkfact
- Additional Data Chunkfmt
- Format Chunk (Encode Avaliable)inst
- Instrument ChunkJUNK
- Garbage ChunksLIST
- LIST Information ChunkResU
- Logic Pro X ChunkRLND
- Roland Sampler Chunk (Encode Avaliable)smpl
- Sample Chunktlst
- Trigger List Chunk
Install
npm install --save @uttori/audio-wave
Configuration
{
// This keeps data chunks even and in spec, however some files fail with this.
roundOddChunks: true,
}
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 { chunks } = AudioWAV.fromFile(data);
// 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
- AudioWAV
- new AudioWAV(list, [overrides], opts)
- instance
- .parse()
- .decodeChunk() ⇒ string
- static
- .fromFile(data, options) ⇒ AudioWAV
- .fromBuffer(buffer, options) ⇒ AudioWAV
- .decodeHeader(chunk) ⇒ object
- .encodeHeader(data) ⇒ Buffer
- .decodeFMT(chunk) ⇒ object
- .encodeFMT([data]) ⇒ Buffer
- .decodeLIST(chunk) ⇒ object
- .decodeLISTINFO(list) ⇒ object
- .decodeLISTadtl(list) ⇒ object
- .decodeDATA(chunk)
- .decodeTLST(chunk) ⇒ object
- .decodeFACT(chunk) ⇒ object
- .decodePEAK(chunk) ⇒ object
- .decodeDISP(chunk) ⇒ object
- .decodeACID(chunk) ⇒ object
- .decodeINST(chunk) ⇒ object
- .decodeSMPL(chunk) ⇒ object
- .decodeRLND(chunk) ⇒ object
- .encodeRLND(data) ⇒ Buffer
- .decodeJUNK(chunk, options)
- .decodePAD(chunk)
- .decodeBEXT(chunk, options) ⇒ object
- .decodeCue(chunk) ⇒ object
- .decodeResU(chunk) ⇒ object
- .decodeDS64(chunk) ⇒ object
- .decodeSTRC(chunk) ⇒ object
new AudioWAV(list, [overrides], opts)
Creates a new AudioWAV.
| Param | Type | Default | Description | | --- | --- | --- | --- | | list | DataBufferList | | The DataBufferList of the audio file to process. | | [overrides] | object | | Options for this DataStream instance. | | [overrides.size] | number | 16 | ArrayBuffer byteLength for the underlying binary parsing. | | opts | object | | Options for this AudioWAV instance. |
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: fmt
, fact
, inst
, DISP
, smpl
, tlst
, data
, LIST
, RLND
, JUNK
, acid
, cue
, bext
, ResU
, ds64
, cart
Chunk Structure: Length: 4 bytes (integer) Type: 4 bytes (string) Chunk: {length} 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, options) ⇒ 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. | | options | object | Options for returned AudioWAV instance. |
AudioWAV.fromBuffer(buffer, options) ⇒ 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. | | options | object | Options for returned AudioWAV instance. |
AudioWAV.decodeHeader(chunk) ⇒ object
Decodes and validates WAV Header.
Checks for RIFF
/ RF64
/ BW64
header, reads the size, and then checks for the `WAVE 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
| 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 header 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'" | WAVE Header, 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.audioFormatValue] | 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.decodeTLST(chunk) ⇒ object
Decode the tlst
(Trigger List) chunk.
Used in Sound Forge by Sonic Foundry
Specifies a list of triggers which can be used to trigger playback of a series of cue points or Playlist entries.
There's a historical bug in dwName (which is in fact an index, and the bug is that it's actually Index-1).
Kind: static method of AudioWAV
Returns: object - The decoded values.
| Param | Type | Description | | --- | --- | --- | | chunk | string | Buffer | Data Blob |
AudioWAV.decodeFACT(chunk) ⇒ object
Decode the fact chunk.
Fact chunks exist in all wave files that are compressed or that have a wave list chunk. A fact chunk is not required in an uncompressed PCM file that does not have a wave list chunk.
According to the fact chunk's initial specification, the data portion of the fact chunk will contain only one 4-byte number that specifies the number of samples in the data chunk of the Wave file. This number, when combined with the samples per second value in the format chunk of the Wave file, can be used to compute the length of the audio data in seconds.
Kind: static method of AudioWAV
Returns: object - The decoded values.
See
| Param | Type | Description | | --- | --- | --- | | chunk | string | Buffer | Data Blob |
AudioWAV.decodePEAK(chunk) ⇒ object
Decode the PEAK chunk.
Kind: static method of AudioWAV
Returns: object - The decoded values.
See: awesome-wav - WAVFormat.wiki
| Param | Type | Description | | --- | --- | --- | | chunk | string | Buffer | Data Blob |
AudioWAV.decodeDISP(chunk) ⇒ object
Decode the DISP (Display) chunk.
The DISP chunk should be used as a direct child of the RIFF chunk so that any RIFF aware application can find it. There can be multiple DISP chunks with each containing different types of displayable data, but all representative of the same object. The DISP chunks should be stored in the file in order of preference (just as in the clipboard).
The DISP chunk is especially beneficial when representing OLE data within an application. For example, when pasting a wave file into Excel, the creating application can use the DISP chunk to associate an icon and a text description to represent the embedded wave file. This text should be short so that it can be easily displayed in menu bars and under icons. Note: do not use a CF_TEXT for a description of the data. Bibliographic data chunks will be added to support the standard MARC (Machine Readable Cataloging) data.
Kind: static method of AudioWAV
Returns: object - The decoded values.
See
| Param | Type | Description | | --- | --- | --- | | chunk | string | Buffer | Data Blob |
AudioWAV.decodeACID(chunk) ⇒ object
ACID Loop File Format
They were originally created for use with Acid, the loop-based, music-sequencing software, created by Sonic Foundry in 1998.
"Acidized" loops contain tempo and key information, so that Acid and other programs that can read the "acidization" can properly time stretch and pitch shift them.
Although the phrase "ACID loops" technically only refers to loops which have been "acidized", some people use the term to refer to loops in general, even when used with other software packages.
Kind: static method of AudioWAV
Returns: object - The decoded values.
| Param | Type | Description | | --- | --- | --- | | chunk | string | Buffer | Data Blob |
AudioWAV.decodeINST(chunk) ⇒ object
Decode the inst (Instrumet) chunk.
When a wave file is used as wave samples in a MIDI synthesizer, the instrument chunk helps the MIDI synthesizer define the sample pitch & relative volume of the samples.
Kind: static method of AudioWAV
Returns: object - The decoded values.
| Param | Type | Description | | --- | --- | --- | | chunk | string | Buffer | Data Blob |
AudioWAV.decodeSMPL(chunk) ⇒ object
Decode the smpl (Sample) chunk.
The sample chunk allows a MIDI sampler to use the Wave file as a collection of samples.
Kind: static method of AudioWAV
Returns: object - The decoded values.
| 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, options)
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 | | options | object | Decoding options. | | options.roundOddChunks | boolean | When true we will round odd chunk sizes up to keep in spec. |
AudioWAV.decodePAD(chunk)
Decode the PAD
(Padding) chunk.
Kind: static method of AudioWAV
| Param | Type | Description | | --- | --- | --- | | chunk | string | Buffer | Data Blob |
AudioWAV.decodeBEXT(chunk, options) ⇒ 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 | | options | object | Decoding options. | | options.roundOddChunks | boolean | When true we will round odd chunk sizes up to keep in spec. |
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 |
AudioWAV.decodeSTRC(chunk) ⇒ object
Decode the STRC (ACID Related) chunk.
When a wave file is used as wave samples in a MIDI synthesizer, the instrument chunk helps the MIDI synthesizer define the sample pitch & relative volume of the samples.
Kind: static method of AudioWAV
Returns: object - The decoded values.
| 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
- Paul Battley - His Roland SP-404SX sample file format was a huge help.