@micdrop/gradium 1.2.0 → 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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @micdrop/gradium
2
2
 
3
- [Micdrop website](https://micdrop.dev) | [Documentation](https://micdrop.dev/docs/ai-integration/provided-integrations/gradium)
3
+ [Micdrop website](https://micdrop.dev) | [Documentation](https://micdrop.dev/docs/providers)
4
4
 
5
5
  Gradium STT and TTS implementation for [@micdrop/server](https://micdrop.dev/docs/server).
6
6
 
@@ -22,7 +22,7 @@ const tts = new GradiumTTS({
22
22
  apiKey: process.env.GRADIUM_API_KEY || '',
23
23
  voiceId: 'Bla6SbVMczYnOhfK', // Gradium voice ID
24
24
  modelName: 'default', // Optional: model name
25
- outputFormat: 'pcm_16000', // Optional: audio format
25
+ sampleRate: 24000, // Optional: 8000, 16000, 24000 or 48000 (default)
26
26
  region: 'eu', // Optional: 'eu' or 'us'
27
27
  jsonConfig: {
28
28
  // Optional: advanced voice settings
@@ -50,7 +50,7 @@ const tts = new GradiumTTS({
50
50
  voiceId: 'Bla6SbVMczYnOhfK',
51
51
  })
52
52
 
53
- // Audio is raw PCM, 16 bits, 16 kHz, mono
53
+ // Audio is raw PCM, 16 bits, mono, at tts.sampleRate
54
54
  tts.on('Audio', (chunk) => console.log('Audio:', chunk.length, 'bytes'))
55
55
  tts.on('Failed', (texts) => console.error('Failed:', texts))
56
56
 
@@ -59,12 +59,12 @@ tts.speak(Readable.from(['Hello! ', 'What can I do for you?']))
59
59
 
60
60
  ### Events
61
61
 
62
- | Event | Payload | Description |
63
- | -------- | ---------- | --------------------------------------------------------------------------------------------------------- |
64
- | `Audio` | `Buffer` | A chunk of audio, PCM 16 bits, 16 kHz, mono, ready to be played. |
62
+ | Event | Payload | Description |
63
+ | -------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
64
+ | `Audio` | `Buffer` | A chunk of audio, PCM 16 bits, mono, at `tts.sampleRate` (the `sampleRate` option, 48 kHz by default), ready to be played. |
65
65
  | `Failed` | `string[]` | Gradium refused the request, or every reconnection attempt failed, with the text that stayed unspoken. The next utterance connects again. |
66
66
 
67
- See the [TTS interface](https://micdrop.dev/docs/ai-integration/custom-integrations/custom-tts) for the full contract.
67
+ See the [TTS interface](https://micdrop.dev/docs/text-to-speech/custom) for the full contract.
68
68
 
69
69
  ## Gradium STT (Speech-to-Text)
70
70
 
@@ -116,11 +116,11 @@ stt.transcribe(createReadStream('speech.pcm'))
116
116
  | `Transcript` | `string` | Transcription of one utterance, emitted once per utterance even when the next one starts before it returns. The text is empty when nothing was recognized. |
117
117
  | `Failed` | `Buffer[]` | Every reconnection attempt failed, with the audio of the utterances still waiting for their transcript. The next utterance connects again. |
118
118
 
119
- See the [STT interface](https://micdrop.dev/docs/ai-integration/custom-integrations/custom-stt) for the full contract.
119
+ See the [STT interface](https://micdrop.dev/docs/speech-to-text/custom) for the full contract.
120
120
 
121
121
  ## Documentation
122
122
 
123
- Read full [documentation of the Gradium integration for Micdrop](https://micdrop.dev/docs/ai-integration/provided-integrations/gradium) on the [website](https://micdrop.dev).
123
+ Read full [documentation of the Gradium integration for Micdrop](https://micdrop.dev/docs/providers) on the [website](https://micdrop.dev).
124
124
 
125
125
  ## License
126
126
 
package/dist/index.d.mts CHANGED
@@ -1,13 +1,24 @@
1
- import { STT, TTS } from '@micdrop/server';
1
+ import { STT, TTS, MicdropVoice } from '@micdrop/server';
2
2
  import { Readable } from 'stream';
3
3
 
4
4
  type GradiumOutputFormat = 'pcm' | 'wav' | 'opus' | 'ulaw_8000' | 'alaw_8000' | 'pcm_8000' | 'pcm_16000' | 'pcm_24000';
5
5
  type GradiumInputFormat = 'pcm' | 'pcm_8000' | 'pcm_16000' | 'pcm_24000' | 'pcm_48000' | 'wav' | 'opus' | 'ulaw_8000' | 'alaw_8000';
6
6
  type GradiumRegion = 'eu' | 'us';
7
+ /** Rates Gradium streams raw PCM at */
8
+ type GradiumSampleRate = 8000 | 16000 | 24000 | 48000;
7
9
  interface GradiumTTSOptions {
8
10
  apiKey: string;
9
11
  voiceId: string;
10
12
  modelName?: string;
13
+ /**
14
+ * Rate of the audio, in Hz, 48000 by default, the highest one offered. It
15
+ * goes to the client as is, so the voice is never resampled on the server.
16
+ */
17
+ sampleRate?: GradiumSampleRate;
18
+ /**
19
+ * @deprecated Use `sampleRate`. The client only plays raw PCM, so only `pcm`
20
+ * and the `pcm_*` formats work.
21
+ */
11
22
  outputFormat?: GradiumOutputFormat;
12
23
  region?: GradiumRegion;
13
24
  jsonConfig?: GradiumJsonConfig;
@@ -21,10 +32,16 @@ interface GradiumJsonConfig {
21
32
  cfg_coef?: number;
22
33
  }
23
34
  declare const DEFAULT_MODEL_NAME = "default";
35
+ declare const DEFAULT_SAMPLE_RATE: GradiumSampleRate;
36
+ /** @deprecated Use `DEFAULT_SAMPLE_RATE` */
24
37
  declare const DEFAULT_OUTPUT_FORMAT: GradiumOutputFormat;
25
38
  declare const DEFAULT_INPUT_FORMAT: GradiumInputFormat;
26
39
  declare const DEFAULT_REGION: GradiumRegion;
27
40
  declare const DEFAULT_LANGUAGE = "any";
41
+ /** The `output_format` asked of Gradium */
42
+ declare function getOutputFormat(options: GradiumTTSOptions): GradiumOutputFormat;
43
+ /** Rate of the audio Gradium sends, read from its `output_format` */
44
+ declare function getSampleRate(options: GradiumTTSOptions): number;
28
45
  interface GradiumASRJsonConfig {
29
46
  language?: string;
30
47
  target_language?: string;
@@ -155,10 +172,26 @@ declare class GradiumSTT extends STT {
155
172
 
156
173
  declare class GradiumTTS extends TTS {
157
174
  private readonly options;
175
+ /**
176
+ * Lists the voices the account can speak with: its own, then the Gradium
177
+ * catalog, over a thousand voices.
178
+ *
179
+ * `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.
180
+ *
181
+ * The list is fetched once and shared by every call of the process for a
182
+ * day. `refresh` fetches it again, for a voice added a moment ago.
183
+ */
184
+ static listVoices(options: {
185
+ apiKey: string;
186
+ region?: GradiumRegion;
187
+ language?: string;
188
+ refresh?: boolean;
189
+ }): Promise<MicdropVoice[]>;
158
190
  private socket;
159
191
  private counter;
160
192
  private generation;
161
193
  private request?;
194
+ get sampleRate(): number;
162
195
  constructor(options: GradiumTTSOptions);
163
196
  speak(textStream: Readable): void;
164
197
  /**
@@ -179,4 +212,4 @@ declare class GradiumTTS extends TTS {
179
212
  private onFailed;
180
213
  }
181
214
 
182
- export { DEFAULT_INPUT_FORMAT, DEFAULT_LANGUAGE, DEFAULT_MODEL_NAME, DEFAULT_OUTPUT_FORMAT, DEFAULT_REGION, type GradiumASRJsonConfig, type GradiumASRResponse, type GradiumASRSetupMessage, type GradiumAudioMessage, type GradiumAudioResponse, type GradiumEndTextResponse, type GradiumEosMessage, type GradiumEosResponse, type GradiumErrorResponse, type GradiumFlushMessage, type GradiumFlushedResponse, type GradiumInputFormat, type GradiumJsonConfig, type GradiumOutputFormat, type GradiumReadyResponse, type GradiumRegion, type GradiumResponse, GradiumSTT, type GradiumSTTOptions, type GradiumSetupMessage, type GradiumStepResponse, GradiumTTS, type GradiumTTSOptions, type GradiumTextMessage, type GradiumTextResponse };
215
+ export { DEFAULT_INPUT_FORMAT, DEFAULT_LANGUAGE, DEFAULT_MODEL_NAME, DEFAULT_OUTPUT_FORMAT, DEFAULT_REGION, DEFAULT_SAMPLE_RATE, type GradiumASRJsonConfig, type GradiumASRResponse, type GradiumASRSetupMessage, type GradiumAudioMessage, type GradiumAudioResponse, type GradiumEndTextResponse, type GradiumEosMessage, type GradiumEosResponse, type GradiumErrorResponse, type GradiumFlushMessage, type GradiumFlushedResponse, type GradiumInputFormat, type GradiumJsonConfig, type GradiumOutputFormat, type GradiumReadyResponse, type GradiumRegion, type GradiumResponse, GradiumSTT, type GradiumSTTOptions, type GradiumSampleRate, type GradiumSetupMessage, type GradiumStepResponse, GradiumTTS, type GradiumTTSOptions, type GradiumTextMessage, type GradiumTextResponse, getOutputFormat, getSampleRate };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,24 @@
1
- import { STT, TTS } from '@micdrop/server';
1
+ import { STT, TTS, MicdropVoice } from '@micdrop/server';
2
2
  import { Readable } from 'stream';
3
3
 
4
4
  type GradiumOutputFormat = 'pcm' | 'wav' | 'opus' | 'ulaw_8000' | 'alaw_8000' | 'pcm_8000' | 'pcm_16000' | 'pcm_24000';
5
5
  type GradiumInputFormat = 'pcm' | 'pcm_8000' | 'pcm_16000' | 'pcm_24000' | 'pcm_48000' | 'wav' | 'opus' | 'ulaw_8000' | 'alaw_8000';
6
6
  type GradiumRegion = 'eu' | 'us';
7
+ /** Rates Gradium streams raw PCM at */
8
+ type GradiumSampleRate = 8000 | 16000 | 24000 | 48000;
7
9
  interface GradiumTTSOptions {
8
10
  apiKey: string;
9
11
  voiceId: string;
10
12
  modelName?: string;
13
+ /**
14
+ * Rate of the audio, in Hz, 48000 by default, the highest one offered. It
15
+ * goes to the client as is, so the voice is never resampled on the server.
16
+ */
17
+ sampleRate?: GradiumSampleRate;
18
+ /**
19
+ * @deprecated Use `sampleRate`. The client only plays raw PCM, so only `pcm`
20
+ * and the `pcm_*` formats work.
21
+ */
11
22
  outputFormat?: GradiumOutputFormat;
12
23
  region?: GradiumRegion;
13
24
  jsonConfig?: GradiumJsonConfig;
@@ -21,10 +32,16 @@ interface GradiumJsonConfig {
21
32
  cfg_coef?: number;
22
33
  }
23
34
  declare const DEFAULT_MODEL_NAME = "default";
35
+ declare const DEFAULT_SAMPLE_RATE: GradiumSampleRate;
36
+ /** @deprecated Use `DEFAULT_SAMPLE_RATE` */
24
37
  declare const DEFAULT_OUTPUT_FORMAT: GradiumOutputFormat;
25
38
  declare const DEFAULT_INPUT_FORMAT: GradiumInputFormat;
26
39
  declare const DEFAULT_REGION: GradiumRegion;
27
40
  declare const DEFAULT_LANGUAGE = "any";
41
+ /** The `output_format` asked of Gradium */
42
+ declare function getOutputFormat(options: GradiumTTSOptions): GradiumOutputFormat;
43
+ /** Rate of the audio Gradium sends, read from its `output_format` */
44
+ declare function getSampleRate(options: GradiumTTSOptions): number;
28
45
  interface GradiumASRJsonConfig {
29
46
  language?: string;
30
47
  target_language?: string;
@@ -155,10 +172,26 @@ declare class GradiumSTT extends STT {
155
172
 
156
173
  declare class GradiumTTS extends TTS {
157
174
  private readonly options;
175
+ /**
176
+ * Lists the voices the account can speak with: its own, then the Gradium
177
+ * catalog, over a thousand voices.
178
+ *
179
+ * `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.
180
+ *
181
+ * The list is fetched once and shared by every call of the process for a
182
+ * day. `refresh` fetches it again, for a voice added a moment ago.
183
+ */
184
+ static listVoices(options: {
185
+ apiKey: string;
186
+ region?: GradiumRegion;
187
+ language?: string;
188
+ refresh?: boolean;
189
+ }): Promise<MicdropVoice[]>;
158
190
  private socket;
159
191
  private counter;
160
192
  private generation;
161
193
  private request?;
194
+ get sampleRate(): number;
162
195
  constructor(options: GradiumTTSOptions);
163
196
  speak(textStream: Readable): void;
164
197
  /**
@@ -179,4 +212,4 @@ declare class GradiumTTS extends TTS {
179
212
  private onFailed;
180
213
  }
181
214
 
182
- export { DEFAULT_INPUT_FORMAT, DEFAULT_LANGUAGE, DEFAULT_MODEL_NAME, DEFAULT_OUTPUT_FORMAT, DEFAULT_REGION, type GradiumASRJsonConfig, type GradiumASRResponse, type GradiumASRSetupMessage, type GradiumAudioMessage, type GradiumAudioResponse, type GradiumEndTextResponse, type GradiumEosMessage, type GradiumEosResponse, type GradiumErrorResponse, type GradiumFlushMessage, type GradiumFlushedResponse, type GradiumInputFormat, type GradiumJsonConfig, type GradiumOutputFormat, type GradiumReadyResponse, type GradiumRegion, type GradiumResponse, GradiumSTT, type GradiumSTTOptions, type GradiumSetupMessage, type GradiumStepResponse, GradiumTTS, type GradiumTTSOptions, type GradiumTextMessage, type GradiumTextResponse };
215
+ export { DEFAULT_INPUT_FORMAT, DEFAULT_LANGUAGE, DEFAULT_MODEL_NAME, DEFAULT_OUTPUT_FORMAT, DEFAULT_REGION, DEFAULT_SAMPLE_RATE, type GradiumASRJsonConfig, type GradiumASRResponse, type GradiumASRSetupMessage, type GradiumAudioMessage, type GradiumAudioResponse, type GradiumEndTextResponse, type GradiumEosMessage, type GradiumEosResponse, type GradiumErrorResponse, type GradiumFlushMessage, type GradiumFlushedResponse, type GradiumInputFormat, type GradiumJsonConfig, type GradiumOutputFormat, type GradiumReadyResponse, type GradiumRegion, type GradiumResponse, GradiumSTT, type GradiumSTTOptions, type GradiumSampleRate, type GradiumSetupMessage, type GradiumStepResponse, GradiumTTS, type GradiumTTSOptions, type GradiumTextMessage, type GradiumTextResponse, getOutputFormat, getSampleRate };
package/dist/index.js CHANGED
@@ -25,8 +25,11 @@ __export(index_exports, {
25
25
  DEFAULT_MODEL_NAME: () => DEFAULT_MODEL_NAME,
26
26
  DEFAULT_OUTPUT_FORMAT: () => DEFAULT_OUTPUT_FORMAT,
27
27
  DEFAULT_REGION: () => DEFAULT_REGION,
28
+ DEFAULT_SAMPLE_RATE: () => DEFAULT_SAMPLE_RATE,
28
29
  GradiumSTT: () => GradiumSTT,
29
- GradiumTTS: () => GradiumTTS
30
+ GradiumTTS: () => GradiumTTS,
31
+ getOutputFormat: () => getOutputFormat,
32
+ getSampleRate: () => getSampleRate
30
33
  });
31
34
  module.exports = __toCommonJS(index_exports);
32
35
 
@@ -35,10 +38,28 @@ var import_server = require("@micdrop/server");
35
38
 
36
39
  // src/types.ts
37
40
  var DEFAULT_MODEL_NAME = "default";
38
- var DEFAULT_OUTPUT_FORMAT = "pcm_16000";
41
+ var DEFAULT_SAMPLE_RATE = 48e3;
42
+ var DEFAULT_OUTPUT_FORMAT = "pcm";
39
43
  var DEFAULT_INPUT_FORMAT = "pcm_16000";
40
44
  var DEFAULT_REGION = "eu";
41
45
  var DEFAULT_LANGUAGE = "any";
46
+ var PCM_FORMATS = {
47
+ 8e3: "pcm_8000",
48
+ 16e3: "pcm_16000",
49
+ 24e3: "pcm_24000",
50
+ 48e3: "pcm"
51
+ // Gradium names its 48 kHz format without a rate
52
+ };
53
+ function getOutputFormat(options) {
54
+ if (options.sampleRate) return PCM_FORMATS[options.sampleRate];
55
+ return options.outputFormat ?? PCM_FORMATS[DEFAULT_SAMPLE_RATE];
56
+ }
57
+ function getSampleRate(options) {
58
+ const format = getOutputFormat(options);
59
+ if (format === "pcm") return 48e3;
60
+ const match = /^pcm_(\d+)$/.exec(format);
61
+ return match ? Number(match[1]) : DEFAULT_SAMPLE_RATE;
62
+ }
42
63
 
43
64
  // src/GradiumSTT.ts
44
65
  var DEFAULT_TRANSCRIPTION_TIMEOUT = 4e3;
@@ -260,6 +281,7 @@ function pcmSampleRate(format) {
260
281
 
261
282
  // src/GradiumTTS.ts
262
283
  var import_server2 = require("@micdrop/server");
284
+ var VOICES_PAGE_SIZE = 500;
263
285
  var GradiumTTS = class extends import_server2.TTS {
264
286
  constructor(options) {
265
287
  super();
@@ -328,6 +350,27 @@ var GradiumTTS = class extends import_server2.TTS {
328
350
  this.socket.on("Failed", this.onFailed);
329
351
  this.socket.connect();
330
352
  }
353
+ /**
354
+ * Lists the voices the account can speak with: its own, then the Gradium
355
+ * catalog, over a thousand voices.
356
+ *
357
+ * `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.
358
+ *
359
+ * The list is fetched once and shared by every call of the process for a
360
+ * day. `refresh` fetches it again, for a voice added a moment ago.
361
+ */
362
+ static listVoices(options) {
363
+ const region = options.region ?? DEFAULT_REGION;
364
+ return (0, import_server2.cachedVoices)(
365
+ ["gradium", region],
366
+ options.apiKey,
367
+ () => fetchVoices(options.apiKey, region),
368
+ options.refresh
369
+ ).then((voices) => (0, import_server2.voicesForLanguage)(voices, options.language));
370
+ }
371
+ get sampleRate() {
372
+ return getSampleRate(this.options);
373
+ }
331
374
  speak(textStream) {
332
375
  const generation = ++this.generation;
333
376
  let request;
@@ -396,7 +439,7 @@ var GradiumTTS = class extends import_server2.TTS {
396
439
  type: "setup",
397
440
  voice_id: this.options.voiceId,
398
441
  model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,
399
- output_format: this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT,
442
+ output_format: getOutputFormat(this.options),
400
443
  close_ws_on_eos: false,
401
444
  json_config: this.options.jsonConfig,
402
445
  client_req_id: clientReqId
@@ -434,6 +477,37 @@ var GradiumTTS = class extends import_server2.TTS {
434
477
  this.emit("Audio", chunk);
435
478
  }
436
479
  };
480
+ async function fetchVoices(apiKey, region) {
481
+ const voices = [];
482
+ for (let skip = 0; ; skip += VOICES_PAGE_SIZE) {
483
+ const params = new URLSearchParams({
484
+ include_catalog: "true",
485
+ limit: String(VOICES_PAGE_SIZE),
486
+ skip: String(skip)
487
+ });
488
+ const response = await fetch(
489
+ `https://${region}.api.gradium.ai/api/voices/?${params}`,
490
+ { headers: { "x-api-key": apiKey } }
491
+ );
492
+ if (!response.ok) throw new Error(`Gradium answered ${response.status}`);
493
+ const page = await response.json();
494
+ voices.push(...page.map(toMicdropVoice));
495
+ if (page.length < VOICES_PAGE_SIZE) break;
496
+ }
497
+ return voices;
498
+ }
499
+ function toMicdropVoice(voice) {
500
+ const [language, region] = (voice.locale || voice.language || "").split("-");
501
+ const locale = region ? `${language}-${region.toUpperCase()}` : language;
502
+ return {
503
+ id: voice.uid,
504
+ name: voice.name,
505
+ languages: locale ? [locale] : void 0,
506
+ gender: (0, import_server2.voiceGender)(voice.gender),
507
+ description: voice.description || void 0,
508
+ custom: voice.is_catalog === false
509
+ };
510
+ }
437
511
  // Annotate the CommonJS export names for ESM import in node:
438
512
  0 && (module.exports = {
439
513
  DEFAULT_INPUT_FORMAT,
@@ -441,7 +515,10 @@ var GradiumTTS = class extends import_server2.TTS {
441
515
  DEFAULT_MODEL_NAME,
442
516
  DEFAULT_OUTPUT_FORMAT,
443
517
  DEFAULT_REGION,
518
+ DEFAULT_SAMPLE_RATE,
444
519
  GradiumSTT,
445
- GradiumTTS
520
+ GradiumTTS,
521
+ getOutputFormat,
522
+ getSampleRate
446
523
  });
447
524
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/GradiumSTT.ts","../src/types.ts","../src/GradiumTTS.ts"],"sourcesContent":["export * from './GradiumSTT'\nexport * from './GradiumTTS'\nexport * from './types'\n","import {\n ReconnectingSocket,\n ReconnectingSocketHandshake,\n STT,\n} from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n DEFAULT_INPUT_FORMAT,\n DEFAULT_LANGUAGE,\n DEFAULT_MODEL_NAME,\n DEFAULT_REGION,\n GradiumASRJsonConfig,\n GradiumASRResponse,\n GradiumASRSetupMessage,\n GradiumInputFormat,\n GradiumSTTOptions,\n} from './types'\n\n/**\n * Gradium Real-time STT (ASR)\n *\n * @see https://docs.gradium.ai/guides/speech-to-text\n */\n\nconst DEFAULT_TRANSCRIPTION_TIMEOUT = 4000\n// Each frame of the transcription model lasts 80 ms\nconst FRAME_DURATION_MS = 80\nconst DEFAULT_DELAY_IN_FRAMES = 10\n\n/**\n * One utterance, from its first audio chunk to its transcript.\n *\n * It is capturing while its audio stream runs, then flushing once the stream\n * ended and the flush went out, and done once its transcript is emitted, by\n * the matching `flushed` or by its own timeout.\n */\ninterface Utterance {\n /** Audio sent so far, sent again whole on a new connection */\n audio: Buffer[]\n /** Text segments received for this utterance */\n transcript: string\n /** Set when the audio stream ends and the flush goes out */\n flushId?: number\n timeout?: ReturnType<typeof setTimeout>\n /** The transcript went out, on `flushed` or on timeout */\n done: boolean\n}\n\nexport class GradiumSTT extends STT {\n private socket: ReconnectingSocket\n // Utterances in the order their audio went out. The server answers in that\n // order too, so every text segment belongs to the first one not flushed yet\n private utterances: Utterance[] = []\n private flushId = 0\n private delayInFrames = DEFAULT_DELAY_IN_FRAMES // Given by the server when ready\n\n constructor(private options: GradiumSTTOptions) {\n super()\n\n const region = options.region ?? DEFAULT_REGION\n this.socket = new ReconnectingSocket({\n url: `wss://${region}.api.gradium.ai/api/speech/asr`,\n clientOptions: { headers: { 'x-api-key': options.apiKey } },\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n // The server opens the connection before checking the setup, so the\n // connection only counts as open once it answers ready\n handshake: this.handshake,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n transcribe(audioStream: Readable) {\n let utterance: Utterance | undefined\n\n // Read audio stream and send to Gradium\n audioStream.on('data', (chunk: Buffer) => {\n // The utterance starts with its first chunk. Voice detection opens a\n // stream on any noise and many of them never carry a word, so they leave\n // no utterance behind.\n if (!utterance) {\n utterance = { audio: [], transcript: '', done: false }\n this.utterances.push(utterance)\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n }\n if (utterance.done) return\n utterance.audio.push(chunk)\n // Without a connection the audio waits, and goes out whole on open\n if (this.sendAudioChunk(chunk)) {\n this.log(`Sent audio chunk (${chunk.byteLength} bytes)`)\n }\n })\n\n // Handle stream end: flush buffered audio so the server finalizes the\n // pending segments and confirms with a matching \"flushed\" message.\n audioStream.on('end', () => {\n if (!utterance || utterance.done) return\n const current = utterance\n current.audio.push(this.trailingSilence())\n current.flushId = ++this.flushId\n if (this.socket.isOpen) this.sendEnd(current)\n\n // Timeout transcription if no \"flushed\" confirmation is received\n current.timeout = setTimeout(() => {\n current.timeout = undefined\n this.log('Transcription timeout')\n this.emitTranscript(current)\n }, this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT)\n })\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.utterances.forEach((utterance) => clearTimeout(utterance.timeout))\n this.utterances = []\n }\n\n private buildJsonConfig(): GradiumASRJsonConfig {\n const config: GradiumASRJsonConfig = { ...this.options.jsonConfig }\n if (config.language === undefined) {\n config.language = this.options.language ?? DEFAULT_LANGUAGE\n }\n return config\n }\n\n /** Sends the setup, and waits for the server to accept it */\n private handshake: ReconnectingSocketHandshake = {\n start: (socket) => {\n socket.send(\n JSON.stringify({\n type: 'setup',\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n input_format: this.options.inputFormat ?? DEFAULT_INPUT_FORMAT,\n json_config: this.buildJsonConfig(),\n } satisfies GradiumASRSetupMessage)\n )\n this.log('Sent setup')\n },\n read: (data) => {\n let message: GradiumASRResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return false\n }\n if (message.type === 'error') {\n throw new Error(`Setup refused: ${message.message}`)\n }\n if (message.type !== 'ready') return false\n this.log('Server ready')\n if (typeof message.delay_in_frames === 'number') {\n this.delayInFrames = message.delay_in_frames\n }\n return true\n },\n }\n\n private sendAudioChunk(chunk: Buffer) {\n return this.socket.send(\n JSON.stringify({\n type: 'audio',\n audio: chunk.toString('base64'),\n })\n )\n }\n\n /**\n * Silence as long as the transcription lags behind, to follow the speech.\n *\n * The model writes each word a few frames after hearing it, and a flush only\n * processes the audio already sent. With nothing after the last word, the\n * server confirms the flush before writing that word, which then opens the\n * next transcript.\n */\n private trailingSilence(): Buffer {\n const sampleRate = pcmSampleRate(\n this.options.inputFormat ?? DEFAULT_INPUT_FORMAT\n )\n // Silence is a run of zeros in raw PCM only\n if (!sampleRate) return Buffer.alloc(0)\n const durationMs = this.delayInFrames * FRAME_DURATION_MS\n const samples = Math.round((sampleRate * durationMs) / 1000)\n return Buffer.alloc(samples * 2)\n }\n\n /** Sends the trailing silence, already the last audio chunk, and the flush */\n private sendEnd(utterance: Utterance) {\n const silence = utterance.audio[utterance.audio.length - 1]\n if (silence.length) this.sendAudioChunk(silence)\n this.socket.send(\n JSON.stringify({ type: 'flush', flush_id: utterance.flushId })\n )\n this.log(`Sent flush (flush_id=${utterance.flushId})`)\n }\n\n private emitTranscript(utterance: Utterance) {\n if (utterance.done) return\n utterance.done = true\n clearTimeout(utterance.timeout)\n utterance.timeout = undefined\n // Its audio is no longer needed, but the utterance stays in the queue until\n // its flush returns, so the late segments of a timed out utterance don't\n // land in the next one\n utterance.audio = []\n const transcript = utterance.transcript.replace(/\\s+/g, ' ').trim()\n this.log(`Received transcript: \"${transcript}\"`)\n this.emit('Transcript', transcript)\n }\n\n // A new connection knows nothing of the utterances under way: the audio read\n // before the first connection opened, or lost with the previous one, goes out\n // again whole, and the server transcribes it from the start\n private onOpen = () => {\n // A new session never confirms the flushes of the previous one\n this.utterances = this.utterances.filter((utterance) => !utterance.done)\n if (this.utterances.length === 0) return\n this.log('Sending audio chunks again')\n for (const utterance of this.utterances) {\n utterance.transcript = ''\n if (utterance.flushId === undefined) {\n utterance.audio.forEach((chunk) => this.sendAudioChunk(chunk))\n } else {\n // The trailing silence goes out with the flush\n utterance.audio\n .slice(0, -1)\n .forEach((chunk) => this.sendAudioChunk(chunk))\n this.sendEnd(utterance)\n }\n }\n }\n\n private onMessage = (data: RawData) => {\n let message: GradiumASRResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n switch (message.type) {\n case 'text': {\n const utterance = this.utterances[0]\n if (typeof message.text === 'string' && utterance) {\n utterance.transcript +=\n (utterance.transcript ? ' ' : '') + message.text\n this.log(`Received text segment: \"${message.text}\"`)\n }\n break\n }\n\n case 'flushed': {\n // Every segment of the flushed utterance arrived. Those before it in\n // the queue are flushed too, since the server answers in order.\n const index = this.utterances.findIndex(\n (utterance) => utterance.flushId === message.flush_id\n )\n if (index === -1) break\n const flushed = this.utterances.splice(0, index + 1)\n flushed.forEach((utterance) => this.emitTranscript(utterance))\n break\n }\n\n case 'end_text':\n // Segment finalized, nothing to accumulate (text already received)\n break\n\n case 'step':\n // Voice activity detection frames, ignored\n break\n\n case 'end_of_stream':\n this.log('End of stream')\n break\n\n case 'error':\n this.log('Error:', message.message, message.code)\n break\n }\n }\n\n private onFailed = () => {\n // The audio of every utterance still waiting for its transcript, for a\n // fallback to transcribe it instead\n const pending = this.utterances.filter((utterance) => !utterance.done)\n this.utterances.forEach((utterance) => clearTimeout(utterance.timeout))\n this.utterances = []\n this.emit(\n 'Failed',\n pending.flatMap((utterance) => utterance.audio)\n )\n }\n}\n\n/** Sample rate of a raw PCM input format, undefined for an encoded one */\nfunction pcmSampleRate(format: GradiumInputFormat): number | undefined {\n if (format === 'pcm') return 24000\n const match = format.match(/^pcm_(\\d+)$/)\n return match ? Number(match[1]) : undefined\n}\n","export type GradiumOutputFormat =\n | 'pcm'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n\nexport type GradiumInputFormat =\n | 'pcm'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n | 'pcm_48000'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n\nexport type GradiumRegion = 'eu' | 'us'\n\nexport interface GradiumTTSOptions {\n apiKey: string\n voiceId: string\n modelName?: string\n outputFormat?: GradiumOutputFormat\n region?: GradiumRegion\n jsonConfig?: GradiumJsonConfig\n connectionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\nexport interface GradiumJsonConfig {\n padding_bonus?: number // Speed control: -4.0 to 4.0\n temp?: number // Temperature: 0 to 1.4, default 0.7\n cfg_coef?: number // Voice similarity: 1.0 to 4.0, default 2.0\n}\n\nexport const DEFAULT_MODEL_NAME = 'default'\nexport const DEFAULT_OUTPUT_FORMAT: GradiumOutputFormat = 'pcm_16000'\nexport const DEFAULT_INPUT_FORMAT: GradiumInputFormat = 'pcm_16000'\nexport const DEFAULT_REGION: GradiumRegion = 'eu'\n// Gradium refuses a transcription without a language, \"any\" detects it\nexport const DEFAULT_LANGUAGE = 'any'\n\n// Speech-to-text (ASR) types\n\nexport interface GradiumASRJsonConfig {\n language?: string\n target_language?: string\n delay_in_frames?: number // 0 to 80, each frame = 80ms\n temp?: number\n padding_bonus?: number\n}\n\nexport interface GradiumSTTOptions {\n apiKey: string\n modelName?: string\n inputFormat?: GradiumInputFormat\n language?: string\n region?: GradiumRegion\n jsonConfig?: GradiumASRJsonConfig\n connectionTimeout?: number\n transcriptionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\n// Client -> Server ASR messages\n\nexport interface GradiumASRSetupMessage {\n type: 'setup'\n model_name: string\n input_format: string\n json_config?: GradiumASRJsonConfig\n}\n\nexport interface GradiumAudioMessage {\n type: 'audio'\n audio: string // Base64 encoded audio data\n}\n\nexport interface GradiumFlushMessage {\n type: 'flush'\n flush_id: number\n}\n\n// Server -> Client ASR messages\n\nexport type GradiumASRResponse =\n | GradiumReadyResponse\n | GradiumTextResponse\n | GradiumEndTextResponse\n | GradiumStepResponse\n | GradiumFlushedResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumTextResponse {\n type: 'text'\n text: string\n start_s?: number\n stream_id?: number\n}\n\nexport interface GradiumEndTextResponse {\n type: 'end_text'\n stop_s?: number\n stream_id?: number\n}\n\nexport interface GradiumStepResponse {\n type: 'step'\n vad?: Array<{ horizon_s: number; inactivity_prob: number }>\n step_idx?: number\n step_duration_s?: number\n total_duration_s?: number\n}\n\nexport interface GradiumFlushedResponse {\n type: 'flushed'\n flush_id: number\n}\n\n// Client -> Server messages\n\nexport interface GradiumSetupMessage {\n type: 'setup'\n voice_id: string\n model_name: string\n output_format: string\n close_ws_on_eos?: boolean\n json_config?: GradiumJsonConfig\n client_req_id?: string\n}\n\nexport interface GradiumTextMessage {\n type: 'text'\n text: string\n client_req_id?: string\n}\n\nexport interface GradiumEosMessage {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\n// Server -> Client messages\n\nexport type GradiumResponse =\n | GradiumReadyResponse\n | GradiumAudioResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumReadyResponse {\n type: 'ready'\n request_id?: string\n delay_in_frames?: number // Frames of 80ms the transcription lags behind the audio\n}\n\nexport interface GradiumAudioResponse {\n type: 'audio'\n audio: string // Base64 encoded PCM data\n client_req_id?: string\n}\n\nexport interface GradiumEosResponse {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\nexport interface GradiumErrorResponse {\n type: 'error'\n message: string\n code?: number\n}\n","import { ReconnectingSocket, TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n DEFAULT_MODEL_NAME,\n DEFAULT_OUTPUT_FORMAT,\n DEFAULT_REGION,\n GradiumAudioResponse,\n GradiumEosMessage,\n GradiumResponse,\n GradiumSetupMessage,\n GradiumTextMessage,\n GradiumTTSOptions,\n} from './types'\n\n// API Reference: https://gradium.ai/api_docs.html\n\n/**\n * The utterance Gradium is synthesizing. While there is one the TTS is\n * speaking, and when there is none it is idle: every way out of a request\n * (end of stream, server error, cancel, text stream error, give up) clears it,\n * so nothing about it outlives it.\n */\ninterface GradiumRequest {\n id: string\n /** Complete words received so far, sent again whole on a new connection */\n words: string\n /** Start of a word, held until a space completes it or the stream ends */\n partial: string\n /** The text stream ended, so Gradium was told no more text follows */\n ended: boolean\n}\n\nexport class GradiumTTS extends TTS {\n private socket: ReconnectingSocket\n // Numbers the audio requests, and only moves when there is something to say\n private counter = 0\n // Bumped by every speak() and every cancel(), so a request claimed late can\n // tell whether it is still the one that should be heard\n private generation = 0\n private request?: GradiumRequest\n\n constructor(private readonly options: GradiumTTSOptions) {\n super()\n\n const region = options.region ?? DEFAULT_REGION\n this.socket = new ReconnectingSocket({\n url: `wss://${region}.api.gradium.ai/api/speech/tts`,\n clientOptions: { headers: { 'x-api-key': options.apiKey } },\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let request: GradiumRequest | undefined\n\n // Claiming the request is deferred until there is something to say.\n //\n // Taking the next id right away would silence the utterance still playing,\n // because incoming audio is matched against the current id and anything\n // older is dropped. A stream that never carries a word, which is what an\n // answer skipped by a tool or by onBeforeAnswer hands over, would then cut\n // the assistant off mid-sentence for nothing.\n const claimRequest = () => {\n if (request) return request\n // Cancelled, or superseded by another speak(), before the first word\n if (this.generation !== generation) return\n request = {\n id: (++this.counter).toString(),\n words: '',\n partial: '',\n ended: false,\n }\n this.request = request\n // Each speak() is its own multiplexed request, opened by a setup stamped\n // with its client_req_id, so the server binds the voice to it.\n // Multiplexing is what lets the connection serve several utterances\n // (sequential mode only works for the first request on a socket). Without\n // a connection, the setup goes out on open.\n this.sendSetup(request.id)\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n return request\n }\n\n textStream.on('data', (chunk: Buffer) => {\n const request = claimRequest()\n if (!request || this.request !== request) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n\n // Only complete words go out, up to the last space\n const spaceIndex = text.lastIndexOf(' ')\n if (spaceIndex === -1) {\n request.partial += text\n return\n }\n const words = request.partial + text.slice(0, spaceIndex + 1)\n request.partial = text.slice(spaceIndex + 1)\n request.words += words\n // Without a connection the words wait, and go out whole on open\n this.sendTranscript(words, request.id)\n })\n\n textStream.on('error', (error) => {\n if (!request || this.request !== request) return\n this.log('Error in text stream, cancelling the audio', error)\n this.stopRequest()\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is no request to close and no audio to\n // wait for\n if (!request || this.request !== request) return\n request.ended = true\n this.sendEnd(request)\n })\n }\n\n /**\n * Stops the current utterance. Gradium can only stop a synthesis by closing\n * its connection, so a new one opens right away.\n */\n cancel() {\n this.generation++\n if (!this.request) return\n this.log('Cancel')\n this.stopRequest()\n }\n\n destroy() {\n // Destroyed first, so the cancel() that super.destroy() runs opens no new\n // connection\n this.socket.destroy()\n super.destroy()\n this.request = undefined\n }\n\n /** Forgets the current request, and closes the connection that carries it */\n private stopRequest() {\n this.request = undefined\n // Free of charge on the retry budget, and a speak() that follows lands its\n // setup and text on the new connection\n this.socket.restart()\n }\n\n private sendSetup(clientReqId: string) {\n const sent = this.socket.send(\n JSON.stringify({\n type: 'setup',\n voice_id: this.options.voiceId,\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n output_format: this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT,\n close_ws_on_eos: false,\n json_config: this.options.jsonConfig,\n client_req_id: clientReqId,\n } satisfies GradiumSetupMessage)\n )\n if (sent) this.log(`Sent setup (client_req_id=${clientReqId})`)\n }\n\n private sendTranscript(text: string, clientReqId: string) {\n const sent = this.socket.send(\n JSON.stringify({\n type: 'text',\n text,\n client_req_id: clientReqId,\n } satisfies GradiumTextMessage)\n )\n if (sent) this.log(`Sent transcript: \"${text}\"`)\n }\n\n /** Sends the last partial word, then the end of the text */\n private sendEnd(request: GradiumRequest) {\n if (request.partial.trim()) {\n this.sendTranscript(request.partial, request.id)\n }\n const sent = this.socket.send(\n JSON.stringify({\n type: 'end_of_stream',\n client_req_id: request.id,\n } satisfies GradiumEosMessage)\n )\n if (sent) this.log('Sent end_of_stream')\n }\n\n // A new connection has no session for the request under way: its setup and\n // its words go out again, the setup first, with the same client_req_id\n private onOpen = () => {\n const request = this.request\n if (!request) return\n this.log('Sending the request again')\n this.sendSetup(request.id)\n if (request.words) this.sendTranscript(request.words, request.id)\n if (request.ended) this.sendEnd(request)\n }\n\n private onMessage = (data: RawData) => {\n let message: GradiumResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n switch (message.type) {\n case 'ready':\n // The server emits one ready per setup, informational only\n this.log('Server ready', message.request_id ?? '')\n break\n\n case 'audio':\n this.processAudioMessage(message)\n break\n\n case 'end_of_stream':\n // Ignore if from a previous request\n if (\n !this.request ||\n (message.client_req_id !== undefined &&\n message.client_req_id !== this.request.id)\n )\n return\n this.log('Audio ended')\n this.request = undefined\n break\n\n case 'error':\n // The connection carries one request at a time, since cancel() closes\n // it, so the error is about the current one, which gets no more audio.\n // Its text is handed over, so a FallbackTTS can speak it instead, and\n // a fresh connection keeps the next request clear of it.\n this.log('Error:', message.message, message.code)\n if (!this.request) break\n this.onFailed()\n this.socket.restart()\n break\n }\n }\n\n private processAudioMessage(message: GradiumAudioResponse) {\n // Ignore messages from previous requests\n if (!this.request || message.client_req_id !== this.request.id) return\n\n const chunk = Buffer.from(message.audio, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n }\n\n private onFailed = () => {\n const request = this.request\n this.request = undefined\n const text = request ? request.words + request.partial : ''\n this.emit('Failed', text ? [text] : [])\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAIO;;;ACqCA,IAAM,qBAAqB;AAC3B,IAAM,wBAA6C;AACnD,IAAM,uBAA2C;AACjD,IAAM,iBAAgC;AAEtC,IAAM,mBAAmB;;;ADrBhC,IAAM,gCAAgC;AAEtC,IAAM,oBAAoB;AAC1B,IAAM,0BAA0B;AAqBzB,IAAM,aAAN,cAAyB,kBAAI;AAAA;AAAA,EAQlC,YAAoB,SAA4B;AAC9C,UAAM;AADY;AAJpB;AAAA;AAAA,SAAQ,aAA0B,CAAC;AACnC,SAAQ,UAAU;AAClB,SAAQ,gBAAgB;AA+ExB;AAAA,SAAQ,YAAyC;AAAA,MAC/C,OAAO,CAAC,WAAW;AACjB,eAAO;AAAA,UACL,KAAK,UAAU;AAAA,YACb,MAAM;AAAA,YACN,YAAY,KAAK,QAAQ,aAAa;AAAA,YACtC,cAAc,KAAK,QAAQ,eAAe;AAAA,YAC1C,aAAa,KAAK,gBAAgB;AAAA,UACpC,CAAkC;AAAA,QACpC;AACA,aAAK,IAAI,YAAY;AAAA,MACvB;AAAA,MACA,MAAM,CAAC,SAAS;AACd,YAAI;AACJ,YAAI;AACF,oBAAU,KAAK,MAAM,KAAK,SAAS,CAAC;AAAA,QACtC,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Q,QAAQ,KAAK,GAAG;AAC1B,WAAK,eAAe,QAAQ,SAAS,QAAQ,EAAE;AAAA,IACjD;AACA,UAAM,OAAO,KAAK,OAAO;AAAA,MACvB,KAAK,UAAU;AAAA,QACb,MAAM;AAAA,QACN,eAAe,QAAQ;AAAA,MACzB,CAA6B;AAAA,IAC/B;AACA,QAAI,KAAM,MAAK,IAAI,oBAAoB;AAAA,EACzC;AAAA,EAyDQ,oBAAoB,SAA+B;AAEzD,QAAI,CAAC,KAAK,WAAW,QAAQ,kBAAkB,KAAK,QAAQ,GAAI;AAEhE,UAAM,QAAQ,OAAO,KAAK,QAAQ,OAAO,QAAQ;AACjD,SAAK,IAAI,yBAAyB,MAAM,MAAM,SAAS;AACvD,SAAK,KAAK,SAAS,KAAK;AAAA,EAC1B;AAQF;","names":["import_server","request"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/GradiumSTT.ts","../src/types.ts","../src/GradiumTTS.ts"],"sourcesContent":["export * from './GradiumSTT'\nexport * from './GradiumTTS'\nexport * from './types'\n","import {\n ReconnectingSocket,\n ReconnectingSocketHandshake,\n STT,\n} from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n DEFAULT_INPUT_FORMAT,\n DEFAULT_LANGUAGE,\n DEFAULT_MODEL_NAME,\n DEFAULT_REGION,\n GradiumASRJsonConfig,\n GradiumASRResponse,\n GradiumASRSetupMessage,\n GradiumInputFormat,\n GradiumSTTOptions,\n} from './types'\n\n/**\n * Gradium Real-time STT (ASR)\n *\n * @see https://docs.gradium.ai/guides/speech-to-text\n */\n\nconst DEFAULT_TRANSCRIPTION_TIMEOUT = 4000\n// Each frame of the transcription model lasts 80 ms\nconst FRAME_DURATION_MS = 80\nconst DEFAULT_DELAY_IN_FRAMES = 10\n\n/**\n * One utterance, from its first audio chunk to its transcript.\n *\n * It is capturing while its audio stream runs, then flushing once the stream\n * ended and the flush went out, and done once its transcript is emitted, by\n * the matching `flushed` or by its own timeout.\n */\ninterface Utterance {\n /** Audio sent so far, sent again whole on a new connection */\n audio: Buffer[]\n /** Text segments received for this utterance */\n transcript: string\n /** Set when the audio stream ends and the flush goes out */\n flushId?: number\n timeout?: ReturnType<typeof setTimeout>\n /** The transcript went out, on `flushed` or on timeout */\n done: boolean\n}\n\nexport class GradiumSTT extends STT {\n private socket: ReconnectingSocket\n // Utterances in the order their audio went out. The server answers in that\n // order too, so every text segment belongs to the first one not flushed yet\n private utterances: Utterance[] = []\n private flushId = 0\n private delayInFrames = DEFAULT_DELAY_IN_FRAMES // Given by the server when ready\n\n constructor(private options: GradiumSTTOptions) {\n super()\n\n const region = options.region ?? DEFAULT_REGION\n this.socket = new ReconnectingSocket({\n url: `wss://${region}.api.gradium.ai/api/speech/asr`,\n clientOptions: { headers: { 'x-api-key': options.apiKey } },\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n // The server opens the connection before checking the setup, so the\n // connection only counts as open once it answers ready\n handshake: this.handshake,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n transcribe(audioStream: Readable) {\n let utterance: Utterance | undefined\n\n // Read audio stream and send to Gradium\n audioStream.on('data', (chunk: Buffer) => {\n // The utterance starts with its first chunk. Voice detection opens a\n // stream on any noise and many of them never carry a word, so they leave\n // no utterance behind.\n if (!utterance) {\n utterance = { audio: [], transcript: '', done: false }\n this.utterances.push(utterance)\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n }\n if (utterance.done) return\n utterance.audio.push(chunk)\n // Without a connection the audio waits, and goes out whole on open\n if (this.sendAudioChunk(chunk)) {\n this.log(`Sent audio chunk (${chunk.byteLength} bytes)`)\n }\n })\n\n // Handle stream end: flush buffered audio so the server finalizes the\n // pending segments and confirms with a matching \"flushed\" message.\n audioStream.on('end', () => {\n if (!utterance || utterance.done) return\n const current = utterance\n current.audio.push(this.trailingSilence())\n current.flushId = ++this.flushId\n if (this.socket.isOpen) this.sendEnd(current)\n\n // Timeout transcription if no \"flushed\" confirmation is received\n current.timeout = setTimeout(() => {\n current.timeout = undefined\n this.log('Transcription timeout')\n this.emitTranscript(current)\n }, this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT)\n })\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.utterances.forEach((utterance) => clearTimeout(utterance.timeout))\n this.utterances = []\n }\n\n private buildJsonConfig(): GradiumASRJsonConfig {\n const config: GradiumASRJsonConfig = { ...this.options.jsonConfig }\n if (config.language === undefined) {\n config.language = this.options.language ?? DEFAULT_LANGUAGE\n }\n return config\n }\n\n /** Sends the setup, and waits for the server to accept it */\n private handshake: ReconnectingSocketHandshake = {\n start: (socket) => {\n socket.send(\n JSON.stringify({\n type: 'setup',\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n input_format: this.options.inputFormat ?? DEFAULT_INPUT_FORMAT,\n json_config: this.buildJsonConfig(),\n } satisfies GradiumASRSetupMessage)\n )\n this.log('Sent setup')\n },\n read: (data) => {\n let message: GradiumASRResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return false\n }\n if (message.type === 'error') {\n throw new Error(`Setup refused: ${message.message}`)\n }\n if (message.type !== 'ready') return false\n this.log('Server ready')\n if (typeof message.delay_in_frames === 'number') {\n this.delayInFrames = message.delay_in_frames\n }\n return true\n },\n }\n\n private sendAudioChunk(chunk: Buffer) {\n return this.socket.send(\n JSON.stringify({\n type: 'audio',\n audio: chunk.toString('base64'),\n })\n )\n }\n\n /**\n * Silence as long as the transcription lags behind, to follow the speech.\n *\n * The model writes each word a few frames after hearing it, and a flush only\n * processes the audio already sent. With nothing after the last word, the\n * server confirms the flush before writing that word, which then opens the\n * next transcript.\n */\n private trailingSilence(): Buffer {\n const sampleRate = pcmSampleRate(\n this.options.inputFormat ?? DEFAULT_INPUT_FORMAT\n )\n // Silence is a run of zeros in raw PCM only\n if (!sampleRate) return Buffer.alloc(0)\n const durationMs = this.delayInFrames * FRAME_DURATION_MS\n const samples = Math.round((sampleRate * durationMs) / 1000)\n return Buffer.alloc(samples * 2)\n }\n\n /** Sends the trailing silence, already the last audio chunk, and the flush */\n private sendEnd(utterance: Utterance) {\n const silence = utterance.audio[utterance.audio.length - 1]\n if (silence.length) this.sendAudioChunk(silence)\n this.socket.send(\n JSON.stringify({ type: 'flush', flush_id: utterance.flushId })\n )\n this.log(`Sent flush (flush_id=${utterance.flushId})`)\n }\n\n private emitTranscript(utterance: Utterance) {\n if (utterance.done) return\n utterance.done = true\n clearTimeout(utterance.timeout)\n utterance.timeout = undefined\n // Its audio is no longer needed, but the utterance stays in the queue until\n // its flush returns, so the late segments of a timed out utterance don't\n // land in the next one\n utterance.audio = []\n const transcript = utterance.transcript.replace(/\\s+/g, ' ').trim()\n this.log(`Received transcript: \"${transcript}\"`)\n this.emit('Transcript', transcript)\n }\n\n // A new connection knows nothing of the utterances under way: the audio read\n // before the first connection opened, or lost with the previous one, goes out\n // again whole, and the server transcribes it from the start\n private onOpen = () => {\n // A new session never confirms the flushes of the previous one\n this.utterances = this.utterances.filter((utterance) => !utterance.done)\n if (this.utterances.length === 0) return\n this.log('Sending audio chunks again')\n for (const utterance of this.utterances) {\n utterance.transcript = ''\n if (utterance.flushId === undefined) {\n utterance.audio.forEach((chunk) => this.sendAudioChunk(chunk))\n } else {\n // The trailing silence goes out with the flush\n utterance.audio\n .slice(0, -1)\n .forEach((chunk) => this.sendAudioChunk(chunk))\n this.sendEnd(utterance)\n }\n }\n }\n\n private onMessage = (data: RawData) => {\n let message: GradiumASRResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n switch (message.type) {\n case 'text': {\n const utterance = this.utterances[0]\n if (typeof message.text === 'string' && utterance) {\n utterance.transcript +=\n (utterance.transcript ? ' ' : '') + message.text\n this.log(`Received text segment: \"${message.text}\"`)\n }\n break\n }\n\n case 'flushed': {\n // Every segment of the flushed utterance arrived. Those before it in\n // the queue are flushed too, since the server answers in order.\n const index = this.utterances.findIndex(\n (utterance) => utterance.flushId === message.flush_id\n )\n if (index === -1) break\n const flushed = this.utterances.splice(0, index + 1)\n flushed.forEach((utterance) => this.emitTranscript(utterance))\n break\n }\n\n case 'end_text':\n // Segment finalized, nothing to accumulate (text already received)\n break\n\n case 'step':\n // Voice activity detection frames, ignored\n break\n\n case 'end_of_stream':\n this.log('End of stream')\n break\n\n case 'error':\n this.log('Error:', message.message, message.code)\n break\n }\n }\n\n private onFailed = () => {\n // The audio of every utterance still waiting for its transcript, for a\n // fallback to transcribe it instead\n const pending = this.utterances.filter((utterance) => !utterance.done)\n this.utterances.forEach((utterance) => clearTimeout(utterance.timeout))\n this.utterances = []\n this.emit(\n 'Failed',\n pending.flatMap((utterance) => utterance.audio)\n )\n }\n}\n\n/** Sample rate of a raw PCM input format, undefined for an encoded one */\nfunction pcmSampleRate(format: GradiumInputFormat): number | undefined {\n if (format === 'pcm') return 24000\n const match = format.match(/^pcm_(\\d+)$/)\n return match ? Number(match[1]) : undefined\n}\n","export type GradiumOutputFormat =\n | 'pcm'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n\nexport type GradiumInputFormat =\n | 'pcm'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n | 'pcm_48000'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n\nexport type GradiumRegion = 'eu' | 'us'\n\n/** Rates Gradium streams raw PCM at */\nexport type GradiumSampleRate = 8000 | 16000 | 24000 | 48000\n\nexport interface GradiumTTSOptions {\n apiKey: string\n voiceId: string\n modelName?: string\n /**\n * Rate of the audio, in Hz, 48000 by default, the highest one offered. It\n * goes to the client as is, so the voice is never resampled on the server.\n */\n sampleRate?: GradiumSampleRate\n /**\n * @deprecated Use `sampleRate`. The client only plays raw PCM, so only `pcm`\n * and the `pcm_*` formats work.\n */\n outputFormat?: GradiumOutputFormat\n region?: GradiumRegion\n jsonConfig?: GradiumJsonConfig\n connectionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\nexport interface GradiumJsonConfig {\n padding_bonus?: number // Speed control: -4.0 to 4.0\n temp?: number // Temperature: 0 to 1.4, default 0.7\n cfg_coef?: number // Voice similarity: 1.0 to 4.0, default 2.0\n}\n\nexport const DEFAULT_MODEL_NAME = 'default'\nexport const DEFAULT_SAMPLE_RATE: GradiumSampleRate = 48000\n/** @deprecated Use `DEFAULT_SAMPLE_RATE` */\nexport const DEFAULT_OUTPUT_FORMAT: GradiumOutputFormat = 'pcm'\nexport const DEFAULT_INPUT_FORMAT: GradiumInputFormat = 'pcm_16000'\nexport const DEFAULT_REGION: GradiumRegion = 'eu'\n// Gradium refuses a transcription without a language, \"any\" detects it\nexport const DEFAULT_LANGUAGE = 'any'\n\nconst PCM_FORMATS: Record<GradiumSampleRate, GradiumOutputFormat> = {\n 8000: 'pcm_8000',\n 16000: 'pcm_16000',\n 24000: 'pcm_24000',\n 48000: 'pcm', // Gradium names its 48 kHz format without a rate\n}\n\n/** The `output_format` asked of Gradium */\nexport function getOutputFormat(\n options: GradiumTTSOptions\n): GradiumOutputFormat {\n if (options.sampleRate) return PCM_FORMATS[options.sampleRate]\n return options.outputFormat ?? PCM_FORMATS[DEFAULT_SAMPLE_RATE]\n}\n\n/** Rate of the audio Gradium sends, read from its `output_format` */\nexport function getSampleRate(options: GradiumTTSOptions): number {\n const format = getOutputFormat(options)\n if (format === 'pcm') return 48000\n const match = /^pcm_(\\d+)$/.exec(format)\n return match ? Number(match[1]) : DEFAULT_SAMPLE_RATE\n}\n\n// Speech-to-text (ASR) types\n\nexport interface GradiumASRJsonConfig {\n language?: string\n target_language?: string\n delay_in_frames?: number // 0 to 80, each frame = 80ms\n temp?: number\n padding_bonus?: number\n}\n\nexport interface GradiumSTTOptions {\n apiKey: string\n modelName?: string\n inputFormat?: GradiumInputFormat\n language?: string\n region?: GradiumRegion\n jsonConfig?: GradiumASRJsonConfig\n connectionTimeout?: number\n transcriptionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\n// Client -> Server ASR messages\n\nexport interface GradiumASRSetupMessage {\n type: 'setup'\n model_name: string\n input_format: string\n json_config?: GradiumASRJsonConfig\n}\n\nexport interface GradiumAudioMessage {\n type: 'audio'\n audio: string // Base64 encoded audio data\n}\n\nexport interface GradiumFlushMessage {\n type: 'flush'\n flush_id: number\n}\n\n// Server -> Client ASR messages\n\nexport type GradiumASRResponse =\n | GradiumReadyResponse\n | GradiumTextResponse\n | GradiumEndTextResponse\n | GradiumStepResponse\n | GradiumFlushedResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumTextResponse {\n type: 'text'\n text: string\n start_s?: number\n stream_id?: number\n}\n\nexport interface GradiumEndTextResponse {\n type: 'end_text'\n stop_s?: number\n stream_id?: number\n}\n\nexport interface GradiumStepResponse {\n type: 'step'\n vad?: Array<{ horizon_s: number; inactivity_prob: number }>\n step_idx?: number\n step_duration_s?: number\n total_duration_s?: number\n}\n\nexport interface GradiumFlushedResponse {\n type: 'flushed'\n flush_id: number\n}\n\n// Client -> Server messages\n\nexport interface GradiumSetupMessage {\n type: 'setup'\n voice_id: string\n model_name: string\n output_format: string\n close_ws_on_eos?: boolean\n json_config?: GradiumJsonConfig\n client_req_id?: string\n}\n\nexport interface GradiumTextMessage {\n type: 'text'\n text: string\n client_req_id?: string\n}\n\nexport interface GradiumEosMessage {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\n// Server -> Client messages\n\nexport type GradiumResponse =\n | GradiumReadyResponse\n | GradiumAudioResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumReadyResponse {\n type: 'ready'\n request_id?: string\n delay_in_frames?: number // Frames of 80ms the transcription lags behind the audio\n}\n\nexport interface GradiumAudioResponse {\n type: 'audio'\n audio: string // Base64 encoded PCM data\n client_req_id?: string\n}\n\nexport interface GradiumEosResponse {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\nexport interface GradiumErrorResponse {\n type: 'error'\n message: string\n code?: number\n}\n","import {\n cachedVoices,\n MicdropVoice,\n ReconnectingSocket,\n TTS,\n voiceGender,\n voicesForLanguage,\n} from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n DEFAULT_MODEL_NAME,\n DEFAULT_REGION,\n GradiumAudioResponse,\n GradiumEosMessage,\n GradiumRegion,\n GradiumResponse,\n GradiumSetupMessage,\n GradiumTextMessage,\n GradiumTTSOptions,\n getOutputFormat,\n getSampleRate,\n} from './types'\n\n// API Reference: https://gradium.ai/api_docs.html\n\n/**\n * The utterance Gradium is synthesizing. While there is one the TTS is\n * speaking, and when there is none it is idle: every way out of a request\n * (end of stream, server error, cancel, text stream error, give up) clears it,\n * so nothing about it outlives it.\n */\ninterface GradiumRequest {\n id: string\n /** Complete words received so far, sent again whole on a new connection */\n words: string\n /** Start of a word, held until a space completes it or the stream ends */\n partial: string\n /** The text stream ended, so Gradium was told no more text follows */\n ended: boolean\n}\n\n/** A voice as the Gradium API lists it, the fields read here */\ninterface GradiumVoice {\n uid: string\n name: string\n description?: string | null\n gender?: string | null\n locale?: string | null\n language?: string | null\n is_catalog?: boolean\n}\n\nconst VOICES_PAGE_SIZE = 500 // Voices per page of the listing\n\nexport class GradiumTTS extends TTS {\n /**\n * Lists the voices the account can speak with: its own, then the Gradium\n * catalog, over a thousand voices.\n *\n * `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.\n *\n * The list is fetched once and shared by every call of the process for a\n * day. `refresh` fetches it again, for a voice added a moment ago.\n */\n static listVoices(options: {\n apiKey: string\n region?: GradiumRegion\n language?: string\n refresh?: boolean\n }): Promise<MicdropVoice[]> {\n const region = options.region ?? DEFAULT_REGION\n return cachedVoices(\n ['gradium', region],\n options.apiKey,\n () => fetchVoices(options.apiKey, region),\n options.refresh\n ).then((voices) => voicesForLanguage(voices, options.language))\n }\n\n private socket: ReconnectingSocket\n // Numbers the audio requests, and only moves when there is something to say\n private counter = 0\n // Bumped by every speak() and every cancel(), so a request claimed late can\n // tell whether it is still the one that should be heard\n private generation = 0\n private request?: GradiumRequest\n\n get sampleRate() {\n return getSampleRate(this.options)\n }\n\n constructor(private readonly options: GradiumTTSOptions) {\n super()\n\n const region = options.region ?? DEFAULT_REGION\n this.socket = new ReconnectingSocket({\n url: `wss://${region}.api.gradium.ai/api/speech/tts`,\n clientOptions: { headers: { 'x-api-key': options.apiKey } },\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let request: GradiumRequest | undefined\n\n // Claiming the request is deferred until there is something to say.\n //\n // Taking the next id right away would silence the utterance still playing,\n // because incoming audio is matched against the current id and anything\n // older is dropped. A stream that never carries a word, which is what an\n // answer skipped by a tool or by onBeforeAnswer hands over, would then cut\n // the assistant off mid-sentence for nothing.\n const claimRequest = () => {\n if (request) return request\n // Cancelled, or superseded by another speak(), before the first word\n if (this.generation !== generation) return\n request = {\n id: (++this.counter).toString(),\n words: '',\n partial: '',\n ended: false,\n }\n this.request = request\n // Each speak() is its own multiplexed request, opened by a setup stamped\n // with its client_req_id, so the server binds the voice to it.\n // Multiplexing is what lets the connection serve several utterances\n // (sequential mode only works for the first request on a socket). Without\n // a connection, the setup goes out on open.\n this.sendSetup(request.id)\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n return request\n }\n\n textStream.on('data', (chunk: Buffer) => {\n const request = claimRequest()\n if (!request || this.request !== request) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n\n // Only complete words go out, up to the last space\n const spaceIndex = text.lastIndexOf(' ')\n if (spaceIndex === -1) {\n request.partial += text\n return\n }\n const words = request.partial + text.slice(0, spaceIndex + 1)\n request.partial = text.slice(spaceIndex + 1)\n request.words += words\n // Without a connection the words wait, and go out whole on open\n this.sendTranscript(words, request.id)\n })\n\n textStream.on('error', (error) => {\n if (!request || this.request !== request) return\n this.log('Error in text stream, cancelling the audio', error)\n this.stopRequest()\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is no request to close and no audio to\n // wait for\n if (!request || this.request !== request) return\n request.ended = true\n this.sendEnd(request)\n })\n }\n\n /**\n * Stops the current utterance. Gradium can only stop a synthesis by closing\n * its connection, so a new one opens right away.\n */\n cancel() {\n this.generation++\n if (!this.request) return\n this.log('Cancel')\n this.stopRequest()\n }\n\n destroy() {\n // Destroyed first, so the cancel() that super.destroy() runs opens no new\n // connection\n this.socket.destroy()\n super.destroy()\n this.request = undefined\n }\n\n /** Forgets the current request, and closes the connection that carries it */\n private stopRequest() {\n this.request = undefined\n // Free of charge on the retry budget, and a speak() that follows lands its\n // setup and text on the new connection\n this.socket.restart()\n }\n\n private sendSetup(clientReqId: string) {\n const sent = this.socket.send(\n JSON.stringify({\n type: 'setup',\n voice_id: this.options.voiceId,\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n output_format: getOutputFormat(this.options),\n close_ws_on_eos: false,\n json_config: this.options.jsonConfig,\n client_req_id: clientReqId,\n } satisfies GradiumSetupMessage)\n )\n if (sent) this.log(`Sent setup (client_req_id=${clientReqId})`)\n }\n\n private sendTranscript(text: string, clientReqId: string) {\n const sent = this.socket.send(\n JSON.stringify({\n type: 'text',\n text,\n client_req_id: clientReqId,\n } satisfies GradiumTextMessage)\n )\n if (sent) this.log(`Sent transcript: \"${text}\"`)\n }\n\n /** Sends the last partial word, then the end of the text */\n private sendEnd(request: GradiumRequest) {\n if (request.partial.trim()) {\n this.sendTranscript(request.partial, request.id)\n }\n const sent = this.socket.send(\n JSON.stringify({\n type: 'end_of_stream',\n client_req_id: request.id,\n } satisfies GradiumEosMessage)\n )\n if (sent) this.log('Sent end_of_stream')\n }\n\n // A new connection has no session for the request under way: its setup and\n // its words go out again, the setup first, with the same client_req_id\n private onOpen = () => {\n const request = this.request\n if (!request) return\n this.log('Sending the request again')\n this.sendSetup(request.id)\n if (request.words) this.sendTranscript(request.words, request.id)\n if (request.ended) this.sendEnd(request)\n }\n\n private onMessage = (data: RawData) => {\n let message: GradiumResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n switch (message.type) {\n case 'ready':\n // The server emits one ready per setup, informational only\n this.log('Server ready', message.request_id ?? '')\n break\n\n case 'audio':\n this.processAudioMessage(message)\n break\n\n case 'end_of_stream':\n // Ignore if from a previous request\n if (\n !this.request ||\n (message.client_req_id !== undefined &&\n message.client_req_id !== this.request.id)\n )\n return\n this.log('Audio ended')\n this.request = undefined\n break\n\n case 'error':\n // The connection carries one request at a time, since cancel() closes\n // it, so the error is about the current one, which gets no more audio.\n // Its text is handed over, so a FallbackTTS can speak it instead, and\n // a fresh connection keeps the next request clear of it.\n this.log('Error:', message.message, message.code)\n if (!this.request) break\n this.onFailed()\n this.socket.restart()\n break\n }\n }\n\n private processAudioMessage(message: GradiumAudioResponse) {\n // Ignore messages from previous requests\n if (!this.request || message.client_req_id !== this.request.id) return\n\n const chunk = Buffer.from(message.audio, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n }\n\n private onFailed = () => {\n const request = this.request\n this.request = undefined\n const text = request ? request.words + request.partial : ''\n this.emit('Failed', text ? [text] : [])\n }\n}\n\n/** Reads every page of the voices, the catalog included */\nasync function fetchVoices(\n apiKey: string,\n region: GradiumRegion\n): Promise<MicdropVoice[]> {\n const voices: MicdropVoice[] = []\n // The listing returns a bare array, a short page is the last one\n for (let skip = 0; ; skip += VOICES_PAGE_SIZE) {\n const params = new URLSearchParams({\n include_catalog: 'true',\n limit: String(VOICES_PAGE_SIZE),\n skip: String(skip),\n })\n const response = await fetch(\n `https://${region}.api.gradium.ai/api/voices/?${params}`,\n { headers: { 'x-api-key': apiKey } }\n )\n if (!response.ok) throw new Error(`Gradium answered ${response.status}`)\n const page = (await response.json()) as GradiumVoice[]\n voices.push(...page.map(toMicdropVoice))\n if (page.length < VOICES_PAGE_SIZE) break\n }\n return voices\n}\n\nfunction toMicdropVoice(voice: GradiumVoice): MicdropVoice {\n // en-us becomes en-US, and a dialect such as fr-fr-x-southern its locale\n const [language, region] = (voice.locale || voice.language || '').split('-')\n const locale = region ? `${language}-${region.toUpperCase()}` : language\n return {\n id: voice.uid,\n name: voice.name,\n languages: locale ? [locale] : undefined,\n gender: voiceGender(voice.gender),\n description: voice.description || undefined,\n custom: voice.is_catalog === false,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAIO;;;ACiDA,IAAM,qBAAqB;AAC3B,IAAM,sBAAyC;AAE/C,IAAM,wBAA6C;AACnD,IAAM,uBAA2C;AACjD,IAAM,iBAAgC;AAEtC,IAAM,mBAAmB;AAEhC,IAAM,cAA8D;AAAA,EAClE,KAAM;AAAA,EACN,MAAO;AAAA,EACP,MAAO;AAAA,EACP,MAAO;AAAA;AACT;AAGO,SAAS,gBACd,SACqB;AACrB,MAAI,QAAQ,WAAY,QAAO,YAAY,QAAQ,UAAU;AAC7D,SAAO,QAAQ,gBAAgB,YAAY,mBAAmB;AAChE;AAGO,SAAS,cAAc,SAAoC;AAChE,QAAM,SAAS,gBAAgB,OAAO;AACtC,MAAI,WAAW,MAAO,QAAO;AAC7B,QAAM,QAAQ,cAAc,KAAK,MAAM;AACvC,SAAO,QAAQ,OAAO,MAAM,CAAC,CAAC,IAAI;AACpC;;;AD1DA,IAAM,gCAAgC;AAEtC,IAAM,oBAAoB;AAC1B,IAAM,0BAA0B;AAqBzB,IAAM,aA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 import {
6
6
 
7
7
  // src/types.ts
8
8
  var DEFAULT_MODEL_NAME = "default";
9
- var DEFAULT_OUTPUT_FORMAT = "pcm_16000";
9
+ var DEFAULT_SAMPLE_RATE = 48e3;
10
+ var DEFAULT_OUTPUT_FORMAT = "pcm";
10
11
  var DEFAULT_INPUT_FORMAT = "pcm_16000";
11
12
  var DEFAULT_REGION = "eu";
12
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
+ }
13
31
 
14
32
  // src/GradiumSTT.ts
15
33
  var DEFAULT_TRANSCRIPTION_TIMEOUT = 4e3;
@@ -230,7 +248,14 @@ function pcmSampleRate(format) {
230
248
  }
231
249
 
232
250
  // src/GradiumTTS.ts
233
- import { ReconnectingSocket as ReconnectingSocket2, TTS } from "@micdrop/server";
251
+ import {
252
+ cachedVoices,
253
+ ReconnectingSocket as ReconnectingSocket2,
254
+ TTS,
255
+ voiceGender,
256
+ voicesForLanguage
257
+ } from "@micdrop/server";
258
+ var VOICES_PAGE_SIZE = 500;
234
259
  var GradiumTTS = class extends TTS {
235
260
  constructor(options) {
236
261
  super();
@@ -299,6 +324,27 @@ var GradiumTTS = class extends TTS {
299
324
  this.socket.on("Failed", this.onFailed);
300
325
  this.socket.connect();
301
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);
347
+ }
302
348
  speak(textStream) {
303
349
  const generation = ++this.generation;
304
350
  let request;
@@ -367,7 +413,7 @@ var GradiumTTS = class extends TTS {
367
413
  type: "setup",
368
414
  voice_id: this.options.voiceId,
369
415
  model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,
370
- output_format: this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT,
416
+ output_format: getOutputFormat(this.options),
371
417
  close_ws_on_eos: false,
372
418
  json_config: this.options.jsonConfig,
373
419
  client_req_id: clientReqId
@@ -405,13 +451,47 @@ var GradiumTTS = class extends TTS {
405
451
  this.emit("Audio", chunk);
406
452
  }
407
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)
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;
470
+ }
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
+ }
408
485
  export {
409
486
  DEFAULT_INPUT_FORMAT,
410
487
  DEFAULT_LANGUAGE,
411
488
  DEFAULT_MODEL_NAME,
412
489
  DEFAULT_OUTPUT_FORMAT,
413
490
  DEFAULT_REGION,
491
+ DEFAULT_SAMPLE_RATE,
414
492
  GradiumSTT,
415
- GradiumTTS
493
+ GradiumTTS,
494
+ getOutputFormat,
495
+ getSampleRate
416
496
  };
417
497
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/GradiumSTT.ts","../src/types.ts","../src/GradiumTTS.ts"],"sourcesContent":["import {\n ReconnectingSocket,\n ReconnectingSocketHandshake,\n STT,\n} from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n DEFAULT_INPUT_FORMAT,\n DEFAULT_LANGUAGE,\n DEFAULT_MODEL_NAME,\n DEFAULT_REGION,\n GradiumASRJsonConfig,\n GradiumASRResponse,\n GradiumASRSetupMessage,\n GradiumInputFormat,\n GradiumSTTOptions,\n} from './types'\n\n/**\n * Gradium Real-time STT (ASR)\n *\n * @see https://docs.gradium.ai/guides/speech-to-text\n */\n\nconst DEFAULT_TRANSCRIPTION_TIMEOUT = 4000\n// Each frame of the transcription model lasts 80 ms\nconst FRAME_DURATION_MS = 80\nconst DEFAULT_DELAY_IN_FRAMES = 10\n\n/**\n * One utterance, from its first audio chunk to its transcript.\n *\n * It is capturing while its audio stream runs, then flushing once the stream\n * ended and the flush went out, and done once its transcript is emitted, by\n * the matching `flushed` or by its own timeout.\n */\ninterface Utterance {\n /** Audio sent so far, sent again whole on a new connection */\n audio: Buffer[]\n /** Text segments received for this utterance */\n transcript: string\n /** Set when the audio stream ends and the flush goes out */\n flushId?: number\n timeout?: ReturnType<typeof setTimeout>\n /** The transcript went out, on `flushed` or on timeout */\n done: boolean\n}\n\nexport class GradiumSTT extends STT {\n private socket: ReconnectingSocket\n // Utterances in the order their audio went out. The server answers in that\n // order too, so every text segment belongs to the first one not flushed yet\n private utterances: Utterance[] = []\n private flushId = 0\n private delayInFrames = DEFAULT_DELAY_IN_FRAMES // Given by the server when ready\n\n constructor(private options: GradiumSTTOptions) {\n super()\n\n const region = options.region ?? DEFAULT_REGION\n this.socket = new ReconnectingSocket({\n url: `wss://${region}.api.gradium.ai/api/speech/asr`,\n clientOptions: { headers: { 'x-api-key': options.apiKey } },\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n // The server opens the connection before checking the setup, so the\n // connection only counts as open once it answers ready\n handshake: this.handshake,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n transcribe(audioStream: Readable) {\n let utterance: Utterance | undefined\n\n // Read audio stream and send to Gradium\n audioStream.on('data', (chunk: Buffer) => {\n // The utterance starts with its first chunk. Voice detection opens a\n // stream on any noise and many of them never carry a word, so they leave\n // no utterance behind.\n if (!utterance) {\n utterance = { audio: [], transcript: '', done: false }\n this.utterances.push(utterance)\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n }\n if (utterance.done) return\n utterance.audio.push(chunk)\n // Without a connection the audio waits, and goes out whole on open\n if (this.sendAudioChunk(chunk)) {\n this.log(`Sent audio chunk (${chunk.byteLength} bytes)`)\n }\n })\n\n // Handle stream end: flush buffered audio so the server finalizes the\n // pending segments and confirms with a matching \"flushed\" message.\n audioStream.on('end', () => {\n if (!utterance || utterance.done) return\n const current = utterance\n current.audio.push(this.trailingSilence())\n current.flushId = ++this.flushId\n if (this.socket.isOpen) this.sendEnd(current)\n\n // Timeout transcription if no \"flushed\" confirmation is received\n current.timeout = setTimeout(() => {\n current.timeout = undefined\n this.log('Transcription timeout')\n this.emitTranscript(current)\n }, this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT)\n })\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.utterances.forEach((utterance) => clearTimeout(utterance.timeout))\n this.utterances = []\n }\n\n private buildJsonConfig(): GradiumASRJsonConfig {\n const config: GradiumASRJsonConfig = { ...this.options.jsonConfig }\n if (config.language === undefined) {\n config.language = this.options.language ?? DEFAULT_LANGUAGE\n }\n return config\n }\n\n /** Sends the setup, and waits for the server to accept it */\n private handshake: ReconnectingSocketHandshake = {\n start: (socket) => {\n socket.send(\n JSON.stringify({\n type: 'setup',\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n input_format: this.options.inputFormat ?? DEFAULT_INPUT_FORMAT,\n json_config: this.buildJsonConfig(),\n } satisfies GradiumASRSetupMessage)\n )\n this.log('Sent setup')\n },\n read: (data) => {\n let message: GradiumASRResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return false\n }\n if (message.type === 'error') {\n throw new Error(`Setup refused: ${message.message}`)\n }\n if (message.type !== 'ready') return false\n this.log('Server ready')\n if (typeof message.delay_in_frames === 'number') {\n this.delayInFrames = message.delay_in_frames\n }\n return true\n },\n }\n\n private sendAudioChunk(chunk: Buffer) {\n return this.socket.send(\n JSON.stringify({\n type: 'audio',\n audio: chunk.toString('base64'),\n })\n )\n }\n\n /**\n * Silence as long as the transcription lags behind, to follow the speech.\n *\n * The model writes each word a few frames after hearing it, and a flush only\n * processes the audio already sent. With nothing after the last word, the\n * server confirms the flush before writing that word, which then opens the\n * next transcript.\n */\n private trailingSilence(): Buffer {\n const sampleRate = pcmSampleRate(\n this.options.inputFormat ?? DEFAULT_INPUT_FORMAT\n )\n // Silence is a run of zeros in raw PCM only\n if (!sampleRate) return Buffer.alloc(0)\n const durationMs = this.delayInFrames * FRAME_DURATION_MS\n const samples = Math.round((sampleRate * durationMs) / 1000)\n return Buffer.alloc(samples * 2)\n }\n\n /** Sends the trailing silence, already the last audio chunk, and the flush */\n private sendEnd(utterance: Utterance) {\n const silence = utterance.audio[utterance.audio.length - 1]\n if (silence.length) this.sendAudioChunk(silence)\n this.socket.send(\n JSON.stringify({ type: 'flush', flush_id: utterance.flushId })\n )\n this.log(`Sent flush (flush_id=${utterance.flushId})`)\n }\n\n private emitTranscript(utterance: Utterance) {\n if (utterance.done) return\n utterance.done = true\n clearTimeout(utterance.timeout)\n utterance.timeout = undefined\n // Its audio is no longer needed, but the utterance stays in the queue until\n // its flush returns, so the late segments of a timed out utterance don't\n // land in the next one\n utterance.audio = []\n const transcript = utterance.transcript.replace(/\\s+/g, ' ').trim()\n this.log(`Received transcript: \"${transcript}\"`)\n this.emit('Transcript', transcript)\n }\n\n // A new connection knows nothing of the utterances under way: the audio read\n // before the first connection opened, or lost with the previous one, goes out\n // again whole, and the server transcribes it from the start\n private onOpen = () => {\n // A new session never confirms the flushes of the previous one\n this.utterances = this.utterances.filter((utterance) => !utterance.done)\n if (this.utterances.length === 0) return\n this.log('Sending audio chunks again')\n for (const utterance of this.utterances) {\n utterance.transcript = ''\n if (utterance.flushId === undefined) {\n utterance.audio.forEach((chunk) => this.sendAudioChunk(chunk))\n } else {\n // The trailing silence goes out with the flush\n utterance.audio\n .slice(0, -1)\n .forEach((chunk) => this.sendAudioChunk(chunk))\n this.sendEnd(utterance)\n }\n }\n }\n\n private onMessage = (data: RawData) => {\n let message: GradiumASRResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n switch (message.type) {\n case 'text': {\n const utterance = this.utterances[0]\n if (typeof message.text === 'string' && utterance) {\n utterance.transcript +=\n (utterance.transcript ? ' ' : '') + message.text\n this.log(`Received text segment: \"${message.text}\"`)\n }\n break\n }\n\n case 'flushed': {\n // Every segment of the flushed utterance arrived. Those before it in\n // the queue are flushed too, since the server answers in order.\n const index = this.utterances.findIndex(\n (utterance) => utterance.flushId === message.flush_id\n )\n if (index === -1) break\n const flushed = this.utterances.splice(0, index + 1)\n flushed.forEach((utterance) => this.emitTranscript(utterance))\n break\n }\n\n case 'end_text':\n // Segment finalized, nothing to accumulate (text already received)\n break\n\n case 'step':\n // Voice activity detection frames, ignored\n break\n\n case 'end_of_stream':\n this.log('End of stream')\n break\n\n case 'error':\n this.log('Error:', message.message, message.code)\n break\n }\n }\n\n private onFailed = () => {\n // The audio of every utterance still waiting for its transcript, for a\n // fallback to transcribe it instead\n const pending = this.utterances.filter((utterance) => !utterance.done)\n this.utterances.forEach((utterance) => clearTimeout(utterance.timeout))\n this.utterances = []\n this.emit(\n 'Failed',\n pending.flatMap((utterance) => utterance.audio)\n )\n }\n}\n\n/** Sample rate of a raw PCM input format, undefined for an encoded one */\nfunction pcmSampleRate(format: GradiumInputFormat): number | undefined {\n if (format === 'pcm') return 24000\n const match = format.match(/^pcm_(\\d+)$/)\n return match ? Number(match[1]) : undefined\n}\n","export type GradiumOutputFormat =\n | 'pcm'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n\nexport type GradiumInputFormat =\n | 'pcm'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n | 'pcm_48000'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n\nexport type GradiumRegion = 'eu' | 'us'\n\nexport interface GradiumTTSOptions {\n apiKey: string\n voiceId: string\n modelName?: string\n outputFormat?: GradiumOutputFormat\n region?: GradiumRegion\n jsonConfig?: GradiumJsonConfig\n connectionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\nexport interface GradiumJsonConfig {\n padding_bonus?: number // Speed control: -4.0 to 4.0\n temp?: number // Temperature: 0 to 1.4, default 0.7\n cfg_coef?: number // Voice similarity: 1.0 to 4.0, default 2.0\n}\n\nexport const DEFAULT_MODEL_NAME = 'default'\nexport const DEFAULT_OUTPUT_FORMAT: GradiumOutputFormat = 'pcm_16000'\nexport const DEFAULT_INPUT_FORMAT: GradiumInputFormat = 'pcm_16000'\nexport const DEFAULT_REGION: GradiumRegion = 'eu'\n// Gradium refuses a transcription without a language, \"any\" detects it\nexport const DEFAULT_LANGUAGE = 'any'\n\n// Speech-to-text (ASR) types\n\nexport interface GradiumASRJsonConfig {\n language?: string\n target_language?: string\n delay_in_frames?: number // 0 to 80, each frame = 80ms\n temp?: number\n padding_bonus?: number\n}\n\nexport interface GradiumSTTOptions {\n apiKey: string\n modelName?: string\n inputFormat?: GradiumInputFormat\n language?: string\n region?: GradiumRegion\n jsonConfig?: GradiumASRJsonConfig\n connectionTimeout?: number\n transcriptionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\n// Client -> Server ASR messages\n\nexport interface GradiumASRSetupMessage {\n type: 'setup'\n model_name: string\n input_format: string\n json_config?: GradiumASRJsonConfig\n}\n\nexport interface GradiumAudioMessage {\n type: 'audio'\n audio: string // Base64 encoded audio data\n}\n\nexport interface GradiumFlushMessage {\n type: 'flush'\n flush_id: number\n}\n\n// Server -> Client ASR messages\n\nexport type GradiumASRResponse =\n | GradiumReadyResponse\n | GradiumTextResponse\n | GradiumEndTextResponse\n | GradiumStepResponse\n | GradiumFlushedResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumTextResponse {\n type: 'text'\n text: string\n start_s?: number\n stream_id?: number\n}\n\nexport interface GradiumEndTextResponse {\n type: 'end_text'\n stop_s?: number\n stream_id?: number\n}\n\nexport interface GradiumStepResponse {\n type: 'step'\n vad?: Array<{ horizon_s: number; inactivity_prob: number }>\n step_idx?: number\n step_duration_s?: number\n total_duration_s?: number\n}\n\nexport interface GradiumFlushedResponse {\n type: 'flushed'\n flush_id: number\n}\n\n// Client -> Server messages\n\nexport interface GradiumSetupMessage {\n type: 'setup'\n voice_id: string\n model_name: string\n output_format: string\n close_ws_on_eos?: boolean\n json_config?: GradiumJsonConfig\n client_req_id?: string\n}\n\nexport interface GradiumTextMessage {\n type: 'text'\n text: string\n client_req_id?: string\n}\n\nexport interface GradiumEosMessage {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\n// Server -> Client messages\n\nexport type GradiumResponse =\n | GradiumReadyResponse\n | GradiumAudioResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumReadyResponse {\n type: 'ready'\n request_id?: string\n delay_in_frames?: number // Frames of 80ms the transcription lags behind the audio\n}\n\nexport interface GradiumAudioResponse {\n type: 'audio'\n audio: string // Base64 encoded PCM data\n client_req_id?: string\n}\n\nexport interface GradiumEosResponse {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\nexport interface GradiumErrorResponse {\n type: 'error'\n message: string\n code?: number\n}\n","import { ReconnectingSocket, TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n DEFAULT_MODEL_NAME,\n DEFAULT_OUTPUT_FORMAT,\n DEFAULT_REGION,\n GradiumAudioResponse,\n GradiumEosMessage,\n GradiumResponse,\n GradiumSetupMessage,\n GradiumTextMessage,\n GradiumTTSOptions,\n} from './types'\n\n// API Reference: https://gradium.ai/api_docs.html\n\n/**\n * The utterance Gradium is synthesizing. While there is one the TTS is\n * speaking, and when there is none it is idle: every way out of a request\n * (end of stream, server error, cancel, text stream error, give up) clears it,\n * so nothing about it outlives it.\n */\ninterface GradiumRequest {\n id: string\n /** Complete words received so far, sent again whole on a new connection */\n words: string\n /** Start of a word, held until a space completes it or the stream ends */\n partial: string\n /** The text stream ended, so Gradium was told no more text follows */\n ended: boolean\n}\n\nexport class GradiumTTS extends TTS {\n private socket: ReconnectingSocket\n // Numbers the audio requests, and only moves when there is something to say\n private counter = 0\n // Bumped by every speak() and every cancel(), so a request claimed late can\n // tell whether it is still the one that should be heard\n private generation = 0\n private request?: GradiumRequest\n\n constructor(private readonly options: GradiumTTSOptions) {\n super()\n\n const region = options.region ?? DEFAULT_REGION\n this.socket = new ReconnectingSocket({\n url: `wss://${region}.api.gradium.ai/api/speech/tts`,\n clientOptions: { headers: { 'x-api-key': options.apiKey } },\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let request: GradiumRequest | undefined\n\n // Claiming the request is deferred until there is something to say.\n //\n // Taking the next id right away would silence the utterance still playing,\n // because incoming audio is matched against the current id and anything\n // older is dropped. A stream that never carries a word, which is what an\n // answer skipped by a tool or by onBeforeAnswer hands over, would then cut\n // the assistant off mid-sentence for nothing.\n const claimRequest = () => {\n if (request) return request\n // Cancelled, or superseded by another speak(), before the first word\n if (this.generation !== generation) return\n request = {\n id: (++this.counter).toString(),\n words: '',\n partial: '',\n ended: false,\n }\n this.request = request\n // Each speak() is its own multiplexed request, opened by a setup stamped\n // with its client_req_id, so the server binds the voice to it.\n // Multiplexing is what lets the connection serve several utterances\n // (sequential mode only works for the first request on a socket). Without\n // a connection, the setup goes out on open.\n this.sendSetup(request.id)\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n return request\n }\n\n textStream.on('data', (chunk: Buffer) => {\n const request = claimRequest()\n if (!request || this.request !== request) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n\n // Only complete words go out, up to the last space\n const spaceIndex = text.lastIndexOf(' ')\n if (spaceIndex === -1) {\n request.partial += text\n return\n }\n const words = request.partial + text.slice(0, spaceIndex + 1)\n request.partial = text.slice(spaceIndex + 1)\n request.words += words\n // Without a connection the words wait, and go out whole on open\n this.sendTranscript(words, request.id)\n })\n\n textStream.on('error', (error) => {\n if (!request || this.request !== request) return\n this.log('Error in text stream, cancelling the audio', error)\n this.stopRequest()\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is no request to close and no audio to\n // wait for\n if (!request || this.request !== request) return\n request.ended = true\n this.sendEnd(request)\n })\n }\n\n /**\n * Stops the current utterance. Gradium can only stop a synthesis by closing\n * its connection, so a new one opens right away.\n */\n cancel() {\n this.generation++\n if (!this.request) return\n this.log('Cancel')\n this.stopRequest()\n }\n\n destroy() {\n // Destroyed first, so the cancel() that super.destroy() runs opens no new\n // connection\n this.socket.destroy()\n super.destroy()\n this.request = undefined\n }\n\n /** Forgets the current request, and closes the connection that carries it */\n private stopRequest() {\n this.request = undefined\n // Free of charge on the retry budget, and a speak() that follows lands its\n // setup and text on the new connection\n this.socket.restart()\n }\n\n private sendSetup(clientReqId: string) {\n const sent = this.socket.send(\n JSON.stringify({\n type: 'setup',\n voice_id: this.options.voiceId,\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n output_format: this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT,\n close_ws_on_eos: false,\n json_config: this.options.jsonConfig,\n client_req_id: clientReqId,\n } satisfies GradiumSetupMessage)\n )\n if (sent) this.log(`Sent setup (client_req_id=${clientReqId})`)\n }\n\n private sendTranscript(text: string, clientReqId: string) {\n const sent = this.socket.send(\n JSON.stringify({\n type: 'text',\n text,\n client_req_id: clientReqId,\n } satisfies GradiumTextMessage)\n )\n if (sent) this.log(`Sent transcript: \"${text}\"`)\n }\n\n /** Sends the last partial word, then the end of the text */\n private sendEnd(request: GradiumRequest) {\n if (request.partial.trim()) {\n this.sendTranscript(request.partial, request.id)\n }\n const sent = this.socket.send(\n JSON.stringify({\n type: 'end_of_stream',\n client_req_id: request.id,\n } satisfies GradiumEosMessage)\n )\n if (sent) this.log('Sent end_of_stream')\n }\n\n // A new connection has no session for the request under way: its setup and\n // its words go out again, the setup first, with the same client_req_id\n private onOpen = () => {\n const request = this.request\n if (!request) return\n this.log('Sending the request again')\n this.sendSetup(request.id)\n if (request.words) this.sendTranscript(request.words, request.id)\n if (request.ended) this.sendEnd(request)\n }\n\n private onMessage = (data: RawData) => {\n let message: GradiumResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n switch (message.type) {\n case 'ready':\n // The server emits one ready per setup, informational only\n this.log('Server ready', message.request_id ?? '')\n break\n\n case 'audio':\n this.processAudioMessage(message)\n break\n\n case 'end_of_stream':\n // Ignore if from a previous request\n if (\n !this.request ||\n (message.client_req_id !== undefined &&\n message.client_req_id !== this.request.id)\n )\n return\n this.log('Audio ended')\n this.request = undefined\n break\n\n case 'error':\n // The connection carries one request at a time, since cancel() closes\n // it, so the error is about the current one, which gets no more audio.\n // Its text is handed over, so a FallbackTTS can speak it instead, and\n // a fresh connection keeps the next request clear of it.\n this.log('Error:', message.message, message.code)\n if (!this.request) break\n this.onFailed()\n this.socket.restart()\n break\n }\n }\n\n private processAudioMessage(message: GradiumAudioResponse) {\n // Ignore messages from previous requests\n if (!this.request || message.client_req_id !== this.request.id) return\n\n const chunk = Buffer.from(message.audio, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n }\n\n private onFailed = () => {\n const request = this.request\n this.request = undefined\n const text = request ? request.words + request.partial : ''\n this.emit('Failed', text ? [text] : [])\n }\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EAEA;AAAA,OACK;;;ACqCA,IAAM,qBAAqB;AAC3B,IAAM,wBAA6C;AACnD,IAAM,uBAA2C;AACjD,IAAM,iBAAgC;AAEtC,IAAM,mBAAmB;;;ADrBhC,IAAM,gCAAgC;AAEtC,IAAM,oBAAoB;AAC1B,IAAM,0BAA0B;AAqBzB,IAAM,aAAN,cAAyB,IAAI;AAAA;AAAA,EAQlC,YAAoB,SAA4B;AAC9C,UAAM;AADY;AAJpB;AAAA;AAAA,SAAQ,aAA0B,CAAC;AACnC,SAAQ,UAAU;AAClB,SAAQ,gBAAgB;AA+ExB;AAAA,SAAQ,YAAyC;AAAA,MAC/C,OAAO,CAAC,WAAW;AACjB,eAAO;AAAA,UACL,KAAK,UAAU;AAAA,YACb,MAAM;AAAA,YACN,YAAY,KAAK,Q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,EAEQ,eAAe,MAAc,aAAqB;AACxD,UAAM,OAAO,KAAK,OAAO;AAAA,MACvB,KAAK,UAAU;AAAA,QACb,MAAM;AAAA,QACN;AAAA,QACA,eAAe;AAAA,MACjB,CAA8B;AAAA,IAChC;AACA,QAAI,KAAM,MAAK,IAAI,qBAAqB,IAAI,GAAG;AAAA,EACjD;AAAA;AAAA,EAGQ,QAAQ,SAAyB;AACvC,QAAI,QAAQ,QAAQ,KAAK,GAAG;AAC1B,WAAK,eAAe,QAAQ,SAAS,QAAQ,EAAE;AAAA,IACjD;AACA,UAAM,OAAO,KAAK,OAAO;AAAA,MACvB,KAAK,UAAU;AAAA,QACb,MAAM;AAAA,QACN,eAAe,QAAQ;AAAA,MACzB,CAA6B;AAAA,IAC/B;AACA,QAAI,KAAM,MAAK,IAAI,oBAAoB;AAAA,EACzC;AAAA,EAyDQ,oBAAoB,SAA+B;AAEzD,QAAI,CAAC,KAAK,WAAW,QAAQ,kBAAkB,KAAK,QAAQ,GAAI;AAEhE,UAAM,QAAQ,OAAO,KAAK,QAAQ,OAAO,QAAQ;AACjD,SAAK,IAAI,yBAAyB,MAAM,MAAM,SAAS;AACvD,SAAK,KAAK,SAAS,KAAK;AAAA,EAC1B;AAQF;","names":["ReconnectingSocket","ReconnectingSocket","request"]}
1
+ {"version":3,"sources":["../src/GradiumSTT.ts","../src/types.ts","../src/GradiumTTS.ts"],"sourcesContent":["import {\n ReconnectingSocket,\n ReconnectingSocketHandshake,\n STT,\n} from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n DEFAULT_INPUT_FORMAT,\n DEFAULT_LANGUAGE,\n DEFAULT_MODEL_NAME,\n DEFAULT_REGION,\n GradiumASRJsonConfig,\n GradiumASRResponse,\n GradiumASRSetupMessage,\n GradiumInputFormat,\n GradiumSTTOptions,\n} from './types'\n\n/**\n * Gradium Real-time STT (ASR)\n *\n * @see https://docs.gradium.ai/guides/speech-to-text\n */\n\nconst DEFAULT_TRANSCRIPTION_TIMEOUT = 4000\n// Each frame of the transcription model lasts 80 ms\nconst FRAME_DURATION_MS = 80\nconst DEFAULT_DELAY_IN_FRAMES = 10\n\n/**\n * One utterance, from its first audio chunk to its transcript.\n *\n * It is capturing while its audio stream runs, then flushing once the stream\n * ended and the flush went out, and done once its transcript is emitted, by\n * the matching `flushed` or by its own timeout.\n */\ninterface Utterance {\n /** Audio sent so far, sent again whole on a new connection */\n audio: Buffer[]\n /** Text segments received for this utterance */\n transcript: string\n /** Set when the audio stream ends and the flush goes out */\n flushId?: number\n timeout?: ReturnType<typeof setTimeout>\n /** The transcript went out, on `flushed` or on timeout */\n done: boolean\n}\n\nexport class GradiumSTT extends STT {\n private socket: ReconnectingSocket\n // Utterances in the order their audio went out. The server answers in that\n // order too, so every text segment belongs to the first one not flushed yet\n private utterances: Utterance[] = []\n private flushId = 0\n private delayInFrames = DEFAULT_DELAY_IN_FRAMES // Given by the server when ready\n\n constructor(private options: GradiumSTTOptions) {\n super()\n\n const region = options.region ?? DEFAULT_REGION\n this.socket = new ReconnectingSocket({\n url: `wss://${region}.api.gradium.ai/api/speech/asr`,\n clientOptions: { headers: { 'x-api-key': options.apiKey } },\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n // The server opens the connection before checking the setup, so the\n // connection only counts as open once it answers ready\n handshake: this.handshake,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n transcribe(audioStream: Readable) {\n let utterance: Utterance | undefined\n\n // Read audio stream and send to Gradium\n audioStream.on('data', (chunk: Buffer) => {\n // The utterance starts with its first chunk. Voice detection opens a\n // stream on any noise and many of them never carry a word, so they leave\n // no utterance behind.\n if (!utterance) {\n utterance = { audio: [], transcript: '', done: false }\n this.utterances.push(utterance)\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n }\n if (utterance.done) return\n utterance.audio.push(chunk)\n // Without a connection the audio waits, and goes out whole on open\n if (this.sendAudioChunk(chunk)) {\n this.log(`Sent audio chunk (${chunk.byteLength} bytes)`)\n }\n })\n\n // Handle stream end: flush buffered audio so the server finalizes the\n // pending segments and confirms with a matching \"flushed\" message.\n audioStream.on('end', () => {\n if (!utterance || utterance.done) return\n const current = utterance\n current.audio.push(this.trailingSilence())\n current.flushId = ++this.flushId\n if (this.socket.isOpen) this.sendEnd(current)\n\n // Timeout transcription if no \"flushed\" confirmation is received\n current.timeout = setTimeout(() => {\n current.timeout = undefined\n this.log('Transcription timeout')\n this.emitTranscript(current)\n }, this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT)\n })\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.utterances.forEach((utterance) => clearTimeout(utterance.timeout))\n this.utterances = []\n }\n\n private buildJsonConfig(): GradiumASRJsonConfig {\n const config: GradiumASRJsonConfig = { ...this.options.jsonConfig }\n if (config.language === undefined) {\n config.language = this.options.language ?? DEFAULT_LANGUAGE\n }\n return config\n }\n\n /** Sends the setup, and waits for the server to accept it */\n private handshake: ReconnectingSocketHandshake = {\n start: (socket) => {\n socket.send(\n JSON.stringify({\n type: 'setup',\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n input_format: this.options.inputFormat ?? DEFAULT_INPUT_FORMAT,\n json_config: this.buildJsonConfig(),\n } satisfies GradiumASRSetupMessage)\n )\n this.log('Sent setup')\n },\n read: (data) => {\n let message: GradiumASRResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return false\n }\n if (message.type === 'error') {\n throw new Error(`Setup refused: ${message.message}`)\n }\n if (message.type !== 'ready') return false\n this.log('Server ready')\n if (typeof message.delay_in_frames === 'number') {\n this.delayInFrames = message.delay_in_frames\n }\n return true\n },\n }\n\n private sendAudioChunk(chunk: Buffer) {\n return this.socket.send(\n JSON.stringify({\n type: 'audio',\n audio: chunk.toString('base64'),\n })\n )\n }\n\n /**\n * Silence as long as the transcription lags behind, to follow the speech.\n *\n * The model writes each word a few frames after hearing it, and a flush only\n * processes the audio already sent. With nothing after the last word, the\n * server confirms the flush before writing that word, which then opens the\n * next transcript.\n */\n private trailingSilence(): Buffer {\n const sampleRate = pcmSampleRate(\n this.options.inputFormat ?? DEFAULT_INPUT_FORMAT\n )\n // Silence is a run of zeros in raw PCM only\n if (!sampleRate) return Buffer.alloc(0)\n const durationMs = this.delayInFrames * FRAME_DURATION_MS\n const samples = Math.round((sampleRate * durationMs) / 1000)\n return Buffer.alloc(samples * 2)\n }\n\n /** Sends the trailing silence, already the last audio chunk, and the flush */\n private sendEnd(utterance: Utterance) {\n const silence = utterance.audio[utterance.audio.length - 1]\n if (silence.length) this.sendAudioChunk(silence)\n this.socket.send(\n JSON.stringify({ type: 'flush', flush_id: utterance.flushId })\n )\n this.log(`Sent flush (flush_id=${utterance.flushId})`)\n }\n\n private emitTranscript(utterance: Utterance) {\n if (utterance.done) return\n utterance.done = true\n clearTimeout(utterance.timeout)\n utterance.timeout = undefined\n // Its audio is no longer needed, but the utterance stays in the queue until\n // its flush returns, so the late segments of a timed out utterance don't\n // land in the next one\n utterance.audio = []\n const transcript = utterance.transcript.replace(/\\s+/g, ' ').trim()\n this.log(`Received transcript: \"${transcript}\"`)\n this.emit('Transcript', transcript)\n }\n\n // A new connection knows nothing of the utterances under way: the audio read\n // before the first connection opened, or lost with the previous one, goes out\n // again whole, and the server transcribes it from the start\n private onOpen = () => {\n // A new session never confirms the flushes of the previous one\n this.utterances = this.utterances.filter((utterance) => !utterance.done)\n if (this.utterances.length === 0) return\n this.log('Sending audio chunks again')\n for (const utterance of this.utterances) {\n utterance.transcript = ''\n if (utterance.flushId === undefined) {\n utterance.audio.forEach((chunk) => this.sendAudioChunk(chunk))\n } else {\n // The trailing silence goes out with the flush\n utterance.audio\n .slice(0, -1)\n .forEach((chunk) => this.sendAudioChunk(chunk))\n this.sendEnd(utterance)\n }\n }\n }\n\n private onMessage = (data: RawData) => {\n let message: GradiumASRResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n switch (message.type) {\n case 'text': {\n const utterance = this.utterances[0]\n if (typeof message.text === 'string' && utterance) {\n utterance.transcript +=\n (utterance.transcript ? ' ' : '') + message.text\n this.log(`Received text segment: \"${message.text}\"`)\n }\n break\n }\n\n case 'flushed': {\n // Every segment of the flushed utterance arrived. Those before it in\n // the queue are flushed too, since the server answers in order.\n const index = this.utterances.findIndex(\n (utterance) => utterance.flushId === message.flush_id\n )\n if (index === -1) break\n const flushed = this.utterances.splice(0, index + 1)\n flushed.forEach((utterance) => this.emitTranscript(utterance))\n break\n }\n\n case 'end_text':\n // Segment finalized, nothing to accumulate (text already received)\n break\n\n case 'step':\n // Voice activity detection frames, ignored\n break\n\n case 'end_of_stream':\n this.log('End of stream')\n break\n\n case 'error':\n this.log('Error:', message.message, message.code)\n break\n }\n }\n\n private onFailed = () => {\n // The audio of every utterance still waiting for its transcript, for a\n // fallback to transcribe it instead\n const pending = this.utterances.filter((utterance) => !utterance.done)\n this.utterances.forEach((utterance) => clearTimeout(utterance.timeout))\n this.utterances = []\n this.emit(\n 'Failed',\n pending.flatMap((utterance) => utterance.audio)\n )\n }\n}\n\n/** Sample rate of a raw PCM input format, undefined for an encoded one */\nfunction pcmSampleRate(format: GradiumInputFormat): number | undefined {\n if (format === 'pcm') return 24000\n const match = format.match(/^pcm_(\\d+)$/)\n return match ? Number(match[1]) : undefined\n}\n","export type GradiumOutputFormat =\n | 'pcm'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n\nexport type GradiumInputFormat =\n | 'pcm'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n | 'pcm_48000'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n\nexport type GradiumRegion = 'eu' | 'us'\n\n/** Rates Gradium streams raw PCM at */\nexport type GradiumSampleRate = 8000 | 16000 | 24000 | 48000\n\nexport interface GradiumTTSOptions {\n apiKey: string\n voiceId: string\n modelName?: string\n /**\n * Rate of the audio, in Hz, 48000 by default, the highest one offered. It\n * goes to the client as is, so the voice is never resampled on the server.\n */\n sampleRate?: GradiumSampleRate\n /**\n * @deprecated Use `sampleRate`. The client only plays raw PCM, so only `pcm`\n * and the `pcm_*` formats work.\n */\n outputFormat?: GradiumOutputFormat\n region?: GradiumRegion\n jsonConfig?: GradiumJsonConfig\n connectionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\nexport interface GradiumJsonConfig {\n padding_bonus?: number // Speed control: -4.0 to 4.0\n temp?: number // Temperature: 0 to 1.4, default 0.7\n cfg_coef?: number // Voice similarity: 1.0 to 4.0, default 2.0\n}\n\nexport const DEFAULT_MODEL_NAME = 'default'\nexport const DEFAULT_SAMPLE_RATE: GradiumSampleRate = 48000\n/** @deprecated Use `DEFAULT_SAMPLE_RATE` */\nexport const DEFAULT_OUTPUT_FORMAT: GradiumOutputFormat = 'pcm'\nexport const DEFAULT_INPUT_FORMAT: GradiumInputFormat = 'pcm_16000'\nexport const DEFAULT_REGION: GradiumRegion = 'eu'\n// Gradium refuses a transcription without a language, \"any\" detects it\nexport const DEFAULT_LANGUAGE = 'any'\n\nconst PCM_FORMATS: Record<GradiumSampleRate, GradiumOutputFormat> = {\n 8000: 'pcm_8000',\n 16000: 'pcm_16000',\n 24000: 'pcm_24000',\n 48000: 'pcm', // Gradium names its 48 kHz format without a rate\n}\n\n/** The `output_format` asked of Gradium */\nexport function getOutputFormat(\n options: GradiumTTSOptions\n): GradiumOutputFormat {\n if (options.sampleRate) return PCM_FORMATS[options.sampleRate]\n return options.outputFormat ?? PCM_FORMATS[DEFAULT_SAMPLE_RATE]\n}\n\n/** Rate of the audio Gradium sends, read from its `output_format` */\nexport function getSampleRate(options: GradiumTTSOptions): number {\n const format = getOutputFormat(options)\n if (format === 'pcm') return 48000\n const match = /^pcm_(\\d+)$/.exec(format)\n return match ? Number(match[1]) : DEFAULT_SAMPLE_RATE\n}\n\n// Speech-to-text (ASR) types\n\nexport interface GradiumASRJsonConfig {\n language?: string\n target_language?: string\n delay_in_frames?: number // 0 to 80, each frame = 80ms\n temp?: number\n padding_bonus?: number\n}\n\nexport interface GradiumSTTOptions {\n apiKey: string\n modelName?: string\n inputFormat?: GradiumInputFormat\n language?: string\n region?: GradiumRegion\n jsonConfig?: GradiumASRJsonConfig\n connectionTimeout?: number\n transcriptionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\n// Client -> Server ASR messages\n\nexport interface GradiumASRSetupMessage {\n type: 'setup'\n model_name: string\n input_format: string\n json_config?: GradiumASRJsonConfig\n}\n\nexport interface GradiumAudioMessage {\n type: 'audio'\n audio: string // Base64 encoded audio data\n}\n\nexport interface GradiumFlushMessage {\n type: 'flush'\n flush_id: number\n}\n\n// Server -> Client ASR messages\n\nexport type GradiumASRResponse =\n | GradiumReadyResponse\n | GradiumTextResponse\n | GradiumEndTextResponse\n | GradiumStepResponse\n | GradiumFlushedResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumTextResponse {\n type: 'text'\n text: string\n start_s?: number\n stream_id?: number\n}\n\nexport interface GradiumEndTextResponse {\n type: 'end_text'\n stop_s?: number\n stream_id?: number\n}\n\nexport interface GradiumStepResponse {\n type: 'step'\n vad?: Array<{ horizon_s: number; inactivity_prob: number }>\n step_idx?: number\n step_duration_s?: number\n total_duration_s?: number\n}\n\nexport interface GradiumFlushedResponse {\n type: 'flushed'\n flush_id: number\n}\n\n// Client -> Server messages\n\nexport interface GradiumSetupMessage {\n type: 'setup'\n voice_id: string\n model_name: string\n output_format: string\n close_ws_on_eos?: boolean\n json_config?: GradiumJsonConfig\n client_req_id?: string\n}\n\nexport interface GradiumTextMessage {\n type: 'text'\n text: string\n client_req_id?: string\n}\n\nexport interface GradiumEosMessage {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\n// Server -> Client messages\n\nexport type GradiumResponse =\n | GradiumReadyResponse\n | GradiumAudioResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumReadyResponse {\n type: 'ready'\n request_id?: string\n delay_in_frames?: number // Frames of 80ms the transcription lags behind the audio\n}\n\nexport interface GradiumAudioResponse {\n type: 'audio'\n audio: string // Base64 encoded PCM data\n client_req_id?: string\n}\n\nexport interface GradiumEosResponse {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\nexport interface GradiumErrorResponse {\n type: 'error'\n message: string\n code?: number\n}\n","import {\n cachedVoices,\n MicdropVoice,\n ReconnectingSocket,\n TTS,\n voiceGender,\n voicesForLanguage,\n} from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n DEFAULT_MODEL_NAME,\n DEFAULT_REGION,\n GradiumAudioResponse,\n GradiumEosMessage,\n GradiumRegion,\n GradiumResponse,\n GradiumSetupMessage,\n GradiumTextMessage,\n GradiumTTSOptions,\n getOutputFormat,\n getSampleRate,\n} from './types'\n\n// API Reference: https://gradium.ai/api_docs.html\n\n/**\n * The utterance Gradium is synthesizing. While there is one the TTS is\n * speaking, and when there is none it is idle: every way out of a request\n * (end of stream, server error, cancel, text stream error, give up) clears it,\n * so nothing about it outlives it.\n */\ninterface GradiumRequest {\n id: string\n /** Complete words received so far, sent again whole on a new connection */\n words: string\n /** Start of a word, held until a space completes it or the stream ends */\n partial: string\n /** The text stream ended, so Gradium was told no more text follows */\n ended: boolean\n}\n\n/** A voice as the Gradium API lists it, the fields read here */\ninterface GradiumVoice {\n uid: string\n name: string\n description?: string | null\n gender?: string | null\n locale?: string | null\n language?: string | null\n is_catalog?: boolean\n}\n\nconst VOICES_PAGE_SIZE = 500 // Voices per page of the listing\n\nexport class GradiumTTS extends TTS {\n /**\n * Lists the voices the account can speak with: its own, then the Gradium\n * catalog, over a thousand voices.\n *\n * `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.\n *\n * The list is fetched once and shared by every call of the process for a\n * day. `refresh` fetches it again, for a voice added a moment ago.\n */\n static listVoices(options: {\n apiKey: string\n region?: GradiumRegion\n language?: string\n refresh?: boolean\n }): Promise<MicdropVoice[]> {\n const region = options.region ?? DEFAULT_REGION\n return cachedVoices(\n ['gradium', region],\n options.apiKey,\n () => fetchVoices(options.apiKey, region),\n options.refresh\n ).then((voices) => voicesForLanguage(voices, options.language))\n }\n\n private socket: ReconnectingSocket\n // Numbers the audio requests, and only moves when there is something to say\n private counter = 0\n // Bumped by every speak() and every cancel(), so a request claimed late can\n // tell whether it is still the one that should be heard\n private generation = 0\n private request?: GradiumRequest\n\n get sampleRate() {\n return getSampleRate(this.options)\n }\n\n constructor(private readonly options: GradiumTTSOptions) {\n super()\n\n const region = options.region ?? DEFAULT_REGION\n this.socket = new ReconnectingSocket({\n url: `wss://${region}.api.gradium.ai/api/speech/tts`,\n clientOptions: { headers: { 'x-api-key': options.apiKey } },\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let request: GradiumRequest | undefined\n\n // Claiming the request is deferred until there is something to say.\n //\n // Taking the next id right away would silence the utterance still playing,\n // because incoming audio is matched against the current id and anything\n // older is dropped. A stream that never carries a word, which is what an\n // answer skipped by a tool or by onBeforeAnswer hands over, would then cut\n // the assistant off mid-sentence for nothing.\n const claimRequest = () => {\n if (request) return request\n // Cancelled, or superseded by another speak(), before the first word\n if (this.generation !== generation) return\n request = {\n id: (++this.counter).toString(),\n words: '',\n partial: '',\n ended: false,\n }\n this.request = request\n // Each speak() is its own multiplexed request, opened by a setup stamped\n // with its client_req_id, so the server binds the voice to it.\n // Multiplexing is what lets the connection serve several utterances\n // (sequential mode only works for the first request on a socket). Without\n // a connection, the setup goes out on open.\n this.sendSetup(request.id)\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n return request\n }\n\n textStream.on('data', (chunk: Buffer) => {\n const request = claimRequest()\n if (!request || this.request !== request) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n\n // Only complete words go out, up to the last space\n const spaceIndex = text.lastIndexOf(' ')\n if (spaceIndex === -1) {\n request.partial += text\n return\n }\n const words = request.partial + text.slice(0, spaceIndex + 1)\n request.partial = text.slice(spaceIndex + 1)\n request.words += words\n // Without a connection the words wait, and go out whole on open\n this.sendTranscript(words, request.id)\n })\n\n textStream.on('error', (error) => {\n if (!request || this.request !== request) return\n this.log('Error in text stream, cancelling the audio', error)\n this.stopRequest()\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is no request to close and no audio to\n // wait for\n if (!request || this.request !== request) return\n request.ended = true\n this.sendEnd(request)\n })\n }\n\n /**\n * Stops the current utterance. Gradium can only stop a synthesis by closing\n * its connection, so a new one opens right away.\n */\n cancel() {\n this.generation++\n if (!this.request) return\n this.log('Cancel')\n this.stopRequest()\n }\n\n destroy() {\n // Destroyed first, so the cancel() that super.destroy() runs opens no new\n // connection\n this.socket.destroy()\n super.destroy()\n this.request = undefined\n }\n\n /** Forgets the current request, and closes the connection that carries it */\n private stopRequest() {\n this.request = undefined\n // Free of charge on the retry budget, and a speak() that follows lands its\n // setup and text on the new connection\n this.socket.restart()\n }\n\n private sendSetup(clientReqId: string) {\n const sent = this.socket.send(\n JSON.stringify({\n type: 'setup',\n voice_id: this.options.voiceId,\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n output_format: getOutputFormat(this.options),\n close_ws_on_eos: false,\n json_config: this.options.jsonConfig,\n client_req_id: clientReqId,\n } satisfies GradiumSetupMessage)\n )\n if (sent) this.log(`Sent setup (client_req_id=${clientReqId})`)\n }\n\n private sendTranscript(text: string, clientReqId: string) {\n const sent = this.socket.send(\n JSON.stringify({\n type: 'text',\n text,\n client_req_id: clientReqId,\n } satisfies GradiumTextMessage)\n )\n if (sent) this.log(`Sent transcript: \"${text}\"`)\n }\n\n /** Sends the last partial word, then the end of the text */\n private sendEnd(request: GradiumRequest) {\n if (request.partial.trim()) {\n this.sendTranscript(request.partial, request.id)\n }\n const sent = this.socket.send(\n JSON.stringify({\n type: 'end_of_stream',\n client_req_id: request.id,\n } satisfies GradiumEosMessage)\n )\n if (sent) this.log('Sent end_of_stream')\n }\n\n // A new connection has no session for the request under way: its setup and\n // its words go out again, the setup first, with the same client_req_id\n private onOpen = () => {\n const request = this.request\n if (!request) return\n this.log('Sending the request again')\n this.sendSetup(request.id)\n if (request.words) this.sendTranscript(request.words, request.id)\n if (request.ended) this.sendEnd(request)\n }\n\n private onMessage = (data: RawData) => {\n let message: GradiumResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n switch (message.type) {\n case 'ready':\n // The server emits one ready per setup, informational only\n this.log('Server ready', message.request_id ?? '')\n break\n\n case 'audio':\n this.processAudioMessage(message)\n break\n\n case 'end_of_stream':\n // Ignore if from a previous request\n if (\n !this.request ||\n (message.client_req_id !== undefined &&\n message.client_req_id !== this.request.id)\n )\n return\n this.log('Audio ended')\n this.request = undefined\n break\n\n case 'error':\n // The connection carries one request at a time, since cancel() closes\n // it, so the error is about the current one, which gets no more audio.\n // Its text is handed over, so a FallbackTTS can speak it instead, and\n // a fresh connection keeps the next request clear of it.\n this.log('Error:', message.message, message.code)\n if (!this.request) break\n this.onFailed()\n this.socket.restart()\n break\n }\n }\n\n private processAudioMessage(message: GradiumAudioResponse) {\n // Ignore messages from previous requests\n if (!this.request || message.client_req_id !== this.request.id) return\n\n const chunk = Buffer.from(message.audio, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n }\n\n private onFailed = () => {\n const request = this.request\n this.request = undefined\n const text = request ? request.words + request.partial : ''\n this.emit('Failed', text ? [text] : [])\n }\n}\n\n/** Reads every page of the voices, the catalog included */\nasync function fetchVoices(\n apiKey: string,\n region: GradiumRegion\n): Promise<MicdropVoice[]> {\n const voices: MicdropVoice[] = []\n // The listing returns a bare array, a short page is the last one\n for (let skip = 0; ; skip += VOICES_PAGE_SIZE) {\n const params = new URLSearchParams({\n include_catalog: 'true',\n limit: String(VOICES_PAGE_SIZE),\n skip: String(skip),\n })\n const response = await fetch(\n `https://${region}.api.gradium.ai/api/voices/?${params}`,\n { headers: { 'x-api-key': apiKey } }\n )\n if (!response.ok) throw new Error(`Gradium answered ${response.status}`)\n const page = (await response.json()) as GradiumVoice[]\n voices.push(...page.map(toMicdropVoice))\n if (page.length < VOICES_PAGE_SIZE) break\n }\n return voices\n}\n\nfunction toMicdropVoice(voice: GradiumVoice): MicdropVoice {\n // en-us becomes en-US, and a dialect such as fr-fr-x-southern its locale\n const [language, region] = (voice.locale || voice.language || '').split('-')\n const locale = region ? `${language}-${region.toUpperCase()}` : language\n return {\n id: voice.uid,\n name: voice.name,\n languages: locale ? [locale] : undefined,\n gender: voiceGender(voice.gender),\n description: voice.description || undefined,\n custom: voice.is_catalog === false,\n }\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EAEA;AAAA,OACK;;;ACiDA,IAAM,qBAAqB;AAC3B,IAAM,sBAAyC;AAE/C,IAAM,wBAA6C;AACnD,IAAM,uBAA2C;AACjD,IAAM,iBAAgC;AAEtC,IAAM,mBAAmB;AAEhC,IAAM,cAA8D;AAAA,EAClE,KAAM;AAAA,EACN,MAAO;AAAA,EACP,MAAO;AAAA,EACP,MAAO;AAAA;AACT;AAGO,SAAS,gBACd,SACqB;AACrB,MAAI,QAAQ,WAAY,QAAO,YAAY,QAAQ,UAAU;AAC7D,SAAO,QAAQ,gBAAgB,YAAY,mBAAmB;AAChE;AAGO,SAAS,cAAc,SAAoC;AAChE,QAAM,SAAS,gBAAgB,OAAO;AACtC,MAAI,WAAW,MAAO,QAAO;AAC7B,QAAM,QAAQ,cAAc,KAA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ACZ,WAAW,SAAS,CAAC,MAAM,IAAI;AAAA,IAC/B,QAAQ,YAAY,MAAM,MAAM;AAAA,IAChC,aAAa,MAAM,eAAe;AAAA,IAClC,QAAQ,MAAM,eAAe;AAAA,EAC/B;AACF;","names":["ReconnectingSocket","ReconnectingSocket","request"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micdrop/gradium",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Gradium TTS implementation for @micdrop/server",
5
5
  "author": "Godefroy de Compreignac",
6
6
  "license": "MIT",
@@ -39,7 +39,7 @@
39
39
  "ws": "^8.19.0"
40
40
  },
41
41
  "peerDependencies": {
42
- "@micdrop/server": ">=2.10.0"
42
+ "@micdrop/server": ">=2.11.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/ws": "^8.18.1",