@cartesia/cartesia-js 1.0.0 → 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 +47 -47
- package/CHANGELOG.md +6 -0
- package/LICENSE.md +21 -0
- package/README.md +92 -19
- package/dist/{chunk-F4QWVJY3.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-FN7BK4PS.js → chunk-ASZKHN7Q.js} +44 -26
- 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-JYLAM6VU.js → chunk-LZO6K34D.js} +2 -2
- package/dist/{chunk-3FL2SNIR.js → chunk-NQVZNVOU.js} +1 -1
- package/dist/{chunk-IEN4NCER.js → chunk-OFH3ML4L.js} +3 -3
- package/dist/index.cjs +102 -31
- 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 +97 -30
- package/dist/lib/index.js +8 -8
- package/dist/react/index.cjs +202 -86
- package/dist/react/index.d.cts +4 -3
- package/dist/react/index.d.ts +4 -3
- package/dist/react/index.js +115 -66
- package/dist/react/utils.js +2 -2
- package/dist/tts/index.cjs +97 -30
- package/dist/tts/index.js +6 -6
- package/dist/tts/player.cjs +5 -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 +97 -30
- package/dist/tts/websocket.d.cts +12 -8
- package/dist/tts/websocket.d.ts +12 -8
- 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 +114 -63
- package/src/tts/source.ts +53 -7
- package/src/tts/utils.ts +26 -12
- package/src/tts/websocket.ts +33 -16
- package/src/types/index.ts +81 -3
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
playAudioBuffer
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-BHY7MNGT.js";
|
|
4
4
|
import {
|
|
5
5
|
__async,
|
|
6
6
|
__privateAdd,
|
|
7
7
|
__privateGet,
|
|
8
8
|
__privateMethod,
|
|
9
9
|
__privateSet
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-GHY2WEOK.js";
|
|
11
11
|
|
|
12
12
|
// src/tts/player.ts
|
|
13
13
|
var _context, _startNextPlaybackAt, _bufferDuration, _playBuffer, playBuffer_fn;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Voices
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-KUSVZXDT.js";
|
|
4
4
|
import {
|
|
5
5
|
TTS
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-2NA5SEML.js";
|
|
7
7
|
import {
|
|
8
8
|
Client
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-5M33ZF3Y.js";
|
|
10
10
|
|
|
11
11
|
// src/lib/index.ts
|
|
12
12
|
var Cartesia = class extends Client {
|
package/dist/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 });
|
|
@@ -90,7 +102,9 @@ var __async = (__this, __arguments, generator) => {
|
|
|
90
102
|
var src_exports = {};
|
|
91
103
|
__export(src_exports, {
|
|
92
104
|
Cartesia: () => Cartesia,
|
|
105
|
+
Source: () => Source,
|
|
93
106
|
WebPlayer: () => Player,
|
|
107
|
+
WebSocket: () => WebSocket,
|
|
94
108
|
default: () => Cartesia
|
|
95
109
|
});
|
|
96
110
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -136,7 +150,13 @@ var import_partysocket = require("partysocket");
|
|
|
136
150
|
|
|
137
151
|
// src/tts/source.ts
|
|
138
152
|
var import_emittery = __toESM(require("emittery"), 1);
|
|
139
|
-
var
|
|
153
|
+
var ENCODING_MAP = {
|
|
154
|
+
pcm_f32le: { arrayType: Float32Array, bytesPerElement: 4 },
|
|
155
|
+
pcm_s16le: { arrayType: Int16Array, bytesPerElement: 2 },
|
|
156
|
+
pcm_alaw: { arrayType: Uint8Array, bytesPerElement: 1 },
|
|
157
|
+
pcm_mulaw: { arrayType: Uint8Array, bytesPerElement: 1 }
|
|
158
|
+
};
|
|
159
|
+
var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _createBuffer, createBuffer_fn;
|
|
140
160
|
var Source = class {
|
|
141
161
|
/**
|
|
142
162
|
* Create a new Source.
|
|
@@ -144,23 +164,44 @@ var Source = class {
|
|
|
144
164
|
* @param options - Options for the Source.
|
|
145
165
|
* @param options.sampleRate - The sample rate of the audio.
|
|
146
166
|
*/
|
|
147
|
-
constructor({
|
|
167
|
+
constructor({
|
|
168
|
+
sampleRate,
|
|
169
|
+
encoding,
|
|
170
|
+
container
|
|
171
|
+
}) {
|
|
172
|
+
/**
|
|
173
|
+
* Create a new buffer for the source.
|
|
174
|
+
*
|
|
175
|
+
* @param size - The size of the buffer to create.
|
|
176
|
+
* @returns The new buffer as a TypedArray based on the encoding.
|
|
177
|
+
*/
|
|
178
|
+
__privateAdd(this, _createBuffer);
|
|
148
179
|
__privateAdd(this, _emitter, new import_emittery.default());
|
|
149
180
|
__privateAdd(this, _buffer, void 0);
|
|
150
181
|
__privateAdd(this, _readIndex, 0);
|
|
151
182
|
__privateAdd(this, _writeIndex, 0);
|
|
152
183
|
__privateAdd(this, _closed, false);
|
|
153
184
|
__privateAdd(this, _sampleRate, void 0);
|
|
185
|
+
__privateAdd(this, _encoding, void 0);
|
|
186
|
+
__privateAdd(this, _container, void 0);
|
|
154
187
|
this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
|
|
155
188
|
this.once = __privateGet(this, _emitter).once.bind(__privateGet(this, _emitter));
|
|
156
189
|
this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
|
|
157
190
|
this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
|
|
158
191
|
__privateSet(this, _sampleRate, sampleRate);
|
|
159
|
-
__privateSet(this,
|
|
192
|
+
__privateSet(this, _encoding, encoding);
|
|
193
|
+
__privateSet(this, _container, container);
|
|
194
|
+
__privateSet(this, _buffer, __privateMethod(this, _createBuffer, createBuffer_fn).call(this, 1024));
|
|
160
195
|
}
|
|
161
196
|
get sampleRate() {
|
|
162
197
|
return __privateGet(this, _sampleRate);
|
|
163
198
|
}
|
|
199
|
+
get encoding() {
|
|
200
|
+
return __privateGet(this, _encoding);
|
|
201
|
+
}
|
|
202
|
+
get container() {
|
|
203
|
+
return __privateGet(this, _container);
|
|
204
|
+
}
|
|
164
205
|
/**
|
|
165
206
|
* Append audio to the buffer.
|
|
166
207
|
*
|
|
@@ -174,7 +215,7 @@ var Source = class {
|
|
|
174
215
|
while (newCapacity < requiredCapacity) {
|
|
175
216
|
newCapacity *= 2;
|
|
176
217
|
}
|
|
177
|
-
const newBuffer =
|
|
218
|
+
const newBuffer = __privateMethod(this, _createBuffer, createBuffer_fn).call(this, newCapacity);
|
|
178
219
|
newBuffer.set(__privateGet(this, _buffer));
|
|
179
220
|
__privateSet(this, _buffer, newBuffer);
|
|
180
221
|
}
|
|
@@ -222,6 +263,9 @@ var Source = class {
|
|
|
222
263
|
get readIndex() {
|
|
223
264
|
return __privateGet(this, _readIndex);
|
|
224
265
|
}
|
|
266
|
+
get writeIndex() {
|
|
267
|
+
return __privateGet(this, _writeIndex);
|
|
268
|
+
}
|
|
225
269
|
/**
|
|
226
270
|
* Close the source. This signals that no more audio will be enqueued.
|
|
227
271
|
*
|
|
@@ -243,19 +287,27 @@ _readIndex = new WeakMap();
|
|
|
243
287
|
_writeIndex = new WeakMap();
|
|
244
288
|
_closed = new WeakMap();
|
|
245
289
|
_sampleRate = new WeakMap();
|
|
290
|
+
_encoding = new WeakMap();
|
|
291
|
+
_container = new WeakMap();
|
|
292
|
+
_createBuffer = new WeakSet();
|
|
293
|
+
createBuffer_fn = function(size) {
|
|
294
|
+
const { arrayType: ArrayType } = ENCODING_MAP[__privateGet(this, _encoding)];
|
|
295
|
+
return new ArrayType(size);
|
|
296
|
+
};
|
|
246
297
|
|
|
247
298
|
// src/tts/utils.ts
|
|
248
299
|
var import_base64_js = __toESM(require("base64-js"), 1);
|
|
249
|
-
function base64ToArray(b64) {
|
|
300
|
+
function base64ToArray(b64, encoding) {
|
|
250
301
|
const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
|
|
302
|
+
const { arrayType: ArrayType, bytesPerElement } = ENCODING_MAP[encoding];
|
|
251
303
|
const totalLength = byteArrays.reduce(
|
|
252
|
-
(acc, arr) => acc + arr.length /
|
|
304
|
+
(acc, arr) => acc + arr.length / bytesPerElement,
|
|
253
305
|
0
|
|
254
306
|
);
|
|
255
|
-
const result = new
|
|
307
|
+
const result = new ArrayType(totalLength);
|
|
256
308
|
let offset = 0;
|
|
257
309
|
for (const arr of byteArrays) {
|
|
258
|
-
const floats = new
|
|
310
|
+
const floats = new ArrayType(arr.buffer);
|
|
259
311
|
result.set(floats, offset);
|
|
260
312
|
offset += floats.length;
|
|
261
313
|
}
|
|
@@ -286,10 +338,10 @@ function createMessageHandlerForContextId(contextId, handler) {
|
|
|
286
338
|
let chunk;
|
|
287
339
|
if (message.done) {
|
|
288
340
|
chunk = getSentinel();
|
|
289
|
-
} else {
|
|
341
|
+
} else if (message.type === "chunk") {
|
|
290
342
|
chunk = message.data;
|
|
291
343
|
}
|
|
292
|
-
handler({ chunk, message: event.data });
|
|
344
|
+
handler({ chunk, message: event.data, data: message });
|
|
293
345
|
};
|
|
294
346
|
}
|
|
295
347
|
function getSentinel() {
|
|
@@ -313,14 +365,14 @@ function getEmitteryCallbacks(emitter) {
|
|
|
313
365
|
}
|
|
314
366
|
|
|
315
367
|
// src/tts/websocket.ts
|
|
316
|
-
var _isConnected, _sampleRate2, _generateId, generateId_fn;
|
|
368
|
+
var _isConnected, _sampleRate2, _container2, _encoding2, _generateId, generateId_fn;
|
|
317
369
|
var WebSocket = class extends Client {
|
|
318
370
|
/**
|
|
319
371
|
* Create a new WebSocket client.
|
|
320
372
|
*
|
|
321
373
|
* @param args - Arguments to pass to the Client constructor.
|
|
322
374
|
*/
|
|
323
|
-
constructor({ sampleRate }, ...args) {
|
|
375
|
+
constructor({ sampleRate, container, encoding }, ...args) {
|
|
324
376
|
super(...args);
|
|
325
377
|
/**
|
|
326
378
|
* Generate a unique ID suitable for a streaming context.
|
|
@@ -333,12 +385,16 @@ var WebSocket = class extends Client {
|
|
|
333
385
|
__privateAdd(this, _generateId);
|
|
334
386
|
__privateAdd(this, _isConnected, false);
|
|
335
387
|
__privateAdd(this, _sampleRate2, void 0);
|
|
388
|
+
__privateAdd(this, _container2, void 0);
|
|
389
|
+
__privateAdd(this, _encoding2, void 0);
|
|
336
390
|
__privateSet(this, _sampleRate2, sampleRate);
|
|
391
|
+
__privateSet(this, _container2, container != null ? container : "raw");
|
|
392
|
+
__privateSet(this, _encoding2, encoding != null ? encoding : "pcm_f32le");
|
|
337
393
|
}
|
|
338
394
|
/**
|
|
339
395
|
* Send a message over the WebSocket to start a stream.
|
|
340
396
|
*
|
|
341
|
-
* @param inputs - Stream options.
|
|
397
|
+
* @param inputs - Stream options. Defined in the StreamRequest type.
|
|
342
398
|
* @param options - Options for the stream.
|
|
343
399
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
344
400
|
* If set to `0`, the stream will not time out.
|
|
@@ -346,26 +402,30 @@ var WebSocket = class extends Client {
|
|
|
346
402
|
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
347
403
|
* @returns An abort function that can be called to cancel the stream.
|
|
348
404
|
*/
|
|
349
|
-
send(
|
|
350
|
-
var
|
|
405
|
+
send(_a, { timeout = 0 } = {}) {
|
|
406
|
+
var inputs = __objRest(_a, []);
|
|
407
|
+
var _a2, _b, _c, _d;
|
|
351
408
|
if (!__privateGet(this, _isConnected)) {
|
|
352
409
|
throw new Error("Not connected to WebSocket. Call .connect() first.");
|
|
353
410
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
411
|
+
if (!inputs.context_id) {
|
|
412
|
+
inputs.context_id = __privateMethod(this, _generateId, generateId_fn).call(this);
|
|
413
|
+
}
|
|
414
|
+
if (!inputs.output_format) {
|
|
415
|
+
inputs.output_format = {
|
|
416
|
+
container: __privateGet(this, _container2),
|
|
417
|
+
encoding: __privateGet(this, _encoding2),
|
|
418
|
+
sample_rate: __privateGet(this, _sampleRate2)
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
(_a2 = this.socket) == null ? void 0 : _a2.send(
|
|
422
|
+
JSON.stringify(__spreadValues({}, inputs))
|
|
365
423
|
);
|
|
366
424
|
const emitter = new import_emittery2.default();
|
|
367
425
|
const source = new Source({
|
|
368
|
-
sampleRate: __privateGet(this, _sampleRate2)
|
|
426
|
+
sampleRate: __privateGet(this, _sampleRate2),
|
|
427
|
+
encoding: __privateGet(this, _encoding2),
|
|
428
|
+
container: __privateGet(this, _container2)
|
|
369
429
|
});
|
|
370
430
|
const streamCompleteController = new AbortController();
|
|
371
431
|
let timeoutId = null;
|
|
@@ -373,19 +433,26 @@ var WebSocket = class extends Client {
|
|
|
373
433
|
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
374
434
|
}
|
|
375
435
|
const handleMessage = createMessageHandlerForContextId(
|
|
376
|
-
|
|
377
|
-
(_0) => __async(this, [_0], function* ({ chunk, message }) {
|
|
436
|
+
inputs.context_id,
|
|
437
|
+
(_0) => __async(this, [_0], function* ({ chunk, message, data }) {
|
|
378
438
|
emitter.emit("message", message);
|
|
439
|
+
if (data.type === "timestamps") {
|
|
440
|
+
emitter.emit("timestamps", data.word_timestamps);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
379
443
|
if (isSentinel(chunk)) {
|
|
380
444
|
yield source.close();
|
|
381
445
|
streamCompleteController.abort();
|
|
382
446
|
return;
|
|
383
447
|
}
|
|
384
|
-
yield source.enqueue(base64ToArray([chunk]));
|
|
385
448
|
if (timeoutId) {
|
|
386
449
|
clearTimeout(timeoutId);
|
|
387
450
|
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
388
451
|
}
|
|
452
|
+
if (!chunk) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
yield source.enqueue(base64ToArray([chunk], __privateGet(this, _encoding2)));
|
|
389
456
|
})
|
|
390
457
|
);
|
|
391
458
|
(_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
|
|
@@ -489,6 +556,8 @@ var WebSocket = class extends Client {
|
|
|
489
556
|
};
|
|
490
557
|
_isConnected = new WeakMap();
|
|
491
558
|
_sampleRate2 = new WeakMap();
|
|
559
|
+
_container2 = new WeakMap();
|
|
560
|
+
_encoding2 = new WeakMap();
|
|
492
561
|
_generateId = new WeakSet();
|
|
493
562
|
generateId_fn = function() {
|
|
494
563
|
return (0, import_human_id.humanId)({
|
|
@@ -689,5 +758,7 @@ playBuffer_fn = function(buf, sampleRate) {
|
|
|
689
758
|
// Annotate the CommonJS export names for ESM import in node:
|
|
690
759
|
0 && (module.exports = {
|
|
691
760
|
Cartesia,
|
|
692
|
-
|
|
761
|
+
Source,
|
|
762
|
+
WebPlayer,
|
|
763
|
+
WebSocket
|
|
693
764
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export { Cartesia, Cartesia as default } from './lib/index.cjs';
|
|
2
|
-
export { Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, CreateVoice, EmitteryCallbacks, Sentinel, SourceEventData, StreamRequest, Voice, WebSocketOptions } from './types/index.cjs';
|
|
2
|
+
export { Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, Voice, VoiceOptions, VoiceSpecifier, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps } from './types/index.cjs';
|
|
3
3
|
export { default as WebPlayer } from './tts/player.cjs';
|
|
4
|
+
export { default as Source } from './tts/source.cjs';
|
|
5
|
+
export { default as WebSocket } from './tts/websocket.cjs';
|
|
4
6
|
import './tts/index.cjs';
|
|
5
7
|
import './lib/client.cjs';
|
|
6
|
-
import './
|
|
8
|
+
import './voices/index.cjs';
|
|
7
9
|
import 'emittery';
|
|
8
10
|
import 'partysocket';
|
|
9
|
-
import './tts/source.cjs';
|
|
10
|
-
import './voices/index.cjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export { Cartesia, Cartesia as default } from './lib/index.js';
|
|
2
|
-
export { Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, CreateVoice, EmitteryCallbacks, Sentinel, SourceEventData, StreamRequest, Voice, WebSocketOptions } from './types/index.js';
|
|
2
|
+
export { Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, Voice, VoiceOptions, VoiceSpecifier, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps } from './types/index.js';
|
|
3
3
|
export { default as WebPlayer } from './tts/player.js';
|
|
4
|
+
export { default as Source } from './tts/source.js';
|
|
5
|
+
export { default as WebSocket } from './tts/websocket.js';
|
|
4
6
|
import './tts/index.js';
|
|
5
7
|
import './lib/client.js';
|
|
6
|
-
import './
|
|
8
|
+
import './voices/index.js';
|
|
7
9
|
import 'emittery';
|
|
8
10
|
import 'partysocket';
|
|
9
|
-
import './tts/source.js';
|
|
10
|
-
import './voices/index.js';
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import "./chunk-FXPGR372.js";
|
|
2
2
|
import {
|
|
3
3
|
Cartesia
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
import "./chunk-
|
|
6
|
-
import "./chunk-
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
} from "./chunk-OFH3ML4L.js";
|
|
5
|
+
import "./chunk-KUSVZXDT.js";
|
|
6
|
+
import "./chunk-2NA5SEML.js";
|
|
7
|
+
import {
|
|
8
|
+
WebSocket
|
|
9
|
+
} from "./chunk-ASZKHN7Q.js";
|
|
10
|
+
import "./chunk-5M33ZF3Y.js";
|
|
10
11
|
import "./chunk-2BFEKY3F.js";
|
|
11
12
|
import {
|
|
12
13
|
Player
|
|
13
|
-
} from "./chunk-
|
|
14
|
-
import "./chunk-
|
|
15
|
-
import
|
|
14
|
+
} from "./chunk-LZO6K34D.js";
|
|
15
|
+
import "./chunk-BHY7MNGT.js";
|
|
16
|
+
import {
|
|
17
|
+
Source
|
|
18
|
+
} from "./chunk-6YQ6KDIQ.js";
|
|
19
|
+
import "./chunk-GHY2WEOK.js";
|
|
16
20
|
export {
|
|
17
21
|
Cartesia,
|
|
22
|
+
Source,
|
|
18
23
|
Player as WebPlayer,
|
|
24
|
+
WebSocket,
|
|
19
25
|
Cartesia as default
|
|
20
26
|
};
|
package/dist/lib/client.js
CHANGED
package/dist/lib/constants.js
CHANGED
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,12 +368,16 @@ 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
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.
|
|
@@ -331,26 +385,30 @@ var WebSocket = class extends Client {
|
|
|
331
385
|
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
332
386
|
* @returns An abort function that can be called to cancel the stream.
|
|
333
387
|
*/
|
|
334
|
-
send(
|
|
335
|
-
var
|
|
388
|
+
send(_a, { timeout = 0 } = {}) {
|
|
389
|
+
var inputs = __objRest(_a, []);
|
|
390
|
+
var _a2, _b, _c, _d;
|
|
336
391
|
if (!__privateGet(this, _isConnected)) {
|
|
337
392
|
throw new Error("Not connected to WebSocket. Call .connect() first.");
|
|
338
393
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
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))
|
|
350
406
|
);
|
|
351
407
|
const emitter = new import_emittery2.default();
|
|
352
408
|
const source = new Source({
|
|
353
|
-
sampleRate: __privateGet(this, _sampleRate2)
|
|
409
|
+
sampleRate: __privateGet(this, _sampleRate2),
|
|
410
|
+
encoding: __privateGet(this, _encoding2),
|
|
411
|
+
container: __privateGet(this, _container2)
|
|
354
412
|
});
|
|
355
413
|
const streamCompleteController = new AbortController();
|
|
356
414
|
let timeoutId = null;
|
|
@@ -358,19 +416,26 @@ var WebSocket = class extends Client {
|
|
|
358
416
|
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
359
417
|
}
|
|
360
418
|
const handleMessage = createMessageHandlerForContextId(
|
|
361
|
-
|
|
362
|
-
(_0) => __async(this, [_0], function* ({ chunk, message }) {
|
|
419
|
+
inputs.context_id,
|
|
420
|
+
(_0) => __async(this, [_0], function* ({ chunk, message, data }) {
|
|
363
421
|
emitter.emit("message", message);
|
|
422
|
+
if (data.type === "timestamps") {
|
|
423
|
+
emitter.emit("timestamps", data.word_timestamps);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
364
426
|
if (isSentinel(chunk)) {
|
|
365
427
|
yield source.close();
|
|
366
428
|
streamCompleteController.abort();
|
|
367
429
|
return;
|
|
368
430
|
}
|
|
369
|
-
yield source.enqueue(base64ToArray([chunk]));
|
|
370
431
|
if (timeoutId) {
|
|
371
432
|
clearTimeout(timeoutId);
|
|
372
433
|
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
373
434
|
}
|
|
435
|
+
if (!chunk) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
yield source.enqueue(base64ToArray([chunk], __privateGet(this, _encoding2)));
|
|
374
439
|
})
|
|
375
440
|
);
|
|
376
441
|
(_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
|
|
@@ -474,6 +539,8 @@ var WebSocket = class extends Client {
|
|
|
474
539
|
};
|
|
475
540
|
_isConnected = new WeakMap();
|
|
476
541
|
_sampleRate2 = new WeakMap();
|
|
542
|
+
_container2 = new WeakMap();
|
|
543
|
+
_encoding2 = new WeakMap();
|
|
477
544
|
_generateId = new WeakSet();
|
|
478
545
|
generateId_fn = function() {
|
|
479
546
|
return (0, import_human_id.humanId)({
|