API を呼び出す
// hello.js
import { CartesiaClient } from "@cartesia/cartesia-js";
import fs from "node:fs";
import { spawn } from "node:child_process";
import process from "node:process";
if (!process.env.CARTESIA_API_KEY) {
throw new Error("CARTESIA_API_KEY is not set");
}
// Set up the client.
const client = new CartesiaClient({
apiKey: process.env.CARTESIA_API_KEY,
});
// Make the API call.
const response = await client.tts.bytes({
modelId: "sonic-2",
voice: {
mode: "id",
id: "a0e99841-438c-4a64-b679-ae501e7d6091",
},
outputFormat: {
container: "wav",
encoding: "pcm_f32le",
sampleRate: 44100,
},
transcript: "Welcome to Cartesia Sonic!",
});
// Write `response` (of type ArrayBuffer) to a file.
fs.writeFileSync("sonic-2.wav", new Uint8Array(response));
// Play the file.
spawn("ffplay", ["-autoexit", "-nodisp", "sonic-2.wav"]);