@streamoji/aitwin 0.1.1 → 0.1.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/dist/aitwin.js CHANGED
@@ -1,48 +1,79 @@
1
- import { jsxs as ft, jsx as We } from "react/jsx-runtime";
2
- import { forwardRef as dt, useRef as M, useState as _e, useCallback as ee, useEffect as le, useMemo as ht, useImperativeHandle as mt } from "react";
3
- const $e = "https://ai.streamoji.com", tt = "https://pub-607ad1fc22e2400eb57d17240aab857c.r2.dev/custom-faces", _t = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin";
4
- class Pe extends Error {
5
- constructor(e = "AI twin not found") {
6
- super(e), this.name = "AiTwinNotFoundError";
1
+ import { jsxs as yt, jsx as qe } from "react/jsx-runtime";
2
+ import { forwardRef as Et, useRef as L, useState as _e, useCallback as ee, useEffect as ie, useMemo as kt, useImperativeHandle as St } from "react";
3
+ const Tt = "https://pub-607ad1fc22e2400eb57d17240aab857c.r2.dev", ot = {
4
+ publicBase: Tt
5
+ }, It = "0.1.4", At = {
6
+ version: It
7
+ }, ve = "https://ai.streamoji.com", vt = `${ot.publicBase}/custom-faces`, Rt = `${ot.publicBase}/aitwin-workers/v${At.version}/visemeDiffPreview.worker.js`, bt = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin", oe = "eng_a7f2b9c1", be = "eng_d4e8f3a2", De = "eng_c9b1e6d4", at = be, Dt = /* @__PURE__ */ new Set([
8
+ be,
9
+ De
10
+ ]), ct = {
11
+ [oe]: "oculus_direct",
12
+ [De]: "oculus_direct",
13
+ [be]: "inworld_word"
14
+ }, Ge = {
15
+ google: oe,
16
+ inworld: be,
17
+ cartesia: De
18
+ };
19
+ function xt(e) {
20
+ return ct[e];
21
+ }
22
+ function Ct(e) {
23
+ return e === De;
24
+ }
25
+ function Mt(e) {
26
+ if (e.ttsEngineId && e.ttsEngineId in ct)
27
+ return e.ttsEngineId;
28
+ const t = e.tts?.toLowerCase();
29
+ return t && t in Ge ? Ge[t] : at;
30
+ }
31
+ class Oe extends Error {
32
+ constructor(t = "AI twin not found") {
33
+ super(t), this.name = "AiTwinNotFoundError";
7
34
  }
8
35
  }
9
- async function wt(t, e = _t) {
10
- const n = new URL(e);
11
- n.searchParams.set("id", t);
36
+ async function Ft(e, t = bt) {
37
+ const n = new URL(t);
38
+ n.searchParams.set("id", e);
12
39
  const r = await fetch(n.toString());
13
40
  if (r.status === 404)
14
- throw new Pe();
41
+ throw new Oe();
15
42
  if (!r.ok)
16
43
  throw new Error(`getAiTwin failed (${r.status})`);
17
44
  const i = await r.json();
18
45
  if (!i.success)
19
- throw new Pe(i.error ?? "AI twin not found");
20
- return i.data;
46
+ throw new Oe(i.error ?? "AI twin not found");
47
+ const { tts: o, ...a } = i.data;
48
+ return {
49
+ ...a,
50
+ ttsEngineId: Mt(i.data)
51
+ };
21
52
  }
22
- const pt = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken", gt = "client_6TNvp3SCs4Og0a1ijm9TommXLql1", yt = "kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1", kt = "6TNvp3SCs4Og0a1ijm9TommXLql1", Et = "Swaraj Mali";
23
- async function Ke() {
24
- const t = await fetch(pt, {
53
+ const Pt = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken", Ot = "client_6TNvp3SCs4Og0a1ijm9TommXLql1", Lt = "kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1", Ut = "6TNvp3SCs4Og0a1ijm9TommXLql1", Bt = "Swaraj Mali";
54
+ async function je() {
55
+ const e = await fetch(Pt, {
25
56
  method: "POST",
26
57
  headers: {
27
58
  "Content-Type": "application/json",
28
- "Client-Id": gt,
29
- "Client-Secret": yt
59
+ "Client-Id": Ot,
60
+ "Client-Secret": Lt
30
61
  },
31
62
  body: JSON.stringify({
32
- userId: kt,
33
- userName: Et
63
+ userId: Ut,
64
+ userName: Bt
34
65
  })
35
66
  });
36
- if (!t.ok)
37
- throw new Error(`getAuthToken failed (${t.status})`);
38
- const e = await t.json(), n = e.authToken ?? e.token ?? "";
67
+ if (!e.ok)
68
+ throw new Error(`getAuthToken failed (${e.status})`);
69
+ const t = await e.json(), n = t.authToken ?? t.token ?? "";
39
70
  if (!n.trim())
40
71
  throw new Error("getAuthToken returned no token");
41
72
  return n;
42
73
  }
43
- async function St(t, e = $e) {
44
- const n = await fetch(`${e.replace(/\/$/, "")}/api/session-value`, {
45
- headers: { Authorization: `Bearer ${t}` }
74
+ async function Nt(e, t = ve) {
75
+ const n = await fetch(`${t.replace(/\/$/, "")}/api/session-value`, {
76
+ headers: { Authorization: `Bearer ${e}` }
46
77
  });
47
78
  if (!n.ok)
48
79
  throw new Error(`/api/session-value failed (${n.status})`);
@@ -50,28 +81,28 @@ async function St(t, e = $e) {
50
81
  if (!r.value) throw new Error("Session value response missing value");
51
82
  return r;
52
83
  }
53
- function vt(t) {
54
- const e = t.trim();
55
- if (e.length % 2 !== 0) throw new Error("Invalid hex key");
56
- const n = new Uint8Array(e.length / 2);
57
- for (let r = 0; r < e.length; r += 2)
58
- n[r / 2] = Number.parseInt(e.slice(r, r + 2), 16);
84
+ function $t(e) {
85
+ const t = e.trim();
86
+ if (t.length % 2 !== 0) throw new Error("Invalid hex key");
87
+ const n = new Uint8Array(t.length / 2);
88
+ for (let r = 0; r < t.length; r += 2)
89
+ n[r / 2] = Number.parseInt(t.slice(r, r + 2), 16);
59
90
  return n;
60
91
  }
61
- function ve(t) {
62
- return Uint8Array.from(t);
92
+ function Se(e) {
93
+ return Uint8Array.from(e);
63
94
  }
64
- function At(t, e) {
65
- const n = new Uint8Array(t.length + e.length);
66
- return n.set(t, 0), n.set(e, t.length), n;
95
+ function Vt(e, t) {
96
+ const n = new Uint8Array(e.length + t.length);
97
+ return n.set(e, 0), n.set(t, e.length), n;
67
98
  }
68
- async function Tt(t, e) {
69
- if (!e) throw new Error("Asset key is required for encrypted assets");
70
- if (t.byteLength < 28)
99
+ async function Ht(e, t) {
100
+ if (!t) throw new Error("Asset key is required for encrypted assets");
101
+ if (e.byteLength < 28)
71
102
  throw new Error("Encrypted payload too small");
72
- const n = new Uint8Array(t), r = ve(n.subarray(0, 12)), i = ve(n.subarray(12, 28)), a = ve(n.subarray(28)), s = ve(At(a, i)), c = await crypto.subtle.importKey(
103
+ const n = new Uint8Array(e), r = Se(n.subarray(0, 12)), i = Se(n.subarray(12, 28)), o = Se(n.subarray(28)), a = Se(Vt(o, i)), c = await crypto.subtle.importKey(
73
104
  "raw",
74
- vt(e),
105
+ $t(t),
75
106
  { name: "AES-GCM" },
76
107
  !1,
77
108
  ["decrypt"]
@@ -79,53 +110,53 @@ async function Tt(t, e) {
79
110
  return crypto.subtle.decrypt(
80
111
  { name: "AES-GCM", iv: r, tagLength: 128 },
81
112
  c,
82
- s
113
+ a
83
114
  );
84
115
  }
85
- let Ce = null;
86
- const Fe = /* @__PURE__ */ new Set();
87
- function It(t, e = tt) {
88
- const n = `${e.replace(/\/$/, "")}/${t}`;
116
+ let Le = null;
117
+ const Ue = /* @__PURE__ */ new Set();
118
+ function Wt(e, t = vt) {
119
+ const n = `${t.replace(/\/$/, "")}/${e}`;
89
120
  return { twinBase: n, binBase: n, encrypted: !0 };
90
121
  }
91
- function Rt(t) {
92
- Ce = t;
93
- for (const e of Fe) e();
122
+ function Kt(e) {
123
+ Le = e;
124
+ for (const t of Ue) t();
94
125
  }
95
- function re() {
96
- if (!Ce)
126
+ function ne() {
127
+ if (!Le)
97
128
  throw new Error("Twin assets not configured; call setActiveTwinAssets first");
98
- return Ce;
129
+ return Le;
99
130
  }
100
- function bt(t) {
101
- return Fe.add(t), () => Fe.delete(t);
131
+ function qt(e) {
132
+ return Ue.add(e), () => Ue.delete(e);
102
133
  }
103
- function xt() {
104
- return `${re().twinBase}/sil.png`;
134
+ function Gt() {
135
+ return `${ne().twinBase}/sil.png`;
105
136
  }
106
- function Dt() {
107
- return `${re().twinBase}/idle.mp4`;
137
+ function jt() {
138
+ return `${ne().twinBase}/idle.mp4`;
108
139
  }
109
- function Pt() {
110
- const { encrypted: t, binBase: e, twinBase: n } = re();
111
- return t ? e : n;
140
+ function zt() {
141
+ const { encrypted: e, binBase: t, twinBase: n } = ne();
142
+ return e ? t : n;
112
143
  }
113
- function ne() {
114
- return re().encrypted;
144
+ function ut() {
145
+ return ne().encrypted;
115
146
  }
116
- function nt() {
117
- const { binBase: t, twinBase: e, encrypted: n } = re();
118
- return n ? `${t}/atlas.bin` : `${e}/atlas.json`;
147
+ function Qt() {
148
+ const { binBase: e, twinBase: t, encrypted: n } = ne();
149
+ return n ? `${e}/atlas.bin` : `${t}/atlas.json`;
119
150
  }
120
- function Ct() {
121
- const { binBase: t, twinBase: e, encrypted: n } = re();
122
- return n ? `${t}/expression_atlas.bin` : `${e}/expression_atlas.json`;
151
+ function Jt() {
152
+ const { binBase: e, twinBase: t, encrypted: n } = ne();
153
+ return n ? `${e}/expression_atlas.bin` : `${t}/expression_atlas.json`;
123
154
  }
124
- function Ft(t) {
125
- const e = t.split(/\r?\n/);
155
+ function Yt(e) {
156
+ const t = e.split(/\r?\n/);
126
157
  let n = "", r = "";
127
- for (const a of e)
128
- a.startsWith("event:") ? n = a.slice(6).trim() : a.startsWith("data:") && (r = a.slice(5).trim());
158
+ for (const o of t)
159
+ o.startsWith("event:") ? n = o.slice(6).trim() : o.startsWith("data:") && (r = o.slice(5).trim());
129
160
  if (!n) return null;
130
161
  let i = {};
131
162
  if (r)
@@ -136,7 +167,7 @@ function Ft(t) {
136
167
  }
137
168
  return { event: n, data: i };
138
169
  }
139
- const Mt = [
170
+ const Xt = [
140
171
  "aa",
141
172
  "CH",
142
173
  "DD",
@@ -152,14 +183,14 @@ const Mt = [
152
183
  "kk",
153
184
  "nn",
154
185
  "sil"
155
- ], Ot = new Map(
156
- Mt.map((t) => [t.toLowerCase(), t])
186
+ ], Zt = new Map(
187
+ Xt.map((e) => [e.toLowerCase(), e])
157
188
  );
158
- function Ut(t) {
159
- const e = (t ?? "").trim();
160
- return e ? Ot.get(e.toLowerCase()) ?? "sil" : "sil";
189
+ function en(e) {
190
+ const t = (e ?? "").trim();
191
+ return t ? Zt.get(t.toLowerCase()) ?? "sil" : "sil";
161
192
  }
162
- const Lt = {
193
+ const tn = {
163
194
  aei: ["aa", "E", "I"],
164
195
  o: ["O", "U"],
165
196
  ee: ["I"],
@@ -171,46 +202,46 @@ const Lt = {
171
202
  qw: ["U", "O"],
172
203
  cdgknstxyz: ["DD", "SS", "kk", "CH"]
173
204
  };
174
- function $t(t) {
175
- if (!t) return ["sil"];
176
- const e = t.toLowerCase();
177
- return Lt[e] ?? ["sil"];
205
+ function nn(e) {
206
+ if (!e) return ["sil"];
207
+ const t = e.toLowerCase();
208
+ return tn[t] ?? ["sil"];
178
209
  }
179
- function Bt(t, e) {
180
- const n = t.word.length;
210
+ function rn(e, t) {
211
+ const n = e.word.length;
181
212
  if (n <= 0) return 0;
182
- const r = t.wduration;
213
+ const r = e.wduration;
183
214
  if (r <= 0) return 0;
184
- const i = Math.max(0, Math.min(1, (e - t.wtime) / r));
215
+ const i = Math.max(0, Math.min(1, (t - e.wtime) / r));
185
216
  return Math.min(n - 1, Math.floor(i * n));
186
217
  }
187
- function xe(t, e) {
188
- const n = Math.max(0, Math.min(t.length - 1, e));
189
- return t[n] ?? "";
218
+ function xe(e, t) {
219
+ const n = Math.max(0, Math.min(e.length - 1, t));
220
+ return e[n] ?? "";
190
221
  }
191
- function Vt(t, e) {
192
- const n = t.toLowerCase(), r = xe(n, e), i = n.slice(Math.max(0, e - 1), e + 2);
222
+ function sn(e, t) {
223
+ const n = e.toLowerCase(), r = xe(n, t), i = n.slice(Math.max(0, t - 1), t + 2);
193
224
  return r === "i" || r === "y" ? "I" : i.includes("ee") || r === "e" || n.includes("ea") ? "E" : "aa";
194
225
  }
195
- function Ht(t, e) {
196
- const n = t.toLowerCase(), r = n.slice(
197
- Math.max(0, e - 1),
198
- Math.min(n.length, e + 3)
226
+ function on(e, t) {
227
+ const n = e.toLowerCase(), r = n.slice(
228
+ Math.max(0, t - 1),
229
+ Math.min(n.length, t + 3)
199
230
  );
200
231
  if (/oo|ou|uw/.test(r)) return "U";
201
- const i = xe(n, e);
232
+ const i = xe(n, t);
202
233
  return i === "u" || i === "w" ? "U" : "O";
203
234
  }
204
- function Nt(t, e) {
205
- const n = t.toLowerCase(), r = n.slice(Math.max(0, e - 2), e + 1);
235
+ function an(e, t) {
236
+ const n = e.toLowerCase(), r = n.slice(Math.max(0, t - 2), t + 1);
206
237
  if (/qu/.test(r)) return "U";
207
238
  const i = n.slice(
208
- Math.max(0, e - 1),
209
- Math.min(n.length, e + 3)
239
+ Math.max(0, t - 1),
240
+ Math.min(n.length, t + 3)
210
241
  );
211
- return /wo|wh/.test(i) || xe(n, e) === "o" ? "O" : "U";
242
+ return /wo|wh/.test(i) || xe(n, t) === "o" ? "O" : "U";
212
243
  }
213
- const Ae = {
244
+ const Te = {
214
245
  d: "DD",
215
246
  t: "DD",
216
247
  n: "DD",
@@ -222,399 +253,399 @@ const Ae = {
222
253
  x: "CH",
223
254
  j: "CH"
224
255
  };
225
- function Wt(t, e) {
226
- const n = t.toLowerCase(), r = xe(n, e);
227
- if (Ae[r]) return Ae[r];
256
+ function cn(e, t) {
257
+ const n = e.toLowerCase(), r = xe(n, t);
258
+ if (Te[r]) return Te[r];
228
259
  for (let i = 0; i < n.length; i++) {
229
- const a = n[i];
230
- if (Ae[a]) return Ae[a];
260
+ const o = n[i];
261
+ if (Te[o]) return Te[o];
231
262
  }
232
263
  return "DD";
233
264
  }
234
- function Kt(t, e, n) {
235
- const r = $t(t);
265
+ function un(e, t, n) {
266
+ const r = nn(e);
236
267
  if (r.length === 1) return r[0];
237
- const i = e.trim();
268
+ const i = t.trim();
238
269
  if (!i) return r[0];
239
- const a = (t ?? "").toLowerCase(), s = Math.max(0, n);
240
- switch (a) {
270
+ const o = (e ?? "").toLowerCase(), a = Math.max(0, n);
271
+ switch (o) {
241
272
  case "aei":
242
- return Vt(i, s);
273
+ return sn(i, a);
243
274
  case "o":
244
- return Ht(i, s);
275
+ return on(i, a);
245
276
  case "qw":
246
- return Nt(i, s);
277
+ return an(i, a);
247
278
  case "cdgknstxyz":
248
- return Wt(i, s);
279
+ return cn(i, a);
249
280
  default:
250
281
  return r[0];
251
282
  }
252
283
  }
253
- function qt(t) {
254
- let e = t.trim();
255
- const n = e.indexOf(",");
256
- e.startsWith("data:") && n >= 0 && (e = e.slice(n + 1)), e = e.replace(/\s+/g, "").replace(/-/g, "+").replace(/_/g, "/");
257
- const r = e.length % 4;
258
- return r !== 0 && (e += "=".repeat(4 - r)), e;
284
+ function ln(e) {
285
+ let t = e.trim();
286
+ const n = t.indexOf(",");
287
+ t.startsWith("data:") && n >= 0 && (t = t.slice(n + 1)), t = t.replace(/\s+/g, "").replace(/-/g, "+").replace(/_/g, "/");
288
+ const r = t.length % 4;
289
+ return r !== 0 && (t += "=".repeat(4 - r)), t;
259
290
  }
260
- const jt = 24e3;
261
- function zt(t) {
262
- return t.length >= 4 && t[0] === 82 && t[1] === 73 && t[2] === 70 && t[3] === 70;
291
+ const fn = 24e3;
292
+ function dn(e) {
293
+ return e.length >= 4 && e[0] === 82 && e[1] === 73 && e[2] === 70 && e[3] === 70;
263
294
  }
264
- function qe(t, e, n = jt) {
265
- const r = e.byteOffset % 2 === 0 ? e : e.slice(), i = new Int16Array(
295
+ function ze(e, t, n = fn) {
296
+ const r = t.byteOffset % 2 === 0 ? t : t.slice(), i = new Int16Array(
266
297
  r.buffer,
267
298
  r.byteOffset,
268
299
  r.byteLength / 2
269
- ), a = new Float32Array(i.length);
300
+ ), o = new Float32Array(i.length);
270
301
  for (let c = 0; c < i.length; c++)
271
- a[c] = i[c] >= 32768 ? -(65536 - i[c]) / 32768 : i[c] / 32767;
272
- const s = t.createBuffer(1, a.length, n);
273
- return s.copyToChannel(a, 0), s;
302
+ o[c] = i[c] >= 32768 ? -(65536 - i[c]) / 32768 : i[c] / 32767;
303
+ const a = e.createBuffer(1, o.length, n);
304
+ return a.copyToChannel(o, 0), a;
274
305
  }
275
- function Gt(t) {
276
- const e = qt(t), n = window.atob(e), r = new Uint8Array(n.length);
306
+ function hn(e) {
307
+ const t = ln(e), n = window.atob(t), r = new Uint8Array(n.length);
277
308
  for (let i = 0; i < n.length; i++)
278
309
  r[i] = n.charCodeAt(i);
279
310
  return r;
280
311
  }
281
- async function Qt(t, e) {
282
- const n = Gt(e);
283
- if (zt(n)) {
312
+ async function _n(e, t) {
313
+ const n = hn(t);
314
+ if (dn(n)) {
284
315
  const r = n.buffer.slice(
285
316
  n.byteOffset,
286
317
  n.byteOffset + n.byteLength
287
318
  );
288
- return t.decodeAudioData(r);
319
+ return e.decodeAudioData(r);
289
320
  }
290
321
  if (n.length >= 2 && n.length % 2 === 0)
291
- return qe(t, n);
322
+ return ze(e, n);
292
323
  try {
293
324
  const r = n.buffer.slice(
294
325
  n.byteOffset,
295
326
  n.byteOffset + n.byteLength
296
327
  );
297
- return await t.decodeAudioData(r);
328
+ return await e.decodeAudioData(r);
298
329
  } catch {
299
- return qe(t, n);
330
+ return ze(e, n);
300
331
  }
301
332
  }
302
- function Jt(t, e) {
333
+ function mn(e, t) {
303
334
  let n = null;
304
- for (const r of t)
305
- e < r.vtime || e >= r.vtime + r.vduration || (!n || r.vtime >= n.vtime) && (n = r);
335
+ for (const r of e)
336
+ t < r.vtime || t >= r.vtime + r.vduration || (!n || r.vtime >= n.vtime) && (n = r);
306
337
  return n ? n.viseme : "sil";
307
338
  }
308
- function Xt(t, e) {
309
- for (const n of t)
310
- if (e >= n.wtime && e < n.wtime + n.wduration)
339
+ function wn(e, t) {
340
+ for (const n of e)
341
+ if (t >= n.wtime && t < n.wtime + n.wduration)
311
342
  return n;
312
343
  return null;
313
344
  }
314
- function je(t) {
315
- let e = 0;
316
- for (const n of t)
317
- e = Math.max(e, n.vtime + n.vduration);
318
- return e;
345
+ function Qe(e) {
346
+ let t = 0;
347
+ for (const n of e)
348
+ t = Math.max(t, n.vtime + n.vduration);
349
+ return t;
319
350
  }
320
- const Yt = 0.5, Zt = 1.5, rt = "Olivia", en = "f786b574-daa5-4673-aa0c-cbe3e8534c02", it = 0.85;
321
- function tn(t, e = $e) {
322
- const n = `${e.replace(/\/$/, "")}/avatar_ttsWithPoses`, r = [], i = [], a = [];
323
- let s = "neutral";
351
+ const gn = 0.5, pn = 1.5, lt = "Olivia", yn = "f786b574-daa5-4673-aa0c-cbe3e8534c02", ft = 0.85;
352
+ function En(e, t = ve) {
353
+ const n = `${t.replace(/\/$/, "")}/avatar_ttsWithPoses`, r = [], i = [], o = [];
354
+ let a = "neutral";
324
355
  const c = [], u = [];
325
- let p = null, _ = !1, o = !1, f = !1, d = !1, k = 0, x = 0, F = 0, Z = 0, H = null, h = 1, g = "", P = "google", O = "google", C = !1;
326
- const K = /* @__PURE__ */ new Set(), ie = () => {
327
- for (const m of u)
328
- m.playbackRate.value = h;
329
- }, D = () => K.forEach((m) => m()), N = () => {
330
- H != null && (clearTimeout(H), H = null);
331
- }, se = () => {
332
- for (const m of u)
356
+ let _ = null, d = !1, k = !1, f = !1, s = !1, w = 0, A = 0, g = 0, V = 0, F = null, N = 1, Q = "", m = oe, p = oe, R = !1;
357
+ const v = /* @__PURE__ */ new Set(), H = () => {
358
+ for (const h of u)
359
+ h.playbackRate.value = N;
360
+ }, W = () => v.forEach((h) => h()), ae = () => {
361
+ F != null && (clearTimeout(F), F = null);
362
+ }, ye = () => {
363
+ for (const h of u)
333
364
  try {
334
- m.stop();
365
+ h.stop();
335
366
  } catch {
336
367
  }
337
368
  u.length = 0;
338
- }, fe = () => {
339
- C = !0, N(), se(), c.length = 0, _ = !1, o = !1, f = !1, d = !1, r.length = 0, i.length = 0, a.length = 0, s = "neutral", F = 0, Z = 0, x = 0, t("idle"), D();
340
- }, oe = () => {
341
- !d || _ || u.length > 0 || c.length > 0 || (o = !1, N(), t("done"), D());
342
- }, de = () => {
343
- const m = je(r);
344
- if (m <= 0) {
345
- t("done"), D();
369
+ }, X = () => {
370
+ R = !0, ae(), ye(), c.length = 0, d = !1, k = !1, f = !1, s = !1, r.length = 0, i.length = 0, o.length = 0, a = "neutral", g = 0, V = 0, A = 0, e("idle"), W();
371
+ }, K = () => {
372
+ !s || d || u.length > 0 || c.length > 0 || (k = !1, ae(), e("done"), W());
373
+ }, Ee = () => {
374
+ const h = Qe(r);
375
+ if (h <= 0) {
376
+ e("done"), W();
346
377
  return;
347
378
  }
348
- o = !0, k = performance.now(), t("speaking"), D(), N();
349
- const R = h > 0 ? m / h : m;
350
- H = setTimeout(() => {
351
- C || (o = !1, H = null, t("done"), D());
352
- }, R);
353
- }, z = (m, R, A) => {
354
- m.length !== 0 && (A && (F = R), m.forEach((T) => {
355
- const I = String(T.word ?? "").trim();
356
- if (!I) return;
357
- const q = Math.round((T.start ?? 0) * 1e3), B = Math.round((T.duration ?? 0) * 1e3);
379
+ k = !0, w = performance.now(), e("speaking"), W(), ae();
380
+ const b = N > 0 ? h / N : h;
381
+ F = setTimeout(() => {
382
+ R || (k = !1, F = null, e("done"), W());
383
+ }, b);
384
+ }, Me = (h, b, U) => {
385
+ h.length !== 0 && (U && (g = b), h.forEach((C) => {
386
+ const P = String(C.word ?? "").trim();
387
+ if (!P) return;
388
+ const z = Math.round((C.start ?? 0) * 1e3), B = Math.round((C.duration ?? 0) * 1e3);
358
389
  i.push({
359
- word: I,
360
- wtime: F + q,
390
+ word: P,
391
+ wtime: g + z,
361
392
  wduration: B,
362
393
  queueIndex: i.length
363
394
  });
364
395
  }));
365
- }, ke = (m, R, A, T) => {
366
- m.length !== 0 && (A && (F = R), m.forEach((b) => {
367
- const I = String(b.symbol ?? "").trim();
368
- if (!I) return;
369
- const q = Math.round((b.start ?? 0) * 1e3), B = Math.max(
396
+ }, ce = (h, b, U, C) => {
397
+ h.length !== 0 && (U && (g = b), h.forEach((E) => {
398
+ const P = String(E.symbol ?? "").trim();
399
+ if (!P) return;
400
+ const z = Math.round((E.start ?? 0) * 1e3), B = Math.max(
370
401
  1,
371
- Math.round((b.duration ?? 0) * 1e3)
372
- ), v = F + q;
373
- let G;
374
- if (T === "google" || T === "cartesia")
375
- G = Ut(I);
402
+ Math.round((E.duration ?? 0) * 1e3)
403
+ ), M = g + z;
404
+ let D;
405
+ if (xt(C) === "oculus_direct")
406
+ D = en(P);
376
407
  else {
377
- const S = Xt(i, v), V = S ? Bt(S, v) : 0;
378
- G = Kt(
379
- I,
380
- S?.word ?? "",
381
- V
408
+ const x = wn(i, M), J = x ? rn(x, M) : 0;
409
+ D = un(
410
+ P,
411
+ x?.word ?? "",
412
+ J
382
413
  );
383
414
  }
384
415
  r.push({
385
- viseme: G,
416
+ viseme: D,
386
417
  weight: 1,
387
- vtime: v,
418
+ vtime: M,
388
419
  vduration: B
389
420
  });
390
- }), D());
391
- }, he = (m, R, A, T, b) => {
392
- z(R, A, T), ke(m, A, T, b);
393
- }, ae = async (m, R, A, T) => {
394
- if (C || !m?.trim()) {
395
- (R.length > 0 || A.length > 0) && !f && he(
396
- R,
397
- A,
398
- F,
399
- T,
400
- O
421
+ }), W());
422
+ }, re = (h, b, U, C, E) => {
423
+ Me(b, U, C), ce(h, U, C, E);
424
+ }, ke = async (h, b, U, C) => {
425
+ if (R || !h?.trim()) {
426
+ (b.length > 0 || U.length > 0) && !f && re(
427
+ b,
428
+ U,
429
+ g,
430
+ C,
431
+ p
401
432
  );
402
433
  return;
403
434
  }
404
- if (_) {
435
+ if (d) {
405
436
  c.push({
406
- audio: m,
407
- visemes: R,
408
- words: A,
409
- isNewSegment: T
437
+ audio: h,
438
+ visemes: b,
439
+ words: U,
440
+ isNewSegment: C
410
441
  });
411
442
  return;
412
443
  }
413
- _ = !0;
444
+ d = !0;
414
445
  try {
415
- const b = window.AudioContext || window.webkitAudioContext, I = p ?? new b();
416
- I.state === "suspended" && await I.resume(), p = I;
417
- const q = await Qt(I, m);
446
+ const E = window.AudioContext || window.webkitAudioContext, P = _ ?? new E();
447
+ P.state === "suspended" && await P.resume(), _ = P;
448
+ const z = await _n(P, h);
418
449
  f = !0;
419
- const B = I.currentTime;
420
- let v = Z;
421
- const G = !o;
422
- v < B && (v = B + 0.1), Z = v + q.duration;
423
- const S = I.createBufferSource();
424
- if (S.buffer = q, S.playbackRate.value = h, S.connect(I.destination), u.push(S), C) {
450
+ const B = P.currentTime;
451
+ let M = V;
452
+ const D = !k;
453
+ M < B && (M = B + 0.1), V = M + z.duration;
454
+ const x = P.createBufferSource();
455
+ if (x.buffer = z, x.playbackRate.value = N, x.connect(P.destination), u.push(x), R) {
425
456
  u.pop();
426
457
  return;
427
458
  }
428
- if (G) {
429
- o = !0, t("speaking"), x = v, F = 0, r.length = 0, i.length = 0;
430
- const $ = (v - B) * 1e3;
431
- k = performance.now() + $;
459
+ if (D) {
460
+ k = !0, e("speaking"), A = M, g = 0, r.length = 0, i.length = 0;
461
+ const $ = (M - B) * 1e3;
462
+ w = performance.now() + $;
432
463
  }
433
- S.onended = () => {
434
- const $ = u.indexOf(S);
435
- $ >= 0 && u.splice($, 1), oe(), D();
436
- }, S.start(v);
437
- const V = (v - x) * 1e3;
438
- he(
439
- R,
440
- A,
441
- V,
442
- T,
443
- O
464
+ x.onended = () => {
465
+ const $ = u.indexOf(x);
466
+ $ >= 0 && u.splice($, 1), K(), W();
467
+ }, x.start(M);
468
+ const J = (M - A) * 1e3;
469
+ re(
470
+ b,
471
+ U,
472
+ J,
473
+ C,
474
+ p
444
475
  );
445
- } catch (b) {
446
- console.error("[avatarTtsLipsync] audio chunk failed:", b);
476
+ } catch (E) {
477
+ console.error("[avatarTtsLipsync] audio chunk failed:", E);
447
478
  } finally {
448
- _ = !1;
449
- const b = c.shift();
450
- b ? await ae(
451
- b.audio,
452
- b.visemes,
453
- b.words,
454
- b.isNewSegment
455
- ) : oe();
479
+ d = !1;
480
+ const E = c.shift();
481
+ E ? await ke(
482
+ E.audio,
483
+ E.visemes,
484
+ E.words,
485
+ E.isNewSegment
486
+ ) : K();
456
487
  }
457
- }, Ee = () => {
458
- if (d = !0, !f && r.length > 0) {
459
- de();
488
+ }, Z = () => {
489
+ if (s = !0, !f && r.length > 0) {
490
+ Ee();
460
491
  return;
461
492
  }
462
- oe();
493
+ K();
463
494
  };
464
495
  return {
465
- setDeveloperToken: (m) => {
466
- g = m;
496
+ setDeveloperToken: (h) => {
497
+ Q = h;
467
498
  },
468
- setTtsProvider: (m) => {
469
- P = m;
499
+ setTtsEngineId: (h) => {
500
+ m = h;
470
501
  },
471
- getTtsProvider: () => P,
472
- speak: async (m, R) => {
473
- fe(), C = !1, t("loading");
474
- const A = g.trim();
475
- if (!A)
502
+ getTtsEngineId: () => m,
503
+ speak: async (h, b) => {
504
+ X(), R = !1, e("loading");
505
+ const U = Q.trim();
506
+ if (!U)
476
507
  throw new Error("Developer token required for avatar_ttsWithPoses");
477
- const T = R?.tts ?? P;
478
- O = T;
479
- const b = R?.voiceId ?? rt, I = R?.speakingRate ?? it, q = Math.max(
480
- Yt,
481
- Math.min(Zt, I)
508
+ const C = b?.ttsEngineId ?? m ?? at;
509
+ p = C;
510
+ const E = b?.voiceId ?? lt, P = b?.speakingRate ?? ft, z = Math.max(
511
+ gn,
512
+ Math.min(pn, P)
482
513
  ), B = {
483
- user_query: m,
484
- tts: T,
485
- speakingRate: q
514
+ user_query: h,
515
+ ttsEngineId: C,
516
+ speakingRate: z
486
517
  };
487
- (T === "inworld" || T === "cartesia") && (B.voice_id = b);
518
+ Dt.has(C) && (B.voice_id = E);
488
519
  try {
489
- const v = await fetch(n, {
520
+ const M = await fetch(n, {
490
521
  method: "POST",
491
522
  headers: {
492
523
  "Content-Type": "application/json",
493
- Authorization: `Bearer ${A}`
524
+ Authorization: `Bearer ${U}`
494
525
  },
495
526
  body: JSON.stringify(B)
496
527
  });
497
- if (!v.ok)
498
- throw new Error(`avatar_ttsWithPoses failed (${v.status})`);
499
- const G = v.body;
500
- if (!G) throw new Error("No response body");
501
- const S = G.getReader(), V = new TextDecoder();
528
+ if (!M.ok)
529
+ throw new Error(`avatar_ttsWithPoses failed (${M.status})`);
530
+ const D = M.body;
531
+ if (!D) throw new Error("No response body");
532
+ const x = D.getReader(), J = new TextDecoder();
502
533
  let $ = "";
503
- const J = async (ce) => {
504
- const U = Ft(ce);
505
- if (U) {
506
- if (U.event === "audio") {
507
- const l = U.data.chunk, y = U.data.visemes ?? [], E = U.data.words ?? [], w = U.data.is_new_segment ?? !1;
508
- if (l)
509
- await ae(l, y, E, w);
510
- else if (y.length > 0 || E.length > 0) {
511
- const L = w && r.length > 0 ? je(r) : F;
512
- he(y, E, L, w, T);
534
+ const le = async (fe) => {
535
+ const l = Yt(fe);
536
+ if (l) {
537
+ if (l.event === "audio") {
538
+ const y = l.data.chunk, S = l.data.visemes ?? [], T = l.data.words ?? [], I = l.data.is_new_segment ?? !1;
539
+ if (y)
540
+ await ke(y, S, T, I);
541
+ else if (S.length > 0 || T.length > 0) {
542
+ const q = I && r.length > 0 ? Qe(r) : g;
543
+ re(S, T, q, I, C);
513
544
  }
514
- } else if (U.event === "metadata") {
515
- const l = U.data.mood;
516
- typeof l == "string" && l.trim() && (s = l.trim());
517
- const y = U.data.sentence_emotions;
518
- if (Array.isArray(y)) {
519
- a.length = 0;
520
- for (const E of y) {
521
- if (!E || typeof E != "object") continue;
522
- const w = E;
523
- a.push({
524
- sentence_index: Number(w.sentence_index ?? 0),
525
- text: String(w.text ?? ""),
526
- sentiment: typeof w.sentiment == "string" ? w.sentiment : void 0,
527
- emotion: String(w.emotion ?? s),
528
- start_word: Number(w.start_word ?? 0),
529
- end_word: Number(w.end_word ?? 0)
545
+ } else if (l.event === "metadata") {
546
+ const y = l.data.mood;
547
+ typeof y == "string" && y.trim() && (a = y.trim());
548
+ const S = l.data.sentence_emotions;
549
+ if (Array.isArray(S)) {
550
+ o.length = 0;
551
+ for (const T of S) {
552
+ if (!T || typeof T != "object") continue;
553
+ const I = T;
554
+ o.push({
555
+ sentence_index: Number(I.sentence_index ?? 0),
556
+ text: String(I.text ?? ""),
557
+ sentiment: typeof I.sentiment == "string" ? I.sentiment : void 0,
558
+ emotion: String(I.emotion ?? a),
559
+ start_word: Number(I.start_word ?? 0),
560
+ end_word: Number(I.end_word ?? 0)
530
561
  });
531
562
  }
532
563
  }
533
- D();
534
- } else if (U.event === "error")
564
+ W();
565
+ } else if (l.event === "error")
535
566
  throw new Error(
536
- String(U.data.message ?? "avatar_ttsWithPoses stream error")
567
+ String(l.data.message ?? "avatar_ttsWithPoses stream error")
537
568
  );
538
569
  }
539
570
  };
540
571
  for (; ; ) {
541
- const { done: ce, value: U } = await S.read();
542
- U && ($ += V.decode(U, { stream: !0 }));
543
- const l = $.split(`
572
+ const { done: fe, value: l } = await x.read();
573
+ l && ($ += J.decode(l, { stream: !0 }));
574
+ const y = $.split(`
544
575
 
545
576
  `);
546
- $ = l.pop() ?? "";
547
- for (const y of l) await J(y);
548
- if (ce) {
549
- $.trim() && await J($.trim()), C || Ee();
577
+ $ = y.pop() ?? "";
578
+ for (const S of y) await le(S);
579
+ if (fe) {
580
+ $.trim() && await le($.trim()), R || Z();
550
581
  break;
551
582
  }
552
583
  }
553
- } catch (v) {
554
- throw console.error("[avatarTtsLipsync]", v), fe(), t("error"), v;
584
+ } catch (M) {
585
+ throw console.error("[avatarTtsLipsync]", M), X(), e("error"), M;
555
586
  }
556
587
  },
557
- stop: fe,
588
+ stop: X,
558
589
  getVisemeQueue: () => r,
559
590
  getWordQueue: () => i,
560
- getSentenceEmotions: () => a,
561
- getStreamMood: () => s,
562
- getPlaybackElapsedMs: () => o ? f && p && x > 0 ? Math.max(
591
+ getSentenceEmotions: () => o,
592
+ getStreamMood: () => a,
593
+ getPlaybackElapsedMs: () => k ? f && _ && A > 0 ? Math.max(
563
594
  0,
564
- (p.currentTime - x) * 1e3
565
- ) : Math.max(0, (performance.now() - k) * h) : 0,
566
- isSpeaking: () => o,
567
- setPlaybackSpeed: (m) => {
568
- h = Math.max(0.1, Math.min(1, m)), ie();
595
+ (_.currentTime - A) * 1e3
596
+ ) : Math.max(0, (performance.now() - w) * N) : 0,
597
+ isSpeaking: () => k,
598
+ setPlaybackSpeed: (h) => {
599
+ N = Math.max(0.1, Math.min(1, h)), H();
569
600
  },
570
- getPlaybackSpeed: () => h,
571
- subscribe: (m) => (K.add(m), () => K.delete(m))
601
+ getPlaybackSpeed: () => N,
602
+ subscribe: (h) => (v.add(h), () => v.delete(h))
572
603
  };
573
604
  }
574
- function nn(t, e, n) {
605
+ function kn(e, t, n) {
575
606
  return {
576
- width: t.crop?.source_width ?? e,
577
- height: t.crop?.source_height ?? n
607
+ width: e.crop?.source_width ?? t,
608
+ height: e.crop?.source_height ?? n
578
609
  };
579
610
  }
580
- function rn(t, e) {
581
- const n = t.crop;
611
+ function Sn(e, t) {
612
+ const n = e.crop;
582
613
  return {
583
- x: e?.source_x ?? n?.x ?? 0,
584
- y: e?.source_y ?? n?.y ?? 0,
585
- width: e?.source_w ?? n?.width ?? t.frame_width ?? 0,
586
- height: e?.source_h ?? n?.height ?? t.frame_height ?? 0
614
+ x: t?.source_x ?? n?.x ?? 0,
615
+ y: t?.source_y ?? n?.y ?? 0,
616
+ width: t?.source_w ?? n?.width ?? e.frame_width ?? 0,
617
+ height: t?.source_h ?? n?.height ?? e.frame_height ?? 0
587
618
  };
588
619
  }
589
- function sn(t) {
590
- return t ? (t.w ?? 0) > 0 && (t.h ?? 0) > 0 : !1;
620
+ function Tn(e) {
621
+ return e ? (e.w ?? 0) > 0 && (e.h ?? 0) > 0 : !1;
591
622
  }
592
- async function st(t, e) {
593
- const n = await fetch(t);
594
- if (!n.ok) throw new Error(`Failed to load encrypted asset: ${t}`);
623
+ async function dt(e, t) {
624
+ const n = await fetch(e);
625
+ if (!n.ok) throw new Error(`Failed to load encrypted asset: ${e}`);
595
626
  const r = await n.arrayBuffer();
596
- return Tt(r, e);
627
+ return Ht(r, t);
597
628
  }
598
- async function ot(t, e) {
599
- if (!e) throw new Error("Missing asset key for encrypted JSON");
600
- const n = await st(t, e), r = new TextDecoder().decode(n);
629
+ async function In(e, t) {
630
+ if (!t) throw new Error("Missing asset key for encrypted JSON");
631
+ const n = await dt(e, t), r = new TextDecoder().decode(n);
601
632
  return JSON.parse(r);
602
633
  }
603
- async function on(t) {
604
- const e = new Blob([t], { type: "image/webp" }), n = URL.createObjectURL(e);
634
+ async function An(e) {
635
+ const t = new Blob([e], { type: "image/webp" }), n = URL.createObjectURL(t);
605
636
  return new Promise((r, i) => {
606
- const a = new Image();
607
- a.decoding = "async", a.onload = () => {
608
- URL.revokeObjectURL(n), r(a);
609
- }, a.onerror = () => {
637
+ const o = new Image();
638
+ o.decoding = "async", o.onload = () => {
639
+ URL.revokeObjectURL(n), r(o);
640
+ }, o.onerror = () => {
610
641
  URL.revokeObjectURL(n), i(new Error("Failed to decode decrypted image"));
611
- }, a.src = n;
642
+ }, o.src = n;
612
643
  });
613
644
  }
614
- function at(t, e) {
615
- return `${t}/${e}_minus_sil.png`;
645
+ function ht(e, t) {
646
+ return `${e}/${t}_minus_sil.png`;
616
647
  }
617
- const Be = 15, er = [
648
+ const We = 15, Er = [
618
649
  "aa",
619
650
  "E",
620
651
  "I",
@@ -629,13 +660,13 @@ const Be = 15, er = [
629
660
  "RR",
630
661
  "kk",
631
662
  "nn"
632
- ], Me = [
663
+ ], Be = [
633
664
  "t01_0.17",
634
665
  "t02_0.33",
635
666
  "t03_0.50",
636
667
  "t04_0.67",
637
668
  "t05_0.83"
638
- ], an = Me.length, ze = /* @__PURE__ */ new Set([
669
+ ], vn = Be.length, Je = /* @__PURE__ */ new Set([
639
670
  "aa__kk",
640
671
  "aa__nn",
641
672
  "aa__sil",
@@ -742,69 +773,59 @@ const Be = 15, er = [
742
773
  "U__nn",
743
774
  "U__sil"
744
775
  ]);
745
- function Ge(t, e) {
746
- return `${t}__${e}`;
776
+ function Ye(e, t) {
777
+ return `${e}__${t}`;
747
778
  }
748
- function Qe(t, e) {
749
- return `pairs/${t}/${e}_minus_sil.png`;
779
+ function Xe(e, t) {
780
+ return `pairs/${e}/${t}_minus_sil.png`;
750
781
  }
751
- function Oe(t) {
752
- return `${t}_minus_sil.png`;
782
+ function Ne(e) {
783
+ return `${e}_minus_sil.png`;
753
784
  }
754
- function cn(t, e) {
755
- if (t === e) return [];
756
- const n = [], r = Ge(t, e);
757
- if (ze.has(r)) {
758
- for (const a of Me)
759
- n.push(Qe(r, a));
785
+ function Rn(e, t) {
786
+ if (e === t) return [];
787
+ const n = [], r = Ye(e, t);
788
+ if (Je.has(r)) {
789
+ for (const o of Be)
790
+ n.push(Xe(r, o));
760
791
  return n;
761
792
  }
762
- const i = Ge(e, t);
763
- if (ze.has(i)) {
764
- for (let a = an - 1; a >= 0; a--)
765
- n.push(Qe(i, Me[a]));
793
+ const i = Ye(t, e);
794
+ if (Je.has(i)) {
795
+ for (let o = vn - 1; o >= 0; o--)
796
+ n.push(Xe(i, Be[o]));
766
797
  return n;
767
798
  }
768
799
  return [];
769
800
  }
770
- function un(t) {
771
- return t < 16 ? [] : t < 35 ? [2] : t < 51 ? [0, 4] : t < 67 ? [0, 2, 4] : t < 83 ? [0, 1, 2, 3] : [0, 1, 2, 3, 4];
801
+ function bn(e) {
802
+ return e < 16 ? [] : e < 35 ? [2] : e < 51 ? [0, 4] : e < 67 ? [0, 2, 4] : e < 83 ? [0, 1, 2, 3] : [0, 1, 2, 3, 4];
772
803
  }
773
- function ln(t, e) {
774
- return un(e).map((r) => t[r]).filter((r) => r != null);
804
+ function Dn(e, t) {
805
+ return bn(t).map((r) => e[r]).filter((r) => r != null);
775
806
  }
776
- function fn(t, e, n, r) {
807
+ function xn(e, t, n, r) {
777
808
  if (r <= 0) return 0;
778
- const a = Math.max(1, n - e) / r, s = t - e, c = Math.floor(s / a);
809
+ const o = Math.max(1, n - t) / r, a = e - t, c = Math.floor(a / o);
779
810
  return Math.min(r - 1, Math.max(0, c));
780
811
  }
781
- function dn(t, e) {
782
- if (!ne()) return `${t}/${e}`;
783
- const n = e.split("/").pop() ?? e, r = n.includes(".") ? n.slice(0, n.lastIndexOf(".")) : n;
784
- return `${t}/${r}.bin`;
812
+ function Cn(e, t, n) {
813
+ if (!n) return `${e}/${t}`;
814
+ const r = t.split("/").pop() ?? t, i = r.includes(".") ? r.slice(0, r.lastIndexOf(".")) : r;
815
+ return `${e}/${i}.bin`;
785
816
  }
786
- async function hn(t) {
787
- const e = nt(), n = ne() ? await ot(e, t) : await fetch(e).then((r) => {
788
- if (!r.ok) throw new Error(`Failed to load ${e}`);
789
- return r.json();
790
- });
791
- return {
792
- width: n.crop?.source_width ?? n.frame_width ?? 842,
793
- height: n.crop?.source_height ?? n.frame_height ?? 1264
794
- };
795
- }
796
- const mn = [0.32, 0.24, 0.16, 0.08, 0], De = mn.map(
797
- (t) => `eyes_${t.toFixed(2)}`
798
- ), _n = [
817
+ const Mn = [0.32, 0.24, 0.16, 0.08, 0], Ce = Mn.map(
818
+ (e) => `eyes_${e.toFixed(2)}`
819
+ ), Fn = [
799
820
  "eyes_0.32",
800
821
  "eyes_0.16",
801
822
  "eyes_0.00"
802
- ], wn = [
823
+ ], Pn = [
803
824
  "eyes_0.32",
804
825
  "eyes_0.16",
805
826
  "eyes_0.08",
806
827
  "eyes_0.00"
807
- ], pn = {
828
+ ], On = {
808
829
  fear: "fear",
809
830
  afraid: "fear",
810
831
  anxiety: "fear",
@@ -827,512 +848,559 @@ const mn = [0.32, 0.24, 0.16, 0.08, 0], De = mn.map(
827
848
  disgust: "disgust",
828
849
  disgusted: "disgust"
829
850
  };
830
- function te(t, e = "neutral") {
831
- const n = String(t ?? "").trim().toLowerCase();
832
- return n ? pn[n] ?? e : e;
851
+ function se(e, t = "neutral") {
852
+ const n = String(e ?? "").trim().toLowerCase();
853
+ return n ? On[n] ?? t : t;
833
854
  }
834
- function W(t) {
835
- return t === "neutral" ? null : at(t, "eyes_standard");
855
+ function j(e) {
856
+ return e === "neutral" ? null : ht(e, "eyes_standard");
836
857
  }
837
- function Ue(t, e) {
838
- return !e || e === "eyes_standard" ? W(t) : t === "neutral" && e === "eyes_standard" ? null : at(t, e);
858
+ function $e(e, t) {
859
+ return !t || t === "eyes_standard" ? j(e) : e === "neutral" && t === "eyes_standard" ? null : ht(e, t);
839
860
  }
840
- function gn(t) {
841
- if (!t) return "open";
842
- const e = t.match(/\/(eyes_[^/]+)_minus_sil\.png$/);
843
- if (!e?.[1]) return "open";
844
- const n = e[1];
845
- return De.includes(n) ? n : "open";
861
+ function Ln(e) {
862
+ if (!e) return "open";
863
+ const t = e.match(/\/(eyes_[^/]+)_minus_sil\.png$/);
864
+ if (!t?.[1]) return "open";
865
+ const n = t[1];
866
+ return Ce.includes(n) ? n : "open";
846
867
  }
847
- function yn(t) {
848
- return t === "open" ? -1 : De.indexOf(t);
868
+ function Un(e) {
869
+ return e === "open" ? -1 : Ce.indexOf(e);
849
870
  }
850
- function kn(t, e) {
871
+ function Bn(e, t) {
851
872
  const n = [];
852
- for (const r of e)
853
- n.push(Ue(t, r));
854
- for (let r = e.length - 2; r >= 0; r--)
855
- n.push(Ue(t, e[r]));
856
- return n.push(W(t)), n;
857
- }
858
- function En(t, e) {
859
- if (e === "open") return [W(t)];
860
- const n = yn(e);
861
- if (n < 0) return [W(t)];
873
+ for (const r of t)
874
+ n.push($e(e, r));
875
+ for (let r = t.length - 2; r >= 0; r--)
876
+ n.push($e(e, t[r]));
877
+ return n.push(j(e)), n;
878
+ }
879
+ function Nn(e, t) {
880
+ if (t === "open") return [j(e)];
881
+ const n = Un(t);
882
+ if (n < 0) return [j(e)];
862
883
  const r = [];
863
884
  for (let i = n; i >= 0; i--)
864
- r.push(Ue(t, De[i]));
865
- return r.push(W(t)), r;
885
+ r.push($e(e, Ce[i]));
886
+ return r.push(j(e)), r;
866
887
  }
867
- const Sn = [
888
+ const $n = [
868
889
  "t01_0.20",
869
890
  "t02_0.40",
870
891
  "t03_0.60",
871
892
  "t04_0.80"
872
893
  ];
873
- function vn(t, e) {
874
- return `${t}__${e}`;
894
+ function Vn(e, t) {
895
+ return `${e}__${t}`;
875
896
  }
876
- function An(t, e, n) {
877
- return `pairs/${vn(t, e)}/${n}_minus_sil.png`;
897
+ function Hn(e, t, n) {
898
+ return `pairs/${Vn(e, t)}/${n}_minus_sil.png`;
878
899
  }
879
- function Tn(t, e) {
880
- return Sn.map(
881
- (n) => An(t, e, n)
900
+ function Wn(e, t) {
901
+ return $n.map(
902
+ (n) => Hn(e, t, n)
882
903
  );
883
904
  }
884
- function pe(t, e) {
885
- return t + Math.random() * (e - t);
905
+ function we(e, t) {
906
+ return e + Math.random() * (t - e);
886
907
  }
887
- function Te() {
888
- return pe(14, 32);
908
+ function Ie() {
909
+ return we(14, 32);
889
910
  }
890
- function In(t = {}) {
911
+ function Kn(e = {}) {
891
912
  const {
892
- initialDelayMs: e = 800 + Math.random() * 1200,
913
+ initialDelayMs: t = 800 + Math.random() * 1200,
893
914
  intervalMinMs: n = 2200,
894
915
  intervalMaxMs: r = 5800,
895
916
  doubleBlinkChance: i = 0.18,
896
- doubleBlinkGapMinMs: a = 120,
897
- doubleBlinkGapMaxMs: s = 220,
917
+ doubleBlinkGapMinMs: o = 120,
918
+ doubleBlinkGapMaxMs: a = 220,
898
919
  fullBlinkChance: c = 0.75,
899
- postEmotionChangeDelayMs: u = 380
900
- } = t;
901
- let p = "neutral", _ = "neutral", o = {
920
+ postEmotionChangeDelayMs: u = 380,
921
+ minDwellMs: _ = 550
922
+ } = e;
923
+ let d = "neutral", k = "neutral", f = 0, s = {
902
924
  kind: "idle",
903
- nextBlinkAt: performance.now() + e
904
- }, f = null, d = "__eyes_open__|neutral";
905
- function k(h, g) {
906
- f = h, d = `${h ?? "__eyes_open__"}|${g}`;
925
+ nextBlinkAt: performance.now() + t
926
+ }, w = null, A = "__eyes_open__|neutral";
927
+ function g(m, p) {
928
+ w = m, A = `${m ?? "__eyes_open__"}|${p}`;
907
929
  }
908
- function x(h, g) {
909
- o = {
930
+ function V(m, p) {
931
+ s = {
910
932
  kind: "idle",
911
- nextBlinkAt: h + pe(n, r)
912
- }, _ = g, k(W(g), g);
933
+ nextBlinkAt: m + we(n, r)
934
+ }, k = p, g(j(p), p);
913
935
  }
914
- function F(h, g, P) {
915
- const O = P ? _n : Math.random() < c ? De : wn;
916
- o = {
936
+ function F(m, p, R) {
937
+ const v = R ? Fn : Math.random() < c ? Ce : Pn;
938
+ s = {
917
939
  kind: "playing",
918
- paths: kn(g, O),
940
+ paths: Bn(p, v),
919
941
  index: 0,
920
- holdUntil: h + Te(),
942
+ holdUntil: m + Ie(),
921
943
  after: "idle",
922
- blinkEmotion: g
923
- }, _ = g, k(o.paths[0] ?? null, g);
944
+ blinkEmotion: p
945
+ }, k = p, g(s.paths[0] ?? null, p);
924
946
  }
925
- function Z(h, g, P) {
926
- const O = gn(f), C = [];
927
- if (O !== "open") {
928
- const K = En(g, O);
929
- C.push(...K.slice(0, -1));
947
+ function N(m, p, R) {
948
+ const v = Ln(w), H = [];
949
+ if (v !== "open") {
950
+ const W = Nn(p, v);
951
+ H.push(...W.slice(0, -1));
930
952
  }
931
- C.push(...Tn(g, P)), C.push(W(P)), o = {
953
+ H.push(...Wn(p, R)), H.push(j(R)), s = {
932
954
  kind: "emotionBlend",
933
- paths: C,
955
+ paths: H,
934
956
  index: 0,
935
- holdUntil: h + Te(),
936
- thenEmotion: P
937
- }, k(C[0] ?? null, g);
957
+ holdUntil: m + Ie(),
958
+ thenEmotion: R
959
+ }, g(H[0] ?? null, p);
938
960
  }
939
- function H(h) {
940
- if (o.kind === "emotionBlend") {
941
- for (; h >= o.holdUntil && o.index < o.paths.length - 1; )
942
- o.index += 1, o.holdUntil = h + Te();
943
- const g = o.paths[o.index] ?? null;
944
- k(g, o.thenEmotion), h >= o.holdUntil && o.index >= o.paths.length - 1 && (p = o.thenEmotion, _ = o.thenEmotion, k(W(o.thenEmotion), o.thenEmotion), o = {
961
+ function Q(m) {
962
+ if (s.kind === "emotionBlend") {
963
+ for (; m >= s.holdUntil && s.index < s.paths.length - 1; )
964
+ s.index += 1, s.holdUntil = m + Ie();
965
+ const p = s.paths[s.index] ?? null;
966
+ g(p, s.thenEmotion), m >= s.holdUntil && s.index >= s.paths.length - 1 && (d = s.thenEmotion, k = s.thenEmotion, g(j(s.thenEmotion), s.thenEmotion), s = {
945
967
  kind: "idle",
946
- nextBlinkAt: h + u
968
+ nextBlinkAt: m + u
947
969
  });
948
970
  return;
949
971
  }
950
- if (o.kind === "idle") {
951
- k(W(_), _), h >= o.nextBlinkAt && F(h, _, !1);
972
+ if (s.kind === "idle") {
973
+ g(j(k), k), m >= s.nextBlinkAt && F(m, k, !1);
952
974
  return;
953
975
  }
954
- if (o.kind === "doublePause") {
955
- k(W(o.blinkEmotion), o.blinkEmotion), h >= o.resumeAt && F(h, o.blinkEmotion, !0);
976
+ if (s.kind === "doublePause") {
977
+ g(j(s.blinkEmotion), s.blinkEmotion), m >= s.resumeAt && F(m, s.blinkEmotion, !0);
956
978
  return;
957
979
  }
958
- if (o.kind === "playing") {
959
- for (; h >= o.holdUntil && o.index < o.paths.length - 1; )
960
- o.index += 1, o.holdUntil = h + Te();
961
- if (k(o.paths[o.index] ?? null, o.blinkEmotion), h >= o.holdUntil && o.index >= o.paths.length - 1) {
962
- if (o.after === "resumeIdle") {
963
- x(h + u, p);
980
+ if (s.kind === "playing") {
981
+ for (; m >= s.holdUntil && s.index < s.paths.length - 1; )
982
+ s.index += 1, s.holdUntil = m + Ie();
983
+ if (g(s.paths[s.index] ?? null, s.blinkEmotion), m >= s.holdUntil && s.index >= s.paths.length - 1) {
984
+ if (s.after === "resumeIdle") {
985
+ V(m + u, d);
964
986
  return;
965
987
  }
966
- if (o.after === "doublePause") {
967
- o = {
988
+ if (s.after === "doublePause") {
989
+ s = {
968
990
  kind: "doublePause",
969
- resumeAt: h + pe(a, s),
970
- blinkEmotion: o.blinkEmotion
971
- }, k(W(o.blinkEmotion), o.blinkEmotion);
991
+ resumeAt: m + we(o, a),
992
+ blinkEmotion: s.blinkEmotion
993
+ }, g(j(s.blinkEmotion), s.blinkEmotion);
972
994
  return;
973
995
  }
974
- if (o.after === "idle" && Math.random() < i) {
975
- o = {
996
+ if (s.after === "idle" && Math.random() < i) {
997
+ s = {
976
998
  kind: "doublePause",
977
- resumeAt: h + pe(a, s),
978
- blinkEmotion: o.blinkEmotion
979
- }, k(W(o.blinkEmotion), o.blinkEmotion);
999
+ resumeAt: m + we(o, a),
1000
+ blinkEmotion: s.blinkEmotion
1001
+ }, g(j(s.blinkEmotion), s.blinkEmotion);
980
1002
  return;
981
1003
  }
982
- x(h, o.blinkEmotion);
1004
+ V(m, s.blinkEmotion);
983
1005
  }
984
1006
  }
985
1007
  }
986
1008
  return {
987
- advance: H,
988
- setTargetEmotion(h) {
989
- const g = te(h, p);
990
- if (g === p && o.kind !== "emotionBlend") return;
991
- const P = performance.now(), O = p;
992
- if (p = g, o.kind === "playing") {
993
- Z(P, _, g);
1009
+ advance: Q,
1010
+ setTargetEmotion(m) {
1011
+ const p = se(m, d);
1012
+ if (p === d && s.kind !== "emotionBlend") return;
1013
+ const R = performance.now();
1014
+ if (_ > 0 && p !== d && s.kind !== "emotionBlend" && !(d === "neutral" && p !== "neutral") && R - f < _)
1015
+ return;
1016
+ const v = d;
1017
+ if (d = p, f = R, s.kind === "playing") {
1018
+ N(R, k, p);
994
1019
  return;
995
1020
  }
996
- if (o.kind === "emotionBlend") {
997
- o = {
998
- ...o,
999
- thenEmotion: g
1021
+ if (s.kind === "emotionBlend") {
1022
+ s = {
1023
+ ...s,
1024
+ thenEmotion: p
1000
1025
  };
1001
1026
  return;
1002
1027
  }
1003
- Z(P, O, g);
1028
+ N(R, v, p);
1004
1029
  },
1005
- getTargetEmotion: () => p,
1006
- getExpressionPath: () => f,
1007
- getDrawKey: () => d,
1030
+ getTargetEmotion: () => d,
1031
+ getExpressionPath: () => w,
1032
+ getDrawKey: () => A,
1008
1033
  reset() {
1009
- p = "neutral", _ = "neutral", x(
1010
- performance.now() + pe(n, r),
1034
+ d = "neutral", k = "neutral", f = 0, V(
1035
+ performance.now() + we(n, r),
1011
1036
  "neutral"
1012
1037
  );
1013
1038
  }
1014
1039
  };
1015
1040
  }
1016
- function Rn(t) {
1017
- const e = document.createElement("video");
1018
- e.muted = !0, e.playsInline = !0, e.preload = "auto", e.setAttribute("playsinline", ""), e.loop = !0;
1041
+ function qn(e) {
1042
+ const t = document.createElement("video");
1043
+ t.muted = !0, t.playsInline = !0, t.preload = "auto", t.setAttribute("playsinline", ""), t.loop = !0;
1019
1044
  let n = !1;
1020
1045
  return {
1021
1046
  async load() {
1022
1047
  n || await new Promise((r, i) => {
1023
- const a = () => {
1024
- const s = e.duration;
1025
- if (!Number.isFinite(s) || s <= 0) {
1026
- i(new Error(`Idle video has invalid duration: ${t}`));
1048
+ const o = () => {
1049
+ const a = t.duration;
1050
+ if (!Number.isFinite(a) || a <= 0) {
1051
+ i(new Error(`Idle video has invalid duration: ${e}`));
1027
1052
  return;
1028
1053
  }
1029
- n = !0, e.currentTime = 0, r();
1054
+ n = !0, t.currentTime = 0, r();
1030
1055
  };
1031
- e.addEventListener("loadeddata", a, { once: !0 }), e.addEventListener(
1056
+ t.addEventListener("loadeddata", o, { once: !0 }), t.addEventListener(
1032
1057
  "error",
1033
- () => i(new Error(`Failed to load idle video: ${t}`)),
1058
+ () => i(new Error(`Failed to load idle video: ${e}`)),
1034
1059
  { once: !0 }
1035
- ), e.src = t, e.load();
1060
+ ), t.src = e, t.load();
1036
1061
  });
1037
1062
  },
1038
1063
  isReady() {
1039
1064
  return n;
1040
1065
  },
1041
1066
  getVideo() {
1042
- return e;
1067
+ return t;
1043
1068
  },
1044
1069
  setActive(r) {
1045
- n && (r ? e.paused && e.play().catch(() => {
1046
- }) : e.paused || e.pause());
1070
+ n && (r ? t.paused && t.play().catch(() => {
1071
+ }) : t.paused || t.pause());
1047
1072
  },
1048
1073
  restart() {
1049
- n && (e.currentTime = 0);
1074
+ n && (t.currentTime = 0);
1050
1075
  }
1051
1076
  };
1052
1077
  }
1053
- function bn(t, e) {
1054
- for (const n of t)
1055
- if (e >= n.wtime && e < n.wtime + n.wduration)
1056
- return n;
1057
- return null;
1078
+ const Gn = 50, _t = 400;
1079
+ function jn(e, t, n) {
1080
+ return e.filter((r) => {
1081
+ const i = r.queueIndex ?? -1;
1082
+ return i >= t && i <= n;
1083
+ });
1058
1084
  }
1059
- function xn(t, e, n, r = "neutral") {
1060
- const i = te(r);
1061
- if (e.length === 0) return i;
1062
- if (n.length === 0)
1063
- return te(e[0]?.emotion, i);
1064
- const a = bn(n, t);
1065
- let s;
1066
- if (a)
1067
- s = a.queueIndex ?? n.findIndex((c) => c === a), s < 0 && (s = 0);
1068
- else {
1069
- if (t < n[0].wtime)
1070
- return te(e[0]?.emotion, i);
1071
- s = n[n.length - 1].queueIndex ?? n.length - 1;
1085
+ function zn(e, t) {
1086
+ const n = [];
1087
+ for (const r of e) {
1088
+ const i = jn(
1089
+ t,
1090
+ r.start_word,
1091
+ r.end_word
1092
+ );
1093
+ if (i.length === 0) continue;
1094
+ const o = i[0], a = i[i.length - 1];
1095
+ n.push({
1096
+ sentence: r,
1097
+ startMs: Math.max(0, o.wtime - Gn),
1098
+ endMs: a.wtime + a.wduration + _t
1099
+ });
1072
1100
  }
1073
- for (const c of e)
1074
- if (s >= c.start_word && s <= c.end_word)
1075
- return te(c.emotion, i);
1076
- for (const c of e)
1077
- if (s < c.start_word)
1078
- return te(c.emotion, i);
1079
- return te(
1080
- e[e.length - 1]?.emotion,
1101
+ return n.sort((r, i) => r.startMs - i.startMs);
1102
+ }
1103
+ function Qn(e, t, n, r = "neutral") {
1104
+ const i = se(r);
1105
+ if (t.length === 0) return i;
1106
+ if (n.length === 0 || e < n[0].wtime)
1107
+ return se(t[0]?.emotion, i);
1108
+ const o = zn(t, n);
1109
+ let a = null;
1110
+ for (const _ of o)
1111
+ e >= _.startMs && e <= _.endMs && (a = _);
1112
+ if (a)
1113
+ return se(a.sentence.emotion, i);
1114
+ const c = n[n.length - 1], u = c.wtime + c.wduration;
1115
+ return e > u + _t ? se(
1116
+ t[t.length - 1]?.emotion,
1081
1117
  i
1082
- );
1118
+ ) : i;
1083
1119
  }
1084
- function ct(t, e, n, r, i = Be) {
1085
- const a = new OffscreenCanvas(n, r), s = a.getContext("2d", { willReadFrequently: !0 });
1086
- s.drawImage(t, 0, 0, n, r);
1087
- const c = s.getImageData(0, 0, n, r), u = s.createImageData(n, r);
1120
+ function mt(e, t, n, r, i = We) {
1121
+ const o = new OffscreenCanvas(n, r), a = o.getContext("2d", { willReadFrequently: !0 });
1122
+ a.drawImage(e, 0, 0, n, r);
1123
+ const c = a.getImageData(0, 0, n, r), u = a.createImageData(n, r);
1088
1124
  u.data.set(c.data);
1089
- const _ = new OffscreenCanvas(n, r).getContext("2d", { willReadFrequently: !0 });
1090
- for (const o of e) {
1091
- _.clearRect(0, 0, n, r), _.drawImage(o, 0, 0, n, r);
1092
- const f = _.getImageData(0, 0, n, r);
1093
- for (let d = 0; d < u.data.length; d += 4) {
1094
- const k = f.data[d], x = f.data[d + 1], F = f.data[d + 2];
1095
- Math.max(k, x, F) > i && (u.data[d] = k, u.data[d + 1] = x, u.data[d + 2] = F, u.data[d + 3] = 255);
1125
+ const d = new OffscreenCanvas(n, r).getContext("2d", { willReadFrequently: !0 });
1126
+ for (const k of t) {
1127
+ d.clearRect(0, 0, n, r), d.drawImage(k, 0, 0, n, r);
1128
+ const f = d.getImageData(0, 0, n, r);
1129
+ for (let s = 0; s < u.data.length; s += 4) {
1130
+ const w = f.data[s], A = f.data[s + 1], g = f.data[s + 2];
1131
+ Math.max(w, A, g) > i && (u.data[s] = w, u.data[s + 1] = A, u.data[s + 2] = g, u.data[s + 3] = 255);
1096
1132
  }
1097
1133
  }
1098
- return s.putImageData(u, 0, 0), a;
1134
+ return a.putImageData(u, 0, 0), o;
1099
1135
  }
1100
- let ge = null, ye = null;
1136
+ let te = null, ge = null;
1101
1137
  const Re = /* @__PURE__ */ new Map();
1102
- function Dn() {
1103
- ge = null, ye = null, Re.clear();
1138
+ function Jn() {
1139
+ te = null, ge = null, Re.clear();
1104
1140
  }
1105
- bt(Dn);
1106
- function Pn(t) {
1107
- return t ?? ye ?? void 0;
1141
+ qt(Jn);
1142
+ function Yn(e) {
1143
+ return e ?? ge ?? void 0;
1108
1144
  }
1109
- function Ve(t) {
1110
- return new Promise((e, n) => {
1145
+ function Ke(e) {
1146
+ return new Promise((t, n) => {
1111
1147
  const r = new Image();
1112
- r.decoding = "async", r.onload = () => e(r), r.onerror = () => n(new Error(`Failed to load image: ${t}`)), r.src = t;
1148
+ r.decoding = "async", r.onload = () => t(r), r.onerror = () => n(new Error(`Failed to load image: ${e}`)), r.src = e;
1113
1149
  });
1114
1150
  }
1115
- async function Je(t, e, n) {
1116
- const r = ne() ? await ot(t, n) : await fetch(t).then((u) => {
1117
- if (!u.ok) throw new Error(`Failed to load ${t}`);
1118
- return u.json();
1119
- }), i = r.sheets?.[0];
1120
- if (!i?.path) throw new Error(`${t} has no sheets[0].path`);
1121
- const a = ne() ? await on(
1122
- await st(dn(e, i.path), n ?? "")
1123
- ) : await Ve(`${e}/${i.path}`), s = /* @__PURE__ */ new Map();
1124
- for (const u of r.cells ?? [])
1125
- u.path && s.set(u.path, u);
1126
- const c = /* @__PURE__ */ new Map();
1127
- for (const u of r.sheets ?? [])
1128
- c.set(u.index, u);
1129
- return { atlas: a, atlasMeta: r, cellByPath: s, sheetByIndex: c, diffBase: e };
1130
- }
1131
- async function be(t) {
1132
- const e = Pn(t);
1133
- if (ne() && !e)
1151
+ async function Ze(e, t, n, r) {
1152
+ const i = n ? await In(e, r) : await fetch(e).then((_) => {
1153
+ if (!_.ok) throw new Error(`Failed to load ${e}`);
1154
+ return _.json();
1155
+ }), o = i.sheets?.[0];
1156
+ if (!o?.path) throw new Error(`${e} has no sheets[0].path`);
1157
+ const a = n ? await An(
1158
+ await dt(
1159
+ Cn(t, o.path, n),
1160
+ r ?? ""
1161
+ )
1162
+ ) : await Ke(`${t}/${o.path}`), c = /* @__PURE__ */ new Map();
1163
+ for (const _ of i.cells ?? [])
1164
+ _.path && c.set(_.path, _);
1165
+ const u = /* @__PURE__ */ new Map();
1166
+ for (const _ of i.sheets ?? [])
1167
+ u.set(_.index, _);
1168
+ return { atlas: a, atlasMeta: i, cellByPath: c, sheetByIndex: u, diffBase: t };
1169
+ }
1170
+ async function Ve(e) {
1171
+ const t = Yn(e), n = ut();
1172
+ if (n && !t)
1134
1173
  throw new Error("Encrypted assets enabled but no key provided");
1135
- if (ge && (!ne() || ye === (e ?? null)))
1136
- return ge;
1137
- ye = e ?? null, Re.clear();
1138
- const n = Pt();
1139
- return ge = (async () => {
1140
- const r = await Ve(xt()), i = await Je(
1141
- nt(),
1174
+ if (te && (!n || ge === (t ?? null))) {
1175
+ const o = await te;
1176
+ return pe(o);
1177
+ }
1178
+ ge = t ?? null, Re.clear();
1179
+ const r = zt();
1180
+ te = (async () => {
1181
+ const o = await Ke(Gt()), a = await Ze(
1182
+ Qt(),
1183
+ r,
1142
1184
  n,
1143
- e
1185
+ t
1144
1186
  );
1145
- let a = null;
1187
+ let c = null;
1146
1188
  try {
1147
- a = await Je(
1148
- Ct(),
1189
+ c = await Ze(
1190
+ Jt(),
1191
+ r,
1149
1192
  n,
1150
- e
1193
+ t
1151
1194
  );
1152
1195
  } catch {
1153
- a = null;
1196
+ c = null;
1154
1197
  }
1155
- return { sil: r, viseme: i, expression: a };
1156
- })(), ge;
1157
- }
1158
- function He(t) {
1159
- return nn(
1160
- t.viseme.atlasMeta,
1161
- t.sil.naturalWidth,
1162
- t.sil.naturalHeight
1198
+ return { sil: o, viseme: a, expression: c };
1199
+ })();
1200
+ const i = await te;
1201
+ return pe(i);
1202
+ }
1203
+ async function wt(e) {
1204
+ if (await Ve(e), !te) throw new Error("Assets not loaded");
1205
+ return te;
1206
+ }
1207
+ function pe(e) {
1208
+ return kn(
1209
+ e.viseme.atlasMeta,
1210
+ e.sil.naturalWidth,
1211
+ e.sil.naturalHeight
1163
1212
  );
1164
1213
  }
1165
- function Cn(t, e) {
1214
+ function Xn(e, t) {
1166
1215
  const n = document.createElement("canvas");
1167
- return n.width = e.w, n.height = e.h, n.getContext("2d", { willReadFrequently: !0 }).drawImage(t.atlas, e.x, e.y, e.w, e.h, 0, 0, e.w, e.h), n;
1216
+ return n.width = t.w, n.height = t.h, n.getContext("2d", { willReadFrequently: !0 }).drawImage(e.atlas, t.x, t.y, t.w, t.h, 0, 0, t.w, t.h), n;
1168
1217
  }
1169
- async function Xe(t, e, n) {
1170
- const r = `${e.diffBase}::${n}`, i = Re.get(r);
1218
+ async function et(e, t, n) {
1219
+ const r = `${t.diffBase}::${n}`, i = Re.get(r);
1171
1220
  if (i) return i;
1172
- const a = (async () => {
1173
- const s = e.cellByPath.get(n);
1174
- if (!s) throw new Error(`No atlas cell for path: ${n}`);
1175
- const c = rn(e.atlasMeta, s), u = He(t);
1176
- if (sn(s)) {
1177
- const o = Cn(e, s), f = document.createElement("canvas");
1221
+ const o = (async () => {
1222
+ const a = t.cellByPath.get(n);
1223
+ if (!a) throw new Error(`No atlas cell for path: ${n}`);
1224
+ const c = Sn(t.atlasMeta, a), u = pe(e);
1225
+ if (Tn(a)) {
1226
+ const k = Xn(t, a), f = document.createElement("canvas");
1178
1227
  f.width = u.width, f.height = u.height;
1179
- const d = f.getContext("2d");
1180
- return d.fillStyle = "#000", d.fillRect(0, 0, u.width, u.height), d.drawImage(o, c.x, c.y), f;
1228
+ const s = f.getContext("2d");
1229
+ return s.fillStyle = "#000", s.fillRect(0, 0, u.width, u.height), s.drawImage(k, c.x, c.y), f;
1181
1230
  }
1182
- const p = `${e.diffBase}/${n}`, _ = await Ve(p);
1183
- if (_.naturalWidth === u.width && _.naturalHeight === u.height)
1184
- return _;
1231
+ const _ = `${t.diffBase}/${n}`, d = await Ke(_);
1232
+ if (d.naturalWidth === u.width && d.naturalHeight === u.height)
1233
+ return d;
1185
1234
  throw new Error(`Diff PNG wrong size for ${n}`);
1186
1235
  })();
1187
- return Re.set(r, a), a;
1188
- }
1189
- async function Ye(t, e, n, r = Be) {
1190
- const i = await be(), { width: a, height: s } = He(i);
1191
- (t.width !== a || t.height !== s) && (t.width = a, t.height = s);
1192
- const c = [], u = t.getContext("2d");
1193
- if (u.clearRect(0, 0, a, s), e.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
1194
- const _ = i.sil;
1195
- u.drawImage(_, 0, 0, a, s);
1236
+ return Re.set(r, o), o;
1237
+ }
1238
+ async function tt(e, t, n, r = We, i, o) {
1239
+ if (i) {
1240
+ const { width: f, height: s } = i;
1241
+ if (o && !o() || t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA)
1242
+ return;
1243
+ const w = document.createElement("canvas");
1244
+ w.width = f, w.height = s;
1245
+ const A = w.getContext("2d");
1246
+ if (A.clearRect(0, 0, f, s), A.drawImage(t, 0, 0, f, s), o && !o()) return;
1247
+ (e.width !== f || e.height !== s) && (e.width = f, e.height = s);
1248
+ const g = e.getContext("2d");
1249
+ g.clearRect(0, 0, f, s), g.drawImage(w, 0, 0);
1250
+ return;
1251
+ }
1252
+ const a = await wt(), { width: c, height: u } = pe(a);
1253
+ (e.width !== c || e.height !== u) && (e.width = c, e.height = u);
1254
+ const _ = [], d = e.getContext("2d");
1255
+ if (d.clearRect(0, 0, c, u), t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
1256
+ const f = a.sil;
1257
+ d.drawImage(f, 0, 0, c, u);
1196
1258
  return;
1197
1259
  }
1198
- if (c.length === 0) {
1199
- u.drawImage(e, 0, 0, a, s);
1260
+ if (_.length === 0) {
1261
+ d.drawImage(t, 0, 0, c, u);
1200
1262
  return;
1201
1263
  }
1202
- const p = ct(e, c, a, s, r);
1203
- u.drawImage(p, 0, 0);
1204
- }
1205
- async function Le(t, e, n = Be) {
1206
- const r = await be(ye ?? void 0), { width: i, height: a } = He(r);
1207
- (t.width !== i || t.height !== a) && (t.width = i, t.height = a);
1208
- const s = [];
1209
- e.mouthPath && s.push(await Xe(r, r.viseme, e.mouthPath)), e.expressionPath && r.expression && s.push(
1210
- await Xe(r, r.expression, e.expressionPath)
1264
+ const k = mt(t, _, c, u, r);
1265
+ d.drawImage(k, 0, 0);
1266
+ }
1267
+ async function He(e, t, n = We) {
1268
+ const r = await wt(ge ?? void 0), { width: i, height: o } = pe(r);
1269
+ (e.width !== i || e.height !== o) && (e.width = i, e.height = o);
1270
+ const a = [];
1271
+ t.mouthPath && a.push(await et(r, r.viseme, t.mouthPath)), t.expressionPath && r.expression && a.push(
1272
+ await et(r, r.expression, t.expressionPath)
1211
1273
  );
1212
- const c = t.getContext("2d");
1213
- if (c.clearRect(0, 0, i, a), s.length === 0) {
1214
- c.drawImage(r.sil, 0, 0, i, a);
1274
+ const c = e.getContext("2d");
1275
+ if (c.clearRect(0, 0, i, o), a.length === 0) {
1276
+ c.drawImage(r.sil, 0, 0, i, o);
1215
1277
  return;
1216
1278
  }
1217
- const u = ct(
1279
+ const u = mt(
1218
1280
  r.sil,
1219
- s,
1220
- i,
1221
1281
  a,
1282
+ i,
1283
+ o,
1222
1284
  n
1223
1285
  );
1224
1286
  c.drawImage(u, 0, 0);
1225
1287
  }
1226
- async function Fn(t) {
1227
- await Le(t, { mouthPath: null, expressionPath: null });
1288
+ async function Zn(e) {
1289
+ await He(e, { mouthPath: null, expressionPath: null });
1228
1290
  }
1229
- function Mn(t) {
1230
- const e = /* @__PURE__ */ new Map();
1231
- for (const n of t)
1232
- e.has(n.vtime) || e.set(n.vtime, n);
1233
- return [...e.values()].sort((n, r) => n.vtime - r.vtime);
1291
+ function er(e) {
1292
+ const t = /* @__PURE__ */ new Map();
1293
+ for (const n of e)
1294
+ t.has(n.vtime) || t.set(n.vtime, n);
1295
+ return [...t.values()].sort((n, r) => n.vtime - r.vtime);
1234
1296
  }
1235
- function On(t) {
1236
- const e = Mn(t), n = [];
1237
- for (let r = 0; r < e.length - 1; r++) {
1238
- const i = e[r].viseme, a = e[r + 1].viseme, s = e[r].vtime, c = e[r + 1].vtime;
1239
- i === a || c <= s || n.push({ from: i, to: a, fromVtime: s, toVtime: c });
1297
+ function tr(e) {
1298
+ const t = er(e), n = [];
1299
+ for (let r = 0; r < t.length - 1; r++) {
1300
+ const i = t[r].viseme, o = t[r + 1].viseme, a = t[r].vtime, c = t[r + 1].vtime;
1301
+ i === o || c <= a || n.push({ from: i, to: o, fromVtime: a, toVtime: c });
1240
1302
  }
1241
1303
  return n;
1242
1304
  }
1243
- function Un(t, e) {
1305
+ function nr(e, t) {
1244
1306
  let n = null;
1245
- for (const r of t)
1246
- e >= r.fromVtime && e < r.toVtime && (n = r);
1307
+ for (const r of e)
1308
+ t >= r.fromVtime && t < r.toVtime && (n = r);
1247
1309
  return n;
1248
1310
  }
1249
- function Ln(t, e) {
1250
- return { gapMs: Math.max(0, e - t), holdEnd: t, transStart: t };
1311
+ function rr(e, t) {
1312
+ return { gapMs: Math.max(0, t - e), holdEnd: e, transStart: e };
1251
1313
  }
1252
- const $n = 500, Bn = "__sil__";
1253
- function Vn() {
1314
+ const ir = 500, sr = "__sil__";
1315
+ function or() {
1254
1316
  return { lastDrawnKey: "" };
1255
1317
  }
1256
- function we(t) {
1257
- t.lastDrawnKey = "";
1318
+ function me(e) {
1319
+ e.lastDrawnKey = "";
1258
1320
  }
1259
- function Ze(t) {
1260
- return t ?? Bn;
1321
+ function nt(e) {
1322
+ return e ?? sr;
1261
1323
  }
1262
- function Hn(t, e) {
1263
- if (t.length === 0)
1324
+ function ar(e, t) {
1325
+ if (e.length === 0)
1264
1326
  return { diffPath: null, label: "sil" };
1265
- const n = On(t), r = Un(n, e);
1327
+ const n = tr(e), r = nr(n, t);
1266
1328
  if (r) {
1267
- const a = r.toVtime - r.fromVtime, s = Ln(r.fromVtime, r.toVtime);
1268
- if (e >= s.transStart && e < r.toVtime) {
1269
- const c = cn(r.from, r.to), u = ln(c, a);
1329
+ const o = r.toVtime - r.fromVtime, a = rr(r.fromVtime, r.toVtime);
1330
+ if (t >= a.transStart && t < r.toVtime) {
1331
+ const c = Rn(r.from, r.to), u = Dn(c, o);
1270
1332
  if (u.length > 0) {
1271
- const p = fn(
1272
- e,
1273
- s.transStart,
1333
+ const _ = xn(
1334
+ t,
1335
+ a.transStart,
1274
1336
  r.toVtime,
1275
1337
  u.length
1276
1338
  );
1277
- return { diffPath: u[p] ?? u[u.length - 1], label: `${r.from}→${r.to}` };
1339
+ return { diffPath: u[_] ?? u[u.length - 1], label: `${r.from}→${r.to}` };
1278
1340
  }
1279
- return { diffPath: Oe(r.to), label: r.to };
1341
+ return { diffPath: Ne(r.to), label: r.to };
1280
1342
  }
1281
1343
  }
1282
- const i = Jt(t, e);
1283
- return i === "sil" ? { diffPath: null, label: "sil" } : { diffPath: Oe(i), label: i };
1344
+ const i = mn(e, t);
1345
+ return i === "sil" ? { diffPath: null, label: "sil" } : { diffPath: Ne(i), label: i };
1284
1346
  }
1285
- function ut(t, e) {
1286
- const n = e ?? Ze(t.expressionPath);
1287
- return `${Ze(t.mouthPath)}|${n}`;
1347
+ function gt(e, t) {
1348
+ const n = t ?? nt(e.expressionPath);
1349
+ return `${nt(e.mouthPath)}|${n}`;
1288
1350
  }
1289
- function Nn(t, e, n, r, i, a, s, c) {
1290
- const u = Hn(e, n), p = {
1351
+ function cr(e, t, n, r, i, o, a, c) {
1352
+ const u = ar(t, n), _ = {
1291
1353
  mouthPath: u.diffPath,
1292
1354
  expressionPath: r
1293
- }, _ = ut(p, i);
1294
- _ !== a.lastDrawnKey && (a.lastDrawnKey = _, c(u.label), s(t, p));
1295
- }
1296
- function Wn(t, e, n, r, i, a, s = null) {
1297
- const c = { mouthPath: null, expressionPath: e }, u = s != null ? `|idle@${Math.round(s * 1e3)}` : "", p = ut(c, n) + u;
1298
- !(s != null) && p === r.lastDrawnKey || (r.lastDrawnKey = p, a(
1299
- s != null ? "idle" : "sil"
1300
- ), i(t, c));
1301
- }
1302
- function Kn(t) {
1303
- return new Worker(
1304
- "/assets/visemeDiffPreview.worker-B8Juk7ys.js",
1305
- {
1306
- name: t?.name
1307
- }
1308
- );
1355
+ }, d = gt(_, i);
1356
+ d !== o.lastDrawnKey && (o.lastDrawnKey = d, c(u.label), a(e, _));
1357
+ }
1358
+ function rt(e, t, n, r, i, o, a = null) {
1359
+ const c = { mouthPath: null, expressionPath: t }, u = a != null ? `|idle@${Math.round(a * 1e3)}` : "", _ = gt(c, n) + u;
1360
+ !(a != null) && _ === r.lastDrawnKey || (r.lastDrawnKey = _, o(
1361
+ a != null ? "idle" : "sil"
1362
+ ), i(e, c));
1363
+ }
1364
+ const it = /* @__PURE__ */ new Map();
1365
+ async function ur(e) {
1366
+ let t = it.get(e);
1367
+ if (!t) {
1368
+ const n = await fetch(e);
1369
+ if (!n.ok)
1370
+ throw new Error(
1371
+ `Failed to fetch worker script (${n.status}): ${e}`
1372
+ );
1373
+ const r = await n.blob();
1374
+ t = URL.createObjectURL(r), it.set(e, t);
1375
+ }
1376
+ return new Worker(t, { name: "visemeDiffPreview" });
1309
1377
  }
1310
- function qn() {
1378
+ function lr() {
1311
1379
  return typeof Worker < "u" && typeof OffscreenCanvas < "u";
1312
1380
  }
1313
- function jn(t, e) {
1314
- (t.width !== e.width || t.height !== e.height) && (t.width = e.width, t.height = e.height);
1315
- const n = t.getContext("2d");
1316
- n && n.drawImage(e, 0, 0);
1381
+ function fr(e, t) {
1382
+ (e.width !== t.width || e.height !== t.height) && (e.width = t.width, e.height = t.height);
1383
+ const n = e.getContext("2d");
1384
+ n && n.drawImage(t, 0, 0);
1317
1385
  }
1318
- function et(t, e) {
1386
+ function st(e, t) {
1319
1387
  let n = 0;
1320
1388
  return {
1321
1389
  usesWorker: !1,
1322
1390
  async loadAssets(r) {
1323
- await be(r);
1391
+ return await Ve(r);
1324
1392
  },
1325
1393
  async renderSilOnly() {
1326
- n += 1, await Fn(t);
1394
+ n += 1, await Zn(e);
1327
1395
  },
1328
1396
  async drawLiveFrame(r, i) {
1329
- await be(), await Le(r, i);
1397
+ await Ve(), await He(r, i);
1330
1398
  },
1331
1399
  async renderViseme(r, i) {
1332
1400
  n += 1;
1333
- const a = n;
1334
- e.onStatus?.(i), await Le(r, {
1335
- mouthPath: Oe(i),
1401
+ const o = n;
1402
+ t.onStatus?.(i), await He(r, {
1403
+ mouthPath: Ne(i),
1336
1404
  expressionPath: null
1337
1405
  });
1338
1406
  },
@@ -1341,157 +1409,177 @@ function et(t, e) {
1341
1409
  }
1342
1410
  };
1343
1411
  }
1344
- function zn(t, e) {
1345
- const n = new Kn();
1346
- let r = 1, i = 0, a = !1, s = !1;
1347
- const c = /* @__PURE__ */ new Map(), u = (o, f) => {
1348
- s || n.postMessage(o, []);
1349
- }, p = (o, f, d = !1) => {
1350
- if (s) return Promise.resolve();
1351
- const k = r++;
1352
- return new Promise((x, F) => {
1353
- c.set(k, {
1354
- resolve: () => x(),
1355
- reject: F,
1356
- generation: typeof o.generation == "number" ? o.generation : void 0,
1357
- expectRenderDone: d
1358
- }), u({ ...o, requestId: k });
1412
+ function dr(e, t, n) {
1413
+ let r = null, i = 1, o = 0, a = !1, c = !1;
1414
+ const u = /* @__PURE__ */ new Map(), _ = (f, s) => {
1415
+ c || !r || r.postMessage(f, []);
1416
+ }, d = (f, s, w = !1) => {
1417
+ if (c) return Promise.resolve();
1418
+ const A = i++;
1419
+ return new Promise((g, V) => {
1420
+ u.set(A, {
1421
+ resolve: (F) => g(F),
1422
+ reject: V,
1423
+ generation: typeof f.generation == "number" ? f.generation : void 0,
1424
+ expectRenderDone: w
1425
+ }), _({ ...f, requestId: A });
1359
1426
  });
1360
- };
1361
- n.onmessage = (o) => {
1362
- if (s) return;
1363
- const f = o.data;
1364
- if (f.type === "status") {
1365
- e.onStatus?.(f.label);
1366
- return;
1367
- }
1368
- if (f.type === "frame") {
1369
- const k = f.generation < 0;
1370
- if (!k && f.generation !== i) {
1371
- f.bitmap.close();
1427
+ }, k = (async () => {
1428
+ r = await ur(n), r.onmessage = (f) => {
1429
+ if (c) return;
1430
+ const s = f.data;
1431
+ if (s.type === "status") {
1432
+ t.onStatus?.(s.label);
1372
1433
  return;
1373
1434
  }
1374
- if (k && e.shouldAcceptLiveFrame && !e.shouldAcceptLiveFrame()) {
1375
- f.bitmap.close();
1435
+ if (s.type === "frame") {
1436
+ const A = s.generation < 0;
1437
+ if (!A && s.generation !== o) {
1438
+ s.bitmap.close();
1439
+ return;
1440
+ }
1441
+ if (A && t.shouldAcceptLiveFrame && !t.shouldAcceptLiveFrame()) {
1442
+ s.bitmap.close();
1443
+ return;
1444
+ }
1445
+ fr(e, s.bitmap), s.bitmap.close();
1376
1446
  return;
1377
1447
  }
1378
- jn(t, f.bitmap), f.bitmap.close();
1379
- return;
1380
- }
1381
- const d = c.get(f.requestId);
1382
- if (d)
1383
- switch (f.type) {
1384
- case "ready":
1385
- case "loadAssetsDone":
1386
- c.delete(f.requestId), d.resolve();
1387
- break;
1388
- case "renderDone":
1389
- if (d.expectRenderDone && d.generation != null && f.generation !== d.generation)
1390
- return;
1391
- c.delete(f.requestId), d.resolve();
1392
- break;
1393
- case "renderAborted":
1394
- c.delete(f.requestId), d.resolve();
1395
- break;
1396
- case "error":
1397
- c.delete(f.requestId), e.onError?.(f.message), d.reject(new Error(f.message));
1398
- break;
1399
- }
1400
- }, n.onerror = (o) => {
1401
- if (s) return;
1402
- const f = o.message || "Worker error";
1403
- e.onError?.(f);
1404
- for (const [, d] of c)
1405
- d.reject(new Error(f));
1406
- c.clear();
1407
- };
1408
- const _ = (async () => {
1409
- await p({ type: "init" }), a = !0;
1448
+ const w = u.get(s.requestId);
1449
+ if (w)
1450
+ switch (s.type) {
1451
+ case "ready":
1452
+ u.delete(s.requestId), w.resolve();
1453
+ break;
1454
+ case "loadAssetsDone":
1455
+ u.delete(s.requestId), w.resolve({ width: s.width, height: s.height });
1456
+ break;
1457
+ case "renderDone":
1458
+ if (w.expectRenderDone && w.generation != null && s.generation !== w.generation)
1459
+ return;
1460
+ u.delete(s.requestId), w.resolve();
1461
+ break;
1462
+ case "renderAborted":
1463
+ u.delete(s.requestId), w.resolve();
1464
+ break;
1465
+ case "error":
1466
+ u.delete(s.requestId), t.onError?.(s.message), w.reject(new Error(s.message));
1467
+ break;
1468
+ }
1469
+ }, r.onerror = (f) => {
1470
+ if (c) return;
1471
+ const s = f.message || "Worker error";
1472
+ t.onError?.(s);
1473
+ for (const [, w] of u)
1474
+ w.reject(new Error(s));
1475
+ u.clear();
1476
+ }, await d({ type: "init" }), a = !0;
1410
1477
  })();
1411
1478
  return {
1412
1479
  usesWorker: !0,
1413
- async loadAssets(o) {
1414
- if (!s && (await _, !s)) {
1415
- if (!a) throw new Error("Worker init failed");
1416
- await p({
1417
- type: "loadAssets",
1418
- keyHex: o,
1419
- urls: re()
1420
- });
1421
- }
1480
+ async loadAssets(f) {
1481
+ if (c) return { width: 842, height: 1264 };
1482
+ if (await k, c) return { width: 842, height: 1264 };
1483
+ if (!a) throw new Error("Worker init failed");
1484
+ return await d({
1485
+ type: "loadAssets",
1486
+ keyHex: f,
1487
+ urls: ne()
1488
+ }) ?? { width: 842, height: 1264 };
1422
1489
  },
1423
1490
  async renderSilOnly() {
1424
- if (s || (await _, s)) return;
1425
- i += 1;
1426
- const o = i;
1427
- await p(
1428
- { type: "renderSil", generation: o },
1491
+ if (c || (await k, c)) return;
1492
+ o += 1;
1493
+ const f = o;
1494
+ await d(
1495
+ { type: "renderSil", generation: f },
1429
1496
  void 0,
1430
1497
  !0
1431
1498
  );
1432
1499
  },
1433
- async drawLiveFrame(o, f) {
1434
- s || (await _, !s && await p(
1500
+ async drawLiveFrame(f, s) {
1501
+ c || (await k, !c && await d(
1435
1502
  {
1436
1503
  type: "drawFrame",
1437
- diffPath: f.mouthPath,
1438
- expressionDiffPath: f.expressionPath
1504
+ diffPath: s.mouthPath,
1505
+ expressionDiffPath: s.expressionPath
1439
1506
  },
1440
1507
  void 0,
1441
1508
  !0
1442
1509
  ));
1443
1510
  },
1444
- async renderViseme(o, f, d = {}) {
1445
- if (s || (await _, s)) return;
1446
- i += 1;
1447
- const k = i;
1511
+ async renderViseme(f, s, w = {}) {
1512
+ if (c || (await k, c)) return;
1513
+ o += 1;
1514
+ const A = o;
1448
1515
  try {
1449
- await p(
1516
+ await d(
1450
1517
  {
1451
1518
  type: "renderViseme",
1452
- generation: k,
1453
- to: f,
1454
- from: d.from,
1455
- transitionDurationMs: d.transitionDurationMs,
1456
- gapMs: d.gapMs,
1457
- threshold: d.threshold
1519
+ generation: A,
1520
+ to: s,
1521
+ from: w.from,
1522
+ transitionDurationMs: w.transitionDurationMs,
1523
+ gapMs: w.gapMs,
1524
+ threshold: w.threshold
1458
1525
  },
1459
1526
  void 0,
1460
1527
  !0
1461
1528
  );
1462
- } catch (x) {
1463
- if (x?.name === "AbortError") return;
1464
- throw x;
1529
+ } catch (g) {
1530
+ if (g?.name === "AbortError") return;
1531
+ throw g;
1465
1532
  }
1466
1533
  },
1467
1534
  dispose() {
1468
- if (!s) {
1469
- s = !0, i += 1;
1470
- for (const [, o] of c)
1471
- o.resolve();
1472
- c.clear(), n.terminate();
1535
+ if (!c) {
1536
+ c = !0, o += 1;
1537
+ for (const [, f] of u)
1538
+ f.resolve();
1539
+ u.clear(), r?.terminate(), r = null;
1473
1540
  }
1474
1541
  }
1475
1542
  };
1476
1543
  }
1477
- function Gn(t, e = {}) {
1478
- if (!qn())
1479
- return et(t, e);
1544
+ function hr(e, t = {}) {
1545
+ if (!lr())
1546
+ return st(e, t);
1547
+ const n = t.workerScriptUrl ?? Rt;
1480
1548
  try {
1481
- return zn(t, e);
1549
+ return dr(e, t, n);
1482
1550
  } catch {
1483
- return et(t, e);
1551
+ return st(e, t);
1484
1552
  }
1485
1553
  }
1486
- const Qn = {
1554
+ class _r extends Error {
1555
+ constructor(t, n) {
1556
+ super(`${t} timed out after ${Math.round(n / 1e3)}s`), this.name = "LoadTimeoutError";
1557
+ }
1558
+ }
1559
+ function Pe(e, t, n) {
1560
+ return new Promise((r, i) => {
1561
+ const o = globalThis.setTimeout(() => {
1562
+ i(new _r(n, t));
1563
+ }, t);
1564
+ e.then(
1565
+ (a) => {
1566
+ globalThis.clearTimeout(o), r(a);
1567
+ },
1568
+ (a) => {
1569
+ globalThis.clearTimeout(o), i(a);
1570
+ }
1571
+ );
1572
+ });
1573
+ }
1574
+ const mr = {
1487
1575
  position: "relative",
1488
1576
  display: "inline-block",
1489
1577
  lineHeight: 0
1490
- }, Jn = {
1578
+ }, wr = {
1491
1579
  display: "block",
1492
1580
  maxWidth: "100%",
1493
1581
  height: "auto"
1494
- }, Xn = {
1582
+ }, gr = {
1495
1583
  position: "absolute",
1496
1584
  inset: 0,
1497
1585
  display: "flex",
@@ -1503,277 +1591,304 @@ const Qn = {
1503
1591
  fontSize: "0.875rem",
1504
1592
  textAlign: "center"
1505
1593
  };
1506
- function Ie(t) {
1507
- return t === "cartesia" ? en : rt;
1594
+ function Ae(e) {
1595
+ return Ct(e) ? yn : lt;
1508
1596
  }
1509
- const tr = dt(function({
1510
- id: e,
1597
+ const kr = Et(function({
1598
+ id: t,
1511
1599
  assets: n,
1512
1600
  authToken: r,
1513
- apiBase: i = $e,
1514
- facesCdnBase: a = tt,
1515
- tts: s = "google",
1516
- voiceId: c,
1517
- speakingRate: u = it,
1518
- className: p,
1519
- style: _,
1520
- canvasStyle: o,
1521
- onStatusChange: f,
1522
- onDisplayStatus: d,
1523
- onError: k,
1524
- onReady: x,
1525
- showErrorOverlay: F = !0
1526
- }, Z) {
1527
- const H = M(null), h = M(null), g = M(Vn()), P = M(In()), O = M(0), C = M(0), K = M(null), ie = M(!1), D = M(
1601
+ ttsEngineId: i = oe,
1602
+ voiceId: o,
1603
+ speakingRate: a = ft,
1604
+ className: c,
1605
+ style: u,
1606
+ canvasStyle: _,
1607
+ onStatusChange: d,
1608
+ onDisplayStatus: k,
1609
+ onError: f,
1610
+ onReady: s,
1611
+ showErrorOverlay: w = !0
1612
+ }, A) {
1613
+ const g = L(null), V = L(null), F = L(or()), N = L(Kn()), Q = L(0), m = L(0), p = L(null), R = L(!1), v = L(
1528
1614
  null
1529
- ), N = M(!1), [se, fe] = _e("idle"), [oe, de] = _e(null), [z, ke] = _e(!1), [he, ae] = _e(!1), [Ee, Ne] = _e({ width: 842, height: 1264 }), m = M(r ?? null), R = M(null), A = M(null), T = M(!1), b = M(x), I = M(d), q = M(k);
1530
- b.current = x, I.current = d, q.current = k;
1531
- const B = ee(
1615
+ ), H = L(!1), [W, ae] = _e("idle"), [ye, X] = _e(null), [K, Ee] = _e(!1), [Me, ce] = _e(!1), [re, ke] = _e({ width: 842, height: 1264 }), Z = L(r ?? null), ue = L(null), h = L(null), b = L(!1), U = L({ width: 842, height: 1264 }), C = L(s), E = L(k), P = L(f);
1616
+ C.current = s, E.current = k, P.current = f;
1617
+ const z = ee(
1532
1618
  (l) => {
1533
- const y = l?.tts ?? s ?? A.current?.tts ?? "google", E = l?.voiceId ?? c ?? A.current?.voiceId ?? Ie(y);
1534
- return { tts: y, voiceId: E };
1619
+ const y = l?.ttsEngineId ?? i ?? h.current?.ttsEngineId ?? oe, S = l?.voiceId ?? o ?? h.current?.voiceId ?? Ae(y);
1620
+ return { ttsEngineId: y, voiceId: S };
1535
1621
  },
1536
- [s, c]
1537
- ), v = ee((l) => {
1538
- de(l), q.current?.(l);
1539
- }, []), G = ee(
1622
+ [i, o]
1623
+ ), B = ee((l) => {
1624
+ X(l), P.current?.(l);
1625
+ }, []), M = ee(
1540
1626
  (l) => {
1541
- fe(l), f?.(l);
1627
+ ae(l), d?.(l);
1542
1628
  },
1543
- [f]
1544
- ), S = M(tn(G, i)), V = ee(() => {
1545
- K.current != null && (clearTimeout(K.current), K.current = null);
1546
- }, []), $ = ee(async () => {
1547
- const l = C.current, y = h.current;
1548
- !y || !z || S.current.isSpeaking() || (we(g.current), !N.current && (await y.renderSilOnly(), l !== C.current || S.current.isSpeaking()));
1549
- }, [z]), J = ee(() => {
1550
- V();
1551
- const l = C.current;
1552
- $(), K.current = setTimeout(() => {
1553
- K.current = null, l === C.current && $();
1554
- }, $n);
1555
- }, [V, $]), ce = ee(async () => {
1629
+ [d]
1630
+ ), D = L(
1631
+ En(M, ve)
1632
+ ), x = ee(() => {
1633
+ p.current != null && (clearTimeout(p.current), p.current = null);
1634
+ }, []), J = ee(async () => {
1635
+ const l = m.current, y = V.current;
1636
+ !y || !K || D.current.isSpeaking() || (me(F.current), await y.renderSilOnly(), l === m.current && (D.current.isSpeaking() || (E.current?.("sil"), H.current && v.current && (v.current.restart(), v.current.setActive(!0)))));
1637
+ }, [K]), $ = ee(() => {
1638
+ x();
1639
+ const l = m.current;
1640
+ J(), p.current = setTimeout(() => {
1641
+ p.current = null, l === m.current && J();
1642
+ }, ir);
1643
+ }, [x, J]), le = ee(async () => {
1556
1644
  const l = Math.floor(Date.now() / 1e3);
1557
- if (R.current && l < R.current.expiresAt - 60)
1558
- return R.current.key;
1559
- const y = m.current ?? await Ke();
1560
- m.current = y, S.current.setDeveloperToken(y);
1561
- const E = await St(y, i);
1562
- return R.current = {
1563
- key: E.value,
1564
- expiresAt: E.expiresAt
1565
- }, E.value;
1566
- }, [i]);
1567
- le(() => {
1568
- r && (m.current = r, S.current.setDeveloperToken(r));
1569
- }, [r]), le(() => {
1570
- S.current.setTtsProvider(s), A.current && (A.current.tts = s);
1571
- }, [s]), le(() => {
1572
- const l = c ?? Ie(s);
1573
- A.current && (A.current.voiceId = l);
1574
- }, [c, s]);
1575
- const U = ht(
1576
- () => n ? JSON.stringify(n) : `id:${e ?? ""}:${a}`,
1577
- [n, e, a]
1645
+ if (ue.current && l < ue.current.expiresAt - 60)
1646
+ return ue.current.key;
1647
+ const y = Z.current ?? await je();
1648
+ Z.current = y, D.current.setDeveloperToken(y);
1649
+ const S = await Nt(y, ve);
1650
+ return ue.current = {
1651
+ key: S.value,
1652
+ expiresAt: S.expiresAt
1653
+ }, S.value;
1654
+ }, []);
1655
+ ie(() => {
1656
+ r && (Z.current = r, D.current.setDeveloperToken(r));
1657
+ }, [r]), ie(() => {
1658
+ D.current.setTtsEngineId(i), h.current && (h.current.ttsEngineId = i);
1659
+ }, [i]), ie(() => {
1660
+ const l = o ?? Ae(i);
1661
+ h.current && (h.current.voiceId = l);
1662
+ }, [o, i]);
1663
+ const fe = kt(
1664
+ () => n ? JSON.stringify(n) : `id:${t ?? ""}`,
1665
+ [n, t]
1578
1666
  );
1579
- return le(() => {
1580
- if (!e && !n) {
1581
- v("AiTwin requires either `id` or `assets`");
1667
+ return ie(() => {
1668
+ if (!t && !n) {
1669
+ B("AiTwin requires either `id` or `assets`");
1582
1670
  return;
1583
1671
  }
1584
1672
  let l = !1;
1585
- T.current = !1, ke(!1), ae(!1), de(null), A.current = null, D.current = null, N.current = !1;
1586
- const y = H.current;
1673
+ b.current = !1, Ee(!1), ce(!1), X(null), h.current = null, v.current = null, H.current = !1;
1674
+ const y = g.current;
1587
1675
  if (!y) return;
1588
- const E = Gn(y, {
1589
- onStatus: (w) => {
1590
- l || I.current?.(w);
1591
- },
1592
- onError: (w) => {
1593
- l || v(w);
1594
- },
1595
- shouldAcceptLiveFrame: () => S.current.isSpeaking()
1596
- });
1597
- return h.current = E, (async () => {
1676
+ let S = null;
1677
+ return (async () => {
1598
1678
  try {
1599
- let w, L, Q;
1679
+ let T, I, q;
1600
1680
  if (n)
1601
- w = n, L = s, Q = c ?? Ie(L);
1681
+ T = n, I = i, q = o ?? Ae(I);
1602
1682
  else {
1603
- const Y = await wt(e);
1683
+ E.current?.("getAiTwin");
1684
+ const O = await Pe(
1685
+ Ft(t),
1686
+ 3e4,
1687
+ "getAiTwin"
1688
+ );
1604
1689
  if (l) return;
1605
- L = Y.tts, Q = c ?? Y.voiceId ?? Ie(L), w = It(Y.faceId, a);
1690
+ if (!O.faceId?.trim())
1691
+ throw new Error("getAiTwin returned no faceId");
1692
+ I = O.ttsEngineId, q = o ?? O.voiceId ?? Ae(I), T = Wt(O.faceId);
1606
1693
  }
1607
- A.current = { tts: L, voiceId: Q }, Rt(w), D.current = Rn(Dt());
1608
- const X = m.current ?? r ?? await Ke();
1694
+ h.current = { ttsEngineId: I, voiceId: q }, Kt(T), v.current = qn(jt()), S = hr(y, {
1695
+ onStatus: (O) => {
1696
+ l || E.current?.(O);
1697
+ },
1698
+ onError: (O) => {
1699
+ l || B(O);
1700
+ },
1701
+ shouldAcceptLiveFrame: () => D.current.isSpeaking()
1702
+ }), V.current = S, E.current?.("auth");
1703
+ const Y = Z.current ?? r ?? await Pe(je(), 3e4, "getAuthToken");
1609
1704
  if (l) return;
1610
- m.current = X, S.current.setDeveloperToken(X), S.current.setTtsProvider(L);
1611
- const Se = ne() ? await ce() : void 0;
1705
+ Z.current = Y, D.current.setDeveloperToken(Y), D.current.setTtsEngineId(I);
1706
+ const Fe = ut() ? await le() : void 0;
1612
1707
  if (l) return;
1613
- const j = await hn(Se);
1614
- if (!l && j.width > 0 && j.height > 0 && Ne(j), await E.loadAssets(Se), l) return;
1615
- let ue = !1;
1616
- try {
1617
- await D.current.load(), ue = !0, N.current = !0, ae(!0);
1618
- } catch (Y) {
1619
- N.current = !1, ae(!1), console.warn("[AiTwin] Idle video unavailable:", Y);
1620
- }
1621
- if (!ue)
1622
- await E.renderSilOnly(), I.current?.("sil");
1623
- else {
1624
- we(g.current);
1625
- const Y = D.current.getVideo();
1626
- D.current.setActive(!0);
1708
+ E.current?.("loadAssets");
1709
+ const G = await Pe(
1710
+ S.loadAssets(Fe),
1711
+ 18e4,
1712
+ "load face assets"
1713
+ );
1714
+ if (!l && G.width > 0 && G.height > 0 && (U.current = G, ke(G)), l) return;
1715
+ b.current = !0, Ee(!0), C.current?.(), S.renderSilOnly().then(() => {
1716
+ l || E.current?.("sil");
1717
+ }), (async () => {
1627
1718
  try {
1628
- await Ye(y, Y, null), I.current?.("idle");
1629
- } catch (me) {
1630
- console.warn("[AiTwin] Initial idle frame paint failed:", me);
1719
+ if (await v.current.load(), l) return;
1720
+ H.current = !0, ce(!0), me(F.current), v.current.setActive(!0);
1721
+ const O = v.current.getVideo();
1722
+ await tt(
1723
+ y,
1724
+ O,
1725
+ null,
1726
+ void 0,
1727
+ U.current
1728
+ ), l || E.current?.("idle");
1729
+ } catch (O) {
1730
+ if (l) return;
1731
+ H.current = !1, ce(!1), console.warn("[AiTwin] Idle video unavailable:", O);
1631
1732
  }
1632
- }
1633
- if (l) return;
1634
- T.current = !0, ke(!0), b.current?.();
1635
- } catch (w) {
1733
+ })();
1734
+ } catch (T) {
1636
1735
  if (l) return;
1637
- if (w instanceof Pe) {
1638
- v(w.message);
1736
+ if (T instanceof Oe) {
1737
+ B(T.message);
1639
1738
  return;
1640
1739
  }
1641
- v(w instanceof Error ? w.message : "Failed to load AI twin");
1740
+ B(T instanceof Error ? T.message : "Failed to load AI twin");
1642
1741
  }
1643
1742
  })(), () => {
1644
- l = !0, E.dispose(), h.current = null;
1743
+ l = !0, S?.dispose(), V.current = null;
1645
1744
  };
1646
- }, [U, e, a, r, i, ce, v]), le(() => {
1647
- se === "done" && z && !S.current.isSpeaking() && J();
1648
- }, [se, z, J]), le(() => {
1649
- const l = S.current, y = { current: !1 }, E = () => {
1650
- const w = H.current, L = h.current;
1651
- if (!w || !L || !z) {
1652
- O.current = requestAnimationFrame(E);
1745
+ }, [fe, n, i, o, r, le, B]), ie(() => {
1746
+ W === "done" && K && !D.current.isSpeaking() && $();
1747
+ }, [W, K, $]), ie(() => {
1748
+ const l = D.current, y = { current: !1 }, S = () => {
1749
+ const T = g.current, I = V.current;
1750
+ if (!T || !I || !K) {
1751
+ Q.current = requestAnimationFrame(S);
1653
1752
  return;
1654
1753
  }
1655
- const Q = l.isSpeaking(), X = l.getPlaybackElapsedMs(), Se = Q ? xn(
1656
- X,
1754
+ const q = l.isSpeaking(), Y = l.getPlaybackElapsedMs(), Fe = q ? Qn(
1755
+ Y,
1657
1756
  l.getSentenceEmotions(),
1658
1757
  l.getWordQueue(),
1659
1758
  l.getStreamMood()
1660
1759
  ) : l.getStreamMood();
1661
- if (Q) {
1662
- if (N.current && D.current?.setActive(!1), l.getVisemeQueue().length === 0) {
1663
- O.current = requestAnimationFrame(E);
1760
+ if (q) {
1761
+ if (H.current && v.current?.setActive(!1), l.getVisemeQueue().length === 0) {
1762
+ Q.current = requestAnimationFrame(S);
1664
1763
  return;
1665
1764
  }
1666
- P.current.setTargetEmotion(Se), P.current.advance(performance.now());
1667
- const ue = P.current.getExpressionPath(), Y = P.current.getDrawKey();
1668
- Nn(
1669
- w,
1765
+ N.current.setTargetEmotion(Fe), N.current.advance(performance.now());
1766
+ const O = N.current.getExpressionPath(), de = N.current.getDrawKey();
1767
+ cr(
1768
+ T,
1670
1769
  l.getVisemeQueue(),
1671
- X,
1672
- ue,
1673
1770
  Y,
1674
- g.current,
1675
- (me, lt) => L.drawLiveFrame(me, lt),
1676
- (me) => I.current?.(me)
1771
+ O,
1772
+ de,
1773
+ F.current,
1774
+ (he, pt) => I.drawLiveFrame(he, pt),
1775
+ (he) => E.current?.(he)
1677
1776
  );
1678
- } else if (N.current && D.current) {
1679
- const j = D.current;
1680
- j.setActive(!0), y.current || (y.current = !0, Ye(w, j.getVideo()).then(() => I.current?.("idle")).finally(() => {
1777
+ } else if (H.current && v.current) {
1778
+ const G = v.current, O = G.getVideo();
1779
+ O.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA ? (G.setActive(!0), y.current || (y.current = !0, tt(
1780
+ T,
1781
+ O,
1782
+ null,
1783
+ void 0,
1784
+ U.current,
1785
+ () => !l.isSpeaking()
1786
+ ).then(() => E.current?.("idle")).finally(() => {
1681
1787
  y.current = !1;
1682
- }));
1788
+ }))) : rt(
1789
+ T,
1790
+ null,
1791
+ "__sil__",
1792
+ F.current,
1793
+ (de, he) => I.drawLiveFrame(de, he),
1794
+ (de) => E.current?.(de)
1795
+ );
1683
1796
  } else
1684
- Wn(
1685
- w,
1797
+ rt(
1798
+ T,
1686
1799
  null,
1687
1800
  "__sil__",
1688
- g.current,
1689
- (j, ue) => L.drawLiveFrame(j, ue),
1690
- (j) => I.current?.(j)
1801
+ F.current,
1802
+ (G, O) => I.drawLiveFrame(G, O),
1803
+ (G) => E.current?.(G)
1691
1804
  );
1692
- ie.current && !Q && (P.current.reset(), N.current && D.current?.restart(), J()), ie.current = Q, O.current = requestAnimationFrame(E);
1805
+ R.current && !q && (N.current.reset(), v.current?.setActive(!1), $()), R.current = q, Q.current = requestAnimationFrame(S);
1693
1806
  };
1694
- return O.current = requestAnimationFrame(E), () => {
1695
- cancelAnimationFrame(O.current), V(), l.stop();
1807
+ return Q.current = requestAnimationFrame(S), () => {
1808
+ cancelAnimationFrame(Q.current), x(), l.stop();
1696
1809
  };
1697
- }, [z, he, J, V]), mt(
1698
- Z,
1810
+ }, [K, Me, $, x]), St(
1811
+ A,
1699
1812
  () => ({
1700
1813
  speakText: async (l, y) => {
1701
- const E = l.trim();
1702
- if (!E) return;
1703
- const w = h.current;
1704
- if (!w || !T.current && !z)
1814
+ const S = l.trim();
1815
+ if (!S) return;
1816
+ const T = V.current;
1817
+ if (!T || !b.current && !K)
1705
1818
  throw new Error("AI twin is not ready");
1706
- const { tts: L, voiceId: Q } = B(y);
1707
- de(null), C.current += 1, V(), we(g.current), P.current.reset(), ie.current = !1;
1819
+ const { ttsEngineId: I, voiceId: q } = z(y);
1820
+ X(null), m.current += 1, x(), me(F.current), N.current.reset(), R.current = !1;
1708
1821
  try {
1709
- N.current ? (D.current?.setActive(!1), D.current?.restart(), we(g.current)) : await w.renderSilOnly(), await S.current.speak(E, {
1710
- voiceId: Q,
1711
- speakingRate: y?.speakingRate ?? u,
1712
- tts: L
1822
+ H.current ? (v.current?.setActive(!1), v.current?.restart(), me(F.current)) : await T.renderSilOnly(), await D.current.speak(S, {
1823
+ voiceId: q,
1824
+ speakingRate: y?.speakingRate ?? a,
1825
+ ttsEngineId: I
1713
1826
  });
1714
- } catch (X) {
1715
- throw v(X instanceof Error ? X.message : "TTS failed"), X;
1827
+ } catch (Y) {
1828
+ throw B(Y instanceof Error ? Y.message : "TTS failed"), Y;
1716
1829
  }
1717
1830
  },
1718
1831
  stop: () => {
1719
- C.current += 1, V(), S.current.stop(), we(g.current), ie.current = !1, J();
1832
+ m.current += 1, x(), D.current.stop(), me(F.current), R.current = !1, $();
1720
1833
  },
1721
- setTtsProvider: (l) => {
1722
- S.current.setTtsProvider(l), A.current && (A.current.tts = l);
1834
+ setTtsEngineId: (l) => {
1835
+ D.current.setTtsEngineId(l), h.current && (h.current.ttsEngineId = l);
1723
1836
  },
1724
1837
  renderViseme: async (l, y) => {
1725
- const E = h.current, w = H.current;
1726
- if (!E || !w || !T.current)
1838
+ const S = V.current, T = g.current;
1839
+ if (!S || !T || !b.current)
1727
1840
  throw new Error("AI twin is not ready");
1728
- await E.renderViseme(w, l, {
1841
+ await S.renderViseme(T, l, {
1729
1842
  from: y?.from,
1730
1843
  transitionDurationMs: y?.transitionDurationMs,
1731
1844
  gapMs: y?.gapMs,
1732
- onStatus: (L) => I.current?.(L)
1845
+ onStatus: (I) => E.current?.(I)
1733
1846
  });
1734
1847
  },
1735
- isReady: () => T.current,
1736
- getStatus: () => se
1848
+ isReady: () => b.current,
1849
+ getStatus: () => W
1737
1850
  }),
1738
1851
  [
1739
- z,
1740
- u,
1741
- V,
1742
- J,
1743
- v,
1852
+ K,
1853
+ a,
1854
+ x,
1855
+ $,
1744
1856
  B,
1745
- se
1857
+ z,
1858
+ W
1746
1859
  ]
1747
- ), /* @__PURE__ */ ft("div", { className: p, style: { ...Qn, ..._ }, children: [
1748
- /* @__PURE__ */ We(
1860
+ ), /* @__PURE__ */ yt("div", { className: c, style: { ...mr, ...u }, children: [
1861
+ /* @__PURE__ */ qe(
1749
1862
  "canvas",
1750
1863
  {
1751
- ref: H,
1752
- width: Ee.width,
1753
- height: Ee.height,
1754
- style: { ...Jn, ...o },
1755
- "aria-label": e ? `AI twin ${e}` : "AI twin face"
1864
+ ref: g,
1865
+ width: re.width,
1866
+ height: re.height,
1867
+ style: { ...wr, ..._ },
1868
+ "aria-label": t ? `AI twin ${t}` : "AI twin face"
1756
1869
  }
1757
1870
  ),
1758
- F && oe ? /* @__PURE__ */ We("div", { style: Xn, children: oe }) : null
1871
+ w && ye ? /* @__PURE__ */ qe("div", { style: gr, children: ye }) : null
1759
1872
  ] });
1760
1873
  });
1761
1874
  export {
1762
- tr as AiTwin,
1763
- Pe as AiTwinNotFoundError,
1764
- $e as DEFAULT_API_BASE,
1765
- tt as DEFAULT_FACES_CDN_BASE,
1766
- Mt as OCULUS_VISEME_IDS,
1767
- Zt as SPEAKING_RATE_MAX,
1768
- Yt as SPEAKING_RATE_MIN,
1769
- er as VISEME_IDS,
1770
- en as VISEME_TEST_CARTESIA_VOICE_ID,
1771
- it as VISEME_TEST_SPEAKING_RATE,
1772
- rt as VISEME_TEST_VOICE_ID,
1773
- tn as createAvatarTtsLipsyncController,
1774
- wt as fetchAiTwin,
1775
- Ke as fetchDevAuthToken,
1776
- Ut as normalizeOculusViseme,
1777
- Jt as resolveVisemeAtTime,
1778
- Xt as resolveWordAtTime
1875
+ kr as AiTwin,
1876
+ Oe as AiTwinNotFoundError,
1877
+ at as DEFAULT_TTS_ENGINE_ID,
1878
+ Xt as OCULUS_VISEME_IDS,
1879
+ pn as SPEAKING_RATE_MAX,
1880
+ gn as SPEAKING_RATE_MIN,
1881
+ De as TTS_ENGINE_CARTESIA,
1882
+ oe as TTS_ENGINE_GOOGLE,
1883
+ be as TTS_ENGINE_INWORLD,
1884
+ Er as VISEME_IDS,
1885
+ yn as VISEME_TEST_CARTESIA_VOICE_ID,
1886
+ ft as VISEME_TEST_SPEAKING_RATE,
1887
+ lt as VISEME_TEST_VOICE_ID,
1888
+ En as createAvatarTtsLipsyncController,
1889
+ Ft as fetchAiTwin,
1890
+ je as fetchDevAuthToken,
1891
+ en as normalizeOculusViseme,
1892
+ mn as resolveVisemeAtTime,
1893
+ wn as resolveWordAtTime
1779
1894
  };