@charivo/tts 0.8.1 → 0.9.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.
@@ -1,25 +1,41 @@
1
1
  // src/remote/index.ts
2
2
  import {
3
3
  CharivoProviderError,
4
+ CharivoTimeoutError,
4
5
  CharivoTransportError,
5
6
  DEFAULT_FETCH_TIMEOUT_MS,
6
7
  fetchWithTimeout,
7
8
  readResponseErrorMessage
8
9
  } from "@charivo/core";
10
+ var STREAM_INACTIVITY_TIMEOUT_MS = 1e4;
11
+ var DEFAULT_STREAM_SAMPLE_RATE = 24e3;
12
+ var DEFAULT_STREAM_CHANNELS = 1;
9
13
  var RemoteTTSPlayer = class {
10
14
  playbackMode = "audio";
11
15
  /**
16
+ * The container of the buffered `generateAudio` path only -- the streaming
17
+ * path carries its layout in `TTSPcmStream.format` instead.
18
+ *
12
19
  * Follows what the server said it sent, because the endpoint decides the
13
20
  * container: the demo's OpenAI route answers with MPEG and its Gemini one
14
21
  * with WAV. Naming either here would mislabel the other. Until a response
15
22
  * has been seen this is WAV, matching the manager's own fallback.
16
23
  */
17
24
  audioMimeType = "audio/wav";
25
+ /**
26
+ * Present only on a streaming player: the manager selects the streaming
27
+ * path with `typeof player.generateAudioStream === "function"`, so an
28
+ * unflagged player must not carry the method at all.
29
+ */
30
+ generateAudioStream;
18
31
  apiEndpoint;
19
32
  defaultVoice;
20
33
  constructor(config = {}) {
21
34
  this.apiEndpoint = config.apiEndpoint || "/api/tts";
22
35
  this.defaultVoice = config.defaultVoice || "marin";
36
+ if (config.streaming) {
37
+ this.generateAudioStream = (text, options, signal) => this.requestAudioStream(text, options, signal);
38
+ }
23
39
  }
24
40
  /**
25
41
  * Stateless audio generation (used by the TTS Manager)
@@ -30,12 +46,7 @@ var RemoteTTSPlayer = class {
30
46
  {
31
47
  method: "POST",
32
48
  headers: { "Content-Type": "application/json" },
33
- body: JSON.stringify({
34
- text,
35
- voice: options?.voice || this.defaultVoice,
36
- speed: options?.rate || 1,
37
- format: "wav"
38
- })
49
+ body: this.buildRequestBody(text, options)
39
50
  },
40
51
  {
41
52
  timeoutMessage: `TTS request timed out after ${DEFAULT_FETCH_TIMEOUT_MS}ms`,
@@ -58,6 +69,124 @@ var RemoteTTSPlayer = class {
58
69
  }
59
70
  );
60
71
  }
72
+ /** The one request payload both paths send; only the Accept header differs. */
73
+ buildRequestBody(text, options) {
74
+ return JSON.stringify({
75
+ text,
76
+ voice: options?.voice || this.defaultVoice,
77
+ speed: options?.rate || 1,
78
+ format: "wav"
79
+ });
80
+ }
81
+ async requestAudioStream(text, options, signal) {
82
+ const response = await fetchWithTimeout(
83
+ this.apiEndpoint,
84
+ {
85
+ method: "POST",
86
+ headers: {
87
+ "Content-Type": "application/json",
88
+ // A streaming route answers `audio/pcm`; a route that ignores this
89
+ // answers its usual container, which is rejected below.
90
+ Accept: "audio/pcm"
91
+ },
92
+ body: this.buildRequestBody(text, options)
93
+ },
94
+ {
95
+ timeoutMessage: `TTS request timed out after ${DEFAULT_FETCH_TIMEOUT_MS}ms`,
96
+ failureMessage: "TTS request failed",
97
+ signal
98
+ },
99
+ // Returns the Response itself rather than draining it: the helper's
100
+ // deadline covers connecting, the headers and an error body, and then
101
+ // clears. Everything after that is the inactivity timer's business.
102
+ async (settled) => {
103
+ if (!settled.ok) {
104
+ throw new CharivoProviderError(
105
+ `TTS API failed: ${await readResponseErrorMessage(settled)}`
106
+ );
107
+ }
108
+ return settled;
109
+ }
110
+ );
111
+ const rejectAnswer = (reason) => {
112
+ void response.body?.cancel().catch(() => void 0);
113
+ throw new CharivoProviderError(
114
+ `Streaming TTS is enabled, but ${this.apiEndpoint} ${reason}`
115
+ );
116
+ };
117
+ const contentType = response.headers.get("content-type");
118
+ const format = parsePcmContentType(contentType);
119
+ if (!format) {
120
+ return rejectAnswer(
121
+ `answered "${contentType ?? "no content type"}" instead of audio/pcm`
122
+ );
123
+ }
124
+ if (format.channels !== 1) {
125
+ return rejectAnswer(
126
+ `answered ${format.channels} audio channels; the manager plays mono only`
127
+ );
128
+ }
129
+ const upstream = response.body;
130
+ if (!upstream) {
131
+ return rejectAnswer("answered audio/pcm with no body to read");
132
+ }
133
+ const reader = upstream.getReader();
134
+ let inactivityTimer;
135
+ let stopped = false;
136
+ let streamController;
137
+ const stopUpstream = () => {
138
+ stopped = true;
139
+ clearTimeout(inactivityTimer);
140
+ signal?.removeEventListener("abort", onCallerAbort);
141
+ void reader.cancel().catch(() => void 0);
142
+ };
143
+ const onCallerAbort = () => {
144
+ stopUpstream();
145
+ streamController?.error(signal.reason);
146
+ };
147
+ const body = new ReadableStream({
148
+ start(controller) {
149
+ streamController = controller;
150
+ },
151
+ async pull(controller) {
152
+ const read = reader.read();
153
+ const stalled = new Promise((_, reject) => {
154
+ inactivityTimer = setTimeout(() => {
155
+ reject(
156
+ new CharivoTimeoutError(
157
+ `TTS stream stalled for ${STREAM_INACTIVITY_TIMEOUT_MS}ms`
158
+ )
159
+ );
160
+ }, STREAM_INACTIVITY_TIMEOUT_MS);
161
+ });
162
+ try {
163
+ const result = await Promise.race([read, stalled]);
164
+ clearTimeout(inactivityTimer);
165
+ if (stopped) {
166
+ return;
167
+ }
168
+ if (result.done) {
169
+ stopUpstream();
170
+ controller.close();
171
+ return;
172
+ }
173
+ controller.enqueue(result.value);
174
+ } catch (error) {
175
+ stopUpstream();
176
+ throw error;
177
+ }
178
+ },
179
+ cancel() {
180
+ stopUpstream();
181
+ }
182
+ });
183
+ if (signal?.aborted) {
184
+ onCallerAbort();
185
+ } else {
186
+ signal?.addEventListener("abort", onCallerAbort);
187
+ }
188
+ return { format, body };
189
+ }
61
190
  /**
62
191
  * Legacy speak method (kept for compatibility)
63
192
  */
@@ -90,6 +219,28 @@ var RemoteTTSPlayer = class {
90
219
  return typeof window !== "undefined" && typeof fetch !== "undefined";
91
220
  }
92
221
  };
222
+ function parsePcmContentType(header) {
223
+ const [mediaType, ...parameters] = (header ?? "").split(";");
224
+ if (mediaType?.trim().toLowerCase() !== "audio/pcm") {
225
+ return null;
226
+ }
227
+ let sampleRate = DEFAULT_STREAM_SAMPLE_RATE;
228
+ let channels = DEFAULT_STREAM_CHANNELS;
229
+ for (const parameter of parameters) {
230
+ const [rawName, rawValue] = parameter.split("=");
231
+ const name = rawName?.trim().toLowerCase();
232
+ const value = Number(rawValue?.trim());
233
+ if (!Number.isInteger(value) || value <= 0) {
234
+ continue;
235
+ }
236
+ if (name === "rate") {
237
+ sampleRate = value;
238
+ } else if (name === "channels") {
239
+ channels = value;
240
+ }
241
+ }
242
+ return { encoding: "pcm-s16le", sampleRate, channels };
243
+ }
93
244
  function createRemoteTTSPlayer(config) {
94
245
  return new RemoteTTSPlayer(config);
95
246
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charivo/tts",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "TTS manager and browser adapters for Charivo",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -40,7 +40,7 @@
40
40
  "lipsync"
41
41
  ],
42
42
  "dependencies": {
43
- "@charivo/core": "^0.34.0",
43
+ "@charivo/core": "^0.35.0",
44
44
  "openai": "^4.47.1"
45
45
  },
46
46
  "devDependencies": {