virtual-human-cf 1.2.0 → 1.3.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.
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { defineComponent as
|
|
2
|
-
const
|
|
1
|
+
import { defineComponent as $, ref as b, watch as C, onMounted as M, onUnmounted as R, openBlock as W, createBlock as J, Transition as L, withCtx as j, createElementBlock as G, normalizeStyle as K, normalizeClass as Q, createElementVNode as F, createCommentVNode as X, renderSlot as Y } from "vue";
|
|
2
|
+
const Z = ["src", "muted"], ee = /* @__PURE__ */ $({
|
|
3
3
|
__name: "VirtualHumanPersona",
|
|
4
4
|
props: {
|
|
5
5
|
// 视频源URL
|
|
@@ -49,88 +49,88 @@ const M = ["src", "muted"], z = /* @__PURE__ */ H({
|
|
|
49
49
|
}
|
|
50
50
|
},
|
|
51
51
|
emits: ["update:isPlaying", "ended", "update:visible"],
|
|
52
|
-
setup(
|
|
53
|
-
const
|
|
54
|
-
if (
|
|
52
|
+
setup(c, { emit: S }) {
|
|
53
|
+
const n = c, a = S, s = b(null), P = b(null), e = b(null), p = b(!1), d = () => {
|
|
54
|
+
if (e.value && e.value.close(), !(!n.wsUrl || !n.screenClientId))
|
|
55
55
|
try {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
},
|
|
56
|
+
const l = new URL(n.wsUrl);
|
|
57
|
+
l.searchParams.append("sessionId", n.screenClientId + "-persona"), e.value = new WebSocket(l.toString()), e.value.onopen = () => {
|
|
58
|
+
p.value = !0, console.log(`[VirtualHumanPersona] Connected to ${n.wsUrl} for session ${n.screenClientId}-persona`);
|
|
59
|
+
}, e.value.onmessage = (i) => {
|
|
60
60
|
try {
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
} catch (
|
|
64
|
-
console.error("[VirtualHumanPersona] Failed to parse message:",
|
|
61
|
+
const u = JSON.parse(i.data);
|
|
62
|
+
v(u);
|
|
63
|
+
} catch (u) {
|
|
64
|
+
console.error("[VirtualHumanPersona] Failed to parse message:", i.data, u);
|
|
65
65
|
}
|
|
66
|
-
},
|
|
67
|
-
console.error("[VirtualHumanPersona] WebSocket error:",
|
|
68
|
-
},
|
|
69
|
-
|
|
66
|
+
}, e.value.onerror = (i) => {
|
|
67
|
+
console.error("[VirtualHumanPersona] WebSocket error:", i);
|
|
68
|
+
}, e.value.onclose = () => {
|
|
69
|
+
p.value = !1, console.log("[VirtualHumanPersona] WebSocket disconnected");
|
|
70
70
|
};
|
|
71
|
-
} catch (
|
|
72
|
-
console.error("[VirtualHumanPersona] Failed to initialize WebSocket:",
|
|
71
|
+
} catch (l) {
|
|
72
|
+
console.error("[VirtualHumanPersona] Failed to initialize WebSocket:", l);
|
|
73
73
|
}
|
|
74
|
-
},
|
|
75
|
-
const { type:
|
|
76
|
-
|
|
74
|
+
}, v = (l) => {
|
|
75
|
+
const { type: i, action: u } = l;
|
|
76
|
+
i === "control" && (u === "play" || u === "resume" ? (a("update:isPlaying", !0), a("update:visible", !0)) : u === "pause" ? (a("update:isPlaying", !1), a("update:visible", !0)) : u === "stop" && (a("update:isPlaying", !1), a("update:visible", !1), s.value && (s.value.currentTime = 0)));
|
|
77
77
|
};
|
|
78
|
-
C(() =>
|
|
79
|
-
|
|
80
|
-
}), C(() =>
|
|
81
|
-
|
|
82
|
-
}), C(() =>
|
|
83
|
-
|
|
78
|
+
C(() => n.screenClientId, () => {
|
|
79
|
+
n.screenClientId && n.wsUrl && d();
|
|
80
|
+
}), C(() => n.wsUrl, () => {
|
|
81
|
+
n.screenClientId && n.wsUrl && d();
|
|
82
|
+
}), C(() => n.isPlaying, (l) => {
|
|
83
|
+
s.value && (l ? s.value.play().catch((i) => console.error("Video play failed:", i)) : s.value.pause());
|
|
84
84
|
});
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
},
|
|
90
|
-
|
|
85
|
+
const y = () => {
|
|
86
|
+
n.isPlaying || a("update:isPlaying", !0);
|
|
87
|
+
}, w = () => {
|
|
88
|
+
n.isPlaying && a("update:isPlaying", !1);
|
|
89
|
+
}, h = () => {
|
|
90
|
+
a("update:isPlaying", !1), a("ended");
|
|
91
91
|
};
|
|
92
|
-
return
|
|
93
|
-
|
|
94
|
-
}),
|
|
95
|
-
|
|
96
|
-
}), (
|
|
97
|
-
default:
|
|
98
|
-
|
|
92
|
+
return M(() => {
|
|
93
|
+
n.isPlaying && s.value && s.value.play().catch((l) => console.error("Video play failed:", l)), n.screenClientId && n.wsUrl && d();
|
|
94
|
+
}), R(() => {
|
|
95
|
+
e.value && e.value.close();
|
|
96
|
+
}), (l, i) => (W(), J(L, { name: "fade" }, {
|
|
97
|
+
default: j(() => [
|
|
98
|
+
c.visible ? (W(), G("div", {
|
|
99
99
|
key: 0,
|
|
100
|
-
class:
|
|
101
|
-
style:
|
|
100
|
+
class: Q(["virtual-human-container", { "is-dark": c.isDark }]),
|
|
101
|
+
style: K(c.styles)
|
|
102
102
|
}, [
|
|
103
|
-
|
|
103
|
+
F("div", {
|
|
104
104
|
class: "video-wrapper",
|
|
105
105
|
ref_key: "wrapperRef",
|
|
106
|
-
ref:
|
|
106
|
+
ref: P
|
|
107
107
|
}, [
|
|
108
|
-
|
|
108
|
+
F("video", {
|
|
109
109
|
ref_key: "videoRef",
|
|
110
|
-
ref:
|
|
111
|
-
src:
|
|
110
|
+
ref: s,
|
|
111
|
+
src: c.videoSrc,
|
|
112
112
|
class: "persona-video",
|
|
113
|
-
muted:
|
|
113
|
+
muted: c.muted,
|
|
114
114
|
playsinline: "",
|
|
115
115
|
loop: "",
|
|
116
116
|
autoPlay: "",
|
|
117
117
|
disablePictureInPicture: "",
|
|
118
|
-
onPlay:
|
|
119
|
-
onPause:
|
|
120
|
-
onEnded:
|
|
121
|
-
}, null, 40,
|
|
118
|
+
onPlay: y,
|
|
119
|
+
onPause: w,
|
|
120
|
+
onEnded: h
|
|
121
|
+
}, null, 40, Z)
|
|
122
122
|
], 512)
|
|
123
|
-
], 6)) :
|
|
123
|
+
], 6)) : X("", !0)
|
|
124
124
|
]),
|
|
125
125
|
_: 1
|
|
126
126
|
}));
|
|
127
127
|
}
|
|
128
|
-
}),
|
|
129
|
-
const
|
|
130
|
-
for (const [
|
|
131
|
-
|
|
132
|
-
return
|
|
133
|
-
},
|
|
128
|
+
}), te = (c, S) => {
|
|
129
|
+
const n = c.__vccOpts || c;
|
|
130
|
+
for (const [a, s] of S)
|
|
131
|
+
n[a] = s;
|
|
132
|
+
return n;
|
|
133
|
+
}, ne = /* @__PURE__ */ te(ee, [["__scopeId", "data-v-bf23f155"]]), ae = /* @__PURE__ */ $({
|
|
134
134
|
__name: "VirtualHumanEventAdapter",
|
|
135
135
|
props: {
|
|
136
136
|
// 屏幕客户端ID
|
|
@@ -145,114 +145,157 @@ const M = ["src", "muted"], z = /* @__PURE__ */ H({
|
|
|
145
145
|
}
|
|
146
146
|
},
|
|
147
147
|
emits: ["eventNotifaction", "controlEvent", "end", "pause", "connected", "error", "playComplete"],
|
|
148
|
-
setup(
|
|
149
|
-
const
|
|
150
|
-
let
|
|
151
|
-
const
|
|
152
|
-
|
|
148
|
+
setup(c, { emit: S }) {
|
|
149
|
+
const n = c, a = S, s = b(null), P = b(!1);
|
|
150
|
+
let e = null, p = 0, d = !1, v = 0, y = !1;
|
|
151
|
+
const w = /* @__PURE__ */ new Map(), h = /* @__PURE__ */ new Set(), l = /* @__PURE__ */ new Map();
|
|
152
|
+
let i = null;
|
|
153
|
+
const u = (t) => {
|
|
154
|
+
if (typeof t == "number" && Number.isFinite(t)) return t;
|
|
155
|
+
if (typeof t == "string" && t.trim() !== "") {
|
|
156
|
+
const o = Number(t);
|
|
157
|
+
if (Number.isFinite(o)) return o;
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
}, _ = () => {
|
|
161
|
+
i !== null && (window.clearInterval(i), i = null);
|
|
162
|
+
}, B = () => {
|
|
163
|
+
_(), w.clear(), h.clear(), l.clear();
|
|
164
|
+
}, N = () => {
|
|
165
|
+
for (const [t] of w)
|
|
166
|
+
if (!h.has(t) && l.has(t)) return !0;
|
|
167
|
+
return !1;
|
|
168
|
+
}, I = () => {
|
|
169
|
+
if (!(!e || e.state !== "running")) {
|
|
170
|
+
for (const [t, o] of l) {
|
|
171
|
+
if (h.has(t)) continue;
|
|
172
|
+
const r = w.get(t);
|
|
173
|
+
r && e.currentTime + 5e-3 >= o && (h.add(t), w.delete(t), a("eventNotifaction", r));
|
|
174
|
+
}
|
|
175
|
+
N() || _();
|
|
176
|
+
}
|
|
177
|
+
}, x = () => {
|
|
178
|
+
i === null && N() && (i = window.setInterval(() => {
|
|
179
|
+
I();
|
|
180
|
+
}, 30));
|
|
181
|
+
}, q = () => {
|
|
182
|
+
e || (e = new (window.AudioContext || window.webkitAudioContext)({
|
|
153
183
|
sampleRate: 24e3
|
|
154
|
-
})),
|
|
155
|
-
},
|
|
156
|
-
|
|
157
|
-
},
|
|
158
|
-
if (
|
|
184
|
+
})), e.state === "suspended" && !y && e.resume();
|
|
185
|
+
}, T = () => {
|
|
186
|
+
d && v === 0 && (a("playComplete", n.screenClientId), d = !1);
|
|
187
|
+
}, z = (t, o) => {
|
|
188
|
+
if (q(), !!e)
|
|
159
189
|
try {
|
|
160
|
-
const
|
|
161
|
-
for (let
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
for (let
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
190
|
+
const r = window.atob(t), g = r.length, k = new Uint8Array(g);
|
|
191
|
+
for (let f = 0; f < g; f++)
|
|
192
|
+
k[f] = r.charCodeAt(f);
|
|
193
|
+
const m = new Int16Array(k.buffer), A = new Float32Array(m.length);
|
|
194
|
+
for (let f = 0; f < m.length; f++)
|
|
195
|
+
A[f] = m[f] / 32768;
|
|
196
|
+
const U = e.createBuffer(1, A.length, 24e3);
|
|
197
|
+
U.getChannelData(0).set(A);
|
|
198
|
+
const E = e.createBufferSource();
|
|
199
|
+
E.buffer = U, E.connect(e.destination);
|
|
200
|
+
const H = p < e.currentTime ? e.currentTime : p;
|
|
201
|
+
o !== null && !l.has(o) && (l.set(o, H), I(), x()), E.start(H), p = H + U.duration, v++, E.onended = () => {
|
|
202
|
+
v--, T();
|
|
171
203
|
};
|
|
172
|
-
} catch (
|
|
173
|
-
console.error("[VirtualHumanEventAdapter] Failed to decode and play audio:",
|
|
204
|
+
} catch (r) {
|
|
205
|
+
console.error("[VirtualHumanEventAdapter] Failed to decode and play audio:", r);
|
|
174
206
|
}
|
|
175
|
-
},
|
|
176
|
-
switch (
|
|
207
|
+
}, D = (t) => {
|
|
208
|
+
switch (t) {
|
|
177
209
|
case "play":
|
|
178
|
-
|
|
210
|
+
B(), d = !1, v = 0, p = 0, y = !1, e && e.state === "suspended" && e.resume();
|
|
179
211
|
break;
|
|
180
212
|
case "resume":
|
|
181
|
-
|
|
213
|
+
y = !1, e && e.state === "suspended" && e.resume(), I(), x();
|
|
182
214
|
break;
|
|
183
215
|
case "pause":
|
|
184
|
-
|
|
216
|
+
y = !0, e && e.state === "running" && e.suspend();
|
|
185
217
|
break;
|
|
186
218
|
case "stop":
|
|
187
|
-
|
|
219
|
+
B(), y = !1, e && (e.close(), e = null), p = 0, d = !1, v = 0;
|
|
188
220
|
break;
|
|
189
221
|
case "tts_complete":
|
|
190
|
-
|
|
222
|
+
d = !0, T();
|
|
191
223
|
break;
|
|
192
224
|
default:
|
|
193
|
-
console.warn(`[VirtualHumanEventAdapter] Unknown control action: ${
|
|
225
|
+
console.warn(`[VirtualHumanEventAdapter] Unknown control action: ${t}`);
|
|
194
226
|
}
|
|
195
|
-
},
|
|
196
|
-
|
|
227
|
+
}, V = () => {
|
|
228
|
+
s.value && s.value.close();
|
|
197
229
|
try {
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
},
|
|
230
|
+
const t = new URL(n.wsUrl);
|
|
231
|
+
t.searchParams.append("sessionId", n.screenClientId + "-event"), s.value = new WebSocket(t.toString()), s.value.onopen = () => {
|
|
232
|
+
P.value = !0, a("connected"), console.log(`[VirtualHumanEventAdapter] Connected to ${n.wsUrl} for session ${n.screenClientId}-event`);
|
|
233
|
+
}, s.value.onmessage = (o) => {
|
|
202
234
|
try {
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
} catch (
|
|
206
|
-
console.error("[VirtualHumanEventAdapter] Failed to parse message:", o.data,
|
|
235
|
+
const r = JSON.parse(o.data);
|
|
236
|
+
O(r);
|
|
237
|
+
} catch (r) {
|
|
238
|
+
console.error("[VirtualHumanEventAdapter] Failed to parse message:", o.data, r);
|
|
207
239
|
}
|
|
208
|
-
},
|
|
209
|
-
console.error("[VirtualHumanEventAdapter] WebSocket error:", o),
|
|
210
|
-
},
|
|
211
|
-
|
|
240
|
+
}, s.value.onerror = (o) => {
|
|
241
|
+
console.error("[VirtualHumanEventAdapter] WebSocket error:", o), a("error", o);
|
|
242
|
+
}, s.value.onclose = () => {
|
|
243
|
+
P.value = !1, console.log("[VirtualHumanEventAdapter] WebSocket disconnected");
|
|
212
244
|
};
|
|
213
|
-
} catch (
|
|
214
|
-
console.error("[VirtualHumanEventAdapter] Failed to initialize WebSocket:",
|
|
245
|
+
} catch (t) {
|
|
246
|
+
console.error("[VirtualHumanEventAdapter] Failed to initialize WebSocket:", t), a("error", t);
|
|
215
247
|
}
|
|
216
|
-
},
|
|
217
|
-
const { type: o, payload:
|
|
248
|
+
}, O = (t) => {
|
|
249
|
+
const { type: o, payload: r, action: g } = t;
|
|
218
250
|
switch (o) {
|
|
219
251
|
case "audio":
|
|
220
|
-
const
|
|
221
|
-
|
|
252
|
+
const k = (r == null ? void 0 : r.data) || t.data;
|
|
253
|
+
if (k) {
|
|
254
|
+
const m = u((r == null ? void 0 : r.index) ?? t.index);
|
|
255
|
+
z(k, m);
|
|
256
|
+
}
|
|
222
257
|
break;
|
|
223
258
|
case "send_event":
|
|
224
|
-
|
|
259
|
+
if (t.event) {
|
|
260
|
+
console.log("adapter send_event:", t);
|
|
261
|
+
const m = u(t.index);
|
|
262
|
+
if (m === null) {
|
|
263
|
+
a("eventNotifaction", t);
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
w.set(m, t), I(), x();
|
|
267
|
+
}
|
|
225
268
|
break;
|
|
226
269
|
case "control":
|
|
227
|
-
|
|
270
|
+
g && (console.log("adapter control:", g), a("controlEvent", g), D(g));
|
|
228
271
|
break;
|
|
229
272
|
case "end":
|
|
230
|
-
console.log("adapter end:",
|
|
273
|
+
console.log("adapter end:", r), a("end", r);
|
|
231
274
|
break;
|
|
232
275
|
case "pause":
|
|
233
|
-
console.log("adapter pause:",
|
|
276
|
+
console.log("adapter pause:", r), a("pause", r);
|
|
234
277
|
break;
|
|
235
278
|
default:
|
|
236
279
|
console.warn(`[VirtualHumanEventAdapter] Unknown message type: ${o}`);
|
|
237
280
|
}
|
|
238
281
|
};
|
|
239
|
-
return C(() =>
|
|
240
|
-
|
|
241
|
-
}), C(() =>
|
|
242
|
-
|
|
243
|
-
}),
|
|
244
|
-
|
|
245
|
-
}),
|
|
246
|
-
|
|
247
|
-
}), (
|
|
282
|
+
return C(() => n.screenClientId, () => {
|
|
283
|
+
n.screenClientId && n.wsUrl && V();
|
|
284
|
+
}), C(() => n.wsUrl, () => {
|
|
285
|
+
n.screenClientId && n.wsUrl && V();
|
|
286
|
+
}), M(() => {
|
|
287
|
+
n.screenClientId && n.wsUrl && V();
|
|
288
|
+
}), R(() => {
|
|
289
|
+
s.value && s.value.close(), e && e.close();
|
|
290
|
+
}), (t, o) => Y(t.$slots, "default");
|
|
248
291
|
}
|
|
249
|
-
}),
|
|
250
|
-
|
|
251
|
-
},
|
|
252
|
-
install:
|
|
292
|
+
}), re = (c) => {
|
|
293
|
+
c.component("VirtualHumanPersona", ne), c.component("VirtualHumanEventAdapter", ae);
|
|
294
|
+
}, oe = {
|
|
295
|
+
install: re
|
|
253
296
|
};
|
|
254
297
|
export {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
298
|
+
ae as VirtualHumanEventAdapter,
|
|
299
|
+
ne as VirtualHumanPersona,
|
|
300
|
+
oe as default
|
|
258
301
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(f,
|
|
1
|
+
(function(f,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],a):(f=typeof globalThis<"u"?globalThis:f||self,a(f.VirtualHumanCf={},f.Vue))})(this,function(f,a){"use strict";const $=["src","muted"],_=((d,k)=>{const n=d.__vccOpts||d;for(const[r,s]of k)n[r]=s;return n})(a.defineComponent({__name:"VirtualHumanPersona",props:{videoSrc:{type:String,required:!0},visible:{type:Boolean,default:!1},isPlaying:{type:Boolean,default:!1},muted:{type:Boolean,default:!0},isDark:{type:Boolean,default:!1},screenClientId:{type:String,required:!1},wsUrl:{type:String,required:!1},styles:{type:Object,default:()=>({width:"400px",height:"500px",left:"16px",bottom:"16px"})}},emits:["update:isPlaying","ended","update:visible"],setup(d,{emit:k}){const n=d,r=k,s=a.ref(null),P=a.ref(null),e=a.ref(null),y=a.ref(!1),p=()=>{if(e.value&&e.value.close(),!(!n.wsUrl||!n.screenClientId))try{const i=new URL(n.wsUrl);i.searchParams.append("sessionId",n.screenClientId+"-persona"),e.value=new WebSocket(i.toString()),e.value.onopen=()=>{y.value=!0,console.log(`[VirtualHumanPersona] Connected to ${n.wsUrl} for session ${n.screenClientId}-persona`)},e.value.onmessage=c=>{try{const u=JSON.parse(c.data);h(u)}catch(u){console.error("[VirtualHumanPersona] Failed to parse message:",c.data,u)}},e.value.onerror=c=>{console.error("[VirtualHumanPersona] WebSocket error:",c)},e.value.onclose=()=>{y.value=!1,console.log("[VirtualHumanPersona] WebSocket disconnected")}}catch(i){console.error("[VirtualHumanPersona] Failed to initialize WebSocket:",i)}},h=i=>{const{type:c,action:u}=i;c==="control"&&(u==="play"||u==="resume"?(r("update:isPlaying",!0),r("update:visible",!0)):u==="pause"?(r("update:isPlaying",!1),r("update:visible",!0)):u==="stop"&&(r("update:isPlaying",!1),r("update:visible",!1),s.value&&(s.value.currentTime=0)))};a.watch(()=>n.screenClientId,()=>{n.screenClientId&&n.wsUrl&&p()}),a.watch(()=>n.wsUrl,()=>{n.screenClientId&&n.wsUrl&&p()}),a.watch(()=>n.isPlaying,i=>{s.value&&(i?s.value.play().catch(c=>console.error("Video play failed:",c)):s.value.pause())});const v=()=>{n.isPlaying||r("update:isPlaying",!0)},g=()=>{n.isPlaying&&r("update:isPlaying",!1)},S=()=>{r("update:isPlaying",!1),r("ended")};return a.onMounted(()=>{n.isPlaying&&s.value&&s.value.play().catch(i=>console.error("Video play failed:",i)),n.screenClientId&&n.wsUrl&&p()}),a.onUnmounted(()=>{e.value&&e.value.close()}),(i,c)=>(a.openBlock(),a.createBlock(a.Transition,{name:"fade"},{default:a.withCtx(()=>[d.visible?(a.openBlock(),a.createElementBlock("div",{key:0,class:a.normalizeClass(["virtual-human-container",{"is-dark":d.isDark}]),style:a.normalizeStyle(d.styles)},[a.createElementVNode("div",{class:"video-wrapper",ref_key:"wrapperRef",ref:P},[a.createElementVNode("video",{ref_key:"videoRef",ref:s,src:d.videoSrc,class:"persona-video",muted:d.muted,playsinline:"",loop:"",autoPlay:"",disablePictureInPicture:"",onPlay:v,onPause:g,onEnded:S},null,40,$)],512)],6)):a.createCommentVNode("",!0)]),_:1}))}}),[["__scopeId","data-v-bf23f155"]]),B=a.defineComponent({__name:"VirtualHumanEventAdapter",props:{screenClientId:{type:String,required:!0},wsUrl:{type:String,required:!0}},emits:["eventNotifaction","controlEvent","end","pause","connected","error","playComplete"],setup(d,{emit:k}){const n=d,r=k,s=a.ref(null),P=a.ref(!1);let e=null,y=0,p=!1,h=0,v=!1;const g=new Map,S=new Set,i=new Map;let c=null;const u=t=>{if(typeof t=="number"&&Number.isFinite(t))return t;if(typeof t=="string"&&t.trim()!==""){const l=Number(t);if(Number.isFinite(l))return l}return null},T=()=>{c!==null&&(window.clearInterval(c),c=null)},N=()=>{T(),g.clear(),S.clear(),i.clear()},M=()=>{for(const[t]of g)if(!S.has(t)&&i.has(t))return!0;return!1},I=()=>{if(!(!e||e.state!=="running")){for(const[t,l]of i){if(S.has(t))continue;const o=g.get(t);o&&e.currentTime+.005>=l&&(S.add(t),g.delete(t),r("eventNotifaction",o))}M()||T()}},E=()=>{c===null&&M()&&(c=window.setInterval(()=>{I()},30))},R=()=>{e||(e=new(window.AudioContext||window.webkitAudioContext)({sampleRate:24e3})),e.state==="suspended"&&!v&&e.resume()},W=()=>{p&&h===0&&(r("playComplete",n.screenClientId),p=!1)},q=(t,l)=>{if(R(),!!e)try{const o=window.atob(t),b=o.length,C=new Uint8Array(b);for(let m=0;m<b;m++)C[m]=o.charCodeAt(m);const w=new Int16Array(C.buffer),A=new Float32Array(w.length);for(let m=0;m<w.length;m++)A[m]=w[m]/32768;const U=e.createBuffer(1,A.length,24e3);U.getChannelData(0).set(A);const V=e.createBufferSource();V.buffer=U,V.connect(e.destination);const H=y<e.currentTime?e.currentTime:y;l!==null&&!i.has(l)&&(i.set(l,H),I(),E()),V.start(H),y=H+U.duration,h++,V.onended=()=>{h--,W()}}catch(o){console.error("[VirtualHumanEventAdapter] Failed to decode and play audio:",o)}},z=t=>{switch(t){case"play":N(),p=!1,h=0,y=0,v=!1,e&&e.state==="suspended"&&e.resume();break;case"resume":v=!1,e&&e.state==="suspended"&&e.resume(),I(),E();break;case"pause":v=!0,e&&e.state==="running"&&e.suspend();break;case"stop":N(),v=!1,e&&(e.close(),e=null),y=0,p=!1,h=0;break;case"tts_complete":p=!0,W();break;default:console.warn(`[VirtualHumanEventAdapter] Unknown control action: ${t}`)}},x=()=>{s.value&&s.value.close();try{const t=new URL(n.wsUrl);t.searchParams.append("sessionId",n.screenClientId+"-event"),s.value=new WebSocket(t.toString()),s.value.onopen=()=>{P.value=!0,r("connected"),console.log(`[VirtualHumanEventAdapter] Connected to ${n.wsUrl} for session ${n.screenClientId}-event`)},s.value.onmessage=l=>{try{const o=JSON.parse(l.data);O(o)}catch(o){console.error("[VirtualHumanEventAdapter] Failed to parse message:",l.data,o)}},s.value.onerror=l=>{console.error("[VirtualHumanEventAdapter] WebSocket error:",l),r("error",l)},s.value.onclose=()=>{P.value=!1,console.log("[VirtualHumanEventAdapter] WebSocket disconnected")}}catch(t){console.error("[VirtualHumanEventAdapter] Failed to initialize WebSocket:",t),r("error",t)}},O=t=>{const{type:l,payload:o,action:b}=t;switch(l){case"audio":const C=(o==null?void 0:o.data)||t.data;if(C){const w=u((o==null?void 0:o.index)??t.index);q(C,w)}break;case"send_event":if(t.event){console.log("adapter send_event:",t);const w=u(t.index);if(w===null){r("eventNotifaction",t);break}g.set(w,t),I(),E()}break;case"control":b&&(console.log("adapter control:",b),r("controlEvent",b),z(b));break;case"end":console.log("adapter end:",o),r("end",o);break;case"pause":console.log("adapter pause:",o),r("pause",o);break;default:console.warn(`[VirtualHumanEventAdapter] Unknown message type: ${l}`)}};return a.watch(()=>n.screenClientId,()=>{n.screenClientId&&n.wsUrl&&x()}),a.watch(()=>n.wsUrl,()=>{n.screenClientId&&n.wsUrl&&x()}),a.onMounted(()=>{n.screenClientId&&n.wsUrl&&x()}),a.onUnmounted(()=>{s.value&&s.value.close(),e&&e.close()}),(t,l)=>a.renderSlot(t.$slots,"default")}}),F={install:d=>{d.component("VirtualHumanPersona",_),d.component("VirtualHumanEventAdapter",B)}};f.VirtualHumanEventAdapter=B,f.VirtualHumanPersona=_,f.default=F,Object.defineProperties(f,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
CHANGED
|
@@ -29,6 +29,67 @@ let nextStartTime = 0;
|
|
|
29
29
|
let isTtsComplete = false;
|
|
30
30
|
let activeSources = 0;
|
|
31
31
|
let isIntentionallyPaused = false;
|
|
32
|
+
const pendingEventByIndex = new Map<number, any>();
|
|
33
|
+
const firedEventIndexSet = new Set<number>();
|
|
34
|
+
const plannedStartTimeByIndex = new Map<number, number>();
|
|
35
|
+
let eventSchedulerTimer: number | null = null;
|
|
36
|
+
|
|
37
|
+
const normalizeIndex = (value: unknown): number | null => {
|
|
38
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
39
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
40
|
+
const n = Number(value);
|
|
41
|
+
if (Number.isFinite(n)) return n;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const stopEventScheduler = () => {
|
|
47
|
+
if (eventSchedulerTimer !== null) {
|
|
48
|
+
window.clearInterval(eventSchedulerTimer);
|
|
49
|
+
eventSchedulerTimer = null;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const clearEventState = () => {
|
|
54
|
+
stopEventScheduler();
|
|
55
|
+
pendingEventByIndex.clear();
|
|
56
|
+
firedEventIndexSet.clear();
|
|
57
|
+
plannedStartTimeByIndex.clear();
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const hasSchedulableEvent = () => {
|
|
61
|
+
for (const [index] of pendingEventByIndex) {
|
|
62
|
+
if (!firedEventIndexSet.has(index) && plannedStartTimeByIndex.has(index)) return true;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const tickEventScheduler = () => {
|
|
68
|
+
if (!audioContext || audioContext.state !== 'running') return;
|
|
69
|
+
|
|
70
|
+
for (const [index, plannedStartTime] of plannedStartTimeByIndex) {
|
|
71
|
+
if (firedEventIndexSet.has(index)) continue;
|
|
72
|
+
const pendingEvent = pendingEventByIndex.get(index);
|
|
73
|
+
if (!pendingEvent) continue;
|
|
74
|
+
|
|
75
|
+
if (audioContext.currentTime + 0.005 >= plannedStartTime) {
|
|
76
|
+
firedEventIndexSet.add(index);
|
|
77
|
+
pendingEventByIndex.delete(index);
|
|
78
|
+
emit('eventNotifaction', pendingEvent);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!hasSchedulableEvent()) stopEventScheduler();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const ensureEventScheduler = () => {
|
|
86
|
+
if (eventSchedulerTimer !== null) return;
|
|
87
|
+
if (!hasSchedulableEvent()) return;
|
|
88
|
+
|
|
89
|
+
eventSchedulerTimer = window.setInterval(() => {
|
|
90
|
+
tickEventScheduler();
|
|
91
|
+
}, 30);
|
|
92
|
+
};
|
|
32
93
|
|
|
33
94
|
const initAudioContext = () => {
|
|
34
95
|
if (!audioContext) {
|
|
@@ -48,7 +109,7 @@ const checkPlayComplete = () => {
|
|
|
48
109
|
}
|
|
49
110
|
};
|
|
50
111
|
|
|
51
|
-
const handleAudioMessage = (base64Data: string) => {
|
|
112
|
+
const handleAudioMessage = (base64Data: string, index: number | null) => {
|
|
52
113
|
initAudioContext();
|
|
53
114
|
if (!audioContext) return;
|
|
54
115
|
|
|
@@ -75,11 +136,15 @@ const handleAudioMessage = (base64Data: string) => {
|
|
|
75
136
|
source.connect(audioContext.destination);
|
|
76
137
|
|
|
77
138
|
// Keep track of the start time for seamless playback
|
|
78
|
-
|
|
79
|
-
|
|
139
|
+
const startAt = nextStartTime < audioContext.currentTime ? audioContext.currentTime : nextStartTime;
|
|
140
|
+
if (index !== null && !plannedStartTimeByIndex.has(index)) {
|
|
141
|
+
plannedStartTimeByIndex.set(index, startAt);
|
|
142
|
+
tickEventScheduler();
|
|
143
|
+
ensureEventScheduler();
|
|
80
144
|
}
|
|
81
|
-
|
|
82
|
-
|
|
145
|
+
|
|
146
|
+
source.start(startAt);
|
|
147
|
+
nextStartTime = startAt + audioBuffer.duration;
|
|
83
148
|
|
|
84
149
|
activeSources++;
|
|
85
150
|
source.onended = () => {
|
|
@@ -94,6 +159,7 @@ const handleAudioMessage = (base64Data: string) => {
|
|
|
94
159
|
const handleControlMessage = (action: string) => {
|
|
95
160
|
switch (action) {
|
|
96
161
|
case 'play':
|
|
162
|
+
clearEventState();
|
|
97
163
|
isTtsComplete = false;
|
|
98
164
|
activeSources = 0;
|
|
99
165
|
nextStartTime = 0;
|
|
@@ -107,6 +173,8 @@ const handleControlMessage = (action: string) => {
|
|
|
107
173
|
if (audioContext && audioContext.state === 'suspended') {
|
|
108
174
|
audioContext.resume();
|
|
109
175
|
}
|
|
176
|
+
tickEventScheduler();
|
|
177
|
+
ensureEventScheduler();
|
|
110
178
|
break;
|
|
111
179
|
case 'pause':
|
|
112
180
|
isIntentionallyPaused = true;
|
|
@@ -115,6 +183,7 @@ const handleControlMessage = (action: string) => {
|
|
|
115
183
|
}
|
|
116
184
|
break;
|
|
117
185
|
case 'stop':
|
|
186
|
+
clearEventState();
|
|
118
187
|
isIntentionallyPaused = false;
|
|
119
188
|
if (audioContext) {
|
|
120
189
|
audioContext.close();
|
|
@@ -181,14 +250,23 @@ const handleMessage = (msg: any) => {
|
|
|
181
250
|
case 'audio':
|
|
182
251
|
const base64Data = payload?.data || msg.data;
|
|
183
252
|
if (base64Data) {
|
|
184
|
-
|
|
253
|
+
const index = normalizeIndex(payload?.index ?? msg.index);
|
|
254
|
+
handleAudioMessage(base64Data, index);
|
|
185
255
|
}
|
|
186
256
|
break;
|
|
187
257
|
// 接收事件通知
|
|
188
258
|
case 'send_event':
|
|
189
259
|
if (msg.event) {
|
|
190
260
|
console.log("adapter send_event:",msg)
|
|
191
|
-
|
|
261
|
+
const index = normalizeIndex(msg.index);
|
|
262
|
+
if (index === null) {
|
|
263
|
+
emit('eventNotifaction', msg);
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
pendingEventByIndex.set(index, msg);
|
|
268
|
+
tickEventScheduler();
|
|
269
|
+
ensureEventScheduler();
|
|
192
270
|
}
|
|
193
271
|
break;
|
|
194
272
|
// 控制指令接口
|