@cartesia/cartesia-js 1.0.0-alpha.4 → 1.0.1
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 +49 -49
- package/CHANGELOG.md +23 -0
- package/LICENSE.md +21 -0
- package/README.md +102 -21
- package/dist/{chunk-VK7LBMVI.js → chunk-2NA5SEML.js} +2 -2
- package/dist/{chunk-PQ5EVEEH.js → chunk-5M33ZF3Y.js} +1 -1
- package/dist/{chunk-PQ6CIPFW.js → chunk-6YQ6KDIQ.js} +44 -5
- package/dist/{chunk-IQAXBRHU.js → chunk-ASZKHN7Q.js} +53 -29
- package/dist/{chunk-RO7TY474.js → chunk-BHY7MNGT.js} +11 -6
- package/dist/{chunk-WIFMLPT5.js → chunk-GHY2WEOK.js} +13 -0
- package/dist/{chunk-SGXUEFII.js → chunk-KUSVZXDT.js} +2 -2
- package/dist/{chunk-36JBKJUN.js → chunk-LZO6K34D.js} +20 -7
- package/dist/{chunk-3FL2SNIR.js → chunk-NQVZNVOU.js} +1 -1
- package/dist/{chunk-ISRU7PLL.js → chunk-OFH3ML4L.js} +3 -3
- package/dist/index.cjs +129 -39
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +15 -9
- package/dist/lib/client.js +2 -2
- package/dist/lib/constants.js +1 -1
- package/dist/lib/index.cjs +106 -33
- package/dist/lib/index.js +8 -8
- package/dist/react/index.cjs +231 -92
- package/dist/react/index.d.cts +4 -3
- package/dist/react/index.d.ts +4 -3
- package/dist/react/index.js +117 -64
- package/dist/react/utils.js +2 -2
- package/dist/tts/index.cjs +106 -33
- package/dist/tts/index.js +6 -6
- package/dist/tts/player.cjs +23 -5
- package/dist/tts/player.d.cts +6 -0
- package/dist/tts/player.d.ts +6 -0
- package/dist/tts/player.js +4 -3
- package/dist/tts/source.cjs +50 -4
- package/dist/tts/source.d.cts +16 -6
- package/dist/tts/source.d.ts +16 -6
- package/dist/tts/source.js +4 -2
- package/dist/tts/utils.cjs +18 -6
- package/dist/tts/utils.d.cts +7 -5
- package/dist/tts/utils.d.ts +7 -5
- package/dist/tts/utils.js +3 -2
- package/dist/tts/websocket.cjs +106 -33
- package/dist/tts/websocket.d.cts +20 -10
- package/dist/tts/websocket.d.ts +20 -10
- package/dist/tts/websocket.js +5 -5
- package/dist/types/index.d.cts +60 -4
- package/dist/types/index.d.ts +60 -4
- package/dist/voices/index.js +3 -3
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/react/index.ts +117 -62
- package/src/tts/player.ts +15 -8
- package/src/tts/source.ts +53 -7
- package/src/tts/utils.ts +26 -12
- package/src/tts/websocket.ts +42 -19
- package/src/types/index.ts +81 -3
package/dist/lib/index.cjs
CHANGED
|
@@ -22,6 +22,18 @@ var __spreadValues = (a, b) => {
|
|
|
22
22
|
return a;
|
|
23
23
|
};
|
|
24
24
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var __objRest = (source, exclude) => {
|
|
26
|
+
var target = {};
|
|
27
|
+
for (var prop in source)
|
|
28
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
+
target[prop] = source[prop];
|
|
30
|
+
if (source != null && __getOwnPropSymbols)
|
|
31
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
+
target[prop] = source[prop];
|
|
34
|
+
}
|
|
35
|
+
return target;
|
|
36
|
+
};
|
|
25
37
|
var __export = (target, all) => {
|
|
26
38
|
for (var name in all)
|
|
27
39
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -134,7 +146,13 @@ var import_partysocket = require("partysocket");
|
|
|
134
146
|
|
|
135
147
|
// src/tts/source.ts
|
|
136
148
|
var import_emittery = __toESM(require("emittery"), 1);
|
|
137
|
-
var
|
|
149
|
+
var ENCODING_MAP = {
|
|
150
|
+
pcm_f32le: { arrayType: Float32Array, bytesPerElement: 4 },
|
|
151
|
+
pcm_s16le: { arrayType: Int16Array, bytesPerElement: 2 },
|
|
152
|
+
pcm_alaw: { arrayType: Uint8Array, bytesPerElement: 1 },
|
|
153
|
+
pcm_mulaw: { arrayType: Uint8Array, bytesPerElement: 1 }
|
|
154
|
+
};
|
|
155
|
+
var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _createBuffer, createBuffer_fn;
|
|
138
156
|
var Source = class {
|
|
139
157
|
/**
|
|
140
158
|
* Create a new Source.
|
|
@@ -142,23 +160,44 @@ var Source = class {
|
|
|
142
160
|
* @param options - Options for the Source.
|
|
143
161
|
* @param options.sampleRate - The sample rate of the audio.
|
|
144
162
|
*/
|
|
145
|
-
constructor({
|
|
163
|
+
constructor({
|
|
164
|
+
sampleRate,
|
|
165
|
+
encoding,
|
|
166
|
+
container
|
|
167
|
+
}) {
|
|
168
|
+
/**
|
|
169
|
+
* Create a new buffer for the source.
|
|
170
|
+
*
|
|
171
|
+
* @param size - The size of the buffer to create.
|
|
172
|
+
* @returns The new buffer as a TypedArray based on the encoding.
|
|
173
|
+
*/
|
|
174
|
+
__privateAdd(this, _createBuffer);
|
|
146
175
|
__privateAdd(this, _emitter, new import_emittery.default());
|
|
147
176
|
__privateAdd(this, _buffer, void 0);
|
|
148
177
|
__privateAdd(this, _readIndex, 0);
|
|
149
178
|
__privateAdd(this, _writeIndex, 0);
|
|
150
179
|
__privateAdd(this, _closed, false);
|
|
151
180
|
__privateAdd(this, _sampleRate, void 0);
|
|
181
|
+
__privateAdd(this, _encoding, void 0);
|
|
182
|
+
__privateAdd(this, _container, void 0);
|
|
152
183
|
this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
|
|
153
184
|
this.once = __privateGet(this, _emitter).once.bind(__privateGet(this, _emitter));
|
|
154
185
|
this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
|
|
155
186
|
this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
|
|
156
187
|
__privateSet(this, _sampleRate, sampleRate);
|
|
157
|
-
__privateSet(this,
|
|
188
|
+
__privateSet(this, _encoding, encoding);
|
|
189
|
+
__privateSet(this, _container, container);
|
|
190
|
+
__privateSet(this, _buffer, __privateMethod(this, _createBuffer, createBuffer_fn).call(this, 1024));
|
|
158
191
|
}
|
|
159
192
|
get sampleRate() {
|
|
160
193
|
return __privateGet(this, _sampleRate);
|
|
161
194
|
}
|
|
195
|
+
get encoding() {
|
|
196
|
+
return __privateGet(this, _encoding);
|
|
197
|
+
}
|
|
198
|
+
get container() {
|
|
199
|
+
return __privateGet(this, _container);
|
|
200
|
+
}
|
|
162
201
|
/**
|
|
163
202
|
* Append audio to the buffer.
|
|
164
203
|
*
|
|
@@ -172,7 +211,7 @@ var Source = class {
|
|
|
172
211
|
while (newCapacity < requiredCapacity) {
|
|
173
212
|
newCapacity *= 2;
|
|
174
213
|
}
|
|
175
|
-
const newBuffer =
|
|
214
|
+
const newBuffer = __privateMethod(this, _createBuffer, createBuffer_fn).call(this, newCapacity);
|
|
176
215
|
newBuffer.set(__privateGet(this, _buffer));
|
|
177
216
|
__privateSet(this, _buffer, newBuffer);
|
|
178
217
|
}
|
|
@@ -220,6 +259,9 @@ var Source = class {
|
|
|
220
259
|
get readIndex() {
|
|
221
260
|
return __privateGet(this, _readIndex);
|
|
222
261
|
}
|
|
262
|
+
get writeIndex() {
|
|
263
|
+
return __privateGet(this, _writeIndex);
|
|
264
|
+
}
|
|
223
265
|
/**
|
|
224
266
|
* Close the source. This signals that no more audio will be enqueued.
|
|
225
267
|
*
|
|
@@ -241,19 +283,27 @@ _readIndex = new WeakMap();
|
|
|
241
283
|
_writeIndex = new WeakMap();
|
|
242
284
|
_closed = new WeakMap();
|
|
243
285
|
_sampleRate = new WeakMap();
|
|
286
|
+
_encoding = new WeakMap();
|
|
287
|
+
_container = new WeakMap();
|
|
288
|
+
_createBuffer = new WeakSet();
|
|
289
|
+
createBuffer_fn = function(size) {
|
|
290
|
+
const { arrayType: ArrayType } = ENCODING_MAP[__privateGet(this, _encoding)];
|
|
291
|
+
return new ArrayType(size);
|
|
292
|
+
};
|
|
244
293
|
|
|
245
294
|
// src/tts/utils.ts
|
|
246
295
|
var import_base64_js = __toESM(require("base64-js"), 1);
|
|
247
|
-
function base64ToArray(b64) {
|
|
296
|
+
function base64ToArray(b64, encoding) {
|
|
248
297
|
const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
|
|
298
|
+
const { arrayType: ArrayType, bytesPerElement } = ENCODING_MAP[encoding];
|
|
249
299
|
const totalLength = byteArrays.reduce(
|
|
250
|
-
(acc, arr) => acc + arr.length /
|
|
300
|
+
(acc, arr) => acc + arr.length / bytesPerElement,
|
|
251
301
|
0
|
|
252
302
|
);
|
|
253
|
-
const result = new
|
|
303
|
+
const result = new ArrayType(totalLength);
|
|
254
304
|
let offset = 0;
|
|
255
305
|
for (const arr of byteArrays) {
|
|
256
|
-
const floats = new
|
|
306
|
+
const floats = new ArrayType(arr.buffer);
|
|
257
307
|
result.set(floats, offset);
|
|
258
308
|
offset += floats.length;
|
|
259
309
|
}
|
|
@@ -271,10 +321,10 @@ function createMessageHandlerForContextId(contextId, handler) {
|
|
|
271
321
|
let chunk;
|
|
272
322
|
if (message.done) {
|
|
273
323
|
chunk = getSentinel();
|
|
274
|
-
} else {
|
|
324
|
+
} else if (message.type === "chunk") {
|
|
275
325
|
chunk = message.data;
|
|
276
326
|
}
|
|
277
|
-
handler({ chunk, message: event.data });
|
|
327
|
+
handler({ chunk, message: event.data, data: message });
|
|
278
328
|
};
|
|
279
329
|
}
|
|
280
330
|
function getSentinel() {
|
|
@@ -298,14 +348,14 @@ function getEmitteryCallbacks(emitter) {
|
|
|
298
348
|
}
|
|
299
349
|
|
|
300
350
|
// src/tts/websocket.ts
|
|
301
|
-
var _isConnected, _sampleRate2, _generateId, generateId_fn;
|
|
351
|
+
var _isConnected, _sampleRate2, _container2, _encoding2, _generateId, generateId_fn;
|
|
302
352
|
var WebSocket = class extends Client {
|
|
303
353
|
/**
|
|
304
354
|
* Create a new WebSocket client.
|
|
305
355
|
*
|
|
306
356
|
* @param args - Arguments to pass to the Client constructor.
|
|
307
357
|
*/
|
|
308
|
-
constructor({ sampleRate }, ...args) {
|
|
358
|
+
constructor({ sampleRate, container, encoding }, ...args) {
|
|
309
359
|
super(...args);
|
|
310
360
|
/**
|
|
311
361
|
* Generate a unique ID suitable for a streaming context.
|
|
@@ -318,37 +368,47 @@ var WebSocket = class extends Client {
|
|
|
318
368
|
__privateAdd(this, _generateId);
|
|
319
369
|
__privateAdd(this, _isConnected, false);
|
|
320
370
|
__privateAdd(this, _sampleRate2, void 0);
|
|
371
|
+
__privateAdd(this, _container2, void 0);
|
|
372
|
+
__privateAdd(this, _encoding2, void 0);
|
|
321
373
|
__privateSet(this, _sampleRate2, sampleRate);
|
|
374
|
+
__privateSet(this, _container2, container != null ? container : "raw");
|
|
375
|
+
__privateSet(this, _encoding2, encoding != null ? encoding : "pcm_f32le");
|
|
322
376
|
}
|
|
323
377
|
/**
|
|
324
|
-
* Send a message over the WebSocket
|
|
378
|
+
* Send a message over the WebSocket to start a stream.
|
|
325
379
|
*
|
|
326
|
-
* @param inputs - Stream options.
|
|
380
|
+
* @param inputs - Stream options. Defined in the StreamRequest type.
|
|
327
381
|
* @param options - Options for the stream.
|
|
328
382
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
329
|
-
*
|
|
383
|
+
* If set to `0`, the stream will not time out.
|
|
330
384
|
* @returns A Source object that can be passed to a Player to play the audio.
|
|
385
|
+
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
386
|
+
* @returns An abort function that can be called to cancel the stream.
|
|
331
387
|
*/
|
|
332
|
-
send(
|
|
333
|
-
var
|
|
388
|
+
send(_a, { timeout = 0 } = {}) {
|
|
389
|
+
var inputs = __objRest(_a, []);
|
|
390
|
+
var _a2, _b, _c, _d;
|
|
334
391
|
if (!__privateGet(this, _isConnected)) {
|
|
335
392
|
throw new Error("Not connected to WebSocket. Call .connect() first.");
|
|
336
393
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
394
|
+
if (!inputs.context_id) {
|
|
395
|
+
inputs.context_id = __privateMethod(this, _generateId, generateId_fn).call(this);
|
|
396
|
+
}
|
|
397
|
+
if (!inputs.output_format) {
|
|
398
|
+
inputs.output_format = {
|
|
399
|
+
container: __privateGet(this, _container2),
|
|
400
|
+
encoding: __privateGet(this, _encoding2),
|
|
401
|
+
sample_rate: __privateGet(this, _sampleRate2)
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
(_a2 = this.socket) == null ? void 0 : _a2.send(
|
|
405
|
+
JSON.stringify(__spreadValues({}, inputs))
|
|
348
406
|
);
|
|
349
407
|
const emitter = new import_emittery2.default();
|
|
350
408
|
const source = new Source({
|
|
351
|
-
sampleRate: __privateGet(this, _sampleRate2)
|
|
409
|
+
sampleRate: __privateGet(this, _sampleRate2),
|
|
410
|
+
encoding: __privateGet(this, _encoding2),
|
|
411
|
+
container: __privateGet(this, _container2)
|
|
352
412
|
});
|
|
353
413
|
const streamCompleteController = new AbortController();
|
|
354
414
|
let timeoutId = null;
|
|
@@ -356,19 +416,26 @@ var WebSocket = class extends Client {
|
|
|
356
416
|
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
357
417
|
}
|
|
358
418
|
const handleMessage = createMessageHandlerForContextId(
|
|
359
|
-
|
|
360
|
-
(_0) => __async(this, [_0], function* ({ chunk, message }) {
|
|
419
|
+
inputs.context_id,
|
|
420
|
+
(_0) => __async(this, [_0], function* ({ chunk, message, data }) {
|
|
361
421
|
emitter.emit("message", message);
|
|
422
|
+
if (data.type === "timestamps") {
|
|
423
|
+
emitter.emit("timestamps", data.word_timestamps);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
362
426
|
if (isSentinel(chunk)) {
|
|
363
427
|
yield source.close();
|
|
364
428
|
streamCompleteController.abort();
|
|
365
429
|
return;
|
|
366
430
|
}
|
|
367
|
-
yield source.enqueue(base64ToArray([chunk]));
|
|
368
431
|
if (timeoutId) {
|
|
369
432
|
clearTimeout(timeoutId);
|
|
370
433
|
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
371
434
|
}
|
|
435
|
+
if (!chunk) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
yield source.enqueue(base64ToArray([chunk], __privateGet(this, _encoding2)));
|
|
372
439
|
})
|
|
373
440
|
);
|
|
374
441
|
(_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
|
|
@@ -398,7 +465,11 @@ var WebSocket = class extends Client {
|
|
|
398
465
|
clearTimeout(timeoutId);
|
|
399
466
|
}
|
|
400
467
|
});
|
|
401
|
-
return __spreadValues({
|
|
468
|
+
return __spreadProps(__spreadValues({
|
|
469
|
+
source
|
|
470
|
+
}, getEmitteryCallbacks(emitter)), {
|
|
471
|
+
stop: streamCompleteController.abort.bind(streamCompleteController)
|
|
472
|
+
});
|
|
402
473
|
}
|
|
403
474
|
/**
|
|
404
475
|
* Authenticate and connect to a Cartesia streaming WebSocket.
|
|
@@ -468,6 +539,8 @@ var WebSocket = class extends Client {
|
|
|
468
539
|
};
|
|
469
540
|
_isConnected = new WeakMap();
|
|
470
541
|
_sampleRate2 = new WeakMap();
|
|
542
|
+
_container2 = new WeakMap();
|
|
543
|
+
_encoding2 = new WeakMap();
|
|
471
544
|
_generateId = new WeakSet();
|
|
472
545
|
generateId_fn = function() {
|
|
473
546
|
return (0, import_human_id.humanId)({
|
package/dist/lib/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Cartesia
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-
|
|
7
|
-
import "../chunk-
|
|
8
|
-
import "../chunk-PQ5EVEEH.js";
|
|
3
|
+
} from "../chunk-OFH3ML4L.js";
|
|
4
|
+
import "../chunk-KUSVZXDT.js";
|
|
5
|
+
import "../chunk-2NA5SEML.js";
|
|
6
|
+
import "../chunk-ASZKHN7Q.js";
|
|
7
|
+
import "../chunk-5M33ZF3Y.js";
|
|
9
8
|
import "../chunk-2BFEKY3F.js";
|
|
10
|
-
import "../chunk-
|
|
11
|
-
import "../chunk-
|
|
9
|
+
import "../chunk-BHY7MNGT.js";
|
|
10
|
+
import "../chunk-6YQ6KDIQ.js";
|
|
11
|
+
import "../chunk-GHY2WEOK.js";
|
|
12
12
|
export {
|
|
13
13
|
Cartesia
|
|
14
14
|
};
|