@cartesia/cartesia-js 1.1.0 → 1.2.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 (52) hide show
  1. package/dist/{chunk-VCZESWYA.js → chunk-5SBAQNWQ.js} +1 -1
  2. package/dist/{chunk-MNOPO7G6.js → chunk-CSOXALSC.js} +7 -7
  3. package/dist/chunk-EYRYHK3X.js +43 -0
  4. package/dist/{chunk-6PWLZAMS.js → chunk-FLWYXP5Z.js} +1 -1
  5. package/dist/{chunk-L3OMQKWL.js → chunk-HQOC2S6C.js} +25 -25
  6. package/dist/{chunk-VVDJR3OA.js → chunk-I5YVYTNK.js} +16 -16
  7. package/dist/{chunk-3FL2SNIR.js → chunk-LKKWJLUG.js} +1 -1
  8. package/dist/{chunk-WIFMLPT5.js → chunk-NJDRWDQ3.js} +8 -22
  9. package/dist/{chunk-CWIJUBG6.js → chunk-RJICGVPL.js} +2 -2
  10. package/dist/chunk-WLEVU3HN.js +42 -0
  11. package/dist/{chunk-4GEDAGVY.js → chunk-YUGIYUMT.js} +8 -4
  12. package/dist/index.cjs +91 -57
  13. package/dist/index.d.cts +2 -1
  14. package/dist/index.d.ts +2 -1
  15. package/dist/index.js +14 -13
  16. package/dist/lib/client.js +2 -2
  17. package/dist/lib/constants.js +1 -1
  18. package/dist/lib/index.cjs +86 -52
  19. package/dist/lib/index.d.cts +2 -0
  20. package/dist/lib/index.d.ts +2 -0
  21. package/dist/lib/index.js +9 -8
  22. package/dist/react/index.cjs +91 -57
  23. package/dist/react/index.js +13 -12
  24. package/dist/react/utils.js +2 -2
  25. package/dist/tts/index.cjs +53 -52
  26. package/dist/tts/index.d.cts +8 -1
  27. package/dist/tts/index.d.ts +8 -1
  28. package/dist/tts/index.js +6 -6
  29. package/dist/tts/player.cjs +13 -27
  30. package/dist/tts/player.js +4 -4
  31. package/dist/tts/source.cjs +23 -37
  32. package/dist/tts/source.js +2 -2
  33. package/dist/tts/utils.js +3 -3
  34. package/dist/tts/websocket.cjs +38 -52
  35. package/dist/tts/websocket.d.cts +6 -6
  36. package/dist/tts/websocket.d.ts +6 -6
  37. package/dist/tts/websocket.js +5 -5
  38. package/dist/types/index.d.cts +25 -1
  39. package/dist/types/index.d.ts +25 -1
  40. package/dist/voice-changer/index.cjs +143 -0
  41. package/dist/voice-changer/index.d.cts +9 -0
  42. package/dist/voice-changer/index.d.ts +9 -0
  43. package/dist/voice-changer/index.js +9 -0
  44. package/dist/voices/index.js +3 -3
  45. package/package.json +10 -17
  46. package/src/lib/index.ts +3 -0
  47. package/src/tts/index.ts +16 -1
  48. package/src/types/index.ts +31 -0
  49. package/src/voice-changer/index.ts +37 -0
  50. package/.turbo/turbo-build.log +0 -75
  51. package/dist/chunk-MUPVAEL7.js +0 -25
  52. package/tsconfig.json +0 -3
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  ENCODING_MAP
3
- } from "./chunk-VVDJR3OA.js";
3
+ } from "./chunk-I5YVYTNK.js";
4
4
 
5
5
  // src/tts/utils.ts
6
6
  import base64 from "base64-js";
@@ -1,16 +1,16 @@
1
1
  import {
2
2
  playAudioBuffer
3
- } from "./chunk-VCZESWYA.js";
3
+ } from "./chunk-5SBAQNWQ.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-NJDRWDQ3.js";
11
11
 
12
12
  // src/tts/player.ts
13
- var _context, _startNextPlaybackAt, _bufferDuration, _playBuffer, playBuffer_fn;
13
+ var _context, _startNextPlaybackAt, _bufferDuration, _Player_instances, playBuffer_fn;
14
14
  var Player = class {
15
15
  /**
16
16
  * Create a new Player.
@@ -19,10 +19,10 @@ var Player = class {
19
19
  * @param options.bufferDuration - The duration of the audio buffer to play.
20
20
  */
21
21
  constructor({ bufferDuration }) {
22
- __privateAdd(this, _playBuffer);
22
+ __privateAdd(this, _Player_instances);
23
23
  __privateAdd(this, _context, null);
24
24
  __privateAdd(this, _startNextPlaybackAt, 0);
25
- __privateAdd(this, _bufferDuration, void 0);
25
+ __privateAdd(this, _bufferDuration);
26
26
  __privateSet(this, _bufferDuration, bufferDuration);
27
27
  }
28
28
  /**
@@ -42,7 +42,7 @@ var Player = class {
42
42
  while (true) {
43
43
  const read = yield source.read(buffer);
44
44
  const playableAudio = buffer.subarray(0, read);
45
- plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
45
+ plays.push(__privateMethod(this, _Player_instances, playBuffer_fn).call(this, playableAudio, source.sampleRate));
46
46
  if (read < buffer.length) {
47
47
  break;
48
48
  }
@@ -111,7 +111,7 @@ var Player = class {
111
111
  _context = new WeakMap();
112
112
  _startNextPlaybackAt = new WeakMap();
113
113
  _bufferDuration = new WeakMap();
114
- _playBuffer = new WeakSet();
114
+ _Player_instances = new WeakSet();
115
115
  playBuffer_fn = function(buf, sampleRate) {
116
116
  return __async(this, null, function* () {
117
117
  if (!__privateGet(this, _context)) {
@@ -0,0 +1,43 @@
1
+ import {
2
+ WebSocket
3
+ } from "./chunk-HQOC2S6C.js";
4
+ import {
5
+ Client
6
+ } from "./chunk-FLWYXP5Z.js";
7
+ import {
8
+ __async
9
+ } from "./chunk-NJDRWDQ3.js";
10
+
11
+ // src/tts/index.ts
12
+ var TTS = class extends Client {
13
+ /**
14
+ * Get a WebSocket client for streaming audio from the TTS API.
15
+ *
16
+ * @returns {WebSocket} A Cartesia WebSocket client.
17
+ */
18
+ websocket(options) {
19
+ return new WebSocket(options, {
20
+ apiKey: this.apiKey,
21
+ baseUrl: this.baseUrl
22
+ });
23
+ }
24
+ /**
25
+ * Generate audio bytes from text.
26
+ *
27
+ * @param options - The options for the request.
28
+ * @returns {Promise<ArrayBuffer>} A promise that resolves to an ArrayBuffer containing the audio bytes.
29
+ */
30
+ bytes(options) {
31
+ return __async(this, null, function* () {
32
+ const response = yield this._fetch("/tts/bytes", {
33
+ method: "POST",
34
+ body: JSON.stringify(options)
35
+ });
36
+ return response.arrayBuffer();
37
+ });
38
+ }
39
+ };
40
+
41
+ export {
42
+ TTS
43
+ };
@@ -7,7 +7,7 @@ import {
7
7
  __async,
8
8
  __spreadProps,
9
9
  __spreadValues
10
- } from "./chunk-WIFMLPT5.js";
10
+ } from "./chunk-NJDRWDQ3.js";
11
11
 
12
12
  // src/lib/client.ts
13
13
  import fetch from "cross-fetch";
@@ -1,19 +1,19 @@
1
- import {
2
- Client
3
- } from "./chunk-6PWLZAMS.js";
4
- import {
5
- CARTESIA_VERSION,
6
- constructApiUrl
7
- } from "./chunk-2BFEKY3F.js";
8
1
  import {
9
2
  base64ToArray,
10
3
  createMessageHandlerForContextId,
11
4
  getEmitteryCallbacks,
12
5
  isSentinel
13
- } from "./chunk-VCZESWYA.js";
6
+ } from "./chunk-5SBAQNWQ.js";
14
7
  import {
15
8
  Source
16
- } from "./chunk-VVDJR3OA.js";
9
+ } from "./chunk-I5YVYTNK.js";
10
+ import {
11
+ Client
12
+ } from "./chunk-FLWYXP5Z.js";
13
+ import {
14
+ CARTESIA_VERSION,
15
+ constructApiUrl
16
+ } from "./chunk-2BFEKY3F.js";
17
17
  import {
18
18
  __async,
19
19
  __privateAdd,
@@ -22,13 +22,13 @@ import {
22
22
  __privateSet,
23
23
  __spreadProps,
24
24
  __spreadValues
25
- } from "./chunk-WIFMLPT5.js";
25
+ } from "./chunk-NJDRWDQ3.js";
26
26
 
27
27
  // src/tts/websocket.ts
28
28
  import Emittery from "emittery";
29
29
  import { humanId } from "human-id";
30
30
  import { WebSocket as PartySocketWebSocket } from "partysocket";
31
- var _isConnected, _sampleRate, _container, _encoding, _generateId, generateId_fn;
31
+ var _isConnected, _sampleRate, _container, _encoding, _WebSocket_instances, generateId_fn;
32
32
  var WebSocket = class extends Client {
33
33
  /**
34
34
  * Create a new WebSocket client.
@@ -37,19 +37,11 @@ var WebSocket = class extends Client {
37
37
  */
38
38
  constructor({ sampleRate, container, encoding }, ...args) {
39
39
  super(...args);
40
- /**
41
- * Generate a unique ID suitable for a streaming context.
42
- *
43
- * Not suitable for security purposes or as a primary key, since
44
- * it lacks the amount of entropy required for those use cases.
45
- *
46
- * @returns A unique ID.
47
- */
48
- __privateAdd(this, _generateId);
40
+ __privateAdd(this, _WebSocket_instances);
49
41
  __privateAdd(this, _isConnected, false);
50
- __privateAdd(this, _sampleRate, void 0);
51
- __privateAdd(this, _container, void 0);
52
- __privateAdd(this, _encoding, void 0);
42
+ __privateAdd(this, _sampleRate);
43
+ __privateAdd(this, _container);
44
+ __privateAdd(this, _encoding);
53
45
  __privateSet(this, _sampleRate, sampleRate);
54
46
  __privateSet(this, _container, container != null ? container : "raw");
55
47
  __privateSet(this, _encoding, encoding != null ? encoding : "pcm_f32le");
@@ -71,7 +63,7 @@ var WebSocket = class extends Client {
71
63
  throw new Error("Not connected to WebSocket. Call .connect() first.");
72
64
  }
73
65
  if (!inputs.context_id) {
74
- inputs.context_id = __privateMethod(this, _generateId, generateId_fn).call(this);
66
+ inputs.context_id = __privateMethod(this, _WebSocket_instances, generateId_fn).call(this);
75
67
  }
76
68
  if (!inputs.output_format) {
77
69
  inputs.output_format = {
@@ -251,7 +243,15 @@ _isConnected = new WeakMap();
251
243
  _sampleRate = new WeakMap();
252
244
  _container = new WeakMap();
253
245
  _encoding = new WeakMap();
254
- _generateId = new WeakSet();
246
+ _WebSocket_instances = new WeakSet();
247
+ /**
248
+ * Generate a unique ID suitable for a streaming context.
249
+ *
250
+ * Not suitable for security purposes or as a primary key, since
251
+ * it lacks the amount of entropy required for those use cases.
252
+ *
253
+ * @returns A unique ID.
254
+ */
255
255
  generateId_fn = function() {
256
256
  return humanId({
257
257
  separator: "-",
@@ -4,7 +4,7 @@ import {
4
4
  __privateGet,
5
5
  __privateMethod,
6
6
  __privateSet
7
- } from "./chunk-WIFMLPT5.js";
7
+ } from "./chunk-NJDRWDQ3.js";
8
8
 
9
9
  // src/tts/source.ts
10
10
  import Emittery from "emittery";
@@ -14,7 +14,7 @@ var ENCODING_MAP = {
14
14
  pcm_alaw: { arrayType: Uint8Array, bytesPerElement: 1 },
15
15
  pcm_mulaw: { arrayType: Uint8Array, bytesPerElement: 1 }
16
16
  };
17
- var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _createBuffer, createBuffer_fn;
17
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate, _encoding, _container, _Source_instances, createBuffer_fn;
18
18
  var Source = class {
19
19
  /**
20
20
  * Create a new Source.
@@ -27,21 +27,15 @@ var Source = class {
27
27
  encoding,
28
28
  container
29
29
  }) {
30
- /**
31
- * Create a new buffer for the source.
32
- *
33
- * @param size - The size of the buffer to create.
34
- * @returns The new buffer as a TypedArray based on the encoding.
35
- */
36
- __privateAdd(this, _createBuffer);
30
+ __privateAdd(this, _Source_instances);
37
31
  __privateAdd(this, _emitter, new Emittery());
38
- __privateAdd(this, _buffer, void 0);
32
+ __privateAdd(this, _buffer);
39
33
  __privateAdd(this, _readIndex, 0);
40
34
  __privateAdd(this, _writeIndex, 0);
41
35
  __privateAdd(this, _closed, false);
42
- __privateAdd(this, _sampleRate, void 0);
43
- __privateAdd(this, _encoding, void 0);
44
- __privateAdd(this, _container, void 0);
36
+ __privateAdd(this, _sampleRate);
37
+ __privateAdd(this, _encoding);
38
+ __privateAdd(this, _container);
45
39
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
46
40
  this.once = __privateGet(this, _emitter).once.bind(__privateGet(this, _emitter));
47
41
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
@@ -49,7 +43,7 @@ var Source = class {
49
43
  __privateSet(this, _sampleRate, sampleRate);
50
44
  __privateSet(this, _encoding, encoding);
51
45
  __privateSet(this, _container, container);
52
- __privateSet(this, _buffer, __privateMethod(this, _createBuffer, createBuffer_fn).call(this, 1024));
46
+ __privateSet(this, _buffer, __privateMethod(this, _Source_instances, createBuffer_fn).call(this, 1024));
53
47
  }
54
48
  get sampleRate() {
55
49
  return __privateGet(this, _sampleRate);
@@ -73,7 +67,7 @@ var Source = class {
73
67
  while (newCapacity < requiredCapacity) {
74
68
  newCapacity *= 2;
75
69
  }
76
- const newBuffer = __privateMethod(this, _createBuffer, createBuffer_fn).call(this, newCapacity);
70
+ const newBuffer = __privateMethod(this, _Source_instances, createBuffer_fn).call(this, newCapacity);
77
71
  newBuffer.set(__privateGet(this, _buffer));
78
72
  __privateSet(this, _buffer, newBuffer);
79
73
  }
@@ -178,7 +172,13 @@ _closed = new WeakMap();
178
172
  _sampleRate = new WeakMap();
179
173
  _encoding = new WeakMap();
180
174
  _container = new WeakMap();
181
- _createBuffer = new WeakSet();
175
+ _Source_instances = new WeakSet();
176
+ /**
177
+ * Create a new buffer for the source.
178
+ *
179
+ * @param size - The size of the buffer to create.
180
+ * @returns The new buffer as a TypedArray based on the encoding.
181
+ */
182
182
  createBuffer_fn = function(size) {
183
183
  const { arrayType: ArrayType } = ENCODING_MAP[__privateGet(this, _encoding)];
184
184
  return new ArrayType(size);
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  __async
3
- } from "./chunk-WIFMLPT5.js";
3
+ } from "./chunk-NJDRWDQ3.js";
4
4
 
5
5
  // src/react/utils.ts
6
6
  function pingServer(url) {
@@ -4,6 +4,9 @@ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
4
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
6
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __typeError = (msg) => {
8
+ throw TypeError(msg);
9
+ };
7
10
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
11
  var __spreadValues = (a, b) => {
9
12
  for (var prop in b || (b = {}))
@@ -17,28 +20,11 @@ var __spreadValues = (a, b) => {
17
20
  return a;
18
21
  };
19
22
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- var __accessCheck = (obj, member, msg) => {
21
- if (!member.has(obj))
22
- throw TypeError("Cannot " + msg);
23
- };
24
- var __privateGet = (obj, member, getter) => {
25
- __accessCheck(obj, member, "read from private field");
26
- return getter ? getter.call(obj) : member.get(obj);
27
- };
28
- var __privateAdd = (obj, member, value) => {
29
- if (member.has(obj))
30
- throw TypeError("Cannot add the same private member more than once");
31
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
32
- };
33
- var __privateSet = (obj, member, value, setter) => {
34
- __accessCheck(obj, member, "write to private field");
35
- setter ? setter.call(obj, value) : member.set(obj, value);
36
- return value;
37
- };
38
- var __privateMethod = (obj, member, method) => {
39
- __accessCheck(obj, member, "access private method");
40
- return method;
41
- };
23
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
24
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
25
+ 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);
26
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
27
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
42
28
  var __async = (__this, __arguments, generator) => {
43
29
  return new Promise((resolve, reject) => {
44
30
  var fulfilled = (value) => {
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  Client
3
- } from "./chunk-6PWLZAMS.js";
3
+ } from "./chunk-FLWYXP5Z.js";
4
4
  import {
5
5
  __async
6
- } from "./chunk-WIFMLPT5.js";
6
+ } from "./chunk-NJDRWDQ3.js";
7
7
 
8
8
  // src/voices/index.ts
9
9
  var Voices = class extends Client {
@@ -0,0 +1,42 @@
1
+ import {
2
+ Client
3
+ } from "./chunk-FLWYXP5Z.js";
4
+ import {
5
+ __async
6
+ } from "./chunk-NJDRWDQ3.js";
7
+
8
+ // src/voice-changer/index.ts
9
+ var VoiceChanger = class extends Client {
10
+ bytes(options) {
11
+ return __async(this, null, function* () {
12
+ const formData = new FormData();
13
+ formData.append("clip", options.clip);
14
+ formData.append("voice[id]", options.voice.id);
15
+ const fmt = options.output_format;
16
+ formData.append("output_format[container]", fmt.container);
17
+ if ("encoding" in fmt) {
18
+ formData.append("output_format[encoding]", fmt.encoding);
19
+ }
20
+ if ("bit_rate" in fmt) {
21
+ formData.append("output_format[bit_rate]", fmt.bit_rate.toString());
22
+ }
23
+ if ("sample_rate" in fmt) {
24
+ formData.append("output_format[sample_rate]", fmt.sample_rate.toString());
25
+ }
26
+ const response = yield this._fetch("/voice-changer/bytes", {
27
+ method: "POST",
28
+ body: formData
29
+ });
30
+ if (!response.ok) {
31
+ throw new Error(
32
+ `Voice changer error! status: ${response.status}, message: ${yield response.text()}`
33
+ );
34
+ }
35
+ return { buffer: yield response.arrayBuffer() };
36
+ });
37
+ }
38
+ };
39
+
40
+ export {
41
+ VoiceChanger
42
+ };
@@ -1,12 +1,15 @@
1
+ import {
2
+ TTS
3
+ } from "./chunk-EYRYHK3X.js";
1
4
  import {
2
5
  Voices
3
- } from "./chunk-CWIJUBG6.js";
6
+ } from "./chunk-RJICGVPL.js";
4
7
  import {
5
- TTS
6
- } from "./chunk-MUPVAEL7.js";
8
+ VoiceChanger
9
+ } from "./chunk-WLEVU3HN.js";
7
10
  import {
8
11
  Client
9
- } from "./chunk-6PWLZAMS.js";
12
+ } from "./chunk-FLWYXP5Z.js";
10
13
 
11
14
  // src/lib/index.ts
12
15
  var Cartesia = class extends Client {
@@ -14,6 +17,7 @@ var Cartesia = class extends Client {
14
17
  super(options);
15
18
  this.tts = new TTS(options);
16
19
  this.voices = new Voices(options);
20
+ this.voiceChanger = new VoiceChanger(options);
17
21
  }
18
22
  };
19
23