audio

@audio/encode-aac1.2.1

Encode PCM audio samples to AAC

Encode PCM audio samples to AAC (ADTS): WebCodecs, else FFmpeg's native AAC encoder (WASM)

Status
calibrated
Updated
Family
encode, 15 packages under it
Category
codecs
Standards
ISO/IEC 14496-3
Licence
MIT AND LGPL-2.1-or-later
Bundles
ffmpeg
Dependencies
none
Downloads
5,826 last month
Tests
at least 34 in audiojs/encode's CI, passed at 22bf060,

Encode PCM audio samples to AAC (ADTS) format, in browsers and Node.
Where the browser's WebCodecs AudioEncoder supports the configuration (Chromium 94+, Safari 16+) it encodes there: zero bundle cost, hardware-accelerated. Everywhere else (Node, Firefox) it runs FFmpeg's native AAC encoder (AAC-LC, LGPL-2.1-or-later) compiled to WebAssembly, loaded on first use.

import aac from '@audio/encode-aac';

const encoder = await aac({ sampleRate: 44100, channels: 2, bitrate: 128 });
const a = await encoder.encode(chunk1); // → Uint8Array (ADTS frames)
const b = await encoder.encode(chunk2);
const c = await encoder.flush();        // → Uint8Array (remaining frames); frees the encoder
// complete ADTS AAC = concat(a, b, c)

Options

Option Default Description
sampleRate — Input sample rate in Hz (required); the WebAssembly encoder takes the rates of ISO/IEC 14496-3 Table 1.16, 7350 to 96000
channels 1 1 or 2; the WebAssembly encoder also 3–6 and 8 (WAV channel order)
bitrate 128 Target bitrate in kbps
profile 'lc' 'lc' AAC-LC; 'he' HE-AAC (SBR) and 'hev2' HE-AACv2 (SBR + PS, stereo only) where WebCodecs encodes them

HE-AAC is not in FFmpeg's encoder. Without WebCodecs, profile: 'he' or 'hev2' throws an error that names @audio/encode-aac-fdk: the Fraunhofer FDK encoder with this same API, HE-AAC included, under the FDK license. Install it in place of this package to have them.

The encoder reports priming (its delay in samples: FFmpeg 1024) and frameLength (1024), which a container needs for gapless playback. @audio/encode-mp4 uses them.

Output is raw ADTS-framed AAC: each chunk is a sequence of whole ADTS frames, playable as .aac.

Quality

SNR of the output, decoded by ffmpeg, against the input at the encoder's delay; the bitrate is the output's, ADTS headers included. test.js asserts the 128 kbps rows above 30 dB.

Input Target FFmpeg (this package) FDK (@audio/encode-aac-fdk)
Lena, mono 44.1 kHz (audio-lena) 64 kbps 28.0 dB at 66.8 kbps 34.7 dB at 64.4 kbps
Lena, mono 44.1 kHz 96 kbps 34.0 dB at 99.1 kbps 39.4 dB at 96.5 kbps
Lena, mono 44.1 kHz 128 kbps 44.3 dB at 132.1 kbps 41.1 dB at 128.6 kbps
440 Hz sine, stereo 48 kHz 64 kbps 24.8 dB at 68.1 kbps 57.3 dB at 67.3 kbps
440 Hz sine, stereo 48 kHz 128 kbps 42.9 dB at 133.8 kbps 61.7 dB at 133.8 kbps

SNR is no listening test: an AAC encoder shapes its noise under what the ear masks, which SNR does not weigh.

License

MIT. The encoder inside (src/aac.wasm.js) is FFmpeg's aac encoder, a slim libavcodec build without --enable-gpl, under the LGPL-2.1-or-later (LICENSE.ffmpeg). Corresponding source: FFmpeg 7.1.5 (release/7.1 at commit 3978a28), with build.sh and src/aac_glue.c. AAC is covered by patents; commercial distributors may need a license from the patent pool (Via LA).

ॐ