@cartesia/cartesia-js 0.0.4-alpha.0 → 1.0.0-alpha.2
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/.turbo/turbo-build.log +63 -45
- package/CHANGELOG.md +12 -0
- package/README.md +123 -16
- package/dist/chunk-36JBKJUN.js +119 -0
- package/dist/chunk-3F5E46FT.js +212 -0
- package/dist/{chunk-XPIMIAAE.js → chunk-3FL2SNIR.js} +1 -1
- package/dist/chunk-JGP5BIUV.js +34 -0
- package/dist/chunk-KWBSQZTY.js +25 -0
- package/dist/chunk-PQ6CIPFW.js +120 -0
- package/dist/chunk-RO7TY474.js +81 -0
- package/dist/chunk-T3RG6WV4.js +22 -0
- package/dist/{chunk-R4P7LWVZ.js → chunk-WIFMLPT5.js} +31 -6
- package/dist/chunk-WVTITUXX.js +58 -0
- package/dist/chunk-XHTDPLFR.js +19 -0
- package/dist/index.cjs +425 -166
- package/dist/index.d.cts +7 -3
- package/dist/index.d.ts +7 -3
- package/dist/index.js +13 -6
- package/dist/lib/client.cjs +49 -1
- package/dist/lib/client.d.cts +2 -0
- package/dist/lib/client.d.ts +2 -0
- package/dist/lib/client.js +3 -3
- package/dist/lib/constants.cjs +15 -8
- package/dist/lib/constants.d.cts +4 -4
- package/dist/lib/constants.d.ts +4 -4
- package/dist/lib/constants.js +6 -6
- package/dist/lib/index.cjs +310 -171
- package/dist/lib/index.d.cts +6 -2
- package/dist/lib/index.d.ts +6 -2
- package/dist/lib/index.js +9 -6
- package/dist/react/index.cjs +573 -290
- package/dist/react/index.d.cts +20 -14
- package/dist/react/index.d.ts +20 -14
- package/dist/react/index.js +157 -105
- package/dist/react/utils.js +2 -2
- package/dist/tts/index.cjs +496 -0
- package/dist/tts/index.d.cts +17 -0
- package/dist/tts/index.d.ts +17 -0
- package/dist/tts/index.js +12 -0
- package/dist/tts/player.cjs +198 -0
- package/dist/tts/player.d.cts +43 -0
- package/dist/tts/player.d.ts +43 -0
- package/dist/tts/player.js +8 -0
- package/dist/tts/source.cjs +181 -0
- package/dist/tts/source.d.cts +53 -0
- package/dist/tts/source.d.ts +53 -0
- package/dist/tts/source.js +7 -0
- package/dist/{audio → tts}/utils.cjs +25 -60
- package/dist/tts/utils.d.cts +67 -0
- package/dist/tts/utils.d.ts +67 -0
- package/dist/{audio → tts}/utils.js +2 -7
- package/dist/tts/websocket.cjs +479 -0
- package/dist/tts/websocket.d.cts +53 -0
- package/dist/tts/websocket.d.ts +53 -0
- package/dist/tts/websocket.js +11 -0
- package/dist/types/index.d.cts +50 -1
- package/dist/types/index.d.ts +50 -1
- package/dist/voices/index.cjs +157 -0
- package/dist/voices/index.d.cts +12 -0
- package/dist/voices/index.d.ts +12 -0
- package/dist/voices/index.js +9 -0
- package/package.json +2 -1
- package/src/index.ts +1 -0
- package/src/lib/client.ts +15 -1
- package/src/lib/constants.ts +15 -4
- package/src/lib/index.ts +6 -3
- package/src/react/index.ts +176 -110
- package/src/tts/index.ts +17 -0
- package/src/tts/player.ts +110 -0
- package/src/tts/source.ts +115 -0
- package/src/tts/utils.ts +150 -0
- package/src/tts/websocket.ts +214 -0
- package/src/types/index.ts +63 -0
- package/src/voices/index.ts +47 -0
- package/dist/audio/index.cjs +0 -404
- package/dist/audio/index.d.cts +0 -5
- package/dist/audio/index.d.ts +0 -5
- package/dist/audio/index.js +0 -10
- package/dist/audio/utils.d.cts +0 -5
- package/dist/audio/utils.d.ts +0 -5
- package/dist/chunk-4MHF74A7.js +0 -272
- package/dist/chunk-5TSWLYOW.js +0 -113
- package/dist/chunk-MJIFZWHS.js +0 -18
- package/dist/chunk-OVI3W3GG.js +0 -12
- package/dist/chunk-S6A27RQL.js +0 -18
- package/dist/index-C2_3XFxn.d.cts +0 -163
- package/dist/index-DgwnZezj.d.ts +0 -163
- package/src/audio/index.ts +0 -297
- package/src/audio/utils.ts +0 -220
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var __export = (target, all) => {
|
|
26
|
+
for (var name in all)
|
|
27
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
28
|
+
};
|
|
29
|
+
var __copyProps = (to, from, except, desc) => {
|
|
30
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
31
|
+
for (let key of __getOwnPropNames(from))
|
|
32
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
33
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
34
|
+
}
|
|
35
|
+
return to;
|
|
36
|
+
};
|
|
37
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
38
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
39
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
40
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
41
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
42
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
43
|
+
mod
|
|
44
|
+
));
|
|
45
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
46
|
+
var __accessCheck = (obj, member, msg) => {
|
|
47
|
+
if (!member.has(obj))
|
|
48
|
+
throw TypeError("Cannot " + msg);
|
|
49
|
+
};
|
|
50
|
+
var __privateGet = (obj, member, getter) => {
|
|
51
|
+
__accessCheck(obj, member, "read from private field");
|
|
52
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
53
|
+
};
|
|
54
|
+
var __privateAdd = (obj, member, value) => {
|
|
55
|
+
if (member.has(obj))
|
|
56
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
57
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
58
|
+
};
|
|
59
|
+
var __privateSet = (obj, member, value, setter) => {
|
|
60
|
+
__accessCheck(obj, member, "write to private field");
|
|
61
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
62
|
+
return value;
|
|
63
|
+
};
|
|
64
|
+
var __privateMethod = (obj, member, method) => {
|
|
65
|
+
__accessCheck(obj, member, "access private method");
|
|
66
|
+
return method;
|
|
67
|
+
};
|
|
68
|
+
var __async = (__this, __arguments, generator) => {
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
var fulfilled = (value) => {
|
|
71
|
+
try {
|
|
72
|
+
step(generator.next(value));
|
|
73
|
+
} catch (e) {
|
|
74
|
+
reject(e);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
var rejected = (value) => {
|
|
78
|
+
try {
|
|
79
|
+
step(generator.throw(value));
|
|
80
|
+
} catch (e) {
|
|
81
|
+
reject(e);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
85
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// src/tts/websocket.ts
|
|
90
|
+
var websocket_exports = {};
|
|
91
|
+
__export(websocket_exports, {
|
|
92
|
+
default: () => WebSocket
|
|
93
|
+
});
|
|
94
|
+
module.exports = __toCommonJS(websocket_exports);
|
|
95
|
+
var import_emittery2 = __toESM(require("emittery"), 1);
|
|
96
|
+
var import_human_id = require("human-id");
|
|
97
|
+
var import_partysocket = require("partysocket");
|
|
98
|
+
|
|
99
|
+
// src/lib/client.ts
|
|
100
|
+
var import_cross_fetch = __toESM(require("cross-fetch"), 1);
|
|
101
|
+
|
|
102
|
+
// src/lib/constants.ts
|
|
103
|
+
var BASE_URL = "https://api.cartesia.ai/";
|
|
104
|
+
var CARTESIA_VERSION = "2024-06-10";
|
|
105
|
+
var constructApiUrl = (baseUrl, path, protocol) => {
|
|
106
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
107
|
+
if (!protocol) {
|
|
108
|
+
return new URL(`${baseUrl}${normalizedPath}`);
|
|
109
|
+
}
|
|
110
|
+
if (!["http", "ws"].includes(protocol)) {
|
|
111
|
+
throw new Error(`Invalid protocol: ${protocol}`);
|
|
112
|
+
}
|
|
113
|
+
return new URL(`${baseUrl.replace(/^http/, protocol)}${normalizedPath}`);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// src/lib/client.ts
|
|
117
|
+
var Client = class {
|
|
118
|
+
constructor(options = {}) {
|
|
119
|
+
if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
|
|
120
|
+
throw new Error("Missing Cartesia API key.");
|
|
121
|
+
}
|
|
122
|
+
this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
|
|
123
|
+
this.baseUrl = options.baseUrl || BASE_URL;
|
|
124
|
+
}
|
|
125
|
+
fetch(path, options = {}) {
|
|
126
|
+
const url = constructApiUrl(this.baseUrl, path);
|
|
127
|
+
return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
|
|
128
|
+
headers: __spreadValues({
|
|
129
|
+
"X-API-Key": this.apiKey,
|
|
130
|
+
"Cartesia-Version": CARTESIA_VERSION
|
|
131
|
+
}, options.headers)
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// src/tts/source.ts
|
|
137
|
+
var import_emittery = __toESM(require("emittery"), 1);
|
|
138
|
+
var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
|
|
139
|
+
var Source = class {
|
|
140
|
+
/**
|
|
141
|
+
* Create a new Source.
|
|
142
|
+
*
|
|
143
|
+
* @param options - Options for the Source.
|
|
144
|
+
* @param options.sampleRate - The sample rate of the audio.
|
|
145
|
+
*/
|
|
146
|
+
constructor({ sampleRate }) {
|
|
147
|
+
__privateAdd(this, _emitter, new import_emittery.default());
|
|
148
|
+
__privateAdd(this, _buffer, void 0);
|
|
149
|
+
__privateAdd(this, _readIndex, 0);
|
|
150
|
+
__privateAdd(this, _writeIndex, 0);
|
|
151
|
+
__privateAdd(this, _closed, false);
|
|
152
|
+
__privateAdd(this, _sampleRate, void 0);
|
|
153
|
+
this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
|
|
154
|
+
this.once = __privateGet(this, _emitter).once.bind(__privateGet(this, _emitter));
|
|
155
|
+
this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
|
|
156
|
+
this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
|
|
157
|
+
__privateSet(this, _sampleRate, sampleRate);
|
|
158
|
+
__privateSet(this, _buffer, new Float32Array(1024));
|
|
159
|
+
}
|
|
160
|
+
get sampleRate() {
|
|
161
|
+
return __privateGet(this, _sampleRate);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Append audio to the buffer.
|
|
165
|
+
*
|
|
166
|
+
* @param src The audio to append.
|
|
167
|
+
*/
|
|
168
|
+
enqueue(src) {
|
|
169
|
+
return __async(this, null, function* () {
|
|
170
|
+
const requiredCapacity = __privateGet(this, _writeIndex) + src.length;
|
|
171
|
+
if (requiredCapacity > __privateGet(this, _buffer).length) {
|
|
172
|
+
let newCapacity = __privateGet(this, _buffer).length;
|
|
173
|
+
while (newCapacity < requiredCapacity) {
|
|
174
|
+
newCapacity *= 2;
|
|
175
|
+
}
|
|
176
|
+
const newBuffer = new Float32Array(newCapacity);
|
|
177
|
+
newBuffer.set(__privateGet(this, _buffer));
|
|
178
|
+
__privateSet(this, _buffer, newBuffer);
|
|
179
|
+
}
|
|
180
|
+
__privateGet(this, _buffer).set(src, __privateGet(this, _writeIndex));
|
|
181
|
+
__privateSet(this, _writeIndex, __privateGet(this, _writeIndex) + src.length);
|
|
182
|
+
yield __privateGet(this, _emitter).emit("enqueue");
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Read audio from the buffer.
|
|
187
|
+
*
|
|
188
|
+
* @param dst The buffer to read the audio into.
|
|
189
|
+
* @returns The number of samples read. If the source is closed, this will be
|
|
190
|
+
* less than the length of the provided buffer.
|
|
191
|
+
*/
|
|
192
|
+
read(dst) {
|
|
193
|
+
return __async(this, null, function* () {
|
|
194
|
+
const targetReadIndex = __privateGet(this, _readIndex) + dst.length;
|
|
195
|
+
while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _writeIndex)) {
|
|
196
|
+
yield __privateGet(this, _emitter).emit("wait");
|
|
197
|
+
yield Promise.race([
|
|
198
|
+
__privateGet(this, _emitter).once("enqueue"),
|
|
199
|
+
__privateGet(this, _emitter).once("close")
|
|
200
|
+
]);
|
|
201
|
+
yield __privateGet(this, _emitter).emit("read");
|
|
202
|
+
}
|
|
203
|
+
const read = Math.min(dst.length, __privateGet(this, _writeIndex) - __privateGet(this, _readIndex));
|
|
204
|
+
dst.set(__privateGet(this, _buffer).subarray(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
|
|
205
|
+
__privateSet(this, _readIndex, __privateGet(this, _readIndex) + read);
|
|
206
|
+
return read;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Get the number of samples in a given duration.
|
|
211
|
+
*
|
|
212
|
+
* @param durationSecs The duration in seconds.
|
|
213
|
+
* @returns The number of samples.
|
|
214
|
+
*/
|
|
215
|
+
durationToSampleCount(durationSecs) {
|
|
216
|
+
return Math.trunc(durationSecs * __privateGet(this, _sampleRate));
|
|
217
|
+
}
|
|
218
|
+
get buffer() {
|
|
219
|
+
return __privateGet(this, _buffer);
|
|
220
|
+
}
|
|
221
|
+
get readIndex() {
|
|
222
|
+
return __privateGet(this, _readIndex);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Close the source. This signals that no more audio will be enqueued.
|
|
226
|
+
*
|
|
227
|
+
* This will emit a "close" event.
|
|
228
|
+
*
|
|
229
|
+
* @returns A promise that resolves when the source is closed.
|
|
230
|
+
*/
|
|
231
|
+
close() {
|
|
232
|
+
return __async(this, null, function* () {
|
|
233
|
+
__privateSet(this, _closed, true);
|
|
234
|
+
yield __privateGet(this, _emitter).emit("close");
|
|
235
|
+
__privateGet(this, _emitter).clearListeners();
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
_emitter = new WeakMap();
|
|
240
|
+
_buffer = new WeakMap();
|
|
241
|
+
_readIndex = new WeakMap();
|
|
242
|
+
_writeIndex = new WeakMap();
|
|
243
|
+
_closed = new WeakMap();
|
|
244
|
+
_sampleRate = new WeakMap();
|
|
245
|
+
|
|
246
|
+
// src/tts/utils.ts
|
|
247
|
+
var import_base64_js = __toESM(require("base64-js"), 1);
|
|
248
|
+
function base64ToArray(b64) {
|
|
249
|
+
const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
|
|
250
|
+
const totalLength = byteArrays.reduce(
|
|
251
|
+
(acc, arr) => acc + arr.length / Float32Array.BYTES_PER_ELEMENT,
|
|
252
|
+
0
|
|
253
|
+
);
|
|
254
|
+
const result = new Float32Array(totalLength);
|
|
255
|
+
let offset = 0;
|
|
256
|
+
for (const arr of byteArrays) {
|
|
257
|
+
const floats = new Float32Array(arr.buffer);
|
|
258
|
+
result.set(floats, offset);
|
|
259
|
+
offset += floats.length;
|
|
260
|
+
}
|
|
261
|
+
return result;
|
|
262
|
+
}
|
|
263
|
+
function createMessageHandlerForContextId(contextId, handler) {
|
|
264
|
+
return (event) => {
|
|
265
|
+
if (typeof event.data !== "string") {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const message = JSON.parse(event.data);
|
|
269
|
+
if (message.context_id !== contextId) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
let chunk;
|
|
273
|
+
if (message.done) {
|
|
274
|
+
chunk = getSentinel();
|
|
275
|
+
} else {
|
|
276
|
+
chunk = message.data;
|
|
277
|
+
}
|
|
278
|
+
handler({ chunk, message: event.data });
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
function getSentinel() {
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
function isSentinel(x) {
|
|
285
|
+
return x === getSentinel();
|
|
286
|
+
}
|
|
287
|
+
function filterSentinel(collection) {
|
|
288
|
+
return collection.filter(
|
|
289
|
+
(x) => !isSentinel(x)
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
function getEmitteryCallbacks(emitter) {
|
|
293
|
+
return {
|
|
294
|
+
on: emitter.on.bind(emitter),
|
|
295
|
+
off: emitter.off.bind(emitter),
|
|
296
|
+
once: emitter.once.bind(emitter),
|
|
297
|
+
events: emitter.events.bind(emitter)
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// src/tts/websocket.ts
|
|
302
|
+
var _isConnected, _sampleRate2, _generateId, generateId_fn;
|
|
303
|
+
var WebSocket = class extends Client {
|
|
304
|
+
/**
|
|
305
|
+
* Create a new WebSocket client.
|
|
306
|
+
*
|
|
307
|
+
* @param args - Arguments to pass to the Client constructor.
|
|
308
|
+
*/
|
|
309
|
+
constructor({ sampleRate }, ...args) {
|
|
310
|
+
super(...args);
|
|
311
|
+
/**
|
|
312
|
+
* Generate a unique ID suitable for a streaming context.
|
|
313
|
+
*
|
|
314
|
+
* Not suitable for security purposes or as a primary key, since
|
|
315
|
+
* it lacks the amount of entropy required for those use cases.
|
|
316
|
+
*
|
|
317
|
+
* @returns A unique ID.
|
|
318
|
+
*/
|
|
319
|
+
__privateAdd(this, _generateId);
|
|
320
|
+
__privateAdd(this, _isConnected, false);
|
|
321
|
+
__privateAdd(this, _sampleRate2, void 0);
|
|
322
|
+
__privateSet(this, _sampleRate2, sampleRate);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Send a message over the WebSocket in order to start a stream.
|
|
326
|
+
*
|
|
327
|
+
* @param inputs - Stream options.
|
|
328
|
+
* @param options - Options for the stream.
|
|
329
|
+
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
330
|
+
* If `0`, the stream will not time out.
|
|
331
|
+
* @returns A Source object that can be passed to a Player to play the audio.
|
|
332
|
+
*/
|
|
333
|
+
send(inputs, { timeout = 0 } = {}) {
|
|
334
|
+
var _a, _b, _c, _d;
|
|
335
|
+
if (!__privateGet(this, _isConnected)) {
|
|
336
|
+
throw new Error("Not connected to WebSocket. Call .connect() first.");
|
|
337
|
+
}
|
|
338
|
+
const contextId = __privateMethod(this, _generateId, generateId_fn).call(this);
|
|
339
|
+
(_a = this.socket) == null ? void 0 : _a.send(
|
|
340
|
+
JSON.stringify(__spreadProps(__spreadValues({
|
|
341
|
+
context_id: contextId
|
|
342
|
+
}, inputs), {
|
|
343
|
+
output_format: {
|
|
344
|
+
container: "raw",
|
|
345
|
+
encoding: "pcm_f32le",
|
|
346
|
+
sample_rate: __privateGet(this, _sampleRate2)
|
|
347
|
+
}
|
|
348
|
+
}))
|
|
349
|
+
);
|
|
350
|
+
const emitter = new import_emittery2.default();
|
|
351
|
+
const source = new Source({
|
|
352
|
+
sampleRate: __privateGet(this, _sampleRate2)
|
|
353
|
+
});
|
|
354
|
+
const streamCompleteController = new AbortController();
|
|
355
|
+
let timeoutId = null;
|
|
356
|
+
if (timeout > 0) {
|
|
357
|
+
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
358
|
+
}
|
|
359
|
+
const handleMessage = createMessageHandlerForContextId(
|
|
360
|
+
contextId,
|
|
361
|
+
(_0) => __async(this, [_0], function* ({ chunk, message }) {
|
|
362
|
+
emitter.emit("message", message);
|
|
363
|
+
if (isSentinel(chunk)) {
|
|
364
|
+
yield source.close();
|
|
365
|
+
streamCompleteController.abort();
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
yield source.enqueue(base64ToArray([chunk]));
|
|
369
|
+
if (timeoutId) {
|
|
370
|
+
clearTimeout(timeoutId);
|
|
371
|
+
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
372
|
+
}
|
|
373
|
+
})
|
|
374
|
+
);
|
|
375
|
+
(_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
|
|
376
|
+
signal: streamCompleteController.signal
|
|
377
|
+
});
|
|
378
|
+
(_c = this.socket) == null ? void 0 : _c.addEventListener(
|
|
379
|
+
"close",
|
|
380
|
+
() => {
|
|
381
|
+
streamCompleteController.abort();
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
once: true
|
|
385
|
+
}
|
|
386
|
+
);
|
|
387
|
+
(_d = this.socket) == null ? void 0 : _d.addEventListener(
|
|
388
|
+
"error",
|
|
389
|
+
() => {
|
|
390
|
+
streamCompleteController.abort();
|
|
391
|
+
},
|
|
392
|
+
{
|
|
393
|
+
once: true
|
|
394
|
+
}
|
|
395
|
+
);
|
|
396
|
+
streamCompleteController.signal.addEventListener("abort", () => {
|
|
397
|
+
source.close();
|
|
398
|
+
if (timeoutId) {
|
|
399
|
+
clearTimeout(timeoutId);
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
return __spreadValues({ source }, getEmitteryCallbacks(emitter));
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Authenticate and connect to a Cartesia streaming WebSocket.
|
|
406
|
+
*
|
|
407
|
+
* @returns A promise that resolves when the WebSocket is connected.
|
|
408
|
+
* @throws {Error} If the WebSocket fails to connect.
|
|
409
|
+
*/
|
|
410
|
+
connect() {
|
|
411
|
+
const url = constructApiUrl(
|
|
412
|
+
this.baseUrl,
|
|
413
|
+
`/tts/websocket?cartesia_version=${CARTESIA_VERSION}`,
|
|
414
|
+
"ws"
|
|
415
|
+
);
|
|
416
|
+
url.searchParams.set("api_key", this.apiKey);
|
|
417
|
+
const emitter = new import_emittery2.default();
|
|
418
|
+
this.socket = new import_partysocket.WebSocket(url.toString());
|
|
419
|
+
this.socket.onopen = () => {
|
|
420
|
+
__privateSet(this, _isConnected, true);
|
|
421
|
+
emitter.emit("open");
|
|
422
|
+
};
|
|
423
|
+
this.socket.onclose = () => {
|
|
424
|
+
__privateSet(this, _isConnected, false);
|
|
425
|
+
emitter.emit("close");
|
|
426
|
+
};
|
|
427
|
+
return new Promise(
|
|
428
|
+
(resolve, reject) => {
|
|
429
|
+
var _a, _b, _c;
|
|
430
|
+
(_a = this.socket) == null ? void 0 : _a.addEventListener(
|
|
431
|
+
"open",
|
|
432
|
+
() => {
|
|
433
|
+
resolve(getEmitteryCallbacks(emitter));
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
once: true
|
|
437
|
+
}
|
|
438
|
+
);
|
|
439
|
+
const aborter = new AbortController();
|
|
440
|
+
(_b = this.socket) == null ? void 0 : _b.addEventListener(
|
|
441
|
+
"error",
|
|
442
|
+
() => {
|
|
443
|
+
aborter.abort();
|
|
444
|
+
reject(new Error("WebSocket failed to connect."));
|
|
445
|
+
},
|
|
446
|
+
{
|
|
447
|
+
signal: aborter.signal
|
|
448
|
+
}
|
|
449
|
+
);
|
|
450
|
+
(_c = this.socket) == null ? void 0 : _c.addEventListener(
|
|
451
|
+
"close",
|
|
452
|
+
() => {
|
|
453
|
+
aborter.abort();
|
|
454
|
+
reject(new Error("WebSocket closed before it could connect."));
|
|
455
|
+
},
|
|
456
|
+
{
|
|
457
|
+
signal: aborter.signal
|
|
458
|
+
}
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Disconnect from the Cartesia streaming WebSocket.
|
|
465
|
+
*/
|
|
466
|
+
disconnect() {
|
|
467
|
+
var _a;
|
|
468
|
+
(_a = this.socket) == null ? void 0 : _a.close();
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
_isConnected = new WeakMap();
|
|
472
|
+
_sampleRate2 = new WeakMap();
|
|
473
|
+
_generateId = new WeakSet();
|
|
474
|
+
generateId_fn = function() {
|
|
475
|
+
return (0, import_human_id.humanId)({
|
|
476
|
+
separator: "-",
|
|
477
|
+
capitalize: false
|
|
478
|
+
});
|
|
479
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as emittery from 'emittery';
|
|
2
|
+
import { WebSocket as WebSocket$1 } from 'partysocket';
|
|
3
|
+
import { Client } from '../lib/client.cjs';
|
|
4
|
+
import { WebSocketOptions, StreamRequest, EmitteryCallbacks, ConnectionEventData } from '../types/index.cjs';
|
|
5
|
+
import Source from './source.cjs';
|
|
6
|
+
|
|
7
|
+
declare class WebSocket extends Client {
|
|
8
|
+
#private;
|
|
9
|
+
socket?: WebSocket$1;
|
|
10
|
+
/**
|
|
11
|
+
* Create a new WebSocket client.
|
|
12
|
+
*
|
|
13
|
+
* @param args - Arguments to pass to the Client constructor.
|
|
14
|
+
*/
|
|
15
|
+
constructor({ sampleRate }: WebSocketOptions, ...args: ConstructorParameters<typeof Client>);
|
|
16
|
+
/**
|
|
17
|
+
* Send a message over the WebSocket in order to start a stream.
|
|
18
|
+
*
|
|
19
|
+
* @param inputs - Stream options.
|
|
20
|
+
* @param options - Options for the stream.
|
|
21
|
+
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
22
|
+
* If `0`, the stream will not time out.
|
|
23
|
+
* @returns A Source object that can be passed to a Player to play the audio.
|
|
24
|
+
*/
|
|
25
|
+
send(inputs: StreamRequest["inputs"], { timeout }?: StreamRequest["options"]): {
|
|
26
|
+
on: <Name extends keyof emittery.OmnipresentEventData | "message">(eventName: Name | readonly Name[], listener: (eventData: ({
|
|
27
|
+
message: string;
|
|
28
|
+
} & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
|
|
29
|
+
off: <Name_1 extends keyof emittery.OmnipresentEventData | "message">(eventName: Name_1 | readonly Name_1[], listener: (eventData: ({
|
|
30
|
+
message: string;
|
|
31
|
+
} & emittery.OmnipresentEventData)[Name_1]) => void | Promise<void>) => void;
|
|
32
|
+
once: <Name_2 extends keyof emittery.OmnipresentEventData | "message">(eventName: Name_2 | readonly Name_2[]) => emittery.EmitteryOncePromise<({
|
|
33
|
+
message: string;
|
|
34
|
+
} & emittery.OmnipresentEventData)[Name_2]>;
|
|
35
|
+
events: <Name_3 extends "message">(eventName: Name_3 | readonly Name_3[]) => AsyncIterableIterator<{
|
|
36
|
+
message: string;
|
|
37
|
+
}[Name_3]>;
|
|
38
|
+
source: Source;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Authenticate and connect to a Cartesia streaming WebSocket.
|
|
42
|
+
*
|
|
43
|
+
* @returns A promise that resolves when the WebSocket is connected.
|
|
44
|
+
* @throws {Error} If the WebSocket fails to connect.
|
|
45
|
+
*/
|
|
46
|
+
connect(): Promise<EmitteryCallbacks<ConnectionEventData>>;
|
|
47
|
+
/**
|
|
48
|
+
* Disconnect from the Cartesia streaming WebSocket.
|
|
49
|
+
*/
|
|
50
|
+
disconnect(): void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { WebSocket as default };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as emittery from 'emittery';
|
|
2
|
+
import { WebSocket as WebSocket$1 } from 'partysocket';
|
|
3
|
+
import { Client } from '../lib/client.js';
|
|
4
|
+
import { WebSocketOptions, StreamRequest, EmitteryCallbacks, ConnectionEventData } from '../types/index.js';
|
|
5
|
+
import Source from './source.js';
|
|
6
|
+
|
|
7
|
+
declare class WebSocket extends Client {
|
|
8
|
+
#private;
|
|
9
|
+
socket?: WebSocket$1;
|
|
10
|
+
/**
|
|
11
|
+
* Create a new WebSocket client.
|
|
12
|
+
*
|
|
13
|
+
* @param args - Arguments to pass to the Client constructor.
|
|
14
|
+
*/
|
|
15
|
+
constructor({ sampleRate }: WebSocketOptions, ...args: ConstructorParameters<typeof Client>);
|
|
16
|
+
/**
|
|
17
|
+
* Send a message over the WebSocket in order to start a stream.
|
|
18
|
+
*
|
|
19
|
+
* @param inputs - Stream options.
|
|
20
|
+
* @param options - Options for the stream.
|
|
21
|
+
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
22
|
+
* If `0`, the stream will not time out.
|
|
23
|
+
* @returns A Source object that can be passed to a Player to play the audio.
|
|
24
|
+
*/
|
|
25
|
+
send(inputs: StreamRequest["inputs"], { timeout }?: StreamRequest["options"]): {
|
|
26
|
+
on: <Name extends keyof emittery.OmnipresentEventData | "message">(eventName: Name | readonly Name[], listener: (eventData: ({
|
|
27
|
+
message: string;
|
|
28
|
+
} & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
|
|
29
|
+
off: <Name_1 extends keyof emittery.OmnipresentEventData | "message">(eventName: Name_1 | readonly Name_1[], listener: (eventData: ({
|
|
30
|
+
message: string;
|
|
31
|
+
} & emittery.OmnipresentEventData)[Name_1]) => void | Promise<void>) => void;
|
|
32
|
+
once: <Name_2 extends keyof emittery.OmnipresentEventData | "message">(eventName: Name_2 | readonly Name_2[]) => emittery.EmitteryOncePromise<({
|
|
33
|
+
message: string;
|
|
34
|
+
} & emittery.OmnipresentEventData)[Name_2]>;
|
|
35
|
+
events: <Name_3 extends "message">(eventName: Name_3 | readonly Name_3[]) => AsyncIterableIterator<{
|
|
36
|
+
message: string;
|
|
37
|
+
}[Name_3]>;
|
|
38
|
+
source: Source;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Authenticate and connect to a Cartesia streaming WebSocket.
|
|
42
|
+
*
|
|
43
|
+
* @returns A promise that resolves when the WebSocket is connected.
|
|
44
|
+
* @throws {Error} If the WebSocket fails to connect.
|
|
45
|
+
*/
|
|
46
|
+
connect(): Promise<EmitteryCallbacks<ConnectionEventData>>;
|
|
47
|
+
/**
|
|
48
|
+
* Disconnect from the Cartesia streaming WebSocket.
|
|
49
|
+
*/
|
|
50
|
+
disconnect(): void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { WebSocket as default };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WebSocket
|
|
3
|
+
} from "../chunk-3F5E46FT.js";
|
|
4
|
+
import "../chunk-PQ6CIPFW.js";
|
|
5
|
+
import "../chunk-JGP5BIUV.js";
|
|
6
|
+
import "../chunk-XHTDPLFR.js";
|
|
7
|
+
import "../chunk-RO7TY474.js";
|
|
8
|
+
import "../chunk-WIFMLPT5.js";
|
|
9
|
+
export {
|
|
10
|
+
WebSocket as default
|
|
11
|
+
};
|
package/dist/types/index.d.cts
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
|
+
import emittery__default from 'emittery';
|
|
2
|
+
|
|
1
3
|
interface ClientOptions {
|
|
2
4
|
apiKey?: string;
|
|
3
5
|
baseUrl?: string;
|
|
4
6
|
}
|
|
7
|
+
type Sentinel = null;
|
|
8
|
+
type Chunk = string | Sentinel;
|
|
9
|
+
type ConnectionEventData = {
|
|
10
|
+
open: never;
|
|
11
|
+
close: never;
|
|
12
|
+
};
|
|
13
|
+
type StreamRequest = {
|
|
14
|
+
inputs: object;
|
|
15
|
+
options: {
|
|
16
|
+
timeout?: number;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
type EmitteryCallbacks<T> = {
|
|
20
|
+
on: emittery__default<T>["on"];
|
|
21
|
+
off: emittery__default<T>["off"];
|
|
22
|
+
once: emittery__default<T>["once"];
|
|
23
|
+
events: emittery__default<T>["events"];
|
|
24
|
+
};
|
|
25
|
+
type CloneOptions = {
|
|
26
|
+
mode: "url";
|
|
27
|
+
link: string;
|
|
28
|
+
} | {
|
|
29
|
+
mode: "clip";
|
|
30
|
+
clip: Blob;
|
|
31
|
+
};
|
|
32
|
+
type Voice = {
|
|
33
|
+
id: string;
|
|
34
|
+
name: string;
|
|
35
|
+
description: string;
|
|
36
|
+
embedding: number[];
|
|
37
|
+
is_public: boolean;
|
|
38
|
+
user_id: string;
|
|
39
|
+
created_at: string;
|
|
40
|
+
};
|
|
41
|
+
type CreateVoice = Pick<Voice, "name" | "description" | "embedding"> & Partial<Omit<Voice, "name" | "description" | "embedding">>;
|
|
42
|
+
type CloneResponse = {
|
|
43
|
+
embedding: number[];
|
|
44
|
+
};
|
|
45
|
+
type WebSocketOptions = {
|
|
46
|
+
sampleRate: number;
|
|
47
|
+
};
|
|
48
|
+
type SourceEventData = {
|
|
49
|
+
enqueue: never;
|
|
50
|
+
close: never;
|
|
51
|
+
wait: never;
|
|
52
|
+
read: never;
|
|
53
|
+
};
|
|
5
54
|
|
|
6
|
-
export type { ClientOptions };
|
|
55
|
+
export type { Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, CreateVoice, EmitteryCallbacks, Sentinel, SourceEventData, StreamRequest, Voice, WebSocketOptions };
|