@lubomirklimes/character-runtime 0.0.0-stage → 0.1.0-preview.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.
Files changed (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +89 -2
  3. package/dist/acs/acs15.d.ts +9 -0
  4. package/dist/acs/acs15.js +382 -0
  5. package/dist/acs/acs2-reader.d.ts +2 -0
  6. package/dist/acs/acs2-reader.js +277 -0
  7. package/dist/acs/binary.d.ts +1 -0
  8. package/dist/acs/binary.js +107 -0
  9. package/dist/acs/compound-file.d.ts +1 -0
  10. package/dist/acs/compound-file.js +326 -0
  11. package/dist/acs/document.d.ts +33 -0
  12. package/dist/acs/document.js +99 -0
  13. package/dist/acs/errors.d.ts +28 -0
  14. package/dist/acs/errors.js +41 -0
  15. package/dist/acs/image.d.ts +30 -0
  16. package/dist/acs/image.js +113 -0
  17. package/dist/acs/index.d.ts +11 -0
  18. package/dist/acs/index.js +10 -0
  19. package/dist/acs/lz.d.ts +7 -0
  20. package/dist/acs/lz.js +102 -0
  21. package/dist/acs/metadata.d.ts +14 -0
  22. package/dist/acs/metadata.js +126 -0
  23. package/dist/acs/model.d.ts +130 -0
  24. package/dist/acs/model.js +12 -0
  25. package/dist/acs/mouth.d.ts +37 -0
  26. package/dist/acs/mouth.js +43 -0
  27. package/dist/acs/wave.d.ts +53 -0
  28. package/dist/acs/wave.js +422 -0
  29. package/dist/browser/animation-frame-clock.d.ts +18 -0
  30. package/dist/browser/animation-frame-clock.js +42 -0
  31. package/dist/browser/canvas-renderer.d.ts +50 -0
  32. package/dist/browser/canvas-renderer.js +145 -0
  33. package/dist/browser/host.d.ts +84 -0
  34. package/dist/browser/host.js +206 -0
  35. package/dist/browser/index.d.ts +6 -0
  36. package/dist/browser/index.js +8 -0
  37. package/dist/browser/web-audio.d.ts +48 -0
  38. package/dist/browser/web-audio.js +205 -0
  39. package/dist/browser/web-speech.d.ts +131 -0
  40. package/dist/browser/web-speech.js +302 -0
  41. package/dist/core/activities.d.ts +10 -0
  42. package/dist/core/activities.js +479 -0
  43. package/dist/core/agent.d.ts +263 -0
  44. package/dist/core/agent.js +764 -0
  45. package/dist/core/audio.d.ts +17 -0
  46. package/dist/core/audio.js +174 -0
  47. package/dist/core/balloon.d.ts +43 -0
  48. package/dist/core/balloon.js +94 -0
  49. package/dist/core/clock.d.ts +32 -0
  50. package/dist/core/clock.js +51 -0
  51. package/dist/core/compositor.d.ts +22 -0
  52. package/dist/core/compositor.js +67 -0
  53. package/dist/core/events.d.ts +131 -0
  54. package/dist/core/events.js +1 -0
  55. package/dist/core/host.d.ts +29 -0
  56. package/dist/core/host.js +77 -0
  57. package/dist/core/index.d.ts +16 -0
  58. package/dist/core/index.js +13 -0
  59. package/dist/core/lipsync.d.ts +41 -0
  60. package/dist/core/lipsync.js +209 -0
  61. package/dist/core/random.d.ts +52 -0
  62. package/dist/core/random.js +105 -0
  63. package/dist/core/request.d.ts +68 -0
  64. package/dist/core/request.js +131 -0
  65. package/dist/core/resources.d.ts +70 -0
  66. package/dist/core/resources.js +243 -0
  67. package/dist/core/scheduler.d.ts +25 -0
  68. package/dist/core/scheduler.js +766 -0
  69. package/dist/core/source.d.ts +21 -0
  70. package/dist/core/source.js +54 -0
  71. package/dist/core/speech.d.ts +91 -0
  72. package/dist/core/speech.js +86 -0
  73. package/dist/core/text.d.ts +38 -0
  74. package/dist/core/text.js +97 -0
  75. package/dist/index.d.ts +2 -0
  76. package/dist/index.js +4 -0
  77. package/package.json +68 -4
@@ -0,0 +1,131 @@
1
+ import type { AgentEvent } from '../core/events.js';
2
+ import type { SpeechAudio, SpeechProvider, SpeechSynthesisRequest, SpeechVoice } from '../core/speech.js';
3
+ import { type PacingOptions } from '../core/text.js';
4
+ /** How a {@link WebSpeechProvider} times speech for the runtime. */
5
+ export type WebSpeechTiming = 'paced' | 'measured';
6
+ /** Options of a {@link WebSpeechProvider}. */
7
+ export interface WebSpeechOptions {
8
+ /** The synthesis interface (default `globalThis.speechSynthesis`). */
9
+ readonly synthesis?: SpeechSynthesis | null;
10
+ /** Reading-time pacing (default: the runtime defaults). */
11
+ readonly pacing?: PacingOptions;
12
+ /**
13
+ * `paced` always uses the runtime's reading-time pacing; `measured` (default) uses the speaking rate measured from earlier utterances
14
+ * of the same voice once one has been measured.
15
+ */
16
+ readonly timing?: WebSpeechTiming;
17
+ /** Utterance pitch 0–2 (default 1). */
18
+ readonly pitch?: number;
19
+ /** Utterance volume 0–1 (default 1). */
20
+ readonly volume?: number;
21
+ }
22
+ /** Counters of a {@link WebSpeechProvider}. */
23
+ export interface WebSpeechStatistics {
24
+ readonly voices: number;
25
+ /** Speeches prepared by `synthesize` (timing returned to the runtime). */
26
+ readonly prepared: number;
27
+ /**
28
+ * Utterances submitted to `speechSynthesis.speak()`. Submission does not mean speech was produced: the browser may report an error
29
+ * (for example without a usable voice or audio output), or never start it. An empty voice list does not prevent submission, because
30
+ * browsers may still speak with a platform default voice.
31
+ */
32
+ readonly submitted: number;
33
+ /** Utterances the browser reported as started (its `start` event). */
34
+ readonly started: number;
35
+ /** Utterances the browser reported as finished (its `end` event). Neither event proves audible output. */
36
+ readonly ended: number;
37
+ /** Speeches cancelled by the runtime (stop, interrupt, pause, removal) or by other code calling `cancel()`. */
38
+ readonly cancelled: number;
39
+ /** Speeches whose runtime speech was over before the browser was free to speak them. */
40
+ readonly skipped: number;
41
+ /** Utterances the browser reported as failed (its `error` event other than a cancellation). */
42
+ readonly errors: number;
43
+ readonly boundaries: number;
44
+ /** Speeches waiting for the browser. */
45
+ readonly queued: number;
46
+ readonly status: string;
47
+ }
48
+ /** What happened to the browser speech of one runtime speech (reported to its owner). */
49
+ export interface WebSpeechOutcome {
50
+ readonly agentId: number;
51
+ readonly requestId: number;
52
+ readonly outcome: 'started' | 'ended' | 'cancelled' | 'skipped' | 'error';
53
+ /** The browser's error code for `error`. */
54
+ readonly error?: string;
55
+ }
56
+ /**
57
+ * The browser's speech synthesis (Web Speech API) as a speech provider (Runtime.md §16.4), shared by every character of a page.
58
+ *
59
+ * The browser speaks directly and cannot render audio data, so synthesis returns speech without audio, timed by the reading-time pacing
60
+ * or by a rate measured from earlier utterances of the voice, and with no visemes or phonemes: lip-sync is the runtime's labelled
61
+ * approximation. Word boundaries arrive only while the browser speaks, after the runtime fixed the speech timeline; they are counted and
62
+ * used for measurement only. The runtime's request lifecycle never depends on the browser.
63
+ *
64
+ * Ownership: pass every advance's events to {@link process} (the browser host does). Each speech that starts in the runtime becomes an
65
+ * entry owned by its agent and request. The page has one synthesis queue and `speechSynthesis.cancel()` empties all of it, so the provider
66
+ * hands the browser one utterance at a time and keeps the others in its own queue: browser speech of several characters is serialized,
67
+ * never simultaneous. Cancelling a speech removes only its own entry; `cancel()` is called only while the browser speaks the entry being
68
+ * cancelled (it then also cancels utterances that other code on the page queued directly). A speech that is over in the runtime before
69
+ * the browser is free is skipped instead of being spoken late. Callbacks of an utterance that is no longer current are ignored.
70
+ */
71
+ export declare class WebSpeechProvider implements SpeechProvider {
72
+ readonly name = "Web Speech API";
73
+ private readonly synthesis;
74
+ private readonly pacing;
75
+ private readonly timing;
76
+ private pitchValue;
77
+ private volumeValue;
78
+ private readonly prepared;
79
+ private readonly measured;
80
+ private readonly voiceListeners;
81
+ private readonly outcomeListeners;
82
+ private readonly queue;
83
+ private active;
84
+ private voices;
85
+ private native;
86
+ private next;
87
+ private pumpScheduled;
88
+ private counters;
89
+ private statusText;
90
+ private disposed;
91
+ private readonly onVoices;
92
+ constructor(options?: WebSpeechOptions);
93
+ /** Utterance pitch 0–2 for speech the browser starts from now on. */
94
+ get pitch(): number;
95
+ set pitch(value: number);
96
+ /** Utterance volume 0–1 for speech the browser starts from now on. */
97
+ get volume(): number;
98
+ set volume(value: number);
99
+ /** True when the browser has speech synthesis and the provider is not disposed. */
100
+ get available(): boolean;
101
+ get statistics(): WebSpeechStatistics;
102
+ /** Calls `listener` when the browser's voice list changes (browsers load voices asynchronously). Returns the unsubscribe function. */
103
+ onVoicesChanged(listener: () => void): () => void;
104
+ /** Calls `listener` with what happens to the browser speech of each runtime speech. Returns the unsubscribe function. */
105
+ onUtterance(listener: (outcome: WebSpeechOutcome) => void): () => void;
106
+ /** Reads the voice list again (the default voice first). */
107
+ refreshVoices(): readonly SpeechVoice[];
108
+ getVoices(): readonly SpeechVoice[];
109
+ /** Returns the speech's timing at once (no audio). The browser speaks later, when the runtime starts the speech. */
110
+ synthesize(request: SpeechSynthesisRequest): SpeechAudio;
111
+ private time;
112
+ /** Starts, queues and cancels browser speech from the runtime's events (call with every advance's events, in order). */
113
+ process(events: readonly AgentEvent[]): void;
114
+ /** Cancels the browser speech (active or queued) of one agent, for example when it is removed. Other agents' speech continues. */
115
+ cancelAgent(agentId: number): void;
116
+ private enqueue;
117
+ private endSpeech;
118
+ private cancelWhere;
119
+ /** Hands the next queued speech to the browser when it is free. */
120
+ private pump;
121
+ private complete;
122
+ private finish;
123
+ private report;
124
+ /** Releases an utterance's handlers, so late callbacks reach nothing and the entry can be collected. */
125
+ private detach;
126
+ /** After `cancel()`, browsers deliver the cancelled utterance's events asynchronously; the next one is spoken after them. */
127
+ private schedulePump;
128
+ private measure;
129
+ /** Cancels this provider's browser speech, drops queued speech and removes the provider's listeners. */
130
+ dispose(): void;
131
+ }
@@ -0,0 +1,302 @@
1
+ import { InvalidArgumentError } from '../acs/errors.js';
2
+ import { pacedSpeech, textLength, textWords } from '../core/text.js';
3
+ const DEFAULT_PACING = { characterMs: 45, wordMs: 120, minimumMs: 1200 };
4
+ const MAX_PREPARED = 64;
5
+ const MAX_QUEUED = 32;
6
+ const MEASUREMENTS = 5;
7
+ /**
8
+ * The browser's speech synthesis (Web Speech API) as a speech provider (Runtime.md §16.4), shared by every character of a page.
9
+ *
10
+ * The browser speaks directly and cannot render audio data, so synthesis returns speech without audio, timed by the reading-time pacing
11
+ * or by a rate measured from earlier utterances of the voice, and with no visemes or phonemes: lip-sync is the runtime's labelled
12
+ * approximation. Word boundaries arrive only while the browser speaks, after the runtime fixed the speech timeline; they are counted and
13
+ * used for measurement only. The runtime's request lifecycle never depends on the browser.
14
+ *
15
+ * Ownership: pass every advance's events to {@link process} (the browser host does). Each speech that starts in the runtime becomes an
16
+ * entry owned by its agent and request. The page has one synthesis queue and `speechSynthesis.cancel()` empties all of it, so the provider
17
+ * hands the browser one utterance at a time and keeps the others in its own queue: browser speech of several characters is serialized,
18
+ * never simultaneous. Cancelling a speech removes only its own entry; `cancel()` is called only while the browser speaks the entry being
19
+ * cancelled (it then also cancels utterances that other code on the page queued directly). A speech that is over in the runtime before
20
+ * the browser is free is skipped instead of being spoken late. Callbacks of an utterance that is no longer current are ignored.
21
+ */
22
+ export class WebSpeechProvider {
23
+ name = 'Web Speech API';
24
+ synthesis;
25
+ pacing;
26
+ timing;
27
+ pitchValue = 1;
28
+ volumeValue = 1;
29
+ prepared = new Map();
30
+ measured = new Map();
31
+ voiceListeners = new Set();
32
+ outcomeListeners = new Set();
33
+ queue = [];
34
+ active = null;
35
+ voices = [];
36
+ native = new Map();
37
+ next = 0;
38
+ pumpScheduled = false;
39
+ counters = { prepared: 0, submitted: 0, started: 0, ended: 0, cancelled: 0, skipped: 0, errors: 0, boundaries: 0 };
40
+ statusText;
41
+ disposed = false;
42
+ onVoices = () => {
43
+ this.refreshVoices();
44
+ for (const listener of [...this.voiceListeners])
45
+ listener();
46
+ };
47
+ constructor(options = {}) {
48
+ this.synthesis = options.synthesis !== undefined ? options.synthesis : typeof speechSynthesis !== 'undefined' ? speechSynthesis : null;
49
+ this.pacing = options.pacing ?? DEFAULT_PACING;
50
+ this.timing = options.timing ?? 'measured';
51
+ this.pitchValue = options.pitch ?? 1;
52
+ this.volumeValue = options.volume ?? 1;
53
+ this.statusText = this.synthesis ? 'Browser speech ready.' : 'This browser has no speech synthesis; speech is paced without audio.';
54
+ this.synthesis?.addEventListener('voiceschanged', this.onVoices);
55
+ this.refreshVoices();
56
+ }
57
+ /** Utterance pitch 0–2 for speech the browser starts from now on. */
58
+ get pitch() {
59
+ return this.pitchValue;
60
+ }
61
+ set pitch(value) {
62
+ this.pitchValue = inRange(value, 0, 2, 'pitch');
63
+ }
64
+ /** Utterance volume 0–1 for speech the browser starts from now on. */
65
+ get volume() {
66
+ return this.volumeValue;
67
+ }
68
+ set volume(value) {
69
+ this.volumeValue = inRange(value, 0, 1, 'volume');
70
+ }
71
+ /** True when the browser has speech synthesis and the provider is not disposed. */
72
+ get available() {
73
+ return this.synthesis !== null && !this.disposed;
74
+ }
75
+ get statistics() {
76
+ return { voices: this.voices.length, ...this.counters, queued: this.queue.length, status: this.statusText };
77
+ }
78
+ /** Calls `listener` when the browser's voice list changes (browsers load voices asynchronously). Returns the unsubscribe function. */
79
+ onVoicesChanged(listener) {
80
+ this.voiceListeners.add(listener);
81
+ return () => this.voiceListeners.delete(listener);
82
+ }
83
+ /** Calls `listener` with what happens to the browser speech of each runtime speech. Returns the unsubscribe function. */
84
+ onUtterance(listener) {
85
+ this.outcomeListeners.add(listener);
86
+ return () => this.outcomeListeners.delete(listener);
87
+ }
88
+ /** Reads the voice list again (the default voice first). */
89
+ refreshVoices() {
90
+ const list = this.synthesis?.getVoices() ?? [];
91
+ const ordered = [...list.filter((v) => v.default), ...list.filter((v) => !v.default)];
92
+ this.native = new Map(ordered.map((v) => [v.voiceURI, v]));
93
+ this.voices = ordered.map((v) => ({ id: v.voiceURI, name: v.name, language: v.lang, gender: 'unknown', provider: this.name }));
94
+ return this.voices;
95
+ }
96
+ getVoices() {
97
+ return this.voices;
98
+ }
99
+ /** Returns the speech's timing at once (no audio). The browser speaks later, when the runtime starts the speech. */
100
+ synthesize(request) {
101
+ if (!this.available)
102
+ throw new Error('This browser has no speech synthesis.');
103
+ const id = ++this.next;
104
+ const elements = Math.max(1, textLength(request.text));
105
+ const { durationMs, words, how } = this.time(request, elements);
106
+ const note = `Browser speech #${id}: the browser speaks directly (no audio data); ${how}.`;
107
+ this.prepared.set(note, { request, elements });
108
+ this.counters.prepared++;
109
+ for (const key of this.prepared.keys()) {
110
+ if (this.prepared.size <= MAX_PREPARED)
111
+ break;
112
+ this.prepared.delete(key);
113
+ }
114
+ return { audio: null, durationMs, words, visemes: [], phonemes: [], voice: request.voice, note };
115
+ }
116
+ time(request, elements) {
117
+ const samples = this.measured.get(request.voice?.id ?? '');
118
+ if (this.timing === 'measured' && samples && samples.length > 0) {
119
+ const perElement = (samples.reduce((a, b) => a + b, 0) / samples.length) * (100 / clampRate(request.ratePercent));
120
+ const words = textWords(request.text).map((w) => ({ offsetMs: textLength(request.text.substring(0, w.start)) * perElement, textStart: w.start, textLength: w.length }));
121
+ return { durationMs: Math.max(perElement, elements * perElement), words, how: `timing measured from ${samples.length} earlier utterance(s) of this voice` };
122
+ }
123
+ const paced = pacedSpeech(request.text, this.pacing);
124
+ return { durationMs: paced.durationMs, words: paced.words, how: 'timing paced by the reading-time rule' };
125
+ }
126
+ /** Starts, queues and cancels browser speech from the runtime's events (call with every advance's events, in order). */
127
+ process(events) {
128
+ for (const e of events) {
129
+ if (e.type === 'speech-started')
130
+ this.enqueue(e.agentId, e.requestId, e.note);
131
+ else if (e.type === 'speech-ended')
132
+ this.endSpeech(e.agentId, e.requestId, e.completed);
133
+ else if (e.type === 'paused')
134
+ this.cancelAgent(e.agentId); // the browser cannot resume where the runtime resumes
135
+ }
136
+ }
137
+ /** Cancels the browser speech (active or queued) of one agent, for example when it is removed. Other agents' speech continues. */
138
+ cancelAgent(agentId) {
139
+ this.cancelWhere((e) => e.agentId === agentId);
140
+ }
141
+ enqueue(agentId, requestId, note) {
142
+ const prepared = this.prepared.get(note);
143
+ if (!prepared || !this.available)
144
+ return;
145
+ this.prepared.delete(note);
146
+ this.queue.push({ agentId, requestId, prepared, utterance: null, startedAt: null });
147
+ while (this.queue.length > MAX_QUEUED)
148
+ this.finish(this.queue.shift(), 'skipped');
149
+ this.pump();
150
+ }
151
+ endSpeech(agentId, requestId, completed) {
152
+ const owned = (e) => e.agentId === agentId && e.requestId === requestId;
153
+ if (!completed) {
154
+ this.cancelWhere(owned);
155
+ return;
156
+ }
157
+ // The runtime speech is over: a speech the browser has not started yet is not spoken late; a speaking one finishes.
158
+ const index = this.queue.findIndex(owned);
159
+ if (index >= 0)
160
+ this.finish(this.queue.splice(index, 1)[0], 'skipped');
161
+ }
162
+ cancelWhere(match) {
163
+ for (let i = this.queue.length - 1; i >= 0; i--)
164
+ if (match(this.queue[i]))
165
+ this.finish(this.queue.splice(i, 1)[0], 'cancelled');
166
+ const active = this.active;
167
+ if (active && match(active)) {
168
+ this.active = null;
169
+ this.detach(active);
170
+ // The browser speaks only this provider's current utterance, so the global cancel stops exactly the owned speech.
171
+ this.synthesis?.cancel();
172
+ this.finish(active, 'cancelled');
173
+ this.schedulePump();
174
+ }
175
+ }
176
+ /** Hands the next queued speech to the browser when it is free. */
177
+ pump() {
178
+ if (this.active || this.disposed || !this.synthesis)
179
+ return;
180
+ const entry = this.queue.shift();
181
+ if (!entry)
182
+ return;
183
+ const { request, elements } = entry.prepared;
184
+ const utterance = new SpeechSynthesisUtterance(request.text);
185
+ const voice = request.voice ? this.native.get(request.voice.id) : undefined;
186
+ if (voice) {
187
+ utterance.voice = voice;
188
+ utterance.lang = voice.lang;
189
+ }
190
+ utterance.rate = Math.min(10, Math.max(0.1, request.ratePercent / 100));
191
+ utterance.pitch = this.pitch;
192
+ utterance.volume = this.volume;
193
+ utterance.onstart = () => {
194
+ if (this.active !== entry)
195
+ return;
196
+ entry.startedAt = performance.now();
197
+ this.counters.started++;
198
+ this.report(entry, 'started');
199
+ };
200
+ utterance.onboundary = () => {
201
+ if (this.active === entry)
202
+ this.counters.boundaries++;
203
+ };
204
+ utterance.onend = () => {
205
+ if (this.active !== entry)
206
+ return;
207
+ if (entry.startedAt !== null)
208
+ this.measure(request, elements, performance.now() - entry.startedAt);
209
+ this.complete(entry, 'ended');
210
+ };
211
+ utterance.onerror = (event) => {
212
+ if (this.active !== entry)
213
+ return;
214
+ // An `interrupted` or `canceled` error of the current utterance comes from outside this provider (another script cancelled).
215
+ if (event.error === 'interrupted' || event.error === 'canceled')
216
+ this.complete(entry, 'cancelled');
217
+ else {
218
+ this.statusText = `Browser speech error: ${event.error}.`;
219
+ this.complete(entry, 'error', event.error);
220
+ }
221
+ };
222
+ entry.utterance = utterance;
223
+ this.active = entry;
224
+ this.counters.submitted++;
225
+ this.synthesis.speak(utterance);
226
+ }
227
+ complete(entry, outcome, error) {
228
+ this.active = null;
229
+ this.detach(entry);
230
+ this.finish(entry, outcome, error);
231
+ this.pump();
232
+ }
233
+ finish(entry, outcome, error) {
234
+ if (outcome === 'ended')
235
+ this.counters.ended++;
236
+ else if (outcome === 'cancelled')
237
+ this.counters.cancelled++;
238
+ else if (outcome === 'skipped')
239
+ this.counters.skipped++;
240
+ else
241
+ this.counters.errors++;
242
+ this.report(entry, outcome, error);
243
+ }
244
+ report(entry, outcome, error) {
245
+ const value = error === undefined ? { agentId: entry.agentId, requestId: entry.requestId, outcome }
246
+ : { agentId: entry.agentId, requestId: entry.requestId, outcome, error };
247
+ for (const listener of [...this.outcomeListeners])
248
+ listener(value);
249
+ }
250
+ /** Releases an utterance's handlers, so late callbacks reach nothing and the entry can be collected. */
251
+ detach(entry) {
252
+ const u = entry.utterance;
253
+ if (u)
254
+ u.onstart = u.onboundary = u.onend = u.onerror = null;
255
+ entry.utterance = null;
256
+ }
257
+ /** After `cancel()`, browsers deliver the cancelled utterance's events asynchronously; the next one is spoken after them. */
258
+ schedulePump() {
259
+ if (this.pumpScheduled)
260
+ return;
261
+ this.pumpScheduled = true;
262
+ setTimeout(() => {
263
+ this.pumpScheduled = false;
264
+ this.pump();
265
+ }, 0);
266
+ }
267
+ measure(request, elements, ms) {
268
+ const key = request.voice?.id ?? '';
269
+ const list = this.measured.get(key) ?? [];
270
+ list.push((ms / elements) * (clampRate(request.ratePercent) / 100));
271
+ if (list.length > MEASUREMENTS)
272
+ list.shift();
273
+ this.measured.set(key, list);
274
+ }
275
+ /** Cancels this provider's browser speech, drops queued speech and removes the provider's listeners. */
276
+ dispose() {
277
+ if (this.disposed)
278
+ return;
279
+ for (const entry of this.queue.splice(0))
280
+ this.finish(entry, 'cancelled');
281
+ const active = this.active;
282
+ this.active = null;
283
+ if (active) {
284
+ this.detach(active);
285
+ this.synthesis?.cancel();
286
+ this.finish(active, 'cancelled');
287
+ }
288
+ this.disposed = true;
289
+ this.synthesis?.removeEventListener('voiceschanged', this.onVoices);
290
+ this.voiceListeners.clear();
291
+ this.outcomeListeners.clear();
292
+ this.prepared.clear();
293
+ }
294
+ }
295
+ function inRange(value, min, max, what) {
296
+ if (!Number.isFinite(value) || value < min || value > max)
297
+ throw new InvalidArgumentError(`The ${what} must be between ${min} and ${max}.`);
298
+ return value;
299
+ }
300
+ function clampRate(ratePercent) {
301
+ return Math.min(1000, Math.max(10, ratePercent));
302
+ }
@@ -0,0 +1,10 @@
1
+ /** Idle scheduling settings (Runtime.md §12). */
2
+ export interface IdleOptions {
3
+ readonly enabled?: boolean;
4
+ /** Quiet time after the last request before the first idle animation (default 4,000 ms). */
5
+ readonly firstDelayMs?: number;
6
+ /** Quiet time between the end of one idle animation and the next (default 4,000 ms). */
7
+ readonly intervalMs?: number;
8
+ /** Idle animations at levels 1 and 2 before the next level (default 10). */
9
+ readonly playsBeforeNextLevel?: number;
10
+ }