@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.
- package/README.md +61 -29
- package/dist/gemini/index.d.mts +43 -11
- package/dist/gemini/index.d.ts +43 -11
- package/dist/gemini/index.js +228 -17
- package/dist/gemini/index.mjs +228 -17
- package/dist/index.js +312 -18
- package/dist/index.mjs +312 -18
- package/dist/remote/index.d.mts +7 -0
- package/dist/remote/index.d.ts +7 -0
- package/dist/remote/index.js +156 -6
- package/dist/remote/index.mjs +157 -6
- package/package.json +2 -2
package/dist/gemini/index.mjs
CHANGED
|
@@ -15,6 +15,9 @@ var DEFAULT_SAMPLE_RATE = 24e3;
|
|
|
15
15
|
var DEFAULT_CHANNELS = 1;
|
|
16
16
|
var BITS_PER_SAMPLE = 16;
|
|
17
17
|
var WAV_HEADER_BYTES = 44;
|
|
18
|
+
var SSE_FRAME_SEPARATOR = /\r?\n\r?\n/;
|
|
19
|
+
var SSE_LINE_SEPARATOR = /\r?\n/;
|
|
20
|
+
var SSE_DATA_PREFIX = "data:";
|
|
18
21
|
var GeminiTTSProvider = class {
|
|
19
22
|
apiKey;
|
|
20
23
|
baseUrl;
|
|
@@ -43,17 +46,7 @@ var GeminiTTSProvider = class {
|
|
|
43
46
|
}
|
|
44
47
|
async generateSpeech(text, options) {
|
|
45
48
|
const url = `${this.baseUrl}/v1beta/models/${this.model}:generateContent`;
|
|
46
|
-
const body =
|
|
47
|
-
contents: [{ parts: [{ text: `${PROMPT_PREAMBLE}${text}` }] }],
|
|
48
|
-
generationConfig: {
|
|
49
|
-
responseModalities: ["AUDIO"],
|
|
50
|
-
speechConfig: {
|
|
51
|
-
voiceConfig: {
|
|
52
|
-
prebuiltVoiceConfig: { voiceName: options?.voice || this.voice }
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
});
|
|
49
|
+
const body = this.buildRequestBody(text, options?.voice || this.voice);
|
|
57
50
|
try {
|
|
58
51
|
const deadline = Date.now() + this.timeoutMs;
|
|
59
52
|
const first = await this.requestOnce(url, body, this.timeoutMs);
|
|
@@ -75,6 +68,49 @@ var GeminiTTSProvider = class {
|
|
|
75
68
|
throw toCharivoError("provider", error, "Gemini TTS request failed");
|
|
76
69
|
}
|
|
77
70
|
}
|
|
71
|
+
async generateSpeechStream(text, options, signal) {
|
|
72
|
+
const url = `${this.baseUrl}/v1beta/models/${this.model}:streamGenerateContent?alt=sse`;
|
|
73
|
+
const body = this.buildRequestBody(text, options?.voice || this.voice);
|
|
74
|
+
try {
|
|
75
|
+
const deadline = Date.now() + this.timeoutMs;
|
|
76
|
+
const first = await this.openStreamOnce(
|
|
77
|
+
url,
|
|
78
|
+
body,
|
|
79
|
+
this.timeoutMs,
|
|
80
|
+
signal
|
|
81
|
+
);
|
|
82
|
+
if ("stream" in first) {
|
|
83
|
+
return first.stream;
|
|
84
|
+
}
|
|
85
|
+
const remainingMs = deadline - Date.now();
|
|
86
|
+
if (remainingMs <= 0) {
|
|
87
|
+
throw new CharivoTimeoutError(this.timeoutMessage, {
|
|
88
|
+
cause: first.retry
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const second = await this.openStreamOnce(url, body, remainingMs, signal);
|
|
92
|
+
if ("stream" in second) {
|
|
93
|
+
return second.stream;
|
|
94
|
+
}
|
|
95
|
+
throw second.retry;
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (signal?.aborted && (error === signal.reason || isAbortError(error))) {
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
throw toCharivoError("provider", error, "Gemini TTS request failed");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
buildRequestBody(text, voice) {
|
|
104
|
+
return JSON.stringify({
|
|
105
|
+
contents: [{ parts: [{ text: `${PROMPT_PREAMBLE}${text}` }] }],
|
|
106
|
+
generationConfig: {
|
|
107
|
+
responseModalities: ["AUDIO"],
|
|
108
|
+
speechConfig: {
|
|
109
|
+
voiceConfig: { prebuiltVoiceConfig: { voiceName: voice } }
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
78
114
|
requestOnce(url, body, timeoutMs) {
|
|
79
115
|
return fetchWithTimeout(
|
|
80
116
|
url,
|
|
@@ -128,16 +164,166 @@ var GeminiTTSProvider = class {
|
|
|
128
164
|
}
|
|
129
165
|
);
|
|
130
166
|
}
|
|
167
|
+
openStreamOnce(url, body, timeoutMs, signal) {
|
|
168
|
+
const attempt = new AbortController();
|
|
169
|
+
const onCallerAbort = () => attempt.abort(signal.reason);
|
|
170
|
+
if (signal?.aborted) {
|
|
171
|
+
attempt.abort(signal.reason);
|
|
172
|
+
} else {
|
|
173
|
+
signal?.addEventListener("abort", onCallerAbort);
|
|
174
|
+
}
|
|
175
|
+
let open = null;
|
|
176
|
+
let cancelledByConsumer = false;
|
|
177
|
+
let deliverStream;
|
|
178
|
+
const delivery = new Promise((resolve) => {
|
|
179
|
+
deliverStream = resolve;
|
|
180
|
+
});
|
|
181
|
+
const request = fetchWithTimeout(
|
|
182
|
+
url,
|
|
183
|
+
{
|
|
184
|
+
method: "POST",
|
|
185
|
+
headers: {
|
|
186
|
+
// Never in the URL: proxies and request logs capture query strings.
|
|
187
|
+
"x-goog-api-key": this.apiKey,
|
|
188
|
+
"Content-Type": "application/json"
|
|
189
|
+
},
|
|
190
|
+
body
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
timeoutMs,
|
|
194
|
+
timeoutMessage: this.timeoutMessage,
|
|
195
|
+
signal: attempt.signal,
|
|
196
|
+
mapError: (error) => toCharivoError("provider", error, "Gemini TTS request failed")
|
|
197
|
+
},
|
|
198
|
+
// The whole SSE pump runs in here, not after this helper resolves: the
|
|
199
|
+
// helper unwires its timer and the caller's signal the moment this
|
|
200
|
+
// callback settles, and an unwired request keeps downloading — keeps
|
|
201
|
+
// Gemini synthesizing, and billing — long after a stop().
|
|
202
|
+
async (response) => {
|
|
203
|
+
if (response.status >= 500) {
|
|
204
|
+
return {
|
|
205
|
+
retry: new CharivoProviderError(
|
|
206
|
+
`Gemini TTS Error: ${await readResponseText(response)}`
|
|
207
|
+
)
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
if (!response.ok) {
|
|
211
|
+
throw new CharivoProviderError(
|
|
212
|
+
`Gemini TTS Error: ${await readResponseText(response)}`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
const noAudio = () => ({
|
|
216
|
+
retry: new CharivoProviderError(
|
|
217
|
+
"Gemini TTS Error: response contained no audio"
|
|
218
|
+
)
|
|
219
|
+
});
|
|
220
|
+
const fail = (message) => {
|
|
221
|
+
attempt.abort();
|
|
222
|
+
throw new CharivoProviderError(`Gemini TTS Error: ${message}`);
|
|
223
|
+
};
|
|
224
|
+
const openWith = (audio, pcm) => {
|
|
225
|
+
const { sampleRate, channels } = parseL16MimeType(audio.mimeType);
|
|
226
|
+
let controller;
|
|
227
|
+
const streamBody = new ReadableStream({
|
|
228
|
+
start(streamController) {
|
|
229
|
+
controller = streamController;
|
|
230
|
+
streamController.enqueue(pcm);
|
|
231
|
+
},
|
|
232
|
+
cancel() {
|
|
233
|
+
cancelledByConsumer = true;
|
|
234
|
+
attempt.abort();
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
return {
|
|
238
|
+
controller,
|
|
239
|
+
mimeType: audio.mimeType,
|
|
240
|
+
result: {
|
|
241
|
+
stream: {
|
|
242
|
+
body: streamBody,
|
|
243
|
+
format: { encoding: "pcm-s16le", sampleRate, channels }
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
const reader = response.body?.getReader();
|
|
249
|
+
if (!reader) {
|
|
250
|
+
return noAudio();
|
|
251
|
+
}
|
|
252
|
+
const decoder = new TextDecoder();
|
|
253
|
+
let buffer = "";
|
|
254
|
+
try {
|
|
255
|
+
for (; ; ) {
|
|
256
|
+
const { done, value } = await reader.read();
|
|
257
|
+
if (done) {
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
buffer += decoder.decode(value, { stream: true });
|
|
261
|
+
const frames = buffer.split(SSE_FRAME_SEPARATOR);
|
|
262
|
+
buffer = frames.pop() ?? "";
|
|
263
|
+
for (const frame of frames) {
|
|
264
|
+
const { audio, finishReason } = extractStreamEvent(
|
|
265
|
+
parseSseData(frame)
|
|
266
|
+
);
|
|
267
|
+
if (audio) {
|
|
268
|
+
const pcm = decodeBase64(audio.data);
|
|
269
|
+
if (!open) {
|
|
270
|
+
open = openWith(audio, pcm);
|
|
271
|
+
deliverStream(open.result);
|
|
272
|
+
} else if (audio.mimeType !== open.mimeType) {
|
|
273
|
+
fail(
|
|
274
|
+
`audio format changed to "${audio.mimeType}" mid-stream`
|
|
275
|
+
);
|
|
276
|
+
} else {
|
|
277
|
+
open.controller.enqueue(pcm);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (finishReason) {
|
|
281
|
+
void reader.cancel().catch(() => void 0);
|
|
282
|
+
if (!open) {
|
|
283
|
+
return noAudio();
|
|
284
|
+
}
|
|
285
|
+
if (finishReason !== "STOP") {
|
|
286
|
+
fail(`stream ended with finishReason "${finishReason}"`);
|
|
287
|
+
}
|
|
288
|
+
open.controller.close();
|
|
289
|
+
return open.result;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return open ? fail("stream ended without a finish reason") : noAudio();
|
|
294
|
+
} catch (error) {
|
|
295
|
+
attempt.abort();
|
|
296
|
+
throw error;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
);
|
|
300
|
+
const outcome = request.then(
|
|
301
|
+
(result) => {
|
|
302
|
+
signal?.removeEventListener("abort", onCallerAbort);
|
|
303
|
+
return result;
|
|
304
|
+
},
|
|
305
|
+
(error) => {
|
|
306
|
+
signal?.removeEventListener("abort", onCallerAbort);
|
|
307
|
+
if (!open) {
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
if (!cancelledByConsumer) {
|
|
311
|
+
open.controller.error(
|
|
312
|
+
isAbortError(error) ? error : toCharivoError("provider", error, "Gemini TTS request failed")
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
return open.result;
|
|
316
|
+
}
|
|
317
|
+
);
|
|
318
|
+
return Promise.race([delivery, outcome]);
|
|
319
|
+
}
|
|
131
320
|
};
|
|
132
321
|
function createGeminiTTSProvider(config) {
|
|
133
322
|
return new GeminiTTSProvider(config);
|
|
134
323
|
}
|
|
135
324
|
function extractInlineAudio(payload) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
const candidate = Array.isArray(payload.candidates) ? payload.candidates[0] : void 0;
|
|
140
|
-
if (!isRecord(candidate) || !isRecord(candidate.content)) {
|
|
325
|
+
const candidate = firstCandidate(payload);
|
|
326
|
+
if (!candidate || !isRecord(candidate.content)) {
|
|
141
327
|
return null;
|
|
142
328
|
}
|
|
143
329
|
const parts = Array.isArray(candidate.content.parts) ? candidate.content.parts : [];
|
|
@@ -150,6 +336,22 @@ function extractInlineAudio(payload) {
|
|
|
150
336
|
const { mimeType, data } = part.inlineData;
|
|
151
337
|
return typeof mimeType === "string" && typeof data === "string" ? { mimeType, data } : null;
|
|
152
338
|
}
|
|
339
|
+
function extractStreamEvent(payload) {
|
|
340
|
+
const candidate = firstCandidate(payload);
|
|
341
|
+
const finishReason = candidate && typeof candidate.finishReason === "string" ? candidate.finishReason : null;
|
|
342
|
+
return { audio: extractInlineAudio(payload), finishReason };
|
|
343
|
+
}
|
|
344
|
+
function parseSseData(frame) {
|
|
345
|
+
const data = frame.split(SSE_LINE_SEPARATOR).filter((line) => line.startsWith(SSE_DATA_PREFIX)).map((line) => line.slice(SSE_DATA_PREFIX.length).trim()).join("\n");
|
|
346
|
+
return data ? JSON.parse(data) : null;
|
|
347
|
+
}
|
|
348
|
+
function firstCandidate(payload) {
|
|
349
|
+
if (!isRecord(payload) || !Array.isArray(payload.candidates)) {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
const candidate = payload.candidates[0];
|
|
353
|
+
return isRecord(candidate) ? candidate : null;
|
|
354
|
+
}
|
|
153
355
|
function parseL16MimeType(mimeType) {
|
|
154
356
|
const [mediaType, ...parameters] = mimeType.split(";");
|
|
155
357
|
if (mediaType?.trim().toLowerCase() !== "audio/l16") {
|
|
@@ -246,12 +448,21 @@ function isAbortError(error) {
|
|
|
246
448
|
var GeminiTTSPlayer = class {
|
|
247
449
|
playbackMode = "audio";
|
|
248
450
|
audioMimeType = "audio/wav";
|
|
451
|
+
/**
|
|
452
|
+
* Present only on a streaming player: the manager selects the streaming
|
|
453
|
+
* path with `typeof player.generateAudioStream === "function"`, so an
|
|
454
|
+
* unflagged player must not carry the method at all.
|
|
455
|
+
*/
|
|
456
|
+
generateAudioStream;
|
|
249
457
|
provider;
|
|
250
|
-
constructor(config) {
|
|
458
|
+
constructor({ streaming, ...config }) {
|
|
251
459
|
this.provider = createGeminiTTSProvider({
|
|
252
460
|
...config,
|
|
253
461
|
dangerouslyAllowBrowser: true
|
|
254
462
|
});
|
|
463
|
+
if (streaming) {
|
|
464
|
+
this.generateAudioStream = (text, options, signal) => this.provider.generateSpeechStream(text, options, signal);
|
|
465
|
+
}
|
|
255
466
|
}
|
|
256
467
|
/**
|
|
257
468
|
* Stateless audio generation (used by the TTS Manager)
|