@micdrop/gradium 1.1.2 → 1.3.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.mjs CHANGED
@@ -1,144 +1,193 @@
1
1
  // src/GradiumSTT.ts
2
- import { STT } from "@micdrop/server";
3
- import WebSocket2 from "ws";
4
-
5
- // src/closeSocket.ts
6
- import WebSocket from "ws";
7
- function closeSocket(socket) {
8
- socket.removeAllListeners();
9
- socket.on("error", () => {
10
- });
11
- if (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING) {
12
- socket.close(1e3);
13
- }
14
- }
2
+ import {
3
+ ReconnectingSocket,
4
+ STT
5
+ } from "@micdrop/server";
15
6
 
16
7
  // src/types.ts
17
8
  var DEFAULT_MODEL_NAME = "default";
18
- var DEFAULT_OUTPUT_FORMAT = "pcm_16000";
9
+ var DEFAULT_SAMPLE_RATE = 48e3;
10
+ var DEFAULT_OUTPUT_FORMAT = "pcm";
19
11
  var DEFAULT_INPUT_FORMAT = "pcm_16000";
20
12
  var DEFAULT_REGION = "eu";
21
13
  var DEFAULT_LANGUAGE = "any";
14
+ var PCM_FORMATS = {
15
+ 8e3: "pcm_8000",
16
+ 16e3: "pcm_16000",
17
+ 24e3: "pcm_24000",
18
+ 48e3: "pcm"
19
+ // Gradium names its 48 kHz format without a rate
20
+ };
21
+ function getOutputFormat(options) {
22
+ if (options.sampleRate) return PCM_FORMATS[options.sampleRate];
23
+ return options.outputFormat ?? PCM_FORMATS[DEFAULT_SAMPLE_RATE];
24
+ }
25
+ function getSampleRate(options) {
26
+ const format = getOutputFormat(options);
27
+ if (format === "pcm") return 48e3;
28
+ const match = /^pcm_(\d+)$/.exec(format);
29
+ return match ? Number(match[1]) : DEFAULT_SAMPLE_RATE;
30
+ }
22
31
 
23
32
  // src/GradiumSTT.ts
24
- var DEFAULT_CONNECTION_TIMEOUT = 5e3;
25
33
  var DEFAULT_TRANSCRIPTION_TIMEOUT = 4e3;
26
- var DEFAULT_RETRY_DELAY = 1e3;
27
- var DEFAULT_MAX_RETRY = 3;
28
34
  var FRAME_DURATION_MS = 80;
29
35
  var DEFAULT_DELAY_IN_FRAMES = 10;
30
36
  var GradiumSTT = class extends STT {
37
+ // Given by the server when ready
31
38
  constructor(options) {
32
39
  super();
33
40
  this.options = options;
34
- this.audioChunksPending = [];
35
- // Store audio chunks to send them again if reconnecting
36
- this.transcript = "";
37
- // Accumulated text segments for the current utterance
41
+ // Utterances in the order their audio went out. The server answers in that
42
+ // order too, so every text segment belongs to the first one not flushed yet
43
+ this.utterances = [];
38
44
  this.flushId = 0;
39
45
  this.delayInFrames = DEFAULT_DELAY_IN_FRAMES;
40
- // Given by the server when ready
41
- this.retryCount = 0;
42
- this.destroyed = false;
43
- this.initPromise = this.connect();
46
+ /** Sends the setup, and waits for the server to accept it */
47
+ this.handshake = {
48
+ start: (socket) => {
49
+ socket.send(
50
+ JSON.stringify({
51
+ type: "setup",
52
+ model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,
53
+ input_format: this.options.inputFormat ?? DEFAULT_INPUT_FORMAT,
54
+ json_config: this.buildJsonConfig()
55
+ })
56
+ );
57
+ this.log("Sent setup");
58
+ },
59
+ read: (data) => {
60
+ let message;
61
+ try {
62
+ message = JSON.parse(data.toString());
63
+ } catch {
64
+ this.log("Error parsing message", data);
65
+ return false;
66
+ }
67
+ if (message.type === "error") {
68
+ throw new Error(`Setup refused: ${message.message}`);
69
+ }
70
+ if (message.type !== "ready") return false;
71
+ this.log("Server ready");
72
+ if (typeof message.delay_in_frames === "number") {
73
+ this.delayInFrames = message.delay_in_frames;
74
+ }
75
+ return true;
76
+ }
77
+ };
78
+ // A new connection knows nothing of the utterances under way: the audio read
79
+ // before the first connection opened, or lost with the previous one, goes out
80
+ // again whole, and the server transcribes it from the start
81
+ this.onOpen = () => {
82
+ this.utterances = this.utterances.filter((utterance) => !utterance.done);
83
+ if (this.utterances.length === 0) return;
84
+ this.log("Sending audio chunks again");
85
+ for (const utterance of this.utterances) {
86
+ utterance.transcript = "";
87
+ if (utterance.flushId === void 0) {
88
+ utterance.audio.forEach((chunk) => this.sendAudioChunk(chunk));
89
+ } else {
90
+ utterance.audio.slice(0, -1).forEach((chunk) => this.sendAudioChunk(chunk));
91
+ this.sendEnd(utterance);
92
+ }
93
+ }
94
+ };
95
+ this.onMessage = (data) => {
96
+ let message;
97
+ try {
98
+ message = JSON.parse(data.toString());
99
+ } catch {
100
+ this.log("Error parsing message", data);
101
+ return;
102
+ }
103
+ switch (message.type) {
104
+ case "text": {
105
+ const utterance = this.utterances[0];
106
+ if (typeof message.text === "string" && utterance) {
107
+ utterance.transcript += (utterance.transcript ? " " : "") + message.text;
108
+ this.log(`Received text segment: "${message.text}"`);
109
+ }
110
+ break;
111
+ }
112
+ case "flushed": {
113
+ const index = this.utterances.findIndex(
114
+ (utterance) => utterance.flushId === message.flush_id
115
+ );
116
+ if (index === -1) break;
117
+ const flushed = this.utterances.splice(0, index + 1);
118
+ flushed.forEach((utterance) => this.emitTranscript(utterance));
119
+ break;
120
+ }
121
+ case "end_text":
122
+ break;
123
+ case "step":
124
+ break;
125
+ case "end_of_stream":
126
+ this.log("End of stream");
127
+ break;
128
+ case "error":
129
+ this.log("Error:", message.message, message.code);
130
+ break;
131
+ }
132
+ };
133
+ this.onFailed = () => {
134
+ const pending = this.utterances.filter((utterance) => !utterance.done);
135
+ this.utterances.forEach((utterance) => clearTimeout(utterance.timeout));
136
+ this.utterances = [];
137
+ this.emit(
138
+ "Failed",
139
+ pending.flatMap((utterance) => utterance.audio)
140
+ );
141
+ };
142
+ const region = options.region ?? DEFAULT_REGION;
143
+ this.socket = new ReconnectingSocket({
144
+ url: `wss://${region}.api.gradium.ai/api/speech/asr`,
145
+ clientOptions: { headers: { "x-api-key": options.apiKey } },
146
+ connectionTimeout: options.connectionTimeout,
147
+ retryDelay: options.retryDelay,
148
+ maxRetry: options.maxRetry,
149
+ // The server opens the connection before checking the setup, so the
150
+ // connection only counts as open once it answers ready
151
+ handshake: this.handshake,
152
+ log: (...message) => this.log(...message)
153
+ });
154
+ this.socket.on("Open", this.onOpen);
155
+ this.socket.on("Message", this.onMessage);
156
+ this.socket.on("Failed", this.onFailed);
157
+ this.socket.connect();
44
158
  }
45
159
  transcribe(audioStream) {
46
- let started = false;
47
- audioStream.on("data", async (chunk) => {
48
- if (!started) {
49
- started = true;
50
- this.transcript = "";
160
+ let utterance;
161
+ audioStream.on("data", (chunk) => {
162
+ if (!utterance) {
163
+ utterance = { audio: [], transcript: "", done: false };
164
+ this.utterances.push(utterance);
165
+ this.socket.connect();
166
+ }
167
+ if (utterance.done) return;
168
+ utterance.audio.push(chunk);
169
+ if (this.sendAudioChunk(chunk)) {
170
+ this.log(`Sent audio chunk (${chunk.byteLength} bytes)`);
51
171
  }
52
- this.audioChunksPending.push(chunk);
53
- await this.initPromise;
54
- this.sendAudioChunk(chunk);
55
- this.log(`Sent audio chunk (${chunk.byteLength} bytes)`);
56
172
  });
57
- audioStream.on("end", async () => {
58
- await this.initPromise;
59
- if (this.audioChunksPending.length === 0) return;
60
- this.sendTrailingSilence();
61
- const flushId = ++this.flushId;
62
- this.sendFlush(flushId);
63
- this.transcriptionTimeout = setTimeout(() => {
64
- this.transcriptionTimeout = void 0;
173
+ audioStream.on("end", () => {
174
+ if (!utterance || utterance.done) return;
175
+ const current = utterance;
176
+ current.audio.push(this.trailingSilence());
177
+ current.flushId = ++this.flushId;
178
+ if (this.socket.isOpen) this.sendEnd(current);
179
+ current.timeout = setTimeout(() => {
180
+ current.timeout = void 0;
65
181
  this.log("Transcription timeout");
66
- this.emitTranscript();
182
+ this.emitTranscript(current);
67
183
  }, this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT);
68
184
  });
69
185
  }
70
186
  destroy() {
71
187
  super.destroy();
72
- this.destroyed = true;
73
- if (this.reconnectTimeout) {
74
- clearTimeout(this.reconnectTimeout);
75
- this.reconnectTimeout = void 0;
76
- }
77
- if (this.transcriptionTimeout) {
78
- clearTimeout(this.transcriptionTimeout);
79
- this.transcriptionTimeout = void 0;
80
- }
81
- if (this.connectionTimeout) {
82
- clearTimeout(this.connectionTimeout);
83
- this.connectionTimeout = void 0;
84
- }
85
- if (this.socket) closeSocket(this.socket);
86
- this.socket = void 0;
87
- }
88
- connect() {
89
- return this.initWS().catch((error) => {
90
- console.error("[GradiumSTT] Connection error:", error);
91
- this.reconnect();
92
- });
93
- }
94
- getEndpoint() {
95
- const region = this.options.region ?? DEFAULT_REGION;
96
- return `wss://${region}.api.gradium.ai/api/speech/asr`;
97
- }
98
- async initWS() {
99
- return new Promise((resolve, reject) => {
100
- const socket = new WebSocket2(this.getEndpoint(), {
101
- headers: {
102
- "x-api-key": this.options.apiKey
103
- }
104
- });
105
- this.socket = socket;
106
- const timeout = setTimeout(() => {
107
- this.log("Connection timeout");
108
- closeSocket(socket);
109
- this.socket = void 0;
110
- reject(new Error("WebSocket connection timeout"));
111
- }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT);
112
- this.connectionTimeout = timeout;
113
- socket.addEventListener("open", () => {
114
- clearTimeout(timeout);
115
- this.sendSetup();
116
- this.log("Connection opened");
117
- resolve();
118
- });
119
- socket.addEventListener("error", (error) => {
120
- clearTimeout(timeout);
121
- this.log("WebSocket error:", error);
122
- reject(new Error("WebSocket connection error"));
123
- });
124
- socket.addEventListener("close", ({ code, reason }) => {
125
- clearTimeout(timeout);
126
- this.socket?.removeAllListeners();
127
- this.socket = void 0;
128
- if (code !== 1e3) {
129
- this.reconnect();
130
- } else {
131
- this.log("Connection closed", { code, reason });
132
- }
133
- });
134
- socket.addEventListener("message", (event) => {
135
- try {
136
- this.handleMessage(JSON.parse(event.data.toString()));
137
- } catch {
138
- this.log("Error parsing message", event.data);
139
- }
140
- });
141
- });
188
+ this.socket.destroy();
189
+ this.utterances.forEach((utterance) => clearTimeout(utterance.timeout));
190
+ this.utterances = [];
142
191
  }
143
192
  buildJsonConfig() {
144
193
  const config = { ...this.options.jsonConfig };
@@ -147,19 +196,8 @@ var GradiumSTT = class extends STT {
147
196
  }
148
197
  return config;
149
198
  }
150
- sendSetup() {
151
- this.socket?.send(
152
- JSON.stringify({
153
- type: "setup",
154
- model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,
155
- input_format: this.options.inputFormat ?? DEFAULT_INPUT_FORMAT,
156
- json_config: this.buildJsonConfig()
157
- })
158
- );
159
- this.log("Sent setup");
160
- }
161
199
  sendAudioChunk(chunk) {
162
- this.socket?.send(
200
+ return this.socket.send(
163
201
  JSON.stringify({
164
202
  type: "audio",
165
203
  audio: chunk.toString("base64")
@@ -167,98 +205,40 @@ var GradiumSTT = class extends STT {
167
205
  );
168
206
  }
169
207
  /**
170
- * Follows the speech with as much silence as the transcription lags behind.
208
+ * Silence as long as the transcription lags behind, to follow the speech.
171
209
  *
172
210
  * The model writes each word a few frames after hearing it, and a flush only
173
211
  * processes the audio already sent. With nothing after the last word, the
174
212
  * server confirms the flush before writing that word, which then opens the
175
213
  * next transcript.
176
214
  */
177
- sendTrailingSilence() {
215
+ trailingSilence() {
178
216
  const sampleRate = pcmSampleRate(
179
217
  this.options.inputFormat ?? DEFAULT_INPUT_FORMAT
180
218
  );
181
- if (!sampleRate) return;
219
+ if (!sampleRate) return Buffer.alloc(0);
182
220
  const durationMs = this.delayInFrames * FRAME_DURATION_MS;
183
221
  const samples = Math.round(sampleRate * durationMs / 1e3);
184
- const silence = Buffer.alloc(samples * 2);
185
- this.audioChunksPending.push(silence);
186
- this.sendAudioChunk(silence);
187
- this.log(`Sent ${durationMs}ms of silence`);
222
+ return Buffer.alloc(samples * 2);
188
223
  }
189
- sendFlush(flushId) {
190
- this.socket?.send(JSON.stringify({ type: "flush", flush_id: flushId }));
191
- this.log(`Sent flush (flush_id=${flushId})`);
224
+ /** Sends the trailing silence, already the last audio chunk, and the flush */
225
+ sendEnd(utterance) {
226
+ const silence = utterance.audio[utterance.audio.length - 1];
227
+ if (silence.length) this.sendAudioChunk(silence);
228
+ this.socket.send(
229
+ JSON.stringify({ type: "flush", flush_id: utterance.flushId })
230
+ );
231
+ this.log(`Sent flush (flush_id=${utterance.flushId})`);
192
232
  }
193
- emitTranscript() {
194
- const transcript = this.transcript.replace(/\s+/g, " ").trim();
233
+ emitTranscript(utterance) {
234
+ if (utterance.done) return;
235
+ utterance.done = true;
236
+ clearTimeout(utterance.timeout);
237
+ utterance.timeout = void 0;
238
+ utterance.audio = [];
239
+ const transcript = utterance.transcript.replace(/\s+/g, " ").trim();
195
240
  this.log(`Received transcript: "${transcript}"`);
196
241
  this.emit("Transcript", transcript);
197
- this.transcript = "";
198
- this.audioChunksPending.length = 0;
199
- if (this.transcriptionTimeout) {
200
- clearTimeout(this.transcriptionTimeout);
201
- this.transcriptionTimeout = void 0;
202
- }
203
- }
204
- handleMessage(message) {
205
- switch (message.type) {
206
- case "ready":
207
- this.log("Server ready");
208
- this.retryCount = 0;
209
- if (typeof message.delay_in_frames === "number") {
210
- this.delayInFrames = message.delay_in_frames;
211
- }
212
- break;
213
- case "text":
214
- if (typeof message.text === "string") {
215
- this.transcript += (this.transcript ? " " : "") + message.text;
216
- this.log(`Received text segment: "${message.text}"`);
217
- }
218
- break;
219
- case "end_text":
220
- break;
221
- case "flushed":
222
- if (message.flush_id === this.flushId) {
223
- this.emitTranscript();
224
- }
225
- break;
226
- case "step":
227
- break;
228
- case "end_of_stream":
229
- this.log("End of stream");
230
- break;
231
- case "error":
232
- this.log("Error:", message.message, message.code);
233
- break;
234
- }
235
- }
236
- reconnect() {
237
- if (this.destroyed) return;
238
- this.retryCount++;
239
- if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
240
- this.log("Max retries reached, giving up");
241
- this.emit("Failed", this.audioChunksPending);
242
- return;
243
- }
244
- this.initPromise = new Promise((resolve) => {
245
- this.log("Reconnecting...");
246
- this.reconnectTimeout = setTimeout(() => {
247
- this.reconnectTimeout = void 0;
248
- this.initWS().then(() => {
249
- if (this.audioChunksPending.length > 0) {
250
- this.log("Sending audio chunks again");
251
- this.transcript = "";
252
- this.audioChunksPending.forEach(
253
- (chunk) => this.sendAudioChunk(chunk)
254
- );
255
- }
256
- }).then(resolve).catch((error) => {
257
- this.log("Reconnection error:", error);
258
- this.reconnect();
259
- });
260
- }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
261
- });
262
242
  }
263
243
  };
264
244
  function pcmSampleRate(format) {
@@ -268,264 +248,250 @@ function pcmSampleRate(format) {
268
248
  }
269
249
 
270
250
  // src/GradiumTTS.ts
271
- import { TTS } from "@micdrop/server";
272
- import WebSocket3 from "ws";
273
- var DEFAULT_CONNECTION_TIMEOUT2 = 5e3;
274
- var DEFAULT_RETRY_DELAY2 = 1e3;
275
- var DEFAULT_MAX_RETRY2 = 3;
251
+ import {
252
+ cachedVoices,
253
+ ReconnectingSocket as ReconnectingSocket2,
254
+ TTS,
255
+ voiceGender,
256
+ voicesForLanguage
257
+ } from "@micdrop/server";
258
+ var VOICES_PAGE_SIZE = 500;
276
259
  var GradiumTTS = class extends TTS {
277
260
  constructor(options) {
278
261
  super();
279
262
  this.options = options;
263
+ // Numbers the audio requests, and only moves when there is something to say
280
264
  this.counter = 0;
281
265
  // Bumped by every speak() and every cancel(), so a request claimed late can
282
- // tell whether it is still the one that should be heard. Kept apart from
283
- // `counter`, which numbers the audio requests and must only move when there
284
- // is something to synthesize.
266
+ // tell whether it is still the one that should be heard
285
267
  this.generation = 0;
286
- this.isProcessing = false;
287
- this.textSent = "";
288
- this.textBuffer = "";
289
- this.retryCount = 0;
290
- this.destroyed = false;
291
- this.connect();
292
- }
293
- connect() {
294
- if (this.destroyed) return;
295
- this.initPromise = this.initWS().catch((error) => {
296
- console.error("[GradiumTTS] Connection error:", error);
297
- this.reconnect();
268
+ // A new connection has no session for the request under way: its setup and
269
+ // its words go out again, the setup first, with the same client_req_id
270
+ this.onOpen = () => {
271
+ const request = this.request;
272
+ if (!request) return;
273
+ this.log("Sending the request again");
274
+ this.sendSetup(request.id);
275
+ if (request.words) this.sendTranscript(request.words, request.id);
276
+ if (request.ended) this.sendEnd(request);
277
+ };
278
+ this.onMessage = (data) => {
279
+ let message;
280
+ try {
281
+ message = JSON.parse(data.toString());
282
+ } catch {
283
+ this.log("Error parsing message", data);
284
+ return;
285
+ }
286
+ switch (message.type) {
287
+ case "ready":
288
+ this.log("Server ready", message.request_id ?? "");
289
+ break;
290
+ case "audio":
291
+ this.processAudioMessage(message);
292
+ break;
293
+ case "end_of_stream":
294
+ if (!this.request || message.client_req_id !== void 0 && message.client_req_id !== this.request.id)
295
+ return;
296
+ this.log("Audio ended");
297
+ this.request = void 0;
298
+ break;
299
+ case "error":
300
+ this.log("Error:", message.message, message.code);
301
+ if (!this.request) break;
302
+ this.onFailed();
303
+ this.socket.restart();
304
+ break;
305
+ }
306
+ };
307
+ this.onFailed = () => {
308
+ const request = this.request;
309
+ this.request = void 0;
310
+ const text = request ? request.words + request.partial : "";
311
+ this.emit("Failed", text ? [text] : []);
312
+ };
313
+ const region = options.region ?? DEFAULT_REGION;
314
+ this.socket = new ReconnectingSocket2({
315
+ url: `wss://${region}.api.gradium.ai/api/speech/tts`,
316
+ clientOptions: { headers: { "x-api-key": options.apiKey } },
317
+ connectionTimeout: options.connectionTimeout,
318
+ retryDelay: options.retryDelay,
319
+ maxRetry: options.maxRetry,
320
+ log: (...message) => this.log(...message)
298
321
  });
322
+ this.socket.on("Open", this.onOpen);
323
+ this.socket.on("Message", this.onMessage);
324
+ this.socket.on("Failed", this.onFailed);
325
+ this.socket.connect();
326
+ }
327
+ /**
328
+ * Lists the voices the account can speak with: its own, then the Gradium
329
+ * catalog, over a thousand voices.
330
+ *
331
+ * `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.
332
+ *
333
+ * The list is fetched once and shared by every call of the process for a
334
+ * day. `refresh` fetches it again, for a voice added a moment ago.
335
+ */
336
+ static listVoices(options) {
337
+ const region = options.region ?? DEFAULT_REGION;
338
+ return cachedVoices(
339
+ ["gradium", region],
340
+ options.apiKey,
341
+ () => fetchVoices(options.apiKey, region),
342
+ options.refresh
343
+ ).then((voices) => voicesForLanguage(voices, options.language));
344
+ }
345
+ get sampleRate() {
346
+ return getSampleRate(this.options);
299
347
  }
300
348
  speak(textStream) {
301
349
  const generation = ++this.generation;
302
- let counter = 0;
303
- let clientReqId = "";
350
+ let request;
304
351
  const claimRequest = () => {
305
- if (counter) return true;
306
- if (this.generation !== generation) return false;
307
- this.counter++;
308
- counter = this.counter;
309
- clientReqId = counter.toString();
310
- this.isProcessing = true;
311
- this.textSent = "";
312
- this.textBuffer = "";
313
- this.initPromise.then(() => {
314
- if (counter !== this.counter) return;
315
- this.sendSetup(clientReqId);
316
- });
317
- return true;
352
+ if (request) return request;
353
+ if (this.generation !== generation) return;
354
+ request = {
355
+ id: (++this.counter).toString(),
356
+ words: "",
357
+ partial: "",
358
+ ended: false
359
+ };
360
+ this.request = request;
361
+ this.sendSetup(request.id);
362
+ this.socket.connect();
363
+ return request;
318
364
  };
319
- textStream.on("data", async (chunk) => {
320
- if (!claimRequest()) return;
321
- if (counter !== this.counter) return;
365
+ textStream.on("data", (chunk) => {
366
+ const request2 = claimRequest();
367
+ if (!request2 || this.request !== request2) return;
322
368
  const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
323
- this.textSent += text;
324
- await this.initPromise;
325
- if (counter !== this.counter) return;
326
369
  const spaceIndex = text.lastIndexOf(" ");
327
370
  if (spaceIndex === -1) {
328
- this.textBuffer += text;
329
- } else {
330
- this.sendTranscript(
331
- this.textBuffer + text.slice(0, spaceIndex + 1),
332
- clientReqId
333
- );
334
- this.textBuffer = text.slice(spaceIndex + 1);
371
+ request2.partial += text;
372
+ return;
335
373
  }
374
+ const words = request2.partial + text.slice(0, spaceIndex + 1);
375
+ request2.partial = text.slice(spaceIndex + 1);
376
+ request2.words += words;
377
+ this.sendTranscript(words, request2.id);
336
378
  });
337
379
  textStream.on("error", (error) => {
338
- if (!counter) return;
339
- this.log("Error in text stream, ending audio stream", error);
340
- this.isProcessing = false;
380
+ if (!request || this.request !== request) return;
381
+ this.log("Error in text stream, cancelling the audio", error);
382
+ this.stopRequest();
341
383
  });
342
- textStream.on("end", async () => {
343
- if (!counter || counter !== this.counter) return;
344
- await this.initPromise;
345
- if (counter !== this.counter) return;
346
- if (this.textBuffer.trim()) {
347
- this.sendTranscript(this.textBuffer, clientReqId);
348
- this.textBuffer = "";
349
- }
350
- this.socket?.send(
351
- JSON.stringify({
352
- type: "end_of_stream",
353
- client_req_id: clientReqId
354
- })
355
- );
356
- this.log("Sent end_of_stream");
384
+ textStream.on("end", () => {
385
+ if (!request || this.request !== request) return;
386
+ request.ended = true;
387
+ this.sendEnd(request);
357
388
  });
358
389
  }
390
+ /**
391
+ * Stops the current utterance. Gradium can only stop a synthesis by closing
392
+ * its connection, so a new one opens right away.
393
+ */
359
394
  cancel() {
360
395
  this.generation++;
361
- if (!this.isProcessing) return;
396
+ if (!this.request) return;
362
397
  this.log("Cancel");
363
- this.isProcessing = false;
364
- this.textSent = "";
365
- this.textBuffer = "";
366
- this.counter++;
367
- if (this.reconnectTimeout) {
368
- clearTimeout(this.reconnectTimeout);
369
- this.reconnectTimeout = void 0;
370
- }
371
- if (this.connectionTimeout) {
372
- clearTimeout(this.connectionTimeout);
373
- this.connectionTimeout = void 0;
374
- }
375
- const socket = this.socket;
376
- this.socket = void 0;
377
- if (socket) closeSocket(socket);
378
- this.connect();
398
+ this.stopRequest();
379
399
  }
380
400
  destroy() {
381
- this.destroyed = true;
401
+ this.socket.destroy();
382
402
  super.destroy();
383
- if (this.reconnectTimeout) {
384
- clearTimeout(this.reconnectTimeout);
385
- this.reconnectTimeout = void 0;
386
- }
387
- if (this.connectionTimeout) {
388
- clearTimeout(this.connectionTimeout);
389
- this.connectionTimeout = void 0;
390
- }
391
- if (this.socket) closeSocket(this.socket);
392
- this.socket = void 0;
393
- this.isProcessing = false;
403
+ this.request = void 0;
394
404
  }
395
- getEndpoint() {
396
- const region = this.options.region ?? DEFAULT_REGION;
397
- return `wss://${region}.api.gradium.ai/api/speech/tts`;
398
- }
399
- async initWS() {
400
- return new Promise((resolve, reject) => {
401
- const socket = new WebSocket3(this.getEndpoint(), {
402
- headers: {
403
- "x-api-key": this.options.apiKey
404
- }
405
- });
406
- this.socket = socket;
407
- const timeout = setTimeout(() => {
408
- this.log("Connection timeout");
409
- closeSocket(socket);
410
- this.socket = void 0;
411
- reject(new Error("WebSocket connection timeout"));
412
- }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT2);
413
- this.connectionTimeout = timeout;
414
- socket.addEventListener("open", () => {
415
- clearTimeout(timeout);
416
- this.log("Connection opened");
417
- resolve();
418
- });
419
- socket.addEventListener("error", (error) => {
420
- clearTimeout(timeout);
421
- this.log("WebSocket error:", error);
422
- reject(new Error("WebSocket connection error"));
423
- });
424
- socket.addEventListener("close", ({ code, reason }) => {
425
- clearTimeout(timeout);
426
- this.socket?.removeAllListeners();
427
- this.socket = void 0;
428
- if (code !== 1e3) {
429
- this.reconnect();
430
- } else {
431
- this.log("Connection closed", { code, reason });
432
- if (!this.isProcessing) {
433
- this.reconnect();
434
- }
435
- }
436
- });
437
- socket.addEventListener("message", (event) => {
438
- try {
439
- const message = JSON.parse(event.data.toString());
440
- switch (message.type) {
441
- case "ready":
442
- this.log("Server ready", message.request_id ?? "");
443
- this.retryCount = 0;
444
- break;
445
- case "audio":
446
- this.processAudioMessage(message);
447
- break;
448
- case "end_of_stream":
449
- if ("client_req_id" in message && message.client_req_id !== this.counter.toString())
450
- return;
451
- this.log("Audio ended");
452
- this.isProcessing = false;
453
- this.textSent = "";
454
- break;
455
- case "error":
456
- this.log("Error:", message.message, message.code);
457
- break;
458
- }
459
- } catch {
460
- this.log("Error parsing message", event.data);
461
- }
462
- });
463
- });
405
+ /** Forgets the current request, and closes the connection that carries it */
406
+ stopRequest() {
407
+ this.request = void 0;
408
+ this.socket.restart();
464
409
  }
465
410
  sendSetup(clientReqId) {
466
- this.socket?.send(
411
+ const sent = this.socket.send(
467
412
  JSON.stringify({
468
413
  type: "setup",
469
414
  voice_id: this.options.voiceId,
470
415
  model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,
471
- output_format: this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT,
416
+ output_format: getOutputFormat(this.options),
472
417
  close_ws_on_eos: false,
473
418
  json_config: this.options.jsonConfig,
474
419
  client_req_id: clientReqId
475
420
  })
476
421
  );
477
- this.log(`Sent setup (client_req_id=${clientReqId})`);
422
+ if (sent) this.log(`Sent setup (client_req_id=${clientReqId})`);
478
423
  }
479
424
  sendTranscript(text, clientReqId) {
480
- this.socket?.send(
425
+ const sent = this.socket.send(
481
426
  JSON.stringify({
482
427
  type: "text",
483
428
  text,
484
429
  client_req_id: clientReqId
485
430
  })
486
431
  );
487
- this.log(`Sent transcript: "${text}"`);
432
+ if (sent) this.log(`Sent transcript: "${text}"`);
433
+ }
434
+ /** Sends the last partial word, then the end of the text */
435
+ sendEnd(request) {
436
+ if (request.partial.trim()) {
437
+ this.sendTranscript(request.partial, request.id);
438
+ }
439
+ const sent = this.socket.send(
440
+ JSON.stringify({
441
+ type: "end_of_stream",
442
+ client_req_id: request.id
443
+ })
444
+ );
445
+ if (sent) this.log("Sent end_of_stream");
488
446
  }
489
447
  processAudioMessage(message) {
490
- if (message.client_req_id !== this.counter.toString()) return;
448
+ if (!this.request || message.client_req_id !== this.request.id) return;
491
449
  const chunk = Buffer.from(message.audio, "base64");
492
450
  this.log(`Received audio chunk (${chunk.length} bytes)`);
493
451
  this.emit("Audio", chunk);
494
452
  }
495
- reconnect() {
496
- if (this.destroyed) return;
497
- this.retryCount++;
498
- if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY2)) {
499
- this.log("Max retries reached, giving up");
500
- this.emit("Failed", [this.textSent]);
501
- return;
502
- }
503
- this.initPromise = new Promise((resolve) => {
504
- this.log("Reconnecting...");
505
- this.reconnectTimeout = setTimeout(() => {
506
- this.reconnectTimeout = void 0;
507
- this.initWS().then(() => {
508
- if (this.textSent.length > 0) {
509
- const clientReqId = this.counter.toString();
510
- this.log("Sending text chunks again");
511
- this.sendSetup(clientReqId);
512
- this.sendTranscript(this.textSent, clientReqId);
513
- }
514
- }).then(resolve).catch((error) => {
515
- this.log("Reconnection error:", error);
516
- this.reconnect();
517
- });
518
- }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY2);
453
+ };
454
+ async function fetchVoices(apiKey, region) {
455
+ const voices = [];
456
+ for (let skip = 0; ; skip += VOICES_PAGE_SIZE) {
457
+ const params = new URLSearchParams({
458
+ include_catalog: "true",
459
+ limit: String(VOICES_PAGE_SIZE),
460
+ skip: String(skip)
519
461
  });
462
+ const response = await fetch(
463
+ `https://${region}.api.gradium.ai/api/voices/?${params}`,
464
+ { headers: { "x-api-key": apiKey } }
465
+ );
466
+ if (!response.ok) throw new Error(`Gradium answered ${response.status}`);
467
+ const page = await response.json();
468
+ voices.push(...page.map(toMicdropVoice));
469
+ if (page.length < VOICES_PAGE_SIZE) break;
520
470
  }
521
- };
471
+ return voices;
472
+ }
473
+ function toMicdropVoice(voice) {
474
+ const [language, region] = (voice.locale || voice.language || "").split("-");
475
+ const locale = region ? `${language}-${region.toUpperCase()}` : language;
476
+ return {
477
+ id: voice.uid,
478
+ name: voice.name,
479
+ languages: locale ? [locale] : void 0,
480
+ gender: voiceGender(voice.gender),
481
+ description: voice.description || void 0,
482
+ custom: voice.is_catalog === false
483
+ };
484
+ }
522
485
  export {
523
486
  DEFAULT_INPUT_FORMAT,
524
487
  DEFAULT_LANGUAGE,
525
488
  DEFAULT_MODEL_NAME,
526
489
  DEFAULT_OUTPUT_FORMAT,
527
490
  DEFAULT_REGION,
491
+ DEFAULT_SAMPLE_RATE,
528
492
  GradiumSTT,
529
- GradiumTTS
493
+ GradiumTTS,
494
+ getOutputFormat,
495
+ getSampleRate
530
496
  };
531
497
  //# sourceMappingURL=index.mjs.map