@cartesia/cartesia-js 1.0.0 → 1.0.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 (62) hide show
  1. package/.turbo/turbo-build.log +50 -50
  2. package/CHANGELOG.md +12 -0
  3. package/LICENSE.md +21 -0
  4. package/README.md +92 -19
  5. package/dist/{chunk-PQ6CIPFW.js → chunk-6YQ6KDIQ.js} +44 -5
  6. package/dist/{chunk-RO7TY474.js → chunk-BHY7MNGT.js} +11 -6
  7. package/dist/{chunk-F4QWVJY3.js → chunk-EDAAHENY.js} +2 -2
  8. package/dist/{chunk-WIFMLPT5.js → chunk-GHY2WEOK.js} +13 -0
  9. package/dist/{chunk-FN7BK4PS.js → chunk-IZBPLCGW.js} +97 -75
  10. package/dist/{chunk-JYLAM6VU.js → chunk-LZO6K34D.js} +2 -2
  11. package/dist/{chunk-3FL2SNIR.js → chunk-NQVZNVOU.js} +1 -1
  12. package/dist/{chunk-IEN4NCER.js → chunk-NVOCUUOF.js} +3 -3
  13. package/dist/chunk-PISCPZK4.js +40 -0
  14. package/dist/{chunk-SGXUEFII.js → chunk-UCYL2SOX.js} +18 -15
  15. package/dist/index.cjs +186 -103
  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.cjs +35 -10
  20. package/dist/lib/client.d.cts +2 -2
  21. package/dist/lib/client.d.ts +2 -2
  22. package/dist/lib/client.js +2 -2
  23. package/dist/lib/constants.js +1 -1
  24. package/dist/lib/index.cjs +181 -102
  25. package/dist/lib/index.js +8 -8
  26. package/dist/react/index.cjs +286 -158
  27. package/dist/react/index.d.cts +5 -4
  28. package/dist/react/index.d.ts +5 -4
  29. package/dist/react/index.js +115 -66
  30. package/dist/react/utils.js +2 -2
  31. package/dist/tts/index.cjs +165 -89
  32. package/dist/tts/index.js +6 -6
  33. package/dist/tts/player.cjs +5 -0
  34. package/dist/tts/player.js +4 -3
  35. package/dist/tts/source.cjs +50 -4
  36. package/dist/tts/source.d.cts +16 -6
  37. package/dist/tts/source.d.ts +16 -6
  38. package/dist/tts/source.js +4 -2
  39. package/dist/tts/utils.cjs +18 -6
  40. package/dist/tts/utils.d.cts +7 -5
  41. package/dist/tts/utils.d.ts +7 -5
  42. package/dist/tts/utils.js +3 -2
  43. package/dist/tts/websocket.cjs +165 -89
  44. package/dist/tts/websocket.d.cts +12 -8
  45. package/dist/tts/websocket.d.ts +12 -8
  46. package/dist/tts/websocket.js +5 -5
  47. package/dist/types/index.d.cts +65 -5
  48. package/dist/types/index.d.ts +65 -5
  49. package/dist/voices/index.cjs +31 -23
  50. package/dist/voices/index.d.cts +2 -1
  51. package/dist/voices/index.d.ts +2 -1
  52. package/dist/voices/index.js +3 -3
  53. package/package.json +1 -1
  54. package/src/index.ts +2 -0
  55. package/src/lib/client.ts +10 -10
  56. package/src/react/index.ts +115 -64
  57. package/src/tts/source.ts +53 -7
  58. package/src/tts/utils.ts +26 -12
  59. package/src/tts/websocket.ts +42 -23
  60. package/src/types/index.ts +89 -4
  61. package/src/voices/index.ts +22 -15
  62. package/dist/chunk-PQ5EVEEH.js +0 -34
@@ -1,9 +1,6 @@
1
- import {
2
- Source
3
- } from "./chunk-PQ6CIPFW.js";
4
1
  import {
5
2
  Client
6
- } from "./chunk-PQ5EVEEH.js";
3
+ } from "./chunk-PISCPZK4.js";
7
4
  import {
8
5
  CARTESIA_VERSION,
9
6
  constructApiUrl
@@ -13,29 +10,33 @@ import {
13
10
  createMessageHandlerForContextId,
14
11
  getEmitteryCallbacks,
15
12
  isSentinel
16
- } from "./chunk-RO7TY474.js";
13
+ } from "./chunk-BHY7MNGT.js";
14
+ import {
15
+ Source
16
+ } from "./chunk-6YQ6KDIQ.js";
17
17
  import {
18
18
  __async,
19
+ __objRest,
19
20
  __privateAdd,
20
21
  __privateGet,
21
22
  __privateMethod,
22
23
  __privateSet,
23
24
  __spreadProps,
24
25
  __spreadValues
25
- } from "./chunk-WIFMLPT5.js";
26
+ } from "./chunk-GHY2WEOK.js";
26
27
 
27
28
  // src/tts/websocket.ts
28
29
  import Emittery from "emittery";
29
30
  import { humanId } from "human-id";
30
31
  import { WebSocket as PartySocketWebSocket } from "partysocket";
31
- var _isConnected, _sampleRate, _generateId, generateId_fn;
32
+ var _isConnected, _sampleRate, _container, _encoding, _generateId, generateId_fn;
32
33
  var WebSocket = class extends Client {
33
34
  /**
34
35
  * Create a new WebSocket client.
35
36
  *
36
37
  * @param args - Arguments to pass to the Client constructor.
37
38
  */
38
- constructor({ sampleRate }, ...args) {
39
+ constructor({ sampleRate, container, encoding }, ...args) {
39
40
  super(...args);
40
41
  /**
41
42
  * Generate a unique ID suitable for a streaming context.
@@ -48,12 +49,16 @@ var WebSocket = class extends Client {
48
49
  __privateAdd(this, _generateId);
49
50
  __privateAdd(this, _isConnected, false);
50
51
  __privateAdd(this, _sampleRate, void 0);
52
+ __privateAdd(this, _container, void 0);
53
+ __privateAdd(this, _encoding, void 0);
51
54
  __privateSet(this, _sampleRate, sampleRate);
55
+ __privateSet(this, _container, container != null ? container : "raw");
56
+ __privateSet(this, _encoding, encoding != null ? encoding : "pcm_f32le");
52
57
  }
53
58
  /**
54
59
  * Send a message over the WebSocket to start a stream.
55
60
  *
56
- * @param inputs - Stream options.
61
+ * @param inputs - Stream options. Defined in the StreamRequest type.
57
62
  * @param options - Options for the stream.
58
63
  * @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
59
64
  * If set to `0`, the stream will not time out.
@@ -61,26 +66,30 @@ var WebSocket = class extends Client {
61
66
  * @returns An Emittery instance that emits messages from the WebSocket.
62
67
  * @returns An abort function that can be called to cancel the stream.
63
68
  */
64
- send(inputs, { timeout = 0 } = {}) {
65
- var _a, _b, _c, _d;
69
+ send(_a, { timeout = 0 } = {}) {
70
+ var inputs = __objRest(_a, []);
71
+ var _a2, _b, _c, _d;
66
72
  if (!__privateGet(this, _isConnected)) {
67
73
  throw new Error("Not connected to WebSocket. Call .connect() first.");
68
74
  }
69
- const contextId = __privateMethod(this, _generateId, generateId_fn).call(this);
70
- (_a = this.socket) == null ? void 0 : _a.send(
71
- JSON.stringify(__spreadProps(__spreadValues({
72
- context_id: contextId
73
- }, inputs), {
74
- output_format: {
75
- container: "raw",
76
- encoding: "pcm_f32le",
77
- sample_rate: __privateGet(this, _sampleRate)
78
- }
79
- }))
75
+ if (!inputs.context_id) {
76
+ inputs.context_id = __privateMethod(this, _generateId, generateId_fn).call(this);
77
+ }
78
+ if (!inputs.output_format) {
79
+ inputs.output_format = {
80
+ container: __privateGet(this, _container),
81
+ encoding: __privateGet(this, _encoding),
82
+ sample_rate: __privateGet(this, _sampleRate)
83
+ };
84
+ }
85
+ (_a2 = this.socket) == null ? void 0 : _a2.send(
86
+ JSON.stringify(__spreadValues({}, inputs))
80
87
  );
81
88
  const emitter = new Emittery();
82
89
  const source = new Source({
83
- sampleRate: __privateGet(this, _sampleRate)
90
+ sampleRate: __privateGet(this, _sampleRate),
91
+ encoding: __privateGet(this, _encoding),
92
+ container: __privateGet(this, _container)
84
93
  });
85
94
  const streamCompleteController = new AbortController();
86
95
  let timeoutId = null;
@@ -88,19 +97,26 @@ var WebSocket = class extends Client {
88
97
  timeoutId = setTimeout(streamCompleteController.abort, timeout);
89
98
  }
90
99
  const handleMessage = createMessageHandlerForContextId(
91
- contextId,
92
- (_0) => __async(this, [_0], function* ({ chunk, message }) {
100
+ inputs.context_id,
101
+ (_0) => __async(this, [_0], function* ({ chunk, message, data }) {
93
102
  emitter.emit("message", message);
103
+ if (data.type === "timestamps") {
104
+ emitter.emit("timestamps", data.word_timestamps);
105
+ return;
106
+ }
94
107
  if (isSentinel(chunk)) {
95
108
  yield source.close();
96
109
  streamCompleteController.abort();
97
110
  return;
98
111
  }
99
- yield source.enqueue(base64ToArray([chunk]));
100
112
  if (timeoutId) {
101
113
  clearTimeout(timeoutId);
102
114
  timeoutId = setTimeout(streamCompleteController.abort, timeout);
103
115
  }
116
+ if (!chunk) {
117
+ return;
118
+ }
119
+ yield source.enqueue(base64ToArray([chunk], __privateGet(this, _encoding)));
104
120
  })
105
121
  );
106
122
  (_b = this.socket) == null ? void 0 : _b.addEventListener("message", handleMessage, {
@@ -143,56 +159,60 @@ var WebSocket = class extends Client {
143
159
  * @throws {Error} If the WebSocket fails to connect.
144
160
  */
145
161
  connect() {
146
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
147
- websocket: true
162
+ return __async(this, null, function* () {
163
+ const emitter = new Emittery();
164
+ this.socket = new PartySocketWebSocket(() => __async(this, null, function* () {
165
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
166
+ websocket: true
167
+ });
168
+ url.searchParams.set("api_key", yield this.apiKey());
169
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
170
+ return url.toString();
171
+ }));
172
+ this.socket.onopen = () => {
173
+ __privateSet(this, _isConnected, true);
174
+ emitter.emit("open");
175
+ };
176
+ this.socket.onclose = () => {
177
+ __privateSet(this, _isConnected, false);
178
+ emitter.emit("close");
179
+ };
180
+ return new Promise(
181
+ (resolve, reject) => {
182
+ var _a, _b, _c;
183
+ (_a = this.socket) == null ? void 0 : _a.addEventListener(
184
+ "open",
185
+ () => {
186
+ resolve(getEmitteryCallbacks(emitter));
187
+ },
188
+ {
189
+ once: true
190
+ }
191
+ );
192
+ const aborter = new AbortController();
193
+ (_b = this.socket) == null ? void 0 : _b.addEventListener(
194
+ "error",
195
+ () => {
196
+ aborter.abort();
197
+ reject(new Error("WebSocket failed to connect."));
198
+ },
199
+ {
200
+ signal: aborter.signal
201
+ }
202
+ );
203
+ (_c = this.socket) == null ? void 0 : _c.addEventListener(
204
+ "close",
205
+ () => {
206
+ aborter.abort();
207
+ reject(new Error("WebSocket closed before it could connect."));
208
+ },
209
+ {
210
+ signal: aborter.signal
211
+ }
212
+ );
213
+ }
214
+ );
148
215
  });
149
- url.searchParams.set("api_key", this.apiKey);
150
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
151
- const emitter = new Emittery();
152
- this.socket = new PartySocketWebSocket(url.toString());
153
- this.socket.onopen = () => {
154
- __privateSet(this, _isConnected, true);
155
- emitter.emit("open");
156
- };
157
- this.socket.onclose = () => {
158
- __privateSet(this, _isConnected, false);
159
- emitter.emit("close");
160
- };
161
- return new Promise(
162
- (resolve, reject) => {
163
- var _a, _b, _c;
164
- (_a = this.socket) == null ? void 0 : _a.addEventListener(
165
- "open",
166
- () => {
167
- resolve(getEmitteryCallbacks(emitter));
168
- },
169
- {
170
- once: true
171
- }
172
- );
173
- const aborter = new AbortController();
174
- (_b = this.socket) == null ? void 0 : _b.addEventListener(
175
- "error",
176
- () => {
177
- aborter.abort();
178
- reject(new Error("WebSocket failed to connect."));
179
- },
180
- {
181
- signal: aborter.signal
182
- }
183
- );
184
- (_c = this.socket) == null ? void 0 : _c.addEventListener(
185
- "close",
186
- () => {
187
- aborter.abort();
188
- reject(new Error("WebSocket closed before it could connect."));
189
- },
190
- {
191
- signal: aborter.signal
192
- }
193
- );
194
- }
195
- );
196
216
  }
197
217
  /**
198
218
  * Disconnect from the Cartesia streaming WebSocket.
@@ -204,6 +224,8 @@ var WebSocket = class extends Client {
204
224
  };
205
225
  _isConnected = new WeakMap();
206
226
  _sampleRate = new WeakMap();
227
+ _container = new WeakMap();
228
+ _encoding = new WeakMap();
207
229
  _generateId = new WeakSet();
208
230
  generateId_fn = function() {
209
231
  return humanId({
@@ -1,13 +1,13 @@
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
13
  var _context, _startNextPlaybackAt, _bufferDuration, _playBuffer, playBuffer_fn;
@@ -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-UCYL2SOX.js";
4
4
  import {
5
5
  TTS
6
- } from "./chunk-F4QWVJY3.js";
6
+ } from "./chunk-EDAAHENY.js";
7
7
  import {
8
8
  Client
9
- } from "./chunk-PQ5EVEEH.js";
9
+ } from "./chunk-PISCPZK4.js";
10
10
 
11
11
  // src/lib/index.ts
12
12
  var Cartesia = class extends Client {
@@ -0,0 +1,40 @@
1
+ import {
2
+ BASE_URL,
3
+ CARTESIA_VERSION,
4
+ constructApiUrl
5
+ } from "./chunk-2BFEKY3F.js";
6
+ import {
7
+ __async,
8
+ __spreadProps,
9
+ __spreadValues
10
+ } from "./chunk-GHY2WEOK.js";
11
+
12
+ // src/lib/client.ts
13
+ import fetch from "cross-fetch";
14
+ var Client = class {
15
+ constructor(options = {}) {
16
+ const apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
17
+ if (!apiKey) {
18
+ throw new Error("Missing Cartesia API key.");
19
+ }
20
+ this.apiKey = typeof apiKey === "function" ? apiKey : () => __async(this, null, function* () {
21
+ return apiKey;
22
+ });
23
+ this.baseUrl = options.baseUrl || BASE_URL;
24
+ }
25
+ _fetch(_0) {
26
+ return __async(this, arguments, function* (path, options = {}) {
27
+ const url = constructApiUrl(this.baseUrl, path);
28
+ const headers = new Headers(options.headers);
29
+ headers.set("X-API-Key", yield this.apiKey());
30
+ headers.set("Cartesia-Version", CARTESIA_VERSION);
31
+ return fetch(url.toString(), __spreadProps(__spreadValues({}, options), {
32
+ headers
33
+ }));
34
+ });
35
+ }
36
+ };
37
+
38
+ export {
39
+ Client
40
+ };
@@ -1,48 +1,51 @@
1
1
  import {
2
2
  Client
3
- } from "./chunk-PQ5EVEEH.js";
3
+ } from "./chunk-PISCPZK4.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 {
10
10
  list() {
11
11
  return __async(this, null, function* () {
12
- const response = yield this.fetch("/voices");
12
+ const response = yield this._fetch("/voices");
13
13
  return response.json();
14
14
  });
15
15
  }
16
16
  get(voiceId) {
17
17
  return __async(this, null, function* () {
18
- const response = yield this.fetch(`/voices/${voiceId}`);
18
+ const response = yield this._fetch(`/voices/${voiceId}`);
19
19
  return response.json();
20
20
  });
21
21
  }
22
22
  create(voice) {
23
23
  return __async(this, null, function* () {
24
- const response = yield this.fetch("/voices", {
24
+ const response = yield this._fetch("/voices", {
25
25
  method: "POST",
26
26
  body: JSON.stringify(voice)
27
27
  });
28
28
  return response.json();
29
29
  });
30
30
  }
31
+ update(id, voice) {
32
+ return __async(this, null, function* () {
33
+ const response = yield this._fetch(`/voices/${id}`, {
34
+ method: "PATCH",
35
+ body: JSON.stringify(voice)
36
+ });
37
+ return response.json();
38
+ });
39
+ }
31
40
  clone(options) {
32
41
  return __async(this, null, function* () {
33
- if (options.mode === "url") {
34
- const response = yield this.fetch(
35
- `/voices/clone/url?link=${options.link}`,
36
- {
37
- method: "POST"
38
- }
39
- );
40
- return response.json();
41
- }
42
42
  if (options.mode === "clip") {
43
43
  const formData = new FormData();
44
44
  formData.append("clip", options.clip);
45
- const response = yield this.fetch("/voices/clone/clip", {
45
+ if (options.enhance !== void 0) {
46
+ formData.append("enhance", options.enhance.toString());
47
+ }
48
+ const response = yield this._fetch("/voices/clone/clip", {
46
49
  method: "POST",
47
50
  body: formData
48
51
  });