@cartesia/cartesia-js 1.0.0-alpha.1 → 1.0.0-alpha.3

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 (44) hide show
  1. package/.turbo/turbo-build.log +26 -26
  2. package/CHANGELOG.md +12 -0
  3. package/dist/chunk-2BFEKY3F.js +16 -0
  4. package/dist/{chunk-WE63M7PJ.js → chunk-36JBKJUN.js} +2 -2
  5. package/dist/{chunk-NDNN326Q.js → chunk-IQAXBRHU.js} +9 -5
  6. package/dist/{chunk-X7SJMF2R.js → chunk-ISRU7PLL.js} +3 -3
  7. package/dist/{chunk-BCQ63627.js → chunk-PQ5EVEEH.js} +4 -2
  8. package/dist/{chunk-JOHSCOLW.js → chunk-PQ6CIPFW.js} +20 -6
  9. package/dist/{chunk-LYPTISWL.js → chunk-RO7TY474.js} +13 -7
  10. package/dist/{chunk-WBK6LLXX.js → chunk-SGXUEFII.js} +1 -1
  11. package/dist/{chunk-4RMSIQLG.js → chunk-VK7LBMVI.js} +2 -2
  12. package/dist/index.cjs +47 -25
  13. package/dist/index.js +9 -9
  14. package/dist/lib/client.cjs +9 -10
  15. package/dist/lib/client.js +2 -2
  16. package/dist/lib/constants.cjs +9 -9
  17. package/dist/lib/constants.d.cts +15 -3
  18. package/dist/lib/constants.d.ts +15 -3
  19. package/dist/lib/constants.js +3 -1
  20. package/dist/lib/index.cjs +46 -24
  21. package/dist/lib/index.js +8 -8
  22. package/dist/react/index.cjs +63 -33
  23. package/dist/react/index.js +25 -17
  24. package/dist/tts/index.cjs +46 -24
  25. package/dist/tts/index.js +6 -6
  26. package/dist/tts/player.cjs +1 -1
  27. package/dist/tts/player.js +2 -2
  28. package/dist/tts/source.cjs +20 -6
  29. package/dist/tts/source.js +1 -1
  30. package/dist/tts/utils.cjs +13 -7
  31. package/dist/tts/utils.js +1 -1
  32. package/dist/tts/websocket.cjs +46 -24
  33. package/dist/tts/websocket.js +5 -5
  34. package/dist/voices/index.cjs +9 -10
  35. package/dist/voices/index.js +3 -3
  36. package/package.json +1 -1
  37. package/src/lib/client.ts +3 -2
  38. package/src/lib/constants.ts +18 -9
  39. package/src/react/index.ts +20 -8
  40. package/src/tts/player.ts +2 -1
  41. package/src/tts/source.ts +22 -5
  42. package/src/tts/utils.ts +15 -7
  43. package/src/tts/websocket.ts +5 -2
  44. package/dist/chunk-3GBZUGUD.js +0 -17
@@ -8,47 +8,47 @@ $ tsup src/ --format cjs,esm --dts
8
8
  ESM dist/index.js 437.00 B
9
9
  ESM dist/chunk-FXPGR372.js 0 B
10
10
  ESM dist/lib/client.js 132.00 B
11
- ESM dist/react/index.js 6.00 KB
11
+ ESM dist/react/index.js 6.22 KB
12
12
  ESM dist/tts/index.js 261.00 B
13
- ESM dist/lib/constants.js 143.00 B
13
+ ESM dist/lib/constants.js 183.00 B
14
14
  ESM dist/lib/index.js 322.00 B
15
- ESM dist/chunk-X7SJMF2R.js 353.00 B
16
- ESM dist/chunk-WBK6LLXX.js 1.35 KB
15
+ ESM dist/chunk-ISRU7PLL.js 353.00 B
16
+ ESM dist/chunk-SGXUEFII.js 1.35 KB
17
17
  ESM dist/react/utils.js 109.00 B
18
18
  ESM dist/chunk-3FL2SNIR.js 337.00 B
19
- ESM dist/chunk-4RMSIQLG.js 439.00 B
20
- ESM dist/chunk-NDNN326Q.js 5.73 KB
21
- ESM dist/chunk-JOHSCOLW.js 3.39 KB
22
- ESM dist/chunk-BCQ63627.js 775.00 B
23
- ESM dist/chunk-3GBZUGUD.js 487.00 B
19
+ ESM dist/chunk-VK7LBMVI.js 439.00 B
20
+ ESM dist/chunk-IQAXBRHU.js 5.84 KB
21
+ ESM dist/chunk-PQ6CIPFW.js 4.02 KB
22
+ ESM dist/chunk-PQ5EVEEH.js 841.00 B
23
+ ESM dist/chunk-2BFEKY3F.js 366.00 B
24
24
  ESM dist/tts/player.js 143.00 B
25
- ESM dist/chunk-WE63M7PJ.js 3.52 KB
26
- ESM dist/chunk-LYPTISWL.js 1.81 KB
25
+ ESM dist/chunk-36JBKJUN.js 3.52 KB
26
+ ESM dist/chunk-RO7TY474.js 1.95 KB
27
27
  ESM dist/chunk-WIFMLPT5.js 2.27 KB
28
28
  ESM dist/types/index.js 31.00 B
29
29
  ESM dist/voices/index.js 174.00 B
30
30
  ESM dist/tts/source.js 112.00 B
31
31
  ESM dist/tts/utils.js 395.00 B
32
32
  ESM dist/tts/websocket.js 242.00 B
33
- ESM ⚡️ Build success in 115ms
34
- CJS dist/index.cjs 20.24 KB
35
- CJS dist/lib/client.cjs 3.43 KB
36
- CJS dist/lib/constants.cjs 1.51 KB
37
- CJS dist/lib/index.cjs 16.42 KB
38
- CJS dist/react/index.cjs 26.25 KB
33
+ ESM ⚡️ Build success in 97ms
34
+ CJS dist/index.cjs 21.01 KB
35
+ CJS dist/lib/client.cjs 3.34 KB
36
+ CJS dist/lib/constants.cjs 1.43 KB
37
+ CJS dist/lib/index.cjs 17.18 KB
38
+ CJS dist/react/index.cjs 27.24 KB
39
39
  CJS dist/react/utils.cjs 1.80 KB
40
- CJS dist/tts/index.cjs 14.89 KB
40
+ CJS dist/tts/index.cjs 15.66 KB
41
41
  CJS dist/tts/player.cjs 6.66 KB
42
- CJS dist/tts/source.cjs 5.99 KB
43
- CJS dist/tts/utils.cjs 3.73 KB
44
- CJS dist/tts/websocket.cjs 14.58 KB
42
+ CJS dist/tts/source.cjs 6.63 KB
43
+ CJS dist/tts/utils.cjs 3.87 KB
44
+ CJS dist/tts/websocket.cjs 15.34 KB
45
45
  CJS dist/types/index.cjs 764.00 B
46
- CJS dist/voices/index.cjs 5.13 KB
47
- CJS ⚡️ Build success in 117ms
46
+ CJS dist/voices/index.cjs 5.04 KB
47
+ CJS ⚡️ Build success in 101ms
48
48
  DTS Build start
49
- DTS ⚡️ Build success in 8423ms
49
+ DTS ⚡️ Build success in 6609ms
50
50
  DTS dist/index.d.cts 509.00 B
51
- DTS dist/lib/constants.d.cts 184.00 B
51
+ DTS dist/lib/constants.d.cts 564.00 B
52
52
  DTS dist/lib/index.d.cts 410.00 B
53
53
  DTS dist/react/index.d.cts 1018.00 B
54
54
  DTS dist/react/utils.d.cts 240.00 B
@@ -61,7 +61,7 @@ $ tsup src/ --format cjs,esm --dts
61
61
  DTS dist/lib/client.d.cts 267.00 B
62
62
  DTS dist/types/index.d.cts 1.28 KB
63
63
  DTS dist/index.d.ts 501.00 B
64
- DTS dist/lib/constants.d.ts 184.00 B
64
+ DTS dist/lib/constants.d.ts 564.00 B
65
65
  DTS dist/lib/index.d.ts 404.00 B
66
66
  DTS dist/react/index.d.ts 1016.00 B
67
67
  DTS dist/react/utils.d.ts 240.00 B
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @cartesia/cartesia-js
2
2
 
3
+ ## 1.0.0-alpha.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 38af01f: Fix how URLs are constructed, solving WebSocket connection failure
8
+
9
+ ## 1.0.0-alpha.2
10
+
11
+ ### Patch Changes
12
+
13
+ - 585d2c9: Makes JS client compatible with the Cartesia Stable API (2024-06-10)
14
+
3
15
  ## 1.0.0-alpha.1
4
16
 
5
17
  ### Major Changes
@@ -0,0 +1,16 @@
1
+ // src/lib/constants.ts
2
+ var BASE_URL = "https://api.cartesia.ai";
3
+ var CARTESIA_VERSION = "2024-06-10";
4
+ var constructApiUrl = (baseUrl, path, { websocket = false } = {}) => {
5
+ const url = new URL(path, baseUrl);
6
+ if (websocket) {
7
+ url.protocol = baseUrl.replace(/^http/, "ws");
8
+ }
9
+ return url;
10
+ };
11
+
12
+ export {
13
+ BASE_URL,
14
+ CARTESIA_VERSION,
15
+ constructApiUrl
16
+ };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  playAudioBuffer
3
- } from "./chunk-LYPTISWL.js";
3
+ } from "./chunk-RO7TY474.js";
4
4
  import {
5
5
  __async,
6
6
  __privateAdd,
@@ -43,7 +43,7 @@ var Player = class {
43
43
  const plays = [];
44
44
  while (true) {
45
45
  const read = yield source.read(buffer);
46
- const playableAudio = buffer.slice(0, read);
46
+ const playableAudio = buffer.subarray(0, read);
47
47
  plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
48
48
  if (read < buffer.length) {
49
49
  yield __privateGet(this, _emitter).emit("finish");
@@ -1,18 +1,19 @@
1
1
  import {
2
2
  Source
3
- } from "./chunk-JOHSCOLW.js";
3
+ } from "./chunk-PQ6CIPFW.js";
4
4
  import {
5
5
  Client
6
- } from "./chunk-BCQ63627.js";
6
+ } from "./chunk-PQ5EVEEH.js";
7
7
  import {
8
+ CARTESIA_VERSION,
8
9
  constructApiUrl
9
- } from "./chunk-3GBZUGUD.js";
10
+ } from "./chunk-2BFEKY3F.js";
10
11
  import {
11
12
  base64ToArray,
12
13
  createMessageHandlerForContextId,
13
14
  getEmitteryCallbacks,
14
15
  isSentinel
15
- } from "./chunk-LYPTISWL.js";
16
+ } from "./chunk-RO7TY474.js";
16
17
  import {
17
18
  __async,
18
19
  __privateAdd,
@@ -136,8 +137,11 @@ var WebSocket = class extends Client {
136
137
  * @throws {Error} If the WebSocket fails to connect.
137
138
  */
138
139
  connect() {
139
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", "ws");
140
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
141
+ websocket: true
142
+ });
140
143
  url.searchParams.set("api_key", this.apiKey);
144
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
141
145
  const emitter = new Emittery();
142
146
  this.socket = new PartySocketWebSocket(url.toString());
143
147
  this.socket.onopen = () => {
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  Voices
3
- } from "./chunk-WBK6LLXX.js";
3
+ } from "./chunk-SGXUEFII.js";
4
4
  import {
5
5
  TTS
6
- } from "./chunk-4RMSIQLG.js";
6
+ } from "./chunk-VK7LBMVI.js";
7
7
  import {
8
8
  Client
9
- } from "./chunk-BCQ63627.js";
9
+ } from "./chunk-PQ5EVEEH.js";
10
10
 
11
11
  // src/lib/index.ts
12
12
  var Cartesia = class extends Client {
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  BASE_URL,
3
+ CARTESIA_VERSION,
3
4
  constructApiUrl
4
- } from "./chunk-3GBZUGUD.js";
5
+ } from "./chunk-2BFEKY3F.js";
5
6
  import {
6
7
  __spreadProps,
7
8
  __spreadValues
@@ -21,7 +22,8 @@ var Client = class {
21
22
  const url = constructApiUrl(this.baseUrl, path);
22
23
  return fetch(url.toString(), __spreadProps(__spreadValues({}, options), {
23
24
  headers: __spreadValues({
24
- "X-API-KEY": this.apiKey
25
+ "X-API-Key": this.apiKey,
26
+ "Cartesia-Version": CARTESIA_VERSION
25
27
  }, options.headers)
26
28
  }));
27
29
  }
@@ -7,7 +7,7 @@ import {
7
7
 
8
8
  // src/tts/source.ts
9
9
  import Emittery from "emittery";
10
- var _emitter, _buffer, _readIndex, _closed, _sampleRate;
10
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
11
11
  var Source = class {
12
12
  /**
13
13
  * Create a new Source.
@@ -17,8 +17,9 @@ var Source = class {
17
17
  */
18
18
  constructor({ sampleRate }) {
19
19
  __privateAdd(this, _emitter, new Emittery());
20
- __privateAdd(this, _buffer, new Float32Array());
20
+ __privateAdd(this, _buffer, void 0);
21
21
  __privateAdd(this, _readIndex, 0);
22
+ __privateAdd(this, _writeIndex, 0);
22
23
  __privateAdd(this, _closed, false);
23
24
  __privateAdd(this, _sampleRate, void 0);
24
25
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
@@ -26,6 +27,7 @@ var Source = class {
26
27
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
27
28
  this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
28
29
  __privateSet(this, _sampleRate, sampleRate);
30
+ __privateSet(this, _buffer, new Float32Array(1024));
29
31
  }
30
32
  get sampleRate() {
31
33
  return __privateGet(this, _sampleRate);
@@ -37,7 +39,18 @@ var Source = class {
37
39
  */
38
40
  enqueue(src) {
39
41
  return __async(this, null, function* () {
40
- __privateSet(this, _buffer, new Float32Array([...__privateGet(this, _buffer), ...src]));
42
+ const requiredCapacity = __privateGet(this, _writeIndex) + src.length;
43
+ if (requiredCapacity > __privateGet(this, _buffer).length) {
44
+ let newCapacity = __privateGet(this, _buffer).length;
45
+ while (newCapacity < requiredCapacity) {
46
+ newCapacity *= 2;
47
+ }
48
+ const newBuffer = new Float32Array(newCapacity);
49
+ newBuffer.set(__privateGet(this, _buffer));
50
+ __privateSet(this, _buffer, newBuffer);
51
+ }
52
+ __privateGet(this, _buffer).set(src, __privateGet(this, _writeIndex));
53
+ __privateSet(this, _writeIndex, __privateGet(this, _writeIndex) + src.length);
41
54
  yield __privateGet(this, _emitter).emit("enqueue");
42
55
  });
43
56
  }
@@ -51,7 +64,7 @@ var Source = class {
51
64
  read(dst) {
52
65
  return __async(this, null, function* () {
53
66
  const targetReadIndex = __privateGet(this, _readIndex) + dst.length;
54
- while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _buffer).length) {
67
+ while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _writeIndex)) {
55
68
  yield __privateGet(this, _emitter).emit("wait");
56
69
  yield Promise.race([
57
70
  __privateGet(this, _emitter).once("enqueue"),
@@ -59,8 +72,8 @@ var Source = class {
59
72
  ]);
60
73
  yield __privateGet(this, _emitter).emit("read");
61
74
  }
62
- const read = Math.min(dst.length, __privateGet(this, _buffer).length - __privateGet(this, _readIndex));
63
- dst.set(__privateGet(this, _buffer).slice(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
75
+ const read = Math.min(dst.length, __privateGet(this, _writeIndex) - __privateGet(this, _readIndex));
76
+ dst.set(__privateGet(this, _buffer).subarray(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
64
77
  __privateSet(this, _readIndex, __privateGet(this, _readIndex) + read);
65
78
  return read;
66
79
  });
@@ -98,6 +111,7 @@ var Source = class {
98
111
  _emitter = new WeakMap();
99
112
  _buffer = new WeakMap();
100
113
  _readIndex = new WeakMap();
114
+ _writeIndex = new WeakMap();
101
115
  _closed = new WeakMap();
102
116
  _sampleRate = new WeakMap();
103
117
 
@@ -1,13 +1,19 @@
1
1
  // src/tts/utils.ts
2
2
  import base64 from "base64-js";
3
3
  function base64ToArray(b64) {
4
- return filterSentinel(b64).reduce((acc, b) => {
5
- const floats = new Float32Array(base64.toByteArray(b).buffer);
6
- const newAcc = new Float32Array(acc.length + floats.length);
7
- newAcc.set(acc, 0);
8
- newAcc.set(floats, acc.length);
9
- return newAcc;
10
- }, new Float32Array(0));
4
+ const byteArrays = filterSentinel(b64).map((b) => base64.toByteArray(b));
5
+ const totalLength = byteArrays.reduce(
6
+ (acc, arr) => acc + arr.length / Float32Array.BYTES_PER_ELEMENT,
7
+ 0
8
+ );
9
+ const result = new Float32Array(totalLength);
10
+ let offset = 0;
11
+ for (const arr of byteArrays) {
12
+ const floats = new Float32Array(arr.buffer);
13
+ result.set(floats, offset);
14
+ offset += floats.length;
15
+ }
16
+ return result;
11
17
  }
12
18
  function playAudioBuffer(floats, context, startAt, sampleRate) {
13
19
  const source = context.createBufferSource();
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Client
3
- } from "./chunk-BCQ63627.js";
3
+ } from "./chunk-PQ5EVEEH.js";
4
4
  import {
5
5
  __async
6
6
  } from "./chunk-WIFMLPT5.js";
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  WebSocket
3
- } from "./chunk-NDNN326Q.js";
3
+ } from "./chunk-IQAXBRHU.js";
4
4
  import {
5
5
  Client
6
- } from "./chunk-BCQ63627.js";
6
+ } from "./chunk-PQ5EVEEH.js";
7
7
 
8
8
  // src/tts/index.ts
9
9
  var TTS = class extends Client {
package/dist/index.cjs CHANGED
@@ -99,16 +99,14 @@ module.exports = __toCommonJS(src_exports);
99
99
  var import_cross_fetch = __toESM(require("cross-fetch"), 1);
100
100
 
101
101
  // src/lib/constants.ts
102
- var BASE_URL = "https://api.cartesia.ai/v0";
103
- var constructApiUrl = (baseUrl, path, protocol) => {
104
- const normalizedPath = path.startsWith("/") ? path : `/${path}`;
105
- if (!protocol) {
106
- return new URL(`${baseUrl}${normalizedPath}`);
102
+ var BASE_URL = "https://api.cartesia.ai";
103
+ var CARTESIA_VERSION = "2024-06-10";
104
+ var constructApiUrl = (baseUrl, path, { websocket = false } = {}) => {
105
+ const url = new URL(path, baseUrl);
106
+ if (websocket) {
107
+ url.protocol = baseUrl.replace(/^http/, "ws");
107
108
  }
108
- if (!["http", "ws"].includes(protocol)) {
109
- throw new Error(`Invalid protocol: ${protocol}`);
110
- }
111
- return new URL(`${baseUrl.replace(/^http/, protocol)}${normalizedPath}`);
109
+ return url;
112
110
  };
113
111
 
114
112
  // src/lib/client.ts
@@ -124,7 +122,8 @@ var Client = class {
124
122
  const url = constructApiUrl(this.baseUrl, path);
125
123
  return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
126
124
  headers: __spreadValues({
127
- "X-API-KEY": this.apiKey
125
+ "X-API-Key": this.apiKey,
126
+ "Cartesia-Version": CARTESIA_VERSION
128
127
  }, options.headers)
129
128
  }));
130
129
  }
@@ -137,7 +136,7 @@ var import_partysocket = require("partysocket");
137
136
 
138
137
  // src/tts/source.ts
139
138
  var import_emittery = __toESM(require("emittery"), 1);
140
- var _emitter, _buffer, _readIndex, _closed, _sampleRate;
139
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
141
140
  var Source = class {
142
141
  /**
143
142
  * Create a new Source.
@@ -147,8 +146,9 @@ var Source = class {
147
146
  */
148
147
  constructor({ sampleRate }) {
149
148
  __privateAdd(this, _emitter, new import_emittery.default());
150
- __privateAdd(this, _buffer, new Float32Array());
149
+ __privateAdd(this, _buffer, void 0);
151
150
  __privateAdd(this, _readIndex, 0);
151
+ __privateAdd(this, _writeIndex, 0);
152
152
  __privateAdd(this, _closed, false);
153
153
  __privateAdd(this, _sampleRate, void 0);
154
154
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
@@ -156,6 +156,7 @@ var Source = class {
156
156
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
157
157
  this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
158
158
  __privateSet(this, _sampleRate, sampleRate);
159
+ __privateSet(this, _buffer, new Float32Array(1024));
159
160
  }
160
161
  get sampleRate() {
161
162
  return __privateGet(this, _sampleRate);
@@ -167,7 +168,18 @@ var Source = class {
167
168
  */
168
169
  enqueue(src) {
169
170
  return __async(this, null, function* () {
170
- __privateSet(this, _buffer, new Float32Array([...__privateGet(this, _buffer), ...src]));
171
+ const requiredCapacity = __privateGet(this, _writeIndex) + src.length;
172
+ if (requiredCapacity > __privateGet(this, _buffer).length) {
173
+ let newCapacity = __privateGet(this, _buffer).length;
174
+ while (newCapacity < requiredCapacity) {
175
+ newCapacity *= 2;
176
+ }
177
+ const newBuffer = new Float32Array(newCapacity);
178
+ newBuffer.set(__privateGet(this, _buffer));
179
+ __privateSet(this, _buffer, newBuffer);
180
+ }
181
+ __privateGet(this, _buffer).set(src, __privateGet(this, _writeIndex));
182
+ __privateSet(this, _writeIndex, __privateGet(this, _writeIndex) + src.length);
171
183
  yield __privateGet(this, _emitter).emit("enqueue");
172
184
  });
173
185
  }
@@ -181,7 +193,7 @@ var Source = class {
181
193
  read(dst) {
182
194
  return __async(this, null, function* () {
183
195
  const targetReadIndex = __privateGet(this, _readIndex) + dst.length;
184
- while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _buffer).length) {
196
+ while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _writeIndex)) {
185
197
  yield __privateGet(this, _emitter).emit("wait");
186
198
  yield Promise.race([
187
199
  __privateGet(this, _emitter).once("enqueue"),
@@ -189,8 +201,8 @@ var Source = class {
189
201
  ]);
190
202
  yield __privateGet(this, _emitter).emit("read");
191
203
  }
192
- const read = Math.min(dst.length, __privateGet(this, _buffer).length - __privateGet(this, _readIndex));
193
- dst.set(__privateGet(this, _buffer).slice(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
204
+ const read = Math.min(dst.length, __privateGet(this, _writeIndex) - __privateGet(this, _readIndex));
205
+ dst.set(__privateGet(this, _buffer).subarray(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
194
206
  __privateSet(this, _readIndex, __privateGet(this, _readIndex) + read);
195
207
  return read;
196
208
  });
@@ -228,19 +240,26 @@ var Source = class {
228
240
  _emitter = new WeakMap();
229
241
  _buffer = new WeakMap();
230
242
  _readIndex = new WeakMap();
243
+ _writeIndex = new WeakMap();
231
244
  _closed = new WeakMap();
232
245
  _sampleRate = new WeakMap();
233
246
 
234
247
  // src/tts/utils.ts
235
248
  var import_base64_js = __toESM(require("base64-js"), 1);
236
249
  function base64ToArray(b64) {
237
- return filterSentinel(b64).reduce((acc, b) => {
238
- const floats = new Float32Array(import_base64_js.default.toByteArray(b).buffer);
239
- const newAcc = new Float32Array(acc.length + floats.length);
240
- newAcc.set(acc, 0);
241
- newAcc.set(floats, acc.length);
242
- return newAcc;
243
- }, new Float32Array(0));
250
+ const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
251
+ const totalLength = byteArrays.reduce(
252
+ (acc, arr) => acc + arr.length / Float32Array.BYTES_PER_ELEMENT,
253
+ 0
254
+ );
255
+ const result = new Float32Array(totalLength);
256
+ let offset = 0;
257
+ for (const arr of byteArrays) {
258
+ const floats = new Float32Array(arr.buffer);
259
+ result.set(floats, offset);
260
+ offset += floats.length;
261
+ }
262
+ return result;
244
263
  }
245
264
  function playAudioBuffer(floats, context, startAt, sampleRate) {
246
265
  const source = context.createBufferSource();
@@ -403,8 +422,11 @@ var WebSocket = class extends Client {
403
422
  * @throws {Error} If the WebSocket fails to connect.
404
423
  */
405
424
  connect() {
406
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", "ws");
425
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
426
+ websocket: true
427
+ });
407
428
  url.searchParams.set("api_key", this.apiKey);
429
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
408
430
  const emitter = new import_emittery2.default();
409
431
  this.socket = new import_partysocket.WebSocket(url.toString());
410
432
  this.socket.onopen = () => {
@@ -575,7 +597,7 @@ var Player = class {
575
597
  const plays = [];
576
598
  while (true) {
577
599
  const read = yield source.read(buffer);
578
- const playableAudio = buffer.slice(0, read);
600
+ const playableAudio = buffer.subarray(0, read);
579
601
  plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
580
602
  if (read < buffer.length) {
581
603
  yield __privateGet(this, _emitter2).emit("finish");
package/dist/index.js CHANGED
@@ -1,17 +1,17 @@
1
1
  import "./chunk-FXPGR372.js";
2
2
  import {
3
3
  Cartesia
4
- } from "./chunk-X7SJMF2R.js";
5
- import "./chunk-WBK6LLXX.js";
6
- import "./chunk-4RMSIQLG.js";
7
- import "./chunk-NDNN326Q.js";
8
- import "./chunk-JOHSCOLW.js";
9
- import "./chunk-BCQ63627.js";
10
- import "./chunk-3GBZUGUD.js";
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";
10
+ import "./chunk-2BFEKY3F.js";
11
11
  import {
12
12
  Player
13
- } from "./chunk-WE63M7PJ.js";
14
- import "./chunk-LYPTISWL.js";
13
+ } from "./chunk-36JBKJUN.js";
14
+ import "./chunk-RO7TY474.js";
15
15
  import "./chunk-WIFMLPT5.js";
16
16
  export {
17
17
  Cartesia,
@@ -53,16 +53,14 @@ module.exports = __toCommonJS(client_exports);
53
53
  var import_cross_fetch = __toESM(require("cross-fetch"), 1);
54
54
 
55
55
  // src/lib/constants.ts
56
- var BASE_URL = "https://api.cartesia.ai/v0";
57
- var constructApiUrl = (baseUrl, path, protocol) => {
58
- const normalizedPath = path.startsWith("/") ? path : `/${path}`;
59
- if (!protocol) {
60
- return new URL(`${baseUrl}${normalizedPath}`);
56
+ var BASE_URL = "https://api.cartesia.ai";
57
+ var CARTESIA_VERSION = "2024-06-10";
58
+ var constructApiUrl = (baseUrl, path, { websocket = false } = {}) => {
59
+ const url = new URL(path, baseUrl);
60
+ if (websocket) {
61
+ url.protocol = baseUrl.replace(/^http/, "ws");
61
62
  }
62
- if (!["http", "ws"].includes(protocol)) {
63
- throw new Error(`Invalid protocol: ${protocol}`);
64
- }
65
- return new URL(`${baseUrl.replace(/^http/, protocol)}${normalizedPath}`);
63
+ return url;
66
64
  };
67
65
 
68
66
  // src/lib/client.ts
@@ -78,7 +76,8 @@ var Client = class {
78
76
  const url = constructApiUrl(this.baseUrl, path);
79
77
  return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
80
78
  headers: __spreadValues({
81
- "X-API-KEY": this.apiKey
79
+ "X-API-Key": this.apiKey,
80
+ "Cartesia-Version": CARTESIA_VERSION
82
81
  }, options.headers)
83
82
  }));
84
83
  }
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  Client
3
- } from "../chunk-BCQ63627.js";
4
- import "../chunk-3GBZUGUD.js";
3
+ } from "../chunk-PQ5EVEEH.js";
4
+ import "../chunk-2BFEKY3F.js";
5
5
  import "../chunk-WIFMLPT5.js";
6
6
  export {
7
7
  Client
@@ -21,22 +21,22 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var constants_exports = {};
22
22
  __export(constants_exports, {
23
23
  BASE_URL: () => BASE_URL,
24
+ CARTESIA_VERSION: () => CARTESIA_VERSION,
24
25
  constructApiUrl: () => constructApiUrl
25
26
  });
26
27
  module.exports = __toCommonJS(constants_exports);
27
- var BASE_URL = "https://api.cartesia.ai/v0";
28
- var constructApiUrl = (baseUrl, path, protocol) => {
29
- const normalizedPath = path.startsWith("/") ? path : `/${path}`;
30
- if (!protocol) {
31
- return new URL(`${baseUrl}${normalizedPath}`);
28
+ var BASE_URL = "https://api.cartesia.ai";
29
+ var CARTESIA_VERSION = "2024-06-10";
30
+ var constructApiUrl = (baseUrl, path, { websocket = false } = {}) => {
31
+ const url = new URL(path, baseUrl);
32
+ if (websocket) {
33
+ url.protocol = baseUrl.replace(/^http/, "ws");
32
34
  }
33
- if (!["http", "ws"].includes(protocol)) {
34
- throw new Error(`Invalid protocol: ${protocol}`);
35
- }
36
- return new URL(`${baseUrl.replace(/^http/, protocol)}${normalizedPath}`);
35
+ return url;
37
36
  };
38
37
  // Annotate the CommonJS export names for ESM import in node:
39
38
  0 && (module.exports = {
40
39
  BASE_URL,
40
+ CARTESIA_VERSION,
41
41
  constructApiUrl
42
42
  });
@@ -1,4 +1,16 @@
1
- declare const BASE_URL = "https://api.cartesia.ai/v0";
2
- declare const constructApiUrl: (baseUrl: string, path: string, protocol?: string) => URL;
1
+ declare const BASE_URL = "https://api.cartesia.ai";
2
+ declare const CARTESIA_VERSION = "2024-06-10";
3
+ /**
4
+ * Construct a URL for the Cartesia API.
5
+ *
6
+ * @param baseUrl The base URL for the API.
7
+ * @param path The path to append to the base URL.
8
+ * @param options Options for the URL.
9
+ * @param options.websocket Whether to use the WebSocket protocol.
10
+ * @returns A URL object.
11
+ */
12
+ declare const constructApiUrl: (baseUrl: string, path: string, { websocket }?: {
13
+ websocket?: boolean | undefined;
14
+ }) => URL;
3
15
 
4
- export { BASE_URL, constructApiUrl };
16
+ export { BASE_URL, CARTESIA_VERSION, constructApiUrl };
@@ -1,4 +1,16 @@
1
- declare const BASE_URL = "https://api.cartesia.ai/v0";
2
- declare const constructApiUrl: (baseUrl: string, path: string, protocol?: string) => URL;
1
+ declare const BASE_URL = "https://api.cartesia.ai";
2
+ declare const CARTESIA_VERSION = "2024-06-10";
3
+ /**
4
+ * Construct a URL for the Cartesia API.
5
+ *
6
+ * @param baseUrl The base URL for the API.
7
+ * @param path The path to append to the base URL.
8
+ * @param options Options for the URL.
9
+ * @param options.websocket Whether to use the WebSocket protocol.
10
+ * @returns A URL object.
11
+ */
12
+ declare const constructApiUrl: (baseUrl: string, path: string, { websocket }?: {
13
+ websocket?: boolean | undefined;
14
+ }) => URL;
3
15
 
4
- export { BASE_URL, constructApiUrl };
16
+ export { BASE_URL, CARTESIA_VERSION, constructApiUrl };
@@ -1,9 +1,11 @@
1
1
  import {
2
2
  BASE_URL,
3
+ CARTESIA_VERSION,
3
4
  constructApiUrl
4
- } from "../chunk-3GBZUGUD.js";
5
+ } from "../chunk-2BFEKY3F.js";
5
6
  import "../chunk-WIFMLPT5.js";
6
7
  export {
7
8
  BASE_URL,
9
+ CARTESIA_VERSION,
8
10
  constructApiUrl
9
11
  };