@cartesia/cartesia-js 1.2.1 → 1.3.0

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 (41) hide show
  1. package/README.md +6 -2
  2. package/dist/{chunk-HQOC2S6C.js → chunk-CJZSHIRI.js} +28 -18
  3. package/dist/{chunk-EYRYHK3X.js → chunk-QT5LHJ3D.js} +1 -1
  4. package/dist/{chunk-YUGIYUMT.js → chunk-RYHHRY64.js} +3 -3
  5. package/dist/index.cjs +21 -11
  6. package/dist/index.d.cts +2 -1
  7. package/dist/index.d.ts +2 -1
  8. package/dist/index.js +11 -11
  9. package/dist/lib/client.d.cts +1 -0
  10. package/dist/lib/client.d.ts +1 -0
  11. package/dist/lib/index.cjs +21 -11
  12. package/dist/lib/index.d.cts +1 -0
  13. package/dist/lib/index.d.ts +1 -0
  14. package/dist/lib/index.js +5 -5
  15. package/dist/react/index.cjs +21 -11
  16. package/dist/react/index.d.cts +1 -0
  17. package/dist/react/index.d.ts +1 -0
  18. package/dist/react/index.js +9 -9
  19. package/dist/tts/index.cjs +21 -11
  20. package/dist/tts/index.d.cts +1 -0
  21. package/dist/tts/index.d.ts +1 -0
  22. package/dist/tts/index.js +4 -4
  23. package/dist/tts/player.d.cts +1 -0
  24. package/dist/tts/player.d.ts +1 -0
  25. package/dist/tts/source.d.cts +1 -0
  26. package/dist/tts/source.d.ts +1 -0
  27. package/dist/tts/utils.d.cts +1 -0
  28. package/dist/tts/utils.d.ts +1 -0
  29. package/dist/tts/websocket.cjs +21 -11
  30. package/dist/tts/websocket.d.cts +3 -2
  31. package/dist/tts/websocket.d.ts +3 -2
  32. package/dist/tts/websocket.js +3 -3
  33. package/dist/types/index.d.cts +3 -1
  34. package/dist/types/index.d.ts +3 -1
  35. package/dist/voice-changer/index.d.cts +1 -0
  36. package/dist/voice-changer/index.d.ts +1 -0
  37. package/dist/voices/index.d.cts +1 -0
  38. package/dist/voices/index.d.ts +1 -0
  39. package/package.json +1 -1
  40. package/src/tts/websocket.ts +21 -9
  41. package/src/types/index.ts +3 -0
package/README.md CHANGED
@@ -92,11 +92,15 @@ const cartesia = new Cartesia({
92
92
  const websocket = cartesia.tts.websocket({
93
93
  container: "raw",
94
94
  encoding: "pcm_f32le",
95
- sampleRate: 44100
95
+ sampleRate: 44100,
96
96
  });
97
97
 
98
98
  try {
99
- await websocket.connect();
99
+ await websocket.connect({
100
+ // If using Node.js, you can pass a custom WebSocket constructor, such as from `ws`.
101
+ // This is not needed for browser usage, so you can call connect() without any arguments.
102
+ WebSocket: WS,
103
+ });
100
104
  } catch (error) {
101
105
  console.error(`Failed to connect to Cartesia: ${error}`);
102
106
  }
@@ -1,3 +1,10 @@
1
+ import {
2
+ Client
3
+ } from "./chunk-FLWYXP5Z.js";
4
+ import {
5
+ CARTESIA_VERSION,
6
+ constructApiUrl
7
+ } from "./chunk-2BFEKY3F.js";
1
8
  import {
2
9
  base64ToArray,
3
10
  createMessageHandlerForContextId,
@@ -7,13 +14,6 @@ import {
7
14
  import {
8
15
  Source
9
16
  } from "./chunk-I5YVYTNK.js";
10
- import {
11
- Client
12
- } from "./chunk-FLWYXP5Z.js";
13
- import {
14
- CARTESIA_VERSION,
15
- constructApiUrl
16
- } from "./chunk-2BFEKY3F.js";
17
17
  import {
18
18
  __async,
19
19
  __privateAdd,
@@ -118,7 +118,8 @@ var WebSocket = class extends Client {
118
118
  streamCompleteController.abort();
119
119
  },
120
120
  {
121
- once: true
121
+ once: true,
122
+ signal: streamCompleteController.signal
122
123
  }
123
124
  );
124
125
  (_d = this.socket) == null ? void 0 : _d.addEventListener(
@@ -127,7 +128,8 @@ var WebSocket = class extends Client {
127
128
  streamCompleteController.abort();
128
129
  },
129
130
  {
130
- once: true
131
+ once: true,
132
+ signal: streamCompleteController.signal
131
133
  }
132
134
  );
133
135
  streamCompleteController.signal.addEventListener("abort", () => {
@@ -135,6 +137,7 @@ var WebSocket = class extends Client {
135
137
  if (timeoutId) {
136
138
  clearTimeout(timeoutId);
137
139
  }
140
+ emitter.clearListeners();
138
141
  });
139
142
  return __spreadProps(__spreadValues({
140
143
  source
@@ -175,16 +178,23 @@ var WebSocket = class extends Client {
175
178
  * @throws {Error} If the WebSocket fails to connect.
176
179
  */
177
180
  connect() {
178
- return __async(this, null, function* () {
181
+ return __async(this, arguments, function* (options = {}) {
182
+ if (__privateGet(this, _isConnected)) {
183
+ throw new Error("WebSocket is already connected.");
184
+ }
179
185
  const emitter = new Emittery();
180
- this.socket = new PartySocketWebSocket(() => __async(this, null, function* () {
181
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
182
- websocket: true
183
- });
184
- url.searchParams.set("api_key", yield this.apiKey());
185
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
186
- return url.toString();
187
- }));
186
+ this.socket = new PartySocketWebSocket(
187
+ () => __async(this, null, function* () {
188
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
189
+ websocket: true
190
+ });
191
+ url.searchParams.set("api_key", yield this.apiKey());
192
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
193
+ return url.toString();
194
+ }),
195
+ void 0,
196
+ options
197
+ );
188
198
  this.socket.binaryType = "arraybuffer";
189
199
  this.socket.onopen = () => {
190
200
  __privateSet(this, _isConnected, true);
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  WebSocket
3
- } from "./chunk-HQOC2S6C.js";
3
+ } from "./chunk-CJZSHIRI.js";
4
4
  import {
5
5
  Client
6
6
  } from "./chunk-FLWYXP5Z.js";
@@ -1,12 +1,12 @@
1
- import {
2
- TTS
3
- } from "./chunk-EYRYHK3X.js";
4
1
  import {
5
2
  Voices
6
3
  } from "./chunk-RJICGVPL.js";
7
4
  import {
8
5
  VoiceChanger
9
6
  } from "./chunk-WLEVU3HN.js";
7
+ import {
8
+ TTS
9
+ } from "./chunk-QT5LHJ3D.js";
10
10
  import {
11
11
  Client
12
12
  } from "./chunk-FLWYXP5Z.js";
package/dist/index.cjs CHANGED
@@ -465,7 +465,8 @@ var WebSocket = class extends Client {
465
465
  streamCompleteController.abort();
466
466
  },
467
467
  {
468
- once: true
468
+ once: true,
469
+ signal: streamCompleteController.signal
469
470
  }
470
471
  );
471
472
  (_d = this.socket) == null ? void 0 : _d.addEventListener(
@@ -474,7 +475,8 @@ var WebSocket = class extends Client {
474
475
  streamCompleteController.abort();
475
476
  },
476
477
  {
477
- once: true
478
+ once: true,
479
+ signal: streamCompleteController.signal
478
480
  }
479
481
  );
480
482
  streamCompleteController.signal.addEventListener("abort", () => {
@@ -482,6 +484,7 @@ var WebSocket = class extends Client {
482
484
  if (timeoutId) {
483
485
  clearTimeout(timeoutId);
484
486
  }
487
+ emitter.clearListeners();
485
488
  });
486
489
  return __spreadProps(__spreadValues({
487
490
  source
@@ -522,16 +525,23 @@ var WebSocket = class extends Client {
522
525
  * @throws {Error} If the WebSocket fails to connect.
523
526
  */
524
527
  connect() {
525
- return __async(this, null, function* () {
528
+ return __async(this, arguments, function* (options = {}) {
529
+ if (__privateGet(this, _isConnected)) {
530
+ throw new Error("WebSocket is already connected.");
531
+ }
526
532
  const emitter = new import_emittery2.default();
527
- this.socket = new import_partysocket.WebSocket(() => __async(this, null, function* () {
528
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
529
- websocket: true
530
- });
531
- url.searchParams.set("api_key", yield this.apiKey());
532
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
533
- return url.toString();
534
- }));
533
+ this.socket = new import_partysocket.WebSocket(
534
+ () => __async(this, null, function* () {
535
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
536
+ websocket: true
537
+ });
538
+ url.searchParams.set("api_key", yield this.apiKey());
539
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
540
+ return url.toString();
541
+ }),
542
+ void 0,
543
+ options
544
+ );
535
545
  this.socket.binaryType = "arraybuffer";
536
546
  this.socket.onopen = () => {
537
547
  __privateSet(this, _isConnected, true);
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { Cartesia, Cartesia as default } from './lib/index.cjs';
2
- export { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps } from './types/index.cjs';
2
+ export { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectOptions, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps } from './types/index.cjs';
3
3
  export { default as WebPlayer } from './tts/player.cjs';
4
4
  export { default as Source } from './tts/source.cjs';
5
5
  export { default as WebSocket } from './tts/websocket.cjs';
@@ -8,4 +8,5 @@ import './lib/client.cjs';
8
8
  import './voice-changer/index.cjs';
9
9
  import './voices/index.cjs';
10
10
  import 'emittery';
11
+ import 'partysocket/ws';
11
12
  import 'partysocket';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { Cartesia, Cartesia as default } from './lib/index.js';
2
- export { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps } from './types/index.js';
2
+ export { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectOptions, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps } from './types/index.js';
3
3
  export { default as WebPlayer } from './tts/player.js';
4
4
  export { default as Source } from './tts/source.js';
5
5
  export { default as WebSocket } from './tts/websocket.js';
@@ -8,4 +8,5 @@ import './lib/client.js';
8
8
  import './voice-changer/index.js';
9
9
  import './voices/index.js';
10
10
  import 'emittery';
11
+ import 'partysocket/ws';
11
12
  import 'partysocket';
package/dist/index.js CHANGED
@@ -1,22 +1,22 @@
1
- import {
2
- Player
3
- } from "./chunk-CSOXALSC.js";
1
+ import "./chunk-FXPGR372.js";
4
2
  import {
5
3
  Cartesia
6
- } from "./chunk-YUGIYUMT.js";
7
- import "./chunk-EYRYHK3X.js";
4
+ } from "./chunk-RYHHRY64.js";
5
+ import "./chunk-RJICGVPL.js";
6
+ import "./chunk-WLEVU3HN.js";
7
+ import "./chunk-QT5LHJ3D.js";
8
8
  import {
9
9
  WebSocket
10
- } from "./chunk-HQOC2S6C.js";
10
+ } from "./chunk-CJZSHIRI.js";
11
+ import "./chunk-FLWYXP5Z.js";
12
+ import "./chunk-2BFEKY3F.js";
13
+ import {
14
+ Player
15
+ } from "./chunk-CSOXALSC.js";
11
16
  import "./chunk-5SBAQNWQ.js";
12
17
  import {
13
18
  Source
14
19
  } from "./chunk-I5YVYTNK.js";
15
- import "./chunk-RJICGVPL.js";
16
- import "./chunk-FXPGR372.js";
17
- import "./chunk-WLEVU3HN.js";
18
- import "./chunk-FLWYXP5Z.js";
19
- import "./chunk-2BFEKY3F.js";
20
20
  import "./chunk-NJDRWDQ3.js";
21
21
  export {
22
22
  Cartesia,
@@ -1,5 +1,6 @@
1
1
  import { ClientOptions } from '../types/index.cjs';
2
2
  import 'emittery';
3
+ import 'partysocket/ws';
3
4
 
4
5
  declare class Client {
5
6
  apiKey: () => Promise<string>;
@@ -1,5 +1,6 @@
1
1
  import { ClientOptions } from '../types/index.js';
2
2
  import 'emittery';
3
+ import 'partysocket/ws';
3
4
 
4
5
  declare class Client {
5
6
  apiKey: () => Promise<string>;
@@ -448,7 +448,8 @@ var WebSocket = class extends Client {
448
448
  streamCompleteController.abort();
449
449
  },
450
450
  {
451
- once: true
451
+ once: true,
452
+ signal: streamCompleteController.signal
452
453
  }
453
454
  );
454
455
  (_d = this.socket) == null ? void 0 : _d.addEventListener(
@@ -457,7 +458,8 @@ var WebSocket = class extends Client {
457
458
  streamCompleteController.abort();
458
459
  },
459
460
  {
460
- once: true
461
+ once: true,
462
+ signal: streamCompleteController.signal
461
463
  }
462
464
  );
463
465
  streamCompleteController.signal.addEventListener("abort", () => {
@@ -465,6 +467,7 @@ var WebSocket = class extends Client {
465
467
  if (timeoutId) {
466
468
  clearTimeout(timeoutId);
467
469
  }
470
+ emitter.clearListeners();
468
471
  });
469
472
  return __spreadProps(__spreadValues({
470
473
  source
@@ -505,16 +508,23 @@ var WebSocket = class extends Client {
505
508
  * @throws {Error} If the WebSocket fails to connect.
506
509
  */
507
510
  connect() {
508
- return __async(this, null, function* () {
511
+ return __async(this, arguments, function* (options = {}) {
512
+ if (__privateGet(this, _isConnected)) {
513
+ throw new Error("WebSocket is already connected.");
514
+ }
509
515
  const emitter = new import_emittery2.default();
510
- this.socket = new import_partysocket.WebSocket(() => __async(this, null, function* () {
511
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
512
- websocket: true
513
- });
514
- url.searchParams.set("api_key", yield this.apiKey());
515
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
516
- return url.toString();
517
- }));
516
+ this.socket = new import_partysocket.WebSocket(
517
+ () => __async(this, null, function* () {
518
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
519
+ websocket: true
520
+ });
521
+ url.searchParams.set("api_key", yield this.apiKey());
522
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
523
+ return url.toString();
524
+ }),
525
+ void 0,
526
+ options
527
+ );
518
528
  this.socket.binaryType = "arraybuffer";
519
529
  this.socket.onopen = () => {
520
530
  __privateSet(this, _isConnected, true);
@@ -7,6 +7,7 @@ import '../tts/websocket.cjs';
7
7
  import 'emittery';
8
8
  import 'partysocket';
9
9
  import '../tts/source.cjs';
10
+ import 'partysocket/ws';
10
11
 
11
12
  declare class Cartesia extends Client {
12
13
  tts: TTS;
@@ -7,6 +7,7 @@ import '../tts/websocket.js';
7
7
  import 'emittery';
8
8
  import 'partysocket';
9
9
  import '../tts/source.js';
10
+ import 'partysocket/ws';
10
11
 
11
12
  declare class Cartesia extends Client {
12
13
  tts: TTS;
package/dist/lib/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  Cartesia
3
- } from "../chunk-YUGIYUMT.js";
4
- import "../chunk-EYRYHK3X.js";
5
- import "../chunk-HQOC2S6C.js";
6
- import "../chunk-5SBAQNWQ.js";
7
- import "../chunk-I5YVYTNK.js";
3
+ } from "../chunk-RYHHRY64.js";
8
4
  import "../chunk-RJICGVPL.js";
9
5
  import "../chunk-WLEVU3HN.js";
6
+ import "../chunk-QT5LHJ3D.js";
7
+ import "../chunk-CJZSHIRI.js";
10
8
  import "../chunk-FLWYXP5Z.js";
11
9
  import "../chunk-2BFEKY3F.js";
10
+ import "../chunk-5SBAQNWQ.js";
11
+ import "../chunk-I5YVYTNK.js";
12
12
  import "../chunk-NJDRWDQ3.js";
13
13
  export {
14
14
  Cartesia
@@ -462,7 +462,8 @@ var WebSocket = class extends Client {
462
462
  streamCompleteController.abort();
463
463
  },
464
464
  {
465
- once: true
465
+ once: true,
466
+ signal: streamCompleteController.signal
466
467
  }
467
468
  );
468
469
  (_d = this.socket) == null ? void 0 : _d.addEventListener(
@@ -471,7 +472,8 @@ var WebSocket = class extends Client {
471
472
  streamCompleteController.abort();
472
473
  },
473
474
  {
474
- once: true
475
+ once: true,
476
+ signal: streamCompleteController.signal
475
477
  }
476
478
  );
477
479
  streamCompleteController.signal.addEventListener("abort", () => {
@@ -479,6 +481,7 @@ var WebSocket = class extends Client {
479
481
  if (timeoutId) {
480
482
  clearTimeout(timeoutId);
481
483
  }
484
+ emitter.clearListeners();
482
485
  });
483
486
  return __spreadProps(__spreadValues({
484
487
  source
@@ -519,16 +522,23 @@ var WebSocket = class extends Client {
519
522
  * @throws {Error} If the WebSocket fails to connect.
520
523
  */
521
524
  connect() {
522
- return __async(this, null, function* () {
525
+ return __async(this, arguments, function* (options = {}) {
526
+ if (__privateGet(this, _isConnected)) {
527
+ throw new Error("WebSocket is already connected.");
528
+ }
523
529
  const emitter = new import_emittery2.default();
524
- this.socket = new import_partysocket.WebSocket(() => __async(this, null, function* () {
525
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
526
- websocket: true
527
- });
528
- url.searchParams.set("api_key", yield this.apiKey());
529
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
530
- return url.toString();
531
- }));
530
+ this.socket = new import_partysocket.WebSocket(
531
+ () => __async(this, null, function* () {
532
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
533
+ websocket: true
534
+ });
535
+ url.searchParams.set("api_key", yield this.apiKey());
536
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
537
+ return url.toString();
538
+ }),
539
+ void 0,
540
+ options
541
+ );
532
542
  this.socket.binaryType = "arraybuffer";
533
543
  this.socket.onopen = () => {
534
544
  __privateSet(this, _isConnected, true);
@@ -1,6 +1,7 @@
1
1
  import Source from '../tts/source.cjs';
2
2
  import { StreamRequest } from '../types/index.cjs';
3
3
  import 'emittery';
4
+ import 'partysocket/ws';
4
5
 
5
6
  type UseTTSOptions = {
6
7
  apiKey: string | (() => Promise<string>) | null;
@@ -1,6 +1,7 @@
1
1
  import Source from '../tts/source.js';
2
2
  import { StreamRequest } from '../types/index.js';
3
3
  import 'emittery';
4
+ import 'partysocket/ws';
4
5
 
5
6
  type UseTTSOptions = {
6
7
  apiKey: string | (() => Promise<string>) | null;
@@ -1,20 +1,20 @@
1
- import {
2
- Player
3
- } from "../chunk-CSOXALSC.js";
4
1
  import {
5
2
  Cartesia
6
- } from "../chunk-YUGIYUMT.js";
7
- import "../chunk-EYRYHK3X.js";
8
- import "../chunk-HQOC2S6C.js";
9
- import "../chunk-5SBAQNWQ.js";
10
- import "../chunk-I5YVYTNK.js";
3
+ } from "../chunk-RYHHRY64.js";
11
4
  import "../chunk-RJICGVPL.js";
5
+ import "../chunk-WLEVU3HN.js";
12
6
  import {
13
7
  pingServer
14
8
  } from "../chunk-LKKWJLUG.js";
15
- import "../chunk-WLEVU3HN.js";
9
+ import "../chunk-QT5LHJ3D.js";
10
+ import "../chunk-CJZSHIRI.js";
16
11
  import "../chunk-FLWYXP5Z.js";
17
12
  import "../chunk-2BFEKY3F.js";
13
+ import {
14
+ Player
15
+ } from "../chunk-CSOXALSC.js";
16
+ import "../chunk-5SBAQNWQ.js";
17
+ import "../chunk-I5YVYTNK.js";
18
18
  import {
19
19
  __async
20
20
  } from "../chunk-NJDRWDQ3.js";
@@ -448,7 +448,8 @@ var WebSocket = class extends Client {
448
448
  streamCompleteController.abort();
449
449
  },
450
450
  {
451
- once: true
451
+ once: true,
452
+ signal: streamCompleteController.signal
452
453
  }
453
454
  );
454
455
  (_d = this.socket) == null ? void 0 : _d.addEventListener(
@@ -457,7 +458,8 @@ var WebSocket = class extends Client {
457
458
  streamCompleteController.abort();
458
459
  },
459
460
  {
460
- once: true
461
+ once: true,
462
+ signal: streamCompleteController.signal
461
463
  }
462
464
  );
463
465
  streamCompleteController.signal.addEventListener("abort", () => {
@@ -465,6 +467,7 @@ var WebSocket = class extends Client {
465
467
  if (timeoutId) {
466
468
  clearTimeout(timeoutId);
467
469
  }
470
+ emitter.clearListeners();
468
471
  });
469
472
  return __spreadProps(__spreadValues({
470
473
  source
@@ -505,16 +508,23 @@ var WebSocket = class extends Client {
505
508
  * @throws {Error} If the WebSocket fails to connect.
506
509
  */
507
510
  connect() {
508
- return __async(this, null, function* () {
511
+ return __async(this, arguments, function* (options = {}) {
512
+ if (__privateGet(this, _isConnected)) {
513
+ throw new Error("WebSocket is already connected.");
514
+ }
509
515
  const emitter = new import_emittery2.default();
510
- this.socket = new import_partysocket.WebSocket(() => __async(this, null, function* () {
511
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
512
- websocket: true
513
- });
514
- url.searchParams.set("api_key", yield this.apiKey());
515
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
516
- return url.toString();
517
- }));
516
+ this.socket = new import_partysocket.WebSocket(
517
+ () => __async(this, null, function* () {
518
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
519
+ websocket: true
520
+ });
521
+ url.searchParams.set("api_key", yield this.apiKey());
522
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
523
+ return url.toString();
524
+ }),
525
+ void 0,
526
+ options
527
+ );
518
528
  this.socket.binaryType = "arraybuffer";
519
529
  this.socket.onopen = () => {
520
530
  __privateSet(this, _isConnected, true);
@@ -2,6 +2,7 @@ import { Client } from '../lib/client.cjs';
2
2
  import { WebSocketOptions, BytesRequest } from '../types/index.cjs';
3
3
  import WebSocket from './websocket.cjs';
4
4
  import 'emittery';
5
+ import 'partysocket/ws';
5
6
  import 'partysocket';
6
7
  import './source.cjs';
7
8
 
@@ -2,6 +2,7 @@ import { Client } from '../lib/client.js';
2
2
  import { WebSocketOptions, BytesRequest } from '../types/index.js';
3
3
  import WebSocket from './websocket.js';
4
4
  import 'emittery';
5
+ import 'partysocket/ws';
5
6
  import 'partysocket';
6
7
  import './source.js';
7
8
 
package/dist/tts/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  TTS
3
- } from "../chunk-EYRYHK3X.js";
4
- import "../chunk-HQOC2S6C.js";
5
- import "../chunk-5SBAQNWQ.js";
6
- import "../chunk-I5YVYTNK.js";
3
+ } from "../chunk-QT5LHJ3D.js";
4
+ import "../chunk-CJZSHIRI.js";
7
5
  import "../chunk-FLWYXP5Z.js";
8
6
  import "../chunk-2BFEKY3F.js";
7
+ import "../chunk-5SBAQNWQ.js";
8
+ import "../chunk-I5YVYTNK.js";
9
9
  import "../chunk-NJDRWDQ3.js";
10
10
  export {
11
11
  TTS as default
@@ -1,6 +1,7 @@
1
1
  import Source from './source.cjs';
2
2
  import 'emittery';
3
3
  import '../types/index.cjs';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class Player {
6
7
  #private;
@@ -1,6 +1,7 @@
1
1
  import Source from './source.js';
2
2
  import 'emittery';
3
3
  import '../types/index.js';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class Player {
6
7
  #private;
@@ -1,5 +1,6 @@
1
1
  import * as emittery from 'emittery';
2
2
  import { Encoding, SourceEventData, TypedArray } from '../types/index.cjs';
3
+ import 'partysocket/ws';
3
4
 
4
5
  type EncodingInfo = {
5
6
  arrayType: Float32ArrayConstructor | Int16ArrayConstructor | Uint8ArrayConstructor;
@@ -1,5 +1,6 @@
1
1
  import * as emittery from 'emittery';
2
2
  import { Encoding, SourceEventData, TypedArray } from '../types/index.js';
3
+ import 'partysocket/ws';
3
4
 
4
5
  type EncodingInfo = {
5
6
  arrayType: Float32ArrayConstructor | Int16ArrayConstructor | Uint8ArrayConstructor;
@@ -1,5 +1,6 @@
1
1
  import emittery__default from 'emittery';
2
2
  import { Chunk, TypedArray, WebSocketResponse, Sentinel, EmitteryCallbacks } from '../types/index.cjs';
3
+ import 'partysocket/ws';
3
4
 
4
5
  /**
5
6
  * Convert base64-encoded audio buffer(s) to a TypedArray.
@@ -1,5 +1,6 @@
1
1
  import emittery__default from 'emittery';
2
2
  import { Chunk, TypedArray, WebSocketResponse, Sentinel, EmitteryCallbacks } from '../types/index.js';
3
+ import 'partysocket/ws';
3
4
 
4
5
  /**
5
6
  * Convert base64-encoded audio buffer(s) to a TypedArray.
@@ -446,7 +446,8 @@ var WebSocket = class extends Client {
446
446
  streamCompleteController.abort();
447
447
  },
448
448
  {
449
- once: true
449
+ once: true,
450
+ signal: streamCompleteController.signal
450
451
  }
451
452
  );
452
453
  (_d = this.socket) == null ? void 0 : _d.addEventListener(
@@ -455,7 +456,8 @@ var WebSocket = class extends Client {
455
456
  streamCompleteController.abort();
456
457
  },
457
458
  {
458
- once: true
459
+ once: true,
460
+ signal: streamCompleteController.signal
459
461
  }
460
462
  );
461
463
  streamCompleteController.signal.addEventListener("abort", () => {
@@ -463,6 +465,7 @@ var WebSocket = class extends Client {
463
465
  if (timeoutId) {
464
466
  clearTimeout(timeoutId);
465
467
  }
468
+ emitter.clearListeners();
466
469
  });
467
470
  return __spreadProps(__spreadValues({
468
471
  source
@@ -503,16 +506,23 @@ var WebSocket = class extends Client {
503
506
  * @throws {Error} If the WebSocket fails to connect.
504
507
  */
505
508
  connect() {
506
- return __async(this, null, function* () {
509
+ return __async(this, arguments, function* (options = {}) {
510
+ if (__privateGet(this, _isConnected)) {
511
+ throw new Error("WebSocket is already connected.");
512
+ }
507
513
  const emitter = new import_emittery2.default();
508
- this.socket = new import_partysocket.WebSocket(() => __async(this, null, function* () {
509
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
510
- websocket: true
511
- });
512
- url.searchParams.set("api_key", yield this.apiKey());
513
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
514
- return url.toString();
515
- }));
514
+ this.socket = new import_partysocket.WebSocket(
515
+ () => __async(this, null, function* () {
516
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
517
+ websocket: true
518
+ });
519
+ url.searchParams.set("api_key", yield this.apiKey());
520
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
521
+ return url.toString();
522
+ }),
523
+ void 0,
524
+ options
525
+ );
516
526
  this.socket.binaryType = "arraybuffer";
517
527
  this.socket.onopen = () => {
518
528
  __privateSet(this, _isConnected, true);
@@ -1,8 +1,9 @@
1
1
  import * as emittery from 'emittery';
2
2
  import { WebSocket as WebSocket$1 } from 'partysocket';
3
3
  import { Client } from '../lib/client.cjs';
4
- import { WebSocketOptions, StreamRequest, StreamOptions, WordTimestamps, ContinueRequest, EmitteryCallbacks, ConnectionEventData } from '../types/index.cjs';
4
+ import { WebSocketOptions, StreamRequest, StreamOptions, WordTimestamps, ContinueRequest, ConnectOptions, EmitteryCallbacks, ConnectionEventData } from '../types/index.cjs';
5
5
  import Source from './source.cjs';
6
+ import 'partysocket/ws';
6
7
 
7
8
  declare class WebSocket extends Client {
8
9
  #private;
@@ -59,7 +60,7 @@ declare class WebSocket extends Client {
59
60
  * @returns A promise that resolves when the WebSocket is connected.
60
61
  * @throws {Error} If the WebSocket fails to connect.
61
62
  */
62
- connect(): Promise<EmitteryCallbacks<ConnectionEventData>>;
63
+ connect(options?: ConnectOptions): Promise<EmitteryCallbacks<ConnectionEventData>>;
63
64
  /**
64
65
  * Disconnect from the Cartesia streaming WebSocket.
65
66
  */
@@ -1,8 +1,9 @@
1
1
  import * as emittery from 'emittery';
2
2
  import { WebSocket as WebSocket$1 } from 'partysocket';
3
3
  import { Client } from '../lib/client.js';
4
- import { WebSocketOptions, StreamRequest, StreamOptions, WordTimestamps, ContinueRequest, EmitteryCallbacks, ConnectionEventData } from '../types/index.js';
4
+ import { WebSocketOptions, StreamRequest, StreamOptions, WordTimestamps, ContinueRequest, ConnectOptions, EmitteryCallbacks, ConnectionEventData } from '../types/index.js';
5
5
  import Source from './source.js';
6
+ import 'partysocket/ws';
6
7
 
7
8
  declare class WebSocket extends Client {
8
9
  #private;
@@ -59,7 +60,7 @@ declare class WebSocket extends Client {
59
60
  * @returns A promise that resolves when the WebSocket is connected.
60
61
  * @throws {Error} If the WebSocket fails to connect.
61
62
  */
62
- connect(): Promise<EmitteryCallbacks<ConnectionEventData>>;
63
+ connect(options?: ConnectOptions): Promise<EmitteryCallbacks<ConnectionEventData>>;
63
64
  /**
64
65
  * Disconnect from the Cartesia streaming WebSocket.
65
66
  */
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  WebSocket
3
- } from "../chunk-HQOC2S6C.js";
4
- import "../chunk-5SBAQNWQ.js";
5
- import "../chunk-I5YVYTNK.js";
3
+ } from "../chunk-CJZSHIRI.js";
6
4
  import "../chunk-FLWYXP5Z.js";
7
5
  import "../chunk-2BFEKY3F.js";
6
+ import "../chunk-5SBAQNWQ.js";
7
+ import "../chunk-I5YVYTNK.js";
8
8
  import "../chunk-NJDRWDQ3.js";
9
9
  export {
10
10
  WebSocket as default
@@ -1,4 +1,5 @@
1
1
  import emittery__default from 'emittery';
2
+ import { Options } from 'partysocket/ws';
2
3
 
3
4
  interface ClientOptions {
4
5
  apiKey?: string | (() => Promise<string>);
@@ -153,6 +154,7 @@ type WebSocketOptions = {
153
154
  encoding?: string;
154
155
  sampleRate: number;
155
156
  };
157
+ type ConnectOptions = Pick<Options, "WebSocket">;
156
158
  type SourceEventData = {
157
159
  enqueue: never;
158
160
  close: never;
@@ -162,4 +164,4 @@ type SourceEventData = {
162
164
  type TypedArray = Float32Array | Int16Array | Uint8Array;
163
165
  type Encoding = "pcm_f32le" | "pcm_s16le" | "pcm_alaw" | "pcm_mulaw";
164
166
 
165
- export type { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps };
167
+ export type { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectOptions, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps };
@@ -1,4 +1,5 @@
1
1
  import emittery__default from 'emittery';
2
+ import { Options } from 'partysocket/ws';
2
3
 
3
4
  interface ClientOptions {
4
5
  apiKey?: string | (() => Promise<string>);
@@ -153,6 +154,7 @@ type WebSocketOptions = {
153
154
  encoding?: string;
154
155
  sampleRate: number;
155
156
  };
157
+ type ConnectOptions = Pick<Options, "WebSocket">;
156
158
  type SourceEventData = {
157
159
  enqueue: never;
158
160
  close: never;
@@ -162,4 +164,4 @@ type SourceEventData = {
162
164
  type TypedArray = Float32Array | Int16Array | Uint8Array;
163
165
  type Encoding = "pcm_f32le" | "pcm_s16le" | "pcm_alaw" | "pcm_mulaw";
164
166
 
165
- export type { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps };
167
+ export type { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectOptions, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps };
@@ -1,6 +1,7 @@
1
1
  import { Client } from '../lib/client.cjs';
2
2
  import { VoiceChangerOptions, VoiceChangerBytesResponse } from '../types/index.cjs';
3
3
  import 'emittery';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class VoiceChanger extends Client {
6
7
  bytes(options: VoiceChangerOptions): Promise<VoiceChangerBytesResponse>;
@@ -1,6 +1,7 @@
1
1
  import { Client } from '../lib/client.js';
2
2
  import { VoiceChangerOptions, VoiceChangerBytesResponse } from '../types/index.js';
3
3
  import 'emittery';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class VoiceChanger extends Client {
6
7
  bytes(options: VoiceChangerOptions): Promise<VoiceChangerBytesResponse>;
@@ -1,6 +1,7 @@
1
1
  import { Client } from '../lib/client.cjs';
2
2
  import { Voice, CreateVoice, UpdateVoice, CloneOptions, CloneResponse, MixVoicesOptions, MixVoicesResponse, LocalizeOptions, LocalizeResponse } from '../types/index.cjs';
3
3
  import 'emittery';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class Voices extends Client {
6
7
  list(): Promise<Voice[]>;
@@ -1,6 +1,7 @@
1
1
  import { Client } from '../lib/client.js';
2
2
  import { Voice, CreateVoice, UpdateVoice, CloneOptions, CloneResponse, MixVoicesOptions, MixVoicesResponse, LocalizeOptions, LocalizeResponse } from '../types/index.js';
3
3
  import 'emittery';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class Voices extends Client {
6
7
  list(): Promise<Voice[]>;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "name": "Cartesia",
5
5
  "url": "https://cartesia.ai"
6
6
  },
7
- "version": "1.2.1",
7
+ "version": "1.3.0",
8
8
  "description": "Client for the Cartesia API.",
9
9
  "type": "module",
10
10
  "module": "./dist/index.js",
@@ -5,6 +5,7 @@ import { Client } from "../lib/client";
5
5
  import { CARTESIA_VERSION, constructApiUrl } from "../lib/constants";
6
6
  import type {
7
7
  ConnectionEventData,
8
+ ConnectOptions,
8
9
  ContinueRequest,
9
10
  EmitteryCallbacks,
10
11
  StreamOptions,
@@ -127,6 +128,7 @@ export default class WebSocket extends Client {
127
128
  },
128
129
  {
129
130
  once: true,
131
+ signal: streamCompleteController.signal,
130
132
  },
131
133
  );
132
134
  this.socket?.addEventListener(
@@ -136,6 +138,7 @@ export default class WebSocket extends Client {
136
138
  },
137
139
  {
138
140
  once: true,
141
+ signal: streamCompleteController.signal,
139
142
  },
140
143
  );
141
144
  streamCompleteController.signal.addEventListener("abort", () => {
@@ -143,6 +146,7 @@ export default class WebSocket extends Client {
143
146
  if (timeoutId) {
144
147
  clearTimeout(timeoutId);
145
148
  }
149
+ emitter.clearListeners();
146
150
  });
147
151
 
148
152
  return {
@@ -203,16 +207,24 @@ export default class WebSocket extends Client {
203
207
  * @returns A promise that resolves when the WebSocket is connected.
204
208
  * @throws {Error} If the WebSocket fails to connect.
205
209
  */
206
- async connect() {
210
+ async connect(options: ConnectOptions = {}) {
211
+ if (this.#isConnected) {
212
+ throw new Error("WebSocket is already connected.");
213
+ }
214
+
207
215
  const emitter = new Emittery<ConnectionEventData>();
208
- this.socket = new PartySocketWebSocket(async () => {
209
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
210
- websocket: true,
211
- });
212
- url.searchParams.set("api_key", await this.apiKey());
213
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
214
- return url.toString();
215
- });
216
+ this.socket = new PartySocketWebSocket(
217
+ async () => {
218
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
219
+ websocket: true,
220
+ });
221
+ url.searchParams.set("api_key", await this.apiKey());
222
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
223
+ return url.toString();
224
+ },
225
+ undefined,
226
+ options,
227
+ );
216
228
  this.socket.binaryType = "arraybuffer";
217
229
 
218
230
  this.socket.onopen = () => {
@@ -1,4 +1,5 @@
1
1
  import type Emittery from "emittery";
2
+ import type { Options } from "partysocket/ws";
2
3
 
3
4
  export interface ClientOptions {
4
5
  apiKey?: string | (() => Promise<string>);
@@ -212,6 +213,8 @@ export type WebSocketOptions = {
212
213
  sampleRate: number;
213
214
  };
214
215
 
216
+ export type ConnectOptions = Pick<Options, "WebSocket">;
217
+
215
218
  export type SourceEventData = {
216
219
  enqueue: never;
217
220
  close: never;