orb-ui 0.6.1 → 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.
Files changed (39) hide show
  1. package/README.md +99 -37
  2. package/dist/adapters/audio-level.d.ts +57 -20
  3. package/dist/adapters/elevenlabs/index.d.ts +13 -2
  4. package/dist/adapters/gemini-live/index.d.ts +9 -5
  5. package/dist/adapters/index.d.ts +3 -2
  6. package/dist/adapters/livekit/browser.d.ts +9 -5
  7. package/dist/adapters/livekit/index.d.ts +9 -5
  8. package/dist/adapters/openai-realtime/index.d.ts +9 -5
  9. package/dist/adapters/pipecat/index.d.ts +9 -5
  10. package/dist/adapters/types.d.ts +0 -5
  11. package/dist/adapters/vapi/index.d.ts +29 -3
  12. package/dist/adapters/volume-presets.d.ts +35 -0
  13. package/dist/adapters.cjs +1 -1
  14. package/dist/adapters.js +405 -457
  15. package/dist/components/Orb/Orb.d.ts +1 -1
  16. package/dist/components/Orb/Orb.types.d.ts +92 -15
  17. package/dist/components/Orb/OrbThemeProvider.d.ts +16 -0
  18. package/dist/components/Orb/index.d.ts +6 -1
  19. package/dist/components/Orb/signals.d.ts +1 -1
  20. package/dist/index.d.ts +2 -2
  21. package/dist/livekit-7Re8E7By.js +394 -0
  22. package/dist/livekit-C36Kmz6s.cjs +1 -0
  23. package/dist/livekit-adapter.cjs +1 -1
  24. package/dist/livekit-adapter.js +6 -4
  25. package/dist/orb-ui.cjs +96 -4
  26. package/dist/orb-ui.js +1461 -407
  27. package/dist/themes/bars/BarsTheme.d.ts +8 -4
  28. package/dist/themes/circle/CircleTheme.d.ts +8 -4
  29. package/dist/themes/cloud/CloudTheme.d.ts +8 -4
  30. package/dist/themes/config.d.ts +151 -0
  31. package/dist/themes/css-variables.d.ts +5 -0
  32. package/dist/themes/debug/DebugTheme.d.ts +11 -6
  33. package/dist/themes/index.d.ts +1 -0
  34. package/dist/themes/radial/RadialTheme.d.ts +19 -0
  35. package/dist/themes/radial/index.d.ts +1 -0
  36. package/dist/themes/slots.d.ts +4 -0
  37. package/package.json +3 -2
  38. package/dist/livekit-CukEeYFK.js +0 -316
  39. package/dist/livekit-Dt-QZ6Gs.cjs +0 -1
package/dist/adapters.js CHANGED
@@ -1,7 +1,7 @@
1
- import { i as e, n as t, r as n, t as r } from "./livekit-CukEeYFK.js";
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 i = .12;
4
- function a(e) {
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 o(e, t) {
24
- let n = /* @__PURE__ */ new Set(), r = null;
25
- function o() {
26
- r || (r = e.start.bind(e), e.start = async (...e) => (n.forEach((e) => e()), r(...e)));
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 s() {
29
- n.size > 0 || !r || (e.start = r, r = null);
28
+ function d() {
29
+ a.size > 0 || !s || (t.start = s, s = null);
30
30
  }
31
31
  return {
32
32
  async start() {
33
- await e.start(t?.assistantId);
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(t) {
39
- let r = {
38
+ subscribe(s) {
39
+ let f = {
40
40
  state: "idle",
41
- volume: 0,
41
+ inputVolume: 0,
42
42
  outputVolume: 0
43
- }, c = 0;
44
- function l(e) {
45
- let t = e < i ? 0 : (e - i) / (1 - i), n = t > c ? .8 : .5;
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 d(e) {
52
- u({
53
- ...r,
47
+ function g(e) {
48
+ h({
49
+ ...f,
54
50
  ...e,
55
- state: e.state ?? r.state
51
+ state: e.state ?? f.state
56
52
  });
57
53
  }
58
- let { emitState: f, clearTimer: p } = a((e) => d({ state: e })), m = () => f("connecting"), h = "idle", g = !1, _ = () => {
59
- g = !0, h = "listening", f("listening"), d({
60
- volume: 0,
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
- }, v = () => {
64
- g = !1, h = "idle", E(), f("idle"), d({
65
- volume: 0,
66
- outputVolume: 0
67
- }), c = 0;
68
- }, y = () => {
69
- g && (h = "speaking", f("speaking"), d({
70
- volume: 0,
71
- outputVolume: 0
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
- }), c = 0, f("listening"));
78
- }, x = 0, S = 0, C = 0, w = () => {
79
- h === "speaking" && (S += (x - S) * .1, d({
80
- volume: S,
81
- outputVolume: S
82
- })), C = requestAnimationFrame(w);
83
- }, T = () => {
84
- C ||= requestAnimationFrame(w);
85
- }, E = () => {
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
- }, O = () => {}, k = (e) => {
93
- console.error("[orb-ui/vapi] Error:", e), g = !1, h = "error", p(), E(), d({
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
- volume: 0,
109
+ inputVolume: 0,
96
110
  outputVolume: 0,
97
111
  error: e
98
- }), c = 0;
112
+ });
99
113
  };
100
- return e.on("call-start", _), e.on("call-end", v), e.on("speech-start", y), e.on("speech-end", b), e.on("volume-level", D), e.on("message", O), e.on("error", k), n.add(m), o(), () => {
101
- p(), E(), c = 0, e.removeListener("call-start", _), e.removeListener("call-end", v), e.removeListener("speech-start", y), e.removeListener("speech-end", b), e.removeListener("volume-level", D), e.removeListener("message", O), e.removeListener("error", k), n.delete(m), s();
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 s(e, t) {
109
- let n = null, r = 0, i = null, a = null, o = {
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
- }, s = "idle", c = /* @__PURE__ */ new Set();
115
- function l(e) {
116
- o = e, c.forEach((t) => t(e));
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 u(e) {
119
- o = {
120
- ...o,
131
+ function y(e) {
132
+ p = {
133
+ ...p,
121
134
  ...e,
122
- state: e.state ?? o.state
123
- }, l(o);
124
- }
125
- function d(e) {
126
- s = e, u({ state: e });
127
- }
128
- function f() {
129
- a ||= setInterval(() => {
130
- if (n) {
131
- if (s === "listening") {
132
- let e = Math.min(n.getInputVolume() * 2, 1);
133
- u({
134
- volume: e,
135
- inputVolume: e
136
- });
137
- } else if (s === "speaking") {
138
- let e = Math.min(n.getOutputVolume() * 2, 1);
139
- u({
140
- volume: e,
141
- outputVolume: e
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 p() {
148
- a &&= (clearInterval(a), null);
159
+ function S() {
160
+ f &&= (clearInterval(f), null);
149
161
  }
150
- function m() {
151
- p(), u({
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 h() {
158
- s = "idle", u({
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 g(e) {
166
- return e === r;
175
+ function T(e) {
176
+ return e === u;
167
177
  }
168
- function _(e) {
178
+ function E(e) {
169
179
  return {
170
180
  onStatusChange: ({ status: t }) => {
171
- g(e) && t === "connecting" && d("connecting");
181
+ T(e) && t === "connecting" && b("connecting");
172
182
  },
173
183
  onConnect: () => {
174
- g(e) && (d("listening"), f());
184
+ T(e) && (b("listening"), x());
175
185
  },
176
186
  onModeChange: ({ mode: t }) => {
177
- g(e) && (t === "speaking" ? (d("speaking"), f()) : (d("listening"), f()));
187
+ T(e) && (t === "speaking" ? (b("speaking"), x()) : (b("listening"), x()));
178
188
  },
179
189
  onDisconnect: () => {
180
- g(e) && (p(), n = null, h());
190
+ T(e) && (S(), l = null, w());
181
191
  },
182
192
  onError: (t) => {
183
- g(e) && (console.error("[orb-ui/elevenlabs] Error:", t), p(), s = "error", u({
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
- }), n = null);
198
+ }), l = null);
190
199
  }
191
200
  };
192
201
  }
193
202
  return {
194
203
  subscribe(e) {
195
- return c.add(e), e(o), n && (s === "listening" || s === "speaking") && f(), () => {
196
- c.delete(e), c.size === 0 && m();
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 (n || i) return i ?? void 0;
201
- r += 1;
202
- let a = r;
203
- return i = (async () => {
209
+ if (l || d) return d ?? void 0;
210
+ u += 1;
211
+ let t = u;
212
+ return d = (async () => {
204
213
  try {
205
- let r = await e.startSession({
206
- ...t,
207
- ..._(a)
214
+ let n = await e.startSession({
215
+ ...c,
216
+ ...E(t)
208
217
  });
209
- if (!g(a)) {
210
- await r.endSession().catch((e) => {
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
- n = r;
224
+ l = n;
216
225
  } catch (e) {
217
- if (!g(a)) return;
218
- console.error("[orb-ui/elevenlabs] startSession failed:", e), d("error"), u({
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
- i = null;
233
+ d = null;
226
234
  }
227
- })(), i;
235
+ })(), d;
228
236
  },
229
237
  async stop() {
230
- let e = n;
238
+ let e = l;
231
239
  if (!e) {
232
- i && (r += 1, p(), h(), await i);
240
+ d && (u += 1, S(), w(), await d);
233
241
  return;
234
242
  }
235
- let t = r;
236
- p();
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
- n === e && g(t) && (n = null, h());
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 c = {
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 h() {
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 g(e) {
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 _(e) {
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 b(t, r = {}) {
305
- let i = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Map(), o = {
306
- state: g(t.state ?? "") ?? "idle",
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
- }, s = !1, l = 0, u = 0, d = null, f = null, p, _;
311
- function b() {
312
- let e = typeof r.outputVolumeCalibration == "function" ? r.outputVolumeCalibration() : r.outputVolumeCalibration;
313
- return {
314
- ...m,
315
- ...e
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
- volume: 0,
325
- inputVolume: 0,
326
- outputVolume: 0,
311
+ ...n ? {
312
+ inputVolume: 0,
313
+ outputVolume: 0
314
+ } : {},
327
315
  ...t === void 0 ? {} : { error: t }
328
316
  });
329
317
  }
330
- function C(e) {
331
- if (o.state !== "listening") return;
332
- l = v(e, l);
333
- let t = l;
334
- x({
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 w(e, t) {
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 || r.isBotParticipant && !r.isBotParticipant(e)) return;
328
+ if (e.local || n.isBotParticipant && !n.isBotParticipant(e)) return;
346
329
  }
347
- let i = n(typeof e == "number" ? e : 0, u, b);
348
- u = i.normalized, r.onOutputVolumeSample?.(i);
349
- let a = i.normalized;
350
- x({
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 T() {
358
- d = null, p?.stop(), p = void 0;
336
+ function C() {
337
+ l = null, d?.stop(), d = void 0;
359
338
  }
360
- function E() {
361
- f = null, _?.stop(), _ = void 0;
339
+ function w() {
340
+ u = null, g?.stop(), g = void 0;
362
341
  }
363
- function D() {
364
- T(), E();
342
+ function T() {
343
+ C(), w();
365
344
  }
366
- function O(t) {
367
- d === t && p || (T(), d = t, p = e(t, r.createAudioContext ?? h, C), p || (d = null));
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 k(t) {
370
- f === t && _ || (E(), f = t, _ = e(t, r.createAudioContext ?? h, w), _ || (f = null));
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 A() {
351
+ function O() {
373
352
  let e = t.tracks?.();
374
- e?.local?.audio && O(e.local.audio), e?.bot?.audio && k(e.bot.audio);
353
+ e?.local?.audio && E(e.local.audio), e?.bot?.audio && D(e.bot.audio);
375
354
  }
376
- function j(e) {
355
+ function k(e) {
377
356
  if (!e || typeof e != "object") return !0;
378
357
  let t = e;
379
- return t.local ? !1 : r.isBotParticipant?.(t) ?? !0;
358
+ return t.local ? !1 : n.isBotParticipant?.(t) ?? !0;
380
359
  }
381
- function M(e, t) {
382
- if (r.playRemoteAudio === !1 || !y(e) || e.kind !== "audio" || !j(t)) return;
383
- let n = r.createAudioElement?.() ?? new Audio();
384
- n.autoplay = !0, n.srcObject = new MediaStream([e]), a.set(e, n), n.play().catch(() => void 0);
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 N(e) {
387
- if (!y(e)) return;
388
- let t = a.get(e);
389
- t && (t.pause(), t.srcObject = null, t.remove(), a.delete(e));
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 P(e, n) {
392
- if (!y(e) || e.kind !== "audio") return;
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 ? O(e) : j(n) && k(e), M(e, n);
373
+ n?.local === !0 || e === r ? E(e) : k(n) && D(e), A(e, n);
395
374
  }
396
- function F(e) {
397
- e === d && T(), e === f && E(), N(e);
375
+ function N(e) {
376
+ e === l && C(), e === u && w(), j(e);
398
377
  }
399
- function I() {
400
- a.forEach((e) => {
378
+ function P() {
379
+ o.forEach((e) => {
401
380
  e.pause(), e.srcObject = null, e.remove();
402
- }), a.clear();
381
+ }), o.clear();
403
382
  }
404
- function L() {
405
- D(), S("idle");
383
+ function F() {
384
+ T(), b("idle");
406
385
  }
407
- function R() {
408
- S("listening"), A();
386
+ function I() {
387
+ b("listening"), O();
409
388
  }
410
- let z = [
411
- [c.connected, () => S("connecting")],
412
- [c.botReady, R],
413
- [c.disconnected, L],
414
- [c.botDisconnected, L],
415
- [c.transportStateChanged, (e) => {
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 = g(e);
418
- t && S(t);
399
+ let t = m(e);
400
+ t && b(t);
419
401
  }],
420
- [c.userStartedSpeaking, () => S("listening")],
421
- [c.userStoppedSpeaking, () => {
422
- o.state === "listening" && S("thinking");
402
+ [f.userStartedSpeaking, () => b("listening")],
403
+ [f.userStoppedSpeaking, () => {
404
+ s.state === "listening" && b("thinking");
423
405
  }],
424
- [c.botLlmStarted, () => S("thinking")],
425
- [c.botTtsStarted, () => S("thinking")],
426
- [c.botStartedSpeaking, () => S("speaking")],
427
- [c.botStoppedSpeaking, () => S("listening")],
428
- [c.localAudioLevel, (e) => !p && C(e)],
429
- [c.remoteAudioLevel, (e, t) => !_ && w(e, t)],
430
- ["trackStarted", P],
431
- ["trackStopped", F],
432
- [c.error, (e) => S("error", e)],
433
- [c.messageError, (e) => S("error", e)],
434
- [c.deviceError, (e) => S("error", e)]
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 B() {
437
- s || (s = !0, z.forEach(([e, n]) => t.on(e, n)));
418
+ function z() {
419
+ c || (c = !0, R.forEach(([e, n]) => t.on(e, n)));
438
420
  }
439
- function V() {
440
- s && (s = !1, z.forEach(([e, n]) => {
421
+ function B() {
422
+ c && (c = !1, R.forEach(([e, n]) => {
441
423
  t.off ? t.off(e, n) : t.removeListener?.(e, n);
442
- }), D(), I());
424
+ }), T(), P());
443
425
  }
444
426
  return {
445
427
  subscribe(e) {
446
- return i.add(e), B(), e(o), () => {
447
- i.delete(e), i.size === 0 && V();
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
- S("connecting");
433
+ b("connecting");
452
434
  try {
453
- await (r.connect ? r.connect() : t.connect());
435
+ await (n.connect ? n.connect() : t.connect());
454
436
  } catch (e) {
455
- throw S("error", e), e;
437
+ throw b("error", e), e;
456
438
  }
457
439
  },
458
440
  async stop() {
459
441
  try {
460
- await (r.disconnect ? r.disconnect() : t.disconnect());
442
+ await (n.disconnect ? n.disconnect() : t.disconnect());
461
443
  } finally {
462
- D(), I(), S("idle");
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 x = "https://api.openai.com/v1/realtime/calls", S = .015, C = 8, w = {
470
- noiseFloor: .003,
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 E(e) {
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 O(e) {
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 k(e) {
508
- let t = /* @__PURE__ */ new Set(), r = {
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
- }, i = null, a = null, o = null, s = null, c, l, u = 0, d = 0, f = !1;
514
- function p() {
515
- let t = typeof e.outputVolumeCalibration == "function" ? e.outputVolumeCalibration() : e.outputVolumeCalibration;
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 h(e, t) {
525
- r.state === e && t === void 0 || (e !== "speaking" && (d = 0), m({
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
- volume: 0,
528
- inputVolume: 0,
529
- outputVolume: 0,
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 _(t) {
544
- let i = n(t, d, p);
545
- d = i.normalized, e.onOutputVolumeSample?.(i);
546
- let a = i.normalized;
547
- if (a > S) u = 0, r.state !== "speaking" && h("speaking");
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 v(e) {
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
- h("listening");
522
+ w("listening");
564
523
  break;
565
524
  case "input_audio_buffer.speech_started":
566
- h("listening");
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
- h("thinking");
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
- h("speaking");
537
+ f = "playing", d = 0, p || w("speaking");
575
538
  break;
576
539
  case "output_audio_buffer.stopped":
577
- h("listening");
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
- r.state !== "speaking" && h("listening");
545
+ n.state !== "speaking" && w("listening");
582
546
  break;
583
547
  case "error":
584
- h("error", e.error ?? e);
548
+ w("error", e.error ?? e);
585
549
  break;
586
550
  }
587
551
  }
588
- async function y(e) {
589
- f = !0, a?.close(), a = null, i?.close(), i = null, o?.getTracks().forEach((e) => e.stop()), o = null, s?.pause(), s && (s.srcObject = null), s?.remove(), s = null, await Promise.all([c?.stop(), l?.stop()]), c = void 0, l = void 0, u = 0, d = 0, e && h("idle"), f = !1;
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(r), () => t.delete(e);
557
+ return t.add(e), e(n), () => t.delete(e);
594
558
  },
595
559
  async start() {
596
- if (!i) {
597
- h("connecting"), f = !1;
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, u = e.createAudioElement ?? (() => new Audio()), d = e.createAudioContext ?? T, p = O(await e.getClientSecret());
600
- o = await t(e.mediaStreamConstraints ?? { audio: !0 }), c = D(o, d, g);
601
- let m = n();
602
- i = m, o.getAudioTracks().forEach((e) => m.addTrack(e, o)), s = u(), s.autoplay = !0, m.ontrack = (e) => {
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
- s && (s.srcObject = t), s?.play().catch(() => void 0), l?.stop(), l = D(t, d, _);
605
- }, m.onconnectionstatechange = () => {
606
- f || (m.connectionState === "failed" ? h("error", /* @__PURE__ */ Error("[orb-ui/openai-realtime] WebRTC connection failed.")) : (m.connectionState === "disconnected" || m.connectionState === "closed") && h("idle"));
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 y = m.createDataChannel("oai-events");
609
- a = y, y.onopen = () => h("listening"), y.onmessage = (e) => {
572
+ let h = p.createDataChannel("oai-events");
573
+ o = h, h.onopen = () => w("listening"), h.onmessage = (e) => {
610
574
  try {
611
- v(JSON.parse(String(e.data)));
575
+ D(JSON.parse(String(e.data)));
612
576
  } catch {}
613
- }, y.onerror = (e) => h("error", e), y.onclose = () => {
614
- f || h("idle");
577
+ }, h.onerror = (e) => w("error", e), h.onclose = () => {
578
+ m || w("idle");
615
579
  };
616
- let b = await m.createOffer();
617
- await m.setLocalDescription(b);
618
- let S = await r(e.callsUrl ?? x, {
580
+ let g = await p.createOffer();
581
+ await p.setLocalDescription(g);
582
+ let v = await r(e.callsUrl ?? _, {
619
583
  method: "POST",
620
- body: b.sdp,
584
+ body: g.sdp,
621
585
  headers: {
622
- Authorization: `Bearer ${p}`,
586
+ Authorization: `Bearer ${f}`,
623
587
  "Content-Type": "application/sdp"
624
588
  }
625
589
  });
626
- if (!S.ok) {
627
- let e = await S.text();
628
- throw Error(`[orb-ui/openai-realtime] Session negotiation failed (${S.status}): ${e}`);
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 m.setRemoteDescription({
594
+ await p.setRemoteDescription({
631
595
  type: "answer",
632
- sdp: await S.text()
596
+ sdp: await v.text()
633
597
  });
634
598
  } catch (e) {
635
- throw await y(!1), h("error", e), e;
599
+ throw await O(!1), w("error", e), e;
636
600
  }
637
601
  }
638
602
  },
639
603
  async stop() {
640
- await y(!0);
604
+ await O(!0);
641
605
  }
642
606
  };
643
607
  }
644
608
  //#endregion
645
609
  //#region src/adapters/gemini-live/index.ts
646
- var A = 16e3, j = {
647
- noiseFloor: .003,
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 N(e) {
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 F(e) {
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 I(e) {
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 L(e) {
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 R(e) {
693
- let t = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Set(), i = {
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
- }, a = null, o = null, s = null, c = null, l = null, u = null, d = null, f = null, p = null, m = !1, h = 0, g = 0, _ = !1, v = !1;
699
- function y() {
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 b() {
703
- let t = typeof e.outputVolumeCalibration == "function" ? e.outputVolumeCalibration() : e.outputVolumeCalibration;
704
- return {
705
- ...j,
706
- ...t
707
- };
656
+ function S(e) {
657
+ a = e, t.forEach((t) => t(e));
708
658
  }
709
- function x(e) {
710
- i = e, t.forEach((t) => t(e));
711
- }
712
- function S(e, t) {
713
- i.state === e && t === void 0 || (e !== "speaking" && (g = 0), x({
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
- volume: 0,
716
- inputVolume: 0,
717
- outputVolume: 0,
665
+ ...n ? {
666
+ inputVolume: 0,
667
+ outputVolume: 0
668
+ } : {},
718
669
  ...t === void 0 ? {} : { error: t }
719
- }));
670
+ });
720
671
  }
721
- function C() {
722
- p &&= (clearTimeout(p), null);
723
- }
724
- function w(t) {
725
- t > (e.speechThreshold ?? .04) ? (!m && y() && a?.sendRealtimeInput({ activityStart: {} }), m = !0, C(), i.state !== "listening" && S("listening")) : m && !p && (p = setTimeout(() => {
726
- p = null, m = !1, y() && a?.sendRealtimeInput({ activityEnd: {} }), i.state === "listening" && S("thinking");
727
- }, e.speechEndDelayMs ?? 500)), i.state === "listening" && x({
728
- ...i,
729
- volume: t,
730
- inputVolume: t,
731
- outputVolume: 0
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 T() {
735
- r.forEach((e) => {
686
+ function M() {
687
+ n.forEach((e) => {
736
688
  try {
737
689
  e.stop();
738
690
  } catch {}
739
691
  e.disconnect();
740
- }), r.clear(), h = s?.currentTime ?? 0;
741
- }
742
- function E() {
743
- _ && r.size === 0 && (_ = !1, S("listening"));
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 D(e) {
746
- if (!s || !d || !e.data) return;
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
- T(), _ = !1, S("listening");
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 && i.state !== "speaking" && S("thinking"), n.forEach((e) => D(e.inlineData)), t.waitingForInput && (T(), S("listening")), (t.turnComplete || t.generationComplete) && (_ = !0, E());
766
- }
767
- function k() {
768
- if (!d) return;
769
- let t = new Float32Array(d.fftSize);
770
- f = setInterval(() => {
771
- if (!d || i.state !== "speaking") return;
772
- d.getFloatTimeDomainData(t);
773
- let r = n(M(t), g, b);
774
- g = r.normalized, e.onOutputVolumeSample?.(r);
775
- let a = r.normalized;
776
- x({
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 R() {
785
- !s || !o || !a || (c = s.createMediaStreamSource(o), l = s.createScriptProcessor(4096, 1, 1), u = s.createGain(), u.gain.value = 0, c.connect(l), l.connect(u), u.connect(s.destination), l.onaudioprocess = (t) => {
786
- if (!a || v) return;
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
- w(N(n));
789
- let r = P(n, s?.sampleRate ?? A, e.inputSampleRate ?? A);
790
- a.sendRealtimeInput({ audio: {
791
- data: F(r),
792
- mimeType: `audio/pcm;rate=${e.inputSampleRate ?? A}`
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 z(e) {
797
- v = !0, C(), f && clearInterval(f), f = null, T(), l?.disconnect(), c?.disconnect(), u?.disconnect(), d?.disconnect(), l = null, c = null, u = null, d = null, o?.getTracks().forEach((e) => e.stop()), o = null, s && s.state !== "closed" && await s.close(), s = null, a = null, m = !1, _ = !1, g = 0, e && S("idle"), v = !1;
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(i), () => t.delete(e);
749
+ return t.add(e), e(a), () => t.delete(e);
802
750
  },
803
751
  async start() {
804
- if (!a) {
805
- S("connecting"), v = !1;
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
- o = await t(e.mediaStreamConstraints ?? { audio: !0 }), s = n(), s.state === "suspended" && await s.resume(), d = s.createAnalyser(), d.fftSize = 512, d.smoothingTimeConstant = .25, d.connect(s.destination), k(), a = await e.connect({
809
- onopen: () => S("listening"),
810
- onmessage: O,
811
- onerror: (e) => S("error", 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 || S("idle");
761
+ v || C("idle");
814
762
  }
815
- }), R(), S("listening");
763
+ }), L(), C("listening");
816
764
  } catch (e) {
817
- throw a?.close(), await z(!1), S("error", e), e;
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
- m && y() && a?.sendRealtimeInput({ activityEnd: {} }), a?.sendRealtimeInput({ audioStreamEnd: !0 });
772
+ h && x() && o?.sendRealtimeInput({ activityEnd: {} }), o?.sendRealtimeInput({ audioStreamEnd: !0 });
825
773
  } catch {}
826
- a?.close(), await z(!0);
774
+ o?.close(), await R(!0);
827
775
  }
828
776
  };
829
777
  }
830
778
  //#endregion
831
- export { t as DEFAULT_OUTPUT_VOLUME_CALIBRATION, s as createElevenLabsAdapter, R as createGeminiLiveAdapter, r as createLiveKitAdapter, k as createOpenAIRealtimeAdapter, b as createPipecatAdapter, o as createVapiAdapter };
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 };