@audio/decode-aac

中文文档
1.6.0发布于 2026/09/29GPL-2.0TypeScriptES Module零依赖近 30 天下载 72.5万

Decode AAC/M4A (FAAD2 WASM) and ALAC/Apple Lossless (pure JS) to PCM samples

原始使用文档
文档目录11 节
  1. Install
  2. Usage
  3. Streaming
  4. API
  5. decode(src: Uint8Array | ArrayBuffer): Promise<AudioData>
  6. decoder(opts?): Promise<AACDecoder>
  7. Gapless
  8. AudioData
  9. Formats
  10. Metadata
  11. License

@audio/decode-aac

Decode AAC/M4A and ALAC audio to PCM float samples. FAAD2 WASM handles AAC; a pure-JS port of Apple's reference decoder handles ALAC. The M4A sample entry selects the codec.

Install

npm i @audio/decode-aac

Usage

import decode from '@audio/decode-aac'

// M4A or raw ADTS; auto-detected
let { channelData, sampleRate } = await decode(uint8array)
// channelData: Float32Array[] (one per channel)
// sampleRate: number

Streaming

import { decoder } from '@audio/decode-aac'

let dec = await decoder()
let { channelData, sampleRate } = dec.decode(chunk)
dec.free()

decoder() is asynchronous. Its decode() and flush() methods are synchronous.

API

decode(src: Uint8Array | ArrayBuffer): Promise<AudioData>

Whole-file decode. Auto-detects M4A (MP4 container) vs raw ADTS.

decoder(opts?): Promise<AACDecoder>

Creates a decoder instance for manual control.

  • dec.decode(data): decode a Uint8Array or ArrayBuffer chunk.
  • dec.flush(): discard buffered partial data and return an empty result.
  • dec.free(): release WASM memory.

Without options the byte stream is auto-detected (M4A or ADTS). Container demuxers that hold the codec config out-of-band pass it up-front and feed raw access units — one frame, or an array of frames, per decode() call:

let dec = await decoder({ asc })   // AudioSpecificConfig (esds, Matroska CodecPrivate, WAVEFORMATEX extra bytes)
let dec = await decoder({ alac })  // ALAC magic cookie

This is how @audio/decode-mp4, @audio/decode-webm and @audio/decode-avi decode AAC and ALAC tracks from video files.

Gapless

An M4A is decoded gapless: the encoder's priming (Apple's encoders: 2112 samples) and padding are trimmed, so the output has the source's length and timing. The track's edit list says where the presentation starts when it has one media edit (ISO/IEC 14496-12 §8.6.6; several are an edit, not a trim: the track decodes whole), else iTunes' iTunSMPB tag; FAAD2's own delay (it withholds its first frame; SBR delays HE-AAC 962 samples more) is accounted for. A raw ADTS stream carries no such information and keeps the decoder's delay. A demuxer passes the window it read from its container:

import { decoder, gapless } from '@audio/decode-aac'
let dec = await decoder({ asc, gapless: gapless({ elst, mdhd, stts, mvhd, ilst, asc }) })  // box bodies
// or directly: { gapless: { start, duration } } in seconds of the media timeline

AudioData

{ channelData: Float32Array[], sampleRate: number }

Formats

  • M4A / MP4 / MOV with AAC audio (LC and HE-AAC v1/v2 with SBR or PS) — QuickTime sound descriptions v0, v1 and v2
  • M4A / MP4 with bit-exact pure-JS ALAC decoding at 16, 20, 24, or 32 bits
  • Raw ADTS streams (.aac)

Metadata

import { parseMeta } from '@audio/decode-aac/meta'

let { meta, sampleRate } = parseMeta(m4aBytes)
// meta: { title, artist, album, year, genre, track, ..., pictures }

License

ॐ · AAC decoding GPL-2.0 (FAAD2), ALAC decoding Apache-2.0 (port of Apple's ALAC reference)

版本与发布记录

@audio/decode-aac 共发布 14 个版本,首次发布于 2026/04/04;过去 12 个月发布了 14 个版本。

版本发布日期距今
1.6.0latest2026/09/291 个月前
1.5.02026/08/282 个月前
1.4.02026/08/122 个月前
1.3.42026/07/143 个月前
1.3.32026/07/143 个月前
1.3.22026/07/113 个月前

关于 @audio/decode-aac 的常见问题

@audio/decode-aac 是什么?

Decode AAC/M4A (FAAD2 WASM) and ALAC/Apple Lossless (pure JS) to PCM samples

如何安装 @audio/decode-aac?

在项目目录运行 npm install @audio/decode-aac,或使用 pnpm add / yarn add / bun add。

@audio/decode-aac 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@audio/decode-aac 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@audio/decode-aac 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@audio/decode-aac 可以商用吗?

@audio/decode-aac 使用 GPL-2.0 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以兼容的 GPL 许可开源。仅在内部使用、不对外分发时不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@audio/decode-aac 的下载量有多少?

根据 npm 官方下载统计,@audio/decode-aac 在 2026/10/07 – 2026/11/05 这 30 天内被下载 725,290 次,日均约 24,176 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@audio/decode-aac 还在维护吗?

最新版本 1.6.0 发布于 2026/09/29(1 个月前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/04/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。