@cartesia/cartesia-js 1.0.0-alpha.1 → 1.0.0-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 (43) hide show
  1. package/.turbo/turbo-build.log +36 -36
  2. package/CHANGELOG.md +6 -0
  3. package/dist/{chunk-WE63M7PJ.js → chunk-36JBKJUN.js} +2 -2
  4. package/dist/{chunk-NDNN326Q.js → chunk-3F5E46FT.js} +10 -5
  5. package/dist/{chunk-BCQ63627.js → chunk-JGP5BIUV.js} +4 -2
  6. package/dist/{chunk-4RMSIQLG.js → chunk-KWBSQZTY.js} +2 -2
  7. package/dist/{chunk-JOHSCOLW.js → chunk-PQ6CIPFW.js} +20 -6
  8. package/dist/{chunk-LYPTISWL.js → chunk-RO7TY474.js} +13 -7
  9. package/dist/{chunk-X7SJMF2R.js → chunk-T3RG6WV4.js} +3 -3
  10. package/dist/{chunk-WBK6LLXX.js → chunk-WVTITUXX.js} +1 -1
  11. package/dist/{chunk-3GBZUGUD.js → chunk-XHTDPLFR.js} +3 -1
  12. package/dist/index.cjs +43 -17
  13. package/dist/index.js +9 -9
  14. package/dist/lib/client.cjs +4 -2
  15. package/dist/lib/client.js +2 -2
  16. package/dist/lib/constants.cjs +4 -1
  17. package/dist/lib/constants.d.cts +3 -2
  18. package/dist/lib/constants.d.ts +3 -2
  19. package/dist/lib/constants.js +3 -1
  20. package/dist/lib/index.cjs +42 -16
  21. package/dist/lib/index.js +8 -8
  22. package/dist/react/index.cjs +59 -25
  23. package/dist/react/index.js +25 -17
  24. package/dist/tts/index.cjs +42 -16
  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 +42 -16
  33. package/dist/tts/websocket.js +5 -5
  34. package/dist/voices/index.cjs +4 -2
  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 +2 -1
  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 +6 -2
@@ -5,50 +5,50 @@ $ tsup src/ --format cjs,esm --dts
5
5
  CLI Target: es6
6
6
  CJS Build start
7
7
  ESM Build start
8
- ESM dist/index.js 437.00 B
8
+ CJS dist/index.cjs 21.16 KB
9
+ CJS dist/lib/client.cjs 3.51 KB
10
+ CJS dist/react/index.cjs 27.38 KB
11
+ CJS dist/tts/index.cjs 15.80 KB
12
+ CJS dist/types/index.cjs 764.00 B
13
+ CJS dist/voices/index.cjs 5.21 KB
14
+ CJS dist/lib/constants.cjs 1.61 KB
15
+ CJS dist/lib/index.cjs 17.33 KB
16
+ CJS dist/react/utils.cjs 1.80 KB
17
+ CJS dist/tts/player.cjs 6.66 KB
18
+ CJS dist/tts/source.cjs 6.63 KB
19
+ CJS dist/tts/utils.cjs 3.87 KB
20
+ CJS dist/tts/websocket.cjs 15.49 KB
21
+ CJS ⚡️ Build success in 134ms
9
22
  ESM dist/chunk-FXPGR372.js 0 B
23
+ ESM dist/tts/source.js 112.00 B
24
+ ESM dist/tts/utils.js 395.00 B
25
+ ESM dist/tts/websocket.js 242.00 B
26
+ ESM dist/types/index.js 31.00 B
27
+ ESM dist/voices/index.js 174.00 B
28
+ ESM dist/index.js 437.00 B
10
29
  ESM dist/lib/client.js 132.00 B
11
- ESM dist/react/index.js 6.00 KB
12
- ESM dist/tts/index.js 261.00 B
13
- ESM dist/lib/constants.js 143.00 B
30
+ ESM dist/lib/constants.js 183.00 B
14
31
  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
32
+ ESM dist/react/index.js 6.22 KB
33
+ ESM dist/chunk-T3RG6WV4.js 353.00 B
34
+ ESM dist/chunk-WVTITUXX.js 1.35 KB
17
35
  ESM dist/react/utils.js 109.00 B
18
36
  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
37
+ ESM dist/tts/index.js 261.00 B
38
+ ESM dist/chunk-KWBSQZTY.js 439.00 B
39
+ ESM dist/chunk-3F5E46FT.js 5.81 KB
40
+ ESM dist/chunk-PQ6CIPFW.js 4.02 KB
41
+ ESM dist/chunk-JGP5BIUV.js 841.00 B
42
+ ESM dist/chunk-XHTDPLFR.js 542.00 B
24
43
  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
44
+ ESM dist/chunk-36JBKJUN.js 3.52 KB
45
+ ESM dist/chunk-RO7TY474.js 1.95 KB
27
46
  ESM dist/chunk-WIFMLPT5.js 2.27 KB
28
- ESM dist/types/index.js 31.00 B
29
- ESM dist/voices/index.js 174.00 B
30
- ESM dist/tts/source.js 112.00 B
31
- ESM dist/tts/utils.js 395.00 B
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
39
- CJS dist/react/utils.cjs 1.80 KB
40
- CJS dist/tts/index.cjs 14.89 KB
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
45
- CJS dist/types/index.cjs 764.00 B
46
- CJS dist/voices/index.cjs 5.13 KB
47
- CJS ⚡️ Build success in 117ms
47
+ ESM ⚡️ Build success in 140ms
48
48
  DTS Build start
49
- DTS ⚡️ Build success in 8423ms
49
+ DTS ⚡️ Build success in 8877ms
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 247.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 247.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,11 @@
1
1
  # @cartesia/cartesia-js
2
2
 
3
+ ## 1.0.0-alpha.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 585d2c9: Makes JS client compatible with the Cartesia Stable API (2024-06-10)
8
+
3
9
  ## 1.0.0-alpha.1
4
10
 
5
11
  ### Major Changes
@@ -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-JGP5BIUV.js";
7
7
  import {
8
+ CARTESIA_VERSION,
8
9
  constructApiUrl
9
- } from "./chunk-3GBZUGUD.js";
10
+ } from "./chunk-XHTDPLFR.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,7 +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(
141
+ this.baseUrl,
142
+ `/tts/websocket?cartesia_version=${CARTESIA_VERSION}`,
143
+ "ws"
144
+ );
140
145
  url.searchParams.set("api_key", this.apiKey);
141
146
  const emitter = new Emittery();
142
147
  this.socket = new PartySocketWebSocket(url.toString());
@@ -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-XHTDPLFR.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
  }
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  WebSocket
3
- } from "./chunk-NDNN326Q.js";
3
+ } from "./chunk-3F5E46FT.js";
4
4
  import {
5
5
  Client
6
- } from "./chunk-BCQ63627.js";
6
+ } from "./chunk-JGP5BIUV.js";
7
7
 
8
8
  // src/tts/index.ts
9
9
  var TTS = class extends Client {
@@ -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,12 +1,12 @@
1
1
  import {
2
2
  Voices
3
- } from "./chunk-WBK6LLXX.js";
3
+ } from "./chunk-WVTITUXX.js";
4
4
  import {
5
5
  TTS
6
- } from "./chunk-4RMSIQLG.js";
6
+ } from "./chunk-KWBSQZTY.js";
7
7
  import {
8
8
  Client
9
- } from "./chunk-BCQ63627.js";
9
+ } from "./chunk-JGP5BIUV.js";
10
10
 
11
11
  // src/lib/index.ts
12
12
  var Cartesia = class extends Client {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Client
3
- } from "./chunk-BCQ63627.js";
3
+ } from "./chunk-JGP5BIUV.js";
4
4
  import {
5
5
  __async
6
6
  } from "./chunk-WIFMLPT5.js";
@@ -1,5 +1,6 @@
1
1
  // src/lib/constants.ts
2
- var BASE_URL = "https://api.cartesia.ai/v0";
2
+ var BASE_URL = "https://api.cartesia.ai/";
3
+ var CARTESIA_VERSION = "2024-06-10";
3
4
  var constructApiUrl = (baseUrl, path, protocol) => {
4
5
  const normalizedPath = path.startsWith("/") ? path : `/${path}`;
5
6
  if (!protocol) {
@@ -13,5 +14,6 @@ var constructApiUrl = (baseUrl, path, protocol) => {
13
14
 
14
15
  export {
15
16
  BASE_URL,
17
+ CARTESIA_VERSION,
16
18
  constructApiUrl
17
19
  };
package/dist/index.cjs CHANGED
@@ -99,7 +99,8 @@ 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";
102
+ var BASE_URL = "https://api.cartesia.ai/";
103
+ var CARTESIA_VERSION = "2024-06-10";
103
104
  var constructApiUrl = (baseUrl, path, protocol) => {
104
105
  const normalizedPath = path.startsWith("/") ? path : `/${path}`;
105
106
  if (!protocol) {
@@ -124,7 +125,8 @@ var Client = class {
124
125
  const url = constructApiUrl(this.baseUrl, path);
125
126
  return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
126
127
  headers: __spreadValues({
127
- "X-API-KEY": this.apiKey
128
+ "X-API-Key": this.apiKey,
129
+ "Cartesia-Version": CARTESIA_VERSION
128
130
  }, options.headers)
129
131
  }));
130
132
  }
@@ -137,7 +139,7 @@ var import_partysocket = require("partysocket");
137
139
 
138
140
  // src/tts/source.ts
139
141
  var import_emittery = __toESM(require("emittery"), 1);
140
- var _emitter, _buffer, _readIndex, _closed, _sampleRate;
142
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
141
143
  var Source = class {
142
144
  /**
143
145
  * Create a new Source.
@@ -147,8 +149,9 @@ var Source = class {
147
149
  */
148
150
  constructor({ sampleRate }) {
149
151
  __privateAdd(this, _emitter, new import_emittery.default());
150
- __privateAdd(this, _buffer, new Float32Array());
152
+ __privateAdd(this, _buffer, void 0);
151
153
  __privateAdd(this, _readIndex, 0);
154
+ __privateAdd(this, _writeIndex, 0);
152
155
  __privateAdd(this, _closed, false);
153
156
  __privateAdd(this, _sampleRate, void 0);
154
157
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
@@ -156,6 +159,7 @@ var Source = class {
156
159
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
157
160
  this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
158
161
  __privateSet(this, _sampleRate, sampleRate);
162
+ __privateSet(this, _buffer, new Float32Array(1024));
159
163
  }
160
164
  get sampleRate() {
161
165
  return __privateGet(this, _sampleRate);
@@ -167,7 +171,18 @@ var Source = class {
167
171
  */
168
172
  enqueue(src) {
169
173
  return __async(this, null, function* () {
170
- __privateSet(this, _buffer, new Float32Array([...__privateGet(this, _buffer), ...src]));
174
+ const requiredCapacity = __privateGet(this, _writeIndex) + src.length;
175
+ if (requiredCapacity > __privateGet(this, _buffer).length) {
176
+ let newCapacity = __privateGet(this, _buffer).length;
177
+ while (newCapacity < requiredCapacity) {
178
+ newCapacity *= 2;
179
+ }
180
+ const newBuffer = new Float32Array(newCapacity);
181
+ newBuffer.set(__privateGet(this, _buffer));
182
+ __privateSet(this, _buffer, newBuffer);
183
+ }
184
+ __privateGet(this, _buffer).set(src, __privateGet(this, _writeIndex));
185
+ __privateSet(this, _writeIndex, __privateGet(this, _writeIndex) + src.length);
171
186
  yield __privateGet(this, _emitter).emit("enqueue");
172
187
  });
173
188
  }
@@ -181,7 +196,7 @@ var Source = class {
181
196
  read(dst) {
182
197
  return __async(this, null, function* () {
183
198
  const targetReadIndex = __privateGet(this, _readIndex) + dst.length;
184
- while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _buffer).length) {
199
+ while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _writeIndex)) {
185
200
  yield __privateGet(this, _emitter).emit("wait");
186
201
  yield Promise.race([
187
202
  __privateGet(this, _emitter).once("enqueue"),
@@ -189,8 +204,8 @@ var Source = class {
189
204
  ]);
190
205
  yield __privateGet(this, _emitter).emit("read");
191
206
  }
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));
207
+ const read = Math.min(dst.length, __privateGet(this, _writeIndex) - __privateGet(this, _readIndex));
208
+ dst.set(__privateGet(this, _buffer).subarray(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
194
209
  __privateSet(this, _readIndex, __privateGet(this, _readIndex) + read);
195
210
  return read;
196
211
  });
@@ -228,19 +243,26 @@ var Source = class {
228
243
  _emitter = new WeakMap();
229
244
  _buffer = new WeakMap();
230
245
  _readIndex = new WeakMap();
246
+ _writeIndex = new WeakMap();
231
247
  _closed = new WeakMap();
232
248
  _sampleRate = new WeakMap();
233
249
 
234
250
  // src/tts/utils.ts
235
251
  var import_base64_js = __toESM(require("base64-js"), 1);
236
252
  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));
253
+ const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
254
+ const totalLength = byteArrays.reduce(
255
+ (acc, arr) => acc + arr.length / Float32Array.BYTES_PER_ELEMENT,
256
+ 0
257
+ );
258
+ const result = new Float32Array(totalLength);
259
+ let offset = 0;
260
+ for (const arr of byteArrays) {
261
+ const floats = new Float32Array(arr.buffer);
262
+ result.set(floats, offset);
263
+ offset += floats.length;
264
+ }
265
+ return result;
244
266
  }
245
267
  function playAudioBuffer(floats, context, startAt, sampleRate) {
246
268
  const source = context.createBufferSource();
@@ -403,7 +425,11 @@ var WebSocket = class extends Client {
403
425
  * @throws {Error} If the WebSocket fails to connect.
404
426
  */
405
427
  connect() {
406
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", "ws");
428
+ const url = constructApiUrl(
429
+ this.baseUrl,
430
+ `/tts/websocket?cartesia_version=${CARTESIA_VERSION}`,
431
+ "ws"
432
+ );
407
433
  url.searchParams.set("api_key", this.apiKey);
408
434
  const emitter = new import_emittery2.default();
409
435
  this.socket = new import_partysocket.WebSocket(url.toString());
@@ -575,7 +601,7 @@ var Player = class {
575
601
  const plays = [];
576
602
  while (true) {
577
603
  const read = yield source.read(buffer);
578
- const playableAudio = buffer.slice(0, read);
604
+ const playableAudio = buffer.subarray(0, read);
579
605
  plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
580
606
  if (read < buffer.length) {
581
607
  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-T3RG6WV4.js";
5
+ import "./chunk-WVTITUXX.js";
6
+ import "./chunk-KWBSQZTY.js";
7
+ import "./chunk-3F5E46FT.js";
8
+ import "./chunk-PQ6CIPFW.js";
9
+ import "./chunk-JGP5BIUV.js";
10
+ import "./chunk-XHTDPLFR.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,7 +53,8 @@ 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";
56
+ var BASE_URL = "https://api.cartesia.ai/";
57
+ var CARTESIA_VERSION = "2024-06-10";
57
58
  var constructApiUrl = (baseUrl, path, protocol) => {
58
59
  const normalizedPath = path.startsWith("/") ? path : `/${path}`;
59
60
  if (!protocol) {
@@ -78,7 +79,8 @@ var Client = class {
78
79
  const url = constructApiUrl(this.baseUrl, path);
79
80
  return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
80
81
  headers: __spreadValues({
81
- "X-API-KEY": this.apiKey
82
+ "X-API-Key": this.apiKey,
83
+ "Cartesia-Version": CARTESIA_VERSION
82
84
  }, options.headers)
83
85
  }));
84
86
  }
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  Client
3
- } from "../chunk-BCQ63627.js";
4
- import "../chunk-3GBZUGUD.js";
3
+ } from "../chunk-JGP5BIUV.js";
4
+ import "../chunk-XHTDPLFR.js";
5
5
  import "../chunk-WIFMLPT5.js";
6
6
  export {
7
7
  Client
@@ -21,10 +21,12 @@ 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 BASE_URL = "https://api.cartesia.ai/";
29
+ var CARTESIA_VERSION = "2024-06-10";
28
30
  var constructApiUrl = (baseUrl, path, protocol) => {
29
31
  const normalizedPath = path.startsWith("/") ? path : `/${path}`;
30
32
  if (!protocol) {
@@ -38,5 +40,6 @@ var constructApiUrl = (baseUrl, path, protocol) => {
38
40
  // Annotate the CommonJS export names for ESM import in node:
39
41
  0 && (module.exports = {
40
42
  BASE_URL,
43
+ CARTESIA_VERSION,
41
44
  constructApiUrl
42
45
  });
@@ -1,4 +1,5 @@
1
- declare const BASE_URL = "https://api.cartesia.ai/v0";
1
+ declare const BASE_URL = "https://api.cartesia.ai/";
2
+ declare const CARTESIA_VERSION = "2024-06-10";
2
3
  declare const constructApiUrl: (baseUrl: string, path: string, protocol?: string) => URL;
3
4
 
4
- export { BASE_URL, constructApiUrl };
5
+ export { BASE_URL, CARTESIA_VERSION, constructApiUrl };
@@ -1,4 +1,5 @@
1
- declare const BASE_URL = "https://api.cartesia.ai/v0";
1
+ declare const BASE_URL = "https://api.cartesia.ai/";
2
+ declare const CARTESIA_VERSION = "2024-06-10";
2
3
  declare const constructApiUrl: (baseUrl: string, path: string, protocol?: string) => URL;
3
4
 
4
- export { BASE_URL, constructApiUrl };
5
+ 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-XHTDPLFR.js";
5
6
  import "../chunk-WIFMLPT5.js";
6
7
  export {
7
8
  BASE_URL,
9
+ CARTESIA_VERSION,
8
10
  constructApiUrl
9
11
  };