@micdrop/cartesia 1.1.6 → 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 +7 -7
- package/dist/index.d.mts +25 -12
- package/dist/index.d.ts +25 -12
- package/dist/index.js +102 -185
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +103 -176
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -20,10 +20,10 @@ import { MicdropServer } from '@micdrop/server'
|
|
|
20
20
|
|
|
21
21
|
const tts = new CartesiaTTS({
|
|
22
22
|
apiKey: process.env.CARTESIA_API_KEY || '',
|
|
23
|
-
modelId: 'sonic-
|
|
23
|
+
modelId: 'sonic-3.6', // Cartesia model ID
|
|
24
24
|
voiceId: 'a0e99841-438c-4a64-b679-ae501e7d6091', // Voice ID
|
|
25
25
|
language: 'en', // Optional: specify language
|
|
26
|
-
speed:
|
|
26
|
+
generationConfig: { speed: 1.1 }, // Optional: speed, volume and emotion
|
|
27
27
|
})
|
|
28
28
|
|
|
29
29
|
// Use with MicdropServer
|
|
@@ -41,7 +41,7 @@ import { Readable } from 'stream'
|
|
|
41
41
|
|
|
42
42
|
const tts = new CartesiaTTS({
|
|
43
43
|
apiKey: process.env.CARTESIA_API_KEY || '',
|
|
44
|
-
modelId: 'sonic-
|
|
44
|
+
modelId: 'sonic-3.6',
|
|
45
45
|
voiceId: 'a0e99841-438c-4a64-b679-ae501e7d6091',
|
|
46
46
|
})
|
|
47
47
|
|
|
@@ -54,10 +54,10 @@ tts.speak(Readable.from(['Hello! ', 'What can I do for you?']))
|
|
|
54
54
|
|
|
55
55
|
### Events
|
|
56
56
|
|
|
57
|
-
| Event | Payload | Description
|
|
58
|
-
| -------- | ---------- |
|
|
59
|
-
| `Audio` | `Buffer` | A chunk of audio, PCM 16 bits, 16 kHz, mono, ready to be played.
|
|
60
|
-
| `Failed` | `string[]` |
|
|
57
|
+
| Event | Payload | Description |
|
|
58
|
+
| -------- | ---------- | --------------------------------------------------------------------------------------------------------- |
|
|
59
|
+
| `Audio` | `Buffer` | A chunk of audio, PCM 16 bits, 16 kHz, mono, ready to be played. |
|
|
60
|
+
| `Failed` | `string[]` | Every reconnection attempt failed, with the text that stayed unspoken. The next utterance connects again. |
|
|
61
61
|
|
|
62
62
|
See the [TTS interface](https://micdrop.dev/docs/ai-integration/custom-integrations/custom-tts) for the full contract.
|
|
63
63
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { TTS } from '@micdrop/server';
|
|
2
2
|
import { Readable } from 'stream';
|
|
3
3
|
|
|
4
|
-
type CartesiaLanguage = 'en' | 'fr' | 'de' | 'es' | 'pt' | 'zh' | 'ja' | 'hi' | 'it' | 'ko' | 'nl' | 'pl' | 'ru' | 'sv' | 'tr';
|
|
4
|
+
type CartesiaLanguage = 'en' | 'fr' | 'de' | 'es' | 'pt' | 'zh' | 'ja' | 'hi' | 'it' | 'ko' | 'nl' | 'pl' | 'ru' | 'sv' | 'tr' | 'tl' | 'bg' | 'ro' | 'ar' | 'cs' | 'el' | 'fi' | 'hr' | 'ms' | 'sk' | 'da' | 'ta' | 'uk' | 'hu' | 'no' | 'vi' | 'bn' | 'th' | 'he' | 'ka' | 'id' | 'te' | 'gu' | 'kn' | 'ml' | 'mr' | 'pa' | 'or' | 'ur';
|
|
5
|
+
type CartesiaEmotion = 'neutral' | 'happy' | 'excited' | 'enthusiastic' | 'elated' | 'euphoric' | 'triumphant' | 'amazed' | 'surprised' | 'flirtatious' | 'curious' | 'content' | 'peaceful' | 'serene' | 'calm' | 'grateful' | 'affectionate' | 'trust' | 'sympathetic' | 'anticipation' | 'mysterious' | 'angry' | 'mad' | 'outraged' | 'frustrated' | 'agitated' | 'threatened' | 'disgusted' | 'contempt' | 'envious' | 'sarcastic' | 'ironic' | 'sad' | 'dejected' | 'melancholic' | 'disappointed' | 'hurt' | 'guilty' | 'bored' | 'tired' | 'rejected' | 'nostalgic' | 'wistful' | 'apologetic' | 'hesitant' | 'insecure' | 'confused' | 'resigned' | 'anxious' | 'panicked' | 'alarmed' | 'scared' | 'proud' | 'confident' | 'distant' | 'skeptical' | 'contemplative' | 'determined';
|
|
6
|
+
interface CartesiaGenerationConfig {
|
|
7
|
+
speed?: number;
|
|
8
|
+
volume?: number;
|
|
9
|
+
emotion?: CartesiaEmotion;
|
|
10
|
+
}
|
|
5
11
|
type CartesiaPayload = CartesiaTranscriptPayload | CartesiaCancelPayload;
|
|
6
12
|
interface CartesiaTranscriptPayload {
|
|
7
13
|
model_id: string;
|
|
@@ -18,6 +24,7 @@ interface CartesiaTranscriptPayload {
|
|
|
18
24
|
language?: CartesiaLanguage;
|
|
19
25
|
duration?: number;
|
|
20
26
|
speed?: 'fast' | 'normal' | 'slow';
|
|
27
|
+
generation_config?: CartesiaGenerationConfig;
|
|
21
28
|
context_id?: string;
|
|
22
29
|
continue?: boolean;
|
|
23
30
|
max_buffer_delay_ms?: number;
|
|
@@ -89,30 +96,36 @@ interface CartesiaTTSOptions {
|
|
|
89
96
|
modelId: string;
|
|
90
97
|
voiceId: string;
|
|
91
98
|
language?: CartesiaLanguage;
|
|
99
|
+
/** @deprecated Sonic 3 models read `generationConfig.speed` instead */
|
|
92
100
|
speed?: 'fast' | 'normal' | 'slow';
|
|
101
|
+
generationConfig?: CartesiaGenerationConfig;
|
|
102
|
+
/** How long a connection attempt may take, 5000 ms by default */
|
|
93
103
|
connectionTimeout?: number;
|
|
104
|
+
/** How long to wait before a new attempt, 1000 ms by default */
|
|
94
105
|
retryDelay?: number;
|
|
106
|
+
/**
|
|
107
|
+
* How many attempts in a row may fail before giving up and emitting
|
|
108
|
+
* `Failed`, 3 by default. The next utterance tries to connect again.
|
|
109
|
+
*/
|
|
95
110
|
maxRetry?: number;
|
|
96
111
|
}
|
|
97
112
|
declare class CartesiaTTS extends TTS {
|
|
98
113
|
private readonly options;
|
|
99
|
-
private socket
|
|
100
|
-
private initPromise;
|
|
114
|
+
private socket;
|
|
101
115
|
private counter;
|
|
102
116
|
private generation;
|
|
103
|
-
private
|
|
104
|
-
private reconnectTimeout?;
|
|
105
|
-
private connectionTimeout?;
|
|
106
|
-
private textSent;
|
|
107
|
-
private retryCount;
|
|
108
|
-
private destroyed;
|
|
117
|
+
private context?;
|
|
109
118
|
constructor(options: CartesiaTTSOptions);
|
|
110
119
|
speak(textStream: Readable): void;
|
|
111
120
|
cancel(): void;
|
|
112
121
|
destroy(): void;
|
|
122
|
+
/** Asks Cartesia to drop the current context, and forgets it */
|
|
123
|
+
private stopContext;
|
|
124
|
+
private sendText;
|
|
113
125
|
private getConfig;
|
|
114
|
-
private
|
|
115
|
-
private
|
|
126
|
+
private onOpen;
|
|
127
|
+
private onMessage;
|
|
128
|
+
private onFailed;
|
|
116
129
|
}
|
|
117
130
|
|
|
118
|
-
export { type CartesiaCancelPayload, type CartesiaChunkResponse, type CartesiaDoneResponse, type CartesiaErrorResponse, type CartesiaFlushDoneResponse, type CartesiaLanguage, type CartesiaPayload, type CartesiaPhonemeTimestampsResponse, type CartesiaResponse, CartesiaTTS, type CartesiaTTSOptions, type CartesiaTimestampsResponse, type CartesiaTranscriptPayload };
|
|
131
|
+
export { type CartesiaCancelPayload, type CartesiaChunkResponse, type CartesiaDoneResponse, type CartesiaEmotion, type CartesiaErrorResponse, type CartesiaFlushDoneResponse, type CartesiaGenerationConfig, type CartesiaLanguage, type CartesiaPayload, type CartesiaPhonemeTimestampsResponse, type CartesiaResponse, CartesiaTTS, type CartesiaTTSOptions, type CartesiaTimestampsResponse, type CartesiaTranscriptPayload };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { TTS } from '@micdrop/server';
|
|
2
2
|
import { Readable } from 'stream';
|
|
3
3
|
|
|
4
|
-
type CartesiaLanguage = 'en' | 'fr' | 'de' | 'es' | 'pt' | 'zh' | 'ja' | 'hi' | 'it' | 'ko' | 'nl' | 'pl' | 'ru' | 'sv' | 'tr';
|
|
4
|
+
type CartesiaLanguage = 'en' | 'fr' | 'de' | 'es' | 'pt' | 'zh' | 'ja' | 'hi' | 'it' | 'ko' | 'nl' | 'pl' | 'ru' | 'sv' | 'tr' | 'tl' | 'bg' | 'ro' | 'ar' | 'cs' | 'el' | 'fi' | 'hr' | 'ms' | 'sk' | 'da' | 'ta' | 'uk' | 'hu' | 'no' | 'vi' | 'bn' | 'th' | 'he' | 'ka' | 'id' | 'te' | 'gu' | 'kn' | 'ml' | 'mr' | 'pa' | 'or' | 'ur';
|
|
5
|
+
type CartesiaEmotion = 'neutral' | 'happy' | 'excited' | 'enthusiastic' | 'elated' | 'euphoric' | 'triumphant' | 'amazed' | 'surprised' | 'flirtatious' | 'curious' | 'content' | 'peaceful' | 'serene' | 'calm' | 'grateful' | 'affectionate' | 'trust' | 'sympathetic' | 'anticipation' | 'mysterious' | 'angry' | 'mad' | 'outraged' | 'frustrated' | 'agitated' | 'threatened' | 'disgusted' | 'contempt' | 'envious' | 'sarcastic' | 'ironic' | 'sad' | 'dejected' | 'melancholic' | 'disappointed' | 'hurt' | 'guilty' | 'bored' | 'tired' | 'rejected' | 'nostalgic' | 'wistful' | 'apologetic' | 'hesitant' | 'insecure' | 'confused' | 'resigned' | 'anxious' | 'panicked' | 'alarmed' | 'scared' | 'proud' | 'confident' | 'distant' | 'skeptical' | 'contemplative' | 'determined';
|
|
6
|
+
interface CartesiaGenerationConfig {
|
|
7
|
+
speed?: number;
|
|
8
|
+
volume?: number;
|
|
9
|
+
emotion?: CartesiaEmotion;
|
|
10
|
+
}
|
|
5
11
|
type CartesiaPayload = CartesiaTranscriptPayload | CartesiaCancelPayload;
|
|
6
12
|
interface CartesiaTranscriptPayload {
|
|
7
13
|
model_id: string;
|
|
@@ -18,6 +24,7 @@ interface CartesiaTranscriptPayload {
|
|
|
18
24
|
language?: CartesiaLanguage;
|
|
19
25
|
duration?: number;
|
|
20
26
|
speed?: 'fast' | 'normal' | 'slow';
|
|
27
|
+
generation_config?: CartesiaGenerationConfig;
|
|
21
28
|
context_id?: string;
|
|
22
29
|
continue?: boolean;
|
|
23
30
|
max_buffer_delay_ms?: number;
|
|
@@ -89,30 +96,36 @@ interface CartesiaTTSOptions {
|
|
|
89
96
|
modelId: string;
|
|
90
97
|
voiceId: string;
|
|
91
98
|
language?: CartesiaLanguage;
|
|
99
|
+
/** @deprecated Sonic 3 models read `generationConfig.speed` instead */
|
|
92
100
|
speed?: 'fast' | 'normal' | 'slow';
|
|
101
|
+
generationConfig?: CartesiaGenerationConfig;
|
|
102
|
+
/** How long a connection attempt may take, 5000 ms by default */
|
|
93
103
|
connectionTimeout?: number;
|
|
104
|
+
/** How long to wait before a new attempt, 1000 ms by default */
|
|
94
105
|
retryDelay?: number;
|
|
106
|
+
/**
|
|
107
|
+
* How many attempts in a row may fail before giving up and emitting
|
|
108
|
+
* `Failed`, 3 by default. The next utterance tries to connect again.
|
|
109
|
+
*/
|
|
95
110
|
maxRetry?: number;
|
|
96
111
|
}
|
|
97
112
|
declare class CartesiaTTS extends TTS {
|
|
98
113
|
private readonly options;
|
|
99
|
-
private socket
|
|
100
|
-
private initPromise;
|
|
114
|
+
private socket;
|
|
101
115
|
private counter;
|
|
102
116
|
private generation;
|
|
103
|
-
private
|
|
104
|
-
private reconnectTimeout?;
|
|
105
|
-
private connectionTimeout?;
|
|
106
|
-
private textSent;
|
|
107
|
-
private retryCount;
|
|
108
|
-
private destroyed;
|
|
117
|
+
private context?;
|
|
109
118
|
constructor(options: CartesiaTTSOptions);
|
|
110
119
|
speak(textStream: Readable): void;
|
|
111
120
|
cancel(): void;
|
|
112
121
|
destroy(): void;
|
|
122
|
+
/** Asks Cartesia to drop the current context, and forgets it */
|
|
123
|
+
private stopContext;
|
|
124
|
+
private sendText;
|
|
113
125
|
private getConfig;
|
|
114
|
-
private
|
|
115
|
-
private
|
|
126
|
+
private onOpen;
|
|
127
|
+
private onMessage;
|
|
128
|
+
private onFailed;
|
|
116
129
|
}
|
|
117
130
|
|
|
118
|
-
export { type CartesiaCancelPayload, type CartesiaChunkResponse, type CartesiaDoneResponse, type CartesiaErrorResponse, type CartesiaFlushDoneResponse, type CartesiaLanguage, type CartesiaPayload, type CartesiaPhonemeTimestampsResponse, type CartesiaResponse, CartesiaTTS, type CartesiaTTSOptions, type CartesiaTimestampsResponse, type CartesiaTranscriptPayload };
|
|
131
|
+
export { type CartesiaCancelPayload, type CartesiaChunkResponse, type CartesiaDoneResponse, type CartesiaEmotion, type CartesiaErrorResponse, type CartesiaFlushDoneResponse, type CartesiaGenerationConfig, type CartesiaLanguage, type CartesiaPayload, type CartesiaPhonemeTimestampsResponse, type CartesiaResponse, CartesiaTTS, type CartesiaTTSOptions, type CartesiaTimestampsResponse, type CartesiaTranscriptPayload };
|
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
|
|
@@ -36,108 +26,131 @@ module.exports = __toCommonJS(index_exports);
|
|
|
36
26
|
|
|
37
27
|
// src/CartesiaTTS.ts
|
|
38
28
|
var import_server = require("@micdrop/server");
|
|
39
|
-
var import_ws = __toESM(require("ws"));
|
|
40
|
-
var DEFAULT_CONNECTION_TIMEOUT = 5e3;
|
|
41
|
-
var DEFAULT_RETRY_DELAY = 1e3;
|
|
42
|
-
var DEFAULT_MAX_RETRY = 3;
|
|
43
29
|
var CartesiaTTS = class extends import_server.TTS {
|
|
44
30
|
constructor(options) {
|
|
45
31
|
super();
|
|
46
32
|
this.options = options;
|
|
33
|
+
// Numbers the audio contexts, and only moves when there is something to say
|
|
47
34
|
this.counter = 0;
|
|
48
35
|
// Bumped by every speak() and every cancel(), so a context claimed late can
|
|
49
|
-
// tell whether it is still the one that should be heard
|
|
50
|
-
// `counter`, which numbers the audio contexts and must only move when there
|
|
51
|
-
// is something to synthesize.
|
|
36
|
+
// tell whether it is still the one that should be heard
|
|
52
37
|
this.generation = 0;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
//
|
|
56
|
-
this.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
38
|
+
// A new connection knows nothing of the utterance under way: the text read
|
|
39
|
+
// before the first connection opened, or lost with the previous one, goes out
|
|
40
|
+
// again whole
|
|
41
|
+
this.onOpen = () => {
|
|
42
|
+
const context = this.context;
|
|
43
|
+
if (!context) return;
|
|
44
|
+
if (context.text) {
|
|
45
|
+
this.log("Sending text again");
|
|
46
|
+
this.sendText(context.id, context.text, true);
|
|
47
|
+
}
|
|
48
|
+
if (context.ended) this.sendText(context.id, "", false);
|
|
49
|
+
};
|
|
50
|
+
this.onMessage = (data) => {
|
|
51
|
+
let message;
|
|
52
|
+
try {
|
|
53
|
+
message = JSON.parse(data.toString());
|
|
54
|
+
} catch {
|
|
55
|
+
this.log("Error parsing message", data);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (!this.context || this.context.id !== message.context_id) return;
|
|
59
|
+
switch (message.type) {
|
|
60
|
+
case "chunk":
|
|
61
|
+
const chunk = Buffer.from(message.data, "base64");
|
|
62
|
+
this.log(`Received audio chunk (${chunk.length} bytes)`);
|
|
63
|
+
this.emit("Audio", chunk);
|
|
64
|
+
break;
|
|
65
|
+
case "done":
|
|
66
|
+
this.log("Audio ended");
|
|
67
|
+
this.context = void 0;
|
|
68
|
+
break;
|
|
69
|
+
case "error":
|
|
70
|
+
this.log("Error", message.error);
|
|
71
|
+
this.context = void 0;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
this.onFailed = () => {
|
|
76
|
+
const text = this.context?.text;
|
|
77
|
+
this.context = void 0;
|
|
78
|
+
this.emit("Failed", text ? [text] : []);
|
|
79
|
+
};
|
|
80
|
+
this.socket = new import_server.ReconnectingSocket({
|
|
81
|
+
url: `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`,
|
|
82
|
+
connectionTimeout: options.connectionTimeout,
|
|
83
|
+
retryDelay: options.retryDelay,
|
|
84
|
+
maxRetry: options.maxRetry,
|
|
85
|
+
log: (...message) => this.log(...message)
|
|
61
86
|
});
|
|
87
|
+
this.socket.on("Open", this.onOpen);
|
|
88
|
+
this.socket.on("Message", this.onMessage);
|
|
89
|
+
this.socket.on("Failed", this.onFailed);
|
|
90
|
+
this.socket.connect();
|
|
62
91
|
}
|
|
63
92
|
speak(textStream) {
|
|
64
93
|
const generation = ++this.generation;
|
|
65
|
-
let
|
|
66
|
-
let context_id = "";
|
|
94
|
+
let context;
|
|
67
95
|
const claimContext = () => {
|
|
68
|
-
if (
|
|
69
|
-
if (this.generation !== generation) return
|
|
70
|
-
this.counter
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.textSent = "";
|
|
75
|
-
return true;
|
|
96
|
+
if (context) return context;
|
|
97
|
+
if (this.generation !== generation) return;
|
|
98
|
+
context = { id: (++this.counter).toString(), text: "", ended: false };
|
|
99
|
+
this.context = context;
|
|
100
|
+
this.socket.connect();
|
|
101
|
+
return context;
|
|
76
102
|
};
|
|
77
|
-
textStream.on("data",
|
|
78
|
-
|
|
79
|
-
if (
|
|
103
|
+
textStream.on("data", (chunk) => {
|
|
104
|
+
const context2 = claimContext();
|
|
105
|
+
if (!context2 || this.context !== context2) return;
|
|
80
106
|
const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
...this.getConfig(),
|
|
86
|
-
transcript: text,
|
|
87
|
-
context_id,
|
|
88
|
-
continue: true
|
|
89
|
-
})
|
|
90
|
-
);
|
|
91
|
-
this.log(`Sent transcript: "${text}"`);
|
|
107
|
+
context2.text += text;
|
|
108
|
+
if (this.sendText(context2.id, text, true)) {
|
|
109
|
+
this.log(`Sent transcript: "${text}"`);
|
|
110
|
+
}
|
|
92
111
|
});
|
|
93
112
|
textStream.on("error", (error) => {
|
|
94
|
-
if (!
|
|
95
|
-
this.log("Error in text stream,
|
|
96
|
-
this.
|
|
113
|
+
if (!context || this.context !== context) return;
|
|
114
|
+
this.log("Error in text stream, cancelling the audio", error);
|
|
115
|
+
this.stopContext();
|
|
97
116
|
});
|
|
98
|
-
textStream.on("end",
|
|
99
|
-
if (!
|
|
100
|
-
|
|
101
|
-
this.
|
|
102
|
-
JSON.stringify({
|
|
103
|
-
...this.getConfig(),
|
|
104
|
-
transcript: "",
|
|
105
|
-
context_id,
|
|
106
|
-
continue: false
|
|
107
|
-
})
|
|
108
|
-
);
|
|
117
|
+
textStream.on("end", () => {
|
|
118
|
+
if (!context || this.context !== context) return;
|
|
119
|
+
context.ended = true;
|
|
120
|
+
this.sendText(context.id, "", false);
|
|
109
121
|
});
|
|
110
122
|
}
|
|
111
123
|
cancel() {
|
|
112
124
|
this.generation++;
|
|
113
|
-
if (!this.
|
|
125
|
+
if (!this.context) return;
|
|
114
126
|
this.log("Cancel");
|
|
115
|
-
this.
|
|
116
|
-
this.textSent = "";
|
|
117
|
-
if (this.socket?.readyState === import_ws.default.OPEN) {
|
|
118
|
-
this.socket.send(
|
|
119
|
-
JSON.stringify({
|
|
120
|
-
context_id: this.counter.toString(),
|
|
121
|
-
cancel: true
|
|
122
|
-
})
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
this.counter++;
|
|
127
|
+
this.stopContext();
|
|
126
128
|
}
|
|
127
129
|
destroy() {
|
|
128
130
|
super.destroy();
|
|
129
|
-
this.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (this.
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
131
|
+
this.socket.destroy();
|
|
132
|
+
this.context = void 0;
|
|
133
|
+
}
|
|
134
|
+
/** Asks Cartesia to drop the current context, and forgets it */
|
|
135
|
+
stopContext() {
|
|
136
|
+
if (!this.context) return;
|
|
137
|
+
this.socket.send(
|
|
138
|
+
JSON.stringify({
|
|
139
|
+
context_id: this.context.id,
|
|
140
|
+
cancel: true
|
|
141
|
+
})
|
|
142
|
+
);
|
|
143
|
+
this.context = void 0;
|
|
144
|
+
}
|
|
145
|
+
sendText(contextId, transcript, more) {
|
|
146
|
+
return this.socket.send(
|
|
147
|
+
JSON.stringify({
|
|
148
|
+
...this.getConfig(),
|
|
149
|
+
transcript,
|
|
150
|
+
context_id: contextId,
|
|
151
|
+
continue: more
|
|
152
|
+
})
|
|
153
|
+
);
|
|
141
154
|
}
|
|
142
155
|
getConfig() {
|
|
143
156
|
return {
|
|
@@ -152,107 +165,11 @@ var CartesiaTTS = class extends import_server.TTS {
|
|
|
152
165
|
sample_rate: 16e3
|
|
153
166
|
},
|
|
154
167
|
language: this.options.language,
|
|
155
|
-
speed: this.options.speed
|
|
168
|
+
speed: this.options.speed,
|
|
169
|
+
generation_config: this.options.generationConfig
|
|
156
170
|
};
|
|
157
171
|
}
|
|
158
|
-
// Connect to Cartesia
|
|
159
|
-
async initWS() {
|
|
160
|
-
return new Promise((resolve, reject) => {
|
|
161
|
-
const socket = new import_ws.default(
|
|
162
|
-
`wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`
|
|
163
|
-
);
|
|
164
|
-
this.socket = socket;
|
|
165
|
-
const timeout = setTimeout(() => {
|
|
166
|
-
this.log("Connection timeout");
|
|
167
|
-
closeSocket(socket);
|
|
168
|
-
this.socket = void 0;
|
|
169
|
-
reject(new Error("WebSocket connection timeout"));
|
|
170
|
-
}, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT);
|
|
171
|
-
this.connectionTimeout = timeout;
|
|
172
|
-
socket.addEventListener("open", () => {
|
|
173
|
-
clearTimeout(timeout);
|
|
174
|
-
this.log("Connection opened");
|
|
175
|
-
resolve();
|
|
176
|
-
});
|
|
177
|
-
socket.addEventListener("error", (error) => {
|
|
178
|
-
clearTimeout(timeout);
|
|
179
|
-
this.log("WebSocket error:", error);
|
|
180
|
-
reject(new Error("WebSocket connection error"));
|
|
181
|
-
});
|
|
182
|
-
socket.addEventListener("close", ({ code, reason }) => {
|
|
183
|
-
clearTimeout(timeout);
|
|
184
|
-
this.socket?.removeAllListeners();
|
|
185
|
-
this.socket = void 0;
|
|
186
|
-
this.isProcessing = false;
|
|
187
|
-
this.log("Connection closed", { code, reason });
|
|
188
|
-
this.reconnect();
|
|
189
|
-
});
|
|
190
|
-
socket.addEventListener("message", (event) => {
|
|
191
|
-
try {
|
|
192
|
-
const message = JSON.parse(event.data.toString());
|
|
193
|
-
if (this.counter.toString() !== message.context_id) return;
|
|
194
|
-
switch (message.type) {
|
|
195
|
-
case "chunk":
|
|
196
|
-
const chunk = Buffer.from(message.data, "base64");
|
|
197
|
-
this.log(`Received audio chunk (${chunk.length} bytes)`);
|
|
198
|
-
this.emit("Audio", chunk);
|
|
199
|
-
break;
|
|
200
|
-
case "done":
|
|
201
|
-
this.log("Audio ended");
|
|
202
|
-
this.isProcessing = false;
|
|
203
|
-
this.textSent = "";
|
|
204
|
-
break;
|
|
205
|
-
case "error":
|
|
206
|
-
this.log("Error", message.error);
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
} catch {
|
|
210
|
-
this.log("Error parsing message", event.data);
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
reconnect() {
|
|
216
|
-
if (this.destroyed) return;
|
|
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
|
-
...this.getConfig(),
|
|
234
|
-
transcript: this.textSent,
|
|
235
|
-
context_id: this.counter.toString(),
|
|
236
|
-
continue: true
|
|
237
|
-
})
|
|
238
|
-
);
|
|
239
|
-
}
|
|
240
|
-
}).then(resolve).catch((error) => {
|
|
241
|
-
this.log("Reconnection error:", error);
|
|
242
|
-
this.reconnect();
|
|
243
|
-
});
|
|
244
|
-
}, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
172
|
};
|
|
248
|
-
function closeSocket(socket) {
|
|
249
|
-
socket.removeAllListeners();
|
|
250
|
-
socket.on("error", () => {
|
|
251
|
-
});
|
|
252
|
-
if (socket.readyState === import_ws.default.OPEN || socket.readyState === import_ws.default.CONNECTING) {
|
|
253
|
-
socket.close(1e3);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
173
|
// Annotate the CommonJS export names for ESM import in node:
|
|
257
174
|
0 && (module.exports = {
|
|
258
175
|
CartesiaTTS
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/CartesiaTTS.ts"],"sourcesContent":["export * from './CartesiaTTS'\nexport * from './types'\n","import { TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport WebSocket from 'ws'\nimport {\n CartesiaCancelPayload,\n CartesiaLanguage,\n CartesiaPayload,\n CartesiaResponse,\n} from './types'\n\nexport interface CartesiaTTSOptions {\n apiKey: string\n modelId: string\n voiceId: string\n language?: CartesiaLanguage\n speed?: 'fast' | 'normal' | 'slow'\n connectionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\nconst DEFAULT_CONNECTION_TIMEOUT = 5000\nconst DEFAULT_RETRY_DELAY = 1000\nconst DEFAULT_MAX_RETRY = 3\nexport class CartesiaTTS extends TTS {\n private socket?: WebSocket\n private initPromise: Promise<void>\n private counter = 0\n // Bumped by every speak() and every cancel(), so a context claimed late can\n // tell whether it is still the one that should be heard. Kept apart from\n // `counter`, which numbers the audio contexts and must only move when there\n // is something to synthesize.\n private generation = 0\n private isProcessing = false\n private reconnectTimeout?: NodeJS.Timeout\n private connectionTimeout?: NodeJS.Timeout\n private textSent: string = '' // Store text chunks to send them again if reconnecting\n private retryCount = 0\n private destroyed = false\n\n constructor(private readonly options: CartesiaTTSOptions) {\n super()\n\n // Setup WebSocket connection\n this.initPromise = this.initWS().catch((error) => {\n console.error('[CartesiaTTS] Connection error:', error)\n this.reconnect()\n })\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let counter = 0\n let context_id = ''\n\n // Claiming the context 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 context and\n // anything older is dropped. A stream that never carries a word, which is\n // what an answer skipped by a tool or by onBeforeAnswer hands over, would\n // then cut the assistant off mid-sentence for nothing.\n const claimContext = () => {\n if (counter) return true\n // Cancelled, or superseded by another speak(), before the first word.\n if (this.generation !== generation) return false\n this.counter++\n counter = this.counter\n context_id = counter.toString()\n this.isProcessing = true\n this.textSent = ''\n return true\n }\n\n textStream.on('data', async (chunk: Buffer) => {\n if (!claimContext()) return\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 this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: text,\n context_id,\n continue: true,\n } satisfies CartesiaPayload)\n )\n this.log(`Sent transcript: \"${text}\"`)\n })\n\n textStream.on('error', (error) => {\n if (!counter) return\n this.log('Error in text stream, ending audio stream', error)\n this.isProcessing = false\n })\n\n textStream.on('end', async () => {\n // Nothing was ever said, so there is no context to close.\n if (!counter || counter !== this.counter) return\n await this.initPromise\n this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: '',\n context_id,\n continue: false,\n } satisfies CartesiaPayload)\n )\n })\n }\n\n cancel() {\n this.generation++\n if (!this.isProcessing) return\n this.log('Cancel')\n this.isProcessing = false\n this.textSent = ''\n\n // Signal Cartesia to stop sending data\n if (this.socket?.readyState === WebSocket.OPEN) {\n this.socket.send(\n JSON.stringify({\n context_id: this.counter.toString(),\n cancel: true,\n } satisfies CartesiaCancelPayload)\n )\n }\n\n // Increment counter to avoid processing messages from previous calls\n this.counter++\n }\n\n destroy() {\n super.destroy()\n this.destroyed = true\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout)\n this.reconnectTimeout = undefined\n }\n if (this.connectionTimeout) {\n clearTimeout(this.connectionTimeout)\n this.connectionTimeout = undefined\n }\n if (this.socket) closeSocket(this.socket)\n this.socket = undefined\n this.isProcessing = false\n }\n\n private getConfig() {\n return {\n model_id: this.options.modelId,\n voice: {\n mode: 'id',\n id: this.options.voiceId,\n },\n output_format: {\n container: 'raw',\n encoding: 'pcm_s16le',\n sample_rate: 16000,\n },\n language: this.options.language,\n speed: this.options.speed,\n } as const\n }\n\n // Connect to Cartesia\n private async initWS() {\n return new Promise<void>((resolve, reject) => {\n const socket = new WebSocket(\n `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`\n )\n this.socket = socket\n\n const timeout = setTimeout(() => {\n this.log('Connection timeout')\n closeSocket(socket)\n this.socket = undefined\n reject(new Error('WebSocket connection timeout'))\n }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT)\n this.connectionTimeout = timeout\n\n socket.addEventListener('open', () => {\n clearTimeout(timeout)\n this.log('Connection opened')\n resolve()\n })\n\n socket.addEventListener('error', (error) => {\n clearTimeout(timeout)\n this.log('WebSocket error:', error)\n reject(new Error('WebSocket connection error'))\n })\n\n socket.addEventListener('close', ({ code, reason }) => {\n clearTimeout(timeout)\n this.socket?.removeAllListeners()\n this.socket = undefined\n this.isProcessing = false\n\n // Always reconnect: cancel() never closes the socket (it cancels\n // in-band), and destroy() detaches its listeners before closing, so any\n // close reaching this handler is server-initiated. Reconnecting even on\n // a graceful 1000 close keeps the connection ready, otherwise the next\n // speak() would send on an undefined socket and silently no-op.\n this.log('Connection closed', { code, reason })\n this.reconnect()\n })\n\n socket.addEventListener('message', (event) => {\n try {\n const message: CartesiaResponse = JSON.parse(event.data.toString())\n\n // Ignore messages from previous calls\n if (this.counter.toString() !== message.context_id) return\n\n switch (message.type) {\n case 'chunk':\n const chunk = Buffer.from(message.data, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n break\n case 'done':\n this.log('Audio ended')\n this.isProcessing = false\n this.textSent = ''\n break\n case 'error':\n this.log('Error', message.error)\n break\n }\n } catch {\n this.log('Error parsing message', event.data)\n }\n })\n })\n }\n\n private reconnect() {\n if (this.destroyed) return\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 // Send text chunks again if reconnecting\n if (this.textSent.length > 0) {\n this.log('Sending text chunks again')\n this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: this.textSent,\n context_id: this.counter.toString(),\n continue: true,\n } satisfies CartesiaPayload)\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\n/**\n * Closes a socket without hearing from it again.\n *\n * A socket closed while still connecting emits an error, and an error nobody\n * listens to crashes the process, so a listener that ignores it stays.\n */\nfunction closeSocket(socket: WebSocket) {\n socket.removeAllListeners()\n socket.on('error', () => {})\n if (\n socket.readyState === WebSocket.OPEN ||\n socket.readyState === WebSocket.CONNECTING\n ) {\n socket.close(1000)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAoB;AAEpB,gBAAsB;AAmBtB,IAAM,6BAA6B;AACnC,IAAM,sBAAsB;AAC5B,IAAM,oBAAoB;AACnB,IAAM,cAAN,cAA0B,kBAAI;AAAA,EAgBnC,YAA6B,SAA6B;AACxD,UAAM;AADqB;AAb7B,SAAQ,UAAU;AAKlB;AAAA;AAAA;AAAA;AAAA,SAAQ,aAAa;AACrB,SAAQ,eAAe;AAGvB,SAAQ,WAAmB;AAC3B;AAAA,SAAQ,aA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AY;AAClB,QAAI,KAAK,UAAW;AACpB,SAAK;AACL,QAAI,KAAK,cAAc,KAAK,QAAQ,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,GAAG,KAAK,UAAU;AAAA,gBAClB,YAAY,KAAK;AAAA,gBACjB,YAAY,KAAK,QAAQ,SAAS;AAAA,gBAClC,UAAU;AAAA,cACZ,CAA2B;AAAA,YAC7B;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;AAQA,SAAS,YAAY,QAAmB;AACtC,SAAO,mBAAmB;AAC1B,SAAO,GAAG,SAAS,MAAM;AAAA,EAAC,CAAC;AAC3B,MACE,OAAO,eAAe,UAAAA,QAAU,QAChC,OAAO,eAAe,UAAAA,QAAU,YAChC;AACA,WAAO,MAAM,GAAI;AAAA,EACnB;AACF;","names":["WebSocket"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/CartesiaTTS.ts"],"sourcesContent":["export * from './CartesiaTTS'\nexport * from './types'\n","import { ReconnectingSocket, TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n CartesiaCancelPayload,\n CartesiaGenerationConfig,\n CartesiaLanguage,\n CartesiaPayload,\n CartesiaResponse,\n} from './types'\n\nexport interface CartesiaTTSOptions {\n apiKey: string\n modelId: string\n voiceId: string\n language?: CartesiaLanguage\n /** @deprecated Sonic 3 models read `generationConfig.speed` instead */\n speed?: 'fast' | 'normal' | 'slow'\n generationConfig?: CartesiaGenerationConfig\n /** How long a connection attempt may take, 5000 ms by default */\n connectionTimeout?: number\n /** How long to wait before a new attempt, 1000 ms by default */\n retryDelay?: number\n /**\n * How many attempts in a row may fail before giving up and emitting\n * `Failed`, 3 by default. The next utterance tries to connect again.\n */\n maxRetry?: number\n}\n\n/**\n * The utterance Cartesia is synthesizing. While there is one the TTS is\n * speaking, and when there is none it is idle: every way out of an utterance\n * (done, server error, cancel, text stream error, give up) clears it, so\n * nothing about it outlives it.\n */\ninterface CartesiaContext {\n id: string\n /** Every word received so far, sent again whole on a new connection */\n text: string\n /** The text stream ended, so Cartesia was told no more text follows */\n ended: boolean\n}\n\nexport class CartesiaTTS extends TTS {\n private socket: ReconnectingSocket\n // Numbers the audio contexts, and only moves when there is something to say\n private counter = 0\n // Bumped by every speak() and every cancel(), so a context claimed late can\n // tell whether it is still the one that should be heard\n private generation = 0\n private context?: CartesiaContext\n\n constructor(private readonly options: CartesiaTTSOptions) {\n super()\n\n this.socket = new ReconnectingSocket({\n url: `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`,\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 context: CartesiaContext | undefined\n\n // Claiming the context 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 context and\n // anything older is dropped. A stream that never carries a word, which is\n // what an answer skipped by a tool or by onBeforeAnswer hands over, would\n // then cut the assistant off mid-sentence for nothing.\n const claimContext = () => {\n if (context) return context\n // Cancelled, or superseded by another speak(), before the first word\n if (this.generation !== generation) return\n context = { id: (++this.counter).toString(), text: '', ended: false }\n this.context = context\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n return context\n }\n\n textStream.on('data', (chunk: Buffer) => {\n const context = claimContext()\n if (!context || this.context !== context) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n context.text += text\n // Without a connection the text waits, and goes out whole on open\n if (this.sendText(context.id, text, true)) {\n this.log(`Sent transcript: \"${text}\"`)\n }\n })\n\n textStream.on('error', (error) => {\n if (!context || this.context !== context) return\n this.log('Error in text stream, cancelling the audio', error)\n this.stopContext()\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is no context to close\n if (!context || this.context !== context) return\n context.ended = true\n this.sendText(context.id, '', false)\n })\n }\n\n cancel() {\n this.generation++\n if (!this.context) return\n this.log('Cancel')\n this.stopContext()\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.context = undefined\n }\n\n /** Asks Cartesia to drop the current context, and forgets it */\n private stopContext() {\n if (!this.context) return\n this.socket.send(\n JSON.stringify({\n context_id: this.context.id,\n cancel: true,\n } satisfies CartesiaCancelPayload)\n )\n this.context = undefined\n }\n\n private sendText(contextId: string, transcript: string, more: boolean) {\n return this.socket.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript,\n context_id: contextId,\n continue: more,\n } satisfies CartesiaPayload)\n )\n }\n\n private getConfig() {\n return {\n model_id: this.options.modelId,\n voice: {\n mode: 'id',\n id: this.options.voiceId,\n },\n output_format: {\n container: 'raw',\n encoding: 'pcm_s16le',\n sample_rate: 16000,\n },\n language: this.options.language,\n speed: this.options.speed,\n generation_config: this.options.generationConfig,\n } as const\n }\n\n // A new connection knows nothing of the utterance under way: the text read\n // before the first connection opened, or lost with the previous one, goes out\n // again whole\n private onOpen = () => {\n const context = this.context\n if (!context) return\n if (context.text) {\n this.log('Sending text again')\n this.sendText(context.id, context.text, true)\n }\n if (context.ended) this.sendText(context.id, '', false)\n }\n\n private onMessage = (data: RawData) => {\n let message: CartesiaResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n // Ignore messages from previous contexts\n if (!this.context || this.context.id !== message.context_id) return\n\n switch (message.type) {\n case 'chunk':\n const chunk = Buffer.from(message.data, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n break\n case 'done':\n this.log('Audio ended')\n this.context = undefined\n break\n case 'error':\n // Cartesia sends nothing more for a context it failed\n this.log('Error', message.error)\n this.context = undefined\n break\n }\n }\n\n private onFailed = () => {\n const text = this.context?.text\n this.context = undefined\n this.emit('Failed', text ? [text] : [])\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAwC;AA4CjC,IAAM,cAAN,cAA0B,kBAAI;AAAA,EASnC,YAA6B,SAA6B;AACxD,UAAM;AADqB;AAN7B;AAAA,SAAQ,UAAU;AAGlB;AAAA;AAAA,SAAQ,aAAa;AA2HrB;AAAA;AAAA;AAAA,SAAQ,SAAS,MAAM;AACrB,YAAM,UAAU,KAAK;AACrB,UAAI,CAAC,QAAS;AACd,UAAI,QAAQ,MAAM;AAChB,aAAK,IAAI,oBAAoB;AAC7B,aAAK,SAAS,QAAQ,IAAI,QAAQ,MAAM,IAAI;AAAA,MAC9C;AACA,UAAI,QAAQ,MAAO,MAAK,SAAS,QAAQ,IAAI,IAAI,KAAK;AAAA,IACxD;AAEA,SAAQ,YAAY,CAAC,SAAkB;AACrC,UAAI;AACJ,UAAI;AACF,kBAAU,KAAK,MAAM,KAAK,SAAS,CAAC;AAAA,MACtC,QAAQ;AACN,aAAK,IAAI,yBAAyB,IAAI;AACtC;AAAA,MACF;AAGA,UAAI,CAAC,KAAK,WAA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F;AAAA,EAEQ,YAAY;AAClB,WAAO;AAAA,MACL,UAAU,KAAK,QAAQ;AAAA,MACvB,OAAO;AAAA,QACL,MAAM;AAAA,QACN,IAAI,KAAK,QAAQ;AAAA,MACnB;AAAA,MACA,eAAe;AAAA,QACb,WAAW;AAAA,QACX,UAAU;AAAA,QACV,aAAa;AAAA,MACf;AAAA,MACA,UAAU,KAAK,QAAQ;AAAA,MACvB,OAAO,KAAK,QAAQ;AAAA,MACpB,mBAAmB,KAAK,QAAQ;AAAA,IAClC;AAAA,EACF;AAkDF;","names":["context"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,107 +1,130 @@
|
|
|
1
1
|
// src/CartesiaTTS.ts
|
|
2
|
-
import { TTS } from "@micdrop/server";
|
|
3
|
-
import WebSocket from "ws";
|
|
4
|
-
var DEFAULT_CONNECTION_TIMEOUT = 5e3;
|
|
5
|
-
var DEFAULT_RETRY_DELAY = 1e3;
|
|
6
|
-
var DEFAULT_MAX_RETRY = 3;
|
|
2
|
+
import { ReconnectingSocket, TTS } from "@micdrop/server";
|
|
7
3
|
var CartesiaTTS = class extends TTS {
|
|
8
4
|
constructor(options) {
|
|
9
5
|
super();
|
|
10
6
|
this.options = options;
|
|
7
|
+
// Numbers the audio contexts, and only moves when there is something to say
|
|
11
8
|
this.counter = 0;
|
|
12
9
|
// Bumped by every speak() and every cancel(), so a context claimed late can
|
|
13
|
-
// tell whether it is still the one that should be heard
|
|
14
|
-
// `counter`, which numbers the audio contexts and must only move when there
|
|
15
|
-
// is something to synthesize.
|
|
10
|
+
// tell whether it is still the one that should be heard
|
|
16
11
|
this.generation = 0;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
-
this.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
12
|
+
// A new connection knows nothing of the utterance under way: the text read
|
|
13
|
+
// before the first connection opened, or lost with the previous one, goes out
|
|
14
|
+
// again whole
|
|
15
|
+
this.onOpen = () => {
|
|
16
|
+
const context = this.context;
|
|
17
|
+
if (!context) return;
|
|
18
|
+
if (context.text) {
|
|
19
|
+
this.log("Sending text again");
|
|
20
|
+
this.sendText(context.id, context.text, true);
|
|
21
|
+
}
|
|
22
|
+
if (context.ended) this.sendText(context.id, "", false);
|
|
23
|
+
};
|
|
24
|
+
this.onMessage = (data) => {
|
|
25
|
+
let message;
|
|
26
|
+
try {
|
|
27
|
+
message = JSON.parse(data.toString());
|
|
28
|
+
} catch {
|
|
29
|
+
this.log("Error parsing message", data);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (!this.context || this.context.id !== message.context_id) return;
|
|
33
|
+
switch (message.type) {
|
|
34
|
+
case "chunk":
|
|
35
|
+
const chunk = Buffer.from(message.data, "base64");
|
|
36
|
+
this.log(`Received audio chunk (${chunk.length} bytes)`);
|
|
37
|
+
this.emit("Audio", chunk);
|
|
38
|
+
break;
|
|
39
|
+
case "done":
|
|
40
|
+
this.log("Audio ended");
|
|
41
|
+
this.context = void 0;
|
|
42
|
+
break;
|
|
43
|
+
case "error":
|
|
44
|
+
this.log("Error", message.error);
|
|
45
|
+
this.context = void 0;
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
this.onFailed = () => {
|
|
50
|
+
const text = this.context?.text;
|
|
51
|
+
this.context = void 0;
|
|
52
|
+
this.emit("Failed", text ? [text] : []);
|
|
53
|
+
};
|
|
54
|
+
this.socket = new ReconnectingSocket({
|
|
55
|
+
url: `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`,
|
|
56
|
+
connectionTimeout: options.connectionTimeout,
|
|
57
|
+
retryDelay: options.retryDelay,
|
|
58
|
+
maxRetry: options.maxRetry,
|
|
59
|
+
log: (...message) => this.log(...message)
|
|
25
60
|
});
|
|
61
|
+
this.socket.on("Open", this.onOpen);
|
|
62
|
+
this.socket.on("Message", this.onMessage);
|
|
63
|
+
this.socket.on("Failed", this.onFailed);
|
|
64
|
+
this.socket.connect();
|
|
26
65
|
}
|
|
27
66
|
speak(textStream) {
|
|
28
67
|
const generation = ++this.generation;
|
|
29
|
-
let
|
|
30
|
-
let context_id = "";
|
|
68
|
+
let context;
|
|
31
69
|
const claimContext = () => {
|
|
32
|
-
if (
|
|
33
|
-
if (this.generation !== generation) return
|
|
34
|
-
this.counter
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.textSent = "";
|
|
39
|
-
return true;
|
|
70
|
+
if (context) return context;
|
|
71
|
+
if (this.generation !== generation) return;
|
|
72
|
+
context = { id: (++this.counter).toString(), text: "", ended: false };
|
|
73
|
+
this.context = context;
|
|
74
|
+
this.socket.connect();
|
|
75
|
+
return context;
|
|
40
76
|
};
|
|
41
|
-
textStream.on("data",
|
|
42
|
-
|
|
43
|
-
if (
|
|
77
|
+
textStream.on("data", (chunk) => {
|
|
78
|
+
const context2 = claimContext();
|
|
79
|
+
if (!context2 || this.context !== context2) return;
|
|
44
80
|
const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
...this.getConfig(),
|
|
50
|
-
transcript: text,
|
|
51
|
-
context_id,
|
|
52
|
-
continue: true
|
|
53
|
-
})
|
|
54
|
-
);
|
|
55
|
-
this.log(`Sent transcript: "${text}"`);
|
|
81
|
+
context2.text += text;
|
|
82
|
+
if (this.sendText(context2.id, text, true)) {
|
|
83
|
+
this.log(`Sent transcript: "${text}"`);
|
|
84
|
+
}
|
|
56
85
|
});
|
|
57
86
|
textStream.on("error", (error) => {
|
|
58
|
-
if (!
|
|
59
|
-
this.log("Error in text stream,
|
|
60
|
-
this.
|
|
87
|
+
if (!context || this.context !== context) return;
|
|
88
|
+
this.log("Error in text stream, cancelling the audio", error);
|
|
89
|
+
this.stopContext();
|
|
61
90
|
});
|
|
62
|
-
textStream.on("end",
|
|
63
|
-
if (!
|
|
64
|
-
|
|
65
|
-
this.
|
|
66
|
-
JSON.stringify({
|
|
67
|
-
...this.getConfig(),
|
|
68
|
-
transcript: "",
|
|
69
|
-
context_id,
|
|
70
|
-
continue: false
|
|
71
|
-
})
|
|
72
|
-
);
|
|
91
|
+
textStream.on("end", () => {
|
|
92
|
+
if (!context || this.context !== context) return;
|
|
93
|
+
context.ended = true;
|
|
94
|
+
this.sendText(context.id, "", false);
|
|
73
95
|
});
|
|
74
96
|
}
|
|
75
97
|
cancel() {
|
|
76
98
|
this.generation++;
|
|
77
|
-
if (!this.
|
|
99
|
+
if (!this.context) return;
|
|
78
100
|
this.log("Cancel");
|
|
79
|
-
this.
|
|
80
|
-
this.textSent = "";
|
|
81
|
-
if (this.socket?.readyState === WebSocket.OPEN) {
|
|
82
|
-
this.socket.send(
|
|
83
|
-
JSON.stringify({
|
|
84
|
-
context_id: this.counter.toString(),
|
|
85
|
-
cancel: true
|
|
86
|
-
})
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
this.counter++;
|
|
101
|
+
this.stopContext();
|
|
90
102
|
}
|
|
91
103
|
destroy() {
|
|
92
104
|
super.destroy();
|
|
93
|
-
this.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (this.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
+
this.socket.destroy();
|
|
106
|
+
this.context = void 0;
|
|
107
|
+
}
|
|
108
|
+
/** Asks Cartesia to drop the current context, and forgets it */
|
|
109
|
+
stopContext() {
|
|
110
|
+
if (!this.context) return;
|
|
111
|
+
this.socket.send(
|
|
112
|
+
JSON.stringify({
|
|
113
|
+
context_id: this.context.id,
|
|
114
|
+
cancel: true
|
|
115
|
+
})
|
|
116
|
+
);
|
|
117
|
+
this.context = void 0;
|
|
118
|
+
}
|
|
119
|
+
sendText(contextId, transcript, more) {
|
|
120
|
+
return this.socket.send(
|
|
121
|
+
JSON.stringify({
|
|
122
|
+
...this.getConfig(),
|
|
123
|
+
transcript,
|
|
124
|
+
context_id: contextId,
|
|
125
|
+
continue: more
|
|
126
|
+
})
|
|
127
|
+
);
|
|
105
128
|
}
|
|
106
129
|
getConfig() {
|
|
107
130
|
return {
|
|
@@ -116,107 +139,11 @@ var CartesiaTTS = class extends TTS {
|
|
|
116
139
|
sample_rate: 16e3
|
|
117
140
|
},
|
|
118
141
|
language: this.options.language,
|
|
119
|
-
speed: this.options.speed
|
|
142
|
+
speed: this.options.speed,
|
|
143
|
+
generation_config: this.options.generationConfig
|
|
120
144
|
};
|
|
121
145
|
}
|
|
122
|
-
// Connect to Cartesia
|
|
123
|
-
async initWS() {
|
|
124
|
-
return new Promise((resolve, reject) => {
|
|
125
|
-
const socket = new WebSocket(
|
|
126
|
-
`wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`
|
|
127
|
-
);
|
|
128
|
-
this.socket = socket;
|
|
129
|
-
const timeout = setTimeout(() => {
|
|
130
|
-
this.log("Connection timeout");
|
|
131
|
-
closeSocket(socket);
|
|
132
|
-
this.socket = void 0;
|
|
133
|
-
reject(new Error("WebSocket connection timeout"));
|
|
134
|
-
}, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT);
|
|
135
|
-
this.connectionTimeout = timeout;
|
|
136
|
-
socket.addEventListener("open", () => {
|
|
137
|
-
clearTimeout(timeout);
|
|
138
|
-
this.log("Connection opened");
|
|
139
|
-
resolve();
|
|
140
|
-
});
|
|
141
|
-
socket.addEventListener("error", (error) => {
|
|
142
|
-
clearTimeout(timeout);
|
|
143
|
-
this.log("WebSocket error:", error);
|
|
144
|
-
reject(new Error("WebSocket connection error"));
|
|
145
|
-
});
|
|
146
|
-
socket.addEventListener("close", ({ code, reason }) => {
|
|
147
|
-
clearTimeout(timeout);
|
|
148
|
-
this.socket?.removeAllListeners();
|
|
149
|
-
this.socket = void 0;
|
|
150
|
-
this.isProcessing = false;
|
|
151
|
-
this.log("Connection closed", { code, reason });
|
|
152
|
-
this.reconnect();
|
|
153
|
-
});
|
|
154
|
-
socket.addEventListener("message", (event) => {
|
|
155
|
-
try {
|
|
156
|
-
const message = JSON.parse(event.data.toString());
|
|
157
|
-
if (this.counter.toString() !== message.context_id) return;
|
|
158
|
-
switch (message.type) {
|
|
159
|
-
case "chunk":
|
|
160
|
-
const chunk = Buffer.from(message.data, "base64");
|
|
161
|
-
this.log(`Received audio chunk (${chunk.length} bytes)`);
|
|
162
|
-
this.emit("Audio", chunk);
|
|
163
|
-
break;
|
|
164
|
-
case "done":
|
|
165
|
-
this.log("Audio ended");
|
|
166
|
-
this.isProcessing = false;
|
|
167
|
-
this.textSent = "";
|
|
168
|
-
break;
|
|
169
|
-
case "error":
|
|
170
|
-
this.log("Error", message.error);
|
|
171
|
-
break;
|
|
172
|
-
}
|
|
173
|
-
} catch {
|
|
174
|
-
this.log("Error parsing message", event.data);
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
reconnect() {
|
|
180
|
-
if (this.destroyed) return;
|
|
181
|
-
this.retryCount++;
|
|
182
|
-
if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
|
|
183
|
-
this.log("Max retries reached, giving up");
|
|
184
|
-
this.emit("Failed", [this.textSent]);
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
this.initPromise = new Promise((resolve) => {
|
|
188
|
-
this.log("Reconnecting...");
|
|
189
|
-
this.reconnectTimeout = setTimeout(() => {
|
|
190
|
-
this.reconnectTimeout = void 0;
|
|
191
|
-
this.initWS().then(() => {
|
|
192
|
-
this.retryCount = 0;
|
|
193
|
-
if (this.textSent.length > 0) {
|
|
194
|
-
this.log("Sending text chunks again");
|
|
195
|
-
this.socket?.send(
|
|
196
|
-
JSON.stringify({
|
|
197
|
-
...this.getConfig(),
|
|
198
|
-
transcript: this.textSent,
|
|
199
|
-
context_id: this.counter.toString(),
|
|
200
|
-
continue: true
|
|
201
|
-
})
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
}).then(resolve).catch((error) => {
|
|
205
|
-
this.log("Reconnection error:", error);
|
|
206
|
-
this.reconnect();
|
|
207
|
-
});
|
|
208
|
-
}, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
146
|
};
|
|
212
|
-
function closeSocket(socket) {
|
|
213
|
-
socket.removeAllListeners();
|
|
214
|
-
socket.on("error", () => {
|
|
215
|
-
});
|
|
216
|
-
if (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING) {
|
|
217
|
-
socket.close(1e3);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
147
|
export {
|
|
221
148
|
CartesiaTTS
|
|
222
149
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/CartesiaTTS.ts"],"sourcesContent":["import { TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport WebSocket from 'ws'\nimport {\n CartesiaCancelPayload,\n CartesiaLanguage,\n CartesiaPayload,\n CartesiaResponse,\n} from './types'\n\nexport interface CartesiaTTSOptions {\n apiKey: string\n modelId: string\n voiceId: string\n language?: CartesiaLanguage\n speed?: 'fast' | 'normal' | 'slow'\n connectionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\nconst DEFAULT_CONNECTION_TIMEOUT = 5000\nconst DEFAULT_RETRY_DELAY = 1000\nconst DEFAULT_MAX_RETRY = 3\nexport class CartesiaTTS extends TTS {\n private socket?: WebSocket\n private initPromise: Promise<void>\n private counter = 0\n // Bumped by every speak() and every cancel(), so a context claimed late can\n // tell whether it is still the one that should be heard. Kept apart from\n // `counter`, which numbers the audio contexts and must only move when there\n // is something to synthesize.\n private generation = 0\n private isProcessing = false\n private reconnectTimeout?: NodeJS.Timeout\n private connectionTimeout?: NodeJS.Timeout\n private textSent: string = '' // Store text chunks to send them again if reconnecting\n private retryCount = 0\n private destroyed = false\n\n constructor(private readonly options: CartesiaTTSOptions) {\n super()\n\n // Setup WebSocket connection\n this.initPromise = this.initWS().catch((error) => {\n console.error('[CartesiaTTS] Connection error:', error)\n this.reconnect()\n })\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let counter = 0\n let context_id = ''\n\n // Claiming the context 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 context and\n // anything older is dropped. A stream that never carries a word, which is\n // what an answer skipped by a tool or by onBeforeAnswer hands over, would\n // then cut the assistant off mid-sentence for nothing.\n const claimContext = () => {\n if (counter) return true\n // Cancelled, or superseded by another speak(), before the first word.\n if (this.generation !== generation) return false\n this.counter++\n counter = this.counter\n context_id = counter.toString()\n this.isProcessing = true\n this.textSent = ''\n return true\n }\n\n textStream.on('data', async (chunk: Buffer) => {\n if (!claimContext()) return\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 this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: text,\n context_id,\n continue: true,\n } satisfies CartesiaPayload)\n )\n this.log(`Sent transcript: \"${text}\"`)\n })\n\n textStream.on('error', (error) => {\n if (!counter) return\n this.log('Error in text stream, ending audio stream', error)\n this.isProcessing = false\n })\n\n textStream.on('end', async () => {\n // Nothing was ever said, so there is no context to close.\n if (!counter || counter !== this.counter) return\n await this.initPromise\n this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: '',\n context_id,\n continue: false,\n } satisfies CartesiaPayload)\n )\n })\n }\n\n cancel() {\n this.generation++\n if (!this.isProcessing) return\n this.log('Cancel')\n this.isProcessing = false\n this.textSent = ''\n\n // Signal Cartesia to stop sending data\n if (this.socket?.readyState === WebSocket.OPEN) {\n this.socket.send(\n JSON.stringify({\n context_id: this.counter.toString(),\n cancel: true,\n } satisfies CartesiaCancelPayload)\n )\n }\n\n // Increment counter to avoid processing messages from previous calls\n this.counter++\n }\n\n destroy() {\n super.destroy()\n this.destroyed = true\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout)\n this.reconnectTimeout = undefined\n }\n if (this.connectionTimeout) {\n clearTimeout(this.connectionTimeout)\n this.connectionTimeout = undefined\n }\n if (this.socket) closeSocket(this.socket)\n this.socket = undefined\n this.isProcessing = false\n }\n\n private getConfig() {\n return {\n model_id: this.options.modelId,\n voice: {\n mode: 'id',\n id: this.options.voiceId,\n },\n output_format: {\n container: 'raw',\n encoding: 'pcm_s16le',\n sample_rate: 16000,\n },\n language: this.options.language,\n speed: this.options.speed,\n } as const\n }\n\n // Connect to Cartesia\n private async initWS() {\n return new Promise<void>((resolve, reject) => {\n const socket = new WebSocket(\n `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`\n )\n this.socket = socket\n\n const timeout = setTimeout(() => {\n this.log('Connection timeout')\n closeSocket(socket)\n this.socket = undefined\n reject(new Error('WebSocket connection timeout'))\n }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT)\n this.connectionTimeout = timeout\n\n socket.addEventListener('open', () => {\n clearTimeout(timeout)\n this.log('Connection opened')\n resolve()\n })\n\n socket.addEventListener('error', (error) => {\n clearTimeout(timeout)\n this.log('WebSocket error:', error)\n reject(new Error('WebSocket connection error'))\n })\n\n socket.addEventListener('close', ({ code, reason }) => {\n clearTimeout(timeout)\n this.socket?.removeAllListeners()\n this.socket = undefined\n this.isProcessing = false\n\n // Always reconnect: cancel() never closes the socket (it cancels\n // in-band), and destroy() detaches its listeners before closing, so any\n // close reaching this handler is server-initiated. Reconnecting even on\n // a graceful 1000 close keeps the connection ready, otherwise the next\n // speak() would send on an undefined socket and silently no-op.\n this.log('Connection closed', { code, reason })\n this.reconnect()\n })\n\n socket.addEventListener('message', (event) => {\n try {\n const message: CartesiaResponse = JSON.parse(event.data.toString())\n\n // Ignore messages from previous calls\n if (this.counter.toString() !== message.context_id) return\n\n switch (message.type) {\n case 'chunk':\n const chunk = Buffer.from(message.data, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n break\n case 'done':\n this.log('Audio ended')\n this.isProcessing = false\n this.textSent = ''\n break\n case 'error':\n this.log('Error', message.error)\n break\n }\n } catch {\n this.log('Error parsing message', event.data)\n }\n })\n })\n }\n\n private reconnect() {\n if (this.destroyed) return\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 // Send text chunks again if reconnecting\n if (this.textSent.length > 0) {\n this.log('Sending text chunks again')\n this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: this.textSent,\n context_id: this.counter.toString(),\n continue: true,\n } satisfies CartesiaPayload)\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\n/**\n * Closes a socket without hearing from it again.\n *\n * A socket closed while still connecting emits an error, and an error nobody\n * listens to crashes the process, so a listener that ignores it stays.\n */\nfunction closeSocket(socket: WebSocket) {\n socket.removeAllListeners()\n socket.on('error', () => {})\n if (\n socket.readyState === WebSocket.OPEN ||\n socket.readyState === WebSocket.CONNECTING\n ) {\n socket.close(1000)\n }\n}\n"],"mappings":";AAAA,SAAS,WAAW;AAEpB,OAAO,eAAe;AAmBtB,IAAM,6BAA6B;AACnC,IAAM,sBAAsB;AAC5B,IAAM,oBAAoB;AACnB,IAAM,cAAN,cAA0B,IAAI;AAAA,EAgBnC,YAA6B,SAA6B;AACxD,UAAM;AADqB;AAb7B,SAAQ,UAAU;AAKlB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B,MAAM,IAAI;AAAA,QAC9C;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEQ,YAAY;AAClB,QAAI,KAAK,UAAW;AACpB,SAAK;AACL,QAAI,KAAK,cAAc,KAAK,QAAQ,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,GAAG,KAAK,UAAU;AAAA,gBAClB,YAAY,KAAK;AAAA,gBACjB,YAAY,KAAK,QAAQ,SAAS;AAAA,gBAClC,UAAU;AAAA,cACZ,CAA2B;AAAA,YAC7B;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;AAQA,SAAS,YAAY,QAAmB;AACtC,SAAO,mBAAmB;AAC1B,SAAO,GAAG,SAAS,MAAM;AAAA,EAAC,CAAC;AAC3B,MACE,OAAO,eAAe,UAAU,QAChC,OAAO,eAAe,UAAU,YAChC;AACA,WAAO,MAAM,GAAI;AAAA,EACnB;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/CartesiaTTS.ts"],"sourcesContent":["import { ReconnectingSocket, TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n CartesiaCancelPayload,\n CartesiaGenerationConfig,\n CartesiaLanguage,\n CartesiaPayload,\n CartesiaResponse,\n} from './types'\n\nexport interface CartesiaTTSOptions {\n apiKey: string\n modelId: string\n voiceId: string\n language?: CartesiaLanguage\n /** @deprecated Sonic 3 models read `generationConfig.speed` instead */\n speed?: 'fast' | 'normal' | 'slow'\n generationConfig?: CartesiaGenerationConfig\n /** How long a connection attempt may take, 5000 ms by default */\n connectionTimeout?: number\n /** How long to wait before a new attempt, 1000 ms by default */\n retryDelay?: number\n /**\n * How many attempts in a row may fail before giving up and emitting\n * `Failed`, 3 by default. The next utterance tries to connect again.\n */\n maxRetry?: number\n}\n\n/**\n * The utterance Cartesia is synthesizing. While there is one the TTS is\n * speaking, and when there is none it is idle: every way out of an utterance\n * (done, server error, cancel, text stream error, give up) clears it, so\n * nothing about it outlives it.\n */\ninterface CartesiaContext {\n id: string\n /** Every word received so far, sent again whole on a new connection */\n text: string\n /** The text stream ended, so Cartesia was told no more text follows */\n ended: boolean\n}\n\nexport class CartesiaTTS extends TTS {\n private socket: ReconnectingSocket\n // Numbers the audio contexts, and only moves when there is something to say\n private counter = 0\n // Bumped by every speak() and every cancel(), so a context claimed late can\n // tell whether it is still the one that should be heard\n private generation = 0\n private context?: CartesiaContext\n\n constructor(private readonly options: CartesiaTTSOptions) {\n super()\n\n this.socket = new ReconnectingSocket({\n url: `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`,\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 context: CartesiaContext | undefined\n\n // Claiming the context 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 context and\n // anything older is dropped. A stream that never carries a word, which is\n // what an answer skipped by a tool or by onBeforeAnswer hands over, would\n // then cut the assistant off mid-sentence for nothing.\n const claimContext = () => {\n if (context) return context\n // Cancelled, or superseded by another speak(), before the first word\n if (this.generation !== generation) return\n context = { id: (++this.counter).toString(), text: '', ended: false }\n this.context = context\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n return context\n }\n\n textStream.on('data', (chunk: Buffer) => {\n const context = claimContext()\n if (!context || this.context !== context) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n context.text += text\n // Without a connection the text waits, and goes out whole on open\n if (this.sendText(context.id, text, true)) {\n this.log(`Sent transcript: \"${text}\"`)\n }\n })\n\n textStream.on('error', (error) => {\n if (!context || this.context !== context) return\n this.log('Error in text stream, cancelling the audio', error)\n this.stopContext()\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is no context to close\n if (!context || this.context !== context) return\n context.ended = true\n this.sendText(context.id, '', false)\n })\n }\n\n cancel() {\n this.generation++\n if (!this.context) return\n this.log('Cancel')\n this.stopContext()\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.context = undefined\n }\n\n /** Asks Cartesia to drop the current context, and forgets it */\n private stopContext() {\n if (!this.context) return\n this.socket.send(\n JSON.stringify({\n context_id: this.context.id,\n cancel: true,\n } satisfies CartesiaCancelPayload)\n )\n this.context = undefined\n }\n\n private sendText(contextId: string, transcript: string, more: boolean) {\n return this.socket.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript,\n context_id: contextId,\n continue: more,\n } satisfies CartesiaPayload)\n )\n }\n\n private getConfig() {\n return {\n model_id: this.options.modelId,\n voice: {\n mode: 'id',\n id: this.options.voiceId,\n },\n output_format: {\n container: 'raw',\n encoding: 'pcm_s16le',\n sample_rate: 16000,\n },\n language: this.options.language,\n speed: this.options.speed,\n generation_config: this.options.generationConfig,\n } as const\n }\n\n // A new connection knows nothing of the utterance under way: the text read\n // before the first connection opened, or lost with the previous one, goes out\n // again whole\n private onOpen = () => {\n const context = this.context\n if (!context) return\n if (context.text) {\n this.log('Sending text again')\n this.sendText(context.id, context.text, true)\n }\n if (context.ended) this.sendText(context.id, '', false)\n }\n\n private onMessage = (data: RawData) => {\n let message: CartesiaResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n // Ignore messages from previous contexts\n if (!this.context || this.context.id !== message.context_id) return\n\n switch (message.type) {\n case 'chunk':\n const chunk = Buffer.from(message.data, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n break\n case 'done':\n this.log('Audio ended')\n this.context = undefined\n break\n case 'error':\n // Cartesia sends nothing more for a context it failed\n this.log('Error', message.error)\n this.context = undefined\n break\n }\n }\n\n private onFailed = () => {\n const text = this.context?.text\n this.context = undefined\n this.emit('Failed', text ? [text] : [])\n }\n}\n"],"mappings":";AAAA,SAAS,oBAAoB,WAAW;AA4CjC,IAAM,cAAN,cAA0B,IAAI;AAAA,EASnC,YAA6B,SAA6B;AACxD,UAAM;AADqB;AAN7B;AAAA,SAAQ,UAAU;AAGlB;AAAA;AAAA,SAAQ,aAAa;AA2HrB;AAAA;AAAA;AAAA,SAAQ,SAAS,MAAM;AACrB,YAAM,UAAU,KAAK;AACrB,UAAI,CAAC,QAAS;AACd,UAAI,QAAQ,MAAM;AAChB,aAAK,IAAI,oBAAoB;AAC7B,aAAK,SAAS,QAAQ,IAAI,QAAQ,MAAM,IAAI;AAAA,MAC9C;AACA,UAAI,QAAQ,MAAO,MAAK,SAAS,QAAQ,IAAI,IAAI,KAAK;AAAA,IACxD;AAEA,SAAQ,YAAY,CAAC,SAAkB;AACrC,UAAI;AACJ,UAAI;AACF,kBAAU,KAAK,MAAM,KAAK,SAAS,CAAC;AAAA,MACtC,QAAQ;AACN,aAAK,IAAI,yBAAyB,IAAI;AACtC;AAAA,MACF;AAGA,UAAI,CAAC,KAAK,WAAW,KAAK,QAAQ,OA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AY;AAClB,WAAO;AAAA,MACL,UAAU,KAAK,QAAQ;AAAA,MACvB,OAAO;AAAA,QACL,MAAM;AAAA,QACN,IAAI,KAAK,QAAQ;AAAA,MACnB;AAAA,MACA,eAAe;AAAA,QACb,WAAW;AAAA,QACX,UAAU;AAAA,QACV,aAAa;AAAA,MACf;AAAA,MACA,UAAU,KAAK,QAAQ;AAAA,MACvB,OAAO,KAAK,QAAQ;AAAA,MACpB,mBAAmB,KAAK,QAAQ;AAAA,IAClC;AAAA,EACF;AAkDF;","names":["context"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@micdrop/cartesia",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Cartesia implementation for @micdrop/server",
|
|
5
5
|
"author": "Godefroy de Compreignac",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"ws": "^8.19.0"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@micdrop/server": "
|
|
41
|
+
"@micdrop/server": ">=2.10.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@types/ws": "^8.18.1",
|