@charivo/tts 0.6.5 → 0.6.7

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 CHANGED
@@ -63,6 +63,10 @@ When connected, the manager emits:
63
63
 
64
64
  ## Audio Lifecycle
65
65
 
66
+ - `stop()` — stops active playback. If it interrupts an in-flight `speak()`
67
+ call, that call's promise settles as part of the stop instead of being left
68
+ pending: a deliberate stop is treated as a cancellation, not a failure, so
69
+ it resolves rather than rejects.
66
70
  - `prepareAudio?.()` — creates the lip-sync `AudioContext` up front. Call it
67
71
  from a user-gesture handler before the first `speak()` so mobile browsers
68
72
  that require audio to start from a gesture do not block playback.
package/dist/index.js CHANGED
@@ -116,6 +116,27 @@ var TTSManagerImpl = class {
116
116
  playbackMode;
117
117
  isAudioSessionActive = false;
118
118
  teardownBrowserLifecycle;
119
+ // Settles the stateless-audio Promise that handleStatelessAudio() is
120
+ // currently awaiting, if any. stop() clears currentAudio's onended/onerror
121
+ // handlers so they can never fire on their own; without this, stopping a
122
+ // still-playing stateless-audio utterance would strand that speak() call's
123
+ // promise forever.
124
+ pendingStatelessStop = null;
125
+ // Settles the web-speech Promise handleWebSpeech() is currently awaiting,
126
+ // if any. Unlike the stateless path, the manager can't sever the
127
+ // TTSPlayer's own onend/onerror handlers directly -- its completion is
128
+ // entirely owned by the player (e.g. cancel() -> onend/onerror), and that
129
+ // callback can arrive late, or never, after a cancellation. stop()
130
+ // resolves this proactively so an interrupted web-speech turn doesn't
131
+ // stay pending on the player's own timing.
132
+ pendingWebSpeechStop = null;
133
+ // Bumped every time a new web-speech utterance starts. A canceled
134
+ // utterance's own completion (a late player callback, or another stop())
135
+ // can still arrive after a newer utterance has already taken over;
136
+ // handleWebSpeech()'s cleanup only runs when its own captured generation
137
+ // still matches the current one, so a stale completion can never act on
138
+ // a session that isn't its own.
139
+ webSpeechGeneration = 0;
119
140
  // Only the Web Speech lip-sync simulation is needed
120
141
  webSimulator;
121
142
  // Reads the emitter at emit time, so a later setEventEmitter() still applies.
@@ -180,6 +201,10 @@ var TTSManagerImpl = class {
180
201
  }
181
202
  this.lipSync.stop();
182
203
  this.endAudioSession();
204
+ this.pendingStatelessStop?.();
205
+ const settleWebSpeech = this.pendingWebSpeechStop;
206
+ this.pendingWebSpeechStop = null;
207
+ settleWebSpeech?.();
183
208
  }
184
209
  }
185
210
  /**
@@ -223,14 +248,25 @@ var TTSManagerImpl = class {
223
248
  * Handle the Web Speech API (simulated lip-sync)
224
249
  */
225
250
  async handleWebSpeech(text, options) {
251
+ const generation = ++this.webSpeechGeneration;
226
252
  this.startAudioSession();
227
253
  const effectiveRate = options?.rate !== void 0 ? Math.max(0.1, Math.min(10, options.rate)) : 1;
228
254
  this.webSimulator.startSimulation(text, effectiveRate);
255
+ let settleThis;
229
256
  try {
230
- await this.ttsPlayer.speak(text, options);
257
+ await new Promise((resolve, reject) => {
258
+ settleThis = resolve;
259
+ this.pendingWebSpeechStop = resolve;
260
+ this.ttsPlayer.speak(text, options).then(resolve, reject);
261
+ });
231
262
  } finally {
232
- this.webSimulator.stopSimulation();
233
- this.endAudioSession();
263
+ if (this.pendingWebSpeechStop === settleThis) {
264
+ this.pendingWebSpeechStop = null;
265
+ }
266
+ if (generation === this.webSpeechGeneration) {
267
+ this.webSimulator.stopSimulation();
268
+ this.endAudioSession();
269
+ }
234
270
  }
235
271
  }
236
272
  /**
@@ -259,6 +295,7 @@ var TTSManagerImpl = class {
259
295
  const finalize = (next) => {
260
296
  if (isFinalized) return;
261
297
  isFinalized = true;
298
+ this.pendingStatelessStop = null;
262
299
  if (this.currentAudioUrl) {
263
300
  URL.revokeObjectURL(this.currentAudioUrl);
264
301
  this.currentAudioUrl = null;
@@ -268,6 +305,7 @@ var TTSManagerImpl = class {
268
305
  this.endAudioSession();
269
306
  next();
270
307
  };
308
+ this.pendingStatelessStop = () => finalize(resolve);
271
309
  audio.onended = () => {
272
310
  finalize(resolve);
273
311
  };
package/dist/index.mjs CHANGED
@@ -95,6 +95,27 @@ var TTSManagerImpl = class {
95
95
  playbackMode;
96
96
  isAudioSessionActive = false;
97
97
  teardownBrowserLifecycle;
98
+ // Settles the stateless-audio Promise that handleStatelessAudio() is
99
+ // currently awaiting, if any. stop() clears currentAudio's onended/onerror
100
+ // handlers so they can never fire on their own; without this, stopping a
101
+ // still-playing stateless-audio utterance would strand that speak() call's
102
+ // promise forever.
103
+ pendingStatelessStop = null;
104
+ // Settles the web-speech Promise handleWebSpeech() is currently awaiting,
105
+ // if any. Unlike the stateless path, the manager can't sever the
106
+ // TTSPlayer's own onend/onerror handlers directly -- its completion is
107
+ // entirely owned by the player (e.g. cancel() -> onend/onerror), and that
108
+ // callback can arrive late, or never, after a cancellation. stop()
109
+ // resolves this proactively so an interrupted web-speech turn doesn't
110
+ // stay pending on the player's own timing.
111
+ pendingWebSpeechStop = null;
112
+ // Bumped every time a new web-speech utterance starts. A canceled
113
+ // utterance's own completion (a late player callback, or another stop())
114
+ // can still arrive after a newer utterance has already taken over;
115
+ // handleWebSpeech()'s cleanup only runs when its own captured generation
116
+ // still matches the current one, so a stale completion can never act on
117
+ // a session that isn't its own.
118
+ webSpeechGeneration = 0;
98
119
  // Only the Web Speech lip-sync simulation is needed
99
120
  webSimulator;
100
121
  // Reads the emitter at emit time, so a later setEventEmitter() still applies.
@@ -159,6 +180,10 @@ var TTSManagerImpl = class {
159
180
  }
160
181
  this.lipSync.stop();
161
182
  this.endAudioSession();
183
+ this.pendingStatelessStop?.();
184
+ const settleWebSpeech = this.pendingWebSpeechStop;
185
+ this.pendingWebSpeechStop = null;
186
+ settleWebSpeech?.();
162
187
  }
163
188
  }
164
189
  /**
@@ -202,14 +227,25 @@ var TTSManagerImpl = class {
202
227
  * Handle the Web Speech API (simulated lip-sync)
203
228
  */
204
229
  async handleWebSpeech(text, options) {
230
+ const generation = ++this.webSpeechGeneration;
205
231
  this.startAudioSession();
206
232
  const effectiveRate = options?.rate !== void 0 ? Math.max(0.1, Math.min(10, options.rate)) : 1;
207
233
  this.webSimulator.startSimulation(text, effectiveRate);
234
+ let settleThis;
208
235
  try {
209
- await this.ttsPlayer.speak(text, options);
236
+ await new Promise((resolve, reject) => {
237
+ settleThis = resolve;
238
+ this.pendingWebSpeechStop = resolve;
239
+ this.ttsPlayer.speak(text, options).then(resolve, reject);
240
+ });
210
241
  } finally {
211
- this.webSimulator.stopSimulation();
212
- this.endAudioSession();
242
+ if (this.pendingWebSpeechStop === settleThis) {
243
+ this.pendingWebSpeechStop = null;
244
+ }
245
+ if (generation === this.webSpeechGeneration) {
246
+ this.webSimulator.stopSimulation();
247
+ this.endAudioSession();
248
+ }
213
249
  }
214
250
  }
215
251
  /**
@@ -238,6 +274,7 @@ var TTSManagerImpl = class {
238
274
  const finalize = (next) => {
239
275
  if (isFinalized) return;
240
276
  isFinalized = true;
277
+ this.pendingStatelessStop = null;
241
278
  if (this.currentAudioUrl) {
242
279
  URL.revokeObjectURL(this.currentAudioUrl);
243
280
  this.currentAudioUrl = null;
@@ -247,6 +284,7 @@ var TTSManagerImpl = class {
247
284
  this.endAudioSession();
248
285
  next();
249
286
  };
287
+ this.pendingStatelessStop = () => finalize(resolve);
250
288
  audio.onended = () => {
251
289
  finalize(resolve);
252
290
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charivo/tts",
3
- "version": "0.6.5",
3
+ "version": "0.6.7",
4
4
  "description": "TTS manager and browser adapters for Charivo",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -36,7 +36,7 @@
36
36
  ],
37
37
  "dependencies": {
38
38
  "openai": "^4.47.1",
39
- "@charivo/core": "^0.21.0"
39
+ "@charivo/core": "^0.23.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "tsup": "^8.5.0",