@hiroosy/baileys-lite 0.4.3 → 0.4.5
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/WAProto/index.d.ts +4 -0
- package/WAProto/parts/Message.part +42 -0
- 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/socket/socket-core.js +3 -3
- package/src/utils/media.js +8 -85
- package/src/utils/message-compose.js +19 -11
- package/src/utils/wa-protocol-core.js +5 -5
- 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,156 @@
|
|
|
1
|
+
import { badData } from '../errors.js';
|
|
2
|
+
import { formats, pcmCodecForBits } from '../codec/pcm.js';
|
|
3
|
+
import { parseRiffInfo } from '../tags.js';
|
|
4
|
+
import { readU64LE } from '../util.js';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function codecFromTag(tag, bits) {
|
|
8
|
+
switch (tag) {
|
|
9
|
+
case 1:
|
|
10
|
+
return pcmCodecForBits(bits, false);
|
|
11
|
+
case 3:
|
|
12
|
+
return bits > 32 ? 'pcm_f64le' : 'pcm_f32le';
|
|
13
|
+
case 6:
|
|
14
|
+
return 'pcm_alaw';
|
|
15
|
+
case 7:
|
|
16
|
+
return 'pcm_mulaw';
|
|
17
|
+
default:
|
|
18
|
+
return 'wav_0x' + tag.toString(16);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class WavDemuxer {
|
|
23
|
+
static probe(head) {
|
|
24
|
+
return head.length >= 12 && (head.toString('latin1', 0, 4) === 'RIFF' || head.toString('latin1', 0, 4) === 'RF64') && head.toString('latin1', 8, 12) === 'WAVE';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static async open(reader) {
|
|
28
|
+
const demuxer = new WavDemuxer(reader);
|
|
29
|
+
await demuxer.init();
|
|
30
|
+
return demuxer;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
constructor(reader) {
|
|
34
|
+
this.reader = reader;
|
|
35
|
+
this.format = 'wav';
|
|
36
|
+
this.tags = {};
|
|
37
|
+
this.streams = [];
|
|
38
|
+
this.cursor = 0;
|
|
39
|
+
this.dataStart = 0;
|
|
40
|
+
this.dataEnd = 0;
|
|
41
|
+
this.blockAlign = 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async init() {
|
|
45
|
+
const r = this.reader;
|
|
46
|
+
const head = await r.readExact(12);
|
|
47
|
+
const rf64 = head.toString('latin1', 0, 4) === 'RF64';
|
|
48
|
+
let fmt = null;
|
|
49
|
+
let dataSize64 = -1;
|
|
50
|
+
let found = false;
|
|
51
|
+
while (r.remaining >= 8) {
|
|
52
|
+
const chunk = await r.readExact(8);
|
|
53
|
+
const id = chunk.toString('latin1', 0, 4);
|
|
54
|
+
let size = chunk.readUInt32LE(4);
|
|
55
|
+
const body = r.pos;
|
|
56
|
+
if (id === 'fmt ') {
|
|
57
|
+
const buf = await r.readExact(Math.min(size, 40));
|
|
58
|
+
const tagRaw = buf.readUInt16LE(0);
|
|
59
|
+
fmt = {
|
|
60
|
+
tag: tagRaw,
|
|
61
|
+
channels: buf.readUInt16LE(2),
|
|
62
|
+
sampleRate: buf.readUInt32LE(4),
|
|
63
|
+
byteRate: buf.readUInt32LE(8),
|
|
64
|
+
blockAlign: buf.readUInt16LE(12),
|
|
65
|
+
bits: buf.readUInt16LE(14),
|
|
66
|
+
};
|
|
67
|
+
if (tagRaw === 0xfffe && buf.length >= 40) fmt.tag = buf.readUInt16LE(24);
|
|
68
|
+
} else if (id === 'ds64') {
|
|
69
|
+
const buf = await r.readExact(Math.min(size, 28));
|
|
70
|
+
dataSize64 = readU64LE(buf, 8);
|
|
71
|
+
} else if (id === 'data') {
|
|
72
|
+
if (rf64 && size === 0xffffffff && dataSize64 >= 0) size = dataSize64;
|
|
73
|
+
this.dataStart = body;
|
|
74
|
+
const avail = r.size - body;
|
|
75
|
+
if (size === 0 || size > avail) size = avail;
|
|
76
|
+
this.dataEnd = body + size;
|
|
77
|
+
found = true;
|
|
78
|
+
if (!fmt) {
|
|
79
|
+
r.seek(this.dataEnd + (size & 1));
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const next = this.dataEnd + (size & 1);
|
|
83
|
+
if (next + 8 <= r.size) {
|
|
84
|
+
r.seek(next);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
} else if (id === 'LIST') {
|
|
89
|
+
if (size >= 4 && size < 1 << 20) {
|
|
90
|
+
const buf = await r.readExact(size);
|
|
91
|
+
if (buf.toString('latin1', 0, 4) === 'INFO') Object.assign(this.tags, parseRiffInfo(buf.subarray(4)));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
r.seek(body + size + (size & 1));
|
|
95
|
+
}
|
|
96
|
+
if (!fmt) throw badData('wav: missing fmt chunk');
|
|
97
|
+
if (!found) throw badData('wav: missing data chunk');
|
|
98
|
+
if (fmt.channels < 1 || fmt.sampleRate < 1) throw badData('wav: invalid format');
|
|
99
|
+
const codec = codecFromTag(fmt.tag, fmt.bits);
|
|
100
|
+
const info = formats[codec];
|
|
101
|
+
this.blockAlign = fmt.blockAlign || fmt.channels * Math.ceil(fmt.bits / 8);
|
|
102
|
+
if (info && this.blockAlign !== fmt.channels * info.bytes) this.blockAlign = fmt.channels * info.bytes;
|
|
103
|
+
const frames = Math.floor((this.dataEnd - this.dataStart) / this.blockAlign);
|
|
104
|
+
this.dataEnd = this.dataStart + frames * this.blockAlign;
|
|
105
|
+
this.cursor = this.dataStart;
|
|
106
|
+
this.streams.push({
|
|
107
|
+
index: 0,
|
|
108
|
+
type: 'audio',
|
|
109
|
+
codec,
|
|
110
|
+
timeScale: fmt.sampleRate,
|
|
111
|
+
sampleRate: fmt.sampleRate,
|
|
112
|
+
channels: fmt.channels,
|
|
113
|
+
bitsPerSample: info ? info.bits : fmt.bits,
|
|
114
|
+
bitRate: fmt.sampleRate * this.blockAlign * 8,
|
|
115
|
+
duration: frames / fmt.sampleRate,
|
|
116
|
+
frames,
|
|
117
|
+
extradata: null,
|
|
118
|
+
});
|
|
119
|
+
this.duration = frames / fmt.sampleRate;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async seek(seconds) {
|
|
123
|
+
const rate = this.streams[0].sampleRate;
|
|
124
|
+
const frame = Math.max(0, Math.min(Math.floor(seconds * rate), (this.dataEnd - this.dataStart) / this.blockAlign));
|
|
125
|
+
this.cursor = this.dataStart + frame * this.blockAlign;
|
|
126
|
+
return frame / rate;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async readPacket() {
|
|
130
|
+
if (this.cursor >= this.dataEnd) return null;
|
|
131
|
+
const want = Math.min(4096 * this.blockAlign, this.dataEnd - this.cursor);
|
|
132
|
+
this.reader.seek(this.cursor);
|
|
133
|
+
const data = await this.reader.read(want);
|
|
134
|
+
if (data.length === 0) return null;
|
|
135
|
+
const usable = data.length - (data.length % this.blockAlign);
|
|
136
|
+
if (usable === 0) return null;
|
|
137
|
+
const frame = (this.cursor - this.dataStart) / this.blockAlign;
|
|
138
|
+
this.cursor += usable;
|
|
139
|
+
return {
|
|
140
|
+
stream: 0,
|
|
141
|
+
pts: frame,
|
|
142
|
+
dts: frame,
|
|
143
|
+
duration: usable / this.blockAlign,
|
|
144
|
+
key: true,
|
|
145
|
+
data: usable === data.length ? data : data.subarray(0, usable),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async close() {
|
|
150
|
+
await this.reader.close();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export {
|
|
155
|
+
WavDemuxer,
|
|
156
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class FFError extends Error {
|
|
2
|
+
constructor(code, message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'FFError';
|
|
5
|
+
this.code = code;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const badArg = (message) => new FFError('EINVAL', message);
|
|
10
|
+
const badData = (message) => new FFError('EFORMAT', message);
|
|
11
|
+
const noSupport = (message) => new FFError('EUNSUPPORTED', message);
|
|
12
|
+
|
|
13
|
+
function wrap(error) {
|
|
14
|
+
if (error instanceof FFError) return error;
|
|
15
|
+
if (error && typeof error.code === 'string' && error.syscall) return error;
|
|
16
|
+
const wrapped = new FFError('EFORMAT', 'invalid or corrupt data: ' + (error && error.message ? error.message : String(error)));
|
|
17
|
+
wrapped.cause = error;
|
|
18
|
+
return wrapped;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
FFError,
|
|
23
|
+
badArg,
|
|
24
|
+
badData,
|
|
25
|
+
noSupport,
|
|
26
|
+
wrap,
|
|
27
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
class Trim {
|
|
2
|
+
constructor(skipFrames, maxFrames) {
|
|
3
|
+
this.skip = Math.max(0, skipFrames);
|
|
4
|
+
this.remaining = maxFrames === undefined ? Infinity : Math.max(0, maxFrames);
|
|
5
|
+
this.done = this.remaining === 0;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
process(data, frames, channels) {
|
|
9
|
+
let from = 0;
|
|
10
|
+
if (this.skip > 0) {
|
|
11
|
+
const drop = Math.min(this.skip, frames);
|
|
12
|
+
this.skip -= drop;
|
|
13
|
+
from = drop;
|
|
14
|
+
}
|
|
15
|
+
let count = frames - from;
|
|
16
|
+
if (count <= 0) return null;
|
|
17
|
+
if (count > this.remaining) count = this.remaining;
|
|
18
|
+
this.remaining -= count;
|
|
19
|
+
if (this.remaining <= 0) this.done = true;
|
|
20
|
+
if (count <= 0) return null;
|
|
21
|
+
return { data: data.subarray(from * channels, (from + count) * channels), frames: count };
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class Gain {
|
|
26
|
+
constructor(options, channels) {
|
|
27
|
+
this.volume = options.volume === undefined ? 1 : options.volume;
|
|
28
|
+
this.fadeIn = Math.max(0, options.fadeInFrames || 0);
|
|
29
|
+
this.fadeOutStart = options.fadeOutFrames > 0 && options.totalFrames > 0 ? Math.max(0, options.totalFrames - options.fadeOutFrames) : Infinity;
|
|
30
|
+
this.fadeOut = Math.max(0, options.fadeOutFrames || 0);
|
|
31
|
+
this.totalFrames = options.totalFrames || 0;
|
|
32
|
+
this.channels = channels;
|
|
33
|
+
this.position = 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get active() {
|
|
37
|
+
return this.volume !== 1 || this.fadeIn > 0 || this.fadeOut > 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
process(data, frames) {
|
|
41
|
+
const ch = this.channels;
|
|
42
|
+
const start = this.position;
|
|
43
|
+
const end = start + frames;
|
|
44
|
+
this.position = end;
|
|
45
|
+
const needFade = (this.fadeIn > 0 && start < this.fadeIn) || (this.fadeOut > 0 && end > this.fadeOutStart);
|
|
46
|
+
if (!needFade) {
|
|
47
|
+
if (this.volume === 1) return;
|
|
48
|
+
const v = this.volume;
|
|
49
|
+
for (let i = 0, n = frames * ch; i < n; i++) data[i] *= v;
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
for (let f = 0; f < frames; f++) {
|
|
53
|
+
const idx = start + f;
|
|
54
|
+
let g = this.volume;
|
|
55
|
+
if (this.fadeIn > 0 && idx < this.fadeIn) g *= idx / this.fadeIn;
|
|
56
|
+
if (idx >= this.fadeOutStart) {
|
|
57
|
+
const rest = this.totalFrames - idx;
|
|
58
|
+
g *= rest <= 0 ? 0 : rest / this.fadeOut;
|
|
59
|
+
}
|
|
60
|
+
const o = f * ch;
|
|
61
|
+
for (let c = 0; c < ch; c++) data[o + c] *= g;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export {
|
|
67
|
+
Trim,
|
|
68
|
+
Gain,
|
|
69
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const sqrtHalf = Math.SQRT1_2;
|
|
2
|
+
|
|
3
|
+
function buildMatrix(inCh, outCh) {
|
|
4
|
+
const m = [];
|
|
5
|
+
for (let o = 0; o < outCh; o++) m.push(new Float64Array(inCh));
|
|
6
|
+
if (outCh === 1) {
|
|
7
|
+
for (let i = 0; i < inCh; i++) m[0][i] = 1 / inCh;
|
|
8
|
+
return m;
|
|
9
|
+
}
|
|
10
|
+
if (inCh === 1) {
|
|
11
|
+
for (let o = 0; o < outCh; o++) m[o][0] = 1;
|
|
12
|
+
return m;
|
|
13
|
+
}
|
|
14
|
+
if (outCh === 2 && inCh > 2) {
|
|
15
|
+
const left = new Float64Array(inCh);
|
|
16
|
+
const right = new Float64Array(inCh);
|
|
17
|
+
left[0] = 1;
|
|
18
|
+
right[1] = 1;
|
|
19
|
+
if (inCh === 3) {
|
|
20
|
+
left[2] = sqrtHalf;
|
|
21
|
+
right[2] = sqrtHalf;
|
|
22
|
+
} else if (inCh === 4) {
|
|
23
|
+
left[2] = sqrtHalf;
|
|
24
|
+
right[3] = sqrtHalf;
|
|
25
|
+
} else if (inCh === 5) {
|
|
26
|
+
left[2] = sqrtHalf;
|
|
27
|
+
right[2] = sqrtHalf;
|
|
28
|
+
left[3] = sqrtHalf;
|
|
29
|
+
right[4] = sqrtHalf;
|
|
30
|
+
} else if (inCh >= 6) {
|
|
31
|
+
left[2] = sqrtHalf;
|
|
32
|
+
right[2] = sqrtHalf;
|
|
33
|
+
left[4] = sqrtHalf;
|
|
34
|
+
right[5] = sqrtHalf;
|
|
35
|
+
if (inCh >= 8) {
|
|
36
|
+
left[6] = sqrtHalf;
|
|
37
|
+
right[7] = sqrtHalf;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let peak = 0;
|
|
41
|
+
for (const row of [left, right]) {
|
|
42
|
+
let sum = 0;
|
|
43
|
+
for (let i = 0; i < inCh; i++) sum += Math.abs(row[i]);
|
|
44
|
+
if (sum > peak) peak = sum;
|
|
45
|
+
}
|
|
46
|
+
for (let i = 0; i < inCh; i++) {
|
|
47
|
+
left[i] /= peak;
|
|
48
|
+
right[i] /= peak;
|
|
49
|
+
}
|
|
50
|
+
m[0] = left;
|
|
51
|
+
m[1] = right;
|
|
52
|
+
return m;
|
|
53
|
+
}
|
|
54
|
+
for (let o = 0; o < Math.min(inCh, outCh); o++) m[o][o] = 1;
|
|
55
|
+
return m;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
class ChannelMixer {
|
|
59
|
+
constructor(inCh, outCh) {
|
|
60
|
+
this.inCh = inCh;
|
|
61
|
+
this.outCh = outCh;
|
|
62
|
+
this.matrix = buildMatrix(inCh, outCh);
|
|
63
|
+
this.out = new Float64Array(0);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
process(data, frames) {
|
|
67
|
+
const inCh = this.inCh;
|
|
68
|
+
const outCh = this.outCh;
|
|
69
|
+
if (this.out.length < frames * outCh) this.out = new Float64Array(frames * outCh);
|
|
70
|
+
const out = this.out;
|
|
71
|
+
for (let o = 0; o < outCh; o++) {
|
|
72
|
+
const row = this.matrix[o];
|
|
73
|
+
const idx = [];
|
|
74
|
+
for (let i = 0; i < inCh; i++) if (row[i] !== 0) idx.push(i);
|
|
75
|
+
if (idx.length === 1 && row[idx[0]] === 1) {
|
|
76
|
+
const src = idx[0];
|
|
77
|
+
for (let f = 0, a = src, b = o; f < frames; f++, a += inCh, b += outCh) out[b] = data[a];
|
|
78
|
+
} else if (idx.length === 0) {
|
|
79
|
+
for (let f = 0, b = o; f < frames; f++, b += outCh) out[b] = 0;
|
|
80
|
+
} else {
|
|
81
|
+
for (let f = 0, b = o; f < frames; f++, b += outCh) {
|
|
82
|
+
let s = 0;
|
|
83
|
+
const base = f * inCh;
|
|
84
|
+
for (let k = 0; k < idx.length; k++) s += row[idx[k]] * data[base + idx[k]];
|
|
85
|
+
out[b] = s;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return { data: out, frames };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export {
|
|
94
|
+
ChannelMixer,
|
|
95
|
+
};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
const phases = 512;
|
|
2
|
+
const beta = 9;
|
|
3
|
+
|
|
4
|
+
function bessel0(x) {
|
|
5
|
+
let sum = 1;
|
|
6
|
+
let term = 1;
|
|
7
|
+
const q = (x * x) / 4;
|
|
8
|
+
for (let k = 1; k < 60; k++) {
|
|
9
|
+
term *= q / (k * k);
|
|
10
|
+
sum += term;
|
|
11
|
+
if (term < 1e-14 * sum) break;
|
|
12
|
+
}
|
|
13
|
+
return sum;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function sinc(x) {
|
|
17
|
+
if (x === 0) return 1;
|
|
18
|
+
const a = Math.PI * x;
|
|
19
|
+
return Math.sin(a) / a;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function gcd(a, b) {
|
|
23
|
+
while (b) {
|
|
24
|
+
const t = a % b;
|
|
25
|
+
a = b;
|
|
26
|
+
b = t;
|
|
27
|
+
}
|
|
28
|
+
return a;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function buildKernel(inRate, outRate) {
|
|
32
|
+
const cutoff = Math.min(1, outRate / inRate) * 0.97;
|
|
33
|
+
const half = Math.ceil(12 / cutoff);
|
|
34
|
+
const taps = half * 2;
|
|
35
|
+
const g = gcd(inRate, outRate);
|
|
36
|
+
const upFactor = outRate / g;
|
|
37
|
+
const exact = upFactor * taps <= 2000000;
|
|
38
|
+
const rows = exact ? upFactor : phases + 1;
|
|
39
|
+
const divisor = exact ? upFactor : phases;
|
|
40
|
+
const table = new Float64Array(rows * taps);
|
|
41
|
+
const norm = bessel0(beta);
|
|
42
|
+
for (let p = 0; p < rows; p++) {
|
|
43
|
+
const frac = p / divisor;
|
|
44
|
+
let sum = 0;
|
|
45
|
+
const row = p * taps;
|
|
46
|
+
for (let k = 0; k < taps; k++) {
|
|
47
|
+
const x = k - half + 1 - frac;
|
|
48
|
+
const r = x / half;
|
|
49
|
+
const win = Math.abs(r) >= 1 ? 0 : bessel0(beta * Math.sqrt(1 - r * r)) / norm;
|
|
50
|
+
const v = cutoff * sinc(cutoff * x) * win;
|
|
51
|
+
table[row + k] = v;
|
|
52
|
+
sum += v;
|
|
53
|
+
}
|
|
54
|
+
for (let k = 0; k < taps; k++) table[row + k] /= sum;
|
|
55
|
+
}
|
|
56
|
+
return { half, taps, table, exact, upFactor, downFactor: inRate / g };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
class Resampler {
|
|
60
|
+
constructor(inRate, outRate, channels) {
|
|
61
|
+
this.inRate = inRate;
|
|
62
|
+
this.outRate = outRate;
|
|
63
|
+
this.channels = channels;
|
|
64
|
+
const k = buildKernel(inRate, outRate);
|
|
65
|
+
this.half = k.half;
|
|
66
|
+
this.taps = k.taps;
|
|
67
|
+
this.table = k.table;
|
|
68
|
+
this.exact = k.exact;
|
|
69
|
+
this.upFactor = k.upFactor;
|
|
70
|
+
this.downFactor = k.downFactor;
|
|
71
|
+
this.coef = new Float64Array(this.taps);
|
|
72
|
+
this.buf = new Float64Array(Math.max(4096, this.taps * 2) * channels);
|
|
73
|
+
this.bufFrames = this.half;
|
|
74
|
+
this.base = -this.half;
|
|
75
|
+
this.outCount = 0;
|
|
76
|
+
this.totalIn = 0;
|
|
77
|
+
this.out = new Float64Array(0);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
append(data, frames) {
|
|
81
|
+
const ch = this.channels;
|
|
82
|
+
const need = (this.bufFrames + frames) * ch;
|
|
83
|
+
if (need > this.buf.length) {
|
|
84
|
+
let cap = this.buf.length;
|
|
85
|
+
while (cap < need) cap *= 2;
|
|
86
|
+
const next = new Float64Array(cap);
|
|
87
|
+
next.set(this.buf.subarray(0, this.bufFrames * ch));
|
|
88
|
+
this.buf = next;
|
|
89
|
+
}
|
|
90
|
+
if (data) this.buf.set(data.subarray(0, frames * ch), this.bufFrames * ch);
|
|
91
|
+
else this.buf.fill(0, this.bufFrames * ch, need);
|
|
92
|
+
this.bufFrames += frames;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
run(limit) {
|
|
96
|
+
const ch = this.channels;
|
|
97
|
+
const half = this.half;
|
|
98
|
+
const taps = this.taps;
|
|
99
|
+
const table = this.table;
|
|
100
|
+
const coef = this.coef;
|
|
101
|
+
const inRate = this.inRate;
|
|
102
|
+
const outRate = this.outRate;
|
|
103
|
+
const availEnd = this.base + this.bufFrames;
|
|
104
|
+
const maxOut = Math.ceil(((availEnd + 1) * outRate) / inRate) - this.outCount + 2;
|
|
105
|
+
if (this.out.length < maxOut * ch) this.out = new Float64Array(maxOut * ch);
|
|
106
|
+
const out = this.out;
|
|
107
|
+
const buf = this.buf;
|
|
108
|
+
let produced = 0;
|
|
109
|
+
const exact = this.exact;
|
|
110
|
+
const upFactor = this.upFactor;
|
|
111
|
+
const downFactor = this.downFactor;
|
|
112
|
+
for (;;) {
|
|
113
|
+
let i;
|
|
114
|
+
let tab;
|
|
115
|
+
let off;
|
|
116
|
+
if (exact) {
|
|
117
|
+
const num = this.outCount * downFactor;
|
|
118
|
+
i = Math.floor(num / upFactor);
|
|
119
|
+
if (limit !== undefined && i >= limit) break;
|
|
120
|
+
if (i + half >= availEnd) break;
|
|
121
|
+
tab = table;
|
|
122
|
+
off = (num - i * upFactor) * taps;
|
|
123
|
+
} else {
|
|
124
|
+
const num = this.outCount * inRate;
|
|
125
|
+
i = Math.floor(num / outRate);
|
|
126
|
+
if (limit !== undefined && i >= limit) break;
|
|
127
|
+
if (i + half >= availEnd) break;
|
|
128
|
+
const phasePos = ((num - i * outRate) / outRate) * phases;
|
|
129
|
+
const p = Math.floor(phasePos);
|
|
130
|
+
const w = phasePos - p;
|
|
131
|
+
const r0 = p * taps;
|
|
132
|
+
const r1 = r0 + taps;
|
|
133
|
+
for (let k = 0; k < taps; k++) coef[k] = table[r0 + k] * (1 - w) + table[r1 + k] * w;
|
|
134
|
+
tab = coef;
|
|
135
|
+
off = 0;
|
|
136
|
+
}
|
|
137
|
+
const local = (i - half + 1 - this.base) * ch;
|
|
138
|
+
if (ch === 1) {
|
|
139
|
+
let s = 0;
|
|
140
|
+
for (let k = 0; k < taps; k++) s += tab[off + k] * buf[local + k];
|
|
141
|
+
out[produced] = s;
|
|
142
|
+
} else if (ch === 2) {
|
|
143
|
+
let s0 = 0;
|
|
144
|
+
let s1 = 0;
|
|
145
|
+
for (let k = 0, o = local; k < taps; k++, o += 2) {
|
|
146
|
+
const c = tab[off + k];
|
|
147
|
+
s0 += c * buf[o];
|
|
148
|
+
s1 += c * buf[o + 1];
|
|
149
|
+
}
|
|
150
|
+
out[produced * 2] = s0;
|
|
151
|
+
out[produced * 2 + 1] = s1;
|
|
152
|
+
} else {
|
|
153
|
+
for (let c = 0; c < ch; c++) {
|
|
154
|
+
let s = 0;
|
|
155
|
+
for (let k = 0, o = local + c; k < taps; k++, o += ch) s += tab[off + k] * buf[o];
|
|
156
|
+
out[produced * ch + c] = s;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
produced++;
|
|
160
|
+
this.outCount++;
|
|
161
|
+
}
|
|
162
|
+
const keepFrom = (exact ? Math.floor((this.outCount * downFactor) / upFactor) : Math.floor((this.outCount * inRate) / outRate)) - half + 1;
|
|
163
|
+
const drop = Math.min(this.bufFrames, Math.max(0, keepFrom - this.base));
|
|
164
|
+
if (drop > 0) {
|
|
165
|
+
buf.copyWithin(0, drop * ch, this.bufFrames * ch);
|
|
166
|
+
this.bufFrames -= drop;
|
|
167
|
+
this.base += drop;
|
|
168
|
+
}
|
|
169
|
+
return { data: out, frames: produced };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
process(data, frames) {
|
|
173
|
+
this.totalIn += frames;
|
|
174
|
+
this.append(data, frames);
|
|
175
|
+
return this.run(undefined);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
flush() {
|
|
179
|
+
const total = this.totalIn;
|
|
180
|
+
this.append(null, this.half + 1);
|
|
181
|
+
return this.run(total);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export {
|
|
186
|
+
Resampler,
|
|
187
|
+
};
|