orb-ui 0.7.0 → 0.8.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 +77 -23
- package/dist/adapters/audio-level.d.ts +57 -20
- package/dist/adapters/elevenlabs/index.d.ts +13 -2
- package/dist/adapters/gemini-live/index.d.ts +9 -5
- package/dist/adapters/index.d.ts +3 -2
- package/dist/adapters/livekit/browser.d.ts +9 -5
- package/dist/adapters/livekit/index.d.ts +9 -5
- package/dist/adapters/openai-realtime/index.d.ts +9 -5
- package/dist/adapters/pipecat/index.d.ts +9 -5
- package/dist/adapters/types.d.ts +0 -5
- package/dist/adapters/vapi/index.d.ts +29 -3
- package/dist/adapters/volume-presets.d.ts +35 -0
- package/dist/adapters.cjs +1 -1
- package/dist/adapters.js +405 -457
- package/dist/components/Orb/Orb.d.ts +1 -1
- package/dist/components/Orb/Orb.types.d.ts +87 -14
- package/dist/components/Orb/OrbThemeProvider.d.ts +16 -0
- package/dist/components/Orb/index.d.ts +6 -1
- package/dist/components/Orb/signals.d.ts +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/livekit-7Re8E7By.js +394 -0
- package/dist/livekit-C36Kmz6s.cjs +1 -0
- package/dist/livekit-adapter.cjs +1 -1
- package/dist/livekit-adapter.js +6 -4
- package/dist/orb-ui.cjs +7 -7
- package/dist/orb-ui.js +1343 -512
- package/dist/themes/bars/BarsTheme.d.ts +8 -4
- package/dist/themes/circle/CircleTheme.d.ts +8 -4
- package/dist/themes/cloud/CloudTheme.d.ts +8 -4
- package/dist/themes/config.d.ts +151 -0
- package/dist/themes/css-variables.d.ts +5 -0
- package/dist/themes/debug/DebugTheme.d.ts +11 -6
- package/dist/themes/radial/RadialTheme.d.ts +8 -2
- package/dist/themes/slots.d.ts +4 -0
- package/package.json +3 -2
- package/dist/livekit-CukEeYFK.js +0 -316
- package/dist/livekit-Dt-QZ6Gs.cjs +0 -1
package/dist/adapters.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as e, c as t, i as n, n as r, o as i, r as a, s as o, t as s } from "./livekit-7Re8E7By.js";
|
|
2
2
|
//#region src/adapters/vapi/index.ts
|
|
3
|
-
var
|
|
4
|
-
function
|
|
3
|
+
var c = 160;
|
|
4
|
+
function l(e) {
|
|
5
5
|
let t = "idle", n = null;
|
|
6
6
|
function r(r) {
|
|
7
7
|
if (n &&= (clearTimeout(n), null), t === "speaking" && r === "listening") {
|
|
@@ -20,232 +20,240 @@ function a(e) {
|
|
|
20
20
|
clearTimer: i
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
-
function
|
|
24
|
-
let
|
|
25
|
-
function
|
|
26
|
-
|
|
23
|
+
function u(t, n) {
|
|
24
|
+
let a = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set(), s = null;
|
|
25
|
+
function u() {
|
|
26
|
+
s || (s = t.start.bind(t), t.start = async (...e) => (a.forEach((e) => e()), s(...e)));
|
|
27
27
|
}
|
|
28
|
-
function
|
|
29
|
-
|
|
28
|
+
function d() {
|
|
29
|
+
a.size > 0 || !s || (t.start = s, s = null);
|
|
30
30
|
}
|
|
31
31
|
return {
|
|
32
32
|
async start() {
|
|
33
|
-
await
|
|
33
|
+
await t.start(n?.assistantId);
|
|
34
34
|
},
|
|
35
35
|
stop() {
|
|
36
|
-
e.stop();
|
|
36
|
+
o.forEach((e) => e()), t.stop();
|
|
37
37
|
},
|
|
38
|
-
subscribe(
|
|
39
|
-
let
|
|
38
|
+
subscribe(s) {
|
|
39
|
+
let f = {
|
|
40
40
|
state: "idle",
|
|
41
|
-
|
|
41
|
+
inputVolume: 0,
|
|
42
42
|
outputVolume: 0
|
|
43
|
-
},
|
|
44
|
-
function
|
|
45
|
-
|
|
46
|
-
return c += (t - c) * n, c;
|
|
47
|
-
}
|
|
48
|
-
function u(e) {
|
|
49
|
-
r = e, t(e);
|
|
43
|
+
}, p = i(r.vapi.input, n?.inputVolumeCalibration), m = i(r.vapi.output, n?.outputVolumeCalibration);
|
|
44
|
+
function h(e) {
|
|
45
|
+
f = e, s(e);
|
|
50
46
|
}
|
|
51
|
-
function
|
|
52
|
-
|
|
53
|
-
...
|
|
47
|
+
function g(e) {
|
|
48
|
+
h({
|
|
49
|
+
...f,
|
|
54
50
|
...e,
|
|
55
|
-
state: e.state ??
|
|
51
|
+
state: e.state ?? f.state
|
|
56
52
|
});
|
|
57
53
|
}
|
|
58
|
-
let { emitState:
|
|
59
|
-
|
|
60
|
-
|
|
54
|
+
let { emitState: _, clearTimer: v } = l((e) => {
|
|
55
|
+
b = e, e === "listening" ? (R(), g({
|
|
56
|
+
state: e,
|
|
61
57
|
outputVolume: 0
|
|
58
|
+
})) : g({ state: e });
|
|
59
|
+
}), y = () => _("connecting"), b = "idle", x = !1, S, C, w;
|
|
60
|
+
function T(e = !0) {
|
|
61
|
+
C?.stop().catch(() => void 0), C = void 0, S = void 0, p.reset(), e && f.inputVolume && g({ inputVolume: 0 });
|
|
62
|
+
}
|
|
63
|
+
function E() {
|
|
64
|
+
let r;
|
|
65
|
+
try {
|
|
66
|
+
let e = t.getDailyCallObject?.()?.participants().local?.tracks?.audio;
|
|
67
|
+
(!e?.state || e.state === "playable") && (r = e?.persistentTrack ?? e?.track);
|
|
68
|
+
} catch {}
|
|
69
|
+
if ((r?.readyState === "ended" || r?.enabled === !1 || r?.muted) && (r = void 0), r === S || (T(), !r)) return;
|
|
70
|
+
S = r;
|
|
71
|
+
let i = r;
|
|
72
|
+
C = e(r, n?.createAudioContext ?? (() => {
|
|
73
|
+
if (!(typeof window > "u" || !window.AudioContext)) return new window.AudioContext();
|
|
74
|
+
}), (e) => {
|
|
75
|
+
if (!x || S !== i) return;
|
|
76
|
+
let t = p.sample(i.enabled && !i.muted && i.readyState !== "ended" ? e : 0);
|
|
77
|
+
n?.onInputVolumeSample?.(t), g({ inputVolume: t.normalized });
|
|
62
78
|
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}), T());
|
|
73
|
-
}, b = () => {
|
|
74
|
-
g && (E(), h = "listening", d({
|
|
75
|
-
volume: 0,
|
|
79
|
+
}
|
|
80
|
+
function D(e = !0) {
|
|
81
|
+
w !== void 0 && clearInterval(w), w = void 0, T(e);
|
|
82
|
+
}
|
|
83
|
+
let O = () => {
|
|
84
|
+
x = !0, _("listening"), t.getDailyCallObject && (E(), w === void 0 && (w = setInterval(E, 100)));
|
|
85
|
+
}, k = () => {
|
|
86
|
+
x = !1, b = "idle", D(), R(), _("idle"), g({
|
|
87
|
+
inputVolume: 0,
|
|
76
88
|
outputVolume: 0
|
|
77
|
-
})
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
C &&= (cancelAnimationFrame(C), 0), S = 0, x = 0;
|
|
87
|
-
}, D = (e) => {
|
|
88
|
-
if (h === "speaking") {
|
|
89
|
-
let t = l(e);
|
|
90
|
-
x = t / (t + .3);
|
|
89
|
+
});
|
|
90
|
+
}, A = () => {
|
|
91
|
+
x && (_("speaking"), L());
|
|
92
|
+
}, j = () => {
|
|
93
|
+
x && (M = 0, N = 0, P = 0, _("listening"));
|
|
94
|
+
}, M = 0, N = 0, P = 0, F = 0, I = (e) => {
|
|
95
|
+
if (b === "speaking") {
|
|
96
|
+
let t = e - P <= c ? N : M, r = m.sample(t, e);
|
|
97
|
+
n?.onOutputVolumeSample?.(r), g({ outputVolume: r.normalized });
|
|
91
98
|
}
|
|
92
|
-
|
|
93
|
-
|
|
99
|
+
F = requestAnimationFrame(I);
|
|
100
|
+
}, L = () => {
|
|
101
|
+
F ||= requestAnimationFrame(I);
|
|
102
|
+
}, R = () => {
|
|
103
|
+
F &&= (cancelAnimationFrame(F), 0), M = 0, N = 0, P = 0, m.reset();
|
|
104
|
+
}, z = (e) => {
|
|
105
|
+
b === "speaking" && (M = e, e > r.vapi.output.amplitude.silenceFloor && (N = e, P = performance.now()));
|
|
106
|
+
}, B = () => {}, V = (e) => {
|
|
107
|
+
console.error("[orb-ui/vapi] Error:", e), x = !1, b = "error", v(), D(), R(), g({
|
|
94
108
|
state: "error",
|
|
95
|
-
|
|
109
|
+
inputVolume: 0,
|
|
96
110
|
outputVolume: 0,
|
|
97
111
|
error: e
|
|
98
|
-
})
|
|
112
|
+
});
|
|
99
113
|
};
|
|
100
|
-
return
|
|
101
|
-
|
|
114
|
+
return t.on("call-start", O), t.on("call-end", k), t.on("speech-start", A), t.on("speech-end", j), t.on("volume-level", z), t.on("message", B), t.on("error", V), a.add(y), o.add(k), u(), () => {
|
|
115
|
+
v(), x = !1, D(!1), R(), t.removeListener("call-start", O), t.removeListener("call-end", k), t.removeListener("speech-start", A), t.removeListener("speech-end", j), t.removeListener("volume-level", z), t.removeListener("message", B), t.removeListener("error", V), a.delete(y), o.delete(k), d();
|
|
102
116
|
};
|
|
103
117
|
}
|
|
104
118
|
};
|
|
105
119
|
}
|
|
106
120
|
//#endregion
|
|
107
121
|
//#region src/adapters/elevenlabs/index.ts
|
|
108
|
-
function
|
|
109
|
-
let n = null,
|
|
122
|
+
function d(e, t) {
|
|
123
|
+
let { inputVolumeCalibration: n, outputVolumeCalibration: a, onInputVolumeSample: o, onOutputVolumeSample: s, ...c } = t, l = null, u = 0, d = null, f = null, p = {
|
|
110
124
|
state: "idle",
|
|
111
|
-
volume: 0,
|
|
112
125
|
inputVolume: 0,
|
|
113
126
|
outputVolume: 0
|
|
114
|
-
},
|
|
115
|
-
function
|
|
116
|
-
|
|
127
|
+
}, m = "idle", h = i(r.elevenlabs.input, n), g = i(r.elevenlabs.output, a), _ = /* @__PURE__ */ new Set();
|
|
128
|
+
function v(e) {
|
|
129
|
+
p = e, _.forEach((t) => t(e));
|
|
117
130
|
}
|
|
118
|
-
function
|
|
119
|
-
|
|
120
|
-
...
|
|
131
|
+
function y(e) {
|
|
132
|
+
p = {
|
|
133
|
+
...p,
|
|
121
134
|
...e,
|
|
122
|
-
state: e.state ??
|
|
123
|
-
},
|
|
124
|
-
}
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
135
|
+
state: e.state ?? p.state
|
|
136
|
+
}, v(p);
|
|
137
|
+
}
|
|
138
|
+
function b(e) {
|
|
139
|
+
if (m = e, e === "idle" || e === "connecting" || e === "error") {
|
|
140
|
+
h.reset(), g.reset(), y({
|
|
141
|
+
state: e,
|
|
142
|
+
inputVolume: 0,
|
|
143
|
+
outputVolume: 0
|
|
144
|
+
});
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
y({ state: e });
|
|
148
|
+
}
|
|
149
|
+
function x() {
|
|
150
|
+
f ||= setInterval(() => {
|
|
151
|
+
if (!l) return;
|
|
152
|
+
let e = h.sample(l.getInputVolume()), t = g.sample(l.getOutputVolume());
|
|
153
|
+
o?.(e), s?.(t), y({
|
|
154
|
+
inputVolume: e.normalized,
|
|
155
|
+
outputVolume: t.normalized
|
|
156
|
+
});
|
|
145
157
|
}, 33);
|
|
146
158
|
}
|
|
147
|
-
function
|
|
148
|
-
|
|
159
|
+
function S() {
|
|
160
|
+
f &&= (clearInterval(f), null);
|
|
149
161
|
}
|
|
150
|
-
function
|
|
151
|
-
|
|
152
|
-
volume: 0,
|
|
162
|
+
function C() {
|
|
163
|
+
S(), h.reset(), g.reset(), y({
|
|
153
164
|
inputVolume: 0,
|
|
154
165
|
outputVolume: 0
|
|
155
166
|
});
|
|
156
167
|
}
|
|
157
|
-
function
|
|
158
|
-
|
|
168
|
+
function w() {
|
|
169
|
+
m = "idle", h.reset(), g.reset(), y({
|
|
159
170
|
state: "idle",
|
|
160
|
-
volume: 0,
|
|
161
171
|
inputVolume: 0,
|
|
162
172
|
outputVolume: 0
|
|
163
173
|
});
|
|
164
174
|
}
|
|
165
|
-
function
|
|
166
|
-
return e ===
|
|
175
|
+
function T(e) {
|
|
176
|
+
return e === u;
|
|
167
177
|
}
|
|
168
|
-
function
|
|
178
|
+
function E(e) {
|
|
169
179
|
return {
|
|
170
180
|
onStatusChange: ({ status: t }) => {
|
|
171
|
-
|
|
181
|
+
T(e) && t === "connecting" && b("connecting");
|
|
172
182
|
},
|
|
173
183
|
onConnect: () => {
|
|
174
|
-
|
|
184
|
+
T(e) && (b("listening"), x());
|
|
175
185
|
},
|
|
176
186
|
onModeChange: ({ mode: t }) => {
|
|
177
|
-
|
|
187
|
+
T(e) && (t === "speaking" ? (b("speaking"), x()) : (b("listening"), x()));
|
|
178
188
|
},
|
|
179
189
|
onDisconnect: () => {
|
|
180
|
-
|
|
190
|
+
T(e) && (S(), l = null, w());
|
|
181
191
|
},
|
|
182
192
|
onError: (t) => {
|
|
183
|
-
|
|
193
|
+
T(e) && (console.error("[orb-ui/elevenlabs] Error:", t), S(), m = "error", y({
|
|
184
194
|
state: "error",
|
|
185
|
-
volume: 0,
|
|
186
195
|
inputVolume: 0,
|
|
187
196
|
outputVolume: 0,
|
|
188
197
|
error: t
|
|
189
|
-
}),
|
|
198
|
+
}), l = null);
|
|
190
199
|
}
|
|
191
200
|
};
|
|
192
201
|
}
|
|
193
202
|
return {
|
|
194
203
|
subscribe(e) {
|
|
195
|
-
return
|
|
196
|
-
|
|
204
|
+
return _.add(e), e(p), l && (m === "listening" || m === "speaking") && x(), () => {
|
|
205
|
+
_.delete(e), _.size === 0 && C();
|
|
197
206
|
};
|
|
198
207
|
},
|
|
199
208
|
async start() {
|
|
200
|
-
if (
|
|
201
|
-
|
|
202
|
-
let
|
|
203
|
-
return
|
|
209
|
+
if (l || d) return d ?? void 0;
|
|
210
|
+
u += 1;
|
|
211
|
+
let t = u;
|
|
212
|
+
return d = (async () => {
|
|
204
213
|
try {
|
|
205
|
-
let
|
|
206
|
-
...
|
|
207
|
-
...
|
|
214
|
+
let n = await e.startSession({
|
|
215
|
+
...c,
|
|
216
|
+
...E(t)
|
|
208
217
|
});
|
|
209
|
-
if (!
|
|
210
|
-
await
|
|
218
|
+
if (!T(t)) {
|
|
219
|
+
await n.endSession().catch((e) => {
|
|
211
220
|
console.error("[orb-ui/elevenlabs] stale session cleanup failed:", e);
|
|
212
221
|
});
|
|
213
222
|
return;
|
|
214
223
|
}
|
|
215
|
-
|
|
224
|
+
l = n;
|
|
216
225
|
} catch (e) {
|
|
217
|
-
if (!
|
|
218
|
-
console.error("[orb-ui/elevenlabs] startSession failed:", e),
|
|
219
|
-
volume: 0,
|
|
226
|
+
if (!T(t)) return;
|
|
227
|
+
console.error("[orb-ui/elevenlabs] startSession failed:", e), b("error"), y({
|
|
220
228
|
inputVolume: 0,
|
|
221
229
|
outputVolume: 0,
|
|
222
230
|
error: e
|
|
223
231
|
});
|
|
224
232
|
} finally {
|
|
225
|
-
|
|
233
|
+
d = null;
|
|
226
234
|
}
|
|
227
|
-
})(),
|
|
235
|
+
})(), d;
|
|
228
236
|
},
|
|
229
237
|
async stop() {
|
|
230
|
-
let e =
|
|
238
|
+
let e = l;
|
|
231
239
|
if (!e) {
|
|
232
|
-
|
|
240
|
+
d && (u += 1, S(), w(), await d);
|
|
233
241
|
return;
|
|
234
242
|
}
|
|
235
|
-
let t =
|
|
236
|
-
|
|
243
|
+
let t = u;
|
|
244
|
+
S();
|
|
237
245
|
try {
|
|
238
246
|
await e.endSession();
|
|
239
247
|
} catch (e) {
|
|
240
248
|
console.error("[orb-ui/elevenlabs] endSession failed:", e);
|
|
241
249
|
}
|
|
242
|
-
|
|
250
|
+
l === e && T(t) && (l = null, w());
|
|
243
251
|
}
|
|
244
252
|
};
|
|
245
253
|
}
|
|
246
254
|
//#endregion
|
|
247
255
|
//#region src/adapters/pipecat/index.ts
|
|
248
|
-
var
|
|
256
|
+
var f = {
|
|
249
257
|
connected: "connected",
|
|
250
258
|
disconnected: "disconnected",
|
|
251
259
|
transportStateChanged: "transportStateChanged",
|
|
@@ -262,19 +270,13 @@ var c = {
|
|
|
262
270
|
userStoppedSpeaking: "userStoppedSpeaking",
|
|
263
271
|
botLlmStarted: "botLlmStarted",
|
|
264
272
|
botTtsStarted: "botTtsStarted"
|
|
265
|
-
}, l = .002, u = 4, d = .8, f = .6, p = .2, m = {
|
|
266
|
-
noiseFloor: .002,
|
|
267
|
-
gain: 5.1,
|
|
268
|
-
exponent: .8,
|
|
269
|
-
attack: .5,
|
|
270
|
-
release: .22
|
|
271
273
|
};
|
|
272
|
-
function
|
|
274
|
+
function p() {
|
|
273
275
|
if (typeof window > "u") return;
|
|
274
276
|
let e = window.AudioContext;
|
|
275
277
|
return e ? new e() : void 0;
|
|
276
278
|
}
|
|
277
|
-
function
|
|
279
|
+
function m(e) {
|
|
278
280
|
switch (e) {
|
|
279
281
|
case "initializing":
|
|
280
282
|
case "initialized":
|
|
@@ -289,198 +291,169 @@ function g(e) {
|
|
|
289
291
|
default: return;
|
|
290
292
|
}
|
|
291
293
|
}
|
|
292
|
-
function
|
|
293
|
-
if (typeof e != "number" || !Number.isFinite(e) || e <= l) return 0;
|
|
294
|
-
let t = Math.min(1, Math.max(0, e - l));
|
|
295
|
-
return Math.min(1, t * u) ** +d;
|
|
296
|
-
}
|
|
297
|
-
function v(e, t) {
|
|
298
|
-
let n = _(e), r = n > t ? f : p;
|
|
299
|
-
return t + (n - t) * r;
|
|
300
|
-
}
|
|
301
|
-
function y(e) {
|
|
294
|
+
function h(e) {
|
|
302
295
|
return typeof e == "object" && !!e && "kind" in e && typeof e.kind == "string";
|
|
303
296
|
}
|
|
304
|
-
function
|
|
305
|
-
let
|
|
306
|
-
state:
|
|
307
|
-
volume: 0,
|
|
297
|
+
function g(t, n = {}) {
|
|
298
|
+
let a = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Map(), s = {
|
|
299
|
+
state: m(t.state ?? "") ?? "idle",
|
|
308
300
|
inputVolume: 0,
|
|
309
301
|
outputVolume: 0
|
|
310
|
-
},
|
|
311
|
-
function
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
function x(e) {
|
|
319
|
-
o = e, i.forEach((t) => t(e));
|
|
320
|
-
}
|
|
321
|
-
function S(e, t) {
|
|
322
|
-
l = 0, u = 0, x({
|
|
302
|
+
}, c = !1, l = null, u = null, d, g, _ = i(r.pipecat.input, n.inputVolumeCalibration), v = i(r.pipecat.output, n.outputVolumeCalibration);
|
|
303
|
+
function y(e) {
|
|
304
|
+
s = e, a.forEach((t) => t(e));
|
|
305
|
+
}
|
|
306
|
+
function b(e, t) {
|
|
307
|
+
let n = e === "idle" || e === "connecting" || e === "error";
|
|
308
|
+
n && (_.reset(), v.reset()), y({
|
|
309
|
+
...s,
|
|
323
310
|
state: e,
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
311
|
+
...n ? {
|
|
312
|
+
inputVolume: 0,
|
|
313
|
+
outputVolume: 0
|
|
314
|
+
} : {},
|
|
327
315
|
...t === void 0 ? {} : { error: t }
|
|
328
316
|
});
|
|
329
317
|
}
|
|
330
|
-
function
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
...o,
|
|
336
|
-
volume: t,
|
|
337
|
-
inputVolume: t,
|
|
338
|
-
outputVolume: 0
|
|
318
|
+
function x(e) {
|
|
319
|
+
let t = _.sample(typeof e == "number" ? e : 0);
|
|
320
|
+
n.onInputVolumeSample?.(t), y({
|
|
321
|
+
...s,
|
|
322
|
+
inputVolume: t.normalized
|
|
339
323
|
});
|
|
340
324
|
}
|
|
341
|
-
function
|
|
342
|
-
if (o.state !== "speaking") return;
|
|
325
|
+
function S(e, t) {
|
|
343
326
|
if (t && typeof t == "object") {
|
|
344
327
|
let e = t;
|
|
345
|
-
if (e.local ||
|
|
328
|
+
if (e.local || n.isBotParticipant && !n.isBotParticipant(e)) return;
|
|
346
329
|
}
|
|
347
|
-
let
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
...o,
|
|
352
|
-
volume: a,
|
|
353
|
-
inputVolume: 0,
|
|
354
|
-
outputVolume: a
|
|
330
|
+
let r = v.sample(typeof e == "number" ? e : 0);
|
|
331
|
+
n.onOutputVolumeSample?.(r), y({
|
|
332
|
+
...s,
|
|
333
|
+
outputVolume: r.normalized
|
|
355
334
|
});
|
|
356
335
|
}
|
|
357
|
-
function
|
|
358
|
-
|
|
336
|
+
function C() {
|
|
337
|
+
l = null, d?.stop(), d = void 0;
|
|
359
338
|
}
|
|
360
|
-
function
|
|
361
|
-
|
|
339
|
+
function w() {
|
|
340
|
+
u = null, g?.stop(), g = void 0;
|
|
362
341
|
}
|
|
363
|
-
function
|
|
364
|
-
|
|
342
|
+
function T() {
|
|
343
|
+
C(), w();
|
|
365
344
|
}
|
|
366
|
-
function
|
|
367
|
-
|
|
345
|
+
function E(t) {
|
|
346
|
+
l === t && d || (C(), l = t, d = e(t, n.createAudioContext ?? p, x), d || (l = null));
|
|
368
347
|
}
|
|
369
|
-
function
|
|
370
|
-
|
|
348
|
+
function D(t) {
|
|
349
|
+
u === t && g || (w(), u = t, g = e(t, n.createAudioContext ?? p, S), g || (u = null));
|
|
371
350
|
}
|
|
372
|
-
function
|
|
351
|
+
function O() {
|
|
373
352
|
let e = t.tracks?.();
|
|
374
|
-
e?.local?.audio &&
|
|
353
|
+
e?.local?.audio && E(e.local.audio), e?.bot?.audio && D(e.bot.audio);
|
|
375
354
|
}
|
|
376
|
-
function
|
|
355
|
+
function k(e) {
|
|
377
356
|
if (!e || typeof e != "object") return !0;
|
|
378
357
|
let t = e;
|
|
379
|
-
return t.local ? !1 :
|
|
358
|
+
return t.local ? !1 : n.isBotParticipant?.(t) ?? !0;
|
|
380
359
|
}
|
|
381
|
-
function
|
|
382
|
-
if (
|
|
383
|
-
let
|
|
384
|
-
|
|
360
|
+
function A(e, t) {
|
|
361
|
+
if (n.playRemoteAudio === !1 || !h(e) || e.kind !== "audio" || !k(t)) return;
|
|
362
|
+
let r = n.createAudioElement?.() ?? new Audio();
|
|
363
|
+
r.autoplay = !0, r.srcObject = new MediaStream([e]), o.set(e, r), r.play().catch(() => void 0);
|
|
385
364
|
}
|
|
386
|
-
function
|
|
387
|
-
if (!
|
|
388
|
-
let t =
|
|
389
|
-
t && (t.pause(), t.srcObject = null, t.remove(),
|
|
365
|
+
function j(e) {
|
|
366
|
+
if (!h(e)) return;
|
|
367
|
+
let t = o.get(e);
|
|
368
|
+
t && (t.pause(), t.srcObject = null, t.remove(), o.delete(e));
|
|
390
369
|
}
|
|
391
|
-
function
|
|
392
|
-
if (!
|
|
370
|
+
function M(e, n) {
|
|
371
|
+
if (!h(e) || e.kind !== "audio") return;
|
|
393
372
|
let r = t.tracks?.().local?.audio;
|
|
394
|
-
n?.local === !0 || e === r ?
|
|
373
|
+
n?.local === !0 || e === r ? E(e) : k(n) && D(e), A(e, n);
|
|
395
374
|
}
|
|
396
|
-
function
|
|
397
|
-
e ===
|
|
375
|
+
function N(e) {
|
|
376
|
+
e === l && C(), e === u && w(), j(e);
|
|
398
377
|
}
|
|
399
|
-
function
|
|
400
|
-
|
|
378
|
+
function P() {
|
|
379
|
+
o.forEach((e) => {
|
|
401
380
|
e.pause(), e.srcObject = null, e.remove();
|
|
402
|
-
}),
|
|
381
|
+
}), o.clear();
|
|
403
382
|
}
|
|
404
|
-
function
|
|
405
|
-
|
|
383
|
+
function F() {
|
|
384
|
+
T(), b("idle");
|
|
406
385
|
}
|
|
407
|
-
function
|
|
408
|
-
|
|
386
|
+
function I() {
|
|
387
|
+
b("listening"), O();
|
|
409
388
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
[
|
|
415
|
-
[
|
|
389
|
+
function L() {
|
|
390
|
+
s.state !== "speaking" && b("thinking");
|
|
391
|
+
}
|
|
392
|
+
let R = [
|
|
393
|
+
[f.connected, () => b("connecting")],
|
|
394
|
+
[f.botReady, I],
|
|
395
|
+
[f.disconnected, F],
|
|
396
|
+
[f.botDisconnected, F],
|
|
397
|
+
[f.transportStateChanged, (e) => {
|
|
416
398
|
if (typeof e != "string") return;
|
|
417
|
-
let t =
|
|
418
|
-
t &&
|
|
399
|
+
let t = m(e);
|
|
400
|
+
t && b(t);
|
|
419
401
|
}],
|
|
420
|
-
[
|
|
421
|
-
[
|
|
422
|
-
|
|
402
|
+
[f.userStartedSpeaking, () => b("listening")],
|
|
403
|
+
[f.userStoppedSpeaking, () => {
|
|
404
|
+
s.state === "listening" && b("thinking");
|
|
423
405
|
}],
|
|
424
|
-
[
|
|
425
|
-
[
|
|
426
|
-
[
|
|
427
|
-
[
|
|
428
|
-
[
|
|
429
|
-
[
|
|
430
|
-
["trackStarted",
|
|
431
|
-
["trackStopped",
|
|
432
|
-
[
|
|
433
|
-
[
|
|
434
|
-
[
|
|
406
|
+
[f.botLlmStarted, L],
|
|
407
|
+
[f.botTtsStarted, L],
|
|
408
|
+
[f.botStartedSpeaking, () => b("speaking")],
|
|
409
|
+
[f.botStoppedSpeaking, () => b("listening")],
|
|
410
|
+
[f.localAudioLevel, (e) => !d && x(e)],
|
|
411
|
+
[f.remoteAudioLevel, (e, t) => !g && S(e, t)],
|
|
412
|
+
["trackStarted", M],
|
|
413
|
+
["trackStopped", N],
|
|
414
|
+
[f.error, (e) => b("error", e)],
|
|
415
|
+
[f.messageError, (e) => b("error", e)],
|
|
416
|
+
[f.deviceError, (e) => b("error", e)]
|
|
435
417
|
];
|
|
436
|
-
function
|
|
437
|
-
|
|
418
|
+
function z() {
|
|
419
|
+
c || (c = !0, R.forEach(([e, n]) => t.on(e, n)));
|
|
438
420
|
}
|
|
439
|
-
function
|
|
440
|
-
|
|
421
|
+
function B() {
|
|
422
|
+
c && (c = !1, R.forEach(([e, n]) => {
|
|
441
423
|
t.off ? t.off(e, n) : t.removeListener?.(e, n);
|
|
442
|
-
}),
|
|
424
|
+
}), T(), P());
|
|
443
425
|
}
|
|
444
426
|
return {
|
|
445
427
|
subscribe(e) {
|
|
446
|
-
return
|
|
447
|
-
|
|
428
|
+
return a.add(e), z(), e(s), () => {
|
|
429
|
+
a.delete(e), a.size === 0 && B();
|
|
448
430
|
};
|
|
449
431
|
},
|
|
450
432
|
async start() {
|
|
451
|
-
|
|
433
|
+
b("connecting");
|
|
452
434
|
try {
|
|
453
|
-
await (
|
|
435
|
+
await (n.connect ? n.connect() : t.connect());
|
|
454
436
|
} catch (e) {
|
|
455
|
-
throw
|
|
437
|
+
throw b("error", e), e;
|
|
456
438
|
}
|
|
457
439
|
},
|
|
458
440
|
async stop() {
|
|
459
441
|
try {
|
|
460
|
-
await (
|
|
442
|
+
await (n.disconnect ? n.disconnect() : t.disconnect());
|
|
461
443
|
} finally {
|
|
462
|
-
|
|
444
|
+
T(), P(), b("idle");
|
|
463
445
|
}
|
|
464
446
|
}
|
|
465
447
|
};
|
|
466
448
|
}
|
|
467
449
|
//#endregion
|
|
468
450
|
//#region src/adapters/openai-realtime/index.ts
|
|
469
|
-
var
|
|
470
|
-
|
|
471
|
-
gain: 4,
|
|
472
|
-
exponent: .8,
|
|
473
|
-
attack: .55,
|
|
474
|
-
release: .1
|
|
475
|
-
};
|
|
476
|
-
function T() {
|
|
451
|
+
var _ = "https://api.openai.com/v1/realtime/calls", v = .015, y = 8;
|
|
452
|
+
function b() {
|
|
477
453
|
let e = window.AudioContext;
|
|
478
454
|
return e ? new e() : void 0;
|
|
479
455
|
}
|
|
480
|
-
function
|
|
481
|
-
return Math.min(1, Math.max(0, (e * 4) ** .8));
|
|
482
|
-
}
|
|
483
|
-
function D(e, t, n) {
|
|
456
|
+
function x(e, t, n) {
|
|
484
457
|
try {
|
|
485
458
|
let r = t();
|
|
486
459
|
if (!r) return;
|
|
@@ -499,166 +472,148 @@ function D(e, t, n) {
|
|
|
499
472
|
return;
|
|
500
473
|
}
|
|
501
474
|
}
|
|
502
|
-
function
|
|
475
|
+
function S(e) {
|
|
503
476
|
let t = typeof e == "string" ? e : e.value;
|
|
504
477
|
if (!t) throw Error("[orb-ui/openai-realtime] getClientSecret returned an empty value.");
|
|
505
478
|
return t;
|
|
506
479
|
}
|
|
507
|
-
function
|
|
508
|
-
let t = /* @__PURE__ */ new Set(),
|
|
480
|
+
function C(e) {
|
|
481
|
+
let t = /* @__PURE__ */ new Set(), n = {
|
|
509
482
|
state: "idle",
|
|
510
|
-
volume: 0,
|
|
511
483
|
inputVolume: 0,
|
|
512
484
|
outputVolume: 0
|
|
513
|
-
},
|
|
514
|
-
function
|
|
515
|
-
|
|
516
|
-
return {
|
|
517
|
-
...w,
|
|
518
|
-
...t
|
|
519
|
-
};
|
|
520
|
-
}
|
|
521
|
-
function m(e) {
|
|
522
|
-
r = e, t.forEach((t) => t(e));
|
|
485
|
+
}, a = null, o = null, s = null, c = null, l, u, d = 0, f = "unknown", p = !1, m = !1, h = i(r.openai.input, e.inputVolumeCalibration), g = i(r.openai.output, e.outputVolumeCalibration);
|
|
486
|
+
function C(e) {
|
|
487
|
+
n = e, t.forEach((t) => t(e));
|
|
523
488
|
}
|
|
524
|
-
function
|
|
525
|
-
|
|
489
|
+
function w(e, t) {
|
|
490
|
+
if (n.state === e && t === void 0) return;
|
|
491
|
+
let r = e === "idle" || e === "connecting" || e === "error";
|
|
492
|
+
r && (h.reset(), g.reset()), C({
|
|
493
|
+
...n,
|
|
526
494
|
state: e,
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
495
|
+
...r ? {
|
|
496
|
+
inputVolume: 0,
|
|
497
|
+
outputVolume: 0
|
|
498
|
+
} : {},
|
|
530
499
|
...t === void 0 ? {} : { error: t }
|
|
531
|
-
}));
|
|
532
|
-
}
|
|
533
|
-
function g(e) {
|
|
534
|
-
if (r.state !== "listening") return;
|
|
535
|
-
let t = E(e);
|
|
536
|
-
m({
|
|
537
|
-
...r,
|
|
538
|
-
volume: t,
|
|
539
|
-
inputVolume: t,
|
|
540
|
-
outputVolume: 0
|
|
541
500
|
});
|
|
542
501
|
}
|
|
543
|
-
function
|
|
544
|
-
let
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
else if (r.state === "speaking" && (u += 1, u >= C)) {
|
|
549
|
-
u = 0, h("listening");
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
r.state === "speaking" && m({
|
|
553
|
-
...r,
|
|
554
|
-
volume: a,
|
|
555
|
-
inputVolume: 0,
|
|
556
|
-
outputVolume: a
|
|
502
|
+
function T(t) {
|
|
503
|
+
let r = h.sample(t);
|
|
504
|
+
e.onInputVolumeSample?.(r), C({
|
|
505
|
+
...n,
|
|
506
|
+
inputVolume: r.normalized
|
|
557
507
|
});
|
|
558
508
|
}
|
|
559
|
-
function
|
|
509
|
+
function E(t) {
|
|
510
|
+
let r = g.sample(t);
|
|
511
|
+
e.onOutputVolumeSample?.(r);
|
|
512
|
+
let i = r.normalized;
|
|
513
|
+
f === "unknown" && !p && n.state !== "idle" && n.state !== "connecting" && n.state !== "error" && (i > v ? (d = 0, n.state !== "speaking" && w("speaking")) : n.state === "speaking" && (d += 1, d >= y && (d = 0, w("listening")))), C({
|
|
514
|
+
...n,
|
|
515
|
+
outputVolume: i
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
function D(e) {
|
|
560
519
|
switch (e.type) {
|
|
561
520
|
case "session.created":
|
|
562
521
|
case "session.updated":
|
|
563
|
-
|
|
522
|
+
w("listening");
|
|
564
523
|
break;
|
|
565
524
|
case "input_audio_buffer.speech_started":
|
|
566
|
-
|
|
525
|
+
p = !0, w("listening");
|
|
567
526
|
break;
|
|
568
527
|
case "input_audio_buffer.speech_stopped":
|
|
528
|
+
p = !1, w(f === "playing" ? "speaking" : "thinking");
|
|
529
|
+
break;
|
|
569
530
|
case "response.created":
|
|
570
|
-
|
|
531
|
+
!p && f !== "playing" && w("thinking");
|
|
571
532
|
break;
|
|
572
533
|
case "response.output_audio.delta":
|
|
534
|
+
p || w("speaking");
|
|
535
|
+
break;
|
|
573
536
|
case "output_audio_buffer.started":
|
|
574
|
-
|
|
537
|
+
f = "playing", d = 0, p || w("speaking");
|
|
575
538
|
break;
|
|
576
539
|
case "output_audio_buffer.stopped":
|
|
577
|
-
|
|
540
|
+
case "output_audio_buffer.cleared":
|
|
541
|
+
f = "stopped", d = 0, w("listening");
|
|
578
542
|
break;
|
|
579
543
|
case "response.done":
|
|
580
544
|
case "response.cancelled":
|
|
581
|
-
|
|
545
|
+
n.state !== "speaking" && w("listening");
|
|
582
546
|
break;
|
|
583
547
|
case "error":
|
|
584
|
-
|
|
548
|
+
w("error", e.error ?? e);
|
|
585
549
|
break;
|
|
586
550
|
}
|
|
587
551
|
}
|
|
588
|
-
async function
|
|
589
|
-
|
|
552
|
+
async function O(e) {
|
|
553
|
+
m = !0, o?.close(), o = null, a?.close(), a = null, s?.getTracks().forEach((e) => e.stop()), s = null, c?.pause(), c && (c.srcObject = null), c?.remove(), c = null, await Promise.all([l?.stop(), u?.stop()]), l = void 0, u = void 0, d = 0, f = "unknown", p = !1, h.reset(), g.reset(), e && w("idle"), m = !1;
|
|
590
554
|
}
|
|
591
555
|
return {
|
|
592
556
|
subscribe(e) {
|
|
593
|
-
return t.add(e), e(
|
|
557
|
+
return t.add(e), e(n), () => t.delete(e);
|
|
594
558
|
},
|
|
595
559
|
async start() {
|
|
596
|
-
if (!
|
|
597
|
-
|
|
560
|
+
if (!a) {
|
|
561
|
+
w("connecting"), m = !1;
|
|
598
562
|
try {
|
|
599
|
-
let t = e.getUserMedia ?? ((e) => navigator.mediaDevices.getUserMedia(e)), n = e.createPeerConnection ?? (() => new RTCPeerConnection()), r = e.fetch ?? fetch,
|
|
600
|
-
|
|
601
|
-
let
|
|
602
|
-
|
|
563
|
+
let t = e.getUserMedia ?? ((e) => navigator.mediaDevices.getUserMedia(e)), n = e.createPeerConnection ?? (() => new RTCPeerConnection()), r = e.fetch ?? fetch, i = e.createAudioElement ?? (() => new Audio()), d = e.createAudioContext ?? b, f = S(await e.getClientSecret());
|
|
564
|
+
s = await t(e.mediaStreamConstraints ?? { audio: !0 }), l = x(s, d, T);
|
|
565
|
+
let p = n();
|
|
566
|
+
a = p, s.getAudioTracks().forEach((e) => p.addTrack(e, s)), c = i(), c.autoplay = !0, p.ontrack = (e) => {
|
|
603
567
|
let t = e.streams[0] ?? new MediaStream([e.track]);
|
|
604
|
-
|
|
605
|
-
},
|
|
606
|
-
|
|
568
|
+
c && (c.srcObject = t), c?.play().catch(() => void 0), u?.stop(), u = x(t, d, E);
|
|
569
|
+
}, p.onconnectionstatechange = () => {
|
|
570
|
+
m || (p.connectionState === "failed" ? w("error", /* @__PURE__ */ Error("[orb-ui/openai-realtime] WebRTC connection failed.")) : (p.connectionState === "disconnected" || p.connectionState === "closed") && w("idle"));
|
|
607
571
|
};
|
|
608
|
-
let
|
|
609
|
-
|
|
572
|
+
let h = p.createDataChannel("oai-events");
|
|
573
|
+
o = h, h.onopen = () => w("listening"), h.onmessage = (e) => {
|
|
610
574
|
try {
|
|
611
|
-
|
|
575
|
+
D(JSON.parse(String(e.data)));
|
|
612
576
|
} catch {}
|
|
613
|
-
},
|
|
614
|
-
|
|
577
|
+
}, h.onerror = (e) => w("error", e), h.onclose = () => {
|
|
578
|
+
m || w("idle");
|
|
615
579
|
};
|
|
616
|
-
let
|
|
617
|
-
await
|
|
618
|
-
let
|
|
580
|
+
let g = await p.createOffer();
|
|
581
|
+
await p.setLocalDescription(g);
|
|
582
|
+
let v = await r(e.callsUrl ?? _, {
|
|
619
583
|
method: "POST",
|
|
620
|
-
body:
|
|
584
|
+
body: g.sdp,
|
|
621
585
|
headers: {
|
|
622
|
-
Authorization: `Bearer ${
|
|
586
|
+
Authorization: `Bearer ${f}`,
|
|
623
587
|
"Content-Type": "application/sdp"
|
|
624
588
|
}
|
|
625
589
|
});
|
|
626
|
-
if (!
|
|
627
|
-
let e = await
|
|
628
|
-
throw Error(`[orb-ui/openai-realtime] Session negotiation failed (${
|
|
590
|
+
if (!v.ok) {
|
|
591
|
+
let e = await v.text();
|
|
592
|
+
throw Error(`[orb-ui/openai-realtime] Session negotiation failed (${v.status}): ${e}`);
|
|
629
593
|
}
|
|
630
|
-
await
|
|
594
|
+
await p.setRemoteDescription({
|
|
631
595
|
type: "answer",
|
|
632
|
-
sdp: await
|
|
596
|
+
sdp: await v.text()
|
|
633
597
|
});
|
|
634
598
|
} catch (e) {
|
|
635
|
-
throw await
|
|
599
|
+
throw await O(!1), w("error", e), e;
|
|
636
600
|
}
|
|
637
601
|
}
|
|
638
602
|
},
|
|
639
603
|
async stop() {
|
|
640
|
-
await
|
|
604
|
+
await O(!0);
|
|
641
605
|
}
|
|
642
606
|
};
|
|
643
607
|
}
|
|
644
608
|
//#endregion
|
|
645
609
|
//#region src/adapters/gemini-live/index.ts
|
|
646
|
-
var
|
|
647
|
-
|
|
648
|
-
gain: 4,
|
|
649
|
-
exponent: .8,
|
|
650
|
-
attack: .3,
|
|
651
|
-
release: .1
|
|
652
|
-
};
|
|
653
|
-
function M(e) {
|
|
610
|
+
var w = 16e3;
|
|
611
|
+
function T(e) {
|
|
654
612
|
let t = 0;
|
|
655
613
|
for (let n of e) t += n * n;
|
|
656
614
|
return Math.sqrt(t / e.length);
|
|
657
615
|
}
|
|
658
|
-
function
|
|
659
|
-
return Math.min(1, Math.max(0, (M(e) * 4) ** .8));
|
|
660
|
-
}
|
|
661
|
-
function P(e, t, n) {
|
|
616
|
+
function E(e, t, n) {
|
|
662
617
|
if (t === n) return e;
|
|
663
618
|
let r = t / n, i = Math.max(1, Math.round(e.length / r)), a = new Float32Array(i);
|
|
664
619
|
for (let t = 0; t < i; t += 1) {
|
|
@@ -667,7 +622,7 @@ function P(e, t, n) {
|
|
|
667
622
|
}
|
|
668
623
|
return a;
|
|
669
624
|
}
|
|
670
|
-
function
|
|
625
|
+
function D(e) {
|
|
671
626
|
let t = new Uint8Array(e.length * 2), n = new DataView(t.buffer);
|
|
672
627
|
e.forEach((e, t) => {
|
|
673
628
|
let r = Math.max(-1, Math.min(1, e));
|
|
@@ -678,154 +633,147 @@ function F(e) {
|
|
|
678
633
|
r += String.fromCharCode(e);
|
|
679
634
|
}), btoa(r);
|
|
680
635
|
}
|
|
681
|
-
function
|
|
636
|
+
function O(e) {
|
|
682
637
|
let t = atob(e), n = new Uint8Array(t.length);
|
|
683
638
|
for (let e = 0; e < t.length; e += 1) n[e] = t.charCodeAt(e);
|
|
684
639
|
let r = new DataView(n.buffer), i = new Float32Array(Math.floor(n.length / 2));
|
|
685
640
|
for (let e = 0; e < i.length; e += 1) i[e] = r.getInt16(e * 2, !0) / 32768;
|
|
686
641
|
return i;
|
|
687
642
|
}
|
|
688
|
-
function
|
|
643
|
+
function k(e) {
|
|
689
644
|
let t = e?.match(/rate=(\d+)/i);
|
|
690
645
|
return t ? Number(t[1]) : 24e3;
|
|
691
646
|
}
|
|
692
|
-
function
|
|
693
|
-
let t = /* @__PURE__ */ new Set(),
|
|
647
|
+
function A(e) {
|
|
648
|
+
let t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), a = {
|
|
694
649
|
state: "idle",
|
|
695
|
-
volume: 0,
|
|
696
650
|
inputVolume: 0,
|
|
697
651
|
outputVolume: 0
|
|
698
|
-
},
|
|
699
|
-
function
|
|
652
|
+
}, o = null, s = null, c = null, l = null, u = null, d = null, f = null, p = null, m = null, h = !1, g = 0, _ = !1, v = !1, y = i(r.gemini.input, e.inputVolumeCalibration), b = i(r.gemini.output, e.outputVolumeCalibration);
|
|
653
|
+
function x() {
|
|
700
654
|
return e.activityDetection !== "server";
|
|
701
655
|
}
|
|
702
|
-
function
|
|
703
|
-
|
|
704
|
-
return {
|
|
705
|
-
...j,
|
|
706
|
-
...t
|
|
707
|
-
};
|
|
656
|
+
function S(e) {
|
|
657
|
+
a = e, t.forEach((t) => t(e));
|
|
708
658
|
}
|
|
709
|
-
function
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
659
|
+
function C(e, t) {
|
|
660
|
+
if (a.state === e && t === void 0) return;
|
|
661
|
+
let n = e === "idle" || e === "connecting" || e === "error";
|
|
662
|
+
n && (y.reset(), b.reset()), S({
|
|
663
|
+
...a,
|
|
714
664
|
state: e,
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
665
|
+
...n ? {
|
|
666
|
+
inputVolume: 0,
|
|
667
|
+
outputVolume: 0
|
|
668
|
+
} : {},
|
|
718
669
|
...t === void 0 ? {} : { error: t }
|
|
719
|
-
})
|
|
670
|
+
});
|
|
720
671
|
}
|
|
721
|
-
function
|
|
722
|
-
|
|
723
|
-
}
|
|
724
|
-
function
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
672
|
+
function A() {
|
|
673
|
+
m &&= (clearTimeout(m), null);
|
|
674
|
+
}
|
|
675
|
+
function j(t) {
|
|
676
|
+
let n = y.sample(t);
|
|
677
|
+
e.onInputVolumeSample?.(n);
|
|
678
|
+
let r = n.normalized, i = e.speechThreshold ?? .04;
|
|
679
|
+
n.mapped > i ? (!h && x() && o?.sendRealtimeInput({ activityStart: {} }), h = !0, A(), a.state !== "listening" && C("listening")) : h && !m && (m = setTimeout(() => {
|
|
680
|
+
m = null, h = !1, x() && o?.sendRealtimeInput({ activityEnd: {} }), a.state === "listening" && C("thinking");
|
|
681
|
+
}, e.speechEndDelayMs ?? 500)), S({
|
|
682
|
+
...a,
|
|
683
|
+
inputVolume: r
|
|
732
684
|
});
|
|
733
685
|
}
|
|
734
|
-
function
|
|
735
|
-
|
|
686
|
+
function M() {
|
|
687
|
+
n.forEach((e) => {
|
|
736
688
|
try {
|
|
737
689
|
e.stop();
|
|
738
690
|
} catch {}
|
|
739
691
|
e.disconnect();
|
|
740
|
-
}),
|
|
741
|
-
}
|
|
742
|
-
function
|
|
743
|
-
_ &&
|
|
692
|
+
}), n.clear(), g = c?.currentTime ?? 0;
|
|
693
|
+
}
|
|
694
|
+
function N() {
|
|
695
|
+
_ && n.size === 0 && (_ = !1, C("listening"));
|
|
696
|
+
}
|
|
697
|
+
function P(e) {
|
|
698
|
+
if (!c || !f || !e.data) return;
|
|
699
|
+
let t = O(e.data), r = c.createBuffer(1, t.length, k(e.mimeType));
|
|
700
|
+
r.getChannelData(0).set(t);
|
|
701
|
+
let i = c.createBufferSource();
|
|
702
|
+
i.buffer = r, i.connect(f);
|
|
703
|
+
let o = Math.max(c.currentTime, g);
|
|
704
|
+
g = o + r.duration, n.add(i), i.onended = () => {
|
|
705
|
+
n.delete(i), i.disconnect(), N();
|
|
706
|
+
}, i.start(o), !h && a.state !== "speaking" && C("speaking");
|
|
744
707
|
}
|
|
745
|
-
function
|
|
746
|
-
|
|
747
|
-
let t = I(e.data), n = s.createBuffer(1, t.length, L(e.mimeType));
|
|
748
|
-
n.getChannelData(0).set(t);
|
|
749
|
-
let a = s.createBufferSource();
|
|
750
|
-
a.buffer = n, a.connect(d);
|
|
751
|
-
let o = Math.max(s.currentTime, h);
|
|
752
|
-
h = o + n.duration, r.add(a), a.onended = () => {
|
|
753
|
-
r.delete(a), a.disconnect(), E();
|
|
754
|
-
}, a.start(o), i.state !== "speaking" && S("speaking");
|
|
755
|
-
}
|
|
756
|
-
function O(e) {
|
|
757
|
-
e.setupComplete && S("listening");
|
|
708
|
+
function F(e) {
|
|
709
|
+
e.setupComplete && C("listening");
|
|
758
710
|
let t = e.serverContent;
|
|
759
711
|
if (!t) return;
|
|
760
712
|
if (t.interrupted) {
|
|
761
|
-
|
|
713
|
+
M(), _ = !1, C("listening");
|
|
762
714
|
return;
|
|
763
715
|
}
|
|
764
716
|
let n = t.modelTurn?.parts?.filter((e) => e.inlineData?.data) ?? [];
|
|
765
|
-
t.modelTurn && n.length === 0 &&
|
|
766
|
-
}
|
|
767
|
-
function
|
|
768
|
-
if (!
|
|
769
|
-
let t = new Float32Array(
|
|
770
|
-
|
|
771
|
-
if (!
|
|
772
|
-
|
|
773
|
-
let
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
...i,
|
|
778
|
-
volume: a,
|
|
779
|
-
inputVolume: 0,
|
|
780
|
-
outputVolume: a
|
|
717
|
+
t.modelTurn && n.length === 0 && a.state !== "speaking" && C("thinking"), n.forEach((e) => P(e.inlineData)), t.waitingForInput && (M(), C("listening")), (t.turnComplete || t.generationComplete) && (_ = !0, N());
|
|
718
|
+
}
|
|
719
|
+
function I() {
|
|
720
|
+
if (!f) return;
|
|
721
|
+
let t = new Float32Array(f.fftSize);
|
|
722
|
+
p = setInterval(() => {
|
|
723
|
+
if (!f) return;
|
|
724
|
+
f.getFloatTimeDomainData(t);
|
|
725
|
+
let n = b.sample(T(t));
|
|
726
|
+
e.onOutputVolumeSample?.(n), S({
|
|
727
|
+
...a,
|
|
728
|
+
outputVolume: n.normalized
|
|
781
729
|
});
|
|
782
730
|
}, 33);
|
|
783
731
|
}
|
|
784
|
-
function
|
|
785
|
-
!
|
|
786
|
-
if (!
|
|
732
|
+
function L() {
|
|
733
|
+
!c || !s || !o || (l = c.createMediaStreamSource(s), u = c.createScriptProcessor(4096, 1, 1), d = c.createGain(), d.gain.value = 0, l.connect(u), u.connect(d), d.connect(c.destination), u.onaudioprocess = (t) => {
|
|
734
|
+
if (!o || v) return;
|
|
787
735
|
let n = t.inputBuffer.getChannelData(0);
|
|
788
|
-
|
|
789
|
-
let r =
|
|
790
|
-
|
|
791
|
-
data:
|
|
792
|
-
mimeType: `audio/pcm;rate=${e.inputSampleRate ??
|
|
736
|
+
j(T(n));
|
|
737
|
+
let r = E(n, c?.sampleRate ?? w, e.inputSampleRate ?? w);
|
|
738
|
+
o.sendRealtimeInput({ audio: {
|
|
739
|
+
data: D(r),
|
|
740
|
+
mimeType: `audio/pcm;rate=${e.inputSampleRate ?? w}`
|
|
793
741
|
} });
|
|
794
742
|
});
|
|
795
743
|
}
|
|
796
|
-
async function
|
|
797
|
-
v = !0,
|
|
744
|
+
async function R(e) {
|
|
745
|
+
v = !0, A(), p && clearInterval(p), p = null, M(), u?.disconnect(), l?.disconnect(), d?.disconnect(), f?.disconnect(), u = null, l = null, d = null, f = null, s?.getTracks().forEach((e) => e.stop()), s = null, c && c.state !== "closed" && await c.close(), c = null, o = null, h = !1, _ = !1, y.reset(), b.reset(), e && C("idle"), v = !1;
|
|
798
746
|
}
|
|
799
747
|
return {
|
|
800
748
|
subscribe(e) {
|
|
801
|
-
return t.add(e), e(
|
|
749
|
+
return t.add(e), e(a), () => t.delete(e);
|
|
802
750
|
},
|
|
803
751
|
async start() {
|
|
804
|
-
if (!
|
|
805
|
-
|
|
752
|
+
if (!o) {
|
|
753
|
+
C("connecting"), v = !1;
|
|
806
754
|
try {
|
|
807
755
|
let t = e.getUserMedia ?? ((e) => navigator.mediaDevices.getUserMedia(e)), n = e.createAudioContext ?? (() => new window.AudioContext());
|
|
808
|
-
|
|
809
|
-
onopen: () =>
|
|
810
|
-
onmessage:
|
|
811
|
-
onerror: (e) =>
|
|
756
|
+
s = await t(e.mediaStreamConstraints ?? { audio: !0 }), c = n(), c.state === "suspended" && await c.resume(), f = c.createAnalyser(), f.fftSize = 512, f.smoothingTimeConstant = .25, f.connect(c.destination), I(), o = await e.connect({
|
|
757
|
+
onopen: () => C("listening"),
|
|
758
|
+
onmessage: F,
|
|
759
|
+
onerror: (e) => C("error", e),
|
|
812
760
|
onclose: () => {
|
|
813
|
-
v ||
|
|
761
|
+
v || C("idle");
|
|
814
762
|
}
|
|
815
|
-
}),
|
|
763
|
+
}), L(), C("listening");
|
|
816
764
|
} catch (e) {
|
|
817
|
-
throw
|
|
765
|
+
throw o?.close(), await R(!1), C("error", e), e;
|
|
818
766
|
}
|
|
819
767
|
}
|
|
820
768
|
},
|
|
821
769
|
async stop() {
|
|
822
770
|
v = !0;
|
|
823
771
|
try {
|
|
824
|
-
|
|
772
|
+
h && x() && o?.sendRealtimeInput({ activityEnd: {} }), o?.sendRealtimeInput({ audioStreamEnd: !0 });
|
|
825
773
|
} catch {}
|
|
826
|
-
|
|
774
|
+
o?.close(), await R(!0);
|
|
827
775
|
}
|
|
828
776
|
};
|
|
829
777
|
}
|
|
830
778
|
//#endregion
|
|
831
|
-
export {
|
|
779
|
+
export { a as DEFAULT_VOLUME_CALIBRATION, r as PROVIDER_VOLUME_CALIBRATIONS, n as calibrateVolume, d as createElevenLabsAdapter, A as createGeminiLiveAdapter, s as createLiveKitAdapter, C as createOpenAIRealtimeAdapter, g as createPipecatAdapter, u as createVapiAdapter, i as createVolumeNormalizer, o as fitVolumeCalibration, t as mapVolumeAmplitude };
|