@cartesia/cartesia-js 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,7 +8,7 @@ import type { StreamRequest } from "../types";
8
8
  import { pingServer } from "./utils";
9
9
 
10
10
  export type UseTTSOptions = {
11
- apiKey: string | null;
11
+ apiKey: string | (() => Promise<string>) | null;
12
12
  baseUrl?: string;
13
13
  sampleRate: number;
14
14
  onError?: (error: Error) => void;
@@ -172,14 +172,18 @@ export default class WebSocket extends Client {
172
172
  * @returns A promise that resolves when the WebSocket is connected.
173
173
  * @throws {Error} If the WebSocket fails to connect.
174
174
  */
175
- connect() {
176
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
177
- websocket: true,
178
- });
179
- url.searchParams.set("api_key", this.apiKey);
180
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
175
+ async connect() {
181
176
  const emitter = new Emittery<ConnectionEventData>();
182
- this.socket = new PartySocketWebSocket(url.toString());
177
+ this.socket = new PartySocketWebSocket(async () => {
178
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
179
+ websocket: true,
180
+ });
181
+ url.searchParams.set("api_key", await this.apiKey());
182
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
183
+ return url.toString();
184
+ });
185
+ this.socket.binaryType = "arraybuffer";
186
+
183
187
  this.socket.onopen = () => {
184
188
  this.#isConnected = true;
185
189
  emitter.emit("open");
@@ -1,7 +1,7 @@
1
1
  import type Emittery from "emittery";
2
2
 
3
3
  export interface ClientOptions {
4
- apiKey?: string;
4
+ apiKey?: string | (() => Promise<string>);
5
5
  baseUrl?: string;
6
6
  }
7
7
 
@@ -16,11 +16,11 @@ export type ConnectionEventData = {
16
16
 
17
17
  export type VoiceSpecifier =
18
18
  | {
19
- mode: "id";
19
+ mode?: "id";
20
20
  id: string;
21
21
  }
22
22
  | {
23
- mode: "embedding";
23
+ mode?: "embedding";
24
24
  embedding: number[];
25
25
  };
26
26
 
@@ -35,7 +35,7 @@ export type EmotionControl = Emotion | `${Emotion}:${Intensity}`;
35
35
 
36
36
  export type VoiceOptions = VoiceSpecifier & {
37
37
  __experimental_controls?: {
38
- speed?: "slowest" | "slow" | "normal" | "fast" | "fastest";
38
+ speed?: "slowest" | "slow" | "normal" | "fast" | "fastest" | number;
39
39
  emotion?: EmotionControl[];
40
40
  };
41
41
  };
@@ -104,12 +104,24 @@ export type CloneOptions =
104
104
  | {
105
105
  mode: "url";
106
106
  link: string;
107
+ enhance?: boolean;
107
108
  }
108
109
  | {
109
110
  mode: "clip";
110
111
  clip: Blob;
112
+ enhance?: boolean;
111
113
  };
112
114
 
115
+ export interface VoiceToMix {
116
+ id?: string;
117
+ embedding?: number[];
118
+ weight: number;
119
+ }
120
+
121
+ export interface MixVoicesOptions {
122
+ voices: VoiceToMix[];
123
+ }
124
+
113
125
  export type Voice = {
114
126
  id: string;
115
127
  name: string;
@@ -118,15 +130,24 @@ export type Voice = {
118
130
  is_public: boolean;
119
131
  user_id: string;
120
132
  created_at: string;
133
+ language: string;
121
134
  };
122
135
 
123
136
  export type CreateVoice = Pick<Voice, "name" | "description" | "embedding"> &
124
137
  Partial<Omit<Voice, "name" | "description" | "embedding">>;
125
138
 
139
+ export type UpdateVoice = Partial<
140
+ Pick<Voice, "name" | "description" | "embedding">
141
+ >;
142
+
126
143
  export type CloneResponse = {
127
144
  embedding: number[];
128
145
  };
129
146
 
147
+ export type MixVoicesResponse = {
148
+ embedding: number[];
149
+ };
150
+
130
151
  export type WebSocketOptions = {
131
152
  container?: string;
132
153
  encoding?: string;
@@ -1,41 +1,50 @@
1
1
  import { Client } from "../lib/client";
2
- import type { CloneOptions, CloneResponse, CreateVoice, Voice } from "../types";
2
+ import type {
3
+ CloneOptions,
4
+ CloneResponse,
5
+ CreateVoice,
6
+ MixVoicesOptions,
7
+ MixVoicesResponse,
8
+ UpdateVoice,
9
+ Voice,
10
+ } from "../types";
3
11
 
4
12
  export default class Voices extends Client {
5
13
  async list(): Promise<Voice[]> {
6
- const response = await this.fetch("/voices");
14
+ const response = await this._fetch("/voices");
7
15
  return response.json();
8
16
  }
9
17
 
10
18
  async get(voiceId: string): Promise<Voice> {
11
- const response = await this.fetch(`/voices/${voiceId}`);
19
+ const response = await this._fetch(`/voices/${voiceId}`);
12
20
  return response.json();
13
21
  }
14
22
 
15
23
  async create(voice: CreateVoice): Promise<Voice> {
16
- const response = await this.fetch("/voices", {
24
+ const response = await this._fetch("/voices", {
17
25
  method: "POST",
18
26
  body: JSON.stringify(voice),
19
27
  });
20
28
  return response.json() as Promise<Voice>;
21
29
  }
22
30
 
23
- async clone(options: CloneOptions): Promise<CloneResponse> {
24
- if (options.mode === "url") {
25
- const response = await this.fetch(
26
- `/voices/clone/url?link=${options.link}`,
27
- {
28
- method: "POST",
29
- },
30
- );
31
- return response.json();
32
- }
31
+ async update(id: string, voice: UpdateVoice): Promise<Voice> {
32
+ const response = await this._fetch(`/voices/${id}`, {
33
+ method: "PATCH",
34
+ body: JSON.stringify(voice),
35
+ });
36
+ return response.json() as Promise<Voice>;
37
+ }
33
38
 
39
+ async clone(options: CloneOptions): Promise<CloneResponse> {
34
40
  if (options.mode === "clip") {
35
41
  const formData = new FormData();
36
42
  formData.append("clip", options.clip);
43
+ if (options.enhance !== undefined) {
44
+ formData.append("enhance", options.enhance.toString());
45
+ }
37
46
 
38
- const response = await this.fetch("/voices/clone/clip", {
47
+ const response = await this._fetch("/voices/clone/clip", {
39
48
  method: "POST",
40
49
  body: formData,
41
50
  });
@@ -44,4 +53,15 @@ export default class Voices extends Client {
44
53
 
45
54
  throw new Error("Invalid mode for clone()");
46
55
  }
56
+
57
+ async mix(options: MixVoicesOptions): Promise<MixVoicesResponse> {
58
+ const request: MixVoicesOptions = options;
59
+
60
+ const response = await this._fetch("/voices/mix", {
61
+ method: "POST",
62
+ body: JSON.stringify(request),
63
+ });
64
+
65
+ return response.json() as Promise<MixVoicesResponse>;
66
+ }
47
67
  }
@@ -1,34 +0,0 @@
1
- import {
2
- BASE_URL,
3
- CARTESIA_VERSION,
4
- constructApiUrl
5
- } from "./chunk-2BFEKY3F.js";
6
- import {
7
- __spreadProps,
8
- __spreadValues
9
- } from "./chunk-GHY2WEOK.js";
10
-
11
- // src/lib/client.ts
12
- import fetch from "cross-fetch";
13
- var Client = class {
14
- constructor(options = {}) {
15
- if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
16
- throw new Error("Missing Cartesia API key.");
17
- }
18
- this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
19
- this.baseUrl = options.baseUrl || BASE_URL;
20
- }
21
- fetch(path, options = {}) {
22
- const url = constructApiUrl(this.baseUrl, path);
23
- return fetch(url.toString(), __spreadProps(__spreadValues({}, options), {
24
- headers: __spreadValues({
25
- "X-API-Key": this.apiKey,
26
- "Cartesia-Version": CARTESIA_VERSION
27
- }, options.headers)
28
- }));
29
- }
30
- };
31
-
32
- export {
33
- Client
34
- };
@@ -1,58 +0,0 @@
1
- import {
2
- Client
3
- } from "./chunk-5M33ZF3Y.js";
4
- import {
5
- __async
6
- } from "./chunk-GHY2WEOK.js";
7
-
8
- // src/voices/index.ts
9
- var Voices = class extends Client {
10
- list() {
11
- return __async(this, null, function* () {
12
- const response = yield this.fetch("/voices");
13
- return response.json();
14
- });
15
- }
16
- get(voiceId) {
17
- return __async(this, null, function* () {
18
- const response = yield this.fetch(`/voices/${voiceId}`);
19
- return response.json();
20
- });
21
- }
22
- create(voice) {
23
- return __async(this, null, function* () {
24
- const response = yield this.fetch("/voices", {
25
- method: "POST",
26
- body: JSON.stringify(voice)
27
- });
28
- return response.json();
29
- });
30
- }
31
- clone(options) {
32
- return __async(this, null, function* () {
33
- if (options.mode === "url") {
34
- const response = yield this.fetch(
35
- `/voices/clone/url?link=${options.link}`,
36
- {
37
- method: "POST"
38
- }
39
- );
40
- return response.json();
41
- }
42
- if (options.mode === "clip") {
43
- const formData = new FormData();
44
- formData.append("clip", options.clip);
45
- const response = yield this.fetch("/voices/clone/clip", {
46
- method: "POST",
47
- body: formData
48
- });
49
- return response.json();
50
- }
51
- throw new Error("Invalid mode for clone()");
52
- });
53
- }
54
- };
55
-
56
- export {
57
- Voices
58
- };