@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
@@ -97,7 +97,8 @@ module.exports = __toCommonJS(lib_exports);
97
97
  var import_cross_fetch = __toESM(require("cross-fetch"), 1);
98
98
 
99
99
  // src/lib/constants.ts
100
- var BASE_URL = "https://api.cartesia.ai/v0";
100
+ var BASE_URL = "https://api.cartesia.ai/";
101
+ var CARTESIA_VERSION = "2024-06-10";
101
102
  var constructApiUrl = (baseUrl, path, protocol) => {
102
103
  const normalizedPath = path.startsWith("/") ? path : `/${path}`;
103
104
  if (!protocol) {
@@ -122,7 +123,8 @@ var Client = class {
122
123
  const url = constructApiUrl(this.baseUrl, path);
123
124
  return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
124
125
  headers: __spreadValues({
125
- "X-API-KEY": this.apiKey
126
+ "X-API-Key": this.apiKey,
127
+ "Cartesia-Version": CARTESIA_VERSION
126
128
  }, options.headers)
127
129
  }));
128
130
  }
@@ -135,7 +137,7 @@ var import_partysocket = require("partysocket");
135
137
 
136
138
  // src/tts/source.ts
137
139
  var import_emittery = __toESM(require("emittery"), 1);
138
- var _emitter, _buffer, _readIndex, _closed, _sampleRate;
140
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
139
141
  var Source = class {
140
142
  /**
141
143
  * Create a new Source.
@@ -145,8 +147,9 @@ var Source = class {
145
147
  */
146
148
  constructor({ sampleRate }) {
147
149
  __privateAdd(this, _emitter, new import_emittery.default());
148
- __privateAdd(this, _buffer, new Float32Array());
150
+ __privateAdd(this, _buffer, void 0);
149
151
  __privateAdd(this, _readIndex, 0);
152
+ __privateAdd(this, _writeIndex, 0);
150
153
  __privateAdd(this, _closed, false);
151
154
  __privateAdd(this, _sampleRate, void 0);
152
155
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
@@ -154,6 +157,7 @@ var Source = class {
154
157
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
155
158
  this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
156
159
  __privateSet(this, _sampleRate, sampleRate);
160
+ __privateSet(this, _buffer, new Float32Array(1024));
157
161
  }
158
162
  get sampleRate() {
159
163
  return __privateGet(this, _sampleRate);
@@ -165,7 +169,18 @@ var Source = class {
165
169
  */
166
170
  enqueue(src) {
167
171
  return __async(this, null, function* () {
168
- __privateSet(this, _buffer, new Float32Array([...__privateGet(this, _buffer), ...src]));
172
+ const requiredCapacity = __privateGet(this, _writeIndex) + src.length;
173
+ if (requiredCapacity > __privateGet(this, _buffer).length) {
174
+ let newCapacity = __privateGet(this, _buffer).length;
175
+ while (newCapacity < requiredCapacity) {
176
+ newCapacity *= 2;
177
+ }
178
+ const newBuffer = new Float32Array(newCapacity);
179
+ newBuffer.set(__privateGet(this, _buffer));
180
+ __privateSet(this, _buffer, newBuffer);
181
+ }
182
+ __privateGet(this, _buffer).set(src, __privateGet(this, _writeIndex));
183
+ __privateSet(this, _writeIndex, __privateGet(this, _writeIndex) + src.length);
169
184
  yield __privateGet(this, _emitter).emit("enqueue");
170
185
  });
171
186
  }
@@ -179,7 +194,7 @@ var Source = class {
179
194
  read(dst) {
180
195
  return __async(this, null, function* () {
181
196
  const targetReadIndex = __privateGet(this, _readIndex) + dst.length;
182
- while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _buffer).length) {
197
+ while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _writeIndex)) {
183
198
  yield __privateGet(this, _emitter).emit("wait");
184
199
  yield Promise.race([
185
200
  __privateGet(this, _emitter).once("enqueue"),
@@ -187,8 +202,8 @@ var Source = class {
187
202
  ]);
188
203
  yield __privateGet(this, _emitter).emit("read");
189
204
  }
190
- const read = Math.min(dst.length, __privateGet(this, _buffer).length - __privateGet(this, _readIndex));
191
- dst.set(__privateGet(this, _buffer).slice(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
205
+ const read = Math.min(dst.length, __privateGet(this, _writeIndex) - __privateGet(this, _readIndex));
206
+ dst.set(__privateGet(this, _buffer).subarray(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
192
207
  __privateSet(this, _readIndex, __privateGet(this, _readIndex) + read);
193
208
  return read;
194
209
  });
@@ -226,19 +241,26 @@ var Source = class {
226
241
  _emitter = new WeakMap();
227
242
  _buffer = new WeakMap();
228
243
  _readIndex = new WeakMap();
244
+ _writeIndex = new WeakMap();
229
245
  _closed = new WeakMap();
230
246
  _sampleRate = new WeakMap();
231
247
 
232
248
  // src/tts/utils.ts
233
249
  var import_base64_js = __toESM(require("base64-js"), 1);
234
250
  function base64ToArray(b64) {
235
- return filterSentinel(b64).reduce((acc, b) => {
236
- const floats = new Float32Array(import_base64_js.default.toByteArray(b).buffer);
237
- const newAcc = new Float32Array(acc.length + floats.length);
238
- newAcc.set(acc, 0);
239
- newAcc.set(floats, acc.length);
240
- return newAcc;
241
- }, new Float32Array(0));
251
+ const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
252
+ const totalLength = byteArrays.reduce(
253
+ (acc, arr) => acc + arr.length / Float32Array.BYTES_PER_ELEMENT,
254
+ 0
255
+ );
256
+ const result = new Float32Array(totalLength);
257
+ let offset = 0;
258
+ for (const arr of byteArrays) {
259
+ const floats = new Float32Array(arr.buffer);
260
+ result.set(floats, offset);
261
+ offset += floats.length;
262
+ }
263
+ return result;
242
264
  }
243
265
  function createMessageHandlerForContextId(contextId, handler) {
244
266
  return (event) => {
@@ -388,7 +410,11 @@ var WebSocket = class extends Client {
388
410
  * @throws {Error} If the WebSocket fails to connect.
389
411
  */
390
412
  connect() {
391
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", "ws");
413
+ const url = constructApiUrl(
414
+ this.baseUrl,
415
+ `/tts/websocket?cartesia_version=${CARTESIA_VERSION}`,
416
+ "ws"
417
+ );
392
418
  url.searchParams.set("api_key", this.apiKey);
393
419
  const emitter = new import_emittery2.default();
394
420
  this.socket = new import_partysocket.WebSocket(url.toString());
package/dist/lib/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  Cartesia
3
- } from "../chunk-X7SJMF2R.js";
4
- import "../chunk-WBK6LLXX.js";
5
- import "../chunk-4RMSIQLG.js";
6
- import "../chunk-NDNN326Q.js";
7
- import "../chunk-JOHSCOLW.js";
8
- import "../chunk-BCQ63627.js";
9
- import "../chunk-3GBZUGUD.js";
10
- import "../chunk-LYPTISWL.js";
3
+ } from "../chunk-T3RG6WV4.js";
4
+ import "../chunk-WVTITUXX.js";
5
+ import "../chunk-KWBSQZTY.js";
6
+ import "../chunk-3F5E46FT.js";
7
+ import "../chunk-PQ6CIPFW.js";
8
+ import "../chunk-JGP5BIUV.js";
9
+ import "../chunk-XHTDPLFR.js";
10
+ import "../chunk-RO7TY474.js";
11
11
  import "../chunk-WIFMLPT5.js";
12
12
  export {
13
13
  Cartesia
@@ -98,7 +98,8 @@ var import_react = require("react");
98
98
  var import_cross_fetch = __toESM(require("cross-fetch"), 1);
99
99
 
100
100
  // src/lib/constants.ts
101
- var BASE_URL = "https://api.cartesia.ai/v0";
101
+ var BASE_URL = "https://api.cartesia.ai/";
102
+ var CARTESIA_VERSION = "2024-06-10";
102
103
  var constructApiUrl = (baseUrl, path, protocol) => {
103
104
  const normalizedPath = path.startsWith("/") ? path : `/${path}`;
104
105
  if (!protocol) {
@@ -123,7 +124,8 @@ var Client = class {
123
124
  const url = constructApiUrl(this.baseUrl, path);
124
125
  return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
125
126
  headers: __spreadValues({
126
- "X-API-KEY": this.apiKey
127
+ "X-API-Key": this.apiKey,
128
+ "Cartesia-Version": CARTESIA_VERSION
127
129
  }, options.headers)
128
130
  }));
129
131
  }
@@ -136,7 +138,7 @@ var import_partysocket = require("partysocket");
136
138
 
137
139
  // src/tts/source.ts
138
140
  var import_emittery = __toESM(require("emittery"), 1);
139
- var _emitter, _buffer, _readIndex, _closed, _sampleRate;
141
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
140
142
  var Source = class {
141
143
  /**
142
144
  * Create a new Source.
@@ -146,8 +148,9 @@ var Source = class {
146
148
  */
147
149
  constructor({ sampleRate }) {
148
150
  __privateAdd(this, _emitter, new import_emittery.default());
149
- __privateAdd(this, _buffer, new Float32Array());
151
+ __privateAdd(this, _buffer, void 0);
150
152
  __privateAdd(this, _readIndex, 0);
153
+ __privateAdd(this, _writeIndex, 0);
151
154
  __privateAdd(this, _closed, false);
152
155
  __privateAdd(this, _sampleRate, void 0);
153
156
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
@@ -155,6 +158,7 @@ var Source = class {
155
158
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
156
159
  this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
157
160
  __privateSet(this, _sampleRate, sampleRate);
161
+ __privateSet(this, _buffer, new Float32Array(1024));
158
162
  }
159
163
  get sampleRate() {
160
164
  return __privateGet(this, _sampleRate);
@@ -166,7 +170,18 @@ var Source = class {
166
170
  */
167
171
  enqueue(src) {
168
172
  return __async(this, null, function* () {
169
- __privateSet(this, _buffer, new Float32Array([...__privateGet(this, _buffer), ...src]));
173
+ const requiredCapacity = __privateGet(this, _writeIndex) + src.length;
174
+ if (requiredCapacity > __privateGet(this, _buffer).length) {
175
+ let newCapacity = __privateGet(this, _buffer).length;
176
+ while (newCapacity < requiredCapacity) {
177
+ newCapacity *= 2;
178
+ }
179
+ const newBuffer = new Float32Array(newCapacity);
180
+ newBuffer.set(__privateGet(this, _buffer));
181
+ __privateSet(this, _buffer, newBuffer);
182
+ }
183
+ __privateGet(this, _buffer).set(src, __privateGet(this, _writeIndex));
184
+ __privateSet(this, _writeIndex, __privateGet(this, _writeIndex) + src.length);
170
185
  yield __privateGet(this, _emitter).emit("enqueue");
171
186
  });
172
187
  }
@@ -180,7 +195,7 @@ var Source = class {
180
195
  read(dst) {
181
196
  return __async(this, null, function* () {
182
197
  const targetReadIndex = __privateGet(this, _readIndex) + dst.length;
183
- while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _buffer).length) {
198
+ while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _writeIndex)) {
184
199
  yield __privateGet(this, _emitter).emit("wait");
185
200
  yield Promise.race([
186
201
  __privateGet(this, _emitter).once("enqueue"),
@@ -188,8 +203,8 @@ var Source = class {
188
203
  ]);
189
204
  yield __privateGet(this, _emitter).emit("read");
190
205
  }
191
- const read = Math.min(dst.length, __privateGet(this, _buffer).length - __privateGet(this, _readIndex));
192
- dst.set(__privateGet(this, _buffer).slice(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
206
+ const read = Math.min(dst.length, __privateGet(this, _writeIndex) - __privateGet(this, _readIndex));
207
+ dst.set(__privateGet(this, _buffer).subarray(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
193
208
  __privateSet(this, _readIndex, __privateGet(this, _readIndex) + read);
194
209
  return read;
195
210
  });
@@ -227,19 +242,26 @@ var Source = class {
227
242
  _emitter = new WeakMap();
228
243
  _buffer = new WeakMap();
229
244
  _readIndex = new WeakMap();
245
+ _writeIndex = new WeakMap();
230
246
  _closed = new WeakMap();
231
247
  _sampleRate = new WeakMap();
232
248
 
233
249
  // src/tts/utils.ts
234
250
  var import_base64_js = __toESM(require("base64-js"), 1);
235
251
  function base64ToArray(b64) {
236
- return filterSentinel(b64).reduce((acc, b) => {
237
- const floats = new Float32Array(import_base64_js.default.toByteArray(b).buffer);
238
- const newAcc = new Float32Array(acc.length + floats.length);
239
- newAcc.set(acc, 0);
240
- newAcc.set(floats, acc.length);
241
- return newAcc;
242
- }, new Float32Array(0));
252
+ const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
253
+ const totalLength = byteArrays.reduce(
254
+ (acc, arr) => acc + arr.length / Float32Array.BYTES_PER_ELEMENT,
255
+ 0
256
+ );
257
+ const result = new Float32Array(totalLength);
258
+ let offset = 0;
259
+ for (const arr of byteArrays) {
260
+ const floats = new Float32Array(arr.buffer);
261
+ result.set(floats, offset);
262
+ offset += floats.length;
263
+ }
264
+ return result;
243
265
  }
244
266
  function playAudioBuffer(floats, context, startAt, sampleRate) {
245
267
  const source = context.createBufferSource();
@@ -402,7 +424,11 @@ var WebSocket = class extends Client {
402
424
  * @throws {Error} If the WebSocket fails to connect.
403
425
  */
404
426
  connect() {
405
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", "ws");
427
+ const url = constructApiUrl(
428
+ this.baseUrl,
429
+ `/tts/websocket?cartesia_version=${CARTESIA_VERSION}`,
430
+ "ws"
431
+ );
406
432
  url.searchParams.set("api_key", this.apiKey);
407
433
  const emitter = new import_emittery2.default();
408
434
  this.socket = new import_partysocket.WebSocket(url.toString());
@@ -574,7 +600,7 @@ var Player = class {
574
600
  const plays = [];
575
601
  while (true) {
576
602
  const read = yield source.read(buffer);
577
- const playableAudio = buffer.slice(0, read);
603
+ const playableAudio = buffer.subarray(0, read);
578
604
  plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
579
605
  if (read < buffer.length) {
580
606
  yield __privateGet(this, _emitter2).emit("finish");
@@ -711,11 +737,12 @@ function useTTS({
711
737
  if (!websocketReturn.current) {
712
738
  return;
713
739
  }
714
- websocketReturn.current.on("message", (message) => {
740
+ const unsubscribe = websocketReturn.current.on("message", (message) => {
715
741
  setMessages((messages2) => [...messages2, JSON.parse(message)]);
716
742
  });
717
743
  yield websocketReturn.current.source.once("close");
718
744
  setBufferStatus("buffered");
745
+ unsubscribe();
719
746
  }),
720
747
  [websocket]
721
748
  );
@@ -741,13 +768,18 @@ function useTTS({
741
768
  if (!connection) {
742
769
  return;
743
770
  }
771
+ const unsubscribes = [];
744
772
  setIsConnected(true);
745
- connection.on("open", () => {
746
- setIsConnected(true);
747
- });
748
- const unsubscribe = connection.on("close", () => {
749
- setIsConnected(false);
750
- });
773
+ unsubscribes.push(
774
+ connection.on("open", () => {
775
+ setIsConnected(true);
776
+ })
777
+ );
778
+ unsubscribes.push(
779
+ connection.on("close", () => {
780
+ setIsConnected(false);
781
+ })
782
+ );
751
783
  const intervalId = setInterval(() => {
752
784
  if (baseUrl) {
753
785
  pingServer(new URL(baseUrl).origin).then((ping) => {
@@ -764,7 +796,9 @@ function useTTS({
764
796
  }
765
797
  }, PING_INTERVAL);
766
798
  return () => {
767
- unsubscribe();
799
+ for (const unsubscribe of unsubscribes) {
800
+ unsubscribe();
801
+ }
768
802
  clearInterval(intervalId);
769
803
  websocket == null ? void 0 : websocket.disconnect();
770
804
  };
@@ -1,19 +1,19 @@
1
1
  import {
2
2
  Cartesia
3
- } from "../chunk-X7SJMF2R.js";
4
- import "../chunk-WBK6LLXX.js";
3
+ } from "../chunk-T3RG6WV4.js";
4
+ import "../chunk-WVTITUXX.js";
5
5
  import {
6
6
  pingServer
7
7
  } from "../chunk-3FL2SNIR.js";
8
- import "../chunk-4RMSIQLG.js";
9
- import "../chunk-NDNN326Q.js";
10
- import "../chunk-JOHSCOLW.js";
11
- import "../chunk-BCQ63627.js";
12
- import "../chunk-3GBZUGUD.js";
8
+ import "../chunk-KWBSQZTY.js";
9
+ import "../chunk-3F5E46FT.js";
10
+ import "../chunk-PQ6CIPFW.js";
11
+ import "../chunk-JGP5BIUV.js";
12
+ import "../chunk-XHTDPLFR.js";
13
13
  import {
14
14
  Player
15
- } from "../chunk-WE63M7PJ.js";
16
- import "../chunk-LYPTISWL.js";
15
+ } from "../chunk-36JBKJUN.js";
16
+ import "../chunk-RO7TY474.js";
17
17
  import {
18
18
  __async
19
19
  } from "../chunk-WIFMLPT5.js";
@@ -75,11 +75,12 @@ function useTTS({
75
75
  if (!websocketReturn.current) {
76
76
  return;
77
77
  }
78
- websocketReturn.current.on("message", (message) => {
78
+ const unsubscribe = websocketReturn.current.on("message", (message) => {
79
79
  setMessages((messages2) => [...messages2, JSON.parse(message)]);
80
80
  });
81
81
  yield websocketReturn.current.source.once("close");
82
82
  setBufferStatus("buffered");
83
+ unsubscribe();
83
84
  }),
84
85
  [websocket]
85
86
  );
@@ -105,13 +106,18 @@ function useTTS({
105
106
  if (!connection) {
106
107
  return;
107
108
  }
109
+ const unsubscribes = [];
108
110
  setIsConnected(true);
109
- connection.on("open", () => {
110
- setIsConnected(true);
111
- });
112
- const unsubscribe = connection.on("close", () => {
113
- setIsConnected(false);
114
- });
111
+ unsubscribes.push(
112
+ connection.on("open", () => {
113
+ setIsConnected(true);
114
+ })
115
+ );
116
+ unsubscribes.push(
117
+ connection.on("close", () => {
118
+ setIsConnected(false);
119
+ })
120
+ );
115
121
  const intervalId = setInterval(() => {
116
122
  if (baseUrl) {
117
123
  pingServer(new URL(baseUrl).origin).then((ping) => {
@@ -128,7 +134,9 @@ function useTTS({
128
134
  }
129
135
  }, PING_INTERVAL);
130
136
  return () => {
131
- unsubscribe();
137
+ for (const unsubscribe of unsubscribes) {
138
+ unsubscribe();
139
+ }
132
140
  clearInterval(intervalId);
133
141
  websocket == null ? void 0 : websocket.disconnect();
134
142
  };
@@ -97,7 +97,8 @@ module.exports = __toCommonJS(tts_exports);
97
97
  var import_cross_fetch = __toESM(require("cross-fetch"), 1);
98
98
 
99
99
  // src/lib/constants.ts
100
- var BASE_URL = "https://api.cartesia.ai/v0";
100
+ var BASE_URL = "https://api.cartesia.ai/";
101
+ var CARTESIA_VERSION = "2024-06-10";
101
102
  var constructApiUrl = (baseUrl, path, protocol) => {
102
103
  const normalizedPath = path.startsWith("/") ? path : `/${path}`;
103
104
  if (!protocol) {
@@ -122,7 +123,8 @@ var Client = class {
122
123
  const url = constructApiUrl(this.baseUrl, path);
123
124
  return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
124
125
  headers: __spreadValues({
125
- "X-API-KEY": this.apiKey
126
+ "X-API-Key": this.apiKey,
127
+ "Cartesia-Version": CARTESIA_VERSION
126
128
  }, options.headers)
127
129
  }));
128
130
  }
@@ -135,7 +137,7 @@ var import_partysocket = require("partysocket");
135
137
 
136
138
  // src/tts/source.ts
137
139
  var import_emittery = __toESM(require("emittery"), 1);
138
- var _emitter, _buffer, _readIndex, _closed, _sampleRate;
140
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
139
141
  var Source = class {
140
142
  /**
141
143
  * Create a new Source.
@@ -145,8 +147,9 @@ var Source = class {
145
147
  */
146
148
  constructor({ sampleRate }) {
147
149
  __privateAdd(this, _emitter, new import_emittery.default());
148
- __privateAdd(this, _buffer, new Float32Array());
150
+ __privateAdd(this, _buffer, void 0);
149
151
  __privateAdd(this, _readIndex, 0);
152
+ __privateAdd(this, _writeIndex, 0);
150
153
  __privateAdd(this, _closed, false);
151
154
  __privateAdd(this, _sampleRate, void 0);
152
155
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
@@ -154,6 +157,7 @@ var Source = class {
154
157
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
155
158
  this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
156
159
  __privateSet(this, _sampleRate, sampleRate);
160
+ __privateSet(this, _buffer, new Float32Array(1024));
157
161
  }
158
162
  get sampleRate() {
159
163
  return __privateGet(this, _sampleRate);
@@ -165,7 +169,18 @@ var Source = class {
165
169
  */
166
170
  enqueue(src) {
167
171
  return __async(this, null, function* () {
168
- __privateSet(this, _buffer, new Float32Array([...__privateGet(this, _buffer), ...src]));
172
+ const requiredCapacity = __privateGet(this, _writeIndex) + src.length;
173
+ if (requiredCapacity > __privateGet(this, _buffer).length) {
174
+ let newCapacity = __privateGet(this, _buffer).length;
175
+ while (newCapacity < requiredCapacity) {
176
+ newCapacity *= 2;
177
+ }
178
+ const newBuffer = new Float32Array(newCapacity);
179
+ newBuffer.set(__privateGet(this, _buffer));
180
+ __privateSet(this, _buffer, newBuffer);
181
+ }
182
+ __privateGet(this, _buffer).set(src, __privateGet(this, _writeIndex));
183
+ __privateSet(this, _writeIndex, __privateGet(this, _writeIndex) + src.length);
169
184
  yield __privateGet(this, _emitter).emit("enqueue");
170
185
  });
171
186
  }
@@ -179,7 +194,7 @@ var Source = class {
179
194
  read(dst) {
180
195
  return __async(this, null, function* () {
181
196
  const targetReadIndex = __privateGet(this, _readIndex) + dst.length;
182
- while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _buffer).length) {
197
+ while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _writeIndex)) {
183
198
  yield __privateGet(this, _emitter).emit("wait");
184
199
  yield Promise.race([
185
200
  __privateGet(this, _emitter).once("enqueue"),
@@ -187,8 +202,8 @@ var Source = class {
187
202
  ]);
188
203
  yield __privateGet(this, _emitter).emit("read");
189
204
  }
190
- const read = Math.min(dst.length, __privateGet(this, _buffer).length - __privateGet(this, _readIndex));
191
- dst.set(__privateGet(this, _buffer).slice(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
205
+ const read = Math.min(dst.length, __privateGet(this, _writeIndex) - __privateGet(this, _readIndex));
206
+ dst.set(__privateGet(this, _buffer).subarray(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
192
207
  __privateSet(this, _readIndex, __privateGet(this, _readIndex) + read);
193
208
  return read;
194
209
  });
@@ -226,19 +241,26 @@ var Source = class {
226
241
  _emitter = new WeakMap();
227
242
  _buffer = new WeakMap();
228
243
  _readIndex = new WeakMap();
244
+ _writeIndex = new WeakMap();
229
245
  _closed = new WeakMap();
230
246
  _sampleRate = new WeakMap();
231
247
 
232
248
  // src/tts/utils.ts
233
249
  var import_base64_js = __toESM(require("base64-js"), 1);
234
250
  function base64ToArray(b64) {
235
- return filterSentinel(b64).reduce((acc, b) => {
236
- const floats = new Float32Array(import_base64_js.default.toByteArray(b).buffer);
237
- const newAcc = new Float32Array(acc.length + floats.length);
238
- newAcc.set(acc, 0);
239
- newAcc.set(floats, acc.length);
240
- return newAcc;
241
- }, new Float32Array(0));
251
+ const byteArrays = filterSentinel(b64).map((b) => import_base64_js.default.toByteArray(b));
252
+ const totalLength = byteArrays.reduce(
253
+ (acc, arr) => acc + arr.length / Float32Array.BYTES_PER_ELEMENT,
254
+ 0
255
+ );
256
+ const result = new Float32Array(totalLength);
257
+ let offset = 0;
258
+ for (const arr of byteArrays) {
259
+ const floats = new Float32Array(arr.buffer);
260
+ result.set(floats, offset);
261
+ offset += floats.length;
262
+ }
263
+ return result;
242
264
  }
243
265
  function createMessageHandlerForContextId(contextId, handler) {
244
266
  return (event) => {
@@ -388,7 +410,11 @@ var WebSocket = class extends Client {
388
410
  * @throws {Error} If the WebSocket fails to connect.
389
411
  */
390
412
  connect() {
391
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", "ws");
413
+ const url = constructApiUrl(
414
+ this.baseUrl,
415
+ `/tts/websocket?cartesia_version=${CARTESIA_VERSION}`,
416
+ "ws"
417
+ );
392
418
  url.searchParams.set("api_key", this.apiKey);
393
419
  const emitter = new import_emittery2.default();
394
420
  this.socket = new import_partysocket.WebSocket(url.toString());
package/dist/tts/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  TTS
3
- } from "../chunk-4RMSIQLG.js";
4
- import "../chunk-NDNN326Q.js";
5
- import "../chunk-JOHSCOLW.js";
6
- import "../chunk-BCQ63627.js";
7
- import "../chunk-3GBZUGUD.js";
8
- import "../chunk-LYPTISWL.js";
3
+ } from "../chunk-KWBSQZTY.js";
4
+ import "../chunk-3F5E46FT.js";
5
+ import "../chunk-PQ6CIPFW.js";
6
+ import "../chunk-JGP5BIUV.js";
7
+ import "../chunk-XHTDPLFR.js";
8
+ import "../chunk-RO7TY474.js";
9
9
  import "../chunk-WIFMLPT5.js";
10
10
  export {
11
11
  TTS as default
@@ -126,7 +126,7 @@ var Player = class {
126
126
  const plays = [];
127
127
  while (true) {
128
128
  const read = yield source.read(buffer);
129
- const playableAudio = buffer.slice(0, read);
129
+ const playableAudio = buffer.subarray(0, read);
130
130
  plays.push(__privateMethod(this, _playBuffer, playBuffer_fn).call(this, playableAudio, source.sampleRate));
131
131
  if (read < buffer.length) {
132
132
  yield __privateGet(this, _emitter).emit("finish");
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  Player
3
- } from "../chunk-WE63M7PJ.js";
4
- import "../chunk-LYPTISWL.js";
3
+ } from "../chunk-36JBKJUN.js";
4
+ import "../chunk-RO7TY474.js";
5
5
  import "../chunk-WIFMLPT5.js";
6
6
  export {
7
7
  Player as default
@@ -72,7 +72,7 @@ __export(source_exports, {
72
72
  });
73
73
  module.exports = __toCommonJS(source_exports);
74
74
  var import_emittery = __toESM(require("emittery"), 1);
75
- var _emitter, _buffer, _readIndex, _closed, _sampleRate;
75
+ var _emitter, _buffer, _readIndex, _writeIndex, _closed, _sampleRate;
76
76
  var Source = class {
77
77
  /**
78
78
  * Create a new Source.
@@ -82,8 +82,9 @@ var Source = class {
82
82
  */
83
83
  constructor({ sampleRate }) {
84
84
  __privateAdd(this, _emitter, new import_emittery.default());
85
- __privateAdd(this, _buffer, new Float32Array());
85
+ __privateAdd(this, _buffer, void 0);
86
86
  __privateAdd(this, _readIndex, 0);
87
+ __privateAdd(this, _writeIndex, 0);
87
88
  __privateAdd(this, _closed, false);
88
89
  __privateAdd(this, _sampleRate, void 0);
89
90
  this.on = __privateGet(this, _emitter).on.bind(__privateGet(this, _emitter));
@@ -91,6 +92,7 @@ var Source = class {
91
92
  this.events = __privateGet(this, _emitter).events.bind(__privateGet(this, _emitter));
92
93
  this.off = __privateGet(this, _emitter).off.bind(__privateGet(this, _emitter));
93
94
  __privateSet(this, _sampleRate, sampleRate);
95
+ __privateSet(this, _buffer, new Float32Array(1024));
94
96
  }
95
97
  get sampleRate() {
96
98
  return __privateGet(this, _sampleRate);
@@ -102,7 +104,18 @@ var Source = class {
102
104
  */
103
105
  enqueue(src) {
104
106
  return __async(this, null, function* () {
105
- __privateSet(this, _buffer, new Float32Array([...__privateGet(this, _buffer), ...src]));
107
+ const requiredCapacity = __privateGet(this, _writeIndex) + src.length;
108
+ if (requiredCapacity > __privateGet(this, _buffer).length) {
109
+ let newCapacity = __privateGet(this, _buffer).length;
110
+ while (newCapacity < requiredCapacity) {
111
+ newCapacity *= 2;
112
+ }
113
+ const newBuffer = new Float32Array(newCapacity);
114
+ newBuffer.set(__privateGet(this, _buffer));
115
+ __privateSet(this, _buffer, newBuffer);
116
+ }
117
+ __privateGet(this, _buffer).set(src, __privateGet(this, _writeIndex));
118
+ __privateSet(this, _writeIndex, __privateGet(this, _writeIndex) + src.length);
106
119
  yield __privateGet(this, _emitter).emit("enqueue");
107
120
  });
108
121
  }
@@ -116,7 +129,7 @@ var Source = class {
116
129
  read(dst) {
117
130
  return __async(this, null, function* () {
118
131
  const targetReadIndex = __privateGet(this, _readIndex) + dst.length;
119
- while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _buffer).length) {
132
+ while (!__privateGet(this, _closed) && targetReadIndex > __privateGet(this, _writeIndex)) {
120
133
  yield __privateGet(this, _emitter).emit("wait");
121
134
  yield Promise.race([
122
135
  __privateGet(this, _emitter).once("enqueue"),
@@ -124,8 +137,8 @@ var Source = class {
124
137
  ]);
125
138
  yield __privateGet(this, _emitter).emit("read");
126
139
  }
127
- const read = Math.min(dst.length, __privateGet(this, _buffer).length - __privateGet(this, _readIndex));
128
- dst.set(__privateGet(this, _buffer).slice(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
140
+ const read = Math.min(dst.length, __privateGet(this, _writeIndex) - __privateGet(this, _readIndex));
141
+ dst.set(__privateGet(this, _buffer).subarray(__privateGet(this, _readIndex), __privateGet(this, _readIndex) + read));
129
142
  __privateSet(this, _readIndex, __privateGet(this, _readIndex) + read);
130
143
  return read;
131
144
  });
@@ -163,5 +176,6 @@ var Source = class {
163
176
  _emitter = new WeakMap();
164
177
  _buffer = new WeakMap();
165
178
  _readIndex = new WeakMap();
179
+ _writeIndex = new WeakMap();
166
180
  _closed = new WeakMap();
167
181
  _sampleRate = new WeakMap();