@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
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
|
};
|
package/dist/react/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 });
|
|
@@ -135,7 +147,13 @@ var import_partysocket = require("partysocket");
|
|
|
135
147
|
|
|
136
148
|
// src/tts/source.ts
|
|
137
149
|
var import_emittery = __toESM(require("emittery"), 1);
|
|
138
|
-
var
|
|
150
|
+
var ENCODING_MAP = {
|
|
151
|
+
pcm_f32le: { arrayType: Float32Array, bytesPerElement: 4 },
|
|
152
|
+
pcm_s16le: { arrayType: Int16Array, bytesPerElement: 2 },
|
|
153
|
+
pcm_alaw: { arrayType: Uint8Array, bytesPerElement: 1 },
|
|
154
|
+
pcm_mulaw: { arrayType: Uint8Array, bytesPerElement: 1 }
|
|
155
|
+
};
|
|
156
|
+
var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _createBuffer, createBuffer_fn;
|
|
139
157
|
var Source = class {
|
|
140
158
|
/**
|
|
141
159
|
* Create a new Source.
|
|
@@ -143,23 +161,44 @@ var Source = class {
|
|
|
143
161
|
* @param options - Options for the Source.
|
|
144
162
|
* @param options.sampleRate - The sample rate of the audio.
|
|
145
163
|
*/
|
|
146
|
-
constructor({
|
|
164
|
+
constructor({
|
|
165
|
+
sampleRate,
|
|
166
|
+
encoding,
|
|
167
|
+
container
|
|
168
|
+
}) {
|
|
169
|
+
/**
|
|
170
|
+
* Create a new buffer for the source.
|
|
171
|
+
*
|
|
172
|
+
* @param size - The size of the buffer to create.
|
|
173
|
+
* @returns The new buffer as a TypedArray based on the encoding.
|
|
174
|
+
*/
|
|
175
|
+
__privateAdd(this, _createBuffer);
|
|
147
176
|
__privateAdd(this, _emitter, new import_emittery.default());
|
|
148
177
|
__privateAdd(this, _buffer, void 0);
|
|
149
178
|
__privateAdd(this, _readIndex, 0);
|
|
150
179
|
__privateAdd(this, _writeIndex, 0);
|
|
151
180
|
__privateAdd(this, _closed, false);
|
|
152
181
|
__privateAdd(this, _sampleRate, void 0);
|
|
182
|
+
__privateAdd(this, _encoding, void 0);
|
|
183
|
+
__privateAdd(this, _container, void 0);
|
|
153
184
|
this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
|
|
154
185
|
this.once = __privateGet(this, _emitter).once.bind(__privateGet(this, _emitter));
|
|
155
186
|
this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
|
|
156
187
|
this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
|
|
157
188
|
__privateSet(this, _sampleRate, sampleRate);
|
|
158
|
-
__privateSet(this,
|
|
189
|
+
__privateSet(this, _encoding, encoding);
|
|
190
|
+
__privateSet(this, _container, container);
|
|
191
|
+
__privateSet(this, _buffer, __privateMethod(this, _createBuffer, createBuffer_fn).call(this, 1024));
|
|
159
192
|
}
|
|
160
193
|
get sampleRate() {
|
|
161
194
|
return __privateGet(this, _sampleRate);
|
|
162
195
|
}
|
|
196
|
+
get encoding() {
|
|
197
|
+
return __privateGet(this, _encoding);
|
|
198
|
+
}
|
|
199
|
+
get container() {
|
|
200
|
+
return __privateGet(this, _container);
|
|
201
|
+
}
|
|
163
202
|
/**
|
|
164
203
|
* Append audio to the buffer.
|
|
165
204
|
*
|
|
@@ -173,7 +212,7 @@ var Source = class {
|
|
|
173
212
|
while (newCapacity < requiredCapacity) {
|
|
174
213
|
newCapacity *= 2;
|
|
175
214
|
}
|
|
176
|
-
const newBuffer =
|
|
215
|
+
const newBuffer = __privateMethod(this, _createBuffer, createBuffer_fn).call(this, newCapacity);
|
|
177
216
|
newBuffer.set(__privateGet(this, _buffer));
|
|
178
217
|
__privateSet(this, _buffer, newBuffer);
|
|
179
218
|
}
|
|
@@ -221,6 +260,9 @@ var Source = class {
|
|
|
221
260
|
get readIndex() {
|
|
222
261
|
return __privateGet(this, _readIndex);
|
|
223
262
|
}
|
|
263
|
+
get writeIndex() {
|
|
264
|
+
return __privateGet(this, _writeIndex);
|
|
265
|
+
}
|
|
224
266
|
/**
|
|
225
267
|
* Close the source. This signals that no more audio will be enqueued.
|
|
226
268
|
*
|
|
@@ -242,19 +284,27 @@ _readIndex = new WeakMap();
|
|
|
242
284
|
_writeIndex = new WeakMap();
|
|
243
285
|
_closed = new WeakMap();
|
|
244
286
|
_sampleRate = new WeakMap();
|
|
287
|
+
_encoding = new WeakMap();
|
|
288
|
+
_container = new WeakMap();
|
|
289
|
+
_createBuffer = new WeakSet();
|
|
290
|
+
createBuffer_fn = function(size) {
|
|
291
|
+
const { arrayType: ArrayType } = ENCODING_MAP[__privateGet(this, _encoding)];
|
|
292
|
+
return new ArrayType(size);
|
|
293
|
+
};
|
|
245
294
|
|
|
246
295
|
// src/tts/utils.ts
|
|
247
296
|
var import_base64_js = __toESM(require("base64-js"), 1);
|
|
248
|
-
function base64ToArray(b64) {
|
|
297
|
+
function base64ToArray(b64, encoding) {
|
|
249
298
|
const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
|
|
299
|
+
const { arrayType: ArrayType, bytesPerElement } = ENCODING_MAP[encoding];
|
|
250
300
|
const totalLength = byteArrays.reduce(
|
|
251
|
-
(acc, arr) => acc + arr.length /
|
|
301
|
+
(acc, arr) => acc + arr.length / bytesPerElement,
|
|
252
302
|
0
|
|
253
303
|
);
|
|
254
|
-
const result = new
|
|
304
|
+
const result = new ArrayType(totalLength);
|
|
255
305
|
let offset = 0;
|
|
256
306
|
for (const arr of byteArrays) {
|
|
257
|
-
const floats = new
|
|
307
|
+
const floats = new ArrayType(arr.buffer);
|
|
258
308
|
result.set(floats, offset);
|
|
259
309
|
offset += floats.length;
|
|
260
310
|
}
|
|
@@ -285,10 +335,10 @@ function createMessageHandlerForContextId(contextId, handler) {
|
|
|
285
335
|
let chunk;
|
|
286
336
|
if (message.done) {
|
|
287
337
|
chunk = getSentinel();
|
|
288
|
-
} else {
|
|
338
|
+
} else if (message.type === "chunk") {
|
|
289
339
|
chunk = message.data;
|
|
290
340
|
}
|
|
291
|
-
handler({ chunk, message: event.data });
|
|
341
|
+
handler({ chunk, message: event.data, data: message });
|
|
292
342
|
};
|
|
293
343
|
}
|
|
294
344
|
function getSentinel() {
|
|
@@ -312,14 +362,14 @@ function getEmitteryCallbacks(emitter) {
|
|
|
312
362
|
}
|
|
313
363
|
|
|
314
364
|
// src/tts/websocket.ts
|
|
315
|
-
var _isConnected, _sampleRate2, _generateId, generateId_fn;
|
|
365
|
+
var _isConnected, _sampleRate2, _container2, _encoding2, _generateId, generateId_fn;
|
|
316
366
|
var WebSocket = class extends Client {
|
|
317
367
|
/**
|
|
318
368
|
* Create a new WebSocket client.
|
|
319
369
|
*
|
|
320
370
|
* @param args - Arguments to pass to the Client constructor.
|
|
321
371
|
*/
|
|
322
|
-
constructor({ sampleRate }, ...args) {
|
|
372
|
+
constructor({ sampleRate, container, encoding }, ...args) {
|
|
323
373
|
super(...args);
|
|
324
374
|
/**
|
|
325
375
|
* Generate a unique ID suitable for a streaming context.
|
|
@@ -332,12 +382,16 @@ var WebSocket = class extends Client {
|
|
|
332
382
|
__privateAdd(this, _generateId);
|
|
333
383
|
__privateAdd(this, _isConnected, false);
|
|
334
384
|
__privateAdd(this, _sampleRate2, void 0);
|
|
385
|
+
__privateAdd(this, _container2, void 0);
|
|
386
|
+
__privateAdd(this, _encoding2, void 0);
|
|
335
387
|
__privateSet(this, _sampleRate2, sampleRate);
|
|
388
|
+
__privateSet(this, _container2, container != null ? container : "raw");
|
|
389
|
+
__privateSet(this, _encoding2, encoding != null ? encoding : "pcm_f32le");
|
|
336
390
|
}
|
|
337
391
|
/**
|
|
338
392
|
* Send a message over the WebSocket to start a stream.
|
|
339
393
|
*
|
|
340
|
-
* @param inputs - Stream options.
|
|
394
|
+
* @param inputs - Stream options. Defined in the StreamRequest type.
|
|
341
395
|
* @param options - Options for the stream.
|
|
342
396
|
* @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
|
|
343
397
|
* If set to `0`, the stream will not time out.
|
|
@@ -345,26 +399,30 @@ var WebSocket = class extends Client {
|
|
|
345
399
|
* @returns An Emittery instance that emits messages from the WebSocket.
|
|
346
400
|
* @returns An abort function that can be called to cancel the stream.
|
|
347
401
|
*/
|
|
348
|
-
send(
|
|
349
|
-
var
|
|
402
|
+
send(_a, { timeout = 0 } = {}) {
|
|
403
|
+
var inputs = __objRest(_a, []);
|
|
404
|
+
var _a2, _b, _c, _d;
|
|
350
405
|
if (!__privateGet(this, _isConnected)) {
|
|
351
406
|
throw new Error("Not connected to WebSocket. Call .connect() first.");
|
|
352
407
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
408
|
+
if (!inputs.context_id) {
|
|
409
|
+
inputs.context_id = __privateMethod(this, _generateId, generateId_fn).call(this);
|
|
410
|
+
}
|
|
411
|
+
if (!inputs.output_format) {
|
|
412
|
+
inputs.output_format = {
|
|
413
|
+
container: __privateGet(this, _container2),
|
|
414
|
+
encoding: __privateGet(this, _encoding2),
|
|
415
|
+
sample_rate: __privateGet(this, _sampleRate2)
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
(_a2 = this.socket) == null ? void 0 : _a2.send(
|
|
419
|
+
JSON.stringify(__spreadValues({}, inputs))
|
|
364
420
|
);
|
|
365
421
|
const emitter = new import_emittery2.default();
|
|
366
422
|
const source = new Source({
|
|
367
|
-
sampleRate: __privateGet(this, _sampleRate2)
|
|
423
|
+
sampleRate: __privateGet(this, _sampleRate2),
|
|
424
|
+
encoding: __privateGet(this, _encoding2),
|
|
425
|
+
container: __privateGet(this, _container2)
|
|
368
426
|
});
|
|
369
427
|
const streamCompleteController = new AbortController();
|
|
370
428
|
let timeoutId = null;
|
|
@@ -372,19 +430,26 @@ var WebSocket = class extends Client {
|
|
|
372
430
|
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
373
431
|
}
|
|
374
432
|
const handleMessage = createMessageHandlerForContextId(
|
|
375
|
-
|
|
376
|
-
(_0) => __async(this, [_0], function* ({ chunk, message }) {
|
|
433
|
+
inputs.context_id,
|
|
434
|
+
(_0) => __async(this, [_0], function* ({ chunk, message, data }) {
|
|
377
435
|
emitter.emit("message", message);
|
|
436
|
+
if (data.type === "timestamps") {
|
|
437
|
+
emitter.emit("timestamps", data.word_timestamps);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
378
440
|
if (isSentinel(chunk)) {
|
|
379
441
|
yield source.close();
|
|
380
442
|
streamCompleteController.abort();
|
|
381
443
|
return;
|
|
382
444
|
}
|
|
383
|
-
yield source.enqueue(base64ToArray([chunk]));
|
|
384
445
|
if (timeoutId) {
|
|
385
446
|
clearTimeout(timeoutId);
|
|
386
447
|
timeoutId = setTimeout(streamCompleteController.abort, timeout);
|
|
387
448
|
}
|
|
449
|
+
if (!chunk) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
yield source.enqueue(base64ToArray([chunk], __privateGet(this, _encoding2)));
|
|
388
453
|
})
|
|
389
454
|
);
|
|
390
455
|
(_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
|
|
@@ -488,6 +553,8 @@ var WebSocket = class extends Client {
|
|
|
488
553
|
};
|
|
489
554
|
_isConnected = new WeakMap();
|
|
490
555
|
_sampleRate2 = new WeakMap();
|
|
556
|
+
_container2 = new WeakMap();
|
|
557
|
+
_encoding2 = new WeakMap();
|
|
491
558
|
_generateId = new WeakSet();
|
|
492
559
|
generateId_fn = function() {
|
|
493
560
|
return (0, import_human_id.humanId)({
|
|
@@ -702,7 +769,8 @@ var DEFAULT_BUFFER_DURATION = 0.01;
|
|
|
702
769
|
function useTTS({
|
|
703
770
|
apiKey,
|
|
704
771
|
baseUrl,
|
|
705
|
-
sampleRate
|
|
772
|
+
sampleRate,
|
|
773
|
+
onError
|
|
706
774
|
}) {
|
|
707
775
|
var _a, _b;
|
|
708
776
|
if (typeof window === "undefined") {
|
|
@@ -733,7 +801,11 @@ function useTTS({
|
|
|
733
801
|
}
|
|
734
802
|
const cartesia = new Cartesia({ apiKey, baseUrl });
|
|
735
803
|
baseUrl = baseUrl != null ? baseUrl : cartesia.baseUrl;
|
|
736
|
-
return cartesia.tts.websocket({
|
|
804
|
+
return cartesia.tts.websocket({
|
|
805
|
+
container: "raw",
|
|
806
|
+
encoding: "pcm_f32le",
|
|
807
|
+
sampleRate
|
|
808
|
+
});
|
|
737
809
|
}, [apiKey, baseUrl, sampleRate]);
|
|
738
810
|
const websocketReturn = (0, import_react.useRef)(null);
|
|
739
811
|
const player = (0, import_react.useRef)(null);
|
|
@@ -747,20 +819,32 @@ function useTTS({
|
|
|
747
819
|
(options) => __async(this, null, function* () {
|
|
748
820
|
var _a2, _b2;
|
|
749
821
|
(_a2 = websocketReturn.current) == null ? void 0 : _a2.stop();
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
822
|
+
try {
|
|
823
|
+
setMessages([]);
|
|
824
|
+
setBufferStatus("buffering");
|
|
825
|
+
websocketReturn.current = (_b2 = websocket == null ? void 0 : websocket.send(options)) != null ? _b2 : null;
|
|
826
|
+
if (!websocketReturn.current) {
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
const unsubscribe = websocketReturn.current.on("message", (message) => {
|
|
830
|
+
const parsedMessage = JSON.parse(message);
|
|
831
|
+
setMessages((messages2) => [...messages2, parsedMessage]);
|
|
832
|
+
if (parsedMessage.error) {
|
|
833
|
+
onError == null ? void 0 : onError(new Error(parsedMessage.error));
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
yield websocketReturn.current.source.once("close");
|
|
837
|
+
setBufferStatus("buffered");
|
|
838
|
+
unsubscribe();
|
|
839
|
+
} catch (error) {
|
|
840
|
+
if (error instanceof Error) {
|
|
841
|
+
onError == null ? void 0 : onError(error);
|
|
842
|
+
} else {
|
|
843
|
+
console.error(error);
|
|
844
|
+
}
|
|
755
845
|
}
|
|
756
|
-
const unsubscribe = websocketReturn.current.on("message", (message) => {
|
|
757
|
-
setMessages((messages2) => [...messages2, JSON.parse(message)]);
|
|
758
|
-
});
|
|
759
|
-
yield websocketReturn.current.source.once("close");
|
|
760
|
-
setBufferStatus("buffered");
|
|
761
|
-
unsubscribe();
|
|
762
846
|
}),
|
|
763
|
-
[websocket]
|
|
847
|
+
[websocket, onError]
|
|
764
848
|
);
|
|
765
849
|
const metrics = (0, import_react.useMemo)(() => {
|
|
766
850
|
var _a2;
|
|
@@ -829,56 +913,88 @@ function useTTS({
|
|
|
829
913
|
return () => cleanup == null ? void 0 : cleanup();
|
|
830
914
|
}, [websocket, baseUrl]);
|
|
831
915
|
const play = (0, import_react.useCallback)(() => __async(this, null, function* () {
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
unsubscribe
|
|
916
|
+
try {
|
|
917
|
+
if (playbackStatus === "playing" || !websocketReturn.current) {
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
if (player.current) {
|
|
921
|
+
yield player.current.stop();
|
|
922
|
+
}
|
|
923
|
+
setPlaybackStatus("playing");
|
|
924
|
+
const unsubscribes = [];
|
|
925
|
+
unsubscribes.push(
|
|
926
|
+
websocketReturn.current.source.on("wait", () => {
|
|
927
|
+
setIsWaiting(true);
|
|
928
|
+
})
|
|
929
|
+
);
|
|
930
|
+
unsubscribes.push(
|
|
931
|
+
websocketReturn.current.source.on("read", () => {
|
|
932
|
+
setIsWaiting(false);
|
|
933
|
+
})
|
|
934
|
+
);
|
|
935
|
+
player.current = new Player({
|
|
936
|
+
bufferDuration: bufferDuration != null ? bufferDuration : DEFAULT_BUFFER_DURATION
|
|
937
|
+
});
|
|
938
|
+
yield player.current.play(websocketReturn.current.source);
|
|
939
|
+
for (const unsubscribe of unsubscribes) {
|
|
940
|
+
unsubscribe();
|
|
941
|
+
}
|
|
942
|
+
setPlaybackStatus("finished");
|
|
943
|
+
} catch (error) {
|
|
944
|
+
if (error instanceof Error) {
|
|
945
|
+
onError == null ? void 0 : onError(error);
|
|
946
|
+
} else {
|
|
947
|
+
console.error(error);
|
|
948
|
+
}
|
|
856
949
|
}
|
|
857
|
-
|
|
858
|
-
}), [playbackStatus, bufferDuration]);
|
|
950
|
+
}), [playbackStatus, bufferDuration, onError]);
|
|
859
951
|
const pause = (0, import_react.useCallback)(() => __async(this, null, function* () {
|
|
860
952
|
var _a2;
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
953
|
+
try {
|
|
954
|
+
yield (_a2 = player.current) == null ? void 0 : _a2.pause();
|
|
955
|
+
setPlaybackStatus("paused");
|
|
956
|
+
} catch (error) {
|
|
957
|
+
if (error instanceof Error) {
|
|
958
|
+
onError == null ? void 0 : onError(error);
|
|
959
|
+
} else {
|
|
960
|
+
console.error(error);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
}), [onError]);
|
|
864
964
|
const resume = (0, import_react.useCallback)(() => __async(this, null, function* () {
|
|
865
965
|
var _a2;
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
966
|
+
try {
|
|
967
|
+
yield (_a2 = player.current) == null ? void 0 : _a2.resume();
|
|
968
|
+
setPlaybackStatus("playing");
|
|
969
|
+
} catch (error) {
|
|
970
|
+
if (error instanceof Error) {
|
|
971
|
+
onError == null ? void 0 : onError(error);
|
|
972
|
+
} else {
|
|
973
|
+
console.error(error);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}), [onError]);
|
|
869
977
|
const toggle = (0, import_react.useCallback)(() => __async(this, null, function* () {
|
|
870
978
|
var _a2;
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
979
|
+
try {
|
|
980
|
+
yield (_a2 = player.current) == null ? void 0 : _a2.toggle();
|
|
981
|
+
setPlaybackStatus((status) => {
|
|
982
|
+
if (status === "playing") {
|
|
983
|
+
return "paused";
|
|
984
|
+
}
|
|
985
|
+
if (status === "paused") {
|
|
986
|
+
return "playing";
|
|
987
|
+
}
|
|
988
|
+
return status;
|
|
989
|
+
});
|
|
990
|
+
} catch (error) {
|
|
991
|
+
if (error instanceof Error) {
|
|
992
|
+
onError == null ? void 0 : onError(error);
|
|
993
|
+
} else {
|
|
994
|
+
console.error(error);
|
|
878
995
|
}
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
}), []);
|
|
996
|
+
}
|
|
997
|
+
}), [onError]);
|
|
882
998
|
return {
|
|
883
999
|
buffer,
|
|
884
1000
|
play,
|
package/dist/react/index.d.cts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import Source from '../tts/source.cjs';
|
|
2
|
+
import { StreamRequest } from '../types/index.cjs';
|
|
2
3
|
import 'emittery';
|
|
3
|
-
import '../types/index.cjs';
|
|
4
4
|
|
|
5
5
|
type UseTTSOptions = {
|
|
6
6
|
apiKey: string | null;
|
|
7
7
|
baseUrl?: string;
|
|
8
8
|
sampleRate: number;
|
|
9
|
+
onError?: (error: Error) => void;
|
|
9
10
|
};
|
|
10
11
|
type PlaybackStatus = "inactive" | "playing" | "paused" | "finished";
|
|
11
12
|
type BufferStatus = "inactive" | "buffering" | "buffered";
|
|
@@ -13,7 +14,7 @@ type Metrics = {
|
|
|
13
14
|
modelLatency: number | null;
|
|
14
15
|
};
|
|
15
16
|
interface UseTTSReturn {
|
|
16
|
-
buffer: (options:
|
|
17
|
+
buffer: (options: StreamRequest) => Promise<void>;
|
|
17
18
|
play: (bufferDuration?: number) => Promise<void>;
|
|
18
19
|
pause: () => Promise<void>;
|
|
19
20
|
resume: () => Promise<void>;
|
|
@@ -28,6 +29,6 @@ interface UseTTSReturn {
|
|
|
28
29
|
/**
|
|
29
30
|
* React hook to use the Cartesia audio API.
|
|
30
31
|
*/
|
|
31
|
-
declare function useTTS({ apiKey, baseUrl, sampleRate, }: UseTTSOptions): UseTTSReturn;
|
|
32
|
+
declare function useTTS({ apiKey, baseUrl, sampleRate, onError, }: UseTTSOptions): UseTTSReturn;
|
|
32
33
|
|
|
33
34
|
export { type BufferStatus, type Metrics, type PlaybackStatus, type UseTTSOptions, type UseTTSReturn, useTTS };
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import Source from '../tts/source.js';
|
|
2
|
+
import { StreamRequest } from '../types/index.js';
|
|
2
3
|
import 'emittery';
|
|
3
|
-
import '../types/index.js';
|
|
4
4
|
|
|
5
5
|
type UseTTSOptions = {
|
|
6
6
|
apiKey: string | null;
|
|
7
7
|
baseUrl?: string;
|
|
8
8
|
sampleRate: number;
|
|
9
|
+
onError?: (error: Error) => void;
|
|
9
10
|
};
|
|
10
11
|
type PlaybackStatus = "inactive" | "playing" | "paused" | "finished";
|
|
11
12
|
type BufferStatus = "inactive" | "buffering" | "buffered";
|
|
@@ -13,7 +14,7 @@ type Metrics = {
|
|
|
13
14
|
modelLatency: number | null;
|
|
14
15
|
};
|
|
15
16
|
interface UseTTSReturn {
|
|
16
|
-
buffer: (options:
|
|
17
|
+
buffer: (options: StreamRequest) => Promise<void>;
|
|
17
18
|
play: (bufferDuration?: number) => Promise<void>;
|
|
18
19
|
pause: () => Promise<void>;
|
|
19
20
|
resume: () => Promise<void>;
|
|
@@ -28,6 +29,6 @@ interface UseTTSReturn {
|
|
|
28
29
|
/**
|
|
29
30
|
* React hook to use the Cartesia audio API.
|
|
30
31
|
*/
|
|
31
|
-
declare function useTTS({ apiKey, baseUrl, sampleRate, }: UseTTSOptions): UseTTSReturn;
|
|
32
|
+
declare function useTTS({ apiKey, baseUrl, sampleRate, onError, }: UseTTSOptions): UseTTSReturn;
|
|
32
33
|
|
|
33
34
|
export { type BufferStatus, type Metrics, type PlaybackStatus, type UseTTSOptions, type UseTTSReturn, useTTS };
|