@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.
Files changed (56) hide show
  1. package/.turbo/turbo-build.log +49 -49
  2. package/CHANGELOG.md +23 -0
  3. package/LICENSE.md +21 -0
  4. package/README.md +102 -21
  5. package/dist/{chunk-VK7LBMVI.js → chunk-2NA5SEML.js} +2 -2
  6. package/dist/{chunk-PQ5EVEEH.js → chunk-5M33ZF3Y.js} +1 -1
  7. package/dist/{chunk-PQ6CIPFW.js → chunk-6YQ6KDIQ.js} +44 -5
  8. package/dist/{chunk-IQAXBRHU.js → chunk-ASZKHN7Q.js} +53 -29
  9. package/dist/{chunk-RO7TY474.js → chunk-BHY7MNGT.js} +11 -6
  10. package/dist/{chunk-WIFMLPT5.js → chunk-GHY2WEOK.js} +13 -0
  11. package/dist/{chunk-SGXUEFII.js → chunk-KUSVZXDT.js} +2 -2
  12. package/dist/{chunk-36JBKJUN.js → chunk-LZO6K34D.js} +20 -7
  13. package/dist/{chunk-3FL2SNIR.js → chunk-NQVZNVOU.js} +1 -1
  14. package/dist/{chunk-ISRU7PLL.js → chunk-OFH3ML4L.js} +3 -3
  15. package/dist/index.cjs +129 -39
  16. package/dist/index.d.cts +4 -4
  17. package/dist/index.d.ts +4 -4
  18. package/dist/index.js +15 -9
  19. package/dist/lib/client.js +2 -2
  20. package/dist/lib/constants.js +1 -1
  21. package/dist/lib/index.cjs +106 -33
  22. package/dist/lib/index.js +8 -8
  23. package/dist/react/index.cjs +231 -92
  24. package/dist/react/index.d.cts +4 -3
  25. package/dist/react/index.d.ts +4 -3
  26. package/dist/react/index.js +117 -64
  27. package/dist/react/utils.js +2 -2
  28. package/dist/tts/index.cjs +106 -33
  29. package/dist/tts/index.js +6 -6
  30. package/dist/tts/player.cjs +23 -5
  31. package/dist/tts/player.d.cts +6 -0
  32. package/dist/tts/player.d.ts +6 -0
  33. package/dist/tts/player.js +4 -3
  34. package/dist/tts/source.cjs +50 -4
  35. package/dist/tts/source.d.cts +16 -6
  36. package/dist/tts/source.d.ts +16 -6
  37. package/dist/tts/source.js +4 -2
  38. package/dist/tts/utils.cjs +18 -6
  39. package/dist/tts/utils.d.cts +7 -5
  40. package/dist/tts/utils.d.ts +7 -5
  41. package/dist/tts/utils.js +3 -2
  42. package/dist/tts/websocket.cjs +106 -33
  43. package/dist/tts/websocket.d.cts +20 -10
  44. package/dist/tts/websocket.d.ts +20 -10
  45. package/dist/tts/websocket.js +5 -5
  46. package/dist/types/index.d.cts +60 -4
  47. package/dist/types/index.d.ts +60 -4
  48. package/dist/voices/index.js +3 -3
  49. package/package.json +1 -1
  50. package/src/index.ts +2 -0
  51. package/src/react/index.ts +117 -62
  52. package/src/tts/player.ts +15 -8
  53. package/src/tts/source.ts +53 -7
  54. package/src/tts/utils.ts +26 -12
  55. package/src/tts/websocket.ts +42 -19
  56. package/src/types/index.ts +81 -3
@@ -1,15 +1,20 @@
1
+ import {
2
+ ENCODING_MAP
3
+ } from "./chunk-6YQ6KDIQ.js";
4
+
1
5
  // src/tts/utils.ts
2
6
  import base64 from "base64-js";
3
- function base64ToArray(b64) {
7
+ function base64ToArray(b64, encoding) {
4
8
  const byteArrays = filterSentinel(b64).map((b) => base64.toByteArray(b));
9
+ const { arrayType: ArrayType, bytesPerElement } = ENCODING_MAP[encoding];
5
10
  const totalLength = byteArrays.reduce(
6
- (acc, arr) => acc + arr.length / Float32Array.BYTES_PER_ELEMENT,
11
+ (acc, arr) => acc + arr.length / bytesPerElement,
7
12
  0
8
13
  );
9
- const result = new Float32Array(totalLength);
14
+ const result = new ArrayType(totalLength);
10
15
  let offset = 0;
11
16
  for (const arr of byteArrays) {
12
- const floats = new Float32Array(arr.buffer);
17
+ const floats = new ArrayType(arr.buffer);
13
18
  result.set(floats, offset);
14
19
  offset += floats.length;
15
20
  }
@@ -40,10 +45,10 @@ function createMessageHandlerForContextId(contextId, handler) {
40
45
  let chunk;
41
46
  if (message.done) {
42
47
  chunk = getSentinel();
43
- } else {
48
+ } else if (message.type === "chunk") {
44
49
  chunk = message.data;
45
50
  }
46
- handler({ chunk, message: event.data });
51
+ handler({ chunk, message: event.data, data: message });
47
52
  };
48
53
  }
49
54
  function getSentinel() {
@@ -17,6 +17,18 @@ var __spreadValues = (a, b) => {
17
17
  return a;
18
18
  };
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
20
32
  var __accessCheck = (obj, member, msg) => {
21
33
  if (!member.has(obj))
22
34
  throw TypeError("Cannot " + msg);
@@ -63,6 +75,7 @@ var __async = (__this, __arguments, generator) => {
63
75
  export {
64
76
  __spreadValues,
65
77
  __spreadProps,
78
+ __objRest,
66
79
  __privateGet,
67
80
  __privateAdd,
68
81
  __privateSet,
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  Client
3
- } from "./chunk-PQ5EVEEH.js";
3
+ } from "./chunk-5M33ZF3Y.js";
4
4
  import {
5
5
  __async
6
- } from "./chunk-WIFMLPT5.js";
6
+ } from "./chunk-GHY2WEOK.js";
7
7
 
8
8
  // src/voices/index.ts
9
9
  var Voices = class extends Client {
@@ -1,17 +1,16 @@
1
1
  import {
2
2
  playAudioBuffer
3
- } from "./chunk-RO7TY474.js";
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-WIFMLPT5.js";
10
+ } from "./chunk-GHY2WEOK.js";
11
11
 
12
12
  // src/tts/player.ts
13
- import Emittery from "emittery";
14
- var _context, _startNextPlaybackAt, _bufferDuration, _emitter, _playBuffer, playBuffer_fn;
13
+ var _context, _startNextPlaybackAt, _bufferDuration, _playBuffer, playBuffer_fn;
15
14
  var Player = class {
16
15
  /**
17
16
  * Create a new Player.
@@ -24,7 +23,6 @@ var Player = class {
24
23
  __privateAdd(this, _context, null);
25
24
  __privateAdd(this, _startNextPlaybackAt, 0);
26
25
  __privateAdd(this, _bufferDuration, void 0);
27
- __privateAdd(this, _emitter, new Emittery());
28
26
  __privateSet(this, _bufferDuration, bufferDuration);
29
27
  }
30
28
  /**
@@ -46,7 +44,6 @@ var Player = class {
46
44
  const playableAudio = buffer.subarray(0, read);
47
45
  plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
48
46
  if (read < buffer.length) {
49
- yield __privateGet(this, _emitter).emit("finish");
50
47
  break;
51
48
  }
52
49
  }
@@ -96,17 +93,33 @@ var Player = class {
96
93
  }
97
94
  });
98
95
  }
96
+ /**
97
+ * Stop the audio.
98
+ *
99
+ * @returns A promise that resolves when the audio has been stopped.
100
+ */
101
+ stop() {
102
+ return __async(this, null, function* () {
103
+ var _a;
104
+ if (!__privateGet(this, _context)) {
105
+ throw new Error("AudioContext not initialized.");
106
+ }
107
+ yield (_a = __privateGet(this, _context)) == null ? void 0 : _a.close();
108
+ });
109
+ }
99
110
  };
100
111
  _context = new WeakMap();
101
112
  _startNextPlaybackAt = new WeakMap();
102
113
  _bufferDuration = new WeakMap();
103
- _emitter = new WeakMap();
104
114
  _playBuffer = new WeakSet();
105
115
  playBuffer_fn = function(buf, sampleRate) {
106
116
  return __async(this, null, function* () {
107
117
  if (!__privateGet(this, _context)) {
108
118
  throw new Error("AudioContext not initialized.");
109
119
  }
120
+ if (buf.length === 0) {
121
+ return;
122
+ }
110
123
  const startAt = __privateGet(this, _startNextPlaybackAt);
111
124
  const duration = buf.length / sampleRate;
112
125
  __privateSet(this, _startNextPlaybackAt, duration + Math.max(__privateGet(this, _context).currentTime, __privateGet(this, _startNextPlaybackAt)));
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  __async
3
- } from "./chunk-WIFMLPT5.js";
3
+ } from "./chunk-GHY2WEOK.js";
4
4
 
5
5
  // src/react/utils.ts
6
6
  function pingServer(url) {
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  Voices
3
- } from "./chunk-SGXUEFII.js";
3
+ } from "./chunk-KUSVZXDT.js";
4
4
  import {
5
5
  TTS
6
- } from "./chunk-VK7LBMVI.js";
6
+ } from "./chunk-2NA5SEML.js";
7
7
  import {
8
8
  Client
9
- } from "./chunk-PQ5EVEEH.js";
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 _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
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({ sampleRate }) {
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, _buffer, new Float32Array(1024));
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 = new Float32Array(newCapacity);
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 / Float32Array.BYTES_PER_ELEMENT,
304
+ (acc, arr) => acc + arr.length / bytesPerElement,
253
305
  0
254
306
  );
255
- const result = new Float32Array(totalLength);
307
+ const result = new ArrayType(totalLength);
256
308
  let offset = 0;
257
309
  for (const arr of byteArrays) {
258
- const floats = new Float32Array(arr.buffer);
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,37 +385,47 @@ 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
- * Send a message over the WebSocket in order to start a stream.
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
- * If `0`, the stream will not time out.
400
+ * If set to `0`, the stream will not time out.
345
401
  * @returns A Source object that can be passed to a Player to play the audio.
402
+ * @returns An Emittery instance that emits messages from the WebSocket.
403
+ * @returns An abort function that can be called to cancel the stream.
346
404
  */
347
- send(inputs, { timeout = 0 } = {}) {
348
- var _a, _b, _c, _d;
405
+ send(_a, { timeout = 0 } = {}) {
406
+ var inputs = __objRest(_a, []);
407
+ var _a2, _b, _c, _d;
349
408
  if (!__privateGet(this, _isConnected)) {
350
409
  throw new Error("Not connected to WebSocket. Call .connect() first.");
351
410
  }
352
- const contextId = __privateMethod(this, _generateId, generateId_fn).call(this);
353
- (_a = this.socket) == null ? void 0 : _a.send(
354
- JSON.stringify(__spreadProps(__spreadValues({
355
- context_id: contextId
356
- }, inputs), {
357
- output_format: {
358
- container: "raw",
359
- encoding: "pcm_f32le",
360
- sample_rate: __privateGet(this, _sampleRate2)
361
- }
362
- }))
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))
363
423
  );
364
424
  const emitter = new import_emittery2.default();
365
425
  const source = new Source({
366
- sampleRate: __privateGet(this, _sampleRate2)
426
+ sampleRate: __privateGet(this, _sampleRate2),
427
+ encoding: __privateGet(this, _encoding2),
428
+ container: __privateGet(this, _container2)
367
429
  });
368
430
  const streamCompleteController = new AbortController();
369
431
  let timeoutId = null;
@@ -371,19 +433,26 @@ var WebSocket = class extends Client {
371
433
  timeoutId = setTimeout(streamCompleteController.abort, timeout);
372
434
  }
373
435
  const handleMessage = createMessageHandlerForContextId(
374
- contextId,
375
- (_0) => __async(this, [_0], function* ({ chunk, message }) {
436
+ inputs.context_id,
437
+ (_0) => __async(this, [_0], function* ({ chunk, message, data }) {
376
438
  emitter.emit("message", message);
439
+ if (data.type === "timestamps") {
440
+ emitter.emit("timestamps", data.word_timestamps);
441
+ return;
442
+ }
377
443
  if (isSentinel(chunk)) {
378
444
  yield source.close();
379
445
  streamCompleteController.abort();
380
446
  return;
381
447
  }
382
- yield source.enqueue(base64ToArray([chunk]));
383
448
  if (timeoutId) {
384
449
  clearTimeout(timeoutId);
385
450
  timeoutId = setTimeout(streamCompleteController.abort, timeout);
386
451
  }
452
+ if (!chunk) {
453
+ return;
454
+ }
455
+ yield source.enqueue(base64ToArray([chunk], __privateGet(this, _encoding2)));
387
456
  })
388
457
  );
389
458
  (_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
@@ -413,7 +482,11 @@ var WebSocket = class extends Client {
413
482
  clearTimeout(timeoutId);
414
483
  }
415
484
  });
416
- return __spreadValues({ source }, getEmitteryCallbacks(emitter));
485
+ return __spreadProps(__spreadValues({
486
+ source
487
+ }, getEmitteryCallbacks(emitter)), {
488
+ stop: streamCompleteController.abort.bind(streamCompleteController)
489
+ });
417
490
  }
418
491
  /**
419
492
  * Authenticate and connect to a Cartesia streaming WebSocket.
@@ -483,6 +556,8 @@ var WebSocket = class extends Client {
483
556
  };
484
557
  _isConnected = new WeakMap();
485
558
  _sampleRate2 = new WeakMap();
559
+ _container2 = new WeakMap();
560
+ _encoding2 = new WeakMap();
486
561
  _generateId = new WeakSet();
487
562
  generateId_fn = function() {
488
563
  return (0, import_human_id.humanId)({
@@ -564,8 +639,7 @@ var Cartesia = class extends Client {
564
639
  };
565
640
 
566
641
  // src/tts/player.ts
567
- var import_emittery3 = __toESM(require("emittery"), 1);
568
- var _context, _startNextPlaybackAt, _bufferDuration, _emitter2, _playBuffer, playBuffer_fn;
642
+ var _context, _startNextPlaybackAt, _bufferDuration, _playBuffer, playBuffer_fn;
569
643
  var Player = class {
570
644
  /**
571
645
  * Create a new Player.
@@ -578,7 +652,6 @@ var Player = class {
578
652
  __privateAdd(this, _context, null);
579
653
  __privateAdd(this, _startNextPlaybackAt, 0);
580
654
  __privateAdd(this, _bufferDuration, void 0);
581
- __privateAdd(this, _emitter2, new import_emittery3.default());
582
655
  __privateSet(this, _bufferDuration, bufferDuration);
583
656
  }
584
657
  /**
@@ -600,7 +673,6 @@ var Player = class {
600
673
  const playableAudio = buffer.subarray(0, read);
601
674
  plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
602
675
  if (read < buffer.length) {
603
- yield __privateGet(this, _emitter2).emit("finish");
604
676
  break;
605
677
  }
606
678
  }
@@ -650,17 +722,33 @@ var Player = class {
650
722
  }
651
723
  });
652
724
  }
725
+ /**
726
+ * Stop the audio.
727
+ *
728
+ * @returns A promise that resolves when the audio has been stopped.
729
+ */
730
+ stop() {
731
+ return __async(this, null, function* () {
732
+ var _a;
733
+ if (!__privateGet(this, _context)) {
734
+ throw new Error("AudioContext not initialized.");
735
+ }
736
+ yield (_a = __privateGet(this, _context)) == null ? void 0 : _a.close();
737
+ });
738
+ }
653
739
  };
654
740
  _context = new WeakMap();
655
741
  _startNextPlaybackAt = new WeakMap();
656
742
  _bufferDuration = new WeakMap();
657
- _emitter2 = new WeakMap();
658
743
  _playBuffer = new WeakSet();
659
744
  playBuffer_fn = function(buf, sampleRate) {
660
745
  return __async(this, null, function* () {
661
746
  if (!__privateGet(this, _context)) {
662
747
  throw new Error("AudioContext not initialized.");
663
748
  }
749
+ if (buf.length === 0) {
750
+ return;
751
+ }
664
752
  const startAt = __privateGet(this, _startNextPlaybackAt);
665
753
  const duration = buf.length / sampleRate;
666
754
  __privateSet(this, _startNextPlaybackAt, duration + Math.max(__privateGet(this, _context).currentTime, __privateGet(this, _startNextPlaybackAt)));
@@ -670,5 +758,7 @@ playBuffer_fn = function(buf, sampleRate) {
670
758
  // Annotate the CommonJS export names for ESM import in node:
671
759
  0 && (module.exports = {
672
760
  Cartesia,
673
- WebPlayer
761
+ Source,
762
+ WebPlayer,
763
+ WebSocket
674
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 './tts/websocket.cjs';
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 './tts/websocket.js';
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-ISRU7PLL.js";
5
- import "./chunk-SGXUEFII.js";
6
- import "./chunk-VK7LBMVI.js";
7
- import "./chunk-IQAXBRHU.js";
8
- import "./chunk-PQ6CIPFW.js";
9
- import "./chunk-PQ5EVEEH.js";
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-36JBKJUN.js";
14
- import "./chunk-RO7TY474.js";
15
- import "./chunk-WIFMLPT5.js";
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
  };
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  Client
3
- } from "../chunk-PQ5EVEEH.js";
3
+ } from "../chunk-5M33ZF3Y.js";
4
4
  import "../chunk-2BFEKY3F.js";
5
- import "../chunk-WIFMLPT5.js";
5
+ import "../chunk-GHY2WEOK.js";
6
6
  export {
7
7
  Client
8
8
  };
@@ -3,7 +3,7 @@ import {
3
3
  CARTESIA_VERSION,
4
4
  constructApiUrl
5
5
  } from "../chunk-2BFEKY3F.js";
6
- import "../chunk-WIFMLPT5.js";
6
+ import "../chunk-GHY2WEOK.js";
7
7
  export {
8
8
  BASE_URL,
9
9
  CARTESIA_VERSION,