sophontalk-services 0.0.4 → 0.0.6
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.
|
@@ -1,460 +1,100 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { env as l } from "onnxruntime-web";
|
|
2
|
+
const m = 16e3, k = 25, u = 10, f = 80, p = 4, g = 0.97, _ = 1e-10, M = 20, R = !0, F = !0, w = 25, L = 1e3 / w, y = (s) => {
|
|
3
|
+
if (s === 128) return 4;
|
|
4
|
+
if (s === 96) return 3;
|
|
5
|
+
throw new Error(`Unsupported crop size: ${s}. Only nano (96) and tiny (128) are supported.`);
|
|
6
|
+
}, P = (s, e, t) => {
|
|
7
|
+
if (e <= 1) return { nextIndex: 0, nextDirection: 1 };
|
|
8
|
+
let r = s + t, a = t;
|
|
9
|
+
return r >= e ? (r = e - 2, a = -1) : r < 0 && (r = 1, a = 1), { nextIndex: r, nextDirection: a };
|
|
9
10
|
};
|
|
10
|
-
async function
|
|
11
|
+
async function b(s = {}) {
|
|
11
12
|
const {
|
|
12
|
-
jsonUrl:
|
|
13
|
-
zipUrl:
|
|
14
|
-
} =
|
|
15
|
-
fetch(
|
|
16
|
-
fetch(
|
|
13
|
+
jsonUrl: e = "/complete_dataset.json",
|
|
14
|
+
zipUrl: t = "/processed_images.zip"
|
|
15
|
+
} = s, [r, a] = await Promise.all([
|
|
16
|
+
fetch(e),
|
|
17
|
+
fetch(t)
|
|
17
18
|
]);
|
|
18
|
-
if (!t.ok) throw new Error(`无法加载 ${n}`);
|
|
19
19
|
if (!r.ok) throw new Error(`无法加载 ${e}`);
|
|
20
|
-
|
|
20
|
+
if (!a.ok) throw new Error(`无法加载 ${t}`);
|
|
21
|
+
const o = await r.json(), c = await a.arrayBuffer();
|
|
21
22
|
return { dataset: o, zipBuffer: c };
|
|
22
23
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const i = (n) => 700 * (Math.exp(n / 1127) - 1), r = (n) => 1127 * Math.log(1 + n / 700), l = r(a), A = r(s), w = new Float32Array(e + 2);
|
|
40
|
-
for (let n = 0; n < e + 2; n++)
|
|
41
|
-
w[n] = l + (A - l) * n / (e + 1);
|
|
42
|
-
const _ = w.map(i).map((n) => n * t / o), f = Math.floor(t / 2) + 1, m = [];
|
|
43
|
-
for (let n = 1; n <= e; n++) {
|
|
44
|
-
const c = _[n - 1], F = _[n], M = _[n + 1], I = Math.ceil(c), T = Math.floor(M), E = Math.max(0, I), p = Math.min(f - 1, T), B = p - E + 1;
|
|
45
|
-
if (B <= 0) {
|
|
46
|
-
m.push({ startBin: 0, values: new Float32Array(0) });
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
const R = new Float32Array(B);
|
|
50
|
-
for (let d = E; d <= p; d++) {
|
|
51
|
-
let g = 0;
|
|
52
|
-
d <= F ? g = (d - c) / (F - c) : g = (M - d) / (M - F), R[d - E] = g;
|
|
53
|
-
}
|
|
54
|
-
m.push({ startBin: E, values: R });
|
|
55
|
-
}
|
|
56
|
-
return m;
|
|
57
|
-
}
|
|
58
|
-
const q = Q(
|
|
59
|
-
u,
|
|
60
|
-
P,
|
|
61
|
-
y,
|
|
62
|
-
b
|
|
63
|
-
);
|
|
64
|
-
function K(e) {
|
|
65
|
-
for (let t = 0; t < e.length; t++)
|
|
66
|
-
e[t] *= v[t];
|
|
67
|
-
}
|
|
68
|
-
function Y(e, t) {
|
|
69
|
-
for (let o = e.length - 1; o > 0; o--)
|
|
70
|
-
e[o] -= t * e[o - 1];
|
|
71
|
-
e[0] -= t * e[0];
|
|
72
|
-
}
|
|
73
|
-
function V(e) {
|
|
74
|
-
const t = e.length;
|
|
75
|
-
if (t === 0) return;
|
|
76
|
-
let o = 0;
|
|
77
|
-
for (let s = 0; s < t; s++) o += e[s];
|
|
78
|
-
const a = o / t;
|
|
79
|
-
for (let s = 0; s < t; s++) e[s] -= a;
|
|
80
|
-
}
|
|
81
|
-
function Z(e, t) {
|
|
82
|
-
const o = e.length;
|
|
83
|
-
for (let a = 0; a < o; a++) {
|
|
84
|
-
const s = t[a * 2], i = t[a * 2 + 1];
|
|
85
|
-
e[a] = s * s + i * i;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
function j(e) {
|
|
89
|
-
const t = e.length;
|
|
90
|
-
if (t < 2) return;
|
|
91
|
-
let o = 0;
|
|
92
|
-
for (let r = 0; r < t; r++) o += e[r];
|
|
93
|
-
const a = o / t;
|
|
94
|
-
let s = 0;
|
|
95
|
-
for (let r = 0; r < t; r++) {
|
|
96
|
-
const l = e[r] - a;
|
|
97
|
-
s += l * l;
|
|
98
|
-
}
|
|
99
|
-
const i = Math.sqrt(s / (t - 1));
|
|
100
|
-
if (i > 1e-8) {
|
|
101
|
-
const r = 1 / i;
|
|
102
|
-
for (let l = 0; l < t; l++)
|
|
103
|
-
e[l] = (e[l] - a) * r;
|
|
104
|
-
} else
|
|
105
|
-
for (let r = 0; r < t; r++)
|
|
106
|
-
e[r] -= a;
|
|
107
|
-
}
|
|
108
|
-
async function z(e) {
|
|
109
|
-
let t = e.leftChannel;
|
|
110
|
-
if (e.rightChannel) {
|
|
111
|
-
const f = e.rightChannel, m = new Float32Array(t.length);
|
|
112
|
-
for (let n = 0; n < t.length; n++)
|
|
113
|
-
m[n] = (t[n] + f[n]) * 0.5;
|
|
114
|
-
t = m;
|
|
115
|
-
}
|
|
116
|
-
if (e.sampleRate !== y) {
|
|
117
|
-
const f = await O(1, e.sampleRate, y, {
|
|
118
|
-
converterType: H.SRC_SINC_MEDIUM_QUALITY
|
|
119
|
-
});
|
|
120
|
-
t = f.simple(t), f.destroy();
|
|
121
|
-
}
|
|
122
|
-
const o = Math.floor((t.length - h) / C) + 1;
|
|
123
|
-
if (o <= 0)
|
|
124
|
-
throw new Error("特征提取失败:音频时长过短。");
|
|
125
|
-
const a = Math.ceil(o / S) * S, s = a * u, i = new Float32Array(s), r = new Float32Array(h), l = N.createComplexArray(), A = N.createComplexArray(), w = new Float32Array(Math.floor(P / 2) + 1);
|
|
126
|
-
for (let f = 0; f < o; f++) {
|
|
127
|
-
const m = f * C;
|
|
128
|
-
r.set(t.subarray(m, m + h)), V(r), Y(r, W), K(r), l.fill(0);
|
|
129
|
-
for (let c = 0; c < h; c++)
|
|
130
|
-
l[c * 2] = r[c];
|
|
131
|
-
N.transform(A, l), Z(w, A);
|
|
132
|
-
const n = f * u;
|
|
133
|
-
for (let c = 0; c < u; c++) {
|
|
134
|
-
const F = q[c];
|
|
135
|
-
let M = 0;
|
|
136
|
-
const I = F.values, T = F.startBin, E = I.length;
|
|
137
|
-
for (let p = 0; p < E; p++)
|
|
138
|
-
M += w[T + p] * I[p];
|
|
139
|
-
M = M < L ? L : M, i[n + c] = Math.log(M);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
if (a > o) {
|
|
143
|
-
const f = (o - 1) * u, m = f + u, n = i.subarray(f, m);
|
|
144
|
-
for (let c = o; c < a; c++)
|
|
145
|
-
i.set(n, c * u);
|
|
146
|
-
}
|
|
147
|
-
j(i);
|
|
148
|
-
const _ = a / S;
|
|
149
|
-
return {
|
|
150
|
-
features: i,
|
|
151
|
-
dimensions: [_, S, u]
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
self.addEventListener(
|
|
155
|
-
"message",
|
|
156
|
-
async (e) => {
|
|
157
|
-
try {
|
|
158
|
-
const t = e.data, o = await z(t);
|
|
159
|
-
self.postMessage(
|
|
160
|
-
{ status: "success", payload: o },
|
|
161
|
-
{ transfer: [o.features.buffer] }
|
|
162
|
-
);
|
|
163
|
-
} catch (t) {
|
|
164
|
-
self.postMessage({ status: "error", error: t.message });
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
);
|
|
168
|
-
`, u = typeof self < "u" && self.Blob && new Blob(["URL.revokeObjectURL(import.meta.url);", h], { type: "text/javascript;charset=utf-8" });
|
|
169
|
-
function p(a) {
|
|
170
|
-
let n;
|
|
171
|
-
try {
|
|
172
|
-
if (n = u && (self.URL || self.webkitURL).createObjectURL(u), !n) throw "";
|
|
173
|
-
const e = new Worker(n, {
|
|
174
|
-
type: "module",
|
|
175
|
-
name: a?.name
|
|
176
|
-
});
|
|
177
|
-
return e.addEventListener("error", () => {
|
|
178
|
-
(self.URL || self.webkitURL).revokeObjectURL(n);
|
|
179
|
-
}), e;
|
|
180
|
-
} catch {
|
|
181
|
-
return new Worker(
|
|
182
|
-
"data:text/javascript;charset=utf-8," + encodeURIComponent(h),
|
|
183
|
-
{
|
|
184
|
-
type: "module",
|
|
185
|
-
name: a?.name
|
|
186
|
-
}
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
class A {
|
|
191
|
-
worker = new p();
|
|
192
|
-
async process(n) {
|
|
193
|
-
const e = n.getChannelData(0), t = n.numberOfChannels > 1 ? n.getChannelData(1) : void 0, r = {
|
|
194
|
-
leftChannel: e,
|
|
195
|
-
rightChannel: t,
|
|
196
|
-
sampleRate: n.sampleRate
|
|
197
|
-
}, o = [e.buffer];
|
|
198
|
-
return t && o.push(t.buffer), new Promise((c, f) => {
|
|
199
|
-
const l = (s) => {
|
|
200
|
-
s.data?.status === "success" ? (this.worker.removeEventListener("message", l), this.worker.removeEventListener("error", i), c(s.data.payload)) : s.data?.status === "error" && (this.worker.removeEventListener("message", l), this.worker.removeEventListener("error", i), f(new Error(s.data.error || "特征提取失败")));
|
|
201
|
-
}, i = (s) => {
|
|
202
|
-
this.worker.removeEventListener("message", l), this.worker.removeEventListener("error", i), f(s);
|
|
24
|
+
class v {
|
|
25
|
+
worker = new Worker(
|
|
26
|
+
new URL("./workers/feature.worker.js", import.meta.url),
|
|
27
|
+
{ type: "module" }
|
|
28
|
+
);
|
|
29
|
+
async process(e) {
|
|
30
|
+
const t = e.getChannelData(0), r = e.numberOfChannels > 1 ? e.getChannelData(1) : void 0, a = {
|
|
31
|
+
leftChannel: t,
|
|
32
|
+
rightChannel: r,
|
|
33
|
+
sampleRate: e.sampleRate
|
|
34
|
+
}, o = [t.buffer];
|
|
35
|
+
return r && o.push(r.buffer), new Promise((c, d) => {
|
|
36
|
+
const i = (n) => {
|
|
37
|
+
n.data?.status === "success" ? (this.worker.removeEventListener("message", i), this.worker.removeEventListener("error", h), c(n.data.payload)) : n.data?.status === "error" && (this.worker.removeEventListener("message", i), this.worker.removeEventListener("error", h), d(new Error(n.data.error || "特征提取失败")));
|
|
38
|
+
}, h = (n) => {
|
|
39
|
+
this.worker.removeEventListener("message", i), this.worker.removeEventListener("error", h), d(n);
|
|
203
40
|
};
|
|
204
|
-
this.worker.addEventListener("message",
|
|
41
|
+
this.worker.addEventListener("message", i), this.worker.addEventListener("error", h), this.worker.postMessage(a, o);
|
|
205
42
|
});
|
|
206
43
|
}
|
|
207
44
|
dispose() {
|
|
208
45
|
this.worker.terminate();
|
|
209
46
|
}
|
|
210
47
|
}
|
|
211
|
-
const d = `import { env as k, InferenceSession as $, Tensor as W } from "onnxruntime-web";
|
|
212
|
-
import q from "jszip";
|
|
213
|
-
const V = 80, G = 4, H = 25, j = 1e3 / H, J = (t) => {
|
|
214
|
-
if (t === 128) return 4;
|
|
215
|
-
if (t === 96) return 3;
|
|
216
|
-
throw new Error(\`Unsupported crop size: \${t}. Only nano (96) and tiny (128) are supported.\`);
|
|
217
|
-
}, Q = (t, e, n) => {
|
|
218
|
-
if (e <= 1) return { nextIndex: 0, nextDirection: 1 };
|
|
219
|
-
let a = t + n, s = n;
|
|
220
|
-
return a >= e ? (a = e - 2, s = -1) : a < 0 && (a = 1, s = 1), { nextIndex: a, nextDirection: s };
|
|
221
|
-
};
|
|
222
|
-
k.wasm.numThreads = 1;
|
|
223
|
-
const Z = () => typeof navigator < "u" && !!navigator.gpu, F = 32;
|
|
224
|
-
let _ = null, R = null, x = null, L = null;
|
|
225
|
-
const w = {
|
|
226
|
-
tensors: /* @__PURE__ */ new Map(),
|
|
227
|
-
faceImages: /* @__PURE__ */ new Map(),
|
|
228
|
-
fullImages: /* @__PURE__ */ new Map()
|
|
229
|
-
};
|
|
230
|
-
let S = 3, b = 96, O = 0, z = 0, C = null, A = null, y = null, E = null, g = null, I = null, D = null, M = null, P = !1, v = "", p = null;
|
|
231
|
-
const T = (t) => {
|
|
232
|
-
self.postMessage({ type: "error", payload: t });
|
|
233
|
-
}, K = (t) => {
|
|
234
|
-
const e = w.tensors.get(t);
|
|
235
|
-
if (!e) throw new Error(\`Cache miss: \${t}\`);
|
|
236
|
-
const n = b - 2 * S;
|
|
237
|
-
return new W("float32", e, [1, 6, n, n]);
|
|
238
|
-
}, X = (t) => {
|
|
239
|
-
const e = w.faceImages.get(t);
|
|
240
|
-
if (!e) throw new Error(\`Cache miss: \${t}\`);
|
|
241
|
-
return e;
|
|
242
|
-
}, Y = async (t) => {
|
|
243
|
-
const e = w.fullImages.get(t);
|
|
244
|
-
if (!e) throw new Error(\`Cache miss: \${t}\`);
|
|
245
|
-
return await createImageBitmap(e);
|
|
246
|
-
}, ee = (t) => {
|
|
247
|
-
const [e, n, a, s] = t.dims, u = t.data;
|
|
248
|
-
(!y || y.width !== s || y.height !== a) && (C.width = s, C.height = a, y = A.createImageData(s, a));
|
|
249
|
-
const d = a * s, l = new Uint32Array(y.data.buffer), o = d, i = d * 2, c = n === 3;
|
|
250
|
-
for (let r = 0; r < d; r++) {
|
|
251
|
-
let m = (c ? u[r + i] : u[r]) * 255, f = (c ? u[r + o] : u[r]) * 255, h = u[r] * 255;
|
|
252
|
-
m = m < 0 ? 0 : m > 255 ? 255 : m, f = f < 0 ? 0 : f > 255 ? 255 : f, h = h < 0 ? 0 : h > 255 ? 255 : h, l[r] = 255 << 24 | (h & 255) << 16 | (f & 255) << 8 | m & 255;
|
|
253
|
-
}
|
|
254
|
-
A.putImageData(y, 0, 0);
|
|
255
|
-
}, te = async (t, e, n) => {
|
|
256
|
-
ee(t);
|
|
257
|
-
const a = X(e.face_image);
|
|
258
|
-
g.globalCompositeOperation = "source-over", g.drawImage(a, 0, 0), g.drawImage(C, S, S), g.globalCompositeOperation = "destination-in", g.drawImage(L, 0, 0, b, b), g.globalCompositeOperation = "destination-over", g.drawImage(a, 0, 0), g.globalCompositeOperation = "source-over", D.drawImage(n, 0, 0);
|
|
259
|
-
const s = e.crop_info.ymax - e.crop_info.ymin;
|
|
260
|
-
return D.drawImage(
|
|
261
|
-
E,
|
|
262
|
-
e.crop_info.xmin,
|
|
263
|
-
e.crop_info.ymin,
|
|
264
|
-
e.crop_info.width,
|
|
265
|
-
s
|
|
266
|
-
), await createImageBitmap(I);
|
|
267
|
-
}, ae = (t, e, n) => {
|
|
268
|
-
const [a, s, u] = e, d = s * u, l = F * d;
|
|
269
|
-
(!M || M.length !== l) && (M = new Float32Array(l));
|
|
270
|
-
for (let o = 0; o < F; o++) {
|
|
271
|
-
const c = Math.min(
|
|
272
|
-
Math.max(n - F / 2 + o, 0),
|
|
273
|
-
a - 1
|
|
274
|
-
) * d, r = o * d;
|
|
275
|
-
M.set(
|
|
276
|
-
t.subarray(c, c + d),
|
|
277
|
-
r
|
|
278
|
-
);
|
|
279
|
-
}
|
|
280
|
-
return M;
|
|
281
|
-
}, ne = (t, e, n) => {
|
|
282
|
-
C || (C = new OffscreenCanvas(1, 1), A = C.getContext("2d", {
|
|
283
|
-
willReadFrequently: !0
|
|
284
|
-
})), (!E || E.width !== n) && (E = new OffscreenCanvas(n, n), g = E.getContext("2d", {
|
|
285
|
-
willReadFrequently: !0
|
|
286
|
-
})), (!I || I.width !== t || I.height !== e) && (I = new OffscreenCanvas(t, e), D = I.getContext("2d", {
|
|
287
|
-
willReadFrequently: !0
|
|
288
|
-
}));
|
|
289
|
-
}, se = async (t) => {
|
|
290
|
-
try {
|
|
291
|
-
if (t.wasmPaths && (k.wasm.wasmPaths = t.wasmPaths), !t.dataset) throw new Error("Missing dataset");
|
|
292
|
-
const e = t.dataset;
|
|
293
|
-
R = e, b = e.dataset_info.config.crop_size, S = J(b), O = e.dataset_info.source_image_dimensions?.width || 0, z = e.dataset_info.source_image_dimensions?.height || 0, ne(O, z, b), x = await q.loadAsync(t.zipBuffer), t.zipBuffer = null, L = t.blendingMask;
|
|
294
|
-
const n = e.images.map(async (l) => {
|
|
295
|
-
if (!w.tensors.has(l.tensor_file)) {
|
|
296
|
-
const o = x.file(l.tensor_file);
|
|
297
|
-
if (o) {
|
|
298
|
-
const i = await o.async("arraybuffer");
|
|
299
|
-
w.tensors.set(l.tensor_file, new Float32Array(i));
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
if (!w.faceImages.has(l.face_image)) {
|
|
303
|
-
const o = x.file(l.face_image);
|
|
304
|
-
if (o) {
|
|
305
|
-
const i = await o.async("blob"), c = await createImageBitmap(i);
|
|
306
|
-
w.faceImages.set(l.face_image, c);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
if (!w.fullImages.has(l.full_image)) {
|
|
310
|
-
const o = x.file(l.full_image);
|
|
311
|
-
if (o) {
|
|
312
|
-
const i = await o.async("blob");
|
|
313
|
-
w.fullImages.set(l.full_image, i);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
await Promise.all(n), x = null;
|
|
318
|
-
const a = [];
|
|
319
|
-
Z() && a.push("webgpu"), a.push("wasm");
|
|
320
|
-
const s = new URL(t.modelPath, self.location.href).toString(), u = new URL(s).pathname.split("/").pop() || "model.onnx", d = new URL(\`\${u}.data\`, s).toString();
|
|
321
|
-
_ = await $.create(s, {
|
|
322
|
-
executionProviders: a,
|
|
323
|
-
graphOptimizationLevel: "all",
|
|
324
|
-
enableMemPattern: !0,
|
|
325
|
-
enableCpuMemArena: !0,
|
|
326
|
-
externalData: [{ data: d, path: \`\${u}.data\` }]
|
|
327
|
-
}), self.postMessage({ type: "ready" });
|
|
328
|
-
} catch (e) {
|
|
329
|
-
T(e?.message || "Init Error");
|
|
330
|
-
}
|
|
331
|
-
}, oe = async (t) => {
|
|
332
|
-
if (!_ || !R) {
|
|
333
|
-
T("Not initialized");
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
P = !1;
|
|
337
|
-
const e = t.audioDimensions[0];
|
|
338
|
-
self.postMessage({
|
|
339
|
-
type: "start",
|
|
340
|
-
payload: { totalFrames: e, frameInterval: j }
|
|
341
|
-
});
|
|
342
|
-
let n = 0, a = 1;
|
|
343
|
-
const s = {}, u = _.inputNames[0], d = _.inputNames[1], l = _.outputNames[0];
|
|
344
|
-
v = "", p && (p.close(), p = null);
|
|
345
|
-
for (let o = 0; o < e && !P; o++) {
|
|
346
|
-
let i = null, c = null, r = null, m = null;
|
|
347
|
-
try {
|
|
348
|
-
const f = R.images[n];
|
|
349
|
-
i = K(f.tensor_file), f.full_image !== v && (p && p.close(), p = await Y(f.full_image), v = f.full_image);
|
|
350
|
-
const h = ae(
|
|
351
|
-
t.audioFeatures,
|
|
352
|
-
t.audioDimensions,
|
|
353
|
-
o
|
|
354
|
-
);
|
|
355
|
-
c = new W("float32", h, [
|
|
356
|
-
1,
|
|
357
|
-
F,
|
|
358
|
-
G,
|
|
359
|
-
V
|
|
360
|
-
]), s[u] = i, s[d] = c, r = await _.run(s);
|
|
361
|
-
const N = r[l];
|
|
362
|
-
if (!N)
|
|
363
|
-
throw new Error("Missing inference output tensor");
|
|
364
|
-
m = N;
|
|
365
|
-
const B = await te(N, f, p);
|
|
366
|
-
self.postMessage(
|
|
367
|
-
{ type: "frame", payload: { frame: B, index: o } },
|
|
368
|
-
[B]
|
|
369
|
-
);
|
|
370
|
-
const U = Q(
|
|
371
|
-
n,
|
|
372
|
-
R.images.length,
|
|
373
|
-
a
|
|
374
|
-
);
|
|
375
|
-
n = U.nextIndex, a = U.nextDirection;
|
|
376
|
-
} catch (f) {
|
|
377
|
-
T(f?.message || "Run Error");
|
|
378
|
-
break;
|
|
379
|
-
} finally {
|
|
380
|
-
i && i.dispose(), c && c.dispose(), m && m.dispose(), i = null, c = null, m = null, r = null;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
p && (p.close(), p = null), v = "", self.postMessage({ type: "done" });
|
|
384
|
-
};
|
|
385
|
-
self.onmessage = (t) => {
|
|
386
|
-
const e = t.data;
|
|
387
|
-
e.type === "init" ? se(e) : e.type === "run" ? oe(e) : e.type === "stop" && (P = !0);
|
|
388
|
-
};
|
|
389
|
-
`, m = typeof self < "u" && self.Blob && new Blob(["URL.revokeObjectURL(import.meta.url);", d], { type: "text/javascript;charset=utf-8" });
|
|
390
|
-
function w(a) {
|
|
391
|
-
let n;
|
|
392
|
-
try {
|
|
393
|
-
if (n = m && (self.URL || self.webkitURL).createObjectURL(m), !n) throw "";
|
|
394
|
-
const e = new Worker(n, {
|
|
395
|
-
type: "module",
|
|
396
|
-
name: a?.name
|
|
397
|
-
});
|
|
398
|
-
return e.addEventListener("error", () => {
|
|
399
|
-
(self.URL || self.webkitURL).revokeObjectURL(n);
|
|
400
|
-
}), e;
|
|
401
|
-
} catch {
|
|
402
|
-
return new Worker(
|
|
403
|
-
"data:text/javascript;charset=utf-8," + encodeURIComponent(d),
|
|
404
|
-
{
|
|
405
|
-
type: "module",
|
|
406
|
-
name: a?.name
|
|
407
|
-
}
|
|
408
|
-
);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
48
|
class O {
|
|
412
49
|
worker;
|
|
413
50
|
handlers = {};
|
|
414
|
-
constructor(
|
|
415
|
-
this.handlers =
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
51
|
+
constructor(e = {}) {
|
|
52
|
+
this.handlers = e, this.worker = new Worker(
|
|
53
|
+
new URL("./workers/inference.worker.js", import.meta.url),
|
|
54
|
+
{ type: "module" }
|
|
55
|
+
), this.worker.onmessage = this.handleMessage.bind(this);
|
|
56
|
+
}
|
|
57
|
+
handleMessage(e) {
|
|
58
|
+
const { type: t, payload: r } = e.data;
|
|
59
|
+
switch (t) {
|
|
420
60
|
case "ready":
|
|
421
61
|
this.handlers.onReady?.();
|
|
422
62
|
break;
|
|
423
63
|
case "start":
|
|
424
|
-
this.handlers.onStart?.(
|
|
64
|
+
this.handlers.onStart?.(r.totalFrames);
|
|
425
65
|
break;
|
|
426
66
|
case "frame":
|
|
427
|
-
this.handlers.onFrame?.(
|
|
67
|
+
this.handlers.onFrame?.(r.frame, r.index);
|
|
428
68
|
break;
|
|
429
69
|
case "done":
|
|
430
70
|
this.handlers.onDone?.();
|
|
431
71
|
break;
|
|
432
72
|
case "error":
|
|
433
|
-
this.handlers.onError?.(
|
|
73
|
+
this.handlers.onError?.(r);
|
|
434
74
|
break;
|
|
435
75
|
}
|
|
436
76
|
}
|
|
437
|
-
init(
|
|
77
|
+
init(e) {
|
|
438
78
|
this.worker.postMessage(
|
|
439
79
|
{
|
|
440
80
|
type: "init",
|
|
441
|
-
modelPath:
|
|
442
|
-
dataset:
|
|
443
|
-
zipBuffer:
|
|
444
|
-
blendingMask:
|
|
445
|
-
wasmPaths:
|
|
81
|
+
modelPath: e.modelPath,
|
|
82
|
+
dataset: e.dataset,
|
|
83
|
+
zipBuffer: e.zipBuffer,
|
|
84
|
+
blendingMask: e.blendingMask,
|
|
85
|
+
wasmPaths: e.wasmPaths || l.wasm.wasmPaths
|
|
446
86
|
},
|
|
447
|
-
[
|
|
87
|
+
[e.zipBuffer, e.blendingMask]
|
|
448
88
|
);
|
|
449
89
|
}
|
|
450
|
-
run(
|
|
90
|
+
run(e) {
|
|
451
91
|
this.worker.postMessage(
|
|
452
92
|
{
|
|
453
93
|
type: "run",
|
|
454
|
-
audioFeatures:
|
|
455
|
-
audioDimensions:
|
|
94
|
+
audioFeatures: e.audioFeatures,
|
|
95
|
+
audioDimensions: e.audioDimensions
|
|
456
96
|
},
|
|
457
|
-
[
|
|
97
|
+
[e.audioFeatures.buffer]
|
|
458
98
|
);
|
|
459
99
|
}
|
|
460
100
|
stop() {
|
|
@@ -465,21 +105,21 @@ class O {
|
|
|
465
105
|
}
|
|
466
106
|
}
|
|
467
107
|
export {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
108
|
+
M as DEFAULT_LOW_FREQ,
|
|
109
|
+
_ as ENERGY_FLOOR,
|
|
110
|
+
L as FRAME_INTERVAL_MS,
|
|
111
|
+
k as FRAME_LENGTH_MS,
|
|
112
|
+
u as FRAME_SHIFT_MS,
|
|
113
|
+
v as FeatureExtractor,
|
|
474
114
|
O as InferenceEngine,
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
115
|
+
f as NUM_MEL_BINS,
|
|
116
|
+
p as NUM_SEQUENCE_FRAMES,
|
|
117
|
+
g as PREEMPH_COEFF,
|
|
118
|
+
R as REMOVE_DC_OFFSET,
|
|
119
|
+
F as ROUND_TO_POWER_OF_TWO,
|
|
120
|
+
m as SAMPLE_RATE,
|
|
121
|
+
w as TARGET_FPS,
|
|
122
|
+
P as calculatePingPongState,
|
|
123
|
+
y as getBorder,
|
|
124
|
+
b as loadDataset
|
|
485
125
|
};
|