@micdrop/elevenlabs 1.1.3 → 1.2.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 +44 -6
- package/dist/index.d.mts +34 -20
- package/dist/index.d.ts +34 -20
- package/dist/index.js +415 -36
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +413 -35
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -9
- package/LICENSE +0 -9
package/dist/index.js
CHANGED
|
@@ -32,27 +32,352 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
DEFAULT_MODEL_ID: () => DEFAULT_MODEL_ID,
|
|
34
34
|
DEFAULT_OUTPUT_FORMAT: () => DEFAULT_OUTPUT_FORMAT,
|
|
35
|
-
ElevenLabsTTS: () => ElevenLabsTTS
|
|
35
|
+
ElevenLabsTTS: () => ElevenLabsTTS,
|
|
36
|
+
isDialogueModel: () => isDialogueModel
|
|
36
37
|
});
|
|
37
38
|
module.exports = __toCommonJS(index_exports);
|
|
38
39
|
|
|
39
40
|
// src/ElevenLabsTTS.ts
|
|
40
41
|
var import_server = require("@micdrop/server");
|
|
42
|
+
|
|
43
|
+
// src/DialogueWebSocket.ts
|
|
44
|
+
var import_ws2 = __toESM(require("ws"));
|
|
45
|
+
|
|
46
|
+
// src/ElevenLabsWebSocket.ts
|
|
41
47
|
var import_ws = __toESM(require("ws"));
|
|
48
|
+
function closeSocket(socket) {
|
|
49
|
+
socket.removeAllListeners();
|
|
50
|
+
socket.on("error", () => {
|
|
51
|
+
});
|
|
52
|
+
if (socket.readyState === import_ws.default.OPEN || socket.readyState === import_ws.default.CONNECTING) {
|
|
53
|
+
socket.close(1e3);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
42
56
|
|
|
43
57
|
// src/types.ts
|
|
44
58
|
var DEFAULT_MODEL_ID = "eleven_turbo_v2_5";
|
|
45
59
|
var DEFAULT_OUTPUT_FORMAT = "pcm_16000";
|
|
60
|
+
function isDialogueModel(modelId) {
|
|
61
|
+
return modelId.startsWith("eleven_v3") || modelId.startsWith("eleven_v4");
|
|
62
|
+
}
|
|
46
63
|
|
|
47
|
-
// src/
|
|
48
|
-
var
|
|
64
|
+
// src/DialogueWebSocket.ts
|
|
65
|
+
var KEEP_ALIVE_INTERVAL = 15e3;
|
|
49
66
|
var DEFAULT_CONNECTION_TIMEOUT = 5e3;
|
|
50
67
|
var DEFAULT_RETRY_DELAY = 1e3;
|
|
51
68
|
var DEFAULT_MAX_RETRY = 3;
|
|
52
|
-
var
|
|
53
|
-
|
|
54
|
-
|
|
69
|
+
var SENTENCE_END = /(?:[!?]+|(?<!\.)\.(?!\.))\s+/g;
|
|
70
|
+
var DialogueWebSocket = class {
|
|
71
|
+
// Whether the connection received any text
|
|
72
|
+
constructor(options, host) {
|
|
73
|
+
this.options = options;
|
|
74
|
+
this.host = host;
|
|
75
|
+
this.retryCount = 0;
|
|
76
|
+
this.destroyed = false;
|
|
77
|
+
// Bumped by every speak() and every cancel(), so a stream that starts late
|
|
78
|
+
// can tell whether it is still the one that should be heard.
|
|
79
|
+
this.generation = 0;
|
|
80
|
+
// Generation of the stream being spoken. A speak() that never carries a
|
|
81
|
+
// word leaves it in place, so the sentence it sends goes on to its end.
|
|
82
|
+
this.speaking = 0;
|
|
83
|
+
this.isProcessing = false;
|
|
84
|
+
this.textEnded = false;
|
|
85
|
+
// Whether the text stream has ended
|
|
86
|
+
this.textSent = "";
|
|
87
|
+
// Text of the utterance sent to ElevenLabs
|
|
88
|
+
this.textBuffer = "";
|
|
89
|
+
// End of the last chunk, held until its word is complete
|
|
90
|
+
this.unflushed = false;
|
|
91
|
+
// Whether text was sent since the last flush
|
|
92
|
+
this.pendingFlushes = 0;
|
|
93
|
+
// Flushes whose audio is not complete yet
|
|
94
|
+
this.newTurn = false;
|
|
95
|
+
// Whether the next input starts a new utterance
|
|
96
|
+
this.hasSpoken = false;
|
|
97
|
+
this.connect();
|
|
98
|
+
}
|
|
99
|
+
speak(textStream) {
|
|
100
|
+
const generation = ++this.generation;
|
|
101
|
+
let started = false;
|
|
102
|
+
const start = () => {
|
|
103
|
+
if (started) return this.speaking === generation;
|
|
104
|
+
if (this.generation !== generation) return false;
|
|
105
|
+
started = true;
|
|
106
|
+
if (this.isProcessing) this.restart();
|
|
107
|
+
this.speaking = generation;
|
|
108
|
+
this.isProcessing = true;
|
|
109
|
+
this.textEnded = false;
|
|
110
|
+
this.textSent = "";
|
|
111
|
+
this.textBuffer = "";
|
|
112
|
+
this.unflushed = false;
|
|
113
|
+
this.pendingFlushes = 0;
|
|
114
|
+
this.newTurn = true;
|
|
115
|
+
return true;
|
|
116
|
+
};
|
|
117
|
+
textStream.on("data", async (chunk) => {
|
|
118
|
+
if (!start()) return;
|
|
119
|
+
const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
|
|
120
|
+
await this.initPromise;
|
|
121
|
+
if (this.speaking !== generation) return;
|
|
122
|
+
const spaceIndex = text.lastIndexOf(" ");
|
|
123
|
+
if (spaceIndex === -1) {
|
|
124
|
+
this.textBuffer += text;
|
|
125
|
+
} else {
|
|
126
|
+
this.sendText(this.textBuffer + text.slice(0, spaceIndex + 1));
|
|
127
|
+
this.textBuffer = text.slice(spaceIndex + 1);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
textStream.on("error", (error) => {
|
|
131
|
+
if (!started || this.speaking !== generation) return;
|
|
132
|
+
this.log("Error in text stream, ending audio stream", error);
|
|
133
|
+
this.isProcessing = false;
|
|
134
|
+
});
|
|
135
|
+
textStream.on("end", async () => {
|
|
136
|
+
if (!started) return;
|
|
137
|
+
await this.initPromise;
|
|
138
|
+
if (this.speaking !== generation) return;
|
|
139
|
+
if (this.textBuffer.trim()) {
|
|
140
|
+
this.sendText(this.textBuffer + " ");
|
|
141
|
+
this.textBuffer = "";
|
|
142
|
+
}
|
|
143
|
+
this.textEnded = true;
|
|
144
|
+
this.flush();
|
|
145
|
+
this.checkEnded();
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
cancel() {
|
|
149
|
+
this.generation++;
|
|
150
|
+
this.speaking = 0;
|
|
151
|
+
if (!this.isProcessing) return;
|
|
152
|
+
this.log("Cancel");
|
|
153
|
+
this.isProcessing = false;
|
|
154
|
+
this.textSent = "";
|
|
155
|
+
this.textBuffer = "";
|
|
156
|
+
this.restart();
|
|
157
|
+
}
|
|
158
|
+
destroy() {
|
|
159
|
+
this.destroyed = true;
|
|
160
|
+
if (this.reconnectTimeout) {
|
|
161
|
+
clearTimeout(this.reconnectTimeout);
|
|
162
|
+
this.reconnectTimeout = void 0;
|
|
163
|
+
}
|
|
164
|
+
this.dropSocket();
|
|
165
|
+
this.isProcessing = false;
|
|
166
|
+
}
|
|
167
|
+
connect() {
|
|
168
|
+
this.initPromise = this.initWS().catch((error) => {
|
|
169
|
+
console.error("[ElevenLabsTTS] Connection error:", error);
|
|
170
|
+
this.reconnect();
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/** Drops the connection, with the synthesis running on it, for a new one */
|
|
174
|
+
restart() {
|
|
175
|
+
if (this.reconnectTimeout) {
|
|
176
|
+
clearTimeout(this.reconnectTimeout);
|
|
177
|
+
this.reconnectTimeout = void 0;
|
|
178
|
+
}
|
|
179
|
+
this.dropSocket();
|
|
180
|
+
this.connect();
|
|
181
|
+
}
|
|
182
|
+
/** Closes the connection without reconnecting */
|
|
183
|
+
dropSocket() {
|
|
184
|
+
if (this.connectionTimeout) {
|
|
185
|
+
clearTimeout(this.connectionTimeout);
|
|
186
|
+
this.connectionTimeout = void 0;
|
|
187
|
+
}
|
|
188
|
+
if (this.keepAliveInterval) {
|
|
189
|
+
clearInterval(this.keepAliveInterval);
|
|
190
|
+
this.keepAliveInterval = void 0;
|
|
191
|
+
}
|
|
192
|
+
if (this.socket) closeSocket(this.socket);
|
|
193
|
+
this.socket = void 0;
|
|
194
|
+
}
|
|
195
|
+
initWS() {
|
|
196
|
+
return new Promise((resolve, reject) => {
|
|
197
|
+
const params = new URLSearchParams();
|
|
198
|
+
params.append("model_id", this.options.modelId ?? DEFAULT_MODEL_ID);
|
|
199
|
+
params.append(
|
|
200
|
+
"output_format",
|
|
201
|
+
this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT
|
|
202
|
+
);
|
|
203
|
+
if (this.options.language) {
|
|
204
|
+
params.append("language_code", this.options.language);
|
|
205
|
+
}
|
|
206
|
+
const socket = new import_ws2.default(
|
|
207
|
+
`wss://api.elevenlabs.io/v1/text-to-dialogue/stream-input?${params.toString()}`,
|
|
208
|
+
{
|
|
209
|
+
headers: {
|
|
210
|
+
"xi-api-key": this.options.apiKey
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
this.socket = socket;
|
|
215
|
+
let opened = false;
|
|
216
|
+
const timeout = setTimeout(() => {
|
|
217
|
+
this.log("Connection timeout");
|
|
218
|
+
closeSocket(socket);
|
|
219
|
+
this.socket = void 0;
|
|
220
|
+
reject(new Error("WebSocket connection timeout"));
|
|
221
|
+
}, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT);
|
|
222
|
+
this.connectionTimeout = timeout;
|
|
223
|
+
socket.addEventListener("open", () => {
|
|
224
|
+
clearTimeout(timeout);
|
|
225
|
+
opened = true;
|
|
226
|
+
this.log("Connection opened");
|
|
227
|
+
const stability = this.options.voiceSettings?.stability;
|
|
228
|
+
this.send({
|
|
229
|
+
voices: [this.options.voiceId],
|
|
230
|
+
voice_settings: stability === void 0 ? void 0 : { stability }
|
|
231
|
+
});
|
|
232
|
+
this.hasSpoken = false;
|
|
233
|
+
this.keepAliveInterval = setInterval(() => {
|
|
234
|
+
this.send({ keep_alive: true });
|
|
235
|
+
this.retryCount = 0;
|
|
236
|
+
}, KEEP_ALIVE_INTERVAL);
|
|
237
|
+
resolve();
|
|
238
|
+
});
|
|
239
|
+
socket.addEventListener("error", (error) => {
|
|
240
|
+
clearTimeout(timeout);
|
|
241
|
+
this.log("WebSocket error:", error);
|
|
242
|
+
reject(new Error("WebSocket connection error"));
|
|
243
|
+
});
|
|
244
|
+
socket.addEventListener("message", (event) => {
|
|
245
|
+
try {
|
|
246
|
+
this.onMessage(JSON.parse(event.data.toString()));
|
|
247
|
+
} catch (error) {
|
|
248
|
+
this.log("message" in error ? error.message : error);
|
|
249
|
+
this.log("Event data during error:", event.data);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
socket.addEventListener("close", ({ code, reason }) => {
|
|
253
|
+
clearTimeout(timeout);
|
|
254
|
+
if (this.keepAliveInterval) {
|
|
255
|
+
clearInterval(this.keepAliveInterval);
|
|
256
|
+
this.keepAliveInterval = void 0;
|
|
257
|
+
}
|
|
258
|
+
this.socket?.removeAllListeners();
|
|
259
|
+
this.socket = void 0;
|
|
260
|
+
this.log("Connection closed", { code, reason });
|
|
261
|
+
if (opened) this.reconnect();
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
onMessage(message) {
|
|
266
|
+
if ("audio" in message && message.audio) {
|
|
267
|
+
const chunk = Buffer.from(message.audio, "base64");
|
|
268
|
+
this.log(`Received audio chunk (${chunk.length} bytes)`);
|
|
269
|
+
this.retryCount = 0;
|
|
270
|
+
this.host.audio(chunk);
|
|
271
|
+
}
|
|
272
|
+
if ("is_final_audio_for_turn" in message && message.is_final_audio_for_turn) {
|
|
273
|
+
this.pendingFlushes = Math.max(0, this.pendingFlushes - 1);
|
|
274
|
+
this.checkEnded();
|
|
275
|
+
}
|
|
276
|
+
if ("error" in message) {
|
|
277
|
+
console.error("[ElevenLabsTTS] Error:", message.error, message.message);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/** Sends text, flushing after each sentence it completes */
|
|
281
|
+
sendText(text) {
|
|
282
|
+
let start = 0;
|
|
283
|
+
for (const match of text.matchAll(SENTENCE_END)) {
|
|
284
|
+
const end = match.index + match[0].length;
|
|
285
|
+
this.sendInput(text.slice(start, end), true);
|
|
286
|
+
start = end;
|
|
287
|
+
}
|
|
288
|
+
if (start < text.length) {
|
|
289
|
+
this.sendInput(text.slice(start), false);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
sendInput(text, flush) {
|
|
293
|
+
this.textSent += text;
|
|
294
|
+
if (!text.trim()) return;
|
|
295
|
+
this.send({
|
|
296
|
+
inputs: [
|
|
297
|
+
{
|
|
298
|
+
text,
|
|
299
|
+
voice_id: this.options.voiceId,
|
|
300
|
+
// Resets the prosody between two utterances
|
|
301
|
+
new_turn: this.newTurn && this.hasSpoken || void 0
|
|
302
|
+
}
|
|
303
|
+
],
|
|
304
|
+
flush: flush || void 0
|
|
305
|
+
});
|
|
306
|
+
this.log(`Sent text${flush ? " and flushed" : ""}: "${text}"`);
|
|
307
|
+
this.newTurn = false;
|
|
308
|
+
this.hasSpoken = true;
|
|
309
|
+
if (flush) {
|
|
310
|
+
this.pendingFlushes++;
|
|
311
|
+
this.unflushed = false;
|
|
312
|
+
} else {
|
|
313
|
+
this.unflushed = true;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/** Generates the text held by the server, if any */
|
|
317
|
+
flush() {
|
|
318
|
+
if (!this.unflushed) return;
|
|
319
|
+
this.send({ flush: true });
|
|
320
|
+
this.pendingFlushes++;
|
|
321
|
+
this.unflushed = false;
|
|
322
|
+
this.log("Flushed text");
|
|
323
|
+
}
|
|
324
|
+
/** Ends the utterance once its text is over and all its audio came */
|
|
325
|
+
checkEnded() {
|
|
326
|
+
if (!this.isProcessing || !this.textEnded) return;
|
|
327
|
+
if (this.unflushed || this.pendingFlushes > 0) return;
|
|
328
|
+
this.log("Audio ended");
|
|
329
|
+
this.isProcessing = false;
|
|
330
|
+
this.textSent = "";
|
|
331
|
+
}
|
|
332
|
+
send(message) {
|
|
333
|
+
if (this.socket?.readyState !== import_ws2.default.OPEN) return;
|
|
334
|
+
this.socket.send(JSON.stringify(message));
|
|
335
|
+
}
|
|
336
|
+
reconnect() {
|
|
337
|
+
if (this.destroyed) return;
|
|
338
|
+
this.retryCount++;
|
|
339
|
+
if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
|
|
340
|
+
this.log("Max retries reached, giving up");
|
|
341
|
+
const unspoken = this.isProcessing ? this.textSent + this.textBuffer : "";
|
|
342
|
+
this.host.failed(unspoken.trim() ? [unspoken] : []);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
this.initPromise = new Promise((resolve) => {
|
|
346
|
+
this.log("Reconnecting...");
|
|
347
|
+
this.reconnectTimeout = setTimeout(() => {
|
|
348
|
+
this.reconnectTimeout = void 0;
|
|
349
|
+
this.initWS().then(() => {
|
|
350
|
+
if (this.isProcessing && this.textSent.trim()) {
|
|
351
|
+
this.log("Sending text again");
|
|
352
|
+
const text = this.textSent;
|
|
353
|
+
this.textSent = "";
|
|
354
|
+
this.unflushed = false;
|
|
355
|
+
this.pendingFlushes = 0;
|
|
356
|
+
this.sendText(text);
|
|
357
|
+
if (this.textEnded) this.flush();
|
|
358
|
+
}
|
|
359
|
+
}).then(resolve).catch((error) => {
|
|
360
|
+
this.log("Reconnection error:", error);
|
|
361
|
+
this.reconnect();
|
|
362
|
+
});
|
|
363
|
+
}, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
log(...message) {
|
|
367
|
+
this.host.log(...message);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
// src/SpeechWebSocket.ts
|
|
372
|
+
var import_ws3 = __toESM(require("ws"));
|
|
373
|
+
var WS_INACTIVITY_TIMEOUT = 180;
|
|
374
|
+
var DEFAULT_CONNECTION_TIMEOUT2 = 5e3;
|
|
375
|
+
var DEFAULT_RETRY_DELAY2 = 1e3;
|
|
376
|
+
var DEFAULT_MAX_RETRY2 = 3;
|
|
377
|
+
var SpeechWebSocket = class {
|
|
378
|
+
constructor(options, host) {
|
|
55
379
|
this.options = options;
|
|
380
|
+
this.host = host;
|
|
56
381
|
this.isProcessing = false;
|
|
57
382
|
this.textEnded = false;
|
|
58
383
|
// Whether the text stream has ended
|
|
@@ -62,20 +387,33 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
62
387
|
// Buffer of text to send to ElevenLabs
|
|
63
388
|
this.receivedAudioText = "";
|
|
64
389
|
this.retryCount = 0;
|
|
390
|
+
this.destroyed = false;
|
|
65
391
|
this.canceled = false;
|
|
392
|
+
// Bumped by every speak() and every cancel(), so a stream that starts late
|
|
393
|
+
// can tell whether it is still the one that should be heard.
|
|
394
|
+
this.generation = 0;
|
|
66
395
|
this.initPromise = this.initWS().catch((error) => {
|
|
67
396
|
console.error("[ElevenLabsTTS] Connection error:", error);
|
|
68
397
|
this.reconnect();
|
|
69
398
|
});
|
|
70
399
|
}
|
|
71
400
|
speak(textStream) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
401
|
+
const generation = ++this.generation;
|
|
402
|
+
let started = false;
|
|
403
|
+
const start = () => {
|
|
404
|
+
if (started) return true;
|
|
405
|
+
if (this.generation !== generation) return false;
|
|
406
|
+
started = true;
|
|
407
|
+
this.canceled = false;
|
|
408
|
+
this.isProcessing = true;
|
|
409
|
+
this.textEnded = false;
|
|
410
|
+
this.textBuffer = "";
|
|
411
|
+
this.textSent = "";
|
|
412
|
+
this.receivedAudioText = "";
|
|
413
|
+
return true;
|
|
414
|
+
};
|
|
78
415
|
textStream.on("data", async (chunk) => {
|
|
416
|
+
if (!start()) return;
|
|
79
417
|
if (this.canceled) return;
|
|
80
418
|
const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
|
|
81
419
|
this.textSent += text;
|
|
@@ -89,11 +427,12 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
89
427
|
}
|
|
90
428
|
});
|
|
91
429
|
textStream.on("error", (error) => {
|
|
430
|
+
if (!started) return;
|
|
92
431
|
this.log("Error in text stream, ending audio stream", error);
|
|
93
432
|
this.isProcessing = false;
|
|
94
433
|
});
|
|
95
434
|
textStream.on("end", async () => {
|
|
96
|
-
if (this.canceled) return;
|
|
435
|
+
if (!started || this.canceled) return;
|
|
97
436
|
await this.initPromise;
|
|
98
437
|
if (this.textBuffer.trim()) {
|
|
99
438
|
this.sendTranscript(this.textBuffer + " ");
|
|
@@ -105,32 +444,34 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
105
444
|
});
|
|
106
445
|
}
|
|
107
446
|
cancel() {
|
|
447
|
+
this.generation++;
|
|
108
448
|
if (!this.isProcessing) return;
|
|
109
449
|
this.log("Cancel");
|
|
110
450
|
this.canceled = true;
|
|
111
451
|
this.textSent = "";
|
|
112
452
|
this.receivedAudioText = "";
|
|
113
453
|
this.isProcessing = false;
|
|
114
|
-
if (this.socket?.readyState ===
|
|
454
|
+
if (this.socket?.readyState === import_ws3.default.OPEN) {
|
|
115
455
|
this.socket.send(JSON.stringify({ text: " ", flush: true }));
|
|
116
456
|
}
|
|
117
457
|
}
|
|
118
458
|
destroy() {
|
|
459
|
+
this.destroyed = true;
|
|
119
460
|
if (this.reconnectTimeout) {
|
|
120
461
|
clearTimeout(this.reconnectTimeout);
|
|
121
462
|
this.reconnectTimeout = void 0;
|
|
122
463
|
}
|
|
464
|
+
if (this.connectionTimeout) {
|
|
465
|
+
clearTimeout(this.connectionTimeout);
|
|
466
|
+
this.connectionTimeout = void 0;
|
|
467
|
+
}
|
|
123
468
|
if (this.keepAliveInterval) {
|
|
124
469
|
clearInterval(this.keepAliveInterval);
|
|
125
470
|
this.keepAliveInterval = void 0;
|
|
126
471
|
}
|
|
127
|
-
this.socket
|
|
128
|
-
if (this.socket?.readyState === import_ws.default.OPEN) {
|
|
129
|
-
this.socket?.close(1e3);
|
|
130
|
-
}
|
|
472
|
+
if (this.socket) closeSocket(this.socket);
|
|
131
473
|
this.socket = void 0;
|
|
132
474
|
this.isProcessing = false;
|
|
133
|
-
super.destroy();
|
|
134
475
|
}
|
|
135
476
|
initWS() {
|
|
136
477
|
return new Promise((resolve, reject) => {
|
|
@@ -141,15 +482,15 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
141
482
|
this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT
|
|
142
483
|
);
|
|
143
484
|
params.append("inactivity_timeout", WS_INACTIVITY_TIMEOUT.toString());
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
JSON.stringify(
|
|
147
|
-
|
|
485
|
+
const voiceSettings = toWireVoiceSettings(this.options.voiceSettings);
|
|
486
|
+
if (voiceSettings) {
|
|
487
|
+
params.append("voice_settings", JSON.stringify(voiceSettings));
|
|
488
|
+
}
|
|
148
489
|
if (this.options.language) {
|
|
149
490
|
params.append("language_code", this.options.language);
|
|
150
491
|
}
|
|
151
492
|
const uri = `wss://api.elevenlabs.io/v1/text-to-speech/${this.options.voiceId}/stream-input?${params.toString()}`;
|
|
152
|
-
const socket = new
|
|
493
|
+
const socket = new import_ws3.default(uri, {
|
|
153
494
|
headers: {
|
|
154
495
|
"xi-api-key": this.options.apiKey
|
|
155
496
|
}
|
|
@@ -157,25 +498,26 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
157
498
|
this.socket = socket;
|
|
158
499
|
const timeout = setTimeout(() => {
|
|
159
500
|
this.log("Connection timeout");
|
|
160
|
-
socket
|
|
161
|
-
socket.close();
|
|
501
|
+
closeSocket(socket);
|
|
162
502
|
this.socket = void 0;
|
|
163
503
|
reject(new Error("WebSocket connection timeout"));
|
|
164
|
-
}, this.options.connectionTimeout ??
|
|
504
|
+
}, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT2);
|
|
505
|
+
this.connectionTimeout = timeout;
|
|
165
506
|
socket.addEventListener("open", () => {
|
|
166
507
|
clearTimeout(timeout);
|
|
167
508
|
this.log("Connection opened");
|
|
168
509
|
this.socket?.send(
|
|
169
510
|
JSON.stringify({
|
|
170
511
|
text: " ",
|
|
171
|
-
voice_settings:
|
|
512
|
+
voice_settings: voiceSettings
|
|
172
513
|
})
|
|
173
514
|
);
|
|
174
515
|
this.keepAliveInterval = setInterval(
|
|
175
516
|
() => {
|
|
176
|
-
if (this.socket?.readyState ===
|
|
517
|
+
if (this.socket?.readyState === import_ws3.default.OPEN) {
|
|
177
518
|
this.log("Sending keep-alive message");
|
|
178
519
|
this.socket.send(JSON.stringify({ text: " " }));
|
|
520
|
+
this.retryCount = 0;
|
|
179
521
|
}
|
|
180
522
|
},
|
|
181
523
|
(WS_INACTIVITY_TIMEOUT - 1) * 1e3
|
|
@@ -220,11 +562,12 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
220
562
|
this.textSent = "";
|
|
221
563
|
}
|
|
222
564
|
if ("error" in message) {
|
|
223
|
-
|
|
565
|
+
console.error("[ElevenLabsTTS] Error:", message.error, message.message);
|
|
224
566
|
}
|
|
225
567
|
}
|
|
226
568
|
processAudioMessage(message) {
|
|
227
569
|
const chunk = Buffer.from(message.audio, "base64");
|
|
570
|
+
this.retryCount = 0;
|
|
228
571
|
if (message.alignment) {
|
|
229
572
|
const chunkText = message.alignment.chars.join("");
|
|
230
573
|
const receivedAudioText = this.receivedAudioText + chunkText;
|
|
@@ -236,7 +579,7 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
236
579
|
this.receivedAudioText = receivedAudioText;
|
|
237
580
|
this.fixIsFinal(message);
|
|
238
581
|
}
|
|
239
|
-
this.
|
|
582
|
+
this.host.audio(chunk);
|
|
240
583
|
}
|
|
241
584
|
fixIsFinal(message) {
|
|
242
585
|
if (message.isFinal || !this.textEnded) return;
|
|
@@ -249,10 +592,11 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
249
592
|
this.log(`Sent transcript: "${text}"`);
|
|
250
593
|
}
|
|
251
594
|
reconnect() {
|
|
595
|
+
if (this.destroyed) return;
|
|
252
596
|
this.retryCount++;
|
|
253
|
-
if (this.retryCount > (this.options.maxRetry ??
|
|
597
|
+
if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY2)) {
|
|
254
598
|
this.log("Max retries reached, giving up");
|
|
255
|
-
this.
|
|
599
|
+
this.host.failed(this.textSent.trim() ? [this.textSent] : []);
|
|
256
600
|
return;
|
|
257
601
|
}
|
|
258
602
|
this.initPromise = new Promise((resolve) => {
|
|
@@ -260,7 +604,6 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
260
604
|
this.reconnectTimeout = setTimeout(() => {
|
|
261
605
|
this.reconnectTimeout = void 0;
|
|
262
606
|
this.initWS().then(() => {
|
|
263
|
-
this.retryCount = 0;
|
|
264
607
|
if (this.textSent.length > 0) {
|
|
265
608
|
this.log("Sending text chunks again");
|
|
266
609
|
this.sendTranscript(this.textSent);
|
|
@@ -269,14 +612,50 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
269
612
|
this.log("Reconnection error:", error);
|
|
270
613
|
this.reconnect();
|
|
271
614
|
});
|
|
272
|
-
}, this.options.retryDelay ??
|
|
615
|
+
}, this.options.retryDelay ?? DEFAULT_RETRY_DELAY2);
|
|
273
616
|
});
|
|
274
617
|
}
|
|
618
|
+
log(...message) {
|
|
619
|
+
this.host.log(...message);
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
function toWireVoiceSettings(settings) {
|
|
623
|
+
if (!settings) return void 0;
|
|
624
|
+
const { similarityBoost, useSpeakerBoost, ...rest } = settings;
|
|
625
|
+
return {
|
|
626
|
+
similarity_boost: similarityBoost,
|
|
627
|
+
use_speaker_boost: useSpeakerBoost,
|
|
628
|
+
...rest
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// src/ElevenLabsTTS.ts
|
|
633
|
+
var ElevenLabsTTS = class extends import_server.TTS {
|
|
634
|
+
constructor(options) {
|
|
635
|
+
super();
|
|
636
|
+
const host = {
|
|
637
|
+
audio: (chunk) => this.emit("Audio", chunk),
|
|
638
|
+
failed: (texts) => this.emit("Failed", texts),
|
|
639
|
+
log: (...message) => this.log(...message)
|
|
640
|
+
};
|
|
641
|
+
this.webSocket = isDialogueModel(options.modelId ?? DEFAULT_MODEL_ID) ? new DialogueWebSocket(options, host) : new SpeechWebSocket(options, host);
|
|
642
|
+
}
|
|
643
|
+
speak(textStream) {
|
|
644
|
+
this.webSocket.speak(textStream);
|
|
645
|
+
}
|
|
646
|
+
cancel() {
|
|
647
|
+
this.webSocket.cancel();
|
|
648
|
+
}
|
|
649
|
+
destroy() {
|
|
650
|
+
this.webSocket.destroy();
|
|
651
|
+
super.destroy();
|
|
652
|
+
}
|
|
275
653
|
};
|
|
276
654
|
// Annotate the CommonJS export names for ESM import in node:
|
|
277
655
|
0 && (module.exports = {
|
|
278
656
|
DEFAULT_MODEL_ID,
|
|
279
657
|
DEFAULT_OUTPUT_FORMAT,
|
|
280
|
-
ElevenLabsTTS
|
|
658
|
+
ElevenLabsTTS,
|
|
659
|
+
isDialogueModel
|
|
281
660
|
});
|
|
282
661
|
//# sourceMappingURL=index.js.map
|