@micdrop/elevenlabs 1.2.1 → 1.4.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/dist/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/index.ts
@@ -32,61 +22,141 @@ var index_exports = {};
32
22
  __export(index_exports, {
33
23
  DEFAULT_MODEL_ID: () => DEFAULT_MODEL_ID,
34
24
  DEFAULT_OUTPUT_FORMAT: () => DEFAULT_OUTPUT_FORMAT,
25
+ DEFAULT_SAMPLE_RATE: () => DEFAULT_SAMPLE_RATE,
35
26
  ElevenLabsTTS: () => ElevenLabsTTS,
27
+ getOutputFormat: () => getOutputFormat,
28
+ getSampleRate: () => getSampleRate,
36
29
  isDialogueModel: () => isDialogueModel
37
30
  });
38
31
  module.exports = __toCommonJS(index_exports);
39
32
 
40
33
  // src/ElevenLabsTTS.ts
41
- var import_server = require("@micdrop/server");
42
-
43
- // src/DialogueWebSocket.ts
44
- var import_ws2 = __toESM(require("ws"));
34
+ var import_server2 = require("@micdrop/server");
45
35
 
46
36
  // src/ElevenLabsWebSocket.ts
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);
37
+ var import_server = require("@micdrop/server");
38
+ var DEFAULT_MAX_RETRY = 3;
39
+ var ElevenLabsConnection = class {
40
+ constructor(options) {
41
+ this.options = options;
42
+ // Whether the current connection carried audio or lived long enough for a
43
+ // keep-alive, which means the server accepted the key and the voice
44
+ this.proven = false;
45
+ this.failures = 0;
46
+ this.socket = this.createSocket();
54
47
  }
55
- }
48
+ /** Opens the connection, again after giving up, and does nothing otherwise */
49
+ connect() {
50
+ this.socket.connect();
51
+ }
52
+ /**
53
+ * Resolves to false when the connection gives up or is destroyed meanwhile,
54
+ * and right away once it gave up, until the next `connect()`: the stream
55
+ * that was speaking then has nothing left to wait for.
56
+ */
57
+ ready() {
58
+ if (this.socket.status === "idle") return Promise.resolve(false);
59
+ return this.socket.ready();
60
+ }
61
+ send(message) {
62
+ return this.socket.send(JSON.stringify(message));
63
+ }
64
+ /** Drops the connection, with the synthesis running on it, for a new one */
65
+ restart() {
66
+ this.socket.restart();
67
+ }
68
+ destroy() {
69
+ this.socket.destroy();
70
+ }
71
+ /** The current connection works, so its next drop is not a failure */
72
+ prove() {
73
+ this.proven = true;
74
+ this.failures = 0;
75
+ }
76
+ createSocket() {
77
+ const { tts, keepAlive, host } = this.options;
78
+ const socket = new import_server.ReconnectingSocket({
79
+ url: this.options.url,
80
+ clientOptions: { headers: { "xi-api-key": tts.apiKey } },
81
+ connectionTimeout: tts.connectionTimeout,
82
+ retryDelay: tts.retryDelay,
83
+ maxRetry: tts.maxRetry,
84
+ handshake: {
85
+ start: (ws) => ws.send(JSON.stringify(this.options.init))
86
+ },
87
+ keepAlive: {
88
+ interval: keepAlive.interval,
89
+ message: () => {
90
+ this.prove();
91
+ return JSON.stringify(keepAlive.message);
92
+ }
93
+ },
94
+ log: (...message) => host.log(...message)
95
+ });
96
+ socket.on("Open", ({ reconnected }) => {
97
+ this.proven = false;
98
+ this.options.onOpen(reconnected);
99
+ });
100
+ socket.on("Message", (data) => {
101
+ try {
102
+ this.options.onMessage(JSON.parse(data.toString()));
103
+ } catch (error) {
104
+ host.log("message" in error ? error.message : error);
105
+ host.log("Event data during error:", data.toString());
106
+ }
107
+ });
108
+ socket.on("Close", () => {
109
+ if (this.proven) return;
110
+ this.failures++;
111
+ if (this.failures > (tts.maxRetry ?? DEFAULT_MAX_RETRY)) this.giveUp();
112
+ });
113
+ socket.on("Failed", () => this.giveUp());
114
+ return socket;
115
+ }
116
+ giveUp() {
117
+ this.options.host.log("Max retries reached, giving up");
118
+ this.failures = 0;
119
+ this.socket.destroy();
120
+ this.socket = this.createSocket();
121
+ this.options.onFailed();
122
+ }
123
+ };
56
124
 
57
125
  // src/types.ts
58
126
  var DEFAULT_MODEL_ID = "eleven_turbo_v2_5";
59
- var DEFAULT_OUTPUT_FORMAT = "pcm_16000";
127
+ var DEFAULT_SAMPLE_RATE = 48e3;
128
+ var DEFAULT_OUTPUT_FORMAT = "pcm_48000";
129
+ function getOutputFormat(options) {
130
+ if (options.sampleRate) return `pcm_${options.sampleRate}`;
131
+ return options.outputFormat ?? `pcm_${DEFAULT_SAMPLE_RATE}`;
132
+ }
133
+ function getSampleRate(options) {
134
+ const match = /^pcm_(\d+)$/.exec(getOutputFormat(options));
135
+ return match ? Number(match[1]) : DEFAULT_SAMPLE_RATE;
136
+ }
60
137
  function isDialogueModel(modelId) {
61
138
  return modelId.startsWith("eleven_v3") || modelId.startsWith("eleven_v4");
62
139
  }
63
140
 
64
141
  // src/DialogueWebSocket.ts
65
142
  var KEEP_ALIVE_INTERVAL = 15e3;
66
- var DEFAULT_CONNECTION_TIMEOUT = 5e3;
67
- var DEFAULT_RETRY_DELAY = 1e3;
68
- var DEFAULT_MAX_RETRY = 3;
69
143
  var SENTENCE_END = /(?:[!?]+|(?<!\.)\.(?!\.))\s+/g;
70
144
  var DialogueWebSocket = class {
71
145
  // Whether the connection received any text
72
146
  constructor(options, host) {
73
147
  this.options = options;
74
148
  this.host = host;
75
- this.retryCount = 0;
76
- this.destroyed = false;
149
+ this.state = "idle";
77
150
  // Bumped by every speak() and every cancel(), so a stream that starts late
78
151
  // can tell whether it is still the one that should be heard.
79
152
  this.generation = 0;
80
153
  // Generation of the stream being spoken. A speak() that never carries a
81
154
  // word leaves it in place, so the sentence it sends goes on to its end.
82
155
  this.speaking = 0;
83
- this.isProcessing = false;
84
- this.textEnded = false;
85
- // Whether the text stream has ended
86
156
  this.textSent = "";
87
157
  // Text of the utterance sent to ElevenLabs
88
158
  this.textBuffer = "";
89
- // End of the last chunk, held until its word is complete
159
+ // Text not sent yet, held until its word is complete
90
160
  this.unflushed = false;
91
161
  // Whether text was sent since the last flush
92
162
  this.pendingFlushes = 0;
@@ -94,7 +164,31 @@ var DialogueWebSocket = class {
94
164
  this.newTurn = false;
95
165
  // Whether the next input starts a new utterance
96
166
  this.hasSpoken = false;
97
- this.connect();
167
+ const params = new URLSearchParams();
168
+ params.append("model_id", this.options.modelId ?? DEFAULT_MODEL_ID);
169
+ params.append("output_format", getOutputFormat(this.options));
170
+ if (this.options.language) {
171
+ params.append("language_code", this.options.language);
172
+ }
173
+ const stability = this.options.voiceSettings?.stability;
174
+ const init = {
175
+ voices: [this.options.voiceId],
176
+ voice_settings: stability === void 0 ? void 0 : { stability }
177
+ };
178
+ this.connection = new ElevenLabsConnection({
179
+ url: `wss://api.elevenlabs.io/v1/text-to-dialogue/stream-input?${params.toString()}`,
180
+ tts: options,
181
+ init,
182
+ keepAlive: {
183
+ interval: KEEP_ALIVE_INTERVAL,
184
+ message: { keep_alive: true }
185
+ },
186
+ host,
187
+ onOpen: () => this.onOpen(),
188
+ onMessage: (message) => this.onMessage(message),
189
+ onFailed: () => this.onFailed()
190
+ });
191
+ this.connection.connect();
98
192
  }
99
193
  speak(textStream) {
100
194
  const generation = ++this.generation;
@@ -103,171 +197,100 @@ var DialogueWebSocket = class {
103
197
  if (started) return this.speaking === generation;
104
198
  if (this.generation !== generation) return false;
105
199
  started = true;
106
- if (this.isProcessing) this.restart();
200
+ if (this.state !== "idle") this.connection.restart();
201
+ else this.connection.connect();
202
+ this.resetUtterance();
107
203
  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;
204
+ this.state = "streaming";
114
205
  this.newTurn = true;
115
206
  return true;
116
207
  };
117
208
  textStream.on("data", async (chunk) => {
118
209
  if (!start()) return;
119
- const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
120
- await this.initPromise;
210
+ this.textBuffer += chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
211
+ if (!await this.connection.ready()) return;
121
212
  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
- }
213
+ const spaceIndex = this.textBuffer.lastIndexOf(" ");
214
+ if (spaceIndex === -1) return;
215
+ this.sendText(this.textBuffer.slice(0, spaceIndex + 1));
216
+ this.textBuffer = this.textBuffer.slice(spaceIndex + 1);
129
217
  });
130
218
  textStream.on("error", (error) => {
131
219
  if (!started || this.speaking !== generation) return;
132
220
  this.log("Error in text stream, ending audio stream", error);
133
- this.isProcessing = false;
221
+ this.speaking = 0;
222
+ this.textBuffer = "";
223
+ this.endText();
134
224
  });
135
225
  textStream.on("end", async () => {
136
226
  if (!started) return;
137
- await this.initPromise;
227
+ if (!await this.connection.ready()) return;
138
228
  if (this.speaking !== generation) return;
139
229
  if (this.textBuffer.trim()) {
140
230
  this.sendText(this.textBuffer + " ");
141
- this.textBuffer = "";
142
231
  }
143
- this.textEnded = true;
144
- this.flush();
145
- this.checkEnded();
232
+ this.textBuffer = "";
233
+ this.endText();
146
234
  });
147
235
  }
148
236
  cancel() {
149
237
  this.generation++;
150
238
  this.speaking = 0;
151
- if (!this.isProcessing) return;
239
+ if (this.state === "idle") return;
152
240
  this.log("Cancel");
153
- this.isProcessing = false;
154
- this.textSent = "";
155
- this.textBuffer = "";
156
- this.restart();
241
+ this.resetUtterance();
242
+ this.connection.restart();
157
243
  }
158
244
  destroy() {
159
- this.destroyed = true;
160
- if (this.reconnectTimeout) {
161
- clearTimeout(this.reconnectTimeout);
162
- this.reconnectTimeout = void 0;
245
+ this.generation++;
246
+ this.speaking = 0;
247
+ this.resetUtterance();
248
+ this.connection.destroy();
249
+ }
250
+ onOpen() {
251
+ this.hasSpoken = false;
252
+ if (this.state === "idle") return;
253
+ const text = this.textSent;
254
+ this.textSent = "";
255
+ this.unflushed = false;
256
+ this.pendingFlushes = 0;
257
+ if (text.trim()) {
258
+ this.log("Sending text again");
259
+ this.sendText(text);
163
260
  }
164
- this.dropSocket();
165
- this.isProcessing = false;
261
+ if (this.state === "draining") this.flush();
262
+ this.checkEnded();
166
263
  }
167
- connect() {
168
- this.initPromise = this.initWS().catch((error) => {
169
- console.error("[ElevenLabsTTS] Connection error:", error);
170
- this.reconnect();
171
- });
264
+ /** Every retry failed: hands the unspoken text over and drops the utterance */
265
+ onFailed() {
266
+ const unspoken = this.state === "idle" ? "" : this.textSent + this.textBuffer;
267
+ this.speaking = 0;
268
+ this.resetUtterance();
269
+ this.host.failed(unspoken.trim() ? [unspoken] : []);
172
270
  }
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
- });
271
+ resetUtterance() {
272
+ this.state = "idle";
273
+ this.textSent = "";
274
+ this.textBuffer = "";
275
+ this.unflushed = false;
276
+ this.pendingFlushes = 0;
277
+ }
278
+ /** The text stream is over: flushes what the server holds */
279
+ endText() {
280
+ this.state = "draining";
281
+ this.flush();
282
+ this.checkEnded();
264
283
  }
265
284
  onMessage(message) {
266
285
  if ("audio" in message && message.audio) {
267
286
  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);
287
+ this.connection.prove();
288
+ if (this.state === "idle") {
289
+ this.log("Ignore audio chunk, nothing is being spoken");
290
+ } else {
291
+ this.log(`Received audio chunk (${chunk.length} bytes)`);
292
+ this.host.audio(chunk);
293
+ }
271
294
  }
272
295
  if ("is_final_audio_for_turn" in message && message.is_final_audio_for_turn) {
273
296
  this.pendingFlushes = Math.max(0, this.pendingFlushes - 1);
@@ -323,45 +346,15 @@ var DialogueWebSocket = class {
323
346
  }
324
347
  /** Ends the utterance once its text is over and all its audio came */
325
348
  checkEnded() {
326
- if (!this.isProcessing || !this.textEnded) return;
349
+ if (this.state !== "draining") return;
327
350
  if (this.unflushed || this.pendingFlushes > 0) return;
328
351
  this.log("Audio ended");
329
- this.isProcessing = false;
330
- this.textSent = "";
352
+ this.resetUtterance();
331
353
  }
354
+ // Lost when the connection just dropped, which is fine: the reconnection
355
+ // sends the utterance again from textSent
332
356
  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
- });
357
+ this.connection.send(message);
365
358
  }
366
359
  log(...message) {
367
360
  this.host.log(...message);
@@ -369,197 +362,150 @@ var DialogueWebSocket = class {
369
362
  };
370
363
 
371
364
  // src/SpeechWebSocket.ts
372
- var import_ws3 = __toESM(require("ws"));
373
365
  var WS_INACTIVITY_TIMEOUT = 180;
374
- var DEFAULT_CONNECTION_TIMEOUT2 = 5e3;
375
- var DEFAULT_RETRY_DELAY2 = 1e3;
376
- var DEFAULT_MAX_RETRY2 = 3;
377
366
  var SpeechWebSocket = class {
367
+ // Index in textSent where the audio received ends
378
368
  constructor(options, host) {
379
369
  this.options = options;
380
370
  this.host = host;
381
- this.isProcessing = false;
382
- this.textEnded = false;
383
- // Whether the text stream has ended
384
- this.textSent = "";
385
- // Text sent to ElevenLabs
386
- this.textBuffer = "";
387
- // Buffer of text to send to ElevenLabs
388
- this.receivedAudioText = "";
389
- this.retryCount = 0;
390
- this.destroyed = false;
391
- this.canceled = false;
371
+ this.state = "idle";
392
372
  // Bumped by every speak() and every cancel(), so a stream that starts late
393
373
  // can tell whether it is still the one that should be heard.
394
374
  this.generation = 0;
395
- this.initPromise = this.initWS().catch((error) => {
396
- console.error("[ElevenLabsTTS] Connection error:", error);
397
- this.reconnect();
375
+ // Generation of the stream being spoken. A speak() that never carries a
376
+ // word leaves it in place, so the sentence it sends goes on to its end.
377
+ this.speaking = 0;
378
+ this.textSent = "";
379
+ // Text of the utterance sent to ElevenLabs
380
+ this.textBuffer = "";
381
+ // Text not sent yet, held until its word is complete
382
+ this.heardUntil = 0;
383
+ const params = new URLSearchParams();
384
+ params.append("model_id", this.options.modelId ?? DEFAULT_MODEL_ID);
385
+ params.append("output_format", getOutputFormat(this.options));
386
+ params.append("inactivity_timeout", WS_INACTIVITY_TIMEOUT.toString());
387
+ const voiceSettings = toWireVoiceSettings(this.options.voiceSettings);
388
+ if (voiceSettings) {
389
+ params.append("voice_settings", JSON.stringify(voiceSettings));
390
+ }
391
+ if (this.options.language) {
392
+ params.append("language_code", this.options.language);
393
+ }
394
+ this.connection = new ElevenLabsConnection({
395
+ url: `wss://api.elevenlabs.io/v1/text-to-speech/${this.options.voiceId}/stream-input?${params.toString()}`,
396
+ tts: options,
397
+ // Initialization message with voice settings
398
+ init: { text: " ", voice_settings: voiceSettings },
399
+ keepAlive: {
400
+ interval: (WS_INACTIVITY_TIMEOUT - 1) * 1e3,
401
+ message: { text: " " }
402
+ },
403
+ host,
404
+ onOpen: () => this.resume(),
405
+ onMessage: (message) => this.onMessage(message),
406
+ onFailed: () => this.onFailed()
398
407
  });
408
+ this.connection.connect();
399
409
  }
400
410
  speak(textStream) {
401
411
  const generation = ++this.generation;
402
412
  let started = false;
403
413
  const start = () => {
404
- if (started) return true;
414
+ if (started) return this.speaking === generation;
405
415
  if (this.generation !== generation) return false;
406
416
  started = true;
407
- this.canceled = false;
408
- this.isProcessing = true;
409
- this.textEnded = false;
410
- this.textBuffer = "";
411
- this.textSent = "";
412
- this.receivedAudioText = "";
417
+ if (this.state !== "idle") this.connection.restart();
418
+ else this.connection.connect();
419
+ this.resetUtterance();
420
+ this.speaking = generation;
421
+ this.state = "speaking";
413
422
  return true;
414
423
  };
415
424
  textStream.on("data", async (chunk) => {
416
425
  if (!start()) return;
417
- if (this.canceled) return;
418
- const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
419
- this.textSent += text;
420
- await this.initPromise;
421
- const spaceIndex = text.lastIndexOf(" ");
422
- if (spaceIndex === -1) {
423
- this.textBuffer += text;
424
- } else {
425
- this.sendTranscript(this.textBuffer + text.slice(0, spaceIndex + 1));
426
- this.textBuffer = text.slice(spaceIndex + 1);
427
- }
426
+ this.textBuffer += chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
427
+ if (!await this.connection.ready()) return;
428
+ if (this.speaking !== generation) return;
429
+ const spaceIndex = this.textBuffer.lastIndexOf(" ");
430
+ if (spaceIndex === -1) return;
431
+ this.sendTranscript(this.textBuffer.slice(0, spaceIndex + 1));
432
+ this.textBuffer = this.textBuffer.slice(spaceIndex + 1);
428
433
  });
429
434
  textStream.on("error", (error) => {
430
- if (!started) return;
435
+ if (!started || this.speaking !== generation) return;
431
436
  this.log("Error in text stream, ending audio stream", error);
432
- this.isProcessing = false;
437
+ this.speaking = 0;
438
+ this.textBuffer = "";
439
+ this.flush();
433
440
  });
434
441
  textStream.on("end", async () => {
435
- if (!started || this.canceled) return;
436
- await this.initPromise;
442
+ if (!started) return;
443
+ if (!await this.connection.ready()) return;
444
+ if (this.speaking !== generation) return;
437
445
  if (this.textBuffer.trim()) {
438
446
  this.sendTranscript(this.textBuffer + " ");
439
- this.textBuffer = "";
440
447
  }
441
- this.textEnded = true;
442
- this.socket?.send(JSON.stringify({ text: " ", flush: true }));
443
- this.log("Flushed text");
448
+ this.textBuffer = "";
449
+ this.flush();
444
450
  });
445
451
  }
446
452
  cancel() {
447
453
  this.generation++;
448
- if (!this.isProcessing) return;
454
+ this.speaking = 0;
455
+ if (this.state === "idle") return;
449
456
  this.log("Cancel");
450
- this.canceled = true;
451
- this.textSent = "";
452
- this.receivedAudioText = "";
453
- this.isProcessing = false;
454
- if (this.socket?.readyState === import_ws3.default.OPEN) {
455
- this.socket.send(JSON.stringify({ text: " ", flush: true }));
456
- }
457
+ this.resetUtterance();
458
+ this.connection.restart();
457
459
  }
458
460
  destroy() {
459
- this.destroyed = true;
460
- if (this.reconnectTimeout) {
461
- clearTimeout(this.reconnectTimeout);
462
- this.reconnectTimeout = void 0;
463
- }
464
- if (this.connectionTimeout) {
465
- clearTimeout(this.connectionTimeout);
466
- this.connectionTimeout = void 0;
467
- }
468
- if (this.keepAliveInterval) {
469
- clearInterval(this.keepAliveInterval);
470
- this.keepAliveInterval = void 0;
461
+ this.generation++;
462
+ this.speaking = 0;
463
+ this.resetUtterance();
464
+ this.connection.destroy();
465
+ }
466
+ /** Flushes buffered text and marks the end of the utterance */
467
+ flush() {
468
+ this.state = "flushed";
469
+ this.connection.send({ text: " ", flush: true });
470
+ this.log("Flushed text");
471
+ this.checkEnded();
472
+ }
473
+ /**
474
+ * Sends the text of the utterance in flight again on a new connection. The
475
+ * server lost what it was generating, so it goes on from the audio received.
476
+ */
477
+ resume() {
478
+ if (this.state === "idle") return;
479
+ const wordStart = this.textSent.lastIndexOf(" ", this.heardUntil - 1) + 1;
480
+ const unheard = this.textSent.slice(wordStart);
481
+ this.textSent = "";
482
+ this.heardUntil = 0;
483
+ if (unheard.trim()) {
484
+ this.log("Sending text chunks again");
485
+ this.sendTranscript(unheard);
471
486
  }
472
- if (this.socket) closeSocket(this.socket);
473
- this.socket = void 0;
474
- this.isProcessing = false;
475
- }
476
- initWS() {
477
- return new Promise((resolve, reject) => {
478
- const params = new URLSearchParams();
479
- params.append("model_id", this.options.modelId ?? DEFAULT_MODEL_ID);
480
- params.append(
481
- "output_format",
482
- this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT
483
- );
484
- params.append("inactivity_timeout", WS_INACTIVITY_TIMEOUT.toString());
485
- const voiceSettings = toWireVoiceSettings(this.options.voiceSettings);
486
- if (voiceSettings) {
487
- params.append("voice_settings", JSON.stringify(voiceSettings));
488
- }
489
- if (this.options.language) {
490
- params.append("language_code", this.options.language);
491
- }
492
- const uri = `wss://api.elevenlabs.io/v1/text-to-speech/${this.options.voiceId}/stream-input?${params.toString()}`;
493
- const socket = new import_ws3.default(uri, {
494
- headers: {
495
- "xi-api-key": this.options.apiKey
496
- }
497
- });
498
- this.socket = socket;
499
- const timeout = setTimeout(() => {
500
- this.log("Connection timeout");
501
- closeSocket(socket);
502
- this.socket = void 0;
503
- reject(new Error("WebSocket connection timeout"));
504
- }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT2);
505
- this.connectionTimeout = timeout;
506
- socket.addEventListener("open", () => {
507
- clearTimeout(timeout);
508
- this.log("Connection opened");
509
- this.socket?.send(
510
- JSON.stringify({
511
- text: " ",
512
- voice_settings: voiceSettings
513
- })
514
- );
515
- this.keepAliveInterval = setInterval(
516
- () => {
517
- if (this.socket?.readyState === import_ws3.default.OPEN) {
518
- this.log("Sending keep-alive message");
519
- this.socket.send(JSON.stringify({ text: " " }));
520
- this.retryCount = 0;
521
- }
522
- },
523
- (WS_INACTIVITY_TIMEOUT - 1) * 1e3
524
- );
525
- resolve();
526
- });
527
- socket.addEventListener("error", (error) => {
528
- clearTimeout(timeout);
529
- this.log("WebSocket error:", error);
530
- reject(new Error("WebSocket connection error"));
531
- });
532
- socket.addEventListener("message", (event) => {
533
- if (this.canceled) return;
534
- try {
535
- this.onMessage(JSON.parse(event.data.toString()));
536
- } catch (error) {
537
- this.log("message" in error ? error.message : error);
538
- this.log("Event data during error:", event.data);
539
- }
540
- });
541
- socket.addEventListener("close", ({ code, reason }) => {
542
- clearTimeout(timeout);
543
- this.socket?.removeAllListeners();
544
- this.socket = void 0;
545
- this.isProcessing = false;
546
- if (this.keepAliveInterval) {
547
- clearInterval(this.keepAliveInterval);
548
- this.keepAliveInterval = void 0;
549
- }
550
- this.log("Connection closed", { code, reason });
551
- this.reconnect();
552
- });
553
- });
487
+ if (this.state === "flushed") this.flush();
488
+ }
489
+ /** Every retry failed: hands the unspoken text over and drops the utterance */
490
+ onFailed() {
491
+ const unspoken = this.state === "idle" ? "" : this.textSent.slice(this.heardUntil) + this.textBuffer;
492
+ this.speaking = 0;
493
+ this.resetUtterance();
494
+ this.host.failed(unspoken.trim() ? [unspoken] : []);
495
+ }
496
+ resetUtterance() {
497
+ this.state = "idle";
498
+ this.textSent = "";
499
+ this.textBuffer = "";
500
+ this.heardUntil = 0;
554
501
  }
555
502
  onMessage(message) {
556
503
  if ("audio" in message && message.audio) {
557
504
  this.processAudioMessage(message);
558
505
  }
559
- if ("isFinal" in message && message.isFinal) {
506
+ if ("isFinal" in message && message.isFinal && this.state !== "idle") {
560
507
  this.log("Audio ended");
561
- this.isProcessing = false;
562
- this.textSent = "";
508
+ this.resetUtterance();
563
509
  }
564
510
  if ("error" in message) {
565
511
  console.error("[ElevenLabsTTS] Error:", message.error, message.message);
@@ -567,58 +513,59 @@ var SpeechWebSocket = class {
567
513
  }
568
514
  processAudioMessage(message) {
569
515
  const chunk = Buffer.from(message.audio, "base64");
570
- this.retryCount = 0;
516
+ this.connection.prove();
517
+ if (this.state === "idle") {
518
+ this.log("Ignore audio chunk, nothing is being spoken");
519
+ return;
520
+ }
571
521
  if (message.alignment) {
572
522
  const chunkText = message.alignment.chars.join("");
573
- const receivedAudioText = this.receivedAudioText + chunkText;
574
- if (this.textSent.trim().indexOf(receivedAudioText.trim()) === -1) {
575
- this.log(`Ignore audio chunk ("${chunkText}")`);
576
- return;
523
+ const end = findHeardEnd(this.textSent, this.heardUntil, chunkText);
524
+ if (end === -1) {
525
+ this.log(`Audio chunk off the text sent ("${chunkText}")`);
526
+ } else {
527
+ this.log(`Received audio chunk ("${chunkText}")`);
528
+ this.heardUntil = end;
577
529
  }
578
- this.log(`Received audio chunk ("${chunkText}")`);
579
- this.receivedAudioText = receivedAudioText;
580
- this.fixIsFinal(message);
581
530
  }
582
531
  this.host.audio(chunk);
532
+ this.checkEnded();
583
533
  }
584
- fixIsFinal(message) {
585
- if (message.isFinal || !this.textEnded) return;
586
- if (this.receivedAudioText.trim() === this.textSent.trim()) {
587
- message.isFinal = true;
588
- }
534
+ checkEnded() {
535
+ if (this.state !== "flushed") return;
536
+ if (this.textSent.slice(this.heardUntil).trim()) return;
537
+ this.log("Audio ended");
538
+ this.resetUtterance();
589
539
  }
590
540
  sendTranscript(text) {
591
- this.socket?.send(JSON.stringify({ text, try_trigger_generation: true }));
541
+ this.textSent += text;
542
+ if (!this.connection.send({ text, try_trigger_generation: true })) return;
592
543
  this.log(`Sent transcript: "${text}"`);
593
544
  }
594
- reconnect() {
595
- if (this.destroyed) return;
596
- this.retryCount++;
597
- if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY2)) {
598
- this.log("Max retries reached, giving up");
599
- this.host.failed(this.textSent.trim() ? [this.textSent] : []);
600
- return;
601
- }
602
- this.initPromise = new Promise((resolve) => {
603
- this.log("Reconnecting...");
604
- this.reconnectTimeout = setTimeout(() => {
605
- this.reconnectTimeout = void 0;
606
- this.initWS().then(() => {
607
- if (this.textSent.length > 0) {
608
- this.log("Sending text chunks again");
609
- this.sendTranscript(this.textSent);
610
- }
611
- }).then(resolve).catch((error) => {
612
- this.log("Reconnection error:", error);
613
- this.reconnect();
614
- });
615
- }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY2);
616
- });
617
- }
618
545
  log(...message) {
619
546
  this.host.log(...message);
620
547
  }
621
548
  };
549
+ function findHeardEnd(text, from, heard) {
550
+ for (let start = from; start < text.length; start++) {
551
+ const end = endOfPrefix(text, start, heard);
552
+ if (end !== -1) return end;
553
+ }
554
+ return -1;
555
+ }
556
+ function endOfPrefix(text, from, heard) {
557
+ let index = from;
558
+ for (let i = 0; i < heard.length; i++) {
559
+ if (isSpace(heard[i])) continue;
560
+ while (index < text.length && isSpace(text[index])) index++;
561
+ if (text[index] !== heard[i]) return -1;
562
+ index++;
563
+ }
564
+ return index;
565
+ }
566
+ function isSpace(char) {
567
+ return char === " " || char === " " || char === "\n" || char === "\r";
568
+ }
622
569
  function toWireVoiceSettings(settings) {
623
570
  if (!settings) return void 0;
624
571
  const { similarityBoost, useSpeakerBoost, ...rest } = settings;
@@ -630,9 +577,56 @@ function toWireVoiceSettings(settings) {
630
577
  }
631
578
 
632
579
  // src/ElevenLabsTTS.ts
633
- var ElevenLabsTTS = class extends import_server.TTS {
580
+ var CUSTOM_CATEGORIES = ["cloned", "generated", "professional"];
581
+ var ElevenLabsTTS = class extends import_server2.TTS {
582
+ /**
583
+ * Lists the models that speak, with the languages each one reads.
584
+ *
585
+ * The list is fetched once and shared by every call of the process for a
586
+ * day. `refresh` fetches it again.
587
+ */
588
+ static listModels(options) {
589
+ return (0, import_server2.cachedModels)(
590
+ ["elevenlabs"],
591
+ options.apiKey,
592
+ async () => {
593
+ const response = await fetch("https://api.elevenlabs.io/v1/models", {
594
+ headers: { "xi-api-key": options.apiKey }
595
+ });
596
+ if (!response.ok) {
597
+ throw new Error(`ElevenLabs answered ${response.status}`);
598
+ }
599
+ const models = await response.json();
600
+ return models.filter((model) => model.can_do_text_to_speech).map((model) => ({
601
+ id: model.model_id,
602
+ name: model.name,
603
+ description: model.description,
604
+ languages: model.languages?.map(({ language_id }) => language_id)
605
+ }));
606
+ },
607
+ options.refresh
608
+ );
609
+ }
610
+ /**
611
+ * Lists the voices the account can speak with: the premade ones, and the
612
+ * ones cloned, designed or added from the library.
613
+ *
614
+ * `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.
615
+ *
616
+ * The list is fetched once and shared by every call of the process for a
617
+ * day. `refresh` fetches it again, for a voice added a moment ago.
618
+ */
619
+ static listVoices(options) {
620
+ return (0, import_server2.cachedVoices)(
621
+ ["elevenlabs"],
622
+ options.apiKey,
623
+ () => fetchVoices(options.apiKey),
624
+ options.refresh
625
+ ).then((voices) => (0, import_server2.voicesForLanguage)(voices, options.language));
626
+ }
634
627
  constructor(options) {
635
628
  super();
629
+ this.rate = getSampleRate(options);
636
630
  const host = {
637
631
  audio: (chunk) => this.emit("Audio", chunk),
638
632
  failed: (texts) => this.emit("Failed", texts),
@@ -640,6 +634,9 @@ var ElevenLabsTTS = class extends import_server.TTS {
640
634
  };
641
635
  this.webSocket = isDialogueModel(options.modelId ?? DEFAULT_MODEL_ID) ? new DialogueWebSocket(options, host) : new SpeechWebSocket(options, host);
642
636
  }
637
+ get sampleRate() {
638
+ return this.rate;
639
+ }
643
640
  speak(textStream) {
644
641
  this.webSocket.speak(textStream);
645
642
  }
@@ -651,11 +648,45 @@ var ElevenLabsTTS = class extends import_server.TTS {
651
648
  super.destroy();
652
649
  }
653
650
  };
651
+ async function fetchVoices(apiKey) {
652
+ const voices = [];
653
+ let pageToken;
654
+ do {
655
+ const params = new URLSearchParams({ page_size: "100" });
656
+ if (pageToken) params.set("next_page_token", pageToken);
657
+ const response = await fetch(
658
+ `https://api.elevenlabs.io/v2/voices?${params}`,
659
+ { headers: { "xi-api-key": apiKey } }
660
+ );
661
+ if (!response.ok) throw new Error(`ElevenLabs answered ${response.status}`);
662
+ const page = await response.json();
663
+ voices.push(...page.voices.map(toMicdropVoice));
664
+ pageToken = page.has_more ? page.next_page_token ?? void 0 : void 0;
665
+ } while (pageToken);
666
+ return voices;
667
+ }
668
+ function toMicdropVoice(voice) {
669
+ const verified = (voice.verified_languages ?? []).map(({ locale, language }) => locale || language).filter((code) => !!code);
670
+ const label = voice.labels?.language;
671
+ const languages = verified.length ? verified : label ? [label] : void 0;
672
+ return {
673
+ id: voice.voice_id,
674
+ name: voice.name,
675
+ languages: languages && [...new Set(languages)],
676
+ gender: (0, import_server2.voiceGender)(voice.labels?.gender),
677
+ description: voice.description || void 0,
678
+ previewUrl: voice.preview_url || void 0,
679
+ custom: CUSTOM_CATEGORIES.includes(voice.category ?? "")
680
+ };
681
+ }
654
682
  // Annotate the CommonJS export names for ESM import in node:
655
683
  0 && (module.exports = {
656
684
  DEFAULT_MODEL_ID,
657
685
  DEFAULT_OUTPUT_FORMAT,
686
+ DEFAULT_SAMPLE_RATE,
658
687
  ElevenLabsTTS,
688
+ getOutputFormat,
689
+ getSampleRate,
659
690
  isDialogueModel
660
691
  });
661
692
  //# sourceMappingURL=index.js.map