audio

@audio/decode4.4.0

Decode audio data in node or browser

Status
calibrated
Updated
Family
24 packages under it: aac, ac3, aiff, amr, ape, avi, caf, dsd, dts, eac3, flac, gsm, mod, mp3, mp4, mpc, opus, qoa, tta, vorbis, wav, wavpack, webm, wma
Category
codecs
Licence
MIT
Dependencies
33 packages; 12 not permissive:
@audio/decode-aac LGPL-2.1-or-later AND Apache-2.0
@audio/decode-ape LGPL-2.1-or-later
@audio/decode-dts LGPL-2.1-or-later
@audio/decode-eac3 LGPL-2.1-or-later
@audio/decode-flac MIT AND BSD-3-Clause AND LGPL-3.0-or-later AND Zlib
@audio/decode-mp3 MIT AND LGPL-2.1-only AND Zlib
@audio/decode-opus MIT AND BSD-3-Clause AND LGPL-3.0-or-later
@audio/decode-tta MIT AND BSD-3-Clause AND LGPL-2.1-or-later
@audio/decode-vorbis MIT AND BSD-3-Clause AND LGPL-3.0-or-later AND Zlib
@audio/decode-webm MIT AND BSD-3-Clause AND LGPL-3.0-or-later AND Zlib
@audio/decode-wma LGPL-2.1-or-later
codec-parser LGPL-3.0-or-later
Downloads
624,248 last month
Tests
at least 88 in audiojs/decode's CI, passed at 58c36d8,

Licences

npm i @audio/decode installs 33 packages besides it. By licence, those not permissive first:

LicencePermissivePackages
LGPL-2.1-or-laterno@audio/decode-ape, @audio/decode-dts, @audio/decode-eac3, @audio/decode-wma
MIT AND BSD-3-Clause AND LGPL-3.0-or-later AND Zlibno@audio/decode-flac, @audio/decode-vorbis, @audio/decode-webm
LGPL-2.1-or-later AND Apache-2.0no@audio/decode-aac
LGPL-3.0-or-laternocodec-parser
MIT AND BSD-3-Clause AND LGPL-2.1-or-laterno@audio/decode-tta
MIT AND BSD-3-Clause AND LGPL-3.0-or-laterno@audio/decode-opus
MIT AND LGPL-2.1-only AND Zlibno@audio/decode-mp3
MITyes@audio/decode-avi, @audio/decode-caf, @audio/decode-dsd, @audio/decode-flac, @audio/decode-mp4, @audio/decode-qoa, @audio/decode-wav, @wasm-audio-decoders/common, audio-type, qoa-format, simple-yenc
Apache-2.0yes@audio/decode-amr, @eshaz/web-worker, @thi.ng/bitstream, @thi.ng/errors
BSD-3-Clauseyes@audio/decode-mod, @audio/decode-mpc, @audio/decode-wavpack
MIT AND TU-Berlin-2.0yes@audio/decode-aiff, @audio/decode-gsm
MIT AND BSD-3-Clauseyes@wasm-audio-decoders/flac

Try it in the browser: Extract audio from video, Audio converter.

Decode any audio format to raw samples.
JS / WASM with no ffmpeg or native bindings; works in Node.js and browsers.
Small API, minimal size, near-native performance, lazy-loading, chunked decoding.

import decode from '@audio/decode';

const { channelData, sampleRate } = await decode(anyAudioBuffer);

audio-decode, its earlier name, is deprecated. What changed in each version: releases.

Supported formats

Format Package Size Engine
MP3 @audio/decode-mp3 92 KB WASM
WAV @audio/decode-wav 11 KB JS
OGG Vorbis @audio/decode-vorbis 166 KB WASM
FLAC @audio/decode-flac 135 KB WASM
Opus @audio/decode-opus 166 KB WASM
M4A / AAC / ALAC @audio/decode-aac 544 KB WASM (FFmpeg libavcodec, LGPL) + JS
MP4 / MOV / M4V / 3GP video @audio/decode-mp4 12 KB + codec JS demux
QOA @audio/decode-qoa 8 KB JS
AIFF @audio/decode-aiff 20 KB JS
CAF @audio/decode-caf 9 KB JS
WebM / MKV video @audio/decode-webm 250 KB WASM
AVI video @audio/decode-avi 8 KB + codec JS demux
AC-3 @audio/decode-eac3 432 KB, shared with E-AC-3 WASM (FFmpeg libavcodec, LGPL)
DTS @audio/decode-dts 601 KB WASM (FFmpeg libavcodec, LGPL)
E-AC-3 (Dolby Digital Plus, + AC-3) @audio/decode-eac3 432 KB WASM (FFmpeg libavcodec, LGPL)
APE (Monkey's Audio) @audio/decode-ape 262 KB WASM (FFmpeg libavcodec, LGPL)
AMR @audio/decode-amr 241 KB WASM
GSM 06.10 (raw frames, by name) @audio/decode-gsm 23 KB WASM (libgsm)
WMA (v1, v2, Pro, Lossless) @audio/decode-wma 463 KB WASM (FFmpeg libavcodec, LGPL)
WavPack @audio/decode-wavpack 51 KB WASM
TTA (True Audio) @audio/decode-tta 11 KB JS
Musepack SV7 / SV8 @audio/decode-mpc 51 KB WASM
MOD / XM / S3M / IT tracker modules @audio/decode-mod 1.4 MB WASM (libopenmpt)
DSF / DFF (DSD64–512 → PCM) @audio/decode-dsd 18 KB JS

Whole-file

Auto-detects format. Input can be ArrayBuffer, Uint8Array, Buffer, or anything that materializes to bytes, including a Blob/File or fetch Response.

import decode from '@audio/decode'

let { channelData, sampleRate } = await decode(buf)
let fromFile = await decode(fileInput.files[0])   // File
let fromUrl  = await decode(await fetch(url))      // Response

Chunked

let dec = await decode.mp3()
let a = await dec(chunk1)    // { channelData, sampleRate }
let b = await dec(chunk2)
await dec()                  // close

Streaming

import decode from '@audio/decode'

for await (let { channelData, sampleRate } of decode.mp3(response.body)) {
  // process chunks
}

Works with ReadableStream, fetch body, Node stream, or any async iterable.

Formats: mp3, flac, opus, oga, m4a, mp4, mov, wav, qoa, aac, aiff, caf, webm, mkv, avi, ac3, dts, amr, gsm, wma, eac3, ape, wv, tta, mpc, dsf, dff, mod, xm, s3m, it.

Video files

Video containers decode straight to their audio track — the video stream is skipped, no ffmpeg involved:

let { channelData, sampleRate } = await decode(await fetch('trailer.mp4'))
Container Package Audio codecs
MP4, MOV, M4V, 3GP @audio/decode-mp4 AAC, ALAC, MP3, FLAC, Opus, AC-3, DTS, AMR, PCM, G.711
WebM, MKV @audio/decode-webm Opus, Vorbis, AAC, ALAC, MP3, FLAC, AC-3, DTS, PCM
AVI @audio/decode-avi PCM, MP3, AAC, AC-3, DTS, G.711

Surround tracks keep their layout (up to 5.1, WAV channel order). E-AC-3 and TrueHD tracks throw an error naming the codec.

Browser

Works from a CDN without a bundler. Codecs load on demand via dynamic import, only for formats you decode:

<script type="module">
  import decode from 'https://esm.sh/@audio/decode'
  let { channelData, sampleRate } = await decode(buf)
</script>

For self-hosting, use an import map to point @audio/decode and each needed @audio/decode-* package to local files. Codec-internal files load by relative path.

Each codec package's main export works in an AudioWorklet without Blob, TextDecoder, Worker, or fetch. Import codec packages directly because @audio/decode uses dynamic imports.

Initialize WASM before rendering. Decoding runs on the worklet thread and can interrupt audio output.

Synchronous codecs

The umbrella remains async for detection, lazy imports, and Blob/Response inputs. Import a codec package directly for synchronous calls.

wav, qoa, aiff, and caf are synchronous:

import decode from '@audio/decode-wav'
let pcm = decode(wavBytes)

WASM codecs initialize asynchronously, then decode synchronously:

import { decoder } from '@audio/decode-flac'
let dec = await decoder()
let pcm = dec.decode(bytes)
let tail = dec.flush()
dec.free()

Metadata

Read tags, pictures, markers and regions without decoding samples. Available for wav, mp3, flac, oga (Ogg Vorbis), opus, and m4a.

import { wav, mp3, flac, oga, opus, m4a } from '@audio/decode/meta'

let { meta, sampleRate, markers, regions } = mp3(bytes)
// meta: { title, artist, album, year, bpm, key, comment, pictures, raw, ... }
// markers: [{ sample, label }]
// regions: [{ sample, length, label }]

Each codec sub-package also exposes its parser directly:

import { parseMeta } from '@audio/decode-wav/meta'
let info = parseMeta(wavBytes)

WebWorker

Each @audio/decode-* package is a self-contained ESM module that can run in a worker:

// decode-worker.js
import decode from '@audio/decode-mp3'

self.onmessage = async ({ data }) => {
  let pcm = await decode(data)
  self.postMessage(pcm, pcm.channelData.map(ch => ch.buffer))
}

// main.js
let worker = new Worker('./decode-worker.js', { type: 'module' })
worker.postMessage(mp3buf, [mp3buf])
worker.onmessage = ({ data }) => { /* { channelData, sampleRate } */ }

See also

  • audio – edit, measure and save: trim, loudness to spec, delivery checks, a CLI.
  • encode – encode PCM into any audio format.
  • audio-type – detect audio format from buffer.

Licensing

The umbrella is MIT. It installs every codec package below as a dependency and loads each on demand; no GPL code is among them. By the license of the code inside:

Each package's license field and LICENSE.* files name what it bundles. To keep a license out of your project, install the codec packages you need in place of the umbrella; the containers @audio/decode-mp4, @audio/decode-webm and @audio/decode-avi install decode-aac, decode-eac3, decode-dts and decode-mp3 (LGPL) with them, and decode-mp4 and decode-webm also decode-flac and decode-opus (codec-parser, LGPL-3.0-or-later); @audio/decode-dsd installs decode-mp3 for its ID3v2 reader. @audio/decode-ac3 (liba52, GPL-2.0-or-later) is no longer a dependency: AC-3 decodes through decode-eac3.

ॐ · MIT