@peerbots/face 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -2
- package/dist/VizijFaceView-DEMuLMAb.d.ts +63 -0
- package/dist/chunk-EAYBAWGE.js +1487 -0
- package/dist/chunk-EAYBAWGE.js.map +1 -0
- package/dist/chunk-QGFC2GUD.js +211 -0
- package/dist/chunk-QGFC2GUD.js.map +1 -0
- package/dist/chunk-ZEXL2QPJ.js +68 -0
- package/dist/chunk-ZEXL2QPJ.js.map +1 -0
- package/dist/index.cjs +4639 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +557 -0
- package/dist/index.js +1660 -0
- package/dist/index.js.map +1 -0
- package/dist/pixi.cjs +2421 -0
- package/dist/pixi.cjs.map +1 -0
- package/dist/pixi.d.ts +33 -0
- package/dist/pixi.js +889 -0
- package/dist/pixi.js.map +1 -0
- package/dist/vizij.cjs +1556 -0
- package/dist/vizij.cjs.map +1 -0
- package/dist/vizij.d.ts +2 -0
- package/dist/vizij.js +193 -0
- package/dist/vizij.js.map +1 -0
- package/package.json +116 -4
- package/public/FaceAssets/Hugo/cheek.png +0 -0
- package/public/FaceAssets/Hugo/concerned_l.png +0 -0
- package/public/FaceAssets/Hugo/concerned_r.png +0 -0
- package/public/FaceAssets/Hugo/happy_l.png +0 -0
- package/public/FaceAssets/Hugo/happy_r.png +0 -0
- package/public/FaceAssets/Hugo/hugo_spritesheet.json +97 -0
- package/public/FaceAssets/Hugo/hugo_spritesheet.png +0 -0
- package/public/FaceAssets/Hugo/neutral_l.png +0 -0
- package/public/FaceAssets/Hugo/neutral_r.png +0 -0
- package/public/FaceAssets/Hugo/sad_l.png +0 -0
- package/public/FaceAssets/Hugo/sad_r.png +0 -0
- package/public/FaceAssets/Hugo/sleepy_l.png +0 -0
- package/public/FaceAssets/Hugo/sleepy_r.png +0 -0
- package/public/FaceAssets/Hugo/surprised_l.png +0 -0
- package/public/FaceAssets/Hugo/surprised_r.png +0 -0
- package/public/FaceAssets/Icons/Concerned.png +0 -0
- package/public/FaceAssets/Icons/Neutral.png +0 -0
- package/public/FaceAssets/Icons/Sad.png +0 -0
- package/public/FaceAssets/Icons/Sleep.png +0 -0
- package/public/FaceAssets/Icons/Surprised.png +0 -0
- package/public/FaceAssets/Misty/misty_spritesheet.json +97 -0
- package/public/FaceAssets/Misty/misty_spritesheet.png +0 -0
- package/public/FaceAssets/Robotic/robotic_spritesheet.json +97 -0
- package/public/FaceAssets/Robotic/robotic_spritesheet.png +0 -0
- package/public/FaceAssets/Vizij/Hugo.glb +0 -0
- package/public/FaceAssets/Vizij/brand.svg +15 -0
package/dist/pixi.cjs
ADDED
|
@@ -0,0 +1,2421 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/components/PixiFaceView.tsx
|
|
21
|
+
var PixiFaceView_exports = {};
|
|
22
|
+
__export(PixiFaceView_exports, {
|
|
23
|
+
FaceBackground: () => FaceBackground,
|
|
24
|
+
FaceContainer: () => FaceContainer,
|
|
25
|
+
FaceEyes: () => FaceEyes,
|
|
26
|
+
FaceFullScreenButton: () => FaceFullScreenButton,
|
|
27
|
+
FaceMouth: () => FaceMouth,
|
|
28
|
+
FaceScrollToTopButton: () => FaceScrollToTopButton,
|
|
29
|
+
FaceText: () => FaceText,
|
|
30
|
+
PixiFaceView: () => PixiFaceView,
|
|
31
|
+
default: () => PixiFaceView_default
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(PixiFaceView_exports);
|
|
34
|
+
var import_react17 = require("react");
|
|
35
|
+
var import_react18 = require("@pixi/react");
|
|
36
|
+
var import_pixi8 = require("pixi.js");
|
|
37
|
+
|
|
38
|
+
// src/store/FaceStoreContext.tsx
|
|
39
|
+
var import_react = require("react");
|
|
40
|
+
var import_zustand2 = require("zustand");
|
|
41
|
+
|
|
42
|
+
// src/store/createPeerbotsFaceStore.ts
|
|
43
|
+
var import_zustand = require("zustand");
|
|
44
|
+
var import_middleware = require("zustand/middleware");
|
|
45
|
+
|
|
46
|
+
// src/types/face.ts
|
|
47
|
+
var DEFAULT_FACE_MESSAGE = {
|
|
48
|
+
title: "Ready",
|
|
49
|
+
speech: "",
|
|
50
|
+
color: "Light Blue",
|
|
51
|
+
emotion: "Neutral"
|
|
52
|
+
};
|
|
53
|
+
var defaultFaceSettings = {
|
|
54
|
+
zoom: 1,
|
|
55
|
+
offset: { x: 0, y: 0 },
|
|
56
|
+
rotation: 0,
|
|
57
|
+
autoRotate: true,
|
|
58
|
+
showFullScreenButton: true,
|
|
59
|
+
showScrollButton: true,
|
|
60
|
+
amplitudeGain: 1,
|
|
61
|
+
vizijSettings: {
|
|
62
|
+
faceExpressivity: 1
|
|
63
|
+
},
|
|
64
|
+
pixiSettings: {
|
|
65
|
+
eyeDisplay: "default",
|
|
66
|
+
mouthDisplay: "default",
|
|
67
|
+
cheekDisplay: "default",
|
|
68
|
+
emotionDisplay: "emoticon"
|
|
69
|
+
},
|
|
70
|
+
selectedLanguage: "English",
|
|
71
|
+
speechQueueMode: "interrupt",
|
|
72
|
+
captions: {
|
|
73
|
+
enabled: false,
|
|
74
|
+
fontSize: 32,
|
|
75
|
+
offset: { x: 0, y: 0 }
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
function normalizeAssetBaseUrl(assetBaseUrl = "/FaceAssets") {
|
|
79
|
+
const trimmed = assetBaseUrl.trim();
|
|
80
|
+
if (!trimmed) {
|
|
81
|
+
return "/FaceAssets";
|
|
82
|
+
}
|
|
83
|
+
return trimmed.endsWith("/") ? trimmed.slice(0, -1) : trimmed;
|
|
84
|
+
}
|
|
85
|
+
function createBuiltInPixiFaces(assetBaseUrl) {
|
|
86
|
+
const base = normalizeAssetBaseUrl(assetBaseUrl);
|
|
87
|
+
return [
|
|
88
|
+
{
|
|
89
|
+
kind: "PixiFace",
|
|
90
|
+
type: "avatar",
|
|
91
|
+
name: "Hugo",
|
|
92
|
+
mouth: "Hugo",
|
|
93
|
+
eyes: {
|
|
94
|
+
kind: "spritesheet",
|
|
95
|
+
path: `${base}/Hugo/hugo_spritesheet.json`
|
|
96
|
+
},
|
|
97
|
+
cheeks: {
|
|
98
|
+
kind: "texture",
|
|
99
|
+
path: `${base}/Hugo/cheek.png`
|
|
100
|
+
},
|
|
101
|
+
eyebrows: "Hugo"
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
kind: "PixiFace",
|
|
105
|
+
type: "avatar",
|
|
106
|
+
name: "Simple",
|
|
107
|
+
mouth: "Ellipse",
|
|
108
|
+
eyes: {
|
|
109
|
+
kind: "spritesheet",
|
|
110
|
+
path: `${base}/Hugo/hugo_spritesheet.json`
|
|
111
|
+
},
|
|
112
|
+
cheeks: null,
|
|
113
|
+
eyebrows: null
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
kind: "PixiFace",
|
|
117
|
+
type: "avatar",
|
|
118
|
+
name: "Robotic",
|
|
119
|
+
mouth: "ZigZag",
|
|
120
|
+
eyes: {
|
|
121
|
+
kind: "spritesheet",
|
|
122
|
+
path: `${base}/Robotic/robotic_spritesheet.json`
|
|
123
|
+
},
|
|
124
|
+
cheeks: null,
|
|
125
|
+
eyebrows: null
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
kind: "PixiFace",
|
|
129
|
+
type: "text",
|
|
130
|
+
name: "Display",
|
|
131
|
+
showEmotion: "emoticon",
|
|
132
|
+
showText: true
|
|
133
|
+
}
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
function createCustomVizijFace(glbUrl = "") {
|
|
137
|
+
return {
|
|
138
|
+
kind: "Vizij",
|
|
139
|
+
name: "Custom Face",
|
|
140
|
+
assetBundle: {
|
|
141
|
+
namespace: "custom",
|
|
142
|
+
glb: {
|
|
143
|
+
kind: "url",
|
|
144
|
+
src: glbUrl,
|
|
145
|
+
aggressiveImport: true
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function createIncludedVizijFaces(assetBaseUrl) {
|
|
151
|
+
const base = normalizeAssetBaseUrl(assetBaseUrl);
|
|
152
|
+
return [
|
|
153
|
+
{
|
|
154
|
+
kind: "Vizij",
|
|
155
|
+
name: "Hugo - Expressive",
|
|
156
|
+
assetBundle: {
|
|
157
|
+
namespace: "hugo",
|
|
158
|
+
glb: {
|
|
159
|
+
kind: "url",
|
|
160
|
+
src: `${base}/Vizij/Hugo.glb`,
|
|
161
|
+
aggressiveImport: true
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
createCustomVizijFace("")
|
|
166
|
+
];
|
|
167
|
+
}
|
|
168
|
+
function createBuiltInFaces(assetBaseUrl) {
|
|
169
|
+
return [
|
|
170
|
+
...createBuiltInPixiFaces(assetBaseUrl),
|
|
171
|
+
...createIncludedVizijFaces(assetBaseUrl)
|
|
172
|
+
];
|
|
173
|
+
}
|
|
174
|
+
var builtInFaces = createBuiltInPixiFaces();
|
|
175
|
+
var customFace = createCustomVizijFace();
|
|
176
|
+
var includedVizijFaces = createIncludedVizijFaces();
|
|
177
|
+
var availableFaces = [...builtInFaces, ...includedVizijFaces];
|
|
178
|
+
var defaultFace = builtInFaces[0];
|
|
179
|
+
|
|
180
|
+
// src/store/faceRenderingSlice.ts
|
|
181
|
+
function clampUnit(value) {
|
|
182
|
+
if (Number.isNaN(value)) {
|
|
183
|
+
return 0;
|
|
184
|
+
}
|
|
185
|
+
return Math.max(-1, Math.min(1, value));
|
|
186
|
+
}
|
|
187
|
+
function mergeFaceSettings(base, patch) {
|
|
188
|
+
if (!patch) {
|
|
189
|
+
return {
|
|
190
|
+
...base,
|
|
191
|
+
offset: { ...base.offset },
|
|
192
|
+
captions: {
|
|
193
|
+
...base.captions,
|
|
194
|
+
offset: { ...base.captions.offset }
|
|
195
|
+
},
|
|
196
|
+
pixiSettings: base.pixiSettings ? { ...base.pixiSettings } : void 0,
|
|
197
|
+
vizijSettings: base.vizijSettings ? { ...base.vizijSettings } : void 0
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
...defaultFaceSettings,
|
|
202
|
+
...base,
|
|
203
|
+
...patch,
|
|
204
|
+
offset: {
|
|
205
|
+
...defaultFaceSettings.offset,
|
|
206
|
+
...base.offset || {},
|
|
207
|
+
...patch.offset || {}
|
|
208
|
+
},
|
|
209
|
+
captions: {
|
|
210
|
+
...defaultFaceSettings.captions,
|
|
211
|
+
...base.captions || {},
|
|
212
|
+
...patch.captions || {},
|
|
213
|
+
offset: {
|
|
214
|
+
...defaultFaceSettings.captions.offset,
|
|
215
|
+
...base.captions?.offset || {},
|
|
216
|
+
...patch.captions?.offset || {}
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
pixiSettings: {
|
|
220
|
+
...defaultFaceSettings.pixiSettings,
|
|
221
|
+
...base.pixiSettings || {},
|
|
222
|
+
...patch.pixiSettings || {}
|
|
223
|
+
},
|
|
224
|
+
vizijSettings: {
|
|
225
|
+
...defaultFaceSettings.vizijSettings,
|
|
226
|
+
...base.vizijSettings || {},
|
|
227
|
+
...patch.vizijSettings || {}
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function createFaceRenderingSlice(options = {}) {
|
|
232
|
+
const initialBaseUrl = normalizeAssetBaseUrl(options.assetBaseUrl);
|
|
233
|
+
const initialFaces = createBuiltInFaces(initialBaseUrl);
|
|
234
|
+
const initialSelectedFace = options.initialFace ?? initialFaces[0];
|
|
235
|
+
const initialMergedSettings = mergeFaceSettings(
|
|
236
|
+
defaultFaceSettings,
|
|
237
|
+
options.initialSettings
|
|
238
|
+
);
|
|
239
|
+
return (set, get) => ({
|
|
240
|
+
assetBaseUrl: initialBaseUrl,
|
|
241
|
+
availableFaces: initialFaces,
|
|
242
|
+
selectedFace: initialSelectedFace,
|
|
243
|
+
faceSettings: initialMergedSettings,
|
|
244
|
+
gaze: { x: 0, y: 0 },
|
|
245
|
+
setFace: (face) => {
|
|
246
|
+
set({ selectedFace: face });
|
|
247
|
+
},
|
|
248
|
+
setFaceByName: (name) => {
|
|
249
|
+
const found = get().availableFaces.find((f) => f.name === name);
|
|
250
|
+
if (found) {
|
|
251
|
+
set({ selectedFace: found });
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
setFaceSettings: (settings) => {
|
|
255
|
+
set((state) => ({
|
|
256
|
+
faceSettings: mergeFaceSettings(state.faceSettings, settings)
|
|
257
|
+
}));
|
|
258
|
+
},
|
|
259
|
+
resetPositioning: () => {
|
|
260
|
+
set((state) => ({
|
|
261
|
+
faceSettings: mergeFaceSettings(state.faceSettings, {
|
|
262
|
+
zoom: 1,
|
|
263
|
+
offset: { x: 0, y: 0 },
|
|
264
|
+
rotation: 0
|
|
265
|
+
})
|
|
266
|
+
}));
|
|
267
|
+
},
|
|
268
|
+
setGaze: (gaze) => {
|
|
269
|
+
set({
|
|
270
|
+
gaze: {
|
|
271
|
+
x: clampUnit(gaze.x),
|
|
272
|
+
y: clampUnit(gaze.y)
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
},
|
|
276
|
+
setLanguage: (lang) => {
|
|
277
|
+
set((state) => ({
|
|
278
|
+
faceSettings: mergeFaceSettings(state.faceSettings, {
|
|
279
|
+
selectedLanguage: lang
|
|
280
|
+
})
|
|
281
|
+
}));
|
|
282
|
+
},
|
|
283
|
+
setAssetBaseUrl: (assetBaseUrl) => {
|
|
284
|
+
const normalized = normalizeAssetBaseUrl(assetBaseUrl);
|
|
285
|
+
const updatedFaces = createBuiltInFaces(normalized);
|
|
286
|
+
const currentName = get().selectedFace?.name;
|
|
287
|
+
const matchingUpdated = updatedFaces.find((f) => f.name === currentName);
|
|
288
|
+
set({
|
|
289
|
+
assetBaseUrl: normalized,
|
|
290
|
+
availableFaces: updatedFaces,
|
|
291
|
+
selectedFace: matchingUpdated ?? get().selectedFace
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// src/helpers/voice_helpers.ts
|
|
298
|
+
var languageDisplayNames = typeof Intl !== "undefined" && typeof Intl.DisplayNames === "function" ? new Intl.DisplayNames(["en"], { type: "language" }) : null;
|
|
299
|
+
function getLanguageName(code) {
|
|
300
|
+
if (!code) {
|
|
301
|
+
return "";
|
|
302
|
+
}
|
|
303
|
+
try {
|
|
304
|
+
const baseCode = code.split("-")[0];
|
|
305
|
+
if (!baseCode || !languageDisplayNames) {
|
|
306
|
+
return code;
|
|
307
|
+
}
|
|
308
|
+
return languageDisplayNames.of(baseCode) || code;
|
|
309
|
+
} catch (err) {
|
|
310
|
+
void err;
|
|
311
|
+
return code;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
function formatVoiceDisplayName(rawName) {
|
|
315
|
+
if (!rawName) return "";
|
|
316
|
+
let name = rawName.replace(/^AWS - /, "").trim();
|
|
317
|
+
if (name.startsWith("aws:")) {
|
|
318
|
+
const parts = name.split(":");
|
|
319
|
+
name = parts[1] || name.slice(4);
|
|
320
|
+
}
|
|
321
|
+
if (!name.includes("com.apple.") && !name.includes("com.google.")) {
|
|
322
|
+
return name;
|
|
323
|
+
}
|
|
324
|
+
if (name.includes("com.google.")) {
|
|
325
|
+
const afterColon = name.split(":").pop() || name;
|
|
326
|
+
return afterColon.replace(/[-_]/g, " ").trim();
|
|
327
|
+
}
|
|
328
|
+
const siriMatch = name.match(
|
|
329
|
+
/siri_([a-zA-Z]+)_[a-zA-Z0-9-_]+?(?:_(compact|premium|enhanced))?$/i
|
|
330
|
+
);
|
|
331
|
+
if (siriMatch) {
|
|
332
|
+
const gender = siriMatch[1].charAt(0).toUpperCase() + siriMatch[1].slice(1).toLowerCase();
|
|
333
|
+
const quality = siriMatch[2] && siriMatch[2].toLowerCase() !== "compact" ? ` (${siriMatch[2].charAt(0).toUpperCase() + siriMatch[2].slice(1).toLowerCase()})` : "";
|
|
334
|
+
return `Siri ${gender}${quality}`;
|
|
335
|
+
}
|
|
336
|
+
const ttsMatch = name.match(
|
|
337
|
+
/com\.apple\.ttsbundle\.([a-zA-Z0-9]+)(?:-(compact|premium|enhanced))?/i
|
|
338
|
+
);
|
|
339
|
+
if (ttsMatch) {
|
|
340
|
+
const base = ttsMatch[1].replace(/([a-z])([A-Z])/g, "$1 $2");
|
|
341
|
+
const quality = ttsMatch[2] && ttsMatch[2].toLowerCase() !== "compact" ? ` (${ttsMatch[2].charAt(0).toUpperCase() + ttsMatch[2].slice(1).toLowerCase()})` : "";
|
|
342
|
+
return `${base}${quality}`;
|
|
343
|
+
}
|
|
344
|
+
const speechMatch = name.match(
|
|
345
|
+
/com\.apple\.speech\.synthesis\.voice\.([a-zA-Z0-9_-]+)/i
|
|
346
|
+
);
|
|
347
|
+
if (speechMatch) {
|
|
348
|
+
return speechMatch[1].replace(/([a-z])([A-Z])/g, "$1 $2");
|
|
349
|
+
}
|
|
350
|
+
const tierMatch = name.match(
|
|
351
|
+
/com\.apple\.voice\.(compact|enhanced|premium)\.[^.]+\.([a-zA-Z0-9_-]+)/i
|
|
352
|
+
);
|
|
353
|
+
if (tierMatch) {
|
|
354
|
+
const base = tierMatch[2].replace(/([a-z])([A-Z])/g, "$1 $2");
|
|
355
|
+
const quality = tierMatch[1].toLowerCase() !== "compact" ? ` (${tierMatch[1].charAt(0).toUpperCase() + tierMatch[1].slice(1).toLowerCase()})` : "";
|
|
356
|
+
return `${base}${quality}`;
|
|
357
|
+
}
|
|
358
|
+
const eloquenceMatch = name.match(
|
|
359
|
+
/com\.apple\.eloquence\.[^.]+\.([a-zA-Z0-9_-]+)/i
|
|
360
|
+
);
|
|
361
|
+
if (eloquenceMatch) {
|
|
362
|
+
return eloquenceMatch[1].replace(/([a-z])([A-Z])/g, "$1 $2");
|
|
363
|
+
}
|
|
364
|
+
const segments = name.split(".");
|
|
365
|
+
let last = segments[segments.length - 1] || name;
|
|
366
|
+
last = last.replace(/[-_](compact|premium|enhanced)$/i, "");
|
|
367
|
+
last = last.replace(/([a-z])([A-Z])/g, "$1 $2");
|
|
368
|
+
last = last.replace(/[-_]/g, " ").trim();
|
|
369
|
+
return last || name;
|
|
370
|
+
}
|
|
371
|
+
function findMatchingVoice(voices, target) {
|
|
372
|
+
if (!target) return void 0;
|
|
373
|
+
const directUri = voices.find((v) => v.voiceURI === target);
|
|
374
|
+
if (directUri) return directUri;
|
|
375
|
+
const directName = voices.find((v) => v.name === target);
|
|
376
|
+
if (directName) return directName;
|
|
377
|
+
const targetClean = formatVoiceDisplayName(target).toLowerCase();
|
|
378
|
+
const cleanMatch = voices.find(
|
|
379
|
+
(v) => formatVoiceDisplayName(v.name).toLowerCase() === targetClean || formatVoiceDisplayName(v.voiceURI).toLowerCase() === targetClean
|
|
380
|
+
);
|
|
381
|
+
if (cleanMatch) return cleanMatch;
|
|
382
|
+
const lowerTarget = target.toLowerCase();
|
|
383
|
+
return voices.find(
|
|
384
|
+
(v) => v.voiceURI.toLowerCase() === lowerTarget || v.name.toLowerCase() === lowerTarget
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// src/helpers/viseme_mapper.ts
|
|
389
|
+
var pollyVisemeToVizijPath = {
|
|
390
|
+
"@": "pose_pzzzfnvy",
|
|
391
|
+
a: "a",
|
|
392
|
+
e: "e_2",
|
|
393
|
+
E: "e",
|
|
394
|
+
f: "f",
|
|
395
|
+
i: "i",
|
|
396
|
+
k: "k",
|
|
397
|
+
l: "r",
|
|
398
|
+
o: "o",
|
|
399
|
+
O: "o_2",
|
|
400
|
+
p: "p",
|
|
401
|
+
r: "r",
|
|
402
|
+
s: "s",
|
|
403
|
+
S: "s",
|
|
404
|
+
t: "t",
|
|
405
|
+
T: "t_2",
|
|
406
|
+
u: "u",
|
|
407
|
+
sil: ""
|
|
408
|
+
};
|
|
409
|
+
var possiblePollyVisemes = Array.from(
|
|
410
|
+
new Set(Object.values(pollyVisemeToVizijPath).filter((v) => v.length > 0))
|
|
411
|
+
);
|
|
412
|
+
var CHAR_TO_POLLY_VISEME = {
|
|
413
|
+
a: "a",
|
|
414
|
+
b: "p",
|
|
415
|
+
c: "k",
|
|
416
|
+
d: "t",
|
|
417
|
+
e: "e",
|
|
418
|
+
f: "f",
|
|
419
|
+
g: "k",
|
|
420
|
+
h: "@",
|
|
421
|
+
i: "i",
|
|
422
|
+
j: "S",
|
|
423
|
+
k: "k",
|
|
424
|
+
l: "l",
|
|
425
|
+
m: "p",
|
|
426
|
+
n: "t",
|
|
427
|
+
o: "o",
|
|
428
|
+
p: "p",
|
|
429
|
+
q: "k",
|
|
430
|
+
r: "r",
|
|
431
|
+
s: "s",
|
|
432
|
+
t: "t",
|
|
433
|
+
u: "u",
|
|
434
|
+
v: "f",
|
|
435
|
+
w: "u",
|
|
436
|
+
x: "s",
|
|
437
|
+
y: "i",
|
|
438
|
+
z: "s"
|
|
439
|
+
};
|
|
440
|
+
function estimateVisemesFromText(text, msPerChar = 65) {
|
|
441
|
+
const cleaned = text.trim();
|
|
442
|
+
if (!cleaned) {
|
|
443
|
+
return [];
|
|
444
|
+
}
|
|
445
|
+
const visemes = [];
|
|
446
|
+
const durationSec = Math.max(0.03, msPerChar / 1e3);
|
|
447
|
+
for (const ch of cleaned.toLowerCase()) {
|
|
448
|
+
const mapped = CHAR_TO_POLLY_VISEME[ch] ?? "sil";
|
|
449
|
+
const prev = visemes[visemes.length - 1];
|
|
450
|
+
if (prev && prev.value === mapped) {
|
|
451
|
+
prev.time = Number((prev.time + durationSec).toFixed(4));
|
|
452
|
+
} else {
|
|
453
|
+
visemes.push({
|
|
454
|
+
time: Number(durationSec.toFixed(4)),
|
|
455
|
+
type: "viseme",
|
|
456
|
+
value: mapped
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
if (visemes[visemes.length - 1]?.value !== "sil") {
|
|
461
|
+
visemes.push({
|
|
462
|
+
time: 0.1,
|
|
463
|
+
type: "viseme",
|
|
464
|
+
value: "sil"
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
return visemes;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// src/services/AudioAnalyzer.ts
|
|
471
|
+
var AudioAnalyzer = class {
|
|
472
|
+
ctx = null;
|
|
473
|
+
analyzer = null;
|
|
474
|
+
data = null;
|
|
475
|
+
connectedElements = /* @__PURE__ */ new WeakSet();
|
|
476
|
+
connect(audio) {
|
|
477
|
+
if (typeof window === "undefined") {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const AudioContextCtor = window.AudioContext || window.webkitAudioContext;
|
|
481
|
+
if (!AudioContextCtor) {
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
try {
|
|
485
|
+
if (!this.ctx) {
|
|
486
|
+
this.ctx = new AudioContextCtor();
|
|
487
|
+
this.analyzer = this.ctx.createAnalyser();
|
|
488
|
+
this.analyzer.fftSize = 256;
|
|
489
|
+
this.data = new Uint8Array(new ArrayBuffer(this.analyzer.frequencyBinCount));
|
|
490
|
+
}
|
|
491
|
+
if (!this.connectedElements.has(audio) && this.analyzer) {
|
|
492
|
+
const source = this.ctx.createMediaElementSource(audio);
|
|
493
|
+
source.connect(this.analyzer);
|
|
494
|
+
this.analyzer.connect(this.ctx.destination);
|
|
495
|
+
this.connectedElements.add(audio);
|
|
496
|
+
}
|
|
497
|
+
} catch (error) {
|
|
498
|
+
console.warn("AudioAnalyzer connect error:", error);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
getAmplitude() {
|
|
502
|
+
if (!this.analyzer || !this.data) {
|
|
503
|
+
return 0;
|
|
504
|
+
}
|
|
505
|
+
this.analyzer.getByteTimeDomainData(this.data);
|
|
506
|
+
let sum = 0;
|
|
507
|
+
for (const v of this.data) {
|
|
508
|
+
const normalized = (v - 128) / 128;
|
|
509
|
+
sum += normalized * normalized;
|
|
510
|
+
}
|
|
511
|
+
return Math.sqrt(sum / this.data.length);
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
var audioAnalyzer = new AudioAnalyzer();
|
|
515
|
+
|
|
516
|
+
// src/services/ttsAudioCache.ts
|
|
517
|
+
function buildTtsCacheKey(params) {
|
|
518
|
+
const voice = params.voiceId.trim();
|
|
519
|
+
const engine = (params.engine ?? "default").trim() || "default";
|
|
520
|
+
const normalizedText = params.text.trim();
|
|
521
|
+
if (params.rate === void 0 && params.pitch === void 0) {
|
|
522
|
+
return `${voice}:${engine}:${normalizedText}`;
|
|
523
|
+
}
|
|
524
|
+
const rate = params.rate ?? 1;
|
|
525
|
+
const pitch = params.pitch ?? 1;
|
|
526
|
+
return `${voice}:${engine}:${rate}:${pitch}:${normalizedText}`;
|
|
527
|
+
}
|
|
528
|
+
async function blobToArrayBuffer(blob) {
|
|
529
|
+
if (typeof blob.arrayBuffer === "function") {
|
|
530
|
+
return blob.arrayBuffer();
|
|
531
|
+
}
|
|
532
|
+
return new Promise((resolve, reject) => {
|
|
533
|
+
const reader = new FileReader();
|
|
534
|
+
reader.onload = () => {
|
|
535
|
+
if (reader.result instanceof ArrayBuffer) {
|
|
536
|
+
resolve(reader.result);
|
|
537
|
+
} else {
|
|
538
|
+
reject(new Error("Failed to read Blob as ArrayBuffer"));
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
reader.onerror = () => {
|
|
542
|
+
reject(reader.error ?? new Error("FileReader error"));
|
|
543
|
+
};
|
|
544
|
+
reader.readAsArrayBuffer(blob);
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
var TtsAudioCache = class {
|
|
548
|
+
maxEntries;
|
|
549
|
+
enableIndexedDb;
|
|
550
|
+
dbName;
|
|
551
|
+
storeName;
|
|
552
|
+
memory = /* @__PURE__ */ new Map();
|
|
553
|
+
dbPromise = null;
|
|
554
|
+
constructor(options = {}) {
|
|
555
|
+
this.maxEntries = Math.max(1, options.maxEntries ?? 100);
|
|
556
|
+
this.enableIndexedDb = options.enableIndexedDb ?? true;
|
|
557
|
+
this.dbName = options.dbName ?? "peerbots-tts-cache";
|
|
558
|
+
this.storeName = options.storeName ?? "entries";
|
|
559
|
+
}
|
|
560
|
+
get size() {
|
|
561
|
+
return this.memory.size;
|
|
562
|
+
}
|
|
563
|
+
hasInMemory(key) {
|
|
564
|
+
return this.memory.has(key);
|
|
565
|
+
}
|
|
566
|
+
async get(key) {
|
|
567
|
+
const existing = this.memory.get(key);
|
|
568
|
+
if (existing) {
|
|
569
|
+
this.memory.delete(key);
|
|
570
|
+
this.memory.set(key, existing);
|
|
571
|
+
return existing;
|
|
572
|
+
}
|
|
573
|
+
const fromIdb = await this.readFromIndexedDb(key);
|
|
574
|
+
if (fromIdb) {
|
|
575
|
+
this.putInMemory(key, fromIdb);
|
|
576
|
+
return fromIdb;
|
|
577
|
+
}
|
|
578
|
+
return void 0;
|
|
579
|
+
}
|
|
580
|
+
async set(key, entry) {
|
|
581
|
+
const fullEntry = {
|
|
582
|
+
...entry,
|
|
583
|
+
createdAt: entry.createdAt ?? Date.now()
|
|
584
|
+
};
|
|
585
|
+
this.putInMemory(key, fullEntry);
|
|
586
|
+
await this.writeToIndexedDb(key, fullEntry);
|
|
587
|
+
}
|
|
588
|
+
async delete(key) {
|
|
589
|
+
const removedMem = this.memory.delete(key);
|
|
590
|
+
const removedIdb = await this.deleteFromIndexedDb(key);
|
|
591
|
+
return removedMem || removedIdb;
|
|
592
|
+
}
|
|
593
|
+
async clear() {
|
|
594
|
+
this.memory.clear();
|
|
595
|
+
await this.clearIndexedDb();
|
|
596
|
+
}
|
|
597
|
+
createObjectUrl(entry) {
|
|
598
|
+
return URL.createObjectURL(entry.audioBlob);
|
|
599
|
+
}
|
|
600
|
+
putInMemory(key, entry) {
|
|
601
|
+
if (this.memory.has(key)) {
|
|
602
|
+
this.memory.delete(key);
|
|
603
|
+
}
|
|
604
|
+
this.memory.set(key, entry);
|
|
605
|
+
while (this.memory.size > this.maxEntries) {
|
|
606
|
+
const oldestKey = this.memory.keys().next().value;
|
|
607
|
+
if (oldestKey !== void 0) {
|
|
608
|
+
this.memory.delete(oldestKey);
|
|
609
|
+
} else {
|
|
610
|
+
break;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
openDatabase() {
|
|
615
|
+
if (!this.enableIndexedDb || typeof indexedDB === "undefined") {
|
|
616
|
+
return Promise.resolve(null);
|
|
617
|
+
}
|
|
618
|
+
if (this.dbPromise) {
|
|
619
|
+
return this.dbPromise;
|
|
620
|
+
}
|
|
621
|
+
this.dbPromise = new Promise((resolve) => {
|
|
622
|
+
try {
|
|
623
|
+
const request = indexedDB.open(this.dbName, 1);
|
|
624
|
+
request.onupgradeneeded = () => {
|
|
625
|
+
const db = request.result;
|
|
626
|
+
if (!db.objectStoreNames.contains(this.storeName)) {
|
|
627
|
+
db.createObjectStore(this.storeName, { keyPath: "key" });
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
request.onsuccess = () => resolve(request.result);
|
|
631
|
+
request.onerror = () => resolve(null);
|
|
632
|
+
} catch (err) {
|
|
633
|
+
void err;
|
|
634
|
+
resolve(null);
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
return this.dbPromise;
|
|
638
|
+
}
|
|
639
|
+
async readFromIndexedDb(key) {
|
|
640
|
+
const db = await this.openDatabase();
|
|
641
|
+
if (!db) {
|
|
642
|
+
return void 0;
|
|
643
|
+
}
|
|
644
|
+
return new Promise((resolve) => {
|
|
645
|
+
try {
|
|
646
|
+
const tx = db.transaction(this.storeName, "readonly");
|
|
647
|
+
const store = tx.objectStore(this.storeName);
|
|
648
|
+
const req = store.get(key);
|
|
649
|
+
req.onsuccess = () => {
|
|
650
|
+
const record = req.result;
|
|
651
|
+
if (!record || !record.audioBuffer) {
|
|
652
|
+
resolve(void 0);
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
resolve({
|
|
656
|
+
audioBlob: new Blob([record.audioBuffer], {
|
|
657
|
+
type: record.mimeType || "audio/mpeg"
|
|
658
|
+
}),
|
|
659
|
+
mimeType: record.mimeType || "audio/mpeg",
|
|
660
|
+
visemes: record.visemes,
|
|
661
|
+
amplitudeData: record.amplitudeData,
|
|
662
|
+
extrema: record.extrema,
|
|
663
|
+
createdAt: record.createdAt
|
|
664
|
+
});
|
|
665
|
+
};
|
|
666
|
+
req.onerror = () => resolve(void 0);
|
|
667
|
+
} catch (err) {
|
|
668
|
+
void err;
|
|
669
|
+
resolve(void 0);
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
async writeToIndexedDb(key, entry) {
|
|
674
|
+
const db = await this.openDatabase();
|
|
675
|
+
if (!db) {
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
try {
|
|
679
|
+
const audioBuffer = await blobToArrayBuffer(entry.audioBlob);
|
|
680
|
+
await new Promise((resolve) => {
|
|
681
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
682
|
+
const store = tx.objectStore(this.storeName);
|
|
683
|
+
const record = {
|
|
684
|
+
key,
|
|
685
|
+
audioBuffer,
|
|
686
|
+
mimeType: entry.mimeType,
|
|
687
|
+
visemes: entry.visemes,
|
|
688
|
+
amplitudeData: entry.amplitudeData,
|
|
689
|
+
extrema: entry.extrema,
|
|
690
|
+
createdAt: entry.createdAt
|
|
691
|
+
};
|
|
692
|
+
const req = store.put(record);
|
|
693
|
+
req.onsuccess = () => resolve();
|
|
694
|
+
req.onerror = () => resolve();
|
|
695
|
+
});
|
|
696
|
+
} catch (err) {
|
|
697
|
+
void err;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
async deleteFromIndexedDb(key) {
|
|
701
|
+
const db = await this.openDatabase();
|
|
702
|
+
if (!db) {
|
|
703
|
+
return false;
|
|
704
|
+
}
|
|
705
|
+
return new Promise((resolve) => {
|
|
706
|
+
try {
|
|
707
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
708
|
+
const store = tx.objectStore(this.storeName);
|
|
709
|
+
const req = store.delete(key);
|
|
710
|
+
req.onsuccess = () => resolve(true);
|
|
711
|
+
req.onerror = () => resolve(false);
|
|
712
|
+
} catch (err) {
|
|
713
|
+
void err;
|
|
714
|
+
resolve(false);
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
async clearIndexedDb() {
|
|
719
|
+
const db = await this.openDatabase();
|
|
720
|
+
if (!db) {
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
await new Promise((resolve) => {
|
|
724
|
+
try {
|
|
725
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
726
|
+
const store = tx.objectStore(this.storeName);
|
|
727
|
+
const req = store.clear();
|
|
728
|
+
req.onsuccess = () => resolve();
|
|
729
|
+
req.onerror = () => resolve();
|
|
730
|
+
} catch (err) {
|
|
731
|
+
void err;
|
|
732
|
+
resolve();
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
var defaultTtsAudioCache = new TtsAudioCache();
|
|
738
|
+
|
|
739
|
+
// src/store/faceSpeechSlice.ts
|
|
740
|
+
var FALLBACK_DEFAULT_VOICE = {
|
|
741
|
+
default: false,
|
|
742
|
+
lang: "en-US",
|
|
743
|
+
localService: false,
|
|
744
|
+
name: "en-US-Standard-B",
|
|
745
|
+
voiceURI: "en-US-Standard-B"
|
|
746
|
+
};
|
|
747
|
+
function isNativeSpeechSynthesisVoice(voice) {
|
|
748
|
+
if (!voice || typeof voice !== "object") {
|
|
749
|
+
return false;
|
|
750
|
+
}
|
|
751
|
+
if (typeof SpeechSynthesisVoice !== "undefined" && voice instanceof SpeechSynthesisVoice) {
|
|
752
|
+
return true;
|
|
753
|
+
}
|
|
754
|
+
return Object.prototype.toString.call(voice) === "[object SpeechSynthesisVoice]";
|
|
755
|
+
}
|
|
756
|
+
function matchesLanguage(voiceLang, selectedLang) {
|
|
757
|
+
if (!voiceLang || !selectedLang) {
|
|
758
|
+
return false;
|
|
759
|
+
}
|
|
760
|
+
return getLanguageName(voiceLang) === selectedLang || voiceLang === selectedLang || voiceLang.toLowerCase() === selectedLang.toLowerCase() || voiceLang.toLowerCase().startsWith(selectedLang.toLowerCase()) || selectedLang.toLowerCase().startsWith(voiceLang.toLowerCase());
|
|
761
|
+
}
|
|
762
|
+
function stopNativeSpeechSynthesis() {
|
|
763
|
+
if (typeof window !== "undefined" && "speechSynthesis" in window) {
|
|
764
|
+
try {
|
|
765
|
+
if (window.speechSynthesis.speaking || window.speechSynthesis.pending) {
|
|
766
|
+
window.speechSynthesis.cancel();
|
|
767
|
+
}
|
|
768
|
+
} catch (err) {
|
|
769
|
+
void err;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
function createFaceSpeechSlice(sliceOptions = {}) {
|
|
774
|
+
let adapter = sliceOptions.speechAdapter ?? null;
|
|
775
|
+
const cache = sliceOptions.ttsCache === false ? null : sliceOptions.ttsCache ?? defaultTtsAudioCache;
|
|
776
|
+
const analyzer = sliceOptions.audioAnalyzer ?? audioAnalyzer;
|
|
777
|
+
const utteranceDeferMs = sliceOptions.utteranceDeferMs ?? 10;
|
|
778
|
+
let activeAudio = null;
|
|
779
|
+
let activeObjectUrl = null;
|
|
780
|
+
let activeUtterance = null;
|
|
781
|
+
let activeTimer = null;
|
|
782
|
+
let activeBoundaryTimer = null;
|
|
783
|
+
let activeGeneration = 0;
|
|
784
|
+
const revokeActiveObjectUrl = () => {
|
|
785
|
+
if (activeObjectUrl) {
|
|
786
|
+
try {
|
|
787
|
+
URL.revokeObjectURL(activeObjectUrl);
|
|
788
|
+
} catch (err) {
|
|
789
|
+
void err;
|
|
790
|
+
}
|
|
791
|
+
activeObjectUrl = null;
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
const cleanupActivePlayback = () => {
|
|
795
|
+
activeGeneration += 1;
|
|
796
|
+
if (activeTimer !== null) {
|
|
797
|
+
clearTimeout(activeTimer);
|
|
798
|
+
activeTimer = null;
|
|
799
|
+
}
|
|
800
|
+
if (activeBoundaryTimer !== null) {
|
|
801
|
+
clearTimeout(activeBoundaryTimer);
|
|
802
|
+
activeBoundaryTimer = null;
|
|
803
|
+
}
|
|
804
|
+
if (activeAudio) {
|
|
805
|
+
activeAudio.onplay = null;
|
|
806
|
+
activeAudio.onended = null;
|
|
807
|
+
activeAudio.onerror = null;
|
|
808
|
+
try {
|
|
809
|
+
activeAudio.pause();
|
|
810
|
+
activeAudio.currentTime = 0;
|
|
811
|
+
} catch (err) {
|
|
812
|
+
void err;
|
|
813
|
+
}
|
|
814
|
+
activeAudio = null;
|
|
815
|
+
}
|
|
816
|
+
revokeActiveObjectUrl();
|
|
817
|
+
if (activeUtterance) {
|
|
818
|
+
activeUtterance.onstart = null;
|
|
819
|
+
activeUtterance.onend = null;
|
|
820
|
+
activeUtterance.onerror = null;
|
|
821
|
+
activeUtterance.onboundary = null;
|
|
822
|
+
activeUtterance = null;
|
|
823
|
+
}
|
|
824
|
+
stopNativeSpeechSynthesis();
|
|
825
|
+
};
|
|
826
|
+
return (set, get) => {
|
|
827
|
+
const playNextFromQueue = () => {
|
|
828
|
+
const queue = get().speechQueue;
|
|
829
|
+
if (queue.length > 0) {
|
|
830
|
+
const [next, ...rest] = queue;
|
|
831
|
+
set({ speechQueue: rest });
|
|
832
|
+
void get().express(next.message, next.options);
|
|
833
|
+
}
|
|
834
|
+
};
|
|
835
|
+
const finishGeneration = (gen) => {
|
|
836
|
+
if (gen !== activeGeneration) {
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
if (activeBoundaryTimer !== null) {
|
|
840
|
+
clearTimeout(activeBoundaryTimer);
|
|
841
|
+
activeBoundaryTimer = null;
|
|
842
|
+
}
|
|
843
|
+
activeAudio = null;
|
|
844
|
+
activeUtterance = null;
|
|
845
|
+
revokeActiveObjectUrl();
|
|
846
|
+
set({
|
|
847
|
+
isSpeaking: false,
|
|
848
|
+
speechStartTime: null,
|
|
849
|
+
currentAmplitude: 0
|
|
850
|
+
});
|
|
851
|
+
playNextFromQueue();
|
|
852
|
+
};
|
|
853
|
+
const expressImpl = async (message, options) => {
|
|
854
|
+
const speechText = (message.speech ?? "").trim();
|
|
855
|
+
const hasWords = speechText.length > 0;
|
|
856
|
+
if (sliceOptions.isSilent || options?.silent || !hasWords) {
|
|
857
|
+
set({
|
|
858
|
+
lastMessage: message,
|
|
859
|
+
isSpeaking: false
|
|
860
|
+
});
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
const effectiveQueueMode = options?.queueMode ?? get().faceSettings?.speechQueueMode ?? "interrupt";
|
|
864
|
+
const currentlyPlaying = get().isSpeaking || activeAudio !== null || activeUtterance !== null || activeTimer !== null;
|
|
865
|
+
if (effectiveQueueMode === "queue" && currentlyPlaying) {
|
|
866
|
+
set((state) => ({
|
|
867
|
+
speechQueue: [...state.speechQueue, { message, options }]
|
|
868
|
+
}));
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
cleanupActivePlayback();
|
|
872
|
+
const myGeneration = activeGeneration;
|
|
873
|
+
set({
|
|
874
|
+
lastMessage: message,
|
|
875
|
+
isSpeaking: true
|
|
876
|
+
});
|
|
877
|
+
const selectedVoice = get().getSelectedVoice();
|
|
878
|
+
const shouldUseAdapter = adapter !== null && typeof adapter.synthesizeSpeech === "function" && (adapter.canHandleVoice ? adapter.canHandleVoice(selectedVoice.voiceURI, selectedVoice) : true);
|
|
879
|
+
if (shouldUseAdapter && adapter?.synthesizeSpeech) {
|
|
880
|
+
try {
|
|
881
|
+
const cacheKey = buildTtsCacheKey({
|
|
882
|
+
voiceId: selectedVoice.voiceURI,
|
|
883
|
+
engine: selectedVoice.engine,
|
|
884
|
+
text: speechText
|
|
885
|
+
});
|
|
886
|
+
const cached = cache ? await cache.get(cacheKey) : void 0;
|
|
887
|
+
if (myGeneration !== activeGeneration) {
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
let playbackUrl;
|
|
891
|
+
let shouldRevokeUrl = false;
|
|
892
|
+
let visemes = null;
|
|
893
|
+
let ampData = null;
|
|
894
|
+
let extrema = null;
|
|
895
|
+
if (cached && cache) {
|
|
896
|
+
playbackUrl = cache.createObjectUrl(cached);
|
|
897
|
+
shouldRevokeUrl = true;
|
|
898
|
+
visemes = cached.visemes ?? null;
|
|
899
|
+
ampData = cached.amplitudeData ?? null;
|
|
900
|
+
extrema = cached.extrema ?? null;
|
|
901
|
+
} else {
|
|
902
|
+
const synthesized = await adapter.synthesizeSpeech(
|
|
903
|
+
speechText,
|
|
904
|
+
selectedVoice.voiceURI,
|
|
905
|
+
{
|
|
906
|
+
engine: selectedVoice.engine,
|
|
907
|
+
language: get().faceSettings?.selectedLanguage
|
|
908
|
+
}
|
|
909
|
+
);
|
|
910
|
+
if (myGeneration !== activeGeneration) {
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
visemes = synthesized.visemes ?? null;
|
|
914
|
+
ampData = synthesized.amplitudeData ?? null;
|
|
915
|
+
extrema = synthesized.extrema ?? null;
|
|
916
|
+
const mimeType = synthesized.mimeType || "audio/mpeg";
|
|
917
|
+
let blobToCache = null;
|
|
918
|
+
if (synthesized.audioBlob) {
|
|
919
|
+
blobToCache = synthesized.audioBlob;
|
|
920
|
+
} else if (synthesized.audioBuffer) {
|
|
921
|
+
blobToCache = new Blob([synthesized.audioBuffer], { type: mimeType });
|
|
922
|
+
}
|
|
923
|
+
if (blobToCache) {
|
|
924
|
+
if (cache) {
|
|
925
|
+
await cache.set(cacheKey, {
|
|
926
|
+
audioBlob: blobToCache,
|
|
927
|
+
mimeType,
|
|
928
|
+
visemes: synthesized.visemes,
|
|
929
|
+
amplitudeData: synthesized.amplitudeData,
|
|
930
|
+
extrema: synthesized.extrema
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
playbackUrl = URL.createObjectURL(blobToCache);
|
|
934
|
+
shouldRevokeUrl = true;
|
|
935
|
+
} else if (synthesized.audioUrl) {
|
|
936
|
+
playbackUrl = synthesized.audioUrl;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
if (playbackUrl && typeof Audio !== "undefined") {
|
|
940
|
+
get().setSpokenVisemes(visemes);
|
|
941
|
+
get().setAmplitudeData(ampData, extrema);
|
|
942
|
+
const audio = new Audio(playbackUrl);
|
|
943
|
+
audio.crossOrigin = "anonymous";
|
|
944
|
+
activeAudio = audio;
|
|
945
|
+
if (shouldRevokeUrl) {
|
|
946
|
+
activeObjectUrl = playbackUrl;
|
|
947
|
+
}
|
|
948
|
+
analyzer.connect(audio);
|
|
949
|
+
audio.onplay = () => {
|
|
950
|
+
if (myGeneration !== activeGeneration) {
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
set({
|
|
954
|
+
isSpeaking: true,
|
|
955
|
+
speechStartTime: performance.now()
|
|
956
|
+
});
|
|
957
|
+
const pollAmplitude = () => {
|
|
958
|
+
if (myGeneration === activeGeneration && activeAudio === audio && !audio.paused && !audio.ended) {
|
|
959
|
+
get().setCurrentAmplitude(analyzer.getAmplitude());
|
|
960
|
+
if (typeof requestAnimationFrame === "function") {
|
|
961
|
+
requestAnimationFrame(pollAmplitude);
|
|
962
|
+
}
|
|
963
|
+
} else if (myGeneration === activeGeneration) {
|
|
964
|
+
get().setCurrentAmplitude(0);
|
|
965
|
+
}
|
|
966
|
+
};
|
|
967
|
+
pollAmplitude();
|
|
968
|
+
};
|
|
969
|
+
audio.onended = () => {
|
|
970
|
+
finishGeneration(myGeneration);
|
|
971
|
+
};
|
|
972
|
+
audio.onerror = () => {
|
|
973
|
+
finishGeneration(myGeneration);
|
|
974
|
+
};
|
|
975
|
+
await audio.play();
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
} catch (adapterError) {
|
|
979
|
+
console.warn(
|
|
980
|
+
"SpeechSynthesizerAdapter failed; falling back to Web Speech API:",
|
|
981
|
+
adapterError
|
|
982
|
+
);
|
|
983
|
+
if (myGeneration !== activeGeneration) {
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
if (typeof window === "undefined" || typeof SpeechSynthesisUtterance === "undefined" || typeof window.speechSynthesis === "undefined") {
|
|
989
|
+
finishGeneration(myGeneration);
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
const utterance = new SpeechSynthesisUtterance(speechText);
|
|
993
|
+
activeUtterance = utterance;
|
|
994
|
+
if (isNativeSpeechSynthesisVoice(selectedVoice)) {
|
|
995
|
+
utterance.voice = selectedVoice;
|
|
996
|
+
} else {
|
|
997
|
+
const nativeVoices = window.speechSynthesis.getVoices();
|
|
998
|
+
const matched = findMatchingVoice(nativeVoices, selectedVoice.voiceURI) || findMatchingVoice(nativeVoices, selectedVoice.name);
|
|
999
|
+
if (matched) {
|
|
1000
|
+
utterance.voice = matched;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
utterance.lang = selectedVoice.lang || get().faceSettings?.selectedLanguage || "en-US";
|
|
1004
|
+
const estimatedVisemes = estimateVisemesFromText(speechText);
|
|
1005
|
+
set({
|
|
1006
|
+
spokenVisemes: estimatedVisemes,
|
|
1007
|
+
amplitudeData: null,
|
|
1008
|
+
amplitudeExtrema: null,
|
|
1009
|
+
currentAmplitude: 0
|
|
1010
|
+
});
|
|
1011
|
+
utterance.onstart = () => {
|
|
1012
|
+
if (myGeneration !== activeGeneration) {
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
set({
|
|
1016
|
+
isSpeaking: true,
|
|
1017
|
+
speechStartTime: performance.now()
|
|
1018
|
+
});
|
|
1019
|
+
};
|
|
1020
|
+
utterance.onboundary = (event) => {
|
|
1021
|
+
if (myGeneration !== activeGeneration) {
|
|
1022
|
+
return;
|
|
1023
|
+
}
|
|
1024
|
+
if (event.name === "word") {
|
|
1025
|
+
get().setCurrentAmplitude(1);
|
|
1026
|
+
if (activeBoundaryTimer !== null) {
|
|
1027
|
+
clearTimeout(activeBoundaryTimer);
|
|
1028
|
+
}
|
|
1029
|
+
activeBoundaryTimer = setTimeout(() => {
|
|
1030
|
+
if (myGeneration === activeGeneration) {
|
|
1031
|
+
get().setCurrentAmplitude(0);
|
|
1032
|
+
}
|
|
1033
|
+
}, 100);
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
utterance.onend = () => {
|
|
1037
|
+
finishGeneration(myGeneration);
|
|
1038
|
+
};
|
|
1039
|
+
utterance.onerror = (event) => {
|
|
1040
|
+
if (event.error === "not-allowed") {
|
|
1041
|
+
get().setSpeechAllowed(false);
|
|
1042
|
+
}
|
|
1043
|
+
finishGeneration(myGeneration);
|
|
1044
|
+
};
|
|
1045
|
+
const invokeSpeak = () => {
|
|
1046
|
+
activeTimer = null;
|
|
1047
|
+
if (myGeneration !== activeGeneration) {
|
|
1048
|
+
return;
|
|
1049
|
+
}
|
|
1050
|
+
try {
|
|
1051
|
+
if (window.speechSynthesis.paused) {
|
|
1052
|
+
window.speechSynthesis.resume();
|
|
1053
|
+
}
|
|
1054
|
+
window.speechSynthesis.speak(utterance);
|
|
1055
|
+
} catch (speakErr) {
|
|
1056
|
+
console.warn("SpeechSynthesis speak invocation error:", speakErr);
|
|
1057
|
+
finishGeneration(myGeneration);
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1060
|
+
if (utteranceDeferMs <= 0) {
|
|
1061
|
+
invokeSpeak();
|
|
1062
|
+
} else {
|
|
1063
|
+
activeTimer = setTimeout(invokeSpeak, utteranceDeferMs);
|
|
1064
|
+
}
|
|
1065
|
+
};
|
|
1066
|
+
return {
|
|
1067
|
+
lastMessage: sliceOptions.initialMessage ?? DEFAULT_FACE_MESSAGE,
|
|
1068
|
+
isSpeaking: false,
|
|
1069
|
+
speechAllowed: false,
|
|
1070
|
+
selectedVoiceUri: null,
|
|
1071
|
+
availableVoices: [],
|
|
1072
|
+
spokenVisemes: null,
|
|
1073
|
+
amplitudeData: null,
|
|
1074
|
+
amplitudeExtrema: null,
|
|
1075
|
+
currentAmplitude: 0,
|
|
1076
|
+
speechStartTime: null,
|
|
1077
|
+
speechQueue: [],
|
|
1078
|
+
setSpeechAllowed: (allowed) => {
|
|
1079
|
+
set({ speechAllowed: allowed });
|
|
1080
|
+
},
|
|
1081
|
+
setAvailableVoices: (voices) => {
|
|
1082
|
+
set({ availableVoices: voices });
|
|
1083
|
+
},
|
|
1084
|
+
refreshVoices: async () => {
|
|
1085
|
+
const collected = [];
|
|
1086
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1087
|
+
if (typeof window !== "undefined" && "speechSynthesis" in window) {
|
|
1088
|
+
const browserVoices = window.speechSynthesis.getVoices();
|
|
1089
|
+
for (const v of browserVoices) {
|
|
1090
|
+
if (!seen.has(v.voiceURI)) {
|
|
1091
|
+
seen.add(v.voiceURI);
|
|
1092
|
+
collected.push(v);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
if (adapter?.fetchVoices) {
|
|
1097
|
+
try {
|
|
1098
|
+
const remoteVoices = await adapter.fetchVoices();
|
|
1099
|
+
for (const v of remoteVoices) {
|
|
1100
|
+
if (!seen.has(v.voiceURI)) {
|
|
1101
|
+
seen.add(v.voiceURI);
|
|
1102
|
+
collected.push(v);
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
} catch (err) {
|
|
1106
|
+
console.warn("Failed to fetch adapter voices:", err);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
set({ availableVoices: collected });
|
|
1110
|
+
return collected;
|
|
1111
|
+
},
|
|
1112
|
+
getDefaultVoice: () => {
|
|
1113
|
+
const voices = get().availableVoices;
|
|
1114
|
+
const selectedLang = get().faceSettings?.selectedLanguage;
|
|
1115
|
+
if (selectedLang) {
|
|
1116
|
+
const langVoice = voices.find(
|
|
1117
|
+
(v) => matchesLanguage(v.lang, selectedLang)
|
|
1118
|
+
);
|
|
1119
|
+
if (langVoice) {
|
|
1120
|
+
return langVoice;
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
const defaultFlagged = voices.find((v) => v.default === true);
|
|
1124
|
+
if (defaultFlagged) {
|
|
1125
|
+
return defaultFlagged;
|
|
1126
|
+
}
|
|
1127
|
+
if (voices.length > 0) {
|
|
1128
|
+
return voices[0];
|
|
1129
|
+
}
|
|
1130
|
+
return {
|
|
1131
|
+
...FALLBACK_DEFAULT_VOICE,
|
|
1132
|
+
lang: selectedLang || "en-US"
|
|
1133
|
+
};
|
|
1134
|
+
},
|
|
1135
|
+
getSelectedVoice: () => {
|
|
1136
|
+
const uri = get().selectedVoiceUri;
|
|
1137
|
+
const voices = get().availableVoices;
|
|
1138
|
+
if (uri !== null) {
|
|
1139
|
+
const matched = findMatchingVoice(voices, uri);
|
|
1140
|
+
if (matched) {
|
|
1141
|
+
return matched;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
return get().getDefaultVoice();
|
|
1145
|
+
},
|
|
1146
|
+
setVoice: (voiceUri, voiceOptions) => {
|
|
1147
|
+
const prevVoice = get().selectedVoiceUri || get().getSelectedVoice()?.voiceURI;
|
|
1148
|
+
set({ selectedVoiceUri: voiceUri });
|
|
1149
|
+
if (!sliceOptions.isSilent && !voiceOptions?.silent && prevVoice && prevVoice !== voiceUri) {
|
|
1150
|
+
void get().express({
|
|
1151
|
+
title: "Switching voice",
|
|
1152
|
+
speech: "Switching voice",
|
|
1153
|
+
color: "Light Blue",
|
|
1154
|
+
emotion: "Neutral"
|
|
1155
|
+
});
|
|
1156
|
+
}
|
|
1157
|
+
},
|
|
1158
|
+
setSpokenVisemes: (visemes) => {
|
|
1159
|
+
set({ spokenVisemes: visemes });
|
|
1160
|
+
},
|
|
1161
|
+
setAmplitudeData: (data, extrema) => {
|
|
1162
|
+
set({
|
|
1163
|
+
amplitudeData: data,
|
|
1164
|
+
amplitudeExtrema: extrema
|
|
1165
|
+
});
|
|
1166
|
+
},
|
|
1167
|
+
setIsSpeaking: (isSpeaking) => {
|
|
1168
|
+
set({ isSpeaking });
|
|
1169
|
+
},
|
|
1170
|
+
setCurrentAmplitude: (amplitude) => {
|
|
1171
|
+
const gain = get().faceSettings?.amplitudeGain ?? 1;
|
|
1172
|
+
const scaled = amplitude * gain;
|
|
1173
|
+
const clamped = Number.isNaN(scaled) ? 0 : Math.min(1, Math.max(0, scaled));
|
|
1174
|
+
set({ currentAmplitude: clamped });
|
|
1175
|
+
},
|
|
1176
|
+
setSpeechAdapter: (nextAdapter) => {
|
|
1177
|
+
adapter = nextAdapter;
|
|
1178
|
+
},
|
|
1179
|
+
express: expressImpl,
|
|
1180
|
+
_express: expressImpl,
|
|
1181
|
+
updateLastMessage: async (message) => {
|
|
1182
|
+
await get().express(message);
|
|
1183
|
+
},
|
|
1184
|
+
stopSpeech: () => {
|
|
1185
|
+
cleanupActivePlayback();
|
|
1186
|
+
set({
|
|
1187
|
+
isSpeaking: false,
|
|
1188
|
+
speechStartTime: null,
|
|
1189
|
+
currentAmplitude: 0,
|
|
1190
|
+
spokenVisemes: null,
|
|
1191
|
+
amplitudeData: null,
|
|
1192
|
+
amplitudeExtrema: null,
|
|
1193
|
+
speechQueue: []
|
|
1194
|
+
});
|
|
1195
|
+
}
|
|
1196
|
+
};
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
// src/store/createPeerbotsFaceStore.ts
|
|
1201
|
+
function restorePersistedFace(persistedSelection, builtIns, fallback) {
|
|
1202
|
+
if (typeof persistedSelection === "string") {
|
|
1203
|
+
return builtIns.find((f) => f.name === persistedSelection) ?? fallback;
|
|
1204
|
+
}
|
|
1205
|
+
if (persistedSelection && typeof persistedSelection === "object") {
|
|
1206
|
+
const selectionObj = persistedSelection;
|
|
1207
|
+
if (typeof selectionObj.name === "string") {
|
|
1208
|
+
const found = builtIns.find((f) => f.name === selectionObj.name);
|
|
1209
|
+
if (found) {
|
|
1210
|
+
if (found.kind === "Vizij" && selectionObj.assetBundle && typeof selectionObj.assetBundle === "object") {
|
|
1211
|
+
const persistedBundle = selectionObj.assetBundle;
|
|
1212
|
+
const persistedGlb = persistedBundle.glb && typeof persistedBundle.glb === "object" ? persistedBundle.glb : {};
|
|
1213
|
+
return {
|
|
1214
|
+
...found,
|
|
1215
|
+
assetBundle: {
|
|
1216
|
+
...found.assetBundle,
|
|
1217
|
+
...persistedBundle,
|
|
1218
|
+
glb: {
|
|
1219
|
+
...found.assetBundle.glb,
|
|
1220
|
+
...persistedGlb
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
};
|
|
1224
|
+
}
|
|
1225
|
+
return found;
|
|
1226
|
+
}
|
|
1227
|
+
if (typeof selectionObj.kind === "string") {
|
|
1228
|
+
return persistedSelection;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
return fallback;
|
|
1233
|
+
}
|
|
1234
|
+
function createPeerbotsFaceStore(options = {}) {
|
|
1235
|
+
const stateInitializer = (...args) => ({
|
|
1236
|
+
...createFaceRenderingSlice(options)(...args),
|
|
1237
|
+
...createFaceSpeechSlice(options)(...args)
|
|
1238
|
+
});
|
|
1239
|
+
if (!options.persistName) {
|
|
1240
|
+
return (0, import_zustand.createStore)()(stateInitializer);
|
|
1241
|
+
}
|
|
1242
|
+
const builtIns = createBuiltInFaces(options.assetBaseUrl);
|
|
1243
|
+
const fallbackFace = options.initialFace ?? builtIns[0];
|
|
1244
|
+
return (0, import_zustand.createStore)()(
|
|
1245
|
+
(0, import_middleware.persist)(stateInitializer, {
|
|
1246
|
+
name: options.persistName,
|
|
1247
|
+
version: 1,
|
|
1248
|
+
partialize: (state) => ({
|
|
1249
|
+
selectedFace: state.selectedFace,
|
|
1250
|
+
faceSettings: state.faceSettings,
|
|
1251
|
+
selectedVoiceUri: state.selectedVoiceUri,
|
|
1252
|
+
lastMessage: state.lastMessage
|
|
1253
|
+
}),
|
|
1254
|
+
merge: (persistedState, currentState) => {
|
|
1255
|
+
const persisted = persistedState ?? {};
|
|
1256
|
+
const restoredFace = restorePersistedFace(
|
|
1257
|
+
persisted.selectedFace,
|
|
1258
|
+
currentState.availableFaces,
|
|
1259
|
+
fallbackFace
|
|
1260
|
+
);
|
|
1261
|
+
const restoredSettings = persisted.faceSettings ? mergeFaceSettings(
|
|
1262
|
+
defaultFaceSettings,
|
|
1263
|
+
persisted.faceSettings
|
|
1264
|
+
) : currentState.faceSettings;
|
|
1265
|
+
return {
|
|
1266
|
+
...currentState,
|
|
1267
|
+
selectedFace: restoredFace,
|
|
1268
|
+
selectedVoiceUri: persisted.selectedVoiceUri ?? null,
|
|
1269
|
+
faceSettings: restoredSettings,
|
|
1270
|
+
lastMessage: persisted.lastMessage ?? options.initialMessage ?? DEFAULT_FACE_MESSAGE,
|
|
1271
|
+
isSpeaking: false
|
|
1272
|
+
};
|
|
1273
|
+
}
|
|
1274
|
+
})
|
|
1275
|
+
);
|
|
1276
|
+
}
|
|
1277
|
+
var globalFaceStore = createPeerbotsFaceStore({
|
|
1278
|
+
persistName: "faceSettings"
|
|
1279
|
+
});
|
|
1280
|
+
|
|
1281
|
+
// src/store/FaceStoreContext.tsx
|
|
1282
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
1283
|
+
var FaceStoreContext = (0, import_react.createContext)(null);
|
|
1284
|
+
var activeGlobalFaceStore = globalFaceStore;
|
|
1285
|
+
function useFaceStoreInstance() {
|
|
1286
|
+
const context = (0, import_react.useContext)(FaceStoreContext);
|
|
1287
|
+
return context ?? activeGlobalFaceStore;
|
|
1288
|
+
}
|
|
1289
|
+
function useFaceStoreBase(selector) {
|
|
1290
|
+
const store = useFaceStoreInstance();
|
|
1291
|
+
return (0, import_zustand2.useStore)(store, selector);
|
|
1292
|
+
}
|
|
1293
|
+
var useFaceStore = Object.assign(useFaceStoreBase, {
|
|
1294
|
+
getState: () => activeGlobalFaceStore.getState(),
|
|
1295
|
+
setState: ((...args) => activeGlobalFaceStore.setState(...args)),
|
|
1296
|
+
subscribe: ((...args) => activeGlobalFaceStore.subscribe(...args))
|
|
1297
|
+
});
|
|
1298
|
+
|
|
1299
|
+
// src/components/FaceContainer.tsx
|
|
1300
|
+
var import_react11 = require("react");
|
|
1301
|
+
var import_pixi5 = require("pixi.js");
|
|
1302
|
+
var import_react12 = require("@pixi/react");
|
|
1303
|
+
|
|
1304
|
+
// src/helpers/color_mappers.ts
|
|
1305
|
+
var color_mapping = {
|
|
1306
|
+
"Light Blue": "#ABE3FA",
|
|
1307
|
+
Blue: "#5F94D6",
|
|
1308
|
+
Green: "#65C77D",
|
|
1309
|
+
Red: "#E6817E",
|
|
1310
|
+
Purple: "#9B86F3",
|
|
1311
|
+
Pink: "#EBA4E4",
|
|
1312
|
+
Yellow: "#F8EDB1",
|
|
1313
|
+
Orange: "#E6A575",
|
|
1314
|
+
Grey: "#CECECE",
|
|
1315
|
+
Black: "#4D4D4D",
|
|
1316
|
+
White: "#FDFEFE"
|
|
1317
|
+
};
|
|
1318
|
+
function hexToRgb(hex) {
|
|
1319
|
+
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
|
|
1320
|
+
const expanded = hex.replace(shorthandRegex, (_m, r, g, b) => {
|
|
1321
|
+
return r + r + g + g + b + b;
|
|
1322
|
+
});
|
|
1323
|
+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(expanded);
|
|
1324
|
+
return result ? {
|
|
1325
|
+
r: parseInt(result[1], 16),
|
|
1326
|
+
g: parseInt(result[2], 16),
|
|
1327
|
+
b: parseInt(result[3], 16)
|
|
1328
|
+
} : null;
|
|
1329
|
+
}
|
|
1330
|
+
var hexToRgba = (color, alpha = 1) => {
|
|
1331
|
+
const rgb = hexToRgb(color);
|
|
1332
|
+
if (rgb !== null) {
|
|
1333
|
+
return `rgba(${rgb.r},${rgb.g},${rgb.b},${alpha})`;
|
|
1334
|
+
}
|
|
1335
|
+
return null;
|
|
1336
|
+
};
|
|
1337
|
+
function rgbaToHex(orig) {
|
|
1338
|
+
const rgb = orig.replace(/\s/g, "").match(/^rgba?\((\d+),(\d+),(\d+),?([^,\s)]+)?/i);
|
|
1339
|
+
if (!rgb) {
|
|
1340
|
+
return orig;
|
|
1341
|
+
}
|
|
1342
|
+
const alphaStr = (rgb[4] || "").trim();
|
|
1343
|
+
const hexRgb = (Number(rgb[1]) | 1 << 8).toString(16).slice(1) + (Number(rgb[2]) | 1 << 8).toString(16).slice(1) + (Number(rgb[3]) | 1 << 8).toString(16).slice(1);
|
|
1344
|
+
const numericAlpha = alphaStr !== "" ? Number(alphaStr) : 1;
|
|
1345
|
+
const clampedAlpha = Math.max(0, Math.min(1, Number.isNaN(numericAlpha) ? 1 : numericAlpha));
|
|
1346
|
+
const hexAlpha = (Math.round(clampedAlpha * 255) | 1 << 8).toString(16).slice(1);
|
|
1347
|
+
return hexRgb + hexAlpha;
|
|
1348
|
+
}
|
|
1349
|
+
var deconstructRgba = (rgba) => {
|
|
1350
|
+
return rgba.replace(/[^\d.,]/g, "").split(",").map((x) => Number(x));
|
|
1351
|
+
};
|
|
1352
|
+
var formatRgba = (color) => {
|
|
1353
|
+
return `rgba(${color.r},${color.g},${color.b},${color.a})`;
|
|
1354
|
+
};
|
|
1355
|
+
var interpolateColor = (colorA, colorB, progress) => {
|
|
1356
|
+
const rgbaColorA = hexToRgba(colorA);
|
|
1357
|
+
const rgbaColorB = hexToRgba(colorB);
|
|
1358
|
+
if (rgbaColorA && rgbaColorB) {
|
|
1359
|
+
const [r1, g1, b1, a1] = deconstructRgba(rgbaColorA);
|
|
1360
|
+
const [r2, g2, b2, a2] = deconstructRgba(rgbaColorB);
|
|
1361
|
+
const clampedProgress = Math.max(0, Math.min(1, progress));
|
|
1362
|
+
return formatRgba({
|
|
1363
|
+
r: Math.round(r1 + (r2 - r1) * clampedProgress),
|
|
1364
|
+
g: Math.round(g1 + (g2 - g1) * clampedProgress),
|
|
1365
|
+
b: Math.round(b1 + (b2 - b1) * clampedProgress),
|
|
1366
|
+
a: Number((a1 + (a2 - a1) * clampedProgress).toFixed(3))
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
return null;
|
|
1370
|
+
};
|
|
1371
|
+
|
|
1372
|
+
// src/components/FaceEyes.tsx
|
|
1373
|
+
var import_pixi = require("pixi.js");
|
|
1374
|
+
var import_react2 = require("@pixi/react");
|
|
1375
|
+
var import_react3 = require("react");
|
|
1376
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
1377
|
+
var EYEBROW_POINTS = [
|
|
1378
|
+
{ x: -5, y: 2.5 },
|
|
1379
|
+
{ x: -4, y: 1.6 },
|
|
1380
|
+
{ x: -3, y: 0.9 },
|
|
1381
|
+
{ x: -2, y: 0.4 },
|
|
1382
|
+
{ x: -1, y: 0.1 },
|
|
1383
|
+
{ x: 0, y: 0 },
|
|
1384
|
+
{ x: 1, y: 0.1 },
|
|
1385
|
+
{ x: 2, y: 0.4 },
|
|
1386
|
+
{ x: 3, y: 0.9 },
|
|
1387
|
+
{ x: 4, y: 1.6 },
|
|
1388
|
+
{ x: 5, y: 2.5 }
|
|
1389
|
+
];
|
|
1390
|
+
function FaceEyes({
|
|
1391
|
+
scale,
|
|
1392
|
+
frozen = false
|
|
1393
|
+
}) {
|
|
1394
|
+
const message = useFaceStore((state) => state.lastMessage);
|
|
1395
|
+
const face = useFaceStore((state) => state.selectedFace);
|
|
1396
|
+
const gaze = useFaceStore((state) => state.gaze);
|
|
1397
|
+
const pixiSettings = useFaceStore((state) => state.faceSettings.pixiSettings);
|
|
1398
|
+
(0, import_react2.useExtend)({ Container: import_pixi.Container, Sprite: import_pixi.Sprite, Graphics: import_pixi.Graphics });
|
|
1399
|
+
const [faceSheet, setFaceSheet] = (0, import_react3.useState)(null);
|
|
1400
|
+
const [cheekTexture, setCheekTexture] = (0, import_react3.useState)(null);
|
|
1401
|
+
const [eyeBrows, setEyeBrows] = (0, import_react3.useState)("Hugo");
|
|
1402
|
+
const leftEyeTexture = (0, import_react3.useMemo)(() => {
|
|
1403
|
+
if (!faceSheet) return import_pixi.Texture.EMPTY;
|
|
1404
|
+
const emotion = message?.emotion || "Neutral";
|
|
1405
|
+
return faceSheet.textures?.[`${emotion.toLowerCase()}_l.png`] || faceSheet.textures?.["neutral_l.png"] || import_pixi.Texture.EMPTY;
|
|
1406
|
+
}, [message, faceSheet]);
|
|
1407
|
+
const rightEyeTexture = (0, import_react3.useMemo)(() => {
|
|
1408
|
+
if (!faceSheet) return import_pixi.Texture.EMPTY;
|
|
1409
|
+
const emotion = message?.emotion || "Neutral";
|
|
1410
|
+
return faceSheet.textures?.[`${emotion.toLowerCase()}_r.png`] || faceSheet.textures?.["neutral_r.png"] || import_pixi.Texture.EMPTY;
|
|
1411
|
+
}, [message, faceSheet]);
|
|
1412
|
+
const breathingScaleRef = (0, import_react3.useRef)(1);
|
|
1413
|
+
const breathingTimeRef = (0, import_react3.useRef)(0);
|
|
1414
|
+
const blinkScaleRef = (0, import_react3.useRef)(1);
|
|
1415
|
+
const isBlinkingRef = (0, import_react3.useRef)(false);
|
|
1416
|
+
const blinkEveryRef = (0, import_react3.useRef)(500);
|
|
1417
|
+
const blinkTimerRef = (0, import_react3.useRef)(0);
|
|
1418
|
+
const blinkTargetScaleRef = (0, import_react3.useRef)(1);
|
|
1419
|
+
const currentGazeRef = (0, import_react3.useRef)({ x: 0, y: 0 });
|
|
1420
|
+
const leftEyeContainerRef = (0, import_react3.useRef)(null);
|
|
1421
|
+
const rightEyeContainerRef = (0, import_react3.useRef)(null);
|
|
1422
|
+
const leftEyebrowRef = (0, import_react3.useRef)(null);
|
|
1423
|
+
const rightEyebrowRef = (0, import_react3.useRef)(null);
|
|
1424
|
+
const leftEyeSpriteRef = (0, import_react3.useRef)(null);
|
|
1425
|
+
const rightEyeSpriteRef = (0, import_react3.useRef)(null);
|
|
1426
|
+
const leftCheekRef = (0, import_react3.useRef)(null);
|
|
1427
|
+
const rightCheekRef = (0, import_react3.useRef)(null);
|
|
1428
|
+
const positions = (0, import_react3.useMemo)(() => {
|
|
1429
|
+
return {
|
|
1430
|
+
leftEyeX: -300 * scale,
|
|
1431
|
+
rightEyeX: 300 * scale,
|
|
1432
|
+
leftEyeY: 0,
|
|
1433
|
+
rightEyeY: 0,
|
|
1434
|
+
leftEyeToRightEyeScaleRatio: 1.1,
|
|
1435
|
+
cheekYOffset: 150 * scale,
|
|
1436
|
+
eyebrowMultiplier: 7.5 * scale,
|
|
1437
|
+
eyebrowOffset: -180 * scale
|
|
1438
|
+
};
|
|
1439
|
+
}, [scale]);
|
|
1440
|
+
(0, import_react2.useTick)((ticker) => {
|
|
1441
|
+
const deltaTime = frozen ? 0 : ticker.deltaTime;
|
|
1442
|
+
const frequency = 785e-5;
|
|
1443
|
+
breathingTimeRef.current += deltaTime;
|
|
1444
|
+
breathingScaleRef.current = 1 + 0.1 * Math.sin(breathingTimeRef.current * frequency);
|
|
1445
|
+
const blinkDuration = 20;
|
|
1446
|
+
const blinkSpeed = 0.5;
|
|
1447
|
+
blinkTimerRef.current += deltaTime;
|
|
1448
|
+
if (isBlinkingRef.current) {
|
|
1449
|
+
if (blinkTimerRef.current > blinkDuration) {
|
|
1450
|
+
blinkEveryRef.current = 200 + Math.random() * 400;
|
|
1451
|
+
blinkTargetScaleRef.current = 1;
|
|
1452
|
+
isBlinkingRef.current = false;
|
|
1453
|
+
blinkTimerRef.current = 0;
|
|
1454
|
+
}
|
|
1455
|
+
} else if (blinkTimerRef.current > blinkEveryRef.current) {
|
|
1456
|
+
blinkTargetScaleRef.current = 0.05;
|
|
1457
|
+
isBlinkingRef.current = true;
|
|
1458
|
+
blinkTimerRef.current = 0;
|
|
1459
|
+
}
|
|
1460
|
+
blinkScaleRef.current += (blinkTargetScaleRef.current - blinkScaleRef.current) * blinkSpeed * deltaTime;
|
|
1461
|
+
currentGazeRef.current.x += (gaze.x - currentGazeRef.current.x) * 0.15;
|
|
1462
|
+
currentGazeRef.current.y += (gaze.y - currentGazeRef.current.y) * 0.15;
|
|
1463
|
+
const gazeOffsetX = currentGazeRef.current.x * 40 * scale;
|
|
1464
|
+
const gazeOffsetY = currentGazeRef.current.y * 30 * scale;
|
|
1465
|
+
if (leftEyeContainerRef.current) {
|
|
1466
|
+
leftEyeContainerRef.current.x = positions.leftEyeX + gazeOffsetX;
|
|
1467
|
+
leftEyeContainerRef.current.y = positions.leftEyeY + gazeOffsetY;
|
|
1468
|
+
}
|
|
1469
|
+
if (rightEyeContainerRef.current) {
|
|
1470
|
+
rightEyeContainerRef.current.x = positions.rightEyeX + gazeOffsetX;
|
|
1471
|
+
rightEyeContainerRef.current.y = positions.rightEyeY + gazeOffsetY;
|
|
1472
|
+
}
|
|
1473
|
+
const breathe = breathingScaleRef.current;
|
|
1474
|
+
const blink = blinkScaleRef.current;
|
|
1475
|
+
if (leftEyeSpriteRef.current) {
|
|
1476
|
+
leftEyeSpriteRef.current.scale.x = positions.leftEyeToRightEyeScaleRatio * breathe * scale;
|
|
1477
|
+
leftEyeSpriteRef.current.scale.y = positions.leftEyeToRightEyeScaleRatio * breathe * blink * scale;
|
|
1478
|
+
}
|
|
1479
|
+
if (rightEyeSpriteRef.current) {
|
|
1480
|
+
rightEyeSpriteRef.current.scale.x = 1 * breathe * scale;
|
|
1481
|
+
rightEyeSpriteRef.current.scale.y = 1 * breathe * blink * scale;
|
|
1482
|
+
}
|
|
1483
|
+
if (leftCheekRef.current) {
|
|
1484
|
+
leftCheekRef.current.scale.x = 0.08 * breathe * scale;
|
|
1485
|
+
leftCheekRef.current.scale.y = 0.04 * breathe * scale;
|
|
1486
|
+
}
|
|
1487
|
+
if (rightCheekRef.current) {
|
|
1488
|
+
rightCheekRef.current.scale.x = 0.08 * breathe * scale;
|
|
1489
|
+
rightCheekRef.current.scale.y = 0.04 * breathe * scale;
|
|
1490
|
+
}
|
|
1491
|
+
const strokeWidth = Math.max(1.5, 3 * scale);
|
|
1492
|
+
if (leftEyebrowRef.current) {
|
|
1493
|
+
const g = leftEyebrowRef.current;
|
|
1494
|
+
g.clear();
|
|
1495
|
+
const mult = positions.eyebrowMultiplier * breathe;
|
|
1496
|
+
g.moveTo(EYEBROW_POINTS[0].x * mult, EYEBROW_POINTS[0].y * mult);
|
|
1497
|
+
for (let i = 1; i < EYEBROW_POINTS.length; i++) {
|
|
1498
|
+
g.lineTo(EYEBROW_POINTS[i].x * mult, EYEBROW_POINTS[i].y * mult);
|
|
1499
|
+
}
|
|
1500
|
+
g.stroke({
|
|
1501
|
+
width: strokeWidth,
|
|
1502
|
+
color: "black",
|
|
1503
|
+
cap: "round",
|
|
1504
|
+
join: "round"
|
|
1505
|
+
});
|
|
1506
|
+
}
|
|
1507
|
+
if (rightEyebrowRef.current) {
|
|
1508
|
+
const g = rightEyebrowRef.current;
|
|
1509
|
+
g.clear();
|
|
1510
|
+
const mult = positions.eyebrowMultiplier * breathe;
|
|
1511
|
+
g.moveTo(EYEBROW_POINTS[0].x * mult, EYEBROW_POINTS[0].y * mult);
|
|
1512
|
+
for (let i = 1; i < EYEBROW_POINTS.length; i++) {
|
|
1513
|
+
g.lineTo(EYEBROW_POINTS[i].x * mult, EYEBROW_POINTS[i].y * mult);
|
|
1514
|
+
}
|
|
1515
|
+
g.stroke({
|
|
1516
|
+
width: strokeWidth,
|
|
1517
|
+
color: "black",
|
|
1518
|
+
cap: "round",
|
|
1519
|
+
join: "round"
|
|
1520
|
+
});
|
|
1521
|
+
}
|
|
1522
|
+
});
|
|
1523
|
+
const drawEyebrow = (0, import_react3.useCallback)(
|
|
1524
|
+
(g) => {
|
|
1525
|
+
g.clear();
|
|
1526
|
+
const mult = positions.eyebrowMultiplier * breathingScaleRef.current;
|
|
1527
|
+
const strokeWidth = Math.max(1.5, 3 * scale);
|
|
1528
|
+
g.moveTo(EYEBROW_POINTS[0].x * mult, EYEBROW_POINTS[0].y * mult);
|
|
1529
|
+
for (let i = 1; i < EYEBROW_POINTS.length; i++) {
|
|
1530
|
+
g.lineTo(EYEBROW_POINTS[i].x * mult, EYEBROW_POINTS[i].y * mult);
|
|
1531
|
+
}
|
|
1532
|
+
g.stroke({
|
|
1533
|
+
width: strokeWidth,
|
|
1534
|
+
color: "black",
|
|
1535
|
+
cap: "round",
|
|
1536
|
+
join: "round"
|
|
1537
|
+
});
|
|
1538
|
+
},
|
|
1539
|
+
[positions.eyebrowMultiplier, scale]
|
|
1540
|
+
);
|
|
1541
|
+
(0, import_react3.useEffect)(() => {
|
|
1542
|
+
let cancelled = false;
|
|
1543
|
+
async function loadAvatarTextures(currentFace) {
|
|
1544
|
+
if (currentFace?.kind !== "PixiFace" || currentFace.type !== "avatar") {
|
|
1545
|
+
return;
|
|
1546
|
+
}
|
|
1547
|
+
try {
|
|
1548
|
+
if (currentFace.eyes?.kind === "spritesheet") {
|
|
1549
|
+
const sheet = await import_pixi.Assets.load(currentFace.eyes.path);
|
|
1550
|
+
if (!cancelled) {
|
|
1551
|
+
setFaceSheet(sheet);
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
if (currentFace.cheeks?.kind === "texture") {
|
|
1555
|
+
const cheek = await import_pixi.Assets.load(currentFace.cheeks.path);
|
|
1556
|
+
if (!cancelled) {
|
|
1557
|
+
setCheekTexture(cheek);
|
|
1558
|
+
}
|
|
1559
|
+
} else if (!cancelled) {
|
|
1560
|
+
setCheekTexture(null);
|
|
1561
|
+
}
|
|
1562
|
+
if (!cancelled) {
|
|
1563
|
+
setEyeBrows(currentFace.eyebrows);
|
|
1564
|
+
}
|
|
1565
|
+
} catch (err) {
|
|
1566
|
+
console.warn("Failed to load PixiFace textures:", err);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
void loadAvatarTextures(face);
|
|
1570
|
+
return () => {
|
|
1571
|
+
cancelled = true;
|
|
1572
|
+
};
|
|
1573
|
+
}, [face]);
|
|
1574
|
+
if (pixiSettings?.eyeDisplay === "none") {
|
|
1575
|
+
return null;
|
|
1576
|
+
}
|
|
1577
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
|
|
1578
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
1579
|
+
"pixiContainer",
|
|
1580
|
+
{
|
|
1581
|
+
ref: leftEyeContainerRef,
|
|
1582
|
+
position: { x: positions.leftEyeX, y: positions.leftEyeY },
|
|
1583
|
+
label: "LeftEyeContainer",
|
|
1584
|
+
children: [
|
|
1585
|
+
eyeBrows === "Hugo" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
1586
|
+
"pixiGraphics",
|
|
1587
|
+
{
|
|
1588
|
+
ref: leftEyebrowRef,
|
|
1589
|
+
label: "LeftEyebrow",
|
|
1590
|
+
position: { x: 0, y: positions.eyebrowOffset },
|
|
1591
|
+
draw: drawEyebrow
|
|
1592
|
+
}
|
|
1593
|
+
),
|
|
1594
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
1595
|
+
"pixiSprite",
|
|
1596
|
+
{
|
|
1597
|
+
ref: leftEyeSpriteRef,
|
|
1598
|
+
label: "LeftEye",
|
|
1599
|
+
anchor: { x: 0.5, y: 0.5 },
|
|
1600
|
+
position: { x: 0, y: 0 },
|
|
1601
|
+
texture: leftEyeTexture,
|
|
1602
|
+
pivot: { x: 0.5, y: 0.5 }
|
|
1603
|
+
}
|
|
1604
|
+
),
|
|
1605
|
+
cheekTexture && pixiSettings?.cheekDisplay !== "none" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
1606
|
+
"pixiSprite",
|
|
1607
|
+
{
|
|
1608
|
+
ref: leftCheekRef,
|
|
1609
|
+
label: "LeftCheek",
|
|
1610
|
+
texture: cheekTexture,
|
|
1611
|
+
pivot: { x: 0.5, y: 0.5 },
|
|
1612
|
+
anchor: { x: 0.5, y: 0.5 },
|
|
1613
|
+
position: { x: 0, y: positions.cheekYOffset }
|
|
1614
|
+
}
|
|
1615
|
+
)
|
|
1616
|
+
]
|
|
1617
|
+
}
|
|
1618
|
+
),
|
|
1619
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
1620
|
+
"pixiContainer",
|
|
1621
|
+
{
|
|
1622
|
+
ref: rightEyeContainerRef,
|
|
1623
|
+
position: { x: positions.rightEyeX, y: positions.rightEyeY },
|
|
1624
|
+
label: "RightEyeContainer",
|
|
1625
|
+
children: [
|
|
1626
|
+
eyeBrows === "Hugo" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
1627
|
+
"pixiGraphics",
|
|
1628
|
+
{
|
|
1629
|
+
ref: rightEyebrowRef,
|
|
1630
|
+
label: "RightEyebrow",
|
|
1631
|
+
position: { x: 0, y: positions.eyebrowOffset },
|
|
1632
|
+
draw: drawEyebrow
|
|
1633
|
+
}
|
|
1634
|
+
),
|
|
1635
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
1636
|
+
"pixiSprite",
|
|
1637
|
+
{
|
|
1638
|
+
ref: rightEyeSpriteRef,
|
|
1639
|
+
label: "RightEye",
|
|
1640
|
+
position: { x: 0, y: 0 },
|
|
1641
|
+
pivot: { x: 0.5, y: 0.5 },
|
|
1642
|
+
anchor: { x: 0.5, y: 0.5 },
|
|
1643
|
+
texture: rightEyeTexture
|
|
1644
|
+
}
|
|
1645
|
+
),
|
|
1646
|
+
cheekTexture && pixiSettings?.cheekDisplay !== "none" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
1647
|
+
"pixiSprite",
|
|
1648
|
+
{
|
|
1649
|
+
ref: rightCheekRef,
|
|
1650
|
+
label: "RightCheek",
|
|
1651
|
+
texture: cheekTexture,
|
|
1652
|
+
pivot: { x: 0.5, y: 0.5 },
|
|
1653
|
+
anchor: { x: 0.5, y: 0.5 },
|
|
1654
|
+
position: { x: 0, y: positions.cheekYOffset }
|
|
1655
|
+
}
|
|
1656
|
+
)
|
|
1657
|
+
]
|
|
1658
|
+
}
|
|
1659
|
+
)
|
|
1660
|
+
] });
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
// src/components/FaceMouth.tsx
|
|
1664
|
+
var import_react4 = require("react");
|
|
1665
|
+
var import_pixi2 = require("pixi.js");
|
|
1666
|
+
var import_react5 = require("@pixi/react");
|
|
1667
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
1668
|
+
var ZIG_ZAG_POINTS = [
|
|
1669
|
+
{ x: 0, y: -10 },
|
|
1670
|
+
{ x: 10, y: 10 },
|
|
1671
|
+
{ x: 20, y: -10 },
|
|
1672
|
+
{ x: 30, y: 10 },
|
|
1673
|
+
{ x: 40, y: -10 },
|
|
1674
|
+
{ x: 50, y: 10 },
|
|
1675
|
+
{ x: 60, y: -10 },
|
|
1676
|
+
{ x: 70, y: 10 },
|
|
1677
|
+
{ x: 80, y: -10 },
|
|
1678
|
+
{ x: 90, y: 10 },
|
|
1679
|
+
{ x: 100, y: -10 },
|
|
1680
|
+
{ x: 110, y: 10 }
|
|
1681
|
+
];
|
|
1682
|
+
var SMILEY_POINTS = [
|
|
1683
|
+
{ x: -40, y: 40 },
|
|
1684
|
+
{ x: -30, y: 30 },
|
|
1685
|
+
{ x: -20, y: 20 },
|
|
1686
|
+
{ x: -10, y: 10 },
|
|
1687
|
+
{ x: 0, y: 0 },
|
|
1688
|
+
{ x: 10, y: 10 },
|
|
1689
|
+
{ x: 20, y: 20 },
|
|
1690
|
+
{ x: 30, y: 30 },
|
|
1691
|
+
{ x: 40, y: 40 }
|
|
1692
|
+
];
|
|
1693
|
+
function FaceMouth({
|
|
1694
|
+
scale,
|
|
1695
|
+
frozen = false
|
|
1696
|
+
}) {
|
|
1697
|
+
(0, import_react5.useExtend)({ Graphics: import_pixi2.Graphics });
|
|
1698
|
+
const talking = useFaceStore((state) => state.isSpeaking);
|
|
1699
|
+
const face = useFaceStore((state) => state.selectedFace);
|
|
1700
|
+
const pixiSettings = useFaceStore((state) => state.faceSettings.pixiSettings);
|
|
1701
|
+
const currentAmplitude = useFaceStore((state) => state.currentAmplitude);
|
|
1702
|
+
const amplitudeData = useFaceStore((state) => state.amplitudeData);
|
|
1703
|
+
const speechStartTime = useFaceStore((state) => state.speechStartTime);
|
|
1704
|
+
const talkingScaleRef = (0, import_react4.useRef)(1);
|
|
1705
|
+
const talkingTimeRef = (0, import_react4.useRef)(0);
|
|
1706
|
+
const talkingShape = (0, import_react4.useMemo)(
|
|
1707
|
+
() => face?.kind === "PixiFace" && face.type === "avatar" ? face.mouth : "Ellipse",
|
|
1708
|
+
[face]
|
|
1709
|
+
);
|
|
1710
|
+
const graphicsRef = (0, import_react4.useRef)(null);
|
|
1711
|
+
const getAmplitudeAtTime = (0, import_react4.useCallback)(
|
|
1712
|
+
(timeMs) => {
|
|
1713
|
+
if (!amplitudeData || amplitudeData.length === 0) return 0;
|
|
1714
|
+
const point = amplitudeData.find((p, i) => {
|
|
1715
|
+
const nextPoint = amplitudeData[i + 1];
|
|
1716
|
+
return p.time <= timeMs && (!nextPoint || nextPoint.time > timeMs);
|
|
1717
|
+
});
|
|
1718
|
+
return point ? point.value : 0;
|
|
1719
|
+
},
|
|
1720
|
+
[amplitudeData]
|
|
1721
|
+
);
|
|
1722
|
+
const drawMouth = (0, import_react4.useCallback)(
|
|
1723
|
+
(graphics) => {
|
|
1724
|
+
graphics.clear();
|
|
1725
|
+
const talkingScale = talkingScaleRef.current;
|
|
1726
|
+
const talkingTime = talkingTimeRef.current;
|
|
1727
|
+
switch (talkingShape) {
|
|
1728
|
+
case "Hugo": {
|
|
1729
|
+
const width = 40 * scale;
|
|
1730
|
+
const smileDepth = 20 * scale;
|
|
1731
|
+
const openHeight = talking ? talkingScale * 30 * scale : 0;
|
|
1732
|
+
const topLipLift = talking ? talkingScale * 10 * scale : 0;
|
|
1733
|
+
const centerY = scale * 240;
|
|
1734
|
+
graphics.moveTo(-width, centerY);
|
|
1735
|
+
graphics.quadraticCurveTo(
|
|
1736
|
+
0,
|
|
1737
|
+
centerY + smileDepth * 0.5 - topLipLift,
|
|
1738
|
+
width,
|
|
1739
|
+
centerY
|
|
1740
|
+
);
|
|
1741
|
+
graphics.quadraticCurveTo(
|
|
1742
|
+
0,
|
|
1743
|
+
centerY + smileDepth + openHeight * 1.5,
|
|
1744
|
+
-width,
|
|
1745
|
+
centerY
|
|
1746
|
+
);
|
|
1747
|
+
graphics.fill({ color: "black" });
|
|
1748
|
+
break;
|
|
1749
|
+
}
|
|
1750
|
+
case "Beak": {
|
|
1751
|
+
SMILEY_POINTS.forEach((eachPoint, index) => {
|
|
1752
|
+
const y = talkingScale * (Math.pow(40, 1.1) - Math.pow(Math.abs(eachPoint.x), 1.1)) + scale * 240;
|
|
1753
|
+
if (index === 0) {
|
|
1754
|
+
graphics.moveTo(eachPoint.x, y);
|
|
1755
|
+
}
|
|
1756
|
+
graphics.lineTo(eachPoint.x, y);
|
|
1757
|
+
});
|
|
1758
|
+
graphics.moveTo(SMILEY_POINTS[0].x, -1 * SMILEY_POINTS[0].y);
|
|
1759
|
+
SMILEY_POINTS.forEach((eachPoint, index) => {
|
|
1760
|
+
const y = -1 * talkingScale * (Math.pow(40, 1.1) - Math.pow(Math.abs(eachPoint.x), 1.1)) + scale * 240;
|
|
1761
|
+
if (index === 0) {
|
|
1762
|
+
graphics.moveTo(eachPoint.x, y);
|
|
1763
|
+
}
|
|
1764
|
+
graphics.lineTo(eachPoint.x, y);
|
|
1765
|
+
});
|
|
1766
|
+
break;
|
|
1767
|
+
}
|
|
1768
|
+
case "ZigZag": {
|
|
1769
|
+
ZIG_ZAG_POINTS.forEach((eachPoint, index) => {
|
|
1770
|
+
const x = eachPoint.x - ZIG_ZAG_POINTS[ZIG_ZAG_POINTS.length - 1].x / 2;
|
|
1771
|
+
const y = talking ? Math.sin(talkingTime * 0.3 + index * 5) * (10 * talkingScale) + scale * 240 : eachPoint.y + scale * 240;
|
|
1772
|
+
if (index === 0) {
|
|
1773
|
+
graphics.moveTo(x, y);
|
|
1774
|
+
}
|
|
1775
|
+
graphics.lineTo(x, y);
|
|
1776
|
+
});
|
|
1777
|
+
break;
|
|
1778
|
+
}
|
|
1779
|
+
case "Ellipse":
|
|
1780
|
+
default: {
|
|
1781
|
+
graphics.ellipse(
|
|
1782
|
+
0,
|
|
1783
|
+
scale * 240,
|
|
1784
|
+
scale * 60,
|
|
1785
|
+
talking ? 2 + talkingScale * scale * 15 : 4
|
|
1786
|
+
);
|
|
1787
|
+
graphics.fill({ color: "black" });
|
|
1788
|
+
break;
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
graphics.stroke({ color: "black", width: 2 });
|
|
1792
|
+
},
|
|
1793
|
+
[scale, talking, talkingShape]
|
|
1794
|
+
);
|
|
1795
|
+
(0, import_react5.useTick)((time) => {
|
|
1796
|
+
if (frozen) return;
|
|
1797
|
+
let targetScale;
|
|
1798
|
+
if (talking) {
|
|
1799
|
+
if (currentAmplitude > 0) {
|
|
1800
|
+
targetScale = Math.min(currentAmplitude * 1.5, 1);
|
|
1801
|
+
} else if (amplitudeData && speechStartTime) {
|
|
1802
|
+
const elapsed = performance.now() - speechStartTime;
|
|
1803
|
+
targetScale = Math.min(getAmplitudeAtTime(elapsed) * 1.5, 1);
|
|
1804
|
+
} else {
|
|
1805
|
+
targetScale = Math.abs(Math.sin(time.lastTime * 0.1));
|
|
1806
|
+
}
|
|
1807
|
+
talkingTimeRef.current = time.lastTime;
|
|
1808
|
+
} else {
|
|
1809
|
+
targetScale = 0;
|
|
1810
|
+
}
|
|
1811
|
+
const lerpAlpha = 0.2;
|
|
1812
|
+
talkingScaleRef.current += (targetScale - talkingScaleRef.current) * lerpAlpha;
|
|
1813
|
+
if (graphicsRef.current) {
|
|
1814
|
+
drawMouth(graphicsRef.current);
|
|
1815
|
+
}
|
|
1816
|
+
});
|
|
1817
|
+
if (pixiSettings?.mouthDisplay === "none") return null;
|
|
1818
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("pixiGraphics", { ref: graphicsRef, label: "Mouth", draw: drawMouth });
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
// src/components/FaceText.tsx
|
|
1822
|
+
var import_react7 = require("react");
|
|
1823
|
+
var import_pixi3 = require("pixi.js");
|
|
1824
|
+
var import_react8 = require("@pixi/react");
|
|
1825
|
+
|
|
1826
|
+
// src/helpers/emotion_mapper.ts
|
|
1827
|
+
var EMOTION_EMOTICONS = {
|
|
1828
|
+
Happy: ":D",
|
|
1829
|
+
Sad: ":(",
|
|
1830
|
+
Surprised: ":o",
|
|
1831
|
+
Neutral: ":)",
|
|
1832
|
+
Sleepy: ":z",
|
|
1833
|
+
Concerned: ":/"
|
|
1834
|
+
};
|
|
1835
|
+
var EMOTION_EMOJIS = {
|
|
1836
|
+
Happy: "\u{1F600}",
|
|
1837
|
+
Sad: "\u{1F622}",
|
|
1838
|
+
Surprised: "\u{1F62E}",
|
|
1839
|
+
Neutral: "\u{1F610}",
|
|
1840
|
+
Sleepy: "\u{1F634}",
|
|
1841
|
+
Concerned: "\u{1F61F}"
|
|
1842
|
+
};
|
|
1843
|
+
var EMOTION_ICON_FILES = {
|
|
1844
|
+
Neutral: "Neutral.png",
|
|
1845
|
+
Surprised: "Surprised.png",
|
|
1846
|
+
Happy: "Neutral.png",
|
|
1847
|
+
Sad: "Sad.png",
|
|
1848
|
+
Concerned: "Concerned.png",
|
|
1849
|
+
Sleepy: "Sleep.png"
|
|
1850
|
+
};
|
|
1851
|
+
function getEmotionIconPath(emotion, assetBaseUrl) {
|
|
1852
|
+
const base = normalizeAssetBaseUrl(assetBaseUrl);
|
|
1853
|
+
const file = EMOTION_ICON_FILES[emotion] ?? EMOTION_ICON_FILES.Neutral;
|
|
1854
|
+
return `${base}/Icons/${file}`;
|
|
1855
|
+
}
|
|
1856
|
+
var emotion_mapping = {
|
|
1857
|
+
Neutral: getEmotionIconPath("Neutral"),
|
|
1858
|
+
Surprised: getEmotionIconPath("Surprised"),
|
|
1859
|
+
Happy: getEmotionIconPath("Happy"),
|
|
1860
|
+
Sad: getEmotionIconPath("Sad"),
|
|
1861
|
+
Concerned: getEmotionIconPath("Concerned"),
|
|
1862
|
+
Sleepy: getEmotionIconPath("Sleepy")
|
|
1863
|
+
};
|
|
1864
|
+
|
|
1865
|
+
// src/hooks/useMessageVisualState.ts
|
|
1866
|
+
var import_react6 = require("react");
|
|
1867
|
+
function useMessageVisualState(isSpeaking, config = {}) {
|
|
1868
|
+
const {
|
|
1869
|
+
msPerChar = 50,
|
|
1870
|
+
persistenceDuration = 3e3,
|
|
1871
|
+
pauseBeforeNextMsg = 500
|
|
1872
|
+
} = config;
|
|
1873
|
+
const [displayText, setDisplayText] = (0, import_react6.useState)("");
|
|
1874
|
+
const [visualState, setVisualState] = (0, import_react6.useState)("IDLE");
|
|
1875
|
+
const stateRef = (0, import_react6.useRef)("IDLE");
|
|
1876
|
+
const displayTextRef = (0, import_react6.useRef)("");
|
|
1877
|
+
const currentMsgRef = (0, import_react6.useRef)(null);
|
|
1878
|
+
const nextMsgRef = (0, import_react6.useRef)(null);
|
|
1879
|
+
const timerRef = (0, import_react6.useRef)(0);
|
|
1880
|
+
const charTimerRef = (0, import_react6.useRef)(0);
|
|
1881
|
+
const typingIndexRef = (0, import_react6.useRef)(0);
|
|
1882
|
+
const startShowing = (0, import_react6.useCallback)(
|
|
1883
|
+
(speech) => {
|
|
1884
|
+
currentMsgRef.current = speech;
|
|
1885
|
+
nextMsgRef.current = null;
|
|
1886
|
+
stateRef.current = "SHOWING";
|
|
1887
|
+
setVisualState("SHOWING");
|
|
1888
|
+
typingIndexRef.current = 0;
|
|
1889
|
+
charTimerRef.current = 0;
|
|
1890
|
+
if (msPerChar <= 0) {
|
|
1891
|
+
setDisplayText(speech);
|
|
1892
|
+
displayTextRef.current = speech;
|
|
1893
|
+
typingIndexRef.current = speech.length;
|
|
1894
|
+
} else {
|
|
1895
|
+
setDisplayText("");
|
|
1896
|
+
displayTextRef.current = "";
|
|
1897
|
+
}
|
|
1898
|
+
},
|
|
1899
|
+
[msPerChar]
|
|
1900
|
+
);
|
|
1901
|
+
const queueMessage = (0, import_react6.useCallback)(
|
|
1902
|
+
(speech) => {
|
|
1903
|
+
if (stateRef.current === "IDLE" || stateRef.current === "PERSISTING") {
|
|
1904
|
+
startShowing(speech);
|
|
1905
|
+
return;
|
|
1906
|
+
}
|
|
1907
|
+
nextMsgRef.current = speech;
|
|
1908
|
+
},
|
|
1909
|
+
[startShowing]
|
|
1910
|
+
);
|
|
1911
|
+
const update = (0, import_react6.useCallback)(
|
|
1912
|
+
(ms) => {
|
|
1913
|
+
const prevState = stateRef.current;
|
|
1914
|
+
switch (stateRef.current) {
|
|
1915
|
+
case "IDLE":
|
|
1916
|
+
if (displayTextRef.current !== "") {
|
|
1917
|
+
setDisplayText("");
|
|
1918
|
+
displayTextRef.current = "";
|
|
1919
|
+
}
|
|
1920
|
+
if (nextMsgRef.current) {
|
|
1921
|
+
startShowing(nextMsgRef.current);
|
|
1922
|
+
}
|
|
1923
|
+
break;
|
|
1924
|
+
case "SHOWING":
|
|
1925
|
+
if (!currentMsgRef.current) {
|
|
1926
|
+
stateRef.current = "IDLE";
|
|
1927
|
+
break;
|
|
1928
|
+
}
|
|
1929
|
+
if (msPerChar <= 0) {
|
|
1930
|
+
if (nextMsgRef.current) {
|
|
1931
|
+
stateRef.current = "PAUSED";
|
|
1932
|
+
timerRef.current = 0;
|
|
1933
|
+
} else {
|
|
1934
|
+
stateRef.current = "WAITING_FOR_AUDIO";
|
|
1935
|
+
}
|
|
1936
|
+
break;
|
|
1937
|
+
}
|
|
1938
|
+
charTimerRef.current += ms;
|
|
1939
|
+
if (charTimerRef.current > msPerChar) {
|
|
1940
|
+
const fullText = currentMsgRef.current;
|
|
1941
|
+
const charCountToAdd = Math.floor(charTimerRef.current / msPerChar);
|
|
1942
|
+
charTimerRef.current %= msPerChar;
|
|
1943
|
+
const nextIndex = typingIndexRef.current + charCountToAdd;
|
|
1944
|
+
if (nextIndex >= fullText.length) {
|
|
1945
|
+
setDisplayText(fullText);
|
|
1946
|
+
displayTextRef.current = fullText;
|
|
1947
|
+
typingIndexRef.current = fullText.length;
|
|
1948
|
+
if (nextMsgRef.current) {
|
|
1949
|
+
stateRef.current = "PAUSED";
|
|
1950
|
+
timerRef.current = 0;
|
|
1951
|
+
} else {
|
|
1952
|
+
stateRef.current = "WAITING_FOR_AUDIO";
|
|
1953
|
+
}
|
|
1954
|
+
} else {
|
|
1955
|
+
typingIndexRef.current = nextIndex;
|
|
1956
|
+
const partial = fullText.substring(0, nextIndex);
|
|
1957
|
+
setDisplayText(partial);
|
|
1958
|
+
displayTextRef.current = partial;
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
break;
|
|
1962
|
+
case "WAITING_FOR_AUDIO":
|
|
1963
|
+
if (nextMsgRef.current) {
|
|
1964
|
+
stateRef.current = "PAUSED";
|
|
1965
|
+
timerRef.current = 0;
|
|
1966
|
+
break;
|
|
1967
|
+
}
|
|
1968
|
+
if (!isSpeaking) {
|
|
1969
|
+
stateRef.current = "PERSISTING";
|
|
1970
|
+
timerRef.current = 0;
|
|
1971
|
+
}
|
|
1972
|
+
break;
|
|
1973
|
+
case "PERSISTING":
|
|
1974
|
+
if (nextMsgRef.current) {
|
|
1975
|
+
stateRef.current = "PAUSED";
|
|
1976
|
+
timerRef.current = 0;
|
|
1977
|
+
break;
|
|
1978
|
+
}
|
|
1979
|
+
timerRef.current += ms;
|
|
1980
|
+
if (timerRef.current > persistenceDuration) {
|
|
1981
|
+
stateRef.current = "IDLE";
|
|
1982
|
+
}
|
|
1983
|
+
break;
|
|
1984
|
+
case "PAUSED":
|
|
1985
|
+
timerRef.current += ms;
|
|
1986
|
+
if (timerRef.current > pauseBeforeNextMsg) {
|
|
1987
|
+
if (nextMsgRef.current) {
|
|
1988
|
+
startShowing(nextMsgRef.current);
|
|
1989
|
+
} else {
|
|
1990
|
+
stateRef.current = "WAITING_FOR_AUDIO";
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
break;
|
|
1994
|
+
}
|
|
1995
|
+
if (stateRef.current !== prevState) {
|
|
1996
|
+
setVisualState(stateRef.current);
|
|
1997
|
+
}
|
|
1998
|
+
},
|
|
1999
|
+
[
|
|
2000
|
+
isSpeaking,
|
|
2001
|
+
msPerChar,
|
|
2002
|
+
persistenceDuration,
|
|
2003
|
+
pauseBeforeNextMsg,
|
|
2004
|
+
startShowing
|
|
2005
|
+
]
|
|
2006
|
+
);
|
|
2007
|
+
return {
|
|
2008
|
+
displayText,
|
|
2009
|
+
visualState,
|
|
2010
|
+
queueMessage,
|
|
2011
|
+
update
|
|
2012
|
+
};
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
// src/components/FaceText.tsx
|
|
2016
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
2017
|
+
var FONT_FAMILY = "Courier New, Courier, monospace";
|
|
2018
|
+
var TEXT_PERSISTENCE_DURATION = 3e3;
|
|
2019
|
+
var MS_PER_CHAR = 50;
|
|
2020
|
+
var PAUSE_BEFORE_NEXT_MSG = 500;
|
|
2021
|
+
function FaceText({ scale }) {
|
|
2022
|
+
(0, import_react8.useExtend)({ Text: import_pixi3.Text });
|
|
2023
|
+
const lastMessage = useFaceStore((state) => state.lastMessage);
|
|
2024
|
+
const isSpeaking = useFaceStore((state) => state.isSpeaking);
|
|
2025
|
+
const selectedFace = useFaceStore((state) => state.selectedFace);
|
|
2026
|
+
const pixiSettings = useFaceStore((state) => state.faceSettings.pixiSettings);
|
|
2027
|
+
const emotionType = (0, import_react7.useMemo)(() => {
|
|
2028
|
+
if (pixiSettings?.emotionDisplay) {
|
|
2029
|
+
return pixiSettings.emotionDisplay;
|
|
2030
|
+
}
|
|
2031
|
+
if (selectedFace?.kind === "PixiFace" && selectedFace.type === "text") {
|
|
2032
|
+
return selectedFace.showEmotion;
|
|
2033
|
+
}
|
|
2034
|
+
return "emoticon";
|
|
2035
|
+
}, [pixiSettings?.emotionDisplay, selectedFace]);
|
|
2036
|
+
const { displayText, queueMessage, update } = useMessageVisualState(
|
|
2037
|
+
isSpeaking,
|
|
2038
|
+
{
|
|
2039
|
+
msPerChar: MS_PER_CHAR,
|
|
2040
|
+
persistenceDuration: TEXT_PERSISTENCE_DURATION,
|
|
2041
|
+
pauseBeforeNextMsg: PAUSE_BEFORE_NEXT_MSG
|
|
2042
|
+
}
|
|
2043
|
+
);
|
|
2044
|
+
(0, import_react7.useEffect)(() => {
|
|
2045
|
+
queueMessage(lastMessage.speech);
|
|
2046
|
+
}, [lastMessage.speech, queueMessage]);
|
|
2047
|
+
(0, import_react8.useTick)((delta) => {
|
|
2048
|
+
update(delta.deltaMS);
|
|
2049
|
+
});
|
|
2050
|
+
const emotionStyle = (0, import_react7.useMemo)(
|
|
2051
|
+
() => new import_pixi3.TextStyle({
|
|
2052
|
+
fontFamily: FONT_FAMILY,
|
|
2053
|
+
fontSize: Math.max(12, 48 * scale),
|
|
2054
|
+
fill: "black",
|
|
2055
|
+
align: "center"
|
|
2056
|
+
}),
|
|
2057
|
+
[scale]
|
|
2058
|
+
);
|
|
2059
|
+
const bodyStyle = (0, import_react7.useMemo)(
|
|
2060
|
+
() => new import_pixi3.TextStyle({
|
|
2061
|
+
fontFamily: FONT_FAMILY,
|
|
2062
|
+
fontSize: Math.max(12, 48 * scale),
|
|
2063
|
+
fill: "black",
|
|
2064
|
+
align: "center",
|
|
2065
|
+
wordWrap: true,
|
|
2066
|
+
wordWrapWidth: Math.max(200, 800 * scale)
|
|
2067
|
+
}),
|
|
2068
|
+
[scale]
|
|
2069
|
+
);
|
|
2070
|
+
const emotionSymbol = emotionType === "emoji" ? EMOTION_EMOJIS[lastMessage.emotion] : EMOTION_EMOTICONS[lastMessage.emotion];
|
|
2071
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
|
|
2072
|
+
emotionType !== "none" && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
2073
|
+
"pixiText",
|
|
2074
|
+
{
|
|
2075
|
+
anchor: 0.5,
|
|
2076
|
+
x: 0,
|
|
2077
|
+
y: -50 * scale,
|
|
2078
|
+
text: emotionSymbol,
|
|
2079
|
+
style: emotionStyle
|
|
2080
|
+
}
|
|
2081
|
+
),
|
|
2082
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
2083
|
+
"pixiText",
|
|
2084
|
+
{
|
|
2085
|
+
text: displayText,
|
|
2086
|
+
anchor: 0.5,
|
|
2087
|
+
x: 0,
|
|
2088
|
+
y: 0,
|
|
2089
|
+
style: bodyStyle
|
|
2090
|
+
}
|
|
2091
|
+
)
|
|
2092
|
+
] });
|
|
2093
|
+
}
|
|
2094
|
+
|
|
2095
|
+
// src/components/FaceBackground.tsx
|
|
2096
|
+
var import_react9 = require("react");
|
|
2097
|
+
var import_pixi4 = require("pixi.js");
|
|
2098
|
+
var import_react10 = require("@pixi/react");
|
|
2099
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
2100
|
+
function createRadialGradientTexture(size) {
|
|
2101
|
+
if (typeof document === "undefined") {
|
|
2102
|
+
return import_pixi4.Texture.EMPTY;
|
|
2103
|
+
}
|
|
2104
|
+
const canvas = document.createElement("canvas");
|
|
2105
|
+
canvas.width = size;
|
|
2106
|
+
canvas.height = size;
|
|
2107
|
+
const ctx = canvas.getContext("2d");
|
|
2108
|
+
if (!ctx) return import_pixi4.Texture.EMPTY;
|
|
2109
|
+
const gradient = ctx.createRadialGradient(
|
|
2110
|
+
size / 2,
|
|
2111
|
+
size / 2,
|
|
2112
|
+
0,
|
|
2113
|
+
size / 2,
|
|
2114
|
+
size / 2,
|
|
2115
|
+
size / 2
|
|
2116
|
+
);
|
|
2117
|
+
gradient.addColorStop(0, "rgba(255, 255, 255, 0.7)");
|
|
2118
|
+
gradient.addColorStop(0.4, "rgba(255, 255, 255, 0.2)");
|
|
2119
|
+
gradient.addColorStop(1, "rgba(255, 255, 255, 0)");
|
|
2120
|
+
ctx.fillStyle = gradient;
|
|
2121
|
+
ctx.fillRect(0, 0, size, size);
|
|
2122
|
+
return import_pixi4.Texture.from(canvas);
|
|
2123
|
+
}
|
|
2124
|
+
function FaceBackground() {
|
|
2125
|
+
(0, import_react10.useExtend)({ Sprite: import_pixi4.Sprite });
|
|
2126
|
+
const { app, isInitialised } = (0, import_react10.useApplication)();
|
|
2127
|
+
const texture = (0, import_react9.useMemo)(() => createRadialGradientTexture(1024), []);
|
|
2128
|
+
if (!app || !isInitialised || !app.renderer) return null;
|
|
2129
|
+
const screenWidth = app.renderer.screen && app.renderer.screen.width || app.renderer.width || 2e3;
|
|
2130
|
+
const screenHeight = app.renderer.screen && app.renderer.screen.height || app.renderer.height || 2e3;
|
|
2131
|
+
const size = Math.max(screenWidth, screenHeight) * 2;
|
|
2132
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
2133
|
+
"pixiSprite",
|
|
2134
|
+
{
|
|
2135
|
+
texture,
|
|
2136
|
+
anchor: 0.5,
|
|
2137
|
+
width: size,
|
|
2138
|
+
height: size,
|
|
2139
|
+
x: 0,
|
|
2140
|
+
y: 0,
|
|
2141
|
+
blendMode: "screen",
|
|
2142
|
+
alpha: 0.5
|
|
2143
|
+
}
|
|
2144
|
+
);
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
// src/components/FaceContainer.tsx
|
|
2148
|
+
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
2149
|
+
function stepRendererBackgroundColor(renderer, targetHex) {
|
|
2150
|
+
try {
|
|
2151
|
+
const currentHex = renderer.background.color.toHex();
|
|
2152
|
+
if (currentHex.toLowerCase() === targetHex.toLowerCase()) {
|
|
2153
|
+
return;
|
|
2154
|
+
}
|
|
2155
|
+
const interpolated = interpolateColor(currentHex, targetHex, 0.1);
|
|
2156
|
+
if (interpolated !== null) {
|
|
2157
|
+
renderer.background.color = rgbaToHex(interpolated);
|
|
2158
|
+
}
|
|
2159
|
+
} catch (err) {
|
|
2160
|
+
void err;
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
function FaceContainer({ frozen = false }) {
|
|
2164
|
+
(0, import_react12.useExtend)({ Container: import_pixi5.Container });
|
|
2165
|
+
const settings = useFaceStore((state) => state.faceSettings);
|
|
2166
|
+
const selectedFace = useFaceStore((state) => state.selectedFace);
|
|
2167
|
+
const lastMessage = useFaceStore((state) => state.lastMessage);
|
|
2168
|
+
const { app, isInitialised } = (0, import_react12.useApplication)();
|
|
2169
|
+
const [layout, setLayout] = (0, import_react11.useState)({
|
|
2170
|
+
landscape: true,
|
|
2171
|
+
scale: 1,
|
|
2172
|
+
x: 400,
|
|
2173
|
+
y: 300
|
|
2174
|
+
});
|
|
2175
|
+
const isAvatar = selectedFace?.kind === "PixiFace" && selectedFace.type === "avatar";
|
|
2176
|
+
(0, import_react11.useEffect)(() => {
|
|
2177
|
+
if (!app || !isInitialised || !app.renderer) return;
|
|
2178
|
+
const updateLayout = () => {
|
|
2179
|
+
if (!app?.renderer) return;
|
|
2180
|
+
const width = app.renderer.screen && app.renderer.screen.width || app.renderer.width || 800;
|
|
2181
|
+
const height = app.renderer.screen && app.renderer.screen.height || app.renderer.height || 600;
|
|
2182
|
+
const isLandscape = width > height;
|
|
2183
|
+
const baseScale = isLandscape ? width / 1200 : height / 1200;
|
|
2184
|
+
const effectiveLandscape = settings.autoRotate ? isLandscape : true;
|
|
2185
|
+
setLayout({
|
|
2186
|
+
landscape: effectiveLandscape,
|
|
2187
|
+
scale: baseScale,
|
|
2188
|
+
x: width / 2,
|
|
2189
|
+
y: height / 2
|
|
2190
|
+
});
|
|
2191
|
+
};
|
|
2192
|
+
updateLayout();
|
|
2193
|
+
const onResize = () => {
|
|
2194
|
+
if (app?.renderer) {
|
|
2195
|
+
updateLayout();
|
|
2196
|
+
}
|
|
2197
|
+
};
|
|
2198
|
+
window.addEventListener("resize", onResize);
|
|
2199
|
+
window.addEventListener("orientationchange", onResize);
|
|
2200
|
+
app.renderer.on("resize", onResize);
|
|
2201
|
+
return () => {
|
|
2202
|
+
window.removeEventListener("resize", onResize);
|
|
2203
|
+
window.removeEventListener("orientationchange", onResize);
|
|
2204
|
+
if (app?.renderer) {
|
|
2205
|
+
app.renderer.off("resize", onResize);
|
|
2206
|
+
}
|
|
2207
|
+
};
|
|
2208
|
+
}, [app, isInitialised, settings.autoRotate]);
|
|
2209
|
+
(0, import_react12.useTick)(() => {
|
|
2210
|
+
if (app?.renderer && lastMessage) {
|
|
2211
|
+
const targetColor = color_mapping[lastMessage.color] ?? color_mapping["Light Blue"];
|
|
2212
|
+
stepRendererBackgroundColor(app.renderer, targetColor);
|
|
2213
|
+
}
|
|
2214
|
+
});
|
|
2215
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
|
|
2216
|
+
"pixiContainer",
|
|
2217
|
+
{
|
|
2218
|
+
label: "FaceContainer",
|
|
2219
|
+
position: {
|
|
2220
|
+
x: layout.x + (settings.offset?.x ?? 0),
|
|
2221
|
+
y: layout.y + (settings.offset?.y ?? 0)
|
|
2222
|
+
},
|
|
2223
|
+
pivot: { x: 0.5, y: 0.5 },
|
|
2224
|
+
angle: layout.landscape ? settings.rotation : 90 + settings.rotation,
|
|
2225
|
+
children: [
|
|
2226
|
+
isAvatar && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(FaceBackground, {}),
|
|
2227
|
+
selectedFace?.kind === "PixiFace" && selectedFace.type === "text" ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(FaceText, { scale: layout.scale * settings.zoom }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_jsx_runtime6.Fragment, { children: [
|
|
2228
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(FaceEyes, { scale: layout.scale * settings.zoom, frozen }),
|
|
2229
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(FaceMouth, { scale: layout.scale * settings.zoom, frozen })
|
|
2230
|
+
] })
|
|
2231
|
+
]
|
|
2232
|
+
}
|
|
2233
|
+
);
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
// src/components/FaceFullScreenButton.tsx
|
|
2237
|
+
var import_react13 = require("react");
|
|
2238
|
+
var import_react14 = require("@pixi/react");
|
|
2239
|
+
var import_pixi6 = require("pixi.js");
|
|
2240
|
+
var import_core = require("@peerbots/core");
|
|
2241
|
+
var import_jsx_runtime7 = require("react/jsx-runtime");
|
|
2242
|
+
function FaceFullScreenButton() {
|
|
2243
|
+
(0, import_react14.useExtend)({ Graphics: import_pixi6.Graphics, Container: import_pixi6.Container });
|
|
2244
|
+
const { app } = (0, import_react14.useApplication)();
|
|
2245
|
+
const [fullScreen, setFullScreen] = (0, import_react13.useState)(false);
|
|
2246
|
+
(0, import_react13.useEffect)(() => {
|
|
2247
|
+
if (typeof document === "undefined") {
|
|
2248
|
+
return;
|
|
2249
|
+
}
|
|
2250
|
+
const handleFullscreenChange = () => {
|
|
2251
|
+
setFullScreen(Boolean(document.fullscreenElement));
|
|
2252
|
+
};
|
|
2253
|
+
document.addEventListener("fullscreenchange", handleFullscreenChange);
|
|
2254
|
+
return () => {
|
|
2255
|
+
document.removeEventListener("fullscreenchange", handleFullscreenChange);
|
|
2256
|
+
};
|
|
2257
|
+
}, []);
|
|
2258
|
+
const toggleFullScreen = (0, import_react13.useCallback)(() => {
|
|
2259
|
+
if (typeof document === "undefined") {
|
|
2260
|
+
return;
|
|
2261
|
+
}
|
|
2262
|
+
if (fullScreen) {
|
|
2263
|
+
void document.exitFullscreen().catch((err) => {
|
|
2264
|
+
console.warn("Exit fullscreen error:", err);
|
|
2265
|
+
});
|
|
2266
|
+
} else {
|
|
2267
|
+
const container = document.getElementById("face-view-container");
|
|
2268
|
+
if (container) {
|
|
2269
|
+
container.requestFullscreen().catch((err) => {
|
|
2270
|
+
console.warn("Container fullscreen error:", err);
|
|
2271
|
+
if (app?.canvas) {
|
|
2272
|
+
void app.canvas.requestFullscreen().catch((canvasErr) => {
|
|
2273
|
+
console.warn("Canvas fullscreen error:", canvasErr);
|
|
2274
|
+
});
|
|
2275
|
+
}
|
|
2276
|
+
});
|
|
2277
|
+
} else if (app?.canvas) {
|
|
2278
|
+
void app.canvas.requestFullscreen().catch((err) => {
|
|
2279
|
+
console.warn("Canvas fullscreen error:", err);
|
|
2280
|
+
});
|
|
2281
|
+
}
|
|
2282
|
+
}
|
|
2283
|
+
}, [app, fullScreen]);
|
|
2284
|
+
const drawIcon = (0, import_react13.useCallback)(
|
|
2285
|
+
(graphics) => {
|
|
2286
|
+
graphics.clear();
|
|
2287
|
+
const pathD = fullScreen ? import_core.iconPaths.arrowsPointingIn : import_core.iconPaths.arrowsPointingOut;
|
|
2288
|
+
graphics.svg(`
|
|
2289
|
+
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="black">
|
|
2290
|
+
<path strokeLinecap="round" strokeLinejoin="round" d="${pathD}" />
|
|
2291
|
+
</svg>
|
|
2292
|
+
`);
|
|
2293
|
+
},
|
|
2294
|
+
[fullScreen]
|
|
2295
|
+
);
|
|
2296
|
+
return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
2297
|
+
"pixiContainer",
|
|
2298
|
+
{
|
|
2299
|
+
label: "FullScreenToggle",
|
|
2300
|
+
x: 20,
|
|
2301
|
+
y: 20,
|
|
2302
|
+
eventMode: "static",
|
|
2303
|
+
onPointerDown: toggleFullScreen,
|
|
2304
|
+
hitArea: new import_pixi6.Rectangle(0, 0, 50, 50),
|
|
2305
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
2306
|
+
"pixiGraphics",
|
|
2307
|
+
{
|
|
2308
|
+
onPointerDown: toggleFullScreen,
|
|
2309
|
+
eventMode: "static",
|
|
2310
|
+
draw: drawIcon
|
|
2311
|
+
}
|
|
2312
|
+
)
|
|
2313
|
+
}
|
|
2314
|
+
);
|
|
2315
|
+
}
|
|
2316
|
+
|
|
2317
|
+
// src/components/FaceScrollToTopButton.tsx
|
|
2318
|
+
var import_react15 = require("react");
|
|
2319
|
+
var import_react16 = require("@pixi/react");
|
|
2320
|
+
var import_pixi7 = require("pixi.js");
|
|
2321
|
+
var import_core2 = require("@peerbots/core");
|
|
2322
|
+
var import_jsx_runtime8 = require("react/jsx-runtime");
|
|
2323
|
+
function FaceScrollToTopButton() {
|
|
2324
|
+
(0, import_react16.useExtend)({ Graphics: import_pixi7.Graphics, Container: import_pixi7.Container });
|
|
2325
|
+
const [xPos, setXPos] = (0, import_react15.useState)(
|
|
2326
|
+
() => typeof window !== "undefined" ? window.innerWidth - 50 : 750
|
|
2327
|
+
);
|
|
2328
|
+
const scrollToTop = (0, import_react15.useCallback)(() => {
|
|
2329
|
+
if (typeof window !== "undefined") {
|
|
2330
|
+
window.scrollTo(0, 0);
|
|
2331
|
+
}
|
|
2332
|
+
}, []);
|
|
2333
|
+
(0, import_react15.useLayoutEffect)(() => {
|
|
2334
|
+
if (typeof window === "undefined") {
|
|
2335
|
+
return;
|
|
2336
|
+
}
|
|
2337
|
+
const calculatePositionBasedOnWindow = () => {
|
|
2338
|
+
setXPos(window.innerWidth - 50);
|
|
2339
|
+
};
|
|
2340
|
+
const orientation = typeof screen !== "undefined" ? screen.orientation : void 0;
|
|
2341
|
+
orientation?.addEventListener("change", calculatePositionBasedOnWindow);
|
|
2342
|
+
window.addEventListener("resize", calculatePositionBasedOnWindow);
|
|
2343
|
+
return () => {
|
|
2344
|
+
orientation?.removeEventListener("change", calculatePositionBasedOnWindow);
|
|
2345
|
+
window.removeEventListener("resize", calculatePositionBasedOnWindow);
|
|
2346
|
+
};
|
|
2347
|
+
}, []);
|
|
2348
|
+
const drawChevron = (0, import_react15.useCallback)((graphics) => {
|
|
2349
|
+
graphics.clear();
|
|
2350
|
+
graphics.svg(`
|
|
2351
|
+
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="black">
|
|
2352
|
+
<path strokeLinecap="round" strokeLinejoin="round" d="${import_core2.iconPaths.chevronUpSmall}" />
|
|
2353
|
+
</svg>
|
|
2354
|
+
`);
|
|
2355
|
+
}, []);
|
|
2356
|
+
return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
2357
|
+
"pixiContainer",
|
|
2358
|
+
{
|
|
2359
|
+
label: "ScrollToTopButton",
|
|
2360
|
+
x: xPos,
|
|
2361
|
+
y: 20,
|
|
2362
|
+
eventMode: "static",
|
|
2363
|
+
onPointerDown: scrollToTop,
|
|
2364
|
+
hitArea: new import_pixi7.Rectangle(0, 0, 50, 50),
|
|
2365
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
2366
|
+
"pixiGraphics",
|
|
2367
|
+
{
|
|
2368
|
+
onPointerDown: scrollToTop,
|
|
2369
|
+
eventMode: "static",
|
|
2370
|
+
draw: drawChevron
|
|
2371
|
+
}
|
|
2372
|
+
)
|
|
2373
|
+
}
|
|
2374
|
+
);
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2377
|
+
// src/components/PixiFaceView.tsx
|
|
2378
|
+
var import_jsx_runtime9 = require("react/jsx-runtime");
|
|
2379
|
+
function PixiFaceView({
|
|
2380
|
+
isPreview = false,
|
|
2381
|
+
frozen = false
|
|
2382
|
+
} = {}) {
|
|
2383
|
+
(0, import_react18.useExtend)({ Container: import_pixi8.Container, Graphics: import_pixi8.Graphics, Sprite: import_pixi8.Sprite, Text: import_pixi8.Text });
|
|
2384
|
+
const settings = useFaceStore((state) => state.faceSettings);
|
|
2385
|
+
const currentStore = useFaceStoreInstance();
|
|
2386
|
+
const [containerEl, setContainerEl] = (0, import_react17.useState)(null);
|
|
2387
|
+
return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
|
2388
|
+
"div",
|
|
2389
|
+
{
|
|
2390
|
+
ref: setContainerEl,
|
|
2391
|
+
className: "pbf:w-full pbf:h-full pbf:relative pbf:overflow-hidden",
|
|
2392
|
+
"data-testid": "pixi-face-view",
|
|
2393
|
+
children: containerEl ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
|
2394
|
+
import_react18.Application,
|
|
2395
|
+
{
|
|
2396
|
+
resizeTo: containerEl,
|
|
2397
|
+
backgroundColor: "#ABE3FA",
|
|
2398
|
+
className: "pbf:w-full pbf:h-full pbf:block",
|
|
2399
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(FaceStoreContext.Provider, { value: currentStore, children: [
|
|
2400
|
+
!isPreview && settings.showFullScreenButton && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(FaceFullScreenButton, {}),
|
|
2401
|
+
!isPreview && settings.showScrollButton && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(FaceScrollToTopButton, {}),
|
|
2402
|
+
/* @__PURE__ */ (0, import_jsx_runtime9.jsx)(FaceContainer, { frozen })
|
|
2403
|
+
] })
|
|
2404
|
+
}
|
|
2405
|
+
) : null
|
|
2406
|
+
}
|
|
2407
|
+
);
|
|
2408
|
+
}
|
|
2409
|
+
var PixiFaceView_default = PixiFaceView;
|
|
2410
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
2411
|
+
0 && (module.exports = {
|
|
2412
|
+
FaceBackground,
|
|
2413
|
+
FaceContainer,
|
|
2414
|
+
FaceEyes,
|
|
2415
|
+
FaceFullScreenButton,
|
|
2416
|
+
FaceMouth,
|
|
2417
|
+
FaceScrollToTopButton,
|
|
2418
|
+
FaceText,
|
|
2419
|
+
PixiFaceView
|
|
2420
|
+
});
|
|
2421
|
+
//# sourceMappingURL=pixi.cjs.map
|