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