@streamoji/aitwin 0.1.1 → 0.1.2

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