@audio/decode-mp4 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 audiojs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # @audio/decode-mp4
2
+
3
+ Decode the audio track of MP4, MOV, M4A, M4V and 3GP files to PCM float samples.<br>
4
+ A pure-JS ISO BMFF demuxer that skips the video track and routes the audio to the matching codec package, loaded on demand.
5
+
6
+ [![npm install @audio/decode-mp4](https://nodei.co/npm/@audio/decode-mp4.png?mini=true)](https://npmjs.org/package/@audio/decode-mp4/)
7
+
8
+ ```js
9
+ import decode, { decoder } from '@audio/decode-mp4'
10
+
11
+ let { channelData, sampleRate } = await decode(mp4Bytes)
12
+
13
+ let dec = await decoder()
14
+ let head = await dec.decode(chunk1) // Promise on the chunk that completes the track header
15
+ let more = dec.decode(chunk2) // synchronous from then on
16
+ let tail = await dec.flush()
17
+ dec.free()
18
+ ```
19
+
20
+ ## Codecs
21
+
22
+ | Sample entry | Codec | Decoded by |
23
+ |---|---|---|
24
+ | `mp4a` (esds object type 0x40, 0x66–0x68) | AAC-LC, HE-AAC v1/v2 | [@audio/decode-aac](../decode-aac) |
25
+ | `alac` | Apple Lossless | [@audio/decode-aac](../decode-aac) (pure JS) |
26
+ | `mp4a` (0x69, 0x6B), `.mp3` | MP3 | [@audio/decode-mp3](../decode-mp3) |
27
+ | `fLaC` | FLAC | [@audio/decode-flac](../decode-flac) |
28
+ | `Opus` | Opus | [@audio/decode-opus](../decode-opus) |
29
+ | `samr`, `sawb` | AMR-NB, AMR-WB | [@audio/decode-amr](../decode-amr) |
30
+ | `sowt`, `twos`, `in24`, `in32`, `fl32`, `fl64`, `lpcm`, `ipcm`, `fpcm`, `raw `, `NONE` | PCM 8–32 bit int, 32/64 bit float, either endianness | built in |
31
+ | `ulaw`, `alaw` | G.711 | built in |
32
+
33
+ AC-3, E-AC-3 and DTS tracks throw an error naming the codec. Fragmented MP4 (`moof`) is not supported. QuickTime sound descriptions v0, v1 and v2 are handled, including `wave`-wrapped configs and `enda` endianness flags. When `moov` follows `mdat` (typical of camera and QuickTime output) the file is buffered until the header arrives.
34
+
35
+ Codec packages are imported dynamically, which is unavailable inside an AudioWorklet — decode in the main thread or a Worker.
36
+
37
+ ## API
38
+
39
+ ### `decode(src: Uint8Array | ArrayBuffer): Promise<AudioData>`
40
+
41
+ Decode a complete file.
42
+
43
+ ### `decoder(): Promise<MP4Decoder>`
44
+
45
+ Streaming decoder. `decode(chunk)` returns `AudioData`, or a Promise of it on the chunk that completes the track header while the codec module loads. `flush()` returns remaining samples and throws if no audio track was found. `free()` releases codec resources.
46
+
47
+ ### `AudioData`
48
+
49
+ ```ts
50
+ { channelData: Float32Array[], sampleRate: number }
51
+ ```
52
+
53
+ ## License
54
+
55
+ [ॐ](https://github.com/krishnized/license/) · [MIT](./LICENSE). Codec packages carry their own licenses.
package/audio.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ // Generated from the audio.js manifest (params metadata is the source of truth).
2
+ // Regenerate: node tools/dts.js in @audio/compile. Do not edit by hand.
3
+
4
+ /** Automatable number — scalar, `t => value` fn, or breakpoint curve {t, v} */
5
+ type Auto = number | ((t: number) => number) | { t: number[], v: number[] }
6
+ /** Per-block param values as delivered by hosts (numbers arrive as 1-length Float32Array) */
7
+ type Live = Record<string, Float32Array | string | boolean>
8
+ type Ctx = { sampleRate: number, maxBlockSize: number, maxChannels: number, currentTime: number, duration?: number, events?: readonly any[], emit?: (name: string, ...args: any[]) => void, [k: string]: unknown }
9
+ type Process = (inputs: Float32Array[][], outputs: Float32Array[][], params: Live) => void
10
+
11
+ /** Codec plugin 'mp4' — extends audio()'s openable formats / save() targets */
12
+ export declare const mp4: {
13
+ codec: 'mp4'
14
+ decode(bytes: Uint8Array): { channelData: Float32Array[], sampleRate: number } | Promise<{ channelData: Float32Array[], sampleRate: number }>
15
+ }
package/audio.js ADDED
@@ -0,0 +1,9 @@
1
+ // audio.js manifest — codec atom, decode half: whole-buffer bytes → { channelData,
2
+ // sampleRate }. Format detection is magic-byte (audio-type) — no test() needed here.
3
+
4
+ import decodeFn from './decode-mp4.js'
5
+
6
+ export const mp4 = {
7
+ codec: 'mp4',
8
+ decode: (bytes) => decodeFn(bytes),
9
+ }
@@ -0,0 +1,21 @@
1
+ export interface AudioData {
2
+ channelData: Float32Array[];
3
+ sampleRate: number;
4
+ }
5
+
6
+ interface MP4Decoder {
7
+ /**
8
+ * Decode an MP4 chunk. Synchronous once the track header (moov) has been seen; the call that
9
+ * completes the header returns a Promise while the codec module loads.
10
+ */
11
+ decode(data: Uint8Array | ArrayBuffer): AudioData | Promise<AudioData>;
12
+ /** Finish the stream. Throws if no audio track was found. */
13
+ flush(): AudioData | Promise<AudioData>;
14
+ free(): void;
15
+ }
16
+
17
+ /** Decode the audio track of a complete MP4 / MOV / M4A / M4V / 3GP file. */
18
+ export default function decode(src: ArrayBuffer | Uint8Array): Promise<AudioData>;
19
+
20
+ /** Create a streaming decoder. */
21
+ export function decoder(): Promise<MP4Decoder>;
package/decode-mp4.js ADDED
@@ -0,0 +1,446 @@
1
+ /**
2
+ * MP4 / MOV / M4A / M4V / 3GP audio decoder — ISO BMFF demuxer that routes the audio track to a codec:
3
+ * AAC, ALAC → @audio/decode-aac · MP3 → @audio/decode-mp3 · FLAC → @audio/decode-flac
4
+ * Opus → @audio/decode-opus/core · AMR → @audio/decode-amr · PCM / µ-law / A-law inline.
5
+ *
6
+ * let { channelData, sampleRate } = await decode(mp4buf)
7
+ * let dec = await decoder(); let result = await dec.decode(chunk)
8
+ */
9
+
10
+ const EMPTY = Object.freeze({ channelData: [], sampleRate: 0 })
11
+
12
+ /**
13
+ * Whole-file decode
14
+ * @param {Uint8Array|ArrayBuffer} src
15
+ * @returns {Promise<{channelData: Float32Array[], sampleRate: number}>}
16
+ */
17
+ export default async function decode(src) {
18
+ if (!src || typeof src === 'string' || !(src.buffer || src.byteLength != null || src.length))
19
+ throw TypeError('Expected ArrayBuffer or Uint8Array')
20
+ let buf = src instanceof Uint8Array ? src : new Uint8Array(src.buffer || src)
21
+ let dec = new MP4Decoder()
22
+ try {
23
+ let result = await dec.decode(buf)
24
+ return merge(result, await dec.flush())
25
+ } finally { dec.free() }
26
+ }
27
+
28
+ /**
29
+ * Create streaming decoder instance. decode() is synchronous once the track header (moov) has been
30
+ * seen; the call that completes the header returns a Promise while the codec module loads.
31
+ */
32
+ export async function decoder() {
33
+ return new MP4Decoder()
34
+ }
35
+
36
+ class MP4Decoder {
37
+ constructor() {
38
+ this.accum = [] // header accumulator (until moov is complete)
39
+ this.accumLen = 0
40
+ this.pending = null // codec load in flight
41
+ this.codec = null // { feed(frames), flush(), free() }
42
+ this.st = null // sample-table walker: { sizes, stco, stsc, idx, ci, sInC, spc, nextOff }
43
+ this.left = null // unconsumed bytes
44
+ this.fileOff = 0 // absolute file offset of left[0]
45
+ this.skip = 0 // bytes to discard before the next sample
46
+ this.freed = false
47
+ }
48
+
49
+ decode(data) {
50
+ if (this.freed) throw Error('Decoder already freed')
51
+ if (!data || !data.byteLength) return EMPTY
52
+ let buf = data instanceof Uint8Array ? data : new Uint8Array(data)
53
+ if (this.pending) return this.pending.then(() => this.decode(buf))
54
+ if (this.st) return this.feed(buf)
55
+
56
+ this.accum.push(buf); this.accumLen += buf.length
57
+ let all = this.accum.length === 1 ? this.accum[0] : concat(this.accum, this.accumLen)
58
+ let track = parseTrack(all)
59
+ if (!track) return EMPTY // moov not complete yet
60
+ this.accum = []; this.accumLen = 0
61
+ return this.pending = createCodec(track).then(codec => {
62
+ this.pending = null
63
+ if (this.freed) { codec.free(); return EMPTY }
64
+ this.codec = codec
65
+ let { sizes, stco, stsc } = track
66
+ this.st = { sizes, stco, stsc, idx: 0, ci: 0, sInC: 0, spc: spcAt(0, stsc), nextOff: stco[0] }
67
+ this.left = all; this.fileOff = 0; this.skip = 0
68
+ return this.extract()
69
+ })
70
+ }
71
+
72
+ feed(buf) {
73
+ if (this.skip > 0) {
74
+ let n = Math.min(this.skip, buf.length)
75
+ this.skip -= n; this.fileOff += n
76
+ buf = buf.subarray(n)
77
+ if (!buf.length) return EMPTY
78
+ }
79
+ this.left = append(this.left, buf)
80
+ return this.extract()
81
+ }
82
+
83
+ // Walk sample tables by absolute file offset so chunk boundaries are irrelevant.
84
+ extract() {
85
+ let st = this.st, frames = []
86
+ while (st.idx < st.sizes.length) {
87
+ let off = st.nextOff, sz = st.sizes[st.idx]
88
+ let bufOff = off - this.fileOff
89
+ if (bufOff + sz > this.left.length) break
90
+ if (bufOff >= 0) frames.push(this.left.subarray(bufOff, bufOff + sz))
91
+ advance(st)
92
+ }
93
+ if (st.idx < st.sizes.length) {
94
+ let nextOff = st.nextOff, end = this.fileOff + this.left.length
95
+ if (nextOff >= end) { this.skip = nextOff - end; this.fileOff = end; this.left = null }
96
+ else if (nextOff > this.fileOff) { this.left = this.left.subarray(nextOff - this.fileOff).slice(); this.fileOff = nextOff }
97
+ } else this.left = null
98
+ return frames.length ? this.codec.feed(frames) : EMPTY
99
+ }
100
+
101
+ flush() {
102
+ if (this.freed) return EMPTY
103
+ if (this.pending) return this.pending.then(() => this.flush())
104
+ this.freed = true
105
+ if (!this.codec) {
106
+ if (this.accumLen && findBox(concat(this.accum, this.accumLen), 'moof')) throw Error('Fragmented MP4 is not supported')
107
+ throw Error(this.accumLen ? 'No audio track found in MP4' : 'Not an MP4 file')
108
+ }
109
+ try { return this.codec.flush() } finally { this.codec.free(); this.codec = null; this.left = null }
110
+ }
111
+
112
+ free() {
113
+ if (this.freed) return
114
+ this.freed = true
115
+ this.codec?.free(); this.codec = null
116
+ this.accum = []; this.left = null; this.st = null
117
+ }
118
+ }
119
+
120
+
121
+ // ===== ISO BMFF demuxer =====
122
+
123
+ const CONTAINERS = new Set(['moov', 'trak', 'mdia', 'minf', 'stbl', 'udta', 'edts', 'sinf', 'wave'])
124
+
125
+ /** Walk boxes in [start, end), calling cb(type, body, bodyOff) for leaves and cb(type, null) at container boundaries. */
126
+ function walk(buf, start, end, cb) {
127
+ let off = start
128
+ while (off < end - 8) {
129
+ let size = r32(buf, off)
130
+ let type = str4(buf, off + 4)
131
+ if (size === 0) size = end - off
132
+ else if (size === 1 && off + 16 <= end) { size = r32(buf, off + 12); if (size < 16) break }
133
+ else if (size < 8) break
134
+ if (type === 'mdat') { off += size; continue } // raw samples — read by offset later
135
+ if (off + size > end) return false // truncated box: wait for more data
136
+ let body = off + 8
137
+ if (type === 'stsd') parseStsd(buf, body, off + size, cb)
138
+ else if (type === 'meta') walk(buf, body + 4, off + size, cb)
139
+ else if (CONTAINERS.has(type)) { cb(type, null); walk(buf, body, off + size, cb); cb('/' + type, null) }
140
+ else cb(type, buf.subarray(body, off + size), body)
141
+ off += size
142
+ }
143
+ return true
144
+ }
145
+
146
+ function findBox(buf, name) {
147
+ let found = false
148
+ walk(buf, 0, buf.length, type => { if (type === name) found = true })
149
+ return found
150
+ }
151
+
152
+ // QuickTime sound sample description: v0 = 36-byte header, v1 adds 16 bytes, v2 declares its own size.
153
+ function parseStsd(buf, off, end, cb) {
154
+ let n = r32(buf, off + 4), pos = off + 8
155
+ for (let i = 0; i < n && pos + 16 <= end; i++) {
156
+ let size = r32(buf, pos), type = str4(buf, pos + 4)
157
+ if (size < 16) break
158
+ let ver = r16(buf, pos + 16)
159
+ let e = { type, ver }
160
+ if (ver === 2) {
161
+ let dv = new DataView(buf.buffer, buf.byteOffset)
162
+ e.sampleRate = dv.getFloat64(pos + 40)
163
+ e.channels = r32(buf, pos + 48)
164
+ e.bits = r32(buf, pos + 56)
165
+ e.flags = r32(buf, pos + 60)
166
+ e.head = r32(buf, pos + 36) || 72
167
+ } else {
168
+ e.channels = r16(buf, pos + 24)
169
+ e.bits = r16(buf, pos + 26)
170
+ e.sampleRate = r16(buf, pos + 32) // 16.16 fixed — integer part
171
+ e.head = ver === 1 ? 52 : 36
172
+ }
173
+ cb('entry', e)
174
+ if (size > e.head) walk(buf, pos + e.head, pos + size, cb)
175
+ pos += size
176
+ }
177
+ }
178
+
179
+ /** Parse the first audio track out of a complete moov. Returns null while moov is incomplete or absent. */
180
+ function parseTrack(buf) {
181
+ let traks = [], t = null, moov = false
182
+ walk(buf, 0, buf.length, (type, data, off) => {
183
+ if (type === 'moov') moov = true
184
+ else if (type === 'trak') traks.push(t = { children: {} })
185
+ else if (!t) return
186
+ else if (type === 'hdlr') t.handler = str4(data, 8)
187
+ else if (type === 'mdhd') t.timescale = r32(data, data[0] === 1 ? 20 : 12)
188
+ else if (type === 'entry') t.entry ??= data
189
+ else if (type === 'stsz') t.sizes = parseStsz(data)
190
+ else if (type === 'stco') t.stco = parseStco(data)
191
+ else if (type === 'co64') t.stco = parseCo64(data)
192
+ else if (type === 'stsc') t.stsc = parseStsc(data)
193
+ else if (data) t.children[type] ??= data
194
+ })
195
+ if (!moov) return null
196
+ let track = traks.find(t => t.handler === 'soun' && t.entry) ?? traks.find(t => t.entry && AUDIO_TYPES.has(t.entry.type))
197
+ if (!track) throw Error('No audio track found in MP4')
198
+ if (!track.sizes || !track.stco?.length) throw Error('Audio track has no sample tables')
199
+ if (!track.entry.sampleRate && track.timescale) track.entry.sampleRate = track.timescale
200
+ return track
201
+ }
202
+
203
+ const AUDIO_TYPES = new Set(['mp4a', 'alac', '.mp3', 'fLaC', 'Opus', 'sowt', 'twos', 'in24', 'in32', 'fl32', 'fl64', 'lpcm', 'ipcm', 'fpcm', 'raw ', 'NONE', 'ulaw', 'alaw', 'samr', 'sawb', 'ac-3', 'ec-3'])
204
+
205
+ /** esds → { oti, dsi } (objectTypeIndication + DecoderSpecificInfo) */
206
+ function parseEsds(data) {
207
+ let off = 4, oti = 0
208
+ while (off < data.length - 2) {
209
+ let tag = data[off++], len = 0, b
210
+ do { b = data[off++]; len = (len << 7) | (b & 0x7f) } while (b & 0x80 && off < data.length)
211
+ if (tag === 0x03) off += 3
212
+ else if (tag === 0x04) { oti = data[off]; off += 13 }
213
+ else if (tag === 0x05) return { oti, dsi: data.subarray(off, off + len) }
214
+ else off += len
215
+ }
216
+ return { oti, dsi: null }
217
+ }
218
+
219
+ function parseStsz(d) {
220
+ let sz = r32(d, 4), n = r32(d, 8)
221
+ if (sz) return new Array(n).fill(sz)
222
+ let sizes = new Array(n)
223
+ for (let i = 0; i < n; i++) sizes[i] = r32(d, 12 + i * 4)
224
+ return sizes
225
+ }
226
+ function parseStco(d) { let n = r32(d, 4), o = new Array(n); for (let i = 0; i < n; i++) o[i] = r32(d, 8 + i * 4); return o }
227
+ function parseCo64(d) { let n = r32(d, 4), o = new Array(n); for (let i = 0; i < n; i++) o[i] = r32(d, 8 + i * 8) * 0x100000000 + r32(d, 12 + i * 8); return o }
228
+ function parseStsc(d) { let n = r32(d, 4), e = new Array(n); for (let i = 0; i < n; i++) e[i] = { first: r32(d, 8 + i * 12), spc: r32(d, 12 + i * 12) }; return e }
229
+
230
+ function spcAt(ci, stsc) {
231
+ if (!stsc?.length) return 1
232
+ let spc = 1, cn = ci + 1
233
+ for (let j = stsc.length - 1; j >= 0; j--) if (cn >= stsc[j].first) { spc = stsc[j].spc; break }
234
+ return spc
235
+ }
236
+
237
+ function advance(st) {
238
+ st.nextOff += st.sizes[st.idx]
239
+ st.idx++; st.sInC++
240
+ if (st.sInC >= st.spc && st.ci + 1 < st.stco.length) {
241
+ st.ci++; st.sInC = 0
242
+ st.spc = spcAt(st.ci, st.stsc)
243
+ st.nextOff = st.stco[st.ci]
244
+ }
245
+ }
246
+
247
+
248
+ // ===== codec routing =====
249
+
250
+ const MP3_OTI = new Set([0x69, 0x6B]), AAC_OTI = new Set([0x40, 0x66, 0x67, 0x68])
251
+ const UNSUPPORTED = { 0xA5: 'AC-3', 0xA6: 'E-AC-3', 0xA9: 'DTS', 0xE1: 'QCELP', 'ac-3': 'AC-3', 'ec-3': 'E-AC-3', 'dtsc': 'DTS', 'dtsh': 'DTS-HD', 'dtsl': 'DTS-HD', 'dtse': 'DTS Express' }
252
+
253
+ async function createCodec({ entry, children }) {
254
+ let { type } = entry
255
+ if (type === 'mp4a') {
256
+ let { oti, dsi } = children.esds ? parseEsds(children.esds) : {}
257
+ if (MP3_OTI.has(oti)) return mp3()
258
+ if (!oti || AAC_OTI.has(oti)) {
259
+ if (!dsi) throw Error('MP4 AAC track has no AudioSpecificConfig')
260
+ return aac({ asc: dsi })
261
+ }
262
+ throw unsupported(UNSUPPORTED[oti] || 'esds object type 0x' + oti.toString(16))
263
+ }
264
+ if (type === 'alac') {
265
+ if (!children.alac) throw Error('MP4 ALAC track has no magic cookie')
266
+ return aac({ alac: children.alac })
267
+ }
268
+ if (type === '.mp3') return mp3()
269
+ if (type === 'fLaC') {
270
+ if (!children.dfLa) throw Error('MP4 FLAC track has no dfLa box')
271
+ return flac(children.dfLa.subarray(4))
272
+ }
273
+ if (type === 'Opus') {
274
+ if (!children.dOps) throw Error('MP4 Opus track has no dOps box')
275
+ return opus(children.dOps)
276
+ }
277
+ if (type === 'samr' || type === 'sawb') return amr(type === 'sawb')
278
+ let fmt = pcmFormat(entry, children)
279
+ if (fmt) return pcm(fmt)
280
+ throw unsupported(UNSUPPORTED[type] || type)
281
+ }
282
+
283
+ function unsupported(name) { return Error('Unsupported MP4 audio codec: ' + name) }
284
+
285
+ function pcmFormat(e, ch) {
286
+ let { type, channels, bits, sampleRate } = e
287
+ let f = { channels, sampleRate, bits, float: false, be: false, signed: true }
288
+ let enda = ch.enda ? r16(ch.enda, 0) === 1 : null // QuickTime endianness flag: 1 = little
289
+ switch (type) {
290
+ case 'sowt': f.bits = bits || 16; break
291
+ case 'twos': f.bits = bits || 16; f.be = true; break
292
+ case 'in24': f.bits = 24; f.be = !enda; break
293
+ case 'in32': f.bits = 32; f.be = !enda; break
294
+ case 'fl32': f.bits = 32; f.float = true; f.be = !enda; break
295
+ case 'fl64': f.bits = 64; f.float = true; f.be = !enda; break
296
+ case 'raw ': case 'NONE': f.bits = bits || 8; f.signed = f.bits > 8; break
297
+ case 'ulaw': f.bits = 8; f.law = 'u'; break
298
+ case 'alaw': f.bits = 8; f.law = 'a'; break
299
+ case 'lpcm': f.float = !!(e.flags & 1); f.be = !!(e.flags & 2); f.signed = !!(e.flags & 4) || f.float; break
300
+ case 'ipcm': case 'fpcm': {
301
+ let c = ch.pcmC
302
+ if (!c) return null
303
+ f.float = type === 'fpcm'; f.be = !(c[4] & 1); f.bits = c[5]
304
+ break
305
+ }
306
+ default: return null
307
+ }
308
+ if (!f.channels || !f.sampleRate || !f.bits) return null
309
+ return f
310
+ }
311
+
312
+ // --- adapters: { feed(frames: Uint8Array[]), flush(), free() } ---
313
+
314
+ async function aac(opts) {
315
+ let dec = await (await import('@audio/decode-aac')).decoder(opts)
316
+ return { feed: frames => dec.decode(frames), flush: () => dec.flush(), free: () => dec.free() }
317
+ }
318
+
319
+ async function mp3() {
320
+ let dec = await (await import('@audio/decode-mp3')).decoder()
321
+ return { feed: frames => dec.decode(concat(frames)), flush: () => dec.flush?.() ?? EMPTY, free: () => dec.free() }
322
+ }
323
+
324
+ async function flac(blocks) {
325
+ let dec = await (await import('@audio/decode-flac')).decoder()
326
+ let head = concat([new Uint8Array([0x66, 0x4C, 0x61, 0x43]), blocks]) // "fLaC" + METADATA_BLOCKs
327
+ let started = false
328
+ return {
329
+ feed: frames => { let bytes = concat(frames); if (!started) { started = true; bytes = concat([head, bytes]) } return dec.decode(bytes) },
330
+ flush: () => started ? dec.flush() : EMPTY,
331
+ free: () => dec.free()
332
+ }
333
+ }
334
+
335
+ async function opus(dOps) {
336
+ let channels = dOps[1]
337
+ let opts = { channels, sampleRate: 48000, preSkip: r16(dOps, 2), outputGain: (r16(dOps, 8) << 16) >> 16 }
338
+ if (dOps[10] > 0) {
339
+ opts.streamCount = dOps[11]; opts.coupledStreamCount = dOps[12]
340
+ opts.channelMappingTable = Array.from(dOps.subarray(13, 13 + channels))
341
+ }
342
+ let dec = await (await import('@audio/decode-opus/core')).createOpusDecoder()
343
+ dec.configure(opts)
344
+ return {
345
+ feed: frames => {
346
+ let r = dec.decodeFrames(frames)
347
+ if (!r.samplesDecoded) return EMPTY
348
+ return { channelData: r.channelData.map(c => c.subarray(0, r.samplesDecoded)), sampleRate: r.sampleRate }
349
+ },
350
+ flush: () => EMPTY,
351
+ free: () => dec.free()
352
+ }
353
+ }
354
+
355
+ async function amr(wb) {
356
+ let dec = await (await import('@audio/decode-amr')).decoder()
357
+ let magic = new TextEncoder().encode(wb ? '#!AMR-WB\n' : '#!AMR\n'), started = false
358
+ return {
359
+ feed: frames => { let bytes = concat(frames); if (!started) { started = true; bytes = concat([magic, bytes]) } return dec.decode(bytes) },
360
+ flush: () => dec.flush?.() ?? EMPTY,
361
+ free: () => dec.free()
362
+ }
363
+ }
364
+
365
+ // interleaved PCM → planar float. Keeps a partial frame between calls.
366
+ function pcm({ channels, sampleRate, bits, float, be, signed, law }) {
367
+ let bps = bits >> 3, frame = bps * channels, left = null
368
+ let read = law === 'u' ? ulaw : law === 'a' ? alaw : reader(bits, float, be, signed)
369
+ return {
370
+ feed(frames) {
371
+ let buf = concat(left ? [left, ...frames] : frames)
372
+ let n = Math.floor(buf.length / frame)
373
+ left = n * frame < buf.length ? buf.slice(n * frame) : null
374
+ if (!n) return EMPTY
375
+ let dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength)
376
+ let channelData = Array.from({ length: channels }, () => new Float32Array(n))
377
+ for (let i = 0, off = 0; i < n; i++)
378
+ for (let c = 0; c < channels; c++, off += bps) channelData[c][i] = read(dv, off)
379
+ return { channelData, sampleRate }
380
+ },
381
+ flush: () => EMPTY,
382
+ free() { left = null }
383
+ }
384
+ }
385
+
386
+ function reader(bits, float, be, signed) {
387
+ let le = !be
388
+ if (float) return bits === 64 ? (dv, o) => dv.getFloat64(o, le) : (dv, o) => dv.getFloat32(o, le)
389
+ switch (bits) {
390
+ case 8: return signed ? (dv, o) => dv.getInt8(o) / 128 : (dv, o) => (dv.getUint8(o) - 128) / 128
391
+ case 16: return (dv, o) => dv.getInt16(o, le) / 32768
392
+ case 24: return be
393
+ ? (dv, o) => ((dv.getUint8(o) << 24 | dv.getUint8(o + 1) << 16 | dv.getUint8(o + 2) << 8) >> 8) / 8388608
394
+ : (dv, o) => ((dv.getUint8(o + 2) << 24 | dv.getUint8(o + 1) << 16 | dv.getUint8(o) << 8) >> 8) / 8388608
395
+ case 32: return (dv, o) => dv.getInt32(o, le) / 2147483648
396
+ }
397
+ throw Error('Unsupported PCM bit depth: ' + bits)
398
+ }
399
+
400
+ // ITU-T G.711
401
+ function ulaw(dv, o) {
402
+ let u = ~dv.getUint8(o) & 0xFF
403
+ let t = (((u & 0x0F) << 3) + 0x84) << ((u & 0x70) >> 4)
404
+ return ((u & 0x80) ? 0x84 - t : t - 0x84) / 32768
405
+ }
406
+ function alaw(dv, o) {
407
+ let a = dv.getUint8(o) ^ 0x55
408
+ let t = (a & 0x0F) << 4, seg = (a & 0x70) >> 4
409
+ t = seg === 0 ? t + 8 : seg === 1 ? t + 0x108 : (t + 0x108) << (seg - 1)
410
+ return ((a & 0x80) ? t : -t) / 32768
411
+ }
412
+
413
+
414
+ // ===== helpers =====
415
+
416
+ function r32(b, o) { return (b[o] << 24 | b[o + 1] << 16 | b[o + 2] << 8 | b[o + 3]) >>> 0 }
417
+ function r16(b, o) { return b[o] << 8 | b[o + 1] }
418
+ function str4(b, o) { return String.fromCharCode(b[o], b[o + 1], b[o + 2], b[o + 3]) }
419
+
420
+ function append(left, buf) {
421
+ if (!left?.length) return buf.slice()
422
+ let out = new Uint8Array(left.length + buf.length)
423
+ out.set(left); out.set(buf, left.length)
424
+ return out
425
+ }
426
+
427
+ function concat(parts, total) {
428
+ if (parts.length === 1) return parts[0]
429
+ if (total == null) { total = 0; for (let p of parts) total += p.length }
430
+ let out = new Uint8Array(total), off = 0
431
+ for (let p of parts) { out.set(p, off); off += p.length }
432
+ return out
433
+ }
434
+
435
+ function merge(a, b) {
436
+ if (!b?.channelData?.length) return a
437
+ if (!a?.channelData?.length) return b
438
+ return {
439
+ channelData: a.channelData.map((ch, i) => {
440
+ let m = new Float32Array(ch.length + b.channelData[i].length)
441
+ m.set(ch); m.set(b.channelData[i], ch.length)
442
+ return m
443
+ }),
444
+ sampleRate: a.sampleRate
445
+ }
446
+ }
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@audio/decode-mp4",
3
+ "version": "1.0.0",
4
+ "description": "Decode the audio track of MP4, MOV, M4A, M4V and 3GP files (AAC, ALAC, MP3, FLAC, Opus, AMR, PCM) to PCM samples",
5
+ "type": "module",
6
+ "main": "decode-mp4.js",
7
+ "types": "decode-mp4.d.ts",
8
+ "exports": {
9
+ ".": "./decode-mp4.js",
10
+ "./audio": {
11
+ "types": "./audio.d.ts",
12
+ "default": "./audio.js"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "scripts": {
20
+ "test": "node test.js"
21
+ },
22
+ "files": [
23
+ "decode-mp4.js",
24
+ "decode-mp4.d.ts",
25
+ "audio.js",
26
+ "audio.d.ts",
27
+ "LICENSE"
28
+ ],
29
+ "dependencies": {
30
+ "@audio/decode-aac": "^1.5.0",
31
+ "@audio/decode-amr": "^1.3.0",
32
+ "@audio/decode-flac": "^1.3.0",
33
+ "@audio/decode-mp3": "^1.3.0",
34
+ "@audio/decode-opus": "^1.3.0"
35
+ },
36
+ "devDependencies": {
37
+ "tst": "^9.2.2"
38
+ },
39
+ "engines": {
40
+ "node": ">=18"
41
+ },
42
+ "keywords": ["mp4", "mov", "m4a", "m4v", "3gp", "quicktime", "isobmff", "demux", "video", "audio", "decode", "decoder", "aac", "alac", "pcm"],
43
+ "license": "MIT",
44
+ "author": "audiojs",
45
+ "homepage": "https://github.com/audiojs/decode/tree/main/packages/decode-mp4",
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "git+https://github.com/audiojs/decode.git",
49
+ "directory": "packages/decode-mp4"
50
+ },
51
+ "bugs": "https://github.com/audiojs/decode/issues",
52
+ "audio": "./audio.js",
53
+ "sideEffects": false,
54
+ "funding": "https://github.com/sponsors/audiojs"
55
+ }