Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New clone method #15

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Cartesia",
"url": "https://cartesia.ai"
},
"version": "1.3.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this to be alpha? Or can we release it as 1.4.0 with backward compat with the old clone method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the types are backwards compatible! I spent like an hour on that :P

"version": "1.4.0",
"description": "Client for the Cartesia API.",
"type": "module",
"module": "./dist/index.js",
Expand Down
29 changes: 21 additions & 8 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,35 @@ export type EmitteryCallbacks<T> = {
events: Emittery<T>["events"];
};

export type CloneOptions =
export type CloneOptions = {
mode: "clip";
clip: Blob;
enhance?: boolean;
};

export type CloneVoiceOptions =
| {
mode: "url";
link: string;
mode: "stability";
clip: Blob;
enhance?: boolean;
name: string;
description: string;
language: Language;
}
| {
mode: "clip";
mode: "similarity";
clip: Blob;
enhance?: boolean;
name: string;
description: string;
language: Language;
transcript?: string;
};

export type CloneResponse = {
embedding: number[];
};

export type VoiceChangerOptions = {
clip: File;
voice: { id: string }; // match VoiceSpecifier shape, but only id is supported for now
Expand Down Expand Up @@ -191,10 +208,6 @@ export type UpdateVoice = Partial<
Pick<Voice, "name" | "description" | "embedding">
>;

export type CloneResponse = {
embedding: number[];
};

export type VoiceChangerBytesResponse = {
buffer: ArrayBuffer;
};
Expand Down
37 changes: 35 additions & 2 deletions src/voices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Client } from "../lib/client";
import type {
CloneOptions,
CloneResponse,
CloneVoiceOptions,
CreateVoice,
LocalizeOptions,
LocalizeResponse,
Expand Down Expand Up @@ -38,7 +39,10 @@ export default class Voices extends Client {
return response.json() as Promise<Voice>;
}

async clone(options: CloneOptions): Promise<CloneResponse> {
async clone(options: CloneOptions): Promise<CloneResponse>
async clone(options: CloneVoiceOptions): Promise<Voice>
async clone(options: CloneOptions | CloneVoiceOptions): Promise<CloneResponse | Voice> {
// First: handle old clip mode/endpoint
if (options.mode === "clip") {
const formData = new FormData();
formData.append("clip", options.clip);
Expand All @@ -53,7 +57,36 @@ export default class Voices extends Client {
return response.json();
}

throw new Error("Invalid mode for clone()");
const formData = new FormData();
formData.append("clip", options.clip);
formData.append("mode", options.mode);
formData.append("name", options.name);
formData.append("description", options.description);
formData.append("language", options.language);
if (options.enhance !== undefined) {
formData.append("enhance", options.enhance.toString());
}
if (options.mode === "similarity") {
if (options.transcript) {
formData.append("transcript", options.transcript);
}
}

const response = await this._fetch("/voices/clone", {
method: "POST",
body: formData,
});

if (!response.ok) {
if (response.headers.get("content-type")?.includes("application/json")) {
const errorData = await response.json();
throw new Error(errorData.message || "Clone voice failed");
}
const errorText = await response.text();
throw new Error(errorText || "Clone voice failed");
}

return response.json() as Promise<Voice>;
}

async mix(options: MixVoicesOptions): Promise<MixVoicesResponse> {
Expand Down