@cartesia/cartesia-js 1.0.3 → 1.2.1-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +32 -3
  3. package/dist/{chunk-BHY7MNGT.js → chunk-5SBAQNWQ.js} +1 -1
  4. package/dist/{chunk-LZO6K34D.js → chunk-CSOXALSC.js} +7 -7
  5. package/dist/{chunk-PISCPZK4.js → chunk-FLWYXP5Z.js} +1 -1
  6. package/dist/chunk-GLZYI5DM.js +43 -0
  7. package/dist/{chunk-6YQ6KDIQ.js → chunk-I5YVYTNK.js} +47 -16
  8. package/dist/{chunk-NQVZNVOU.js → chunk-LKKWJLUG.js} +1 -1
  9. package/dist/{chunk-GHY2WEOK.js → chunk-NJDRWDQ3.js} +8 -35
  10. package/dist/{chunk-ZF6HASZT.js → chunk-QWNB544W.js} +7 -3
  11. package/dist/{chunk-YFN6TFR4.js → chunk-RJICGVPL.js} +12 -4
  12. package/dist/chunk-WLEVU3HN.js +42 -0
  13. package/dist/{chunk-EUW2435M.js → chunk-WSIVWXEI.js} +49 -25
  14. package/dist/index.cjs +162 -76
  15. package/dist/index.d.cts +2 -1
  16. package/dist/index.d.ts +2 -1
  17. package/dist/index.js +10 -9
  18. package/dist/lib/client.js +2 -2
  19. package/dist/lib/constants.js +1 -1
  20. package/dist/lib/index.cjs +157 -71
  21. package/dist/lib/index.d.cts +2 -0
  22. package/dist/lib/index.d.ts +2 -0
  23. package/dist/lib/index.js +9 -8
  24. package/dist/react/index.cjs +165 -76
  25. package/dist/react/index.js +14 -10
  26. package/dist/react/utils.js +2 -2
  27. package/dist/tts/index.cjs +114 -69
  28. package/dist/tts/index.d.cts +8 -1
  29. package/dist/tts/index.d.ts +8 -1
  30. package/dist/tts/index.js +6 -6
  31. package/dist/tts/player.cjs +13 -27
  32. package/dist/tts/player.js +4 -4
  33. package/dist/tts/source.cjs +54 -37
  34. package/dist/tts/source.d.cts +9 -0
  35. package/dist/tts/source.d.ts +9 -0
  36. package/dist/tts/source.js +2 -2
  37. package/dist/tts/utils.js +3 -3
  38. package/dist/tts/websocket.cjs +99 -69
  39. package/dist/tts/websocket.d.cts +15 -9
  40. package/dist/tts/websocket.d.ts +15 -9
  41. package/dist/tts/websocket.js +5 -5
  42. package/dist/types/index.d.cts +42 -3
  43. package/dist/types/index.d.ts +42 -3
  44. package/dist/voice-changer/index.cjs +143 -0
  45. package/dist/voice-changer/index.d.cts +9 -0
  46. package/dist/voice-changer/index.d.ts +9 -0
  47. package/dist/voice-changer/index.js +9 -0
  48. package/dist/voices/index.cjs +10 -2
  49. package/dist/voices/index.d.cts +2 -1
  50. package/dist/voices/index.d.ts +2 -1
  51. package/dist/voices/index.js +3 -3
  52. package/package.json +8 -16
  53. package/src/lib/index.ts +3 -0
  54. package/src/react/index.ts +4 -0
  55. package/src/tts/index.ts +16 -1
  56. package/src/tts/source.ts +35 -0
  57. package/src/tts/websocket.ts +33 -2
  58. package/src/types/index.ts +60 -2
  59. package/src/voice-changer/index.ts +37 -0
  60. package/src/voices/index.ts +12 -3
  61. package/.turbo/turbo-build.log +0 -75
  62. package/dist/chunk-NWCW6C7H.js +0 -25
  63. package/tsconfig.json +0 -3
@@ -9,6 +9,9 @@ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
9
  var __getProtoOf = Object.getPrototypeOf;
10
10
  var __hasOwnProp = Object.prototype.hasOwnProperty;
11
11
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __typeError = (msg) => {
13
+ throw TypeError(msg);
14
+ };
12
15
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
16
  var __spreadValues = (a, b) => {
14
17
  for (var prop in b || (b = {}))
@@ -22,18 +25,6 @@ var __spreadValues = (a, b) => {
22
25
  return a;
23
26
  };
24
27
  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
- };
37
28
  var __export = (target, all) => {
38
29
  for (var name in all)
39
30
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -55,28 +46,11 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
55
46
  mod
56
47
  ));
57
48
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
58
- var __accessCheck = (obj, member, msg) => {
59
- if (!member.has(obj))
60
- throw TypeError("Cannot " + msg);
61
- };
62
- var __privateGet = (obj, member, getter) => {
63
- __accessCheck(obj, member, "read from private field");
64
- return getter ? getter.call(obj) : member.get(obj);
65
- };
66
- var __privateAdd = (obj, member, value) => {
67
- if (member.has(obj))
68
- throw TypeError("Cannot add the same private member more than once");
69
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
70
- };
71
- var __privateSet = (obj, member, value, setter) => {
72
- __accessCheck(obj, member, "write to private field");
73
- setter ? setter.call(obj, value) : member.set(obj, value);
74
- return value;
75
- };
76
- var __privateMethod = (obj, member, method) => {
77
- __accessCheck(obj, member, "access private method");
78
- return method;
79
- };
49
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
50
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
51
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
52
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
53
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
80
54
  var __async = (__this, __arguments, generator) => {
81
55
  return new Promise((resolve, reject) => {
82
56
  var fulfilled = (value) => {
@@ -157,7 +131,7 @@ var ENCODING_MAP = {
157
131
  pcm_alaw: { arrayType: Uint8Array, bytesPerElement: 1 },
158
132
  pcm_mulaw: { arrayType: Uint8Array, bytesPerElement: 1 }
159
133
  };
160
- var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _createBuffer, createBuffer_fn;
134
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _Source_instances, createBuffer_fn;
161
135
  var Source = class {
162
136
  /**
163
137
  * Create a new Source.
@@ -170,21 +144,15 @@ var Source = class {
170
144
  encoding,
171
145
  container
172
146
  }) {
173
- /**
174
- * Create a new buffer for the source.
175
- *
176
- * @param size - The size of the buffer to create.
177
- * @returns The new buffer as a TypedArray based on the encoding.
178
- */
179
- __privateAdd(this, _createBuffer);
147
+ __privateAdd(this, _Source_instances);
180
148
  __privateAdd(this, _emitter, new import_emittery.default());
181
- __privateAdd(this, _buffer, void 0);
149
+ __privateAdd(this, _buffer);
182
150
  __privateAdd(this, _readIndex, 0);
183
151
  __privateAdd(this, _writeIndex, 0);
184
152
  __privateAdd(this, _closed, false);
185
- __privateAdd(this, _sampleRate, void 0);
186
- __privateAdd(this, _encoding, void 0);
187
- __privateAdd(this, _container, void 0);
153
+ __privateAdd(this, _sampleRate);
154
+ __privateAdd(this, _encoding);
155
+ __privateAdd(this, _container);
188
156
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
189
157
  this.once = __privateGet(this, _emitter).once.bind(__privateGet(this, _emitter));
190
158
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
@@ -192,7 +160,7 @@ var Source = class {
192
160
  __privateSet(this, _sampleRate, sampleRate);
193
161
  __privateSet(this, _encoding, encoding);
194
162
  __privateSet(this, _container, container);
195
- __privateSet(this, _buffer, __privateMethod(this, _createBuffer, createBuffer_fn).call(this, 1024));
163
+ __privateSet(this, _buffer, __privateMethod(this, _Source_instances, createBuffer_fn).call(this, 1024));
196
164
  }
197
165
  get sampleRate() {
198
166
  return __privateGet(this, _sampleRate);
@@ -216,7 +184,7 @@ var Source = class {
216
184
  while (newCapacity < requiredCapacity) {
217
185
  newCapacity *= 2;
218
186
  }
219
- const newBuffer = __privateMethod(this, _createBuffer, createBuffer_fn).call(this, newCapacity);
187
+ const newBuffer = __privateMethod(this, _Source_instances, createBuffer_fn).call(this, newCapacity);
220
188
  newBuffer.set(__privateGet(this, _buffer));
221
189
  __privateSet(this, _buffer, newBuffer);
222
190
  }
@@ -249,6 +217,37 @@ var Source = class {
249
217
  return read;
250
218
  });
251
219
  }
220
+ /**
221
+ * Seek in the buffer.
222
+ *
223
+ * @param offset The offset to seek to.
224
+ * @param whence The position to seek from.
225
+ * @returns The new position in the buffer.
226
+ * @throws {Error} If the seek is invalid.
227
+ */
228
+ seek(offset, whence) {
229
+ return __async(this, null, function* () {
230
+ let position = __privateGet(this, _readIndex);
231
+ switch (whence) {
232
+ case "start":
233
+ position = offset;
234
+ break;
235
+ case "current":
236
+ position += offset;
237
+ break;
238
+ case "end":
239
+ position = __privateGet(this, _writeIndex) + offset;
240
+ break;
241
+ default:
242
+ throw new Error(`Invalid seek mode: ${whence}`);
243
+ }
244
+ if (position < 0 || position > __privateGet(this, _writeIndex)) {
245
+ throw new Error("Seek out of bounds");
246
+ }
247
+ __privateSet(this, _readIndex, position);
248
+ return position;
249
+ });
250
+ }
252
251
  /**
253
252
  * Get the number of samples in a given duration.
254
253
  *
@@ -290,7 +289,13 @@ _closed = new WeakMap();
290
289
  _sampleRate = new WeakMap();
291
290
  _encoding = new WeakMap();
292
291
  _container = new WeakMap();
293
- _createBuffer = new WeakSet();
292
+ _Source_instances = new WeakSet();
293
+ /**
294
+ * Create a new buffer for the source.
295
+ *
296
+ * @param size - The size of the buffer to create.
297
+ * @returns The new buffer as a TypedArray based on the encoding.
298
+ */
294
299
  createBuffer_fn = function(size) {
295
300
  const { arrayType: ArrayType } = ENCODING_MAP[__privateGet(this, _encoding)];
296
301
  return new ArrayType(size);
@@ -353,7 +358,7 @@ function getEmitteryCallbacks(emitter) {
353
358
  }
354
359
 
355
360
  // src/tts/websocket.ts
356
- var _isConnected, _sampleRate2, _container2, _encoding2, _generateId, generateId_fn;
361
+ var _isConnected, _sampleRate2, _container2, _encoding2, _WebSocket_instances, generateId_fn;
357
362
  var WebSocket = class extends Client {
358
363
  /**
359
364
  * Create a new WebSocket client.
@@ -362,19 +367,11 @@ var WebSocket = class extends Client {
362
367
  */
363
368
  constructor({ sampleRate, container, encoding }, ...args) {
364
369
  super(...args);
365
- /**
366
- * Generate a unique ID suitable for a streaming context.
367
- *
368
- * Not suitable for security purposes or as a primary key, since
369
- * it lacks the amount of entropy required for those use cases.
370
- *
371
- * @returns A unique ID.
372
- */
373
- __privateAdd(this, _generateId);
370
+ __privateAdd(this, _WebSocket_instances);
374
371
  __privateAdd(this, _isConnected, false);
375
- __privateAdd(this, _sampleRate2, void 0);
376
- __privateAdd(this, _container2, void 0);
377
- __privateAdd(this, _encoding2, void 0);
372
+ __privateAdd(this, _sampleRate2);
373
+ __privateAdd(this, _container2);
374
+ __privateAdd(this, _encoding2);
378
375
  __privateSet(this, _sampleRate2, sampleRate);
379
376
  __privateSet(this, _container2, container != null ? container : "raw");
380
377
  __privateSet(this, _encoding2, encoding != null ? encoding : "pcm_f32le");
@@ -382,7 +379,7 @@ var WebSocket = class extends Client {
382
379
  /**
383
380
  * Send a message over the WebSocket to start a stream.
384
381
  *
385
- * @param inputs - Stream options. Defined in the StreamRequest type.
382
+ * @param inputs - Generation parameters. Defined in the StreamRequest type.
386
383
  * @param options - Options for the stream.
387
384
  * @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
388
385
  * If set to `0`, the stream will not time out.
@@ -390,14 +387,13 @@ var WebSocket = class extends Client {
390
387
  * @returns An Emittery instance that emits messages from the WebSocket.
391
388
  * @returns An abort function that can be called to cancel the stream.
392
389
  */
393
- send(_a, { timeout = 0 } = {}) {
394
- var inputs = __objRest(_a, []);
395
- var _a2, _b, _c, _d;
390
+ send(inputs, { timeout = 0 } = {}) {
391
+ var _a, _b, _c, _d;
396
392
  if (!__privateGet(this, _isConnected)) {
397
393
  throw new Error("Not connected to WebSocket. Call .connect() first.");
398
394
  }
399
395
  if (!inputs.context_id) {
400
- inputs.context_id = __privateMethod(this, _generateId, generateId_fn).call(this);
396
+ inputs.context_id = __privateMethod(this, _WebSocket_instances, generateId_fn).call(this);
401
397
  }
402
398
  if (!inputs.output_format) {
403
399
  inputs.output_format = {
@@ -406,7 +402,7 @@ var WebSocket = class extends Client {
406
402
  sample_rate: __privateGet(this, _sampleRate2)
407
403
  };
408
404
  }
409
- (_a2 = this.socket) == null ? void 0 : _a2.send(
405
+ (_a = this.socket) == null ? void 0 : _a.send(
410
406
  JSON.stringify(__spreadValues({}, inputs))
411
407
  );
412
408
  const emitter = new import_emittery2.default();
@@ -476,6 +472,32 @@ var WebSocket = class extends Client {
476
472
  stop: streamCompleteController.abort.bind(streamCompleteController)
477
473
  });
478
474
  }
475
+ /**
476
+ * Continue a stream.
477
+ *
478
+ * @param inputs - Generation parameters. Defined in the StreamRequest type, but must include a `context_id` field. `continue` is set to true by default.
479
+ */
480
+ continue(inputs) {
481
+ var _a;
482
+ if (!__privateGet(this, _isConnected)) {
483
+ throw new Error("Not connected to WebSocket. Call .connect() first.");
484
+ }
485
+ if (!inputs.context_id) {
486
+ throw new Error("context_id is required to continue a context.");
487
+ }
488
+ if (!inputs.output_format) {
489
+ inputs.output_format = {
490
+ container: __privateGet(this, _container2),
491
+ encoding: __privateGet(this, _encoding2),
492
+ sample_rate: __privateGet(this, _sampleRate2)
493
+ };
494
+ }
495
+ (_a = this.socket) == null ? void 0 : _a.send(
496
+ JSON.stringify(__spreadValues({
497
+ continue: true
498
+ }, inputs))
499
+ );
500
+ }
479
501
  /**
480
502
  * Authenticate and connect to a Cartesia streaming WebSocket.
481
503
  *
@@ -551,7 +573,15 @@ _isConnected = new WeakMap();
551
573
  _sampleRate2 = new WeakMap();
552
574
  _container2 = new WeakMap();
553
575
  _encoding2 = new WeakMap();
554
- _generateId = new WeakSet();
576
+ _WebSocket_instances = new WeakSet();
577
+ /**
578
+ * Generate a unique ID suitable for a streaming context.
579
+ *
580
+ * Not suitable for security purposes or as a primary key, since
581
+ * it lacks the amount of entropy required for those use cases.
582
+ *
583
+ * @returns A unique ID.
584
+ */
555
585
  generateId_fn = function() {
556
586
  return (0, import_human_id.humanId)({
557
587
  separator: "-",
@@ -572,6 +602,53 @@ var TTS = class extends Client {
572
602
  baseUrl: this.baseUrl
573
603
  });
574
604
  }
605
+ /**
606
+ * Generate audio bytes from text.
607
+ *
608
+ * @param options - The options for the request.
609
+ * @returns {Promise<ArrayBuffer>} A promise that resolves to an ArrayBuffer containing the audio bytes.
610
+ */
611
+ bytes(options) {
612
+ return __async(this, null, function* () {
613
+ const response = yield this._fetch("/tts/bytes", {
614
+ method: "POST",
615
+ body: JSON.stringify(options)
616
+ });
617
+ return response.arrayBuffer();
618
+ });
619
+ }
620
+ };
621
+
622
+ // src/voice-changer/index.ts
623
+ var VoiceChanger = class extends Client {
624
+ bytes(options) {
625
+ return __async(this, null, function* () {
626
+ const formData = new FormData();
627
+ formData.append("clip", options.clip);
628
+ formData.append("voice[id]", options.voice.id);
629
+ const fmt = options.output_format;
630
+ formData.append("output_format[container]", fmt.container);
631
+ if ("encoding" in fmt) {
632
+ formData.append("output_format[encoding]", fmt.encoding);
633
+ }
634
+ if ("bit_rate" in fmt) {
635
+ formData.append("output_format[bit_rate]", fmt.bit_rate.toString());
636
+ }
637
+ if ("sample_rate" in fmt) {
638
+ formData.append("output_format[sample_rate]", fmt.sample_rate.toString());
639
+ }
640
+ const response = yield this._fetch("/voice-changer/bytes", {
641
+ method: "POST",
642
+ body: formData
643
+ });
644
+ if (!response.ok) {
645
+ throw new Error(
646
+ `Voice changer error! status: ${response.status}, message: ${yield response.text()}`
647
+ );
648
+ }
649
+ return { buffer: yield response.arrayBuffer() };
650
+ });
651
+ }
575
652
  };
576
653
 
577
654
  // src/voices/index.ts
@@ -625,10 +702,18 @@ var Voices = class extends Client {
625
702
  }
626
703
  mix(options) {
627
704
  return __async(this, null, function* () {
628
- const request = options;
629
705
  const response = yield this._fetch("/voices/mix", {
630
706
  method: "POST",
631
- body: JSON.stringify(request)
707
+ body: JSON.stringify(options)
708
+ });
709
+ return response.json();
710
+ });
711
+ }
712
+ localize(options) {
713
+ return __async(this, null, function* () {
714
+ const response = yield this._fetch("/voices/localize", {
715
+ method: "POST",
716
+ body: JSON.stringify(options)
632
717
  });
633
718
  return response.json();
634
719
  });
@@ -641,6 +726,7 @@ var Cartesia = class extends Client {
641
726
  super(options);
642
727
  this.tts = new TTS(options);
643
728
  this.voices = new Voices(options);
729
+ this.voiceChanger = new VoiceChanger(options);
644
730
  }
645
731
  };
646
732
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,5 +1,6 @@
1
1
  import TTS from '../tts/index.cjs';
2
2
  import { ClientOptions } from '../types/index.cjs';
3
+ import VoiceChanger from '../voice-changer/index.cjs';
3
4
  import Voices from '../voices/index.cjs';
4
5
  import { Client } from './client.cjs';
5
6
  import '../tts/websocket.cjs';
@@ -10,6 +11,7 @@ import '../tts/source.cjs';
10
11
  declare class Cartesia extends Client {
11
12
  tts: TTS;
12
13
  voices: Voices;
14
+ voiceChanger: VoiceChanger;
13
15
  constructor(options?: ClientOptions);
14
16
  }
15
17
 
@@ -1,5 +1,6 @@
1
1
  import TTS from '../tts/index.js';
2
2
  import { ClientOptions } from '../types/index.js';
3
+ import VoiceChanger from '../voice-changer/index.js';
3
4
  import Voices from '../voices/index.js';
4
5
  import { Client } from './client.js';
5
6
  import '../tts/websocket.js';
@@ -10,6 +11,7 @@ import '../tts/source.js';
10
11
  declare class Cartesia extends Client {
11
12
  tts: TTS;
12
13
  voices: Voices;
14
+ voiceChanger: VoiceChanger;
13
15
  constructor(options?: ClientOptions);
14
16
  }
15
17
 
package/dist/lib/index.js CHANGED
@@ -1,14 +1,15 @@
1
1
  import {
2
2
  Cartesia
3
- } from "../chunk-ZF6HASZT.js";
4
- import "../chunk-YFN6TFR4.js";
5
- import "../chunk-NWCW6C7H.js";
6
- import "../chunk-EUW2435M.js";
7
- import "../chunk-PISCPZK4.js";
3
+ } from "../chunk-QWNB544W.js";
4
+ import "../chunk-WLEVU3HN.js";
5
+ import "../chunk-RJICGVPL.js";
6
+ import "../chunk-GLZYI5DM.js";
7
+ import "../chunk-WSIVWXEI.js";
8
+ import "../chunk-FLWYXP5Z.js";
8
9
  import "../chunk-2BFEKY3F.js";
9
- import "../chunk-BHY7MNGT.js";
10
- import "../chunk-6YQ6KDIQ.js";
11
- import "../chunk-GHY2WEOK.js";
10
+ import "../chunk-5SBAQNWQ.js";
11
+ import "../chunk-I5YVYTNK.js";
12
+ import "../chunk-NJDRWDQ3.js";
12
13
  export {
13
14
  Cartesia
14
15
  };