@hiroosy/baileys-lite 0.4.3 → 0.4.4
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/README.md +8 -17
- package/package.json +1 -1
- package/src/ffmpeg/LICENSE +502 -0
- package/src/ffmpeg/index.js +64 -0
- package/src/ffmpeg/lib/bits.js +150 -0
- package/src/ffmpeg/lib/bsf.js +242 -0
- package/src/ffmpeg/lib/codec/flac.js +801 -0
- package/src/ffmpeg/lib/codec/index.js +108 -0
- package/src/ffmpeg/lib/codec/mp3.js +768 -0
- package/src/ffmpeg/lib/codec/mpa.js +65 -0
- package/src/ffmpeg/lib/codec/mpa_tables.js +27 -0
- package/src/ffmpeg/lib/codec/opus.js +140 -0
- package/src/ffmpeg/lib/codec/pcm.js +198 -0
- package/src/ffmpeg/lib/compat.js +42 -0
- package/src/ffmpeg/lib/crc.js +60 -0
- package/src/ffmpeg/lib/demux/adts.js +151 -0
- package/src/ffmpeg/lib/demux/flac.js +150 -0
- package/src/ffmpeg/lib/demux/image.js +47 -0
- package/src/ffmpeg/lib/demux/index.js +60 -0
- package/src/ffmpeg/lib/demux/matroska.js +525 -0
- package/src/ffmpeg/lib/demux/merge.js +106 -0
- package/src/ffmpeg/lib/demux/mp3.js +199 -0
- package/src/ffmpeg/lib/demux/mp4.js +622 -0
- package/src/ffmpeg/lib/demux/ogg.js +250 -0
- package/src/ffmpeg/lib/demux/ts.js +354 -0
- package/src/ffmpeg/lib/demux/wav.js +156 -0
- package/src/ffmpeg/lib/errors.js +27 -0
- package/src/ffmpeg/lib/filter/gain.js +69 -0
- package/src/ffmpeg/lib/filter/mix.js +95 -0
- package/src/ffmpeg/lib/filter/resample.js +187 -0
- package/src/ffmpeg/lib/filter/video.js +575 -0
- package/src/ffmpeg/lib/image/bmp.js +123 -0
- package/src/ffmpeg/lib/image/frame.js +216 -0
- package/src/ffmpeg/lib/image/gif.js +215 -0
- package/src/ffmpeg/lib/image/index.js +70 -0
- package/src/ffmpeg/lib/image/jpeg.js +964 -0
- package/src/ffmpeg/lib/image/png.js +256 -0
- package/src/ffmpeg/lib/image/run.js +67 -0
- package/src/ffmpeg/lib/io.js +298 -0
- package/src/ffmpeg/lib/mux/adts.js +75 -0
- package/src/ffmpeg/lib/mux/flac.js +79 -0
- package/src/ffmpeg/lib/mux/mp3.js +85 -0
- package/src/ffmpeg/lib/mux/mp4.js +450 -0
- package/src/ffmpeg/lib/mux/ogg.js +146 -0
- package/src/ffmpeg/lib/mux/wav.js +94 -0
- package/src/ffmpeg/lib/net.js +270 -0
- package/src/ffmpeg/lib/pipeline.js +579 -0
- package/src/ffmpeg/lib/probe.js +45 -0
- package/src/ffmpeg/lib/tags.js +307 -0
- package/src/ffmpeg/lib/util.js +104 -0
- package/src/foundation/image-thumb.js +4 -56
- package/src/socket/messages-recv.js +5 -6
- package/src/utils/media.js +8 -85
- package/src/utils/message-compose.js +19 -11
- package/src/voip/index.js +16 -50
- package/src/voip/media/WaAudioEngine.js +8 -216
- package/src/voip/media/WaVideoEngine.js +8 -237
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
import * as tables from './mpa_tables.js';
|
|
2
|
+
import { parseMpaHeader } from './mpa.js';
|
|
3
|
+
import { BitReader } from '../bits.js';
|
|
4
|
+
import { badData } from '../errors.js';
|
|
5
|
+
|
|
6
|
+
const decoderDelay = 529;
|
|
7
|
+
|
|
8
|
+
let shared = null;
|
|
9
|
+
|
|
10
|
+
function buildTrie(entries) {
|
|
11
|
+
let nodes = new Int32Array(1024);
|
|
12
|
+
let count = 1;
|
|
13
|
+
for (const [code, len, symbol] of entries) {
|
|
14
|
+
let n = 0;
|
|
15
|
+
for (let b = len - 1; b >= 0; b--) {
|
|
16
|
+
const bit = Math.floor(code / Math.pow(2, b)) % 2;
|
|
17
|
+
const slot = n * 2 + bit;
|
|
18
|
+
if (b === 0) {
|
|
19
|
+
nodes[slot] = -(symbol + 1);
|
|
20
|
+
} else {
|
|
21
|
+
if (nodes[slot] <= 0) {
|
|
22
|
+
if (count * 2 + 2 > nodes.length) {
|
|
23
|
+
const bigger = new Int32Array(nodes.length * 2);
|
|
24
|
+
bigger.set(nodes);
|
|
25
|
+
nodes = bigger;
|
|
26
|
+
}
|
|
27
|
+
nodes[slot] = count++;
|
|
28
|
+
}
|
|
29
|
+
n = nodes[slot];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return nodes;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const fastBits = 8;
|
|
37
|
+
|
|
38
|
+
function buildFast(entries) {
|
|
39
|
+
const table = new Uint16Array(1 << fastBits);
|
|
40
|
+
for (const [code, len, symbol] of entries) {
|
|
41
|
+
if (len > fastBits) continue;
|
|
42
|
+
const shift = fastBits - len;
|
|
43
|
+
const base = code << shift;
|
|
44
|
+
for (let i = 0; i < 1 << shift; i++) table[base + i] = (len << 9) | (symbol + 1);
|
|
45
|
+
}
|
|
46
|
+
return table;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function buildShared() {
|
|
50
|
+
const huff = [null];
|
|
51
|
+
const huffFast = [null];
|
|
52
|
+
let off = 0;
|
|
53
|
+
tables.huffSizes.forEach((size) => {
|
|
54
|
+
const entries = [];
|
|
55
|
+
let code = 0;
|
|
56
|
+
for (let j = 0; j < size; j++) {
|
|
57
|
+
const len = tables.huffLens[off + j];
|
|
58
|
+
entries.push([Math.floor(code / Math.pow(2, 32 - len)), len, tables.huffSyms[off + j]]);
|
|
59
|
+
code += Math.pow(2, 32 - len);
|
|
60
|
+
}
|
|
61
|
+
huff.push(buildTrie(entries));
|
|
62
|
+
huffFast.push(buildFast(entries));
|
|
63
|
+
off += size;
|
|
64
|
+
});
|
|
65
|
+
const quadEntries = [0, 1].map((t) => {
|
|
66
|
+
const entries = [];
|
|
67
|
+
for (let i = 0; i < 16; i++) entries.push([tables.quadCodes[t][i], tables.quadBits[t][i], i]);
|
|
68
|
+
return entries;
|
|
69
|
+
});
|
|
70
|
+
const quadFast = quadEntries.map(buildFast);
|
|
71
|
+
const pow43 = new Float64Array(8300);
|
|
72
|
+
for (let i = 0; i < pow43.length; i++) pow43[i] = Math.pow(i, 4 / 3);
|
|
73
|
+
const cos36 = new Float64Array(36 * 18);
|
|
74
|
+
for (let i = 0; i < 36; i++) for (let k = 0; k < 18; k++) cos36[i * 18 + k] = Math.cos((Math.PI / 72) * (2 * i + 1 + 18) * (2 * k + 1));
|
|
75
|
+
const cos12 = new Float64Array(12 * 6);
|
|
76
|
+
for (let i = 0; i < 12; i++) for (let k = 0; k < 6; k++) cos12[i * 6 + k] = Math.cos((Math.PI / 24) * (2 * i + 1 + 6) * (2 * k + 1));
|
|
77
|
+
const win = [new Float64Array(36), new Float64Array(36), new Float64Array(36), new Float64Array(36)];
|
|
78
|
+
for (let i = 0; i < 36; i++) {
|
|
79
|
+
win[0][i] = Math.sin((Math.PI / 36) * (i + 0.5));
|
|
80
|
+
if (i < 18) win[1][i] = Math.sin((Math.PI / 36) * (i + 0.5));
|
|
81
|
+
else if (i < 24) win[1][i] = 1;
|
|
82
|
+
else if (i < 30) win[1][i] = Math.sin((Math.PI / 12) * (i - 18 + 0.5));
|
|
83
|
+
else win[1][i] = 0;
|
|
84
|
+
if (i < 6) win[3][i] = 0;
|
|
85
|
+
else if (i < 12) win[3][i] = Math.sin((Math.PI / 12) * (i - 6 + 0.5));
|
|
86
|
+
else if (i < 18) win[3][i] = 1;
|
|
87
|
+
else win[3][i] = Math.sin((Math.PI / 36) * (i + 0.5));
|
|
88
|
+
}
|
|
89
|
+
const win12 = new Float64Array(12);
|
|
90
|
+
for (let i = 0; i < 12; i++) win12[i] = Math.sin((Math.PI / 12) * (i + 0.5));
|
|
91
|
+
const dwin = new Float64Array(512);
|
|
92
|
+
for (let i = 0; i < 257; i++) {
|
|
93
|
+
let v = tables.enWindow[i] / 65536;
|
|
94
|
+
dwin[i] = v;
|
|
95
|
+
if ((i & 63) !== 0) v = -v;
|
|
96
|
+
if (i !== 0) dwin[512 - i] = v;
|
|
97
|
+
}
|
|
98
|
+
const ci = [-0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037];
|
|
99
|
+
const cs = ci.map((c) => 1 / Math.sqrt(1 + c * c));
|
|
100
|
+
const ca = ci.map((c, i) => c * cs[i]);
|
|
101
|
+
const isTable = [new Float64Array(16), new Float64Array(16)];
|
|
102
|
+
for (let i = 0; i < 7; i++) {
|
|
103
|
+
const v = i !== 6 ? Math.tan((i * Math.PI) / 12) / (1 + Math.tan((i * Math.PI) / 12)) : 1;
|
|
104
|
+
isTable[0][i] = v;
|
|
105
|
+
isTable[1][6 - i] = v;
|
|
106
|
+
}
|
|
107
|
+
const isLsf = [[new Float64Array(16), new Float64Array(16)], [new Float64Array(16), new Float64Array(16)]];
|
|
108
|
+
for (let i = 0; i < 16; i++) {
|
|
109
|
+
for (let j = 0; j < 2; j++) {
|
|
110
|
+
const e = -(j + 1) * ((i + 1) >> 1);
|
|
111
|
+
const f = Math.pow(2, e / 4);
|
|
112
|
+
const k = i & 1;
|
|
113
|
+
isLsf[j][k ^ 1][i] = f;
|
|
114
|
+
isLsf[j][k][i] = 1;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const bandIndexLong = tables.bandLong.map((row) => {
|
|
118
|
+
const out = [];
|
|
119
|
+
let k = 0;
|
|
120
|
+
for (let j = 0; j < 22; j++) {
|
|
121
|
+
out.push(k);
|
|
122
|
+
k += row[j] >> 1;
|
|
123
|
+
}
|
|
124
|
+
out.push(k);
|
|
125
|
+
return out;
|
|
126
|
+
});
|
|
127
|
+
return { huff, huffFast, quadFast, pow43, cos36, cos12, win, win12, dwin, cs, ca, isTable, isLsf, bandIndexLong };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const dctSizes = [32, 16, 8, 4, 2];
|
|
131
|
+
|
|
132
|
+
function createDct32() {
|
|
133
|
+
const tabs = dctSizes.map((dctLen) => {
|
|
134
|
+
const t = new Float64Array(dctLen / 2);
|
|
135
|
+
for (let i = 0; i < dctLen / 2; i++) t[i] = 1 / (2 * Math.cos(((2 * i + 1) * Math.PI) / (2 * dctLen)));
|
|
136
|
+
return t;
|
|
137
|
+
});
|
|
138
|
+
const mk = () => dctSizes.map((dctLen) => new Float64Array(dctLen / 2));
|
|
139
|
+
const a = mk();
|
|
140
|
+
const b = mk();
|
|
141
|
+
const ra = mk();
|
|
142
|
+
const rb = mk();
|
|
143
|
+
const rec = (l, x, out) => {
|
|
144
|
+
const dctN = dctSizes[l];
|
|
145
|
+
if (dctN === 2) {
|
|
146
|
+
out[0] = x[0] + x[1];
|
|
147
|
+
out[1] = (x[0] - x[1]) * Math.SQRT1_2;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const h = dctN / 2;
|
|
151
|
+
const hiSums = a[l];
|
|
152
|
+
const diffs = b[l];
|
|
153
|
+
const t = tabs[l];
|
|
154
|
+
for (let i = 0; i < h; i++) {
|
|
155
|
+
hiSums[i] = x[i] + x[dctN - 1 - i];
|
|
156
|
+
diffs[i] = (x[i] - x[dctN - 1 - i]) * t[i];
|
|
157
|
+
}
|
|
158
|
+
rec(l + 1, hiSums, ra[l]);
|
|
159
|
+
rec(l + 1, diffs, rb[l]);
|
|
160
|
+
const rbLevel = rb[l];
|
|
161
|
+
for (let k = 0; k < h; k++) out[2 * k] = ra[l][k];
|
|
162
|
+
for (let k = 0; k < h - 1; k++) out[2 * k + 1] = rbLevel[k] + rbLevel[k + 1];
|
|
163
|
+
out[dctN - 1] = rbLevel[h - 1];
|
|
164
|
+
};
|
|
165
|
+
return (x, out) => rec(0, x, out);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function split(sf, n) {
|
|
169
|
+
if (n >= 3 && n <= 6) {
|
|
170
|
+
const m = Math.floor(sf / n);
|
|
171
|
+
return [sf - m * n, m];
|
|
172
|
+
}
|
|
173
|
+
return [0, sf];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function newGranule() {
|
|
177
|
+
return {
|
|
178
|
+
part23: 0,
|
|
179
|
+
bigValues: 0,
|
|
180
|
+
globalGain: 0,
|
|
181
|
+
scalefacCompress: 0,
|
|
182
|
+
blockType: 0,
|
|
183
|
+
switchPoint: 0,
|
|
184
|
+
tableSelect: [0, 0, 0],
|
|
185
|
+
subblockGain: [0, 0, 0],
|
|
186
|
+
regionSize: [0, 0, 0],
|
|
187
|
+
preflag: 0,
|
|
188
|
+
scalefacScale: 0,
|
|
189
|
+
count1Table: 0,
|
|
190
|
+
scfsi: 0,
|
|
191
|
+
sf: new Int32Array(48),
|
|
192
|
+
longEnd: 22,
|
|
193
|
+
shortStart: 13,
|
|
194
|
+
xr: new Float64Array(576 + 8),
|
|
195
|
+
is: new Int32Array(576 + 8),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
class Mp3Decoder {
|
|
200
|
+
constructor(stream) {
|
|
201
|
+
if (!shared) shared = buildShared();
|
|
202
|
+
this.s = shared;
|
|
203
|
+
this.reservoir = Buffer.alloc(0);
|
|
204
|
+
this.gr = [[newGranule(), newGranule()], [newGranule(), newGranule()]];
|
|
205
|
+
this.overlap = [new Float64Array(576), new Float64Array(576)];
|
|
206
|
+
this.vbuf = [new Float64Array(1024), new Float64Array(1024)];
|
|
207
|
+
this.voff = [0, 0];
|
|
208
|
+
this.samples = new Float64Array(576);
|
|
209
|
+
this.expo = new Float64Array(576);
|
|
210
|
+
this.tmp = new Float64Array(576);
|
|
211
|
+
this.sbs = new Float64Array(18 * 32);
|
|
212
|
+
this.dct = createDct32();
|
|
213
|
+
this.acc = new Float64Array(32);
|
|
214
|
+
this.dctIn = new Float64Array(32);
|
|
215
|
+
this.dctOut = new Float64Array(32);
|
|
216
|
+
this.out = new Float64Array(0);
|
|
217
|
+
this.spf = 0;
|
|
218
|
+
const gap = stream && stream.gapless;
|
|
219
|
+
this.skip = gap ? gap.delay + decoderDelay : 0;
|
|
220
|
+
this.endLimit = gap && gap.frames > 0 ? gap.frames * (stream.samplesPerFrame || 1152) - gap.padding + decoderDelay : Infinity;
|
|
221
|
+
this.index = 0;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
static get preroll() {
|
|
225
|
+
return 4;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
decode(buf, pkt) {
|
|
229
|
+
const hdr = parseMpaHeader(buf, 0);
|
|
230
|
+
if (!hdr || hdr.layer !== 3) throw badData('mp3: invalid frame header');
|
|
231
|
+
const sr = { 44100: 0, 48000: 1, 32000: 2, 22050: 3, 24000: 4, 16000: 5, 11025: 6, 12000: 7, 8000: 8 }[hdr.sampleRate];
|
|
232
|
+
const lsf = !hdr.mpeg1;
|
|
233
|
+
const nch = hdr.channels;
|
|
234
|
+
const nGr = lsf ? 1 : 2;
|
|
235
|
+
const jstereo = hdr.mode === 1;
|
|
236
|
+
const modeExt = jstereo ? (hdr.b3 >> 4) & 3 : 0;
|
|
237
|
+
const frames = nGr * 576;
|
|
238
|
+
this.spf = frames;
|
|
239
|
+
let pos = 4 + (hdr.protection ? 2 : 0);
|
|
240
|
+
const sideLen = lsf ? (nch === 1 ? 9 : 17) : nch === 1 ? 17 : 32;
|
|
241
|
+
if (buf.length < pos + sideLen) throw badData('mp3: truncated frame');
|
|
242
|
+
const side = new BitReader(buf, pos, pos + sideLen);
|
|
243
|
+
const mainStart = pos + sideLen;
|
|
244
|
+
const mainBegin = side.read(lsf ? 8 : 9);
|
|
245
|
+
side.read(lsf ? nch : nch === 2 ? 3 : 5);
|
|
246
|
+
if (!lsf) {
|
|
247
|
+
for (let c = 0; c < nch; c++) {
|
|
248
|
+
this.gr[c][0].scfsi = 0;
|
|
249
|
+
this.gr[c][1].scfsi = side.read(4);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
for (let g = 0; g < nGr; g++) {
|
|
253
|
+
for (let c = 0; c < nch; c++) {
|
|
254
|
+
const gr = this.gr[c][g];
|
|
255
|
+
gr.part23 = side.read(12);
|
|
256
|
+
gr.bigValues = side.read(9);
|
|
257
|
+
gr.globalGain = side.read(8);
|
|
258
|
+
if ((modeExt & 3) === 2) gr.globalGain -= 2;
|
|
259
|
+
gr.scalefacCompress = side.read(lsf ? 9 : 4);
|
|
260
|
+
const window = side.read(1);
|
|
261
|
+
if (window) {
|
|
262
|
+
gr.blockType = side.read(2);
|
|
263
|
+
gr.switchPoint = side.read(1);
|
|
264
|
+
gr.tableSelect[0] = side.read(5);
|
|
265
|
+
gr.tableSelect[1] = side.read(5);
|
|
266
|
+
gr.tableSelect[2] = 0;
|
|
267
|
+
for (let i = 0; i < 3; i++) gr.subblockGain[i] = side.read(3);
|
|
268
|
+
this.shortRegions(gr, sr);
|
|
269
|
+
} else {
|
|
270
|
+
gr.blockType = 0;
|
|
271
|
+
gr.switchPoint = 0;
|
|
272
|
+
for (let i = 0; i < 3; i++) gr.tableSelect[i] = side.read(5);
|
|
273
|
+
const r1 = side.read(4);
|
|
274
|
+
const r2 = side.read(3);
|
|
275
|
+
const bi = this.s.bandIndexLong[sr];
|
|
276
|
+
gr.regionSize[0] = bi[r1 + 1];
|
|
277
|
+
gr.regionSize[1] = bi[Math.min(r1 + r2 + 2, 22)];
|
|
278
|
+
}
|
|
279
|
+
if (gr.bigValues > 288) throw badData('mp3: invalid big_values');
|
|
280
|
+
let j = 0;
|
|
281
|
+
gr.regionSize[2] = 288;
|
|
282
|
+
for (let i = 0; i < 3; i++) {
|
|
283
|
+
const k = Math.min(gr.regionSize[i], gr.bigValues);
|
|
284
|
+
gr.regionSize[i] = k - j;
|
|
285
|
+
j = k;
|
|
286
|
+
}
|
|
287
|
+
if (gr.blockType === 2) {
|
|
288
|
+
if (gr.switchPoint) {
|
|
289
|
+
gr.longEnd = sr <= 2 ? 8 : 6;
|
|
290
|
+
gr.shortStart = 3;
|
|
291
|
+
} else {
|
|
292
|
+
gr.longEnd = 0;
|
|
293
|
+
gr.shortStart = 0;
|
|
294
|
+
}
|
|
295
|
+
} else {
|
|
296
|
+
gr.shortStart = 13;
|
|
297
|
+
gr.longEnd = 22;
|
|
298
|
+
}
|
|
299
|
+
gr.preflag = lsf ? 0 : side.read(1);
|
|
300
|
+
gr.scalefacScale = side.read(1);
|
|
301
|
+
gr.count1Table = side.read(1);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const own = buf.subarray(mainStart);
|
|
305
|
+
const silent = this.reservoir.length < mainBegin;
|
|
306
|
+
let main;
|
|
307
|
+
if (silent) main = own;
|
|
308
|
+
else main = mainBegin > 0 ? Buffer.concat([this.reservoir.subarray(this.reservoir.length - mainBegin), own]) : own;
|
|
309
|
+
const merged = Buffer.concat([this.reservoir, own]);
|
|
310
|
+
this.reservoir = merged.subarray(Math.max(0, merged.length - 511));
|
|
311
|
+
const br = new BitReader(main, 0, main.length);
|
|
312
|
+
let bitPos = 0;
|
|
313
|
+
const out = this.out.length >= frames * nch ? this.out : (this.out = new Float64Array(frames * nch));
|
|
314
|
+
for (let g = 0; g < nGr; g++) {
|
|
315
|
+
for (let c = 0; c < nch; c++) {
|
|
316
|
+
const gr = this.gr[c][g];
|
|
317
|
+
gr.xr.fill(0);
|
|
318
|
+
if (!silent) {
|
|
319
|
+
try {
|
|
320
|
+
br.pos = bitPos;
|
|
321
|
+
br.limit = main.length * 8;
|
|
322
|
+
this.readGranule(br, gr, g, c, lsf, sr, modeExt, bitPos + gr.part23, this.gr[c][0]);
|
|
323
|
+
} catch (e) {
|
|
324
|
+
gr.xr.fill(0);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
bitPos += gr.part23;
|
|
328
|
+
}
|
|
329
|
+
if (nch === 2 && jstereo) this.stereo(this.gr[0][g], this.gr[1][g], lsf, sr, modeExt);
|
|
330
|
+
for (let c = 0; c < nch; c++) {
|
|
331
|
+
const gr = this.gr[c][g];
|
|
332
|
+
this.reorder(gr, sr);
|
|
333
|
+
this.antialias(gr);
|
|
334
|
+
this.imdct(gr, c);
|
|
335
|
+
this.synth(c, g, out, nch);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return this.trim(out, frames, nch, hdr.sampleRate, pkt);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
trim(out, frames, nch, sampleRate, pkt) {
|
|
342
|
+
const base = pkt ? (pkt.pts0 !== undefined ? pkt.pts0 : pkt.pts) : this.index * frames;
|
|
343
|
+
this.index++;
|
|
344
|
+
const from = Math.max(base, this.skip);
|
|
345
|
+
const to = Math.min(base + frames, this.endLimit);
|
|
346
|
+
if (to <= from) return { data: out, frames: 0, pts: 0, channels: nch, sampleRate };
|
|
347
|
+
const a = from - base;
|
|
348
|
+
const n = to - from;
|
|
349
|
+
return { data: out.subarray(a * nch, (a + n) * nch), frames: n, pts: from - this.skip, channels: nch, sampleRate };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
shortRegions(gr, sr) {
|
|
353
|
+
if (gr.blockType === 2) gr.regionSize[0] = sr !== 8 ? 18 : 36;
|
|
354
|
+
else if (sr <= 2) gr.regionSize[0] = 18;
|
|
355
|
+
else gr.regionSize[0] = sr !== 8 ? 27 : 54;
|
|
356
|
+
gr.regionSize[1] = 288;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
readGranule(br, gr, g, c, lsf, sr, modeExt, endBits, gr0) {
|
|
360
|
+
const sf = gr.sf;
|
|
361
|
+
sf.fill(0);
|
|
362
|
+
if (!lsf) {
|
|
363
|
+
const slen1 = tables.slenTable[0][gr.scalefacCompress];
|
|
364
|
+
const slen2 = tables.slenTable[1][gr.scalefacCompress];
|
|
365
|
+
if (gr.blockType === 2) {
|
|
366
|
+
const n = gr.switchPoint ? 17 : 18;
|
|
367
|
+
let j = 0;
|
|
368
|
+
for (let i = 0; i < n; i++) sf[j++] = slen1 ? br.read(slen1) : 0;
|
|
369
|
+
for (let i = 0; i < 18; i++) sf[j++] = slen2 ? br.read(slen2) : 0;
|
|
370
|
+
} else {
|
|
371
|
+
let j = 0;
|
|
372
|
+
for (let k = 0; k < 4; k++) {
|
|
373
|
+
const n = k === 0 ? 6 : 5;
|
|
374
|
+
if ((gr.scfsi & (8 >> k)) === 0) {
|
|
375
|
+
const slen = k < 2 ? slen1 : slen2;
|
|
376
|
+
for (let i = 0; i < n; i++) sf[j++] = slen ? br.read(slen) : 0;
|
|
377
|
+
} else {
|
|
378
|
+
for (let i = 0; i < n; i++) {
|
|
379
|
+
sf[j] = gr0.sf[j];
|
|
380
|
+
j++;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
} else {
|
|
386
|
+
const tindex = gr.blockType === 2 ? (gr.switchPoint ? 2 : 1) : 0;
|
|
387
|
+
let value = gr.scalefacCompress;
|
|
388
|
+
const slen = [0, 0, 0, 0];
|
|
389
|
+
let tindex2;
|
|
390
|
+
const expand = (v, n1, n2, n3) => {
|
|
391
|
+
let r;
|
|
392
|
+
r = split(v, n3);
|
|
393
|
+
slen[3] = r[0];
|
|
394
|
+
v = r[1];
|
|
395
|
+
r = split(v, n2);
|
|
396
|
+
slen[2] = r[0];
|
|
397
|
+
v = r[1];
|
|
398
|
+
r = split(v, n1);
|
|
399
|
+
slen[1] = r[0];
|
|
400
|
+
v = r[1];
|
|
401
|
+
slen[0] = v;
|
|
402
|
+
};
|
|
403
|
+
if (modeExt & 1 && c === 1) {
|
|
404
|
+
value >>= 1;
|
|
405
|
+
if (value < 180) {
|
|
406
|
+
expand(value, 6, 6, 0);
|
|
407
|
+
tindex2 = 3;
|
|
408
|
+
} else if (value < 244) {
|
|
409
|
+
expand(value - 180, 4, 4, 0);
|
|
410
|
+
tindex2 = 4;
|
|
411
|
+
} else {
|
|
412
|
+
expand(value - 244, 3, 0, 0);
|
|
413
|
+
tindex2 = 5;
|
|
414
|
+
}
|
|
415
|
+
} else if (value < 400) {
|
|
416
|
+
expand(value, 5, 4, 4);
|
|
417
|
+
tindex2 = 0;
|
|
418
|
+
} else if (value < 500) {
|
|
419
|
+
expand(value - 400, 5, 4, 0);
|
|
420
|
+
tindex2 = 1;
|
|
421
|
+
} else {
|
|
422
|
+
expand(value - 500, 3, 0, 0);
|
|
423
|
+
tindex2 = 2;
|
|
424
|
+
gr.preflag = 1;
|
|
425
|
+
}
|
|
426
|
+
let j = 0;
|
|
427
|
+
for (let k = 0; k < 4; k++) {
|
|
428
|
+
const n = tables.lsfNsfTable[tindex2][tindex][k];
|
|
429
|
+
const sl = slen[k];
|
|
430
|
+
for (let i = 0; i < n; i++) sf[j++] = sl ? br.read(sl) : 0;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
this.exponents(gr, sr);
|
|
434
|
+
this.huffman(br, gr, endBits);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
exponents(gr, sr) {
|
|
438
|
+
const expo = this.expo;
|
|
439
|
+
const gain = gr.globalGain - 210;
|
|
440
|
+
const shift = gr.scalefacScale + 1;
|
|
441
|
+
const bl = tables.bandLong[sr];
|
|
442
|
+
const pre = tables.pretabTable[gr.preflag];
|
|
443
|
+
let p = 0;
|
|
444
|
+
for (let i = 0; i < gr.longEnd; i++) {
|
|
445
|
+
const v = Math.pow(2, (gain - ((gr.sf[i] + pre[i]) << shift)) / 4);
|
|
446
|
+
for (let j = bl[i]; j > 0; j--) expo[p++] = v;
|
|
447
|
+
}
|
|
448
|
+
if (gr.shortStart < 13) {
|
|
449
|
+
const bs = tables.bandShort[sr];
|
|
450
|
+
const gains = [0, 1, 2].map((w) => gain - (gr.subblockGain[w] << 3));
|
|
451
|
+
let k = gr.longEnd;
|
|
452
|
+
for (let i = gr.shortStart; i < 13; i++) {
|
|
453
|
+
for (let w = 0; w < 3; w++) {
|
|
454
|
+
const v = Math.pow(2, (gains[w] - (gr.sf[k++] << shift)) / 4);
|
|
455
|
+
for (let j = bs[i]; j > 0; j--) expo[p++] = v;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
while (p < 576) expo[p++] = 0;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
huffman(br, gr, endBits) {
|
|
463
|
+
const s = this.s;
|
|
464
|
+
const is = gr.is;
|
|
465
|
+
const xr = gr.xr;
|
|
466
|
+
const expo = this.expo;
|
|
467
|
+
const pow43 = s.pow43;
|
|
468
|
+
is.fill(0);
|
|
469
|
+
let idx = 0;
|
|
470
|
+
for (let r = 0; r < 3; r++) {
|
|
471
|
+
let count = gr.regionSize[r];
|
|
472
|
+
if (count === 0) continue;
|
|
473
|
+
const entry = tables.huffData[gr.tableSelect[r]];
|
|
474
|
+
const l = entry[0];
|
|
475
|
+
const linbits = entry[1];
|
|
476
|
+
if (!l) {
|
|
477
|
+
idx += 2 * count;
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
const trie = s.huff[l];
|
|
481
|
+
const fast = s.huffFast[l];
|
|
482
|
+
for (; count > 0; count--) {
|
|
483
|
+
if (br.pos >= endBits) break;
|
|
484
|
+
let sym;
|
|
485
|
+
const f = fast[br.peek(fastBits)];
|
|
486
|
+
if (f !== 0) {
|
|
487
|
+
br.pos += f >> 9;
|
|
488
|
+
sym = (f & 511) - 1;
|
|
489
|
+
} else {
|
|
490
|
+
let n = 0;
|
|
491
|
+
do {
|
|
492
|
+
n = trie[n * 2 + br.read(1)];
|
|
493
|
+
} while (n > 0);
|
|
494
|
+
if (n === 0) throw badData('mp3: invalid huffman code');
|
|
495
|
+
sym = -n - 1;
|
|
496
|
+
}
|
|
497
|
+
let x = sym >> 4;
|
|
498
|
+
let y = sym & 15;
|
|
499
|
+
if (x === 15 && linbits) x += br.read(linbits);
|
|
500
|
+
if (x !== 0 && br.read(1)) x = -x;
|
|
501
|
+
if (y === 15 && linbits) y += br.read(linbits);
|
|
502
|
+
if (y !== 0 && br.read(1)) y = -y;
|
|
503
|
+
is[idx] = x;
|
|
504
|
+
is[idx + 1] = y;
|
|
505
|
+
idx += 2;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
const quadFast = s.quadFast[gr.count1Table];
|
|
509
|
+
let last = -1;
|
|
510
|
+
while (idx <= 572) {
|
|
511
|
+
const at = br.pos;
|
|
512
|
+
if (at >= endBits) {
|
|
513
|
+
if (at > endBits && last >= 0) {
|
|
514
|
+
idx -= 4;
|
|
515
|
+
is[idx] = is[idx + 1] = is[idx + 2] = is[idx + 3] = 0;
|
|
516
|
+
}
|
|
517
|
+
break;
|
|
518
|
+
}
|
|
519
|
+
last = at;
|
|
520
|
+
const qf = quadFast[br.peek(fastBits)];
|
|
521
|
+
if (qf === 0) throw badData('mp3: invalid count1 code');
|
|
522
|
+
br.pos += qf >> 9;
|
|
523
|
+
const code = (qf & 511) - 1;
|
|
524
|
+
for (let i = 0; i < 4; i++) {
|
|
525
|
+
if (code & (8 >> i)) is[idx + i] = br.read(1) ? -1 : 1;
|
|
526
|
+
}
|
|
527
|
+
idx += 4;
|
|
528
|
+
}
|
|
529
|
+
for (let i = 0; i < 576; i++) {
|
|
530
|
+
const v = is[i];
|
|
531
|
+
if (v === 0) {
|
|
532
|
+
xr[i] = 0;
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
if (v > 8299 || v < -8299) throw badData('mp3: value out of range');
|
|
536
|
+
xr[i] = (v > 0 ? pow43[v] : -pow43[-v]) * expo[i];
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
stereo(g0, g1, lsf, sr, modeExt) {
|
|
541
|
+
const t0 = g0.xr;
|
|
542
|
+
const t1 = g1.xr;
|
|
543
|
+
const sqrtHalf = Math.SQRT1_2;
|
|
544
|
+
if (modeExt & 1) {
|
|
545
|
+
const isTab = lsf ? this.s.isLsf[g1.scalefacCompress & 1] : this.s.isTable;
|
|
546
|
+
const sfMax = lsf ? 16 : 7;
|
|
547
|
+
const nz = [0, 0, 0];
|
|
548
|
+
let p = 576;
|
|
549
|
+
let k = (13 - g1.shortStart) * 3 + g1.longEnd - 3;
|
|
550
|
+
const ms = (from, len) => {
|
|
551
|
+
for (let j = 0; j < len; j++) {
|
|
552
|
+
const a = t0[from + j];
|
|
553
|
+
const b = t1[from + j];
|
|
554
|
+
t0[from + j] = (a + b) * sqrtHalf;
|
|
555
|
+
t1[from + j] = (a - b) * sqrtHalf;
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
for (let i = 12; i >= g1.shortStart; i--) {
|
|
559
|
+
if (i !== 11) k -= 3;
|
|
560
|
+
const len = tables.bandShort[sr][i];
|
|
561
|
+
for (let l = 2; l >= 0; l--) {
|
|
562
|
+
p -= len;
|
|
563
|
+
let found = nz[l] === 1;
|
|
564
|
+
if (!found) {
|
|
565
|
+
for (let j = 0; j < len; j++) {
|
|
566
|
+
if (t1[p + j] !== 0) {
|
|
567
|
+
nz[l] = 1;
|
|
568
|
+
found = true;
|
|
569
|
+
break;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (!found) {
|
|
573
|
+
const sf = g1.sf[k + l];
|
|
574
|
+
if (sf >= sfMax) found = true;
|
|
575
|
+
else {
|
|
576
|
+
const v1 = isTab[0][sf];
|
|
577
|
+
const v2 = isTab[1][sf];
|
|
578
|
+
for (let j = 0; j < len; j++) {
|
|
579
|
+
const a = t0[p + j];
|
|
580
|
+
t0[p + j] = a * v1;
|
|
581
|
+
t1[p + j] = a * v2;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
if (found && modeExt & 2) ms(p, len);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
let nonZero = nz[0] | nz[1] | nz[2];
|
|
590
|
+
for (let i = g1.longEnd - 1; i >= 0; i--) {
|
|
591
|
+
const len = tables.bandLong[sr][i];
|
|
592
|
+
p -= len;
|
|
593
|
+
let found = nonZero === 1;
|
|
594
|
+
if (!found) {
|
|
595
|
+
for (let j = 0; j < len; j++) {
|
|
596
|
+
if (t1[p + j] !== 0) {
|
|
597
|
+
nonZero = 1;
|
|
598
|
+
found = true;
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
if (!found) {
|
|
603
|
+
const sf = g1.sf[i === 21 ? 20 : i];
|
|
604
|
+
if (sf >= sfMax) found = true;
|
|
605
|
+
else {
|
|
606
|
+
const v1 = isTab[0][sf];
|
|
607
|
+
const v2 = isTab[1][sf];
|
|
608
|
+
for (let j = 0; j < len; j++) {
|
|
609
|
+
const a = t0[p + j];
|
|
610
|
+
t0[p + j] = a * v1;
|
|
611
|
+
t1[p + j] = a * v2;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
if (found && modeExt & 2) ms(p, len);
|
|
617
|
+
}
|
|
618
|
+
} else if (modeExt & 2) {
|
|
619
|
+
for (let i = 0; i < 576; i++) {
|
|
620
|
+
const a = t0[i];
|
|
621
|
+
const b = t1[i];
|
|
622
|
+
t0[i] = a + b;
|
|
623
|
+
t1[i] = a - b;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
reorder(gr, sr) {
|
|
629
|
+
if (gr.blockType !== 2) return;
|
|
630
|
+
const xr = gr.xr;
|
|
631
|
+
const tmp = this.tmp;
|
|
632
|
+
let p = gr.switchPoint ? (sr !== 8 ? 36 : 72) : 0;
|
|
633
|
+
for (let i = gr.shortStart; i < 13; i++) {
|
|
634
|
+
const len = tables.bandShort[sr][i];
|
|
635
|
+
let d = 0;
|
|
636
|
+
for (let j = 0; j < len; j++) {
|
|
637
|
+
tmp[d++] = xr[p + j];
|
|
638
|
+
tmp[d++] = xr[p + len + j];
|
|
639
|
+
tmp[d++] = xr[p + 2 * len + j];
|
|
640
|
+
}
|
|
641
|
+
for (let j = 0; j < len * 3; j++) xr[p + j] = tmp[j];
|
|
642
|
+
p += len * 3;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
antialias(gr) {
|
|
647
|
+
let n;
|
|
648
|
+
if (gr.blockType === 2) {
|
|
649
|
+
if (!gr.switchPoint) return;
|
|
650
|
+
n = 1;
|
|
651
|
+
} else n = 31;
|
|
652
|
+
const xr = gr.xr;
|
|
653
|
+
const cs = this.s.cs;
|
|
654
|
+
const ca = this.s.ca;
|
|
655
|
+
let base = 18;
|
|
656
|
+
for (let i = 0; i < n; i++, base += 18) {
|
|
657
|
+
for (let j = 0; j < 8; j++) {
|
|
658
|
+
const a = xr[base - 1 - j];
|
|
659
|
+
const b = xr[base + j];
|
|
660
|
+
xr[base - 1 - j] = a * cs[j] - b * ca[j];
|
|
661
|
+
xr[base + j] = b * cs[j] + a * ca[j];
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
imdct(gr, ch) {
|
|
667
|
+
const s = this.s;
|
|
668
|
+
const xr = gr.xr;
|
|
669
|
+
const overlap = this.overlap[ch];
|
|
670
|
+
const sbs = this.sbs;
|
|
671
|
+
const y = this.imdctBuf || (this.imdctBuf = new Float64Array(36));
|
|
672
|
+
for (let sb = 0; sb < 32; sb++) {
|
|
673
|
+
const base = sb * 18;
|
|
674
|
+
let any = false;
|
|
675
|
+
for (let k = 0; k < 18; k++) {
|
|
676
|
+
if (xr[base + k] !== 0) {
|
|
677
|
+
any = true;
|
|
678
|
+
break;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
const ob = sb * 18;
|
|
682
|
+
if (!any) {
|
|
683
|
+
for (let i = 0; i < 18; i++) {
|
|
684
|
+
sbs[i * 32 + sb] = overlap[ob + i];
|
|
685
|
+
overlap[ob + i] = 0;
|
|
686
|
+
}
|
|
687
|
+
} else {
|
|
688
|
+
const short = gr.blockType === 2 && !(gr.switchPoint && sb < 2);
|
|
689
|
+
if (!short) {
|
|
690
|
+
const wt = gr.switchPoint && sb < 2 ? 0 : gr.blockType;
|
|
691
|
+
const w = s.win[wt];
|
|
692
|
+
for (let i = 0; i < 9; i++) {
|
|
693
|
+
let lo = 0;
|
|
694
|
+
let hi = 0;
|
|
695
|
+
const r0 = i * 18;
|
|
696
|
+
const r1 = (i + 18) * 18;
|
|
697
|
+
for (let k = 0; k < 18; k++) {
|
|
698
|
+
const x = xr[base + k];
|
|
699
|
+
lo += x * s.cos36[r0 + k];
|
|
700
|
+
hi += x * s.cos36[r1 + k];
|
|
701
|
+
}
|
|
702
|
+
y[i] = lo;
|
|
703
|
+
y[17 - i] = -lo;
|
|
704
|
+
y[18 + i] = hi;
|
|
705
|
+
y[35 - i] = hi;
|
|
706
|
+
}
|
|
707
|
+
for (let i = 0; i < 36; i++) y[i] *= w[i];
|
|
708
|
+
} else {
|
|
709
|
+
y.fill(0);
|
|
710
|
+
for (let w = 0; w < 3; w++) {
|
|
711
|
+
for (let i = 0; i < 12; i++) {
|
|
712
|
+
let sum = 0;
|
|
713
|
+
const row = i * 6;
|
|
714
|
+
for (let k = 0; k < 6; k++) sum += xr[base + 3 * k + w] * s.cos12[row + k];
|
|
715
|
+
y[6 + 6 * w + i] += sum * s.win12[i];
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
for (let i = 0; i < 18; i++) {
|
|
720
|
+
sbs[i * 32 + sb] = y[i] + overlap[ob + i];
|
|
721
|
+
overlap[ob + i] = y[18 + i];
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
if (sb & 1) for (let i = 1; i < 18; i += 2) sbs[i * 32 + sb] = -sbs[i * 32 + sb];
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
synth(ch, g, out, nch) {
|
|
729
|
+
const s = this.s;
|
|
730
|
+
const v = this.vbuf[ch];
|
|
731
|
+
const sbs = this.sbs;
|
|
732
|
+
const dwin = s.dwin;
|
|
733
|
+
const din = this.dctIn;
|
|
734
|
+
const dout = this.dctOut;
|
|
735
|
+
let off = this.voff[ch];
|
|
736
|
+
for (let t = 0; t < 18; t++) {
|
|
737
|
+
off = (off - 64) & 1023;
|
|
738
|
+
const sb = t * 32;
|
|
739
|
+
for (let k = 0; k < 32; k++) din[k] = sbs[sb + k];
|
|
740
|
+
this.dct(din, dout);
|
|
741
|
+
for (let i = 0; i < 64; i++) {
|
|
742
|
+
const m = 16 + i;
|
|
743
|
+
let val;
|
|
744
|
+
if (m <= 31) val = dout[m];
|
|
745
|
+
else if (m === 32) val = 0;
|
|
746
|
+
else if (m <= 64) val = -dout[64 - m];
|
|
747
|
+
else val = -dout[m - 64];
|
|
748
|
+
v[(off + i) & 1023] = val;
|
|
749
|
+
}
|
|
750
|
+
const acc = this.acc;
|
|
751
|
+
acc.fill(0);
|
|
752
|
+
for (let m = 0; m < 8; m++) {
|
|
753
|
+
const b1 = (off + 128 * m) & 1023;
|
|
754
|
+
const b2 = (b1 + 96) & 1023;
|
|
755
|
+
const d1 = 64 * m;
|
|
756
|
+
const d2 = d1 + 32;
|
|
757
|
+
for (let j = 0; j < 32; j++) acc[j] += v[b1 + j] * dwin[d1 + j] + v[b2 + j] * dwin[d2 + j];
|
|
758
|
+
}
|
|
759
|
+
const outBase = (g * 576 + t * 32) * nch + ch;
|
|
760
|
+
for (let j = 0; j < 32; j++) out[outBase + j * nch] = acc[j];
|
|
761
|
+
}
|
|
762
|
+
this.voff[ch] = off;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
export {
|
|
767
|
+
Mp3Decoder,
|
|
768
|
+
};
|