@burdenoff/website-sdk 2026.1105.2 → 2026.1106.1
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 +133 -0
- package/dist/avatar-live-view/index.d.mts +1 -1
- package/dist/avatar-live-view/index.d.ts +1 -1
- package/dist/avatar-live-view/index.js +1071 -205
- package/dist/avatar-live-view/index.js.map +1 -1
- package/dist/avatar-live-view/index.mjs +1064 -198
- package/dist/avatar-live-view/index.mjs.map +1 -1
- package/dist/avatar-mic/index.d.mts +181 -62
- package/dist/avatar-mic/index.d.ts +181 -62
- package/dist/avatar-mic/index.js +717 -5
- package/dist/avatar-mic/index.js.map +1 -1
- package/dist/avatar-mic/index.mjs +717 -6
- package/dist/avatar-mic/index.mjs.map +1 -1
- package/dist/{chunk-MVNN6LLO.mjs → chunk-BHEHF3Y5.mjs} +2 -2
- package/dist/chunk-BHEHF3Y5.mjs.map +1 -0
- package/dist/{chunk-XIZE37IB.js → chunk-Z2UP22GV.js} +2 -2
- package/dist/chunk-Z2UP22GV.js.map +1 -0
- package/dist/{explore-avatar-live-config-auLKxSzx.d.ts → explore-avatar-live-config-CJstiSAN.d.ts} +10 -3
- package/dist/{explore-avatar-live-config-BL8FTm0I.d.mts → explore-avatar-live-config-D2PGF8fG.d.mts} +10 -3
- package/dist/index.d.mts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-MVNN6LLO.mjs.map +0 -1
- package/dist/chunk-XIZE37IB.js.map +0 -1
|
@@ -1,5 +1,716 @@
|
|
|
1
1
|
import { __publicField } from '../chunk-VNS5WDWO.mjs';
|
|
2
2
|
|
|
3
|
+
// src/avatar-mic/speech-words.ts
|
|
4
|
+
var FILLERS = /* @__PURE__ */ new Set([
|
|
5
|
+
"uh",
|
|
6
|
+
"um",
|
|
7
|
+
"uhm",
|
|
8
|
+
"umm",
|
|
9
|
+
"er",
|
|
10
|
+
"erm",
|
|
11
|
+
"eh",
|
|
12
|
+
"ah",
|
|
13
|
+
"hm",
|
|
14
|
+
"hmm",
|
|
15
|
+
"hmmm",
|
|
16
|
+
"mm",
|
|
17
|
+
"mmm",
|
|
18
|
+
"mhm",
|
|
19
|
+
"huh",
|
|
20
|
+
"oh",
|
|
21
|
+
"so",
|
|
22
|
+
"well",
|
|
23
|
+
"okay",
|
|
24
|
+
"ok",
|
|
25
|
+
"and",
|
|
26
|
+
"like",
|
|
27
|
+
"hey",
|
|
28
|
+
"hi",
|
|
29
|
+
"hello",
|
|
30
|
+
"actually"
|
|
31
|
+
]);
|
|
32
|
+
var OPEN_ENDINGS = /* @__PURE__ */ new Set([
|
|
33
|
+
"the",
|
|
34
|
+
"a",
|
|
35
|
+
"an",
|
|
36
|
+
"of",
|
|
37
|
+
"to",
|
|
38
|
+
"for",
|
|
39
|
+
"about",
|
|
40
|
+
"and",
|
|
41
|
+
"or",
|
|
42
|
+
"but",
|
|
43
|
+
"what",
|
|
44
|
+
"how",
|
|
45
|
+
"which",
|
|
46
|
+
"is",
|
|
47
|
+
"are",
|
|
48
|
+
"can"
|
|
49
|
+
]);
|
|
50
|
+
var PRONOUNS = /* @__PURE__ */ new Set([
|
|
51
|
+
"i",
|
|
52
|
+
"me",
|
|
53
|
+
"you",
|
|
54
|
+
"it",
|
|
55
|
+
"we",
|
|
56
|
+
"us",
|
|
57
|
+
"they",
|
|
58
|
+
"them",
|
|
59
|
+
"he",
|
|
60
|
+
"him",
|
|
61
|
+
"she",
|
|
62
|
+
"her",
|
|
63
|
+
"this",
|
|
64
|
+
"that",
|
|
65
|
+
"these",
|
|
66
|
+
"those"
|
|
67
|
+
]);
|
|
68
|
+
var ECHO_MIN_WORDS = 3;
|
|
69
|
+
var SELF_CONTAINED_SCRIPT = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
|
|
70
|
+
function tidyWords(text) {
|
|
71
|
+
return text.replace(/\s+/g, " ").trim();
|
|
72
|
+
}
|
|
73
|
+
function speechWords(text) {
|
|
74
|
+
const flat = text.toLowerCase().replace(/[^\p{L}\p{M}\p{N}]+/gu, " ").trim();
|
|
75
|
+
return flat === "" ? [] : flat.split(" ");
|
|
76
|
+
}
|
|
77
|
+
function startsWithWords(whole, head) {
|
|
78
|
+
return head.length <= whole.length && head.every((word, index) => whole[index] === word);
|
|
79
|
+
}
|
|
80
|
+
function endsWithWords(whole, tail) {
|
|
81
|
+
return tail.length <= whole.length && tail.every(
|
|
82
|
+
(word, index) => whole[whole.length - tail.length + index] === word
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
function startsWithWordsOf(text, head) {
|
|
86
|
+
return startsWithWords(speechWords(text), speechWords(head));
|
|
87
|
+
}
|
|
88
|
+
function mergeTurnPhrase(turn, phrase) {
|
|
89
|
+
const heard = speechWords(turn);
|
|
90
|
+
const incoming = speechWords(phrase);
|
|
91
|
+
if (incoming.length === 0) return turn;
|
|
92
|
+
if (heard.length === 0) return tidyWords(phrase);
|
|
93
|
+
if (startsWithWords(incoming, heard)) {
|
|
94
|
+
return incoming.length === heard.length ? turn : tidyWords(phrase);
|
|
95
|
+
}
|
|
96
|
+
if (incoming.length >= 2 && endsWithWords(heard, incoming)) return turn;
|
|
97
|
+
return tidyWords(`${turn} ${phrase}`);
|
|
98
|
+
}
|
|
99
|
+
function mergeInterim(turn, interim) {
|
|
100
|
+
const said = tidyWords(interim);
|
|
101
|
+
if (said === "") return turn;
|
|
102
|
+
const head = speechWords(turn);
|
|
103
|
+
if (head.length === 0) return said;
|
|
104
|
+
const tokens = said.split(" ");
|
|
105
|
+
let matched = 0;
|
|
106
|
+
for (let at = 0; at < tokens.length; at += 1) {
|
|
107
|
+
const words = speechWords(tokens[at] ?? "");
|
|
108
|
+
for (const word of words) {
|
|
109
|
+
if (word !== head[matched]) return tidyWords(`${turn} ${said}`);
|
|
110
|
+
matched += 1;
|
|
111
|
+
if (matched === head.length) {
|
|
112
|
+
return tidyWords(`${turn} ${tokens.slice(at + 1).join(" ")}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return tidyWords(`${turn} ${said}`);
|
|
117
|
+
}
|
|
118
|
+
function saidWords(words) {
|
|
119
|
+
return words.filter((word) => !FILLERS.has(word));
|
|
120
|
+
}
|
|
121
|
+
function isGenuineSpeech(text) {
|
|
122
|
+
const said = saidWords(speechWords(text));
|
|
123
|
+
if (said.length === 1 && PRONOUNS.has(said[0] ?? "")) return false;
|
|
124
|
+
const content = said.filter((word) => !OPEN_ENDINGS.has(word));
|
|
125
|
+
if (content.length === 0) return false;
|
|
126
|
+
const letters = [...content.join("")];
|
|
127
|
+
if (letters.length >= 2) return true;
|
|
128
|
+
return letters.length === 1 && SELF_CONTAINED_SCRIPT.test(letters[0] ?? "");
|
|
129
|
+
}
|
|
130
|
+
function waitsLonger(text) {
|
|
131
|
+
const words = speechWords(text);
|
|
132
|
+
const last = words[words.length - 1];
|
|
133
|
+
if (last === void 0) return false;
|
|
134
|
+
return saidWords(words).length <= 1 || OPEN_ENDINGS.has(last) || FILLERS.has(last);
|
|
135
|
+
}
|
|
136
|
+
function isEchoOf(text, spoken) {
|
|
137
|
+
if (spoken === null) return false;
|
|
138
|
+
const heard = speechWords(text);
|
|
139
|
+
if (heard.length < ECHO_MIN_WORDS) return false;
|
|
140
|
+
return ` ${speechWords(spoken).join(" ")} `.includes(` ${heard.join(" ")} `);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// src/avatar-mic/browser-press.ts
|
|
144
|
+
var BROWSER_START_WAIT_MS = 15e3;
|
|
145
|
+
var BROWSER_FINAL_WAIT_MS = 3e3;
|
|
146
|
+
var BROWSER_RESTART_COOLDOWN_MS = 2e3;
|
|
147
|
+
var BROWSER_TOUCH_RESTART_MS = 120;
|
|
148
|
+
var BROWSER_MAX_QUICK_ENDS = 8;
|
|
149
|
+
var BROWSER_MAX_RESTARTS = 60;
|
|
150
|
+
var BROWSER_PRIME_SETTLE_MS = 250;
|
|
151
|
+
var BROWSER_GRANT_RETRY_MS = 350;
|
|
152
|
+
var BROWSER_PAUSE_MIN_MS = 2e3;
|
|
153
|
+
var BROWSER_PAUSE_MAX_MS = 2500;
|
|
154
|
+
var BROWSER_PAUSE_AFTER_START_MS = 1500;
|
|
155
|
+
var BROWSER_PAUSE_SHOWN_MS = 800;
|
|
156
|
+
var BROWSER_ECHO_WINDOW_MS = 2500;
|
|
157
|
+
var QUICK_END_MS = 1e3;
|
|
158
|
+
var NETWORK_FAILURES_FOR_GOOD = 2;
|
|
159
|
+
var IDLE = { name: "idle" };
|
|
160
|
+
function isRecognitionConstructor(value) {
|
|
161
|
+
return typeof value === "function";
|
|
162
|
+
}
|
|
163
|
+
function newBrowserRecognition() {
|
|
164
|
+
const found = Reflect.get(globalThis, "SpeechRecognition") ?? Reflect.get(globalThis, "webkitSpeechRecognition");
|
|
165
|
+
return isRecognitionConstructor(found) ? new found() : null;
|
|
166
|
+
}
|
|
167
|
+
function primeBrowserMicrophone() {
|
|
168
|
+
const media = typeof navigator === "undefined" ? void 0 : navigator.mediaDevices;
|
|
169
|
+
if (typeof media?.getUserMedia !== "function") return null;
|
|
170
|
+
return media.getUserMedia({ audio: true }).then((stream) => {
|
|
171
|
+
for (const track of stream.getTracks()) track.stop();
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
function watchBrowserMicPermission(onState) {
|
|
175
|
+
const permissions = typeof navigator === "undefined" ? void 0 : navigator.permissions;
|
|
176
|
+
if (typeof permissions?.query !== "function") return;
|
|
177
|
+
try {
|
|
178
|
+
void permissions.query({ name: "microphone" }).then(
|
|
179
|
+
(status) => {
|
|
180
|
+
onState(status.state);
|
|
181
|
+
status.addEventListener("change", () => onState(status.state));
|
|
182
|
+
},
|
|
183
|
+
() => void 0
|
|
184
|
+
);
|
|
185
|
+
} catch {
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function hasCoarsePointer() {
|
|
189
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function") {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
return window.matchMedia("(pointer: coarse)").matches;
|
|
194
|
+
} catch {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
var BROWSER = {
|
|
199
|
+
createRecognition: newBrowserRecognition,
|
|
200
|
+
now: () => typeof performance !== "undefined" ? performance.now() : Date.now(),
|
|
201
|
+
primeMicrophone: primeBrowserMicrophone,
|
|
202
|
+
watchMicPermission: watchBrowserMicPermission,
|
|
203
|
+
touch: hasCoarsePointer
|
|
204
|
+
};
|
|
205
|
+
function primeFailure(error) {
|
|
206
|
+
const name = typeof error === "object" && error !== null && "name" in error ? String(error.name) : "";
|
|
207
|
+
if (name === "NotAllowedError" || name === "SecurityError") return "denied";
|
|
208
|
+
if (name === "NotFoundError" || name === "DevicesNotFoundError") {
|
|
209
|
+
return "unsupported";
|
|
210
|
+
}
|
|
211
|
+
return "failed";
|
|
212
|
+
}
|
|
213
|
+
function tidy(text) {
|
|
214
|
+
return text.replace(/\s+/g, " ").trim();
|
|
215
|
+
}
|
|
216
|
+
function readRecognitionResults(results) {
|
|
217
|
+
const finals = [];
|
|
218
|
+
const interim = [];
|
|
219
|
+
for (let index = 0; index < results.length; index += 1) {
|
|
220
|
+
const result = results[index];
|
|
221
|
+
const text = tidy(result?.[0]?.transcript ?? "");
|
|
222
|
+
if (result === void 0 || text === "") continue;
|
|
223
|
+
if (!result.isFinal) {
|
|
224
|
+
interim.push(text);
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const last = finals.length > 0 ? finals[finals.length - 1] : void 0;
|
|
228
|
+
if (last !== void 0 && startsWithWordsOf(text, last)) {
|
|
229
|
+
finals[finals.length - 1] = text;
|
|
230
|
+
} else {
|
|
231
|
+
finals.push(text);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return { finals, interim: interim.join(" ") };
|
|
235
|
+
}
|
|
236
|
+
function createBrowserAvatarMic(options, seams = {}) {
|
|
237
|
+
const { createRecognition, now, primeMicrophone, watchMicPermission, touch } = { ...BROWSER, ...seams };
|
|
238
|
+
const { endpoint } = options;
|
|
239
|
+
const pauseMs = endpoint === void 0 || !Number.isFinite(endpoint.pauseMs) ? BROWSER_PAUSE_MIN_MS : Math.min(
|
|
240
|
+
BROWSER_PAUSE_MAX_MS,
|
|
241
|
+
Math.max(BROWSER_PAUSE_MIN_MS, endpoint.pauseMs)
|
|
242
|
+
);
|
|
243
|
+
let press = null;
|
|
244
|
+
let epoch = 0;
|
|
245
|
+
let state = IDLE;
|
|
246
|
+
let networkFailures = 0;
|
|
247
|
+
let permission = null;
|
|
248
|
+
let hadMicrophone = false;
|
|
249
|
+
watchMicPermission((next) => {
|
|
250
|
+
permission = next;
|
|
251
|
+
});
|
|
252
|
+
const report = (next) => {
|
|
253
|
+
state = next;
|
|
254
|
+
options.onState(next);
|
|
255
|
+
};
|
|
256
|
+
const turnOf = (earlier, instance) => {
|
|
257
|
+
let turn = earlier;
|
|
258
|
+
for (const final of instance?.finals ?? []) {
|
|
259
|
+
turn = mergeTurnPhrase(turn, final);
|
|
260
|
+
}
|
|
261
|
+
return mergeInterim(turn, instance?.interim ?? "");
|
|
262
|
+
};
|
|
263
|
+
const heard = (p) => turnOf(p.earlier, p.instance);
|
|
264
|
+
const show = (p) => {
|
|
265
|
+
if (p.stage === "listening") {
|
|
266
|
+
const text = heard(p);
|
|
267
|
+
report({
|
|
268
|
+
name: "listening",
|
|
269
|
+
epoch: p.epoch,
|
|
270
|
+
startedAt: p.startedAt,
|
|
271
|
+
text,
|
|
272
|
+
...endpoint === void 0 ? {} : { genuine: isGenuineSpeech(text), pausing: p.pausing }
|
|
273
|
+
});
|
|
274
|
+
} else if (p.stage === "finishing") {
|
|
275
|
+
report({
|
|
276
|
+
name: "finishing",
|
|
277
|
+
epoch: p.epoch,
|
|
278
|
+
text: heard(p),
|
|
279
|
+
limit: p.limit
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
const release = (instance) => {
|
|
284
|
+
const { recognition } = instance;
|
|
285
|
+
recognition.onstart = null;
|
|
286
|
+
recognition.onresult = null;
|
|
287
|
+
recognition.onerror = null;
|
|
288
|
+
recognition.onend = null;
|
|
289
|
+
try {
|
|
290
|
+
recognition.abort();
|
|
291
|
+
} catch {
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
const clearTimers = (p) => {
|
|
295
|
+
for (const timer of [
|
|
296
|
+
p.startTimer,
|
|
297
|
+
p.limitTimer,
|
|
298
|
+
p.finalTimer,
|
|
299
|
+
p.launchTimer,
|
|
300
|
+
p.pauseTimer,
|
|
301
|
+
p.pausingTimer
|
|
302
|
+
]) {
|
|
303
|
+
if (timer !== null) clearTimeout(timer);
|
|
304
|
+
}
|
|
305
|
+
p.startTimer = null;
|
|
306
|
+
p.limitTimer = null;
|
|
307
|
+
p.finalTimer = null;
|
|
308
|
+
p.launchTimer = null;
|
|
309
|
+
p.pauseTimer = null;
|
|
310
|
+
p.pausingTimer = null;
|
|
311
|
+
};
|
|
312
|
+
const clearPause = (p) => {
|
|
313
|
+
if (p.pauseTimer !== null) clearTimeout(p.pauseTimer);
|
|
314
|
+
if (p.pausingTimer !== null) clearTimeout(p.pausingTimer);
|
|
315
|
+
p.pauseTimer = null;
|
|
316
|
+
p.pausingTimer = null;
|
|
317
|
+
};
|
|
318
|
+
const settle = (p, next) => {
|
|
319
|
+
if (p.stage === "ended") return;
|
|
320
|
+
p.stage = "ended";
|
|
321
|
+
clearTimers(p);
|
|
322
|
+
const instance = p.instance;
|
|
323
|
+
p.instance = null;
|
|
324
|
+
if (instance !== null) release(instance);
|
|
325
|
+
if (press === p) press = null;
|
|
326
|
+
report(next);
|
|
327
|
+
};
|
|
328
|
+
const complete = (p) => {
|
|
329
|
+
const text = heard(p);
|
|
330
|
+
if (text !== "") networkFailures = 0;
|
|
331
|
+
if (endpoint === void 0) {
|
|
332
|
+
settle(p, { name: "done", epoch: p.epoch, text, limit: p.limit });
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const by = p.stage === "finishing" ? p.by ?? "tap" : p.stage === "starting" ? "silence" : "failed";
|
|
336
|
+
const echo = by === "pause" && p.speechAt !== null && p.speechAt - p.listenedAt <= BROWSER_ECHO_WINDOW_MS && isEchoOf(text, endpoint.echoOf());
|
|
337
|
+
settle(p, {
|
|
338
|
+
name: "done",
|
|
339
|
+
epoch: p.epoch,
|
|
340
|
+
text,
|
|
341
|
+
limit: p.limit,
|
|
342
|
+
by,
|
|
343
|
+
echo
|
|
344
|
+
});
|
|
345
|
+
};
|
|
346
|
+
const fail = (p, reason) => settle(p, { name: "failed", epoch: p.epoch, reason, text: heard(p) });
|
|
347
|
+
const current = (p, instance) => press === p && p.stage !== "ended" && p.instance === instance;
|
|
348
|
+
const armPause = (p, turn, speech) => {
|
|
349
|
+
clearPause(p);
|
|
350
|
+
if (!speech) return;
|
|
351
|
+
const at = now();
|
|
352
|
+
if (!p.pausing) {
|
|
353
|
+
p.pausingTimer = setTimeout(
|
|
354
|
+
() => {
|
|
355
|
+
p.pausingTimer = null;
|
|
356
|
+
if (press !== p || p.stage !== "listening") return;
|
|
357
|
+
p.pausing = true;
|
|
358
|
+
show(p);
|
|
359
|
+
},
|
|
360
|
+
Math.max(0, p.wordsAt + BROWSER_PAUSE_SHOWN_MS - at)
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
const instance = p.instance;
|
|
364
|
+
if (instance === null || !instance.started) return;
|
|
365
|
+
const wait = waitsLonger(turn) ? 2 * pauseMs : pauseMs;
|
|
366
|
+
const due = Math.max(
|
|
367
|
+
p.wordsAt + wait,
|
|
368
|
+
instance.startedAt + BROWSER_PAUSE_AFTER_START_MS
|
|
369
|
+
);
|
|
370
|
+
p.pauseTimer = setTimeout(
|
|
371
|
+
() => {
|
|
372
|
+
p.pauseTimer = null;
|
|
373
|
+
if (press === p && p.stage === "listening" && p.instance === instance) {
|
|
374
|
+
stopRecording(p, "pause");
|
|
375
|
+
}
|
|
376
|
+
},
|
|
377
|
+
Math.max(0, due - at)
|
|
378
|
+
);
|
|
379
|
+
};
|
|
380
|
+
const listenOn = (p) => {
|
|
381
|
+
if (endpoint === void 0 || p.stage !== "listening") return;
|
|
382
|
+
const turn = heard(p);
|
|
383
|
+
const words = speechWords(turn).join(" ");
|
|
384
|
+
const at = now();
|
|
385
|
+
if (words !== p.words) {
|
|
386
|
+
p.words = words;
|
|
387
|
+
p.wordsAt = at;
|
|
388
|
+
p.pausing = false;
|
|
389
|
+
}
|
|
390
|
+
const speech = isGenuineSpeech(turn);
|
|
391
|
+
if (speech && p.speechAt === null) {
|
|
392
|
+
p.speechAt = at;
|
|
393
|
+
if (p.limitTimer !== null) clearTimeout(p.limitTimer);
|
|
394
|
+
p.limitTimer = setTimeout(() => {
|
|
395
|
+
p.limitTimer = null;
|
|
396
|
+
if (press === p) stopRecording(p, "limit");
|
|
397
|
+
}, options.maxPressMs);
|
|
398
|
+
}
|
|
399
|
+
armPause(p, turn, speech);
|
|
400
|
+
};
|
|
401
|
+
const began = (p, instance) => {
|
|
402
|
+
instance.started = true;
|
|
403
|
+
instance.startedAt = now();
|
|
404
|
+
hadMicrophone = true;
|
|
405
|
+
if (p.stage === "starting") {
|
|
406
|
+
if (p.startTimer !== null) clearTimeout(p.startTimer);
|
|
407
|
+
p.startTimer = null;
|
|
408
|
+
p.stage = "listening";
|
|
409
|
+
p.startedAt = Date.now();
|
|
410
|
+
p.listenedAt = instance.startedAt;
|
|
411
|
+
p.limitTimer = setTimeout(() => {
|
|
412
|
+
p.limitTimer = null;
|
|
413
|
+
if (press !== p) return;
|
|
414
|
+
if (endpoint === void 0) {
|
|
415
|
+
stopRecording(p, "limit");
|
|
416
|
+
} else if (p.stage === "listening" && p.speechAt === null) {
|
|
417
|
+
settle(p, {
|
|
418
|
+
name: "done",
|
|
419
|
+
epoch: p.epoch,
|
|
420
|
+
text: "",
|
|
421
|
+
limit: false,
|
|
422
|
+
by: "silence",
|
|
423
|
+
echo: false
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
}, options.maxPressMs);
|
|
427
|
+
}
|
|
428
|
+
listenOn(p);
|
|
429
|
+
};
|
|
430
|
+
const launchIn = (p, delay) => {
|
|
431
|
+
if (delay <= 0) {
|
|
432
|
+
launch(p);
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
const stage = p.stage;
|
|
436
|
+
p.launchTimer = setTimeout(() => {
|
|
437
|
+
p.launchTimer = null;
|
|
438
|
+
if (press === p && p.stage === stage) launch(p);
|
|
439
|
+
}, delay);
|
|
440
|
+
};
|
|
441
|
+
const restart = (p, end) => {
|
|
442
|
+
p.quickEnds = end === "listened" ? 0 : p.quickEnds + 1;
|
|
443
|
+
if (p.restarts >= BROWSER_MAX_RESTARTS || p.quickEnds >= BROWSER_MAX_QUICK_ENDS) {
|
|
444
|
+
complete(p);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
p.restarts += 1;
|
|
448
|
+
launchIn(
|
|
449
|
+
p,
|
|
450
|
+
end === "threw" || end === "quick" && !p.touch ? BROWSER_RESTART_COOLDOWN_MS : p.touch ? BROWSER_TOUCH_RESTART_MS : 0
|
|
451
|
+
);
|
|
452
|
+
};
|
|
453
|
+
const keep = (p, instance) => {
|
|
454
|
+
p.earlier = turnOf(p.earlier, instance);
|
|
455
|
+
};
|
|
456
|
+
const onError = (p, instance, code) => {
|
|
457
|
+
if (code === "no-speech") {
|
|
458
|
+
instance.noSpeech = true;
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
const going = p.stage === "starting" || p.stage === "listening";
|
|
462
|
+
if (code === "language-not-supported" && going && !p.langRetried) {
|
|
463
|
+
p.langRetried = true;
|
|
464
|
+
if (options.fallbackLang !== p.lang) {
|
|
465
|
+
p.lang = options.fallbackLang;
|
|
466
|
+
keep(p, instance);
|
|
467
|
+
p.instance = null;
|
|
468
|
+
release(instance);
|
|
469
|
+
launch(p);
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
const refused = code === "not-allowed" || code === "service-not-allowed";
|
|
474
|
+
if (refused && going && p.confirmed && !p.grantRetried) {
|
|
475
|
+
p.grantRetried = true;
|
|
476
|
+
keep(p, instance);
|
|
477
|
+
p.instance = null;
|
|
478
|
+
release(instance);
|
|
479
|
+
launchIn(p, BROWSER_GRANT_RETRY_MS);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
if (refused && !p.confirmed) hadMicrophone = false;
|
|
483
|
+
if (heard(p) !== "") {
|
|
484
|
+
complete(p);
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
switch (code) {
|
|
488
|
+
case "not-allowed":
|
|
489
|
+
fail(p, p.confirmed ? "unsupported" : "denied");
|
|
490
|
+
return;
|
|
491
|
+
case "service-not-allowed":
|
|
492
|
+
case "audio-capture":
|
|
493
|
+
case "language-not-supported":
|
|
494
|
+
fail(p, "unsupported");
|
|
495
|
+
return;
|
|
496
|
+
case "network":
|
|
497
|
+
networkFailures += 1;
|
|
498
|
+
fail(
|
|
499
|
+
p,
|
|
500
|
+
networkFailures >= NETWORK_FAILURES_FOR_GOOD ? "unsupported" : "failed"
|
|
501
|
+
);
|
|
502
|
+
return;
|
|
503
|
+
default:
|
|
504
|
+
fail(p, "failed");
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
const onEnd = (p, instance) => {
|
|
508
|
+
keep(p, instance);
|
|
509
|
+
p.instance = null;
|
|
510
|
+
instance.recognition.onstart = null;
|
|
511
|
+
instance.recognition.onresult = null;
|
|
512
|
+
instance.recognition.onerror = null;
|
|
513
|
+
instance.recognition.onend = null;
|
|
514
|
+
switch (p.stage) {
|
|
515
|
+
case "starting":
|
|
516
|
+
if (instance.noSpeech) complete(p);
|
|
517
|
+
else fail(p, "failed");
|
|
518
|
+
return;
|
|
519
|
+
case "listening": {
|
|
520
|
+
if (p.pauseTimer !== null) clearTimeout(p.pauseTimer);
|
|
521
|
+
p.pauseTimer = null;
|
|
522
|
+
const quick = !instance.started || now() - instance.startedAt < QUICK_END_MS && instance.finals.length === 0 && instance.interim === "";
|
|
523
|
+
restart(p, quick ? "quick" : "listened");
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
case "finishing":
|
|
527
|
+
complete(p);
|
|
528
|
+
return;
|
|
529
|
+
default:
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
function launch(p) {
|
|
534
|
+
let recognition;
|
|
535
|
+
try {
|
|
536
|
+
recognition = createRecognition();
|
|
537
|
+
} catch {
|
|
538
|
+
recognition = null;
|
|
539
|
+
}
|
|
540
|
+
if (recognition === null) {
|
|
541
|
+
if (p.stage === "starting") fail(p, "unsupported");
|
|
542
|
+
else complete(p);
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
const instance = {
|
|
546
|
+
recognition,
|
|
547
|
+
started: false,
|
|
548
|
+
startedAt: 0,
|
|
549
|
+
finals: [],
|
|
550
|
+
interim: "",
|
|
551
|
+
noSpeech: false
|
|
552
|
+
};
|
|
553
|
+
recognition.lang = p.lang;
|
|
554
|
+
recognition.continuous = !p.touch;
|
|
555
|
+
recognition.interimResults = true;
|
|
556
|
+
recognition.maxAlternatives = 1;
|
|
557
|
+
recognition.onstart = () => {
|
|
558
|
+
if (!current(p, instance)) return;
|
|
559
|
+
began(p, instance);
|
|
560
|
+
show(p);
|
|
561
|
+
};
|
|
562
|
+
recognition.onresult = (event) => {
|
|
563
|
+
if (!current(p, instance)) return;
|
|
564
|
+
if (!instance.started) began(p, instance);
|
|
565
|
+
const read = readRecognitionResults(event.results);
|
|
566
|
+
instance.finals = read.finals;
|
|
567
|
+
instance.interim = read.interim;
|
|
568
|
+
listenOn(p);
|
|
569
|
+
show(p);
|
|
570
|
+
};
|
|
571
|
+
recognition.onerror = (event) => {
|
|
572
|
+
if (current(p, instance)) onError(p, instance, event.error);
|
|
573
|
+
};
|
|
574
|
+
recognition.onend = () => {
|
|
575
|
+
if (current(p, instance)) onEnd(p, instance);
|
|
576
|
+
};
|
|
577
|
+
p.instance = instance;
|
|
578
|
+
try {
|
|
579
|
+
recognition.start();
|
|
580
|
+
} catch {
|
|
581
|
+
p.instance = null;
|
|
582
|
+
release(instance);
|
|
583
|
+
if (p.stage === "starting") fail(p, "failed");
|
|
584
|
+
else if (p.stage === "listening") restart(p, "threw");
|
|
585
|
+
else complete(p);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
function prime(p) {
|
|
589
|
+
let opening;
|
|
590
|
+
try {
|
|
591
|
+
opening = primeMicrophone();
|
|
592
|
+
} catch (error) {
|
|
593
|
+
fail(p, primeFailure(error));
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
if (opening === null) {
|
|
597
|
+
launch(p);
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
void opening.then(
|
|
601
|
+
() => {
|
|
602
|
+
hadMicrophone = true;
|
|
603
|
+
if (press !== p || p.stage !== "starting") return;
|
|
604
|
+
p.confirmed = true;
|
|
605
|
+
launchIn(p, BROWSER_PRIME_SETTLE_MS);
|
|
606
|
+
},
|
|
607
|
+
(error) => {
|
|
608
|
+
if (press === p && p.stage === "starting") fail(p, primeFailure(error));
|
|
609
|
+
}
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
function stopRecording(p, by) {
|
|
613
|
+
if (p.stage !== "listening") return;
|
|
614
|
+
if (p.limitTimer !== null) clearTimeout(p.limitTimer);
|
|
615
|
+
if (p.launchTimer !== null) clearTimeout(p.launchTimer);
|
|
616
|
+
p.limitTimer = null;
|
|
617
|
+
p.launchTimer = null;
|
|
618
|
+
clearPause(p);
|
|
619
|
+
p.stage = "finishing";
|
|
620
|
+
p.limit = by === "limit";
|
|
621
|
+
p.by = by;
|
|
622
|
+
const instance = p.instance;
|
|
623
|
+
if (instance === null || !instance.started) {
|
|
624
|
+
complete(p);
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
try {
|
|
628
|
+
instance.recognition.stop();
|
|
629
|
+
} catch {
|
|
630
|
+
complete(p);
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
p.finalTimer = setTimeout(() => {
|
|
634
|
+
p.finalTimer = null;
|
|
635
|
+
if (press === p && p.stage === "finishing") complete(p);
|
|
636
|
+
}, BROWSER_FINAL_WAIT_MS);
|
|
637
|
+
show(p);
|
|
638
|
+
}
|
|
639
|
+
const cancel = () => {
|
|
640
|
+
const p = press;
|
|
641
|
+
press = null;
|
|
642
|
+
if (p !== null && p.stage !== "ended") {
|
|
643
|
+
p.stage = "ended";
|
|
644
|
+
clearTimers(p);
|
|
645
|
+
const instance = p.instance;
|
|
646
|
+
p.instance = null;
|
|
647
|
+
if (instance !== null) release(instance);
|
|
648
|
+
}
|
|
649
|
+
if (state.name !== "idle") report(IDLE);
|
|
650
|
+
};
|
|
651
|
+
const start = () => {
|
|
652
|
+
const held = press;
|
|
653
|
+
if (held !== null && (held.stage === "starting" || held.stage === "listening")) {
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
if (held !== null && held.stage === "finishing") {
|
|
657
|
+
held.stage = "ended";
|
|
658
|
+
clearTimers(held);
|
|
659
|
+
const instance = held.instance;
|
|
660
|
+
held.instance = null;
|
|
661
|
+
if (instance !== null) release(instance);
|
|
662
|
+
press = null;
|
|
663
|
+
}
|
|
664
|
+
epoch += 1;
|
|
665
|
+
const p = {
|
|
666
|
+
epoch,
|
|
667
|
+
stage: "starting",
|
|
668
|
+
instance: null,
|
|
669
|
+
earlier: "",
|
|
670
|
+
lang: options.lang,
|
|
671
|
+
langRetried: false,
|
|
672
|
+
restarts: 0,
|
|
673
|
+
quickEnds: 0,
|
|
674
|
+
confirmed: permission === "granted",
|
|
675
|
+
grantRetried: false,
|
|
676
|
+
touch: touch(),
|
|
677
|
+
startedAt: 0,
|
|
678
|
+
limit: false,
|
|
679
|
+
startTimer: null,
|
|
680
|
+
limitTimer: null,
|
|
681
|
+
finalTimer: null,
|
|
682
|
+
launchTimer: null,
|
|
683
|
+
by: null,
|
|
684
|
+
words: "",
|
|
685
|
+
wordsAt: 0,
|
|
686
|
+
speechAt: null,
|
|
687
|
+
listenedAt: 0,
|
|
688
|
+
pausing: false,
|
|
689
|
+
pauseTimer: null,
|
|
690
|
+
pausingTimer: null
|
|
691
|
+
};
|
|
692
|
+
press = p;
|
|
693
|
+
report({ name: "starting", epoch });
|
|
694
|
+
p.startTimer = setTimeout(() => {
|
|
695
|
+
p.startTimer = null;
|
|
696
|
+
if (press === p && p.stage === "starting") fail(p, "failed");
|
|
697
|
+
}, BROWSER_START_WAIT_MS);
|
|
698
|
+
if (p.confirmed || permission === null && hadMicrophone) launch(p);
|
|
699
|
+
else prime(p);
|
|
700
|
+
};
|
|
701
|
+
return {
|
|
702
|
+
start,
|
|
703
|
+
stop() {
|
|
704
|
+
const p = press;
|
|
705
|
+
if (p === null) return;
|
|
706
|
+
if (p.stage === "starting") cancel();
|
|
707
|
+
else stopRecording(p, "tap");
|
|
708
|
+
},
|
|
709
|
+
cancel,
|
|
710
|
+
state: () => state
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
|
|
3
714
|
// src/avatar-mic/capture-worklet.ts
|
|
4
715
|
var CAPTURE_FRAME_SAMPLES = 320;
|
|
5
716
|
var CAPTURE_SAMPLE_RATE = 16e3;
|
|
@@ -322,7 +1033,7 @@ var FINAL_WAIT_MS = 12e3;
|
|
|
322
1033
|
var OFFER_TTL_MS = 12e4;
|
|
323
1034
|
var OFFER_MARGIN_MS = 5e3;
|
|
324
1035
|
var CLOCK_TOLERANCE_MS = 5e3;
|
|
325
|
-
var
|
|
1036
|
+
var IDLE2 = { name: "idle" };
|
|
326
1037
|
function offerFreshUntil(offer) {
|
|
327
1038
|
const ahead = offer.expiresAt - offer.receivedAt;
|
|
328
1039
|
const life = ahead > 0 && ahead <= OFFER_TTL_MS + CLOCK_TOLERANCE_MS ? Math.min(ahead, OFFER_TTL_MS) : OFFER_TTL_MS;
|
|
@@ -355,20 +1066,20 @@ function randomUtteranceId() {
|
|
|
355
1066
|
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
356
1067
|
return `web-${Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
357
1068
|
}
|
|
358
|
-
var
|
|
1069
|
+
var BROWSER2 = {
|
|
359
1070
|
createSocket: openBrowserSocket,
|
|
360
1071
|
startCapture: startMicCapture,
|
|
361
1072
|
newUtteranceId: randomUtteranceId
|
|
362
1073
|
};
|
|
363
1074
|
function createAvatarMic(options, seams = {}) {
|
|
364
1075
|
const { createSocket, startCapture, newUtteranceId } = {
|
|
365
|
-
...
|
|
1076
|
+
...BROWSER2,
|
|
366
1077
|
...seams
|
|
367
1078
|
};
|
|
368
1079
|
let press = null;
|
|
369
1080
|
let connection = null;
|
|
370
1081
|
let epoch = 0;
|
|
371
|
-
let state =
|
|
1082
|
+
let state = IDLE2;
|
|
372
1083
|
const report = (next) => {
|
|
373
1084
|
state = next;
|
|
374
1085
|
options.onState(next);
|
|
@@ -650,7 +1361,7 @@ function createAvatarMic(options, seams = {}) {
|
|
|
650
1361
|
clearTimers(p);
|
|
651
1362
|
}
|
|
652
1363
|
closeConnection();
|
|
653
|
-
if (state.name !== "idle") report(
|
|
1364
|
+
if (state.name !== "idle") report(IDLE2);
|
|
654
1365
|
};
|
|
655
1366
|
const start = () => {
|
|
656
1367
|
const current = press;
|
|
@@ -731,6 +1442,6 @@ function createAvatarMic(options, seams = {}) {
|
|
|
731
1442
|
};
|
|
732
1443
|
}
|
|
733
1444
|
|
|
734
|
-
export { createAvatarMic };
|
|
1445
|
+
export { createAvatarMic, createBrowserAvatarMic };
|
|
735
1446
|
//# sourceMappingURL=index.mjs.map
|
|
736
1447
|
//# sourceMappingURL=index.mjs.map
|