@micdrop/gradium 1.0.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 ADDED
@@ -0,0 +1,52 @@
1
+ # @micdrop/gradium
2
+
3
+ [Micdrop website](https://micdrop.dev) | [Documentation](https://micdrop.dev/docs/ai-integration/provided-integrations/gradium)
4
+
5
+ Gradium TTS implementation for [@micdrop/server](https://micdrop.dev/docs/server).
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @micdrop/gradium
11
+ ```
12
+
13
+ ## Gradium TTS (Text-to-Speech)
14
+
15
+ ### Usage
16
+
17
+ ```typescript
18
+ import { GradiumTTS } from '@micdrop/gradium'
19
+ import { MicdropServer } from '@micdrop/server'
20
+
21
+ const tts = new GradiumTTS({
22
+ apiKey: process.env.GRADIUM_API_KEY || '',
23
+ voiceId: 'YTpq7expH9539ERJ', // Gradium voice ID
24
+ modelName: 'default', // Optional: model name
25
+ outputFormat: 'pcm_16000', // Optional: audio format
26
+ region: 'eu', // Optional: 'eu' or 'us'
27
+ jsonConfig: {
28
+ // Optional: advanced voice settings
29
+ temp: 0.7, // Temperature (0-1.4)
30
+ cfg_coef: 2.0, // Voice similarity (1.0-4.0)
31
+ padding_bonus: 0, // Speed control (-4.0 to 4.0)
32
+ },
33
+ })
34
+
35
+ // Use with MicdropServer
36
+ new MicdropServer(socket, {
37
+ tts,
38
+ // ... other options
39
+ })
40
+ ```
41
+
42
+ ## Documentation
43
+
44
+ Read full [documentation of the Gradium integration for Micdrop](https://micdrop.dev/docs/ai-integration/provided-integrations/gradium) on the [website](https://micdrop.dev).
45
+
46
+ ## License
47
+
48
+ MIT
49
+
50
+ ## Author
51
+
52
+ Originally developed for [Raconte.ai](https://www.raconte.ai) and open sourced by [Lonestone](https://www.lonestone.io) ([GitHub](https://github.com/lonestone))
@@ -0,0 +1,82 @@
1
+ import { TTS } from '@micdrop/server';
2
+ import { Readable } from 'stream';
3
+
4
+ type GradiumOutputFormat = 'pcm' | 'wav' | 'opus' | 'ulaw_8000' | 'alaw_8000' | 'pcm_8000' | 'pcm_16000' | 'pcm_24000';
5
+ type GradiumRegion = 'eu' | 'us';
6
+ interface GradiumTTSOptions {
7
+ apiKey: string;
8
+ voiceId: string;
9
+ modelName?: string;
10
+ outputFormat?: GradiumOutputFormat;
11
+ region?: GradiumRegion;
12
+ jsonConfig?: GradiumJsonConfig;
13
+ retryDelay?: number;
14
+ maxRetry?: number;
15
+ }
16
+ interface GradiumJsonConfig {
17
+ padding_bonus?: number;
18
+ temp?: number;
19
+ cfg_coef?: number;
20
+ }
21
+ declare const DEFAULT_MODEL_NAME = "default";
22
+ declare const DEFAULT_OUTPUT_FORMAT: GradiumOutputFormat;
23
+ declare const DEFAULT_REGION: GradiumRegion;
24
+ interface GradiumSetupMessage {
25
+ type: 'setup';
26
+ voice_id: string;
27
+ model_name: string;
28
+ output_format: string;
29
+ close_ws_on_eos?: boolean;
30
+ json_config?: GradiumJsonConfig;
31
+ }
32
+ interface GradiumTextMessage {
33
+ type: 'text';
34
+ text: string;
35
+ client_req_id?: string;
36
+ }
37
+ interface GradiumEosMessage {
38
+ type: 'end_of_stream';
39
+ client_req_id?: string;
40
+ }
41
+ type GradiumResponse = GradiumReadyResponse | GradiumAudioResponse | GradiumEosResponse | GradiumErrorResponse;
42
+ interface GradiumReadyResponse {
43
+ type: 'ready';
44
+ request_id?: string;
45
+ }
46
+ interface GradiumAudioResponse {
47
+ type: 'audio';
48
+ audio: string;
49
+ client_req_id?: string;
50
+ }
51
+ interface GradiumEosResponse {
52
+ type: 'end_of_stream';
53
+ client_req_id?: string;
54
+ }
55
+ interface GradiumErrorResponse {
56
+ type: 'error';
57
+ message: string;
58
+ code?: number;
59
+ }
60
+
61
+ declare class GradiumTTS extends TTS {
62
+ private readonly options;
63
+ private socket?;
64
+ private initPromise;
65
+ private counter;
66
+ private isProcessing;
67
+ private reconnectTimeout?;
68
+ private textSent;
69
+ private textBuffer;
70
+ private retryCount;
71
+ constructor(options: GradiumTTSOptions);
72
+ speak(textStream: Readable): void;
73
+ cancel(): void;
74
+ destroy(): void;
75
+ private getEndpoint;
76
+ private initWS;
77
+ private sendTranscript;
78
+ private processAudioMessage;
79
+ private reconnect;
80
+ }
81
+
82
+ export { DEFAULT_MODEL_NAME, DEFAULT_OUTPUT_FORMAT, DEFAULT_REGION, type GradiumAudioResponse, type GradiumEosMessage, type GradiumEosResponse, type GradiumErrorResponse, type GradiumJsonConfig, type GradiumOutputFormat, type GradiumReadyResponse, type GradiumRegion, type GradiumResponse, type GradiumSetupMessage, GradiumTTS, type GradiumTTSOptions, type GradiumTextMessage };
@@ -0,0 +1,82 @@
1
+ import { TTS } from '@micdrop/server';
2
+ import { Readable } from 'stream';
3
+
4
+ type GradiumOutputFormat = 'pcm' | 'wav' | 'opus' | 'ulaw_8000' | 'alaw_8000' | 'pcm_8000' | 'pcm_16000' | 'pcm_24000';
5
+ type GradiumRegion = 'eu' | 'us';
6
+ interface GradiumTTSOptions {
7
+ apiKey: string;
8
+ voiceId: string;
9
+ modelName?: string;
10
+ outputFormat?: GradiumOutputFormat;
11
+ region?: GradiumRegion;
12
+ jsonConfig?: GradiumJsonConfig;
13
+ retryDelay?: number;
14
+ maxRetry?: number;
15
+ }
16
+ interface GradiumJsonConfig {
17
+ padding_bonus?: number;
18
+ temp?: number;
19
+ cfg_coef?: number;
20
+ }
21
+ declare const DEFAULT_MODEL_NAME = "default";
22
+ declare const DEFAULT_OUTPUT_FORMAT: GradiumOutputFormat;
23
+ declare const DEFAULT_REGION: GradiumRegion;
24
+ interface GradiumSetupMessage {
25
+ type: 'setup';
26
+ voice_id: string;
27
+ model_name: string;
28
+ output_format: string;
29
+ close_ws_on_eos?: boolean;
30
+ json_config?: GradiumJsonConfig;
31
+ }
32
+ interface GradiumTextMessage {
33
+ type: 'text';
34
+ text: string;
35
+ client_req_id?: string;
36
+ }
37
+ interface GradiumEosMessage {
38
+ type: 'end_of_stream';
39
+ client_req_id?: string;
40
+ }
41
+ type GradiumResponse = GradiumReadyResponse | GradiumAudioResponse | GradiumEosResponse | GradiumErrorResponse;
42
+ interface GradiumReadyResponse {
43
+ type: 'ready';
44
+ request_id?: string;
45
+ }
46
+ interface GradiumAudioResponse {
47
+ type: 'audio';
48
+ audio: string;
49
+ client_req_id?: string;
50
+ }
51
+ interface GradiumEosResponse {
52
+ type: 'end_of_stream';
53
+ client_req_id?: string;
54
+ }
55
+ interface GradiumErrorResponse {
56
+ type: 'error';
57
+ message: string;
58
+ code?: number;
59
+ }
60
+
61
+ declare class GradiumTTS extends TTS {
62
+ private readonly options;
63
+ private socket?;
64
+ private initPromise;
65
+ private counter;
66
+ private isProcessing;
67
+ private reconnectTimeout?;
68
+ private textSent;
69
+ private textBuffer;
70
+ private retryCount;
71
+ constructor(options: GradiumTTSOptions);
72
+ speak(textStream: Readable): void;
73
+ cancel(): void;
74
+ destroy(): void;
75
+ private getEndpoint;
76
+ private initWS;
77
+ private sendTranscript;
78
+ private processAudioMessage;
79
+ private reconnect;
80
+ }
81
+
82
+ export { DEFAULT_MODEL_NAME, DEFAULT_OUTPUT_FORMAT, DEFAULT_REGION, type GradiumAudioResponse, type GradiumEosMessage, type GradiumEosResponse, type GradiumErrorResponse, type GradiumJsonConfig, type GradiumOutputFormat, type GradiumReadyResponse, type GradiumRegion, type GradiumResponse, type GradiumSetupMessage, GradiumTTS, type GradiumTTSOptions, type GradiumTextMessage };
package/dist/index.js ADDED
@@ -0,0 +1,254 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
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
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ DEFAULT_MODEL_NAME: () => DEFAULT_MODEL_NAME,
34
+ DEFAULT_OUTPUT_FORMAT: () => DEFAULT_OUTPUT_FORMAT,
35
+ DEFAULT_REGION: () => DEFAULT_REGION,
36
+ GradiumTTS: () => GradiumTTS
37
+ });
38
+ module.exports = __toCommonJS(index_exports);
39
+
40
+ // src/GradiumTTS.ts
41
+ var import_server = require("@micdrop/server");
42
+ var import_ws = __toESM(require("ws"));
43
+
44
+ // src/types.ts
45
+ var DEFAULT_MODEL_NAME = "default";
46
+ var DEFAULT_OUTPUT_FORMAT = "pcm_16000";
47
+ var DEFAULT_REGION = "eu";
48
+
49
+ // src/GradiumTTS.ts
50
+ var DEFAULT_RETRY_DELAY = 1e3;
51
+ var DEFAULT_MAX_RETRY = 3;
52
+ var GradiumTTS = class extends import_server.TTS {
53
+ constructor(options) {
54
+ super();
55
+ this.options = options;
56
+ this.counter = 0;
57
+ this.isProcessing = false;
58
+ this.textSent = "";
59
+ this.textBuffer = "";
60
+ this.retryCount = 0;
61
+ this.initPromise = this.initWS().catch((error) => {
62
+ console.error("[GradiumTTS] Connection error:", error);
63
+ this.reconnect();
64
+ });
65
+ }
66
+ speak(textStream) {
67
+ this.counter++;
68
+ const counter = this.counter;
69
+ const clientReqId = counter.toString();
70
+ this.isProcessing = true;
71
+ this.textSent = "";
72
+ this.textBuffer = "";
73
+ textStream.on("data", async (chunk) => {
74
+ if (counter !== this.counter) return;
75
+ const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
76
+ this.textSent += text;
77
+ await this.initPromise;
78
+ const spaceIndex = text.lastIndexOf(" ");
79
+ if (spaceIndex === -1) {
80
+ this.textBuffer += text;
81
+ } else {
82
+ this.sendTranscript(
83
+ this.textBuffer + text.slice(0, spaceIndex + 1),
84
+ clientReqId
85
+ );
86
+ this.textBuffer = text.slice(spaceIndex + 1);
87
+ }
88
+ });
89
+ textStream.on("error", (error) => {
90
+ this.log("Error in text stream, ending audio stream", error);
91
+ this.isProcessing = false;
92
+ });
93
+ textStream.on("end", async () => {
94
+ if (counter !== this.counter) return;
95
+ await this.initPromise;
96
+ if (this.textBuffer.trim()) {
97
+ this.sendTranscript(this.textBuffer, clientReqId);
98
+ this.textBuffer = "";
99
+ }
100
+ this.socket?.send(
101
+ JSON.stringify({
102
+ type: "end_of_stream",
103
+ client_req_id: clientReqId
104
+ })
105
+ );
106
+ this.log("Sent end_of_stream");
107
+ });
108
+ }
109
+ cancel() {
110
+ if (!this.isProcessing) return;
111
+ this.log("Cancel");
112
+ this.isProcessing = false;
113
+ this.textSent = "";
114
+ this.counter++;
115
+ if (this.socket?.readyState === import_ws.default.OPEN) {
116
+ this.socket.close(1e3);
117
+ }
118
+ }
119
+ destroy() {
120
+ super.destroy();
121
+ if (this.reconnectTimeout) {
122
+ clearTimeout(this.reconnectTimeout);
123
+ this.reconnectTimeout = void 0;
124
+ }
125
+ this.socket?.removeAllListeners();
126
+ if (this.socket?.readyState === import_ws.default.OPEN) {
127
+ this.socket?.close(1e3);
128
+ }
129
+ this.socket = void 0;
130
+ this.isProcessing = false;
131
+ }
132
+ getEndpoint() {
133
+ const region = this.options.region ?? DEFAULT_REGION;
134
+ return `wss://${region}.api.gradium.ai/api/speech/tts`;
135
+ }
136
+ async initWS() {
137
+ return new Promise((resolve) => {
138
+ const socket = new import_ws.default(this.getEndpoint(), {
139
+ headers: {
140
+ "x-api-key": this.options.apiKey
141
+ }
142
+ });
143
+ this.socket = socket;
144
+ socket.addEventListener("open", () => {
145
+ this.log("Connection opened");
146
+ socket.send(
147
+ JSON.stringify({
148
+ type: "setup",
149
+ voice_id: this.options.voiceId,
150
+ model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,
151
+ output_format: this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT,
152
+ close_ws_on_eos: false,
153
+ json_config: this.options.jsonConfig
154
+ })
155
+ );
156
+ });
157
+ socket.addEventListener("error", (error) => {
158
+ this.log("WebSocket error:", error);
159
+ });
160
+ socket.addEventListener("close", ({ code, reason }) => {
161
+ this.socket?.removeAllListeners();
162
+ this.socket = void 0;
163
+ if (code !== 1e3) {
164
+ this.reconnect();
165
+ } else {
166
+ this.log("Connection closed", { code, reason });
167
+ if (!this.isProcessing) {
168
+ this.reconnect();
169
+ }
170
+ }
171
+ });
172
+ socket.addEventListener("message", (event) => {
173
+ try {
174
+ const message = JSON.parse(event.data.toString());
175
+ switch (message.type) {
176
+ case "ready":
177
+ this.log("Server ready");
178
+ resolve();
179
+ break;
180
+ case "audio":
181
+ this.processAudioMessage(message);
182
+ break;
183
+ case "end_of_stream":
184
+ if ("client_req_id" in message && message.client_req_id !== this.counter.toString())
185
+ return;
186
+ this.log("Audio ended");
187
+ this.isProcessing = false;
188
+ this.textSent = "";
189
+ break;
190
+ case "error":
191
+ this.log("Error:", message.message, message.code);
192
+ break;
193
+ }
194
+ } catch {
195
+ this.log("Error parsing message", event.data);
196
+ }
197
+ });
198
+ });
199
+ }
200
+ sendTranscript(text, clientReqId) {
201
+ this.socket?.send(
202
+ JSON.stringify({
203
+ type: "text",
204
+ text,
205
+ client_req_id: clientReqId
206
+ })
207
+ );
208
+ this.log(`Sent transcript: "${text}"`);
209
+ }
210
+ processAudioMessage(message) {
211
+ if (message.client_req_id !== this.counter.toString()) return;
212
+ const chunk = Buffer.from(message.audio, "base64");
213
+ this.log(`Received audio chunk (${chunk.length} bytes)`);
214
+ this.emit("Audio", chunk);
215
+ }
216
+ reconnect() {
217
+ this.retryCount++;
218
+ if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
219
+ this.log("Max retries reached, giving up");
220
+ this.emit("Failed", [this.textSent]);
221
+ return;
222
+ }
223
+ this.initPromise = new Promise((resolve) => {
224
+ this.log("Reconnecting...");
225
+ this.reconnectTimeout = setTimeout(() => {
226
+ this.reconnectTimeout = void 0;
227
+ this.initWS().then(() => {
228
+ this.retryCount = 0;
229
+ if (this.textSent.length > 0) {
230
+ this.log("Sending text chunks again");
231
+ this.socket?.send(
232
+ JSON.stringify({
233
+ type: "text",
234
+ text: this.textSent,
235
+ client_req_id: this.counter.toString()
236
+ })
237
+ );
238
+ }
239
+ }).then(resolve).catch((error) => {
240
+ this.log("Reconnection error:", error);
241
+ this.reconnect();
242
+ });
243
+ }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
244
+ });
245
+ }
246
+ };
247
+ // Annotate the CommonJS export names for ESM import in node:
248
+ 0 && (module.exports = {
249
+ DEFAULT_MODEL_NAME,
250
+ DEFAULT_OUTPUT_FORMAT,
251
+ DEFAULT_REGION,
252
+ GradiumTTS
253
+ });
254
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/GradiumTTS.ts","../src/types.ts"],"sourcesContent":["export * from './GradiumTTS'\nexport * from './types'\n","import { TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport WebSocket 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\nconst DEFAULT_RETRY_DELAY = 1000\nconst DEFAULT_MAX_RETRY = 3\n\nexport class GradiumTTS extends TTS {\n private socket?: WebSocket\n private initPromise: Promise<void>\n private counter = 0\n private isProcessing = false\n private reconnectTimeout?: NodeJS.Timeout\n private textSent = ''\n private textBuffer = ''\n private retryCount = 0\n\n constructor(private readonly options: GradiumTTSOptions) {\n super()\n\n this.initPromise = this.initWS().catch((error) => {\n console.error('[GradiumTTS] Connection error:', error)\n this.reconnect()\n })\n }\n\n speak(textStream: Readable) {\n this.counter++\n const counter = this.counter\n const clientReqId = counter.toString()\n this.isProcessing = true\n this.textSent = ''\n this.textBuffer = ''\n\n textStream.on('data', async (chunk: Buffer) => {\n if (counter !== this.counter) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n this.textSent += text\n\n await this.initPromise\n\n // Buffer text and only send complete words (flush on last space)\n const spaceIndex = text.lastIndexOf(' ')\n if (spaceIndex === -1) {\n this.textBuffer += text\n } else {\n this.sendTranscript(\n this.textBuffer + text.slice(0, spaceIndex + 1),\n clientReqId\n )\n this.textBuffer = text.slice(spaceIndex + 1)\n }\n })\n\n textStream.on('error', (error) => {\n this.log('Error in text stream, ending audio stream', error)\n this.isProcessing = false\n })\n\n textStream.on('end', async () => {\n if (counter !== this.counter) return\n await this.initPromise\n\n // Send remaining buffered text\n if (this.textBuffer.trim()) {\n this.sendTranscript(this.textBuffer, clientReqId)\n this.textBuffer = ''\n }\n\n // Signal end of text input for this request\n this.socket?.send(\n JSON.stringify({\n type: 'end_of_stream',\n client_req_id: clientReqId,\n } satisfies GradiumEosMessage)\n )\n this.log('Sent end_of_stream')\n })\n }\n\n cancel() {\n if (!this.isProcessing) return\n this.log('Cancel')\n this.isProcessing = false\n this.textSent = ''\n\n // Increment counter to ignore messages from previous calls\n this.counter++\n\n // Close and reconnect to cancel ongoing synthesis\n if (this.socket?.readyState === WebSocket.OPEN) {\n this.socket.close(1000)\n }\n }\n\n destroy() {\n super.destroy()\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout)\n this.reconnectTimeout = undefined\n }\n this.socket?.removeAllListeners()\n if (this.socket?.readyState === WebSocket.OPEN) {\n this.socket?.close(1000)\n }\n this.socket = undefined\n this.isProcessing = false\n }\n\n private getEndpoint() {\n const region = this.options.region ?? DEFAULT_REGION\n return `wss://${region}.api.gradium.ai/api/speech/tts`\n }\n\n private async initWS() {\n return new Promise<void>((resolve) => {\n const socket = new WebSocket(this.getEndpoint(), {\n headers: {\n 'x-api-key': this.options.apiKey,\n },\n })\n this.socket = socket\n\n socket.addEventListener('open', () => {\n this.log('Connection opened')\n\n // Send setup message\n 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 } satisfies GradiumSetupMessage)\n )\n })\n\n socket.addEventListener('error', (error) => {\n this.log('WebSocket error:', error)\n })\n\n socket.addEventListener('close', ({ code, reason }) => {\n this.socket?.removeAllListeners()\n this.socket = undefined\n\n if (code !== 1000) {\n this.reconnect()\n } else {\n this.log('Connection closed', { code, reason })\n // Reconnect for next usage\n if (!this.isProcessing) {\n this.reconnect()\n }\n }\n })\n\n socket.addEventListener('message', (event) => {\n try {\n const message: GradiumResponse = JSON.parse(event.data.toString())\n\n switch (message.type) {\n case 'ready':\n this.log('Server ready')\n resolve()\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 'client_req_id' in message &&\n message.client_req_id !== this.counter.toString()\n )\n return\n this.log('Audio ended')\n this.isProcessing = false\n this.textSent = ''\n break\n\n case 'error':\n this.log('Error:', message.message, message.code)\n break\n }\n } catch {\n this.log('Error parsing message', event.data)\n }\n })\n })\n }\n\n private sendTranscript(text: string, clientReqId: string) {\n this.socket?.send(\n JSON.stringify({\n type: 'text',\n text,\n client_req_id: clientReqId,\n } satisfies GradiumTextMessage)\n )\n this.log(`Sent transcript: \"${text}\"`)\n }\n\n private processAudioMessage(message: GradiumAudioResponse) {\n // Ignore messages from previous requests\n if (message.client_req_id !== this.counter.toString()) 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 reconnect() {\n this.retryCount++\n if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {\n this.log('Max retries reached, giving up')\n this.emit('Failed', [this.textSent])\n return\n }\n\n this.initPromise = new Promise((resolve) => {\n this.log('Reconnecting...')\n this.reconnectTimeout = setTimeout(() => {\n this.reconnectTimeout = undefined\n this.initWS()\n .then(() => {\n this.retryCount = 0\n\n // Resend text if reconnecting during processing\n if (this.textSent.length > 0) {\n this.log('Sending text chunks again')\n this.socket?.send(\n JSON.stringify({\n type: 'text',\n text: this.textSent,\n client_req_id: this.counter.toString(),\n } satisfies GradiumTextMessage)\n )\n }\n })\n .then(resolve)\n .catch((error) => {\n this.log('Reconnection error:', error)\n this.reconnect()\n })\n }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY)\n })\n }\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 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 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_REGION: GradiumRegion = 'eu'\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}\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}\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAoB;AAEpB,gBAAsB;;;AC2Bf,IAAM,qBAAqB;AAC3B,IAAM,wBAA6C;AACnD,IAAM,iBAAgC;;;ADd7C,IAAM,sBAAsB;AAC5B,IAAM,oBAAoB;AAEnB,IAAM,aAAN,cAAyB,kBAAI;AAAA,EAUlC,YAA6B,SAA4B;AACvD,UAAM;AADqB;AAP7B,SAAQ,UAAU;AAClB,SAAQ,eAAe;AAEvB,SAAQ,WAAW;AACnB,SAAQ,aAAa;AACrB,SAAQ,aAAa;AAKnB,SAAK,cAAc,KAAK,OAAO,EAAE,MAAM,CAAC,UAAU;AAChD,cAAQ,MAAM,kCAAkC,KAAK;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YAAY,oBAAoB;AAClE,WAAK,IAAI,gCAAgC;AACzC,WAAK,KAAK,UAAU,CAAC,KAAK,QAAQ,CAAC;AACnC;AAAA,IACF;AAEA,SAAK,cAAc,IAAI,QAAQ,CAAC,YAAY;AAC1C,WAAK,IAAI,iBAAiB;AAC1B,WAAK,mBAAmB,WAAW,MAAM;AACvC,aAAK,mBAAmB;AACxB,aAAK,OAAO,EACT,KAAK,MAAM;AACV,eAAK,aAAa;AAGlB,cAAI,KAAK,SAAS,SAAS,GAAG;AAC5B,iBAAK,IAAI,2BAA2B;AACpC,iBAAK,QAAQ;AAAA,cACX,KAAK,UAAU;AAAA,gBACb,MAAM;AAAA,gBACN,MAAM,KAAK;AAAA,gBACX,eAAe,KAAK,QAAQ,SAAS;AAAA,cACvC,CAA8B;AAAA,YAChC;AAAA,UACF;AAAA,QACF,CAAC,EACA,KAAK,OAAO,EACZ,MAAM,CAAC,UAAU;AAChB,eAAK,IAAI,uBAAuB,KAAK;AACrC,eAAK,UAAU;AAAA,QACjB,CAAC;AAAA,MACL,GAAG,KAAK,QAAQ,cAAc,mBAAmB;AAAA,IACnD,CAAC;AAAA,EACH;AACF;","names":["WebSocket"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,214 @@
1
+ // src/GradiumTTS.ts
2
+ import { TTS } from "@micdrop/server";
3
+ import WebSocket from "ws";
4
+
5
+ // src/types.ts
6
+ var DEFAULT_MODEL_NAME = "default";
7
+ var DEFAULT_OUTPUT_FORMAT = "pcm_16000";
8
+ var DEFAULT_REGION = "eu";
9
+
10
+ // src/GradiumTTS.ts
11
+ var DEFAULT_RETRY_DELAY = 1e3;
12
+ var DEFAULT_MAX_RETRY = 3;
13
+ var GradiumTTS = class extends TTS {
14
+ constructor(options) {
15
+ super();
16
+ this.options = options;
17
+ this.counter = 0;
18
+ this.isProcessing = false;
19
+ this.textSent = "";
20
+ this.textBuffer = "";
21
+ this.retryCount = 0;
22
+ this.initPromise = this.initWS().catch((error) => {
23
+ console.error("[GradiumTTS] Connection error:", error);
24
+ this.reconnect();
25
+ });
26
+ }
27
+ speak(textStream) {
28
+ this.counter++;
29
+ const counter = this.counter;
30
+ const clientReqId = counter.toString();
31
+ this.isProcessing = true;
32
+ this.textSent = "";
33
+ this.textBuffer = "";
34
+ textStream.on("data", async (chunk) => {
35
+ if (counter !== this.counter) return;
36
+ const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
37
+ this.textSent += text;
38
+ await this.initPromise;
39
+ const spaceIndex = text.lastIndexOf(" ");
40
+ if (spaceIndex === -1) {
41
+ this.textBuffer += text;
42
+ } else {
43
+ this.sendTranscript(
44
+ this.textBuffer + text.slice(0, spaceIndex + 1),
45
+ clientReqId
46
+ );
47
+ this.textBuffer = text.slice(spaceIndex + 1);
48
+ }
49
+ });
50
+ textStream.on("error", (error) => {
51
+ this.log("Error in text stream, ending audio stream", error);
52
+ this.isProcessing = false;
53
+ });
54
+ textStream.on("end", async () => {
55
+ if (counter !== this.counter) return;
56
+ await this.initPromise;
57
+ if (this.textBuffer.trim()) {
58
+ this.sendTranscript(this.textBuffer, clientReqId);
59
+ this.textBuffer = "";
60
+ }
61
+ this.socket?.send(
62
+ JSON.stringify({
63
+ type: "end_of_stream",
64
+ client_req_id: clientReqId
65
+ })
66
+ );
67
+ this.log("Sent end_of_stream");
68
+ });
69
+ }
70
+ cancel() {
71
+ if (!this.isProcessing) return;
72
+ this.log("Cancel");
73
+ this.isProcessing = false;
74
+ this.textSent = "";
75
+ this.counter++;
76
+ if (this.socket?.readyState === WebSocket.OPEN) {
77
+ this.socket.close(1e3);
78
+ }
79
+ }
80
+ destroy() {
81
+ super.destroy();
82
+ if (this.reconnectTimeout) {
83
+ clearTimeout(this.reconnectTimeout);
84
+ this.reconnectTimeout = void 0;
85
+ }
86
+ this.socket?.removeAllListeners();
87
+ if (this.socket?.readyState === WebSocket.OPEN) {
88
+ this.socket?.close(1e3);
89
+ }
90
+ this.socket = void 0;
91
+ this.isProcessing = false;
92
+ }
93
+ getEndpoint() {
94
+ const region = this.options.region ?? DEFAULT_REGION;
95
+ return `wss://${region}.api.gradium.ai/api/speech/tts`;
96
+ }
97
+ async initWS() {
98
+ return new Promise((resolve) => {
99
+ const socket = new WebSocket(this.getEndpoint(), {
100
+ headers: {
101
+ "x-api-key": this.options.apiKey
102
+ }
103
+ });
104
+ this.socket = socket;
105
+ socket.addEventListener("open", () => {
106
+ this.log("Connection opened");
107
+ socket.send(
108
+ JSON.stringify({
109
+ type: "setup",
110
+ voice_id: this.options.voiceId,
111
+ model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,
112
+ output_format: this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT,
113
+ close_ws_on_eos: false,
114
+ json_config: this.options.jsonConfig
115
+ })
116
+ );
117
+ });
118
+ socket.addEventListener("error", (error) => {
119
+ this.log("WebSocket error:", error);
120
+ });
121
+ socket.addEventListener("close", ({ code, reason }) => {
122
+ this.socket?.removeAllListeners();
123
+ this.socket = void 0;
124
+ if (code !== 1e3) {
125
+ this.reconnect();
126
+ } else {
127
+ this.log("Connection closed", { code, reason });
128
+ if (!this.isProcessing) {
129
+ this.reconnect();
130
+ }
131
+ }
132
+ });
133
+ socket.addEventListener("message", (event) => {
134
+ try {
135
+ const message = JSON.parse(event.data.toString());
136
+ switch (message.type) {
137
+ case "ready":
138
+ this.log("Server ready");
139
+ resolve();
140
+ break;
141
+ case "audio":
142
+ this.processAudioMessage(message);
143
+ break;
144
+ case "end_of_stream":
145
+ if ("client_req_id" in message && message.client_req_id !== this.counter.toString())
146
+ return;
147
+ this.log("Audio ended");
148
+ this.isProcessing = false;
149
+ this.textSent = "";
150
+ break;
151
+ case "error":
152
+ this.log("Error:", message.message, message.code);
153
+ break;
154
+ }
155
+ } catch {
156
+ this.log("Error parsing message", event.data);
157
+ }
158
+ });
159
+ });
160
+ }
161
+ sendTranscript(text, clientReqId) {
162
+ this.socket?.send(
163
+ JSON.stringify({
164
+ type: "text",
165
+ text,
166
+ client_req_id: clientReqId
167
+ })
168
+ );
169
+ this.log(`Sent transcript: "${text}"`);
170
+ }
171
+ processAudioMessage(message) {
172
+ if (message.client_req_id !== this.counter.toString()) return;
173
+ const chunk = Buffer.from(message.audio, "base64");
174
+ this.log(`Received audio chunk (${chunk.length} bytes)`);
175
+ this.emit("Audio", chunk);
176
+ }
177
+ reconnect() {
178
+ this.retryCount++;
179
+ if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
180
+ this.log("Max retries reached, giving up");
181
+ this.emit("Failed", [this.textSent]);
182
+ return;
183
+ }
184
+ this.initPromise = new Promise((resolve) => {
185
+ this.log("Reconnecting...");
186
+ this.reconnectTimeout = setTimeout(() => {
187
+ this.reconnectTimeout = void 0;
188
+ this.initWS().then(() => {
189
+ this.retryCount = 0;
190
+ if (this.textSent.length > 0) {
191
+ this.log("Sending text chunks again");
192
+ this.socket?.send(
193
+ JSON.stringify({
194
+ type: "text",
195
+ text: this.textSent,
196
+ client_req_id: this.counter.toString()
197
+ })
198
+ );
199
+ }
200
+ }).then(resolve).catch((error) => {
201
+ this.log("Reconnection error:", error);
202
+ this.reconnect();
203
+ });
204
+ }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
205
+ });
206
+ }
207
+ };
208
+ export {
209
+ DEFAULT_MODEL_NAME,
210
+ DEFAULT_OUTPUT_FORMAT,
211
+ DEFAULT_REGION,
212
+ GradiumTTS
213
+ };
214
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/GradiumTTS.ts","../src/types.ts"],"sourcesContent":["import { TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport WebSocket 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\nconst DEFAULT_RETRY_DELAY = 1000\nconst DEFAULT_MAX_RETRY = 3\n\nexport class GradiumTTS extends TTS {\n private socket?: WebSocket\n private initPromise: Promise<void>\n private counter = 0\n private isProcessing = false\n private reconnectTimeout?: NodeJS.Timeout\n private textSent = ''\n private textBuffer = ''\n private retryCount = 0\n\n constructor(private readonly options: GradiumTTSOptions) {\n super()\n\n this.initPromise = this.initWS().catch((error) => {\n console.error('[GradiumTTS] Connection error:', error)\n this.reconnect()\n })\n }\n\n speak(textStream: Readable) {\n this.counter++\n const counter = this.counter\n const clientReqId = counter.toString()\n this.isProcessing = true\n this.textSent = ''\n this.textBuffer = ''\n\n textStream.on('data', async (chunk: Buffer) => {\n if (counter !== this.counter) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n this.textSent += text\n\n await this.initPromise\n\n // Buffer text and only send complete words (flush on last space)\n const spaceIndex = text.lastIndexOf(' ')\n if (spaceIndex === -1) {\n this.textBuffer += text\n } else {\n this.sendTranscript(\n this.textBuffer + text.slice(0, spaceIndex + 1),\n clientReqId\n )\n this.textBuffer = text.slice(spaceIndex + 1)\n }\n })\n\n textStream.on('error', (error) => {\n this.log('Error in text stream, ending audio stream', error)\n this.isProcessing = false\n })\n\n textStream.on('end', async () => {\n if (counter !== this.counter) return\n await this.initPromise\n\n // Send remaining buffered text\n if (this.textBuffer.trim()) {\n this.sendTranscript(this.textBuffer, clientReqId)\n this.textBuffer = ''\n }\n\n // Signal end of text input for this request\n this.socket?.send(\n JSON.stringify({\n type: 'end_of_stream',\n client_req_id: clientReqId,\n } satisfies GradiumEosMessage)\n )\n this.log('Sent end_of_stream')\n })\n }\n\n cancel() {\n if (!this.isProcessing) return\n this.log('Cancel')\n this.isProcessing = false\n this.textSent = ''\n\n // Increment counter to ignore messages from previous calls\n this.counter++\n\n // Close and reconnect to cancel ongoing synthesis\n if (this.socket?.readyState === WebSocket.OPEN) {\n this.socket.close(1000)\n }\n }\n\n destroy() {\n super.destroy()\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout)\n this.reconnectTimeout = undefined\n }\n this.socket?.removeAllListeners()\n if (this.socket?.readyState === WebSocket.OPEN) {\n this.socket?.close(1000)\n }\n this.socket = undefined\n this.isProcessing = false\n }\n\n private getEndpoint() {\n const region = this.options.region ?? DEFAULT_REGION\n return `wss://${region}.api.gradium.ai/api/speech/tts`\n }\n\n private async initWS() {\n return new Promise<void>((resolve) => {\n const socket = new WebSocket(this.getEndpoint(), {\n headers: {\n 'x-api-key': this.options.apiKey,\n },\n })\n this.socket = socket\n\n socket.addEventListener('open', () => {\n this.log('Connection opened')\n\n // Send setup message\n 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 } satisfies GradiumSetupMessage)\n )\n })\n\n socket.addEventListener('error', (error) => {\n this.log('WebSocket error:', error)\n })\n\n socket.addEventListener('close', ({ code, reason }) => {\n this.socket?.removeAllListeners()\n this.socket = undefined\n\n if (code !== 1000) {\n this.reconnect()\n } else {\n this.log('Connection closed', { code, reason })\n // Reconnect for next usage\n if (!this.isProcessing) {\n this.reconnect()\n }\n }\n })\n\n socket.addEventListener('message', (event) => {\n try {\n const message: GradiumResponse = JSON.parse(event.data.toString())\n\n switch (message.type) {\n case 'ready':\n this.log('Server ready')\n resolve()\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 'client_req_id' in message &&\n message.client_req_id !== this.counter.toString()\n )\n return\n this.log('Audio ended')\n this.isProcessing = false\n this.textSent = ''\n break\n\n case 'error':\n this.log('Error:', message.message, message.code)\n break\n }\n } catch {\n this.log('Error parsing message', event.data)\n }\n })\n })\n }\n\n private sendTranscript(text: string, clientReqId: string) {\n this.socket?.send(\n JSON.stringify({\n type: 'text',\n text,\n client_req_id: clientReqId,\n } satisfies GradiumTextMessage)\n )\n this.log(`Sent transcript: \"${text}\"`)\n }\n\n private processAudioMessage(message: GradiumAudioResponse) {\n // Ignore messages from previous requests\n if (message.client_req_id !== this.counter.toString()) 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 reconnect() {\n this.retryCount++\n if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {\n this.log('Max retries reached, giving up')\n this.emit('Failed', [this.textSent])\n return\n }\n\n this.initPromise = new Promise((resolve) => {\n this.log('Reconnecting...')\n this.reconnectTimeout = setTimeout(() => {\n this.reconnectTimeout = undefined\n this.initWS()\n .then(() => {\n this.retryCount = 0\n\n // Resend text if reconnecting during processing\n if (this.textSent.length > 0) {\n this.log('Sending text chunks again')\n this.socket?.send(\n JSON.stringify({\n type: 'text',\n text: this.textSent,\n client_req_id: this.counter.toString(),\n } satisfies GradiumTextMessage)\n )\n }\n })\n .then(resolve)\n .catch((error) => {\n this.log('Reconnection error:', error)\n this.reconnect()\n })\n }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY)\n })\n }\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 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 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_REGION: GradiumRegion = 'eu'\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}\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}\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"],"mappings":";AAAA,SAAS,WAAW;AAEpB,OAAO,eAAe;;;AC2Bf,IAAM,qBAAqB;AAC3B,IAAM,wBAA6C;AACnD,IAAM,iBAAgC;;;ADd7C,IAAM,sBAAsB;AAC5B,IAAM,oBAAoB;AAEnB,IAAM,aAAN,cAAyB,IAAI;AAAA,EAUlC,YAA6B,SAA4B;AACvD,UAAM;AADqB;AAP7B,SAAQ,UAAU;AAClB,SAAQ,eAAe;AAEvB,SAAQ,WAAW;AACnB,SAAQ,aAAa;AACrB,SAAQ,aAAa;AAKnB,SAAK,cAAc,KAAK,OAAO,EAAE,MAAM,CAAC,UAAU;AAChD,cAAQ,MAAM,kCAAkC,KAAK;AACrD,WAAK,UAAU;AAAA,IACjB,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,YAAsB;AAC1B,SAAK;AACL,UAAM,UAAU,KAAK;AACrB,UAAM,cAAc,QAAQ,SAAS;AACrC,SAAK,eAAe;AACpB,SAAK,WAAW;AAChB,SA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MAAM;AACvC,aAAK,mBAAmB;AACxB,aAAK,OAAO,EACT,KAAK,MAAM;AACV,eAAK,aAAa;AAGlB,cAAI,KAAK,SAAS,SAAS,GAAG;AAC5B,iBAAK,IAAI,2BAA2B;AACpC,iBAAK,QAAQ;AAAA,cACX,KAAK,UAAU;AAAA,gBACb,MAAM;AAAA,gBACN,MAAM,KAAK;AAAA,gBACX,eAAe,KAAK,QAAQ,SAAS;AAAA,cACvC,CAA8B;AAAA,YAChC;AAAA,UACF;AAAA,QACF,CAAC,EACA,KAAK,OAAO,EACZ,MAAM,CAAC,UAAU;AAChB,eAAK,IAAI,uBAAuB,KAAK;AACrC,eAAK,UAAU;AAAA,QACjB,CAAC;AAAA,MACL,GAAG,KAAK,QAAQ,cAAc,mBAAmB;AAAA,IACnD,CAAC;AAAA,EACH;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@micdrop/gradium",
3
+ "version": "1.0.0",
4
+ "description": "Gradium TTS implementation for @micdrop/server",
5
+ "author": "Lonestone",
6
+ "license": "MIT",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.mjs",
9
+ "types": "./dist/index.d.ts",
10
+ "scripts": {
11
+ "build": "tsup",
12
+ "dev": "tsup --watch",
13
+ "clean": "rm -rf dist",
14
+ "typecheck": "tsc --noEmit",
15
+ "prepublishOnly": "npm run build"
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/Godefroy/micdrop.git",
23
+ "directory": "packages/gradium"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "keywords": [
29
+ "microphone",
30
+ "audio",
31
+ "browser",
32
+ "voice",
33
+ "conversation",
34
+ "gradium",
35
+ "tts"
36
+ ],
37
+ "dependencies": {
38
+ "ws": "^8.18.2"
39
+ },
40
+ "peerDependencies": {
41
+ "@micdrop/server": "*"
42
+ },
43
+ "devDependencies": {
44
+ "@types/ws": "^8.18.1",
45
+ "tsup": "^8.3.6",
46
+ "typescript": "^5.7.3"
47
+ }
48
+ }