On this page

Text-to-speech

Turn text into audio, speak a model's reply while it is still being written, and pick the voice that says it.

session.speak turns text into audio. It takes a whole string or a stream of strings, and returns a Speech you can play chunk by chunk or wait for in one piece. session.say is a shorter form for speaking into a call.

Speak a string

TypeScript
import { createSession } from "voiceai-sdk";
import { elevenlabs } from "voiceai-sdk/elevenlabs";

await using session = await createSession(elevenlabs());

const result = await session.speak("Your order has shipped.");
console.log(result.ok, result.durationMs, result.characters);

Awaiting a Speech gives you the whole clip:

FieldWhat it holds
oktrue when audio was produced.
audioThe complete audio.
durationMsHow long the audio plays, in milliseconds.
charactersHow many characters of text were spoken.

Iterate or await

You can await a Speech or iterate it. Await it when you need the whole clip, for example to save a file. Iterate it when you want to start playing before the rest is ready.

TypeScript
import { createSession } from "voiceai-sdk";
import { cartesia } from "voiceai-sdk/cartesia";

await using session = await createSession(cartesia());

for await (const chunk of session.speak("Thanks for calling. One moment.")) {
  process.stdout.write(chunk.data);
}

Each chunk is { data, ms }: the audio bytes, and how long they play. Whether the first chunk arrives before the whole clip is synthesized depends on the provider. Check streamingTts in Capabilities.

Stream text in

Pass an AsyncIterable<string> instead of a string to speak text that is still being written, such as a model's streamed reply. The session sends each piece to the provider as it arrives, so speech can start before the reply is finished.

TypeScript
import { createSession } from "voiceai-sdk";
import { elevenlabs } from "voiceai-sdk/elevenlabs";

await using session = await createSession(elevenlabs());

async function* reply() {
  yield "Your order shipped on Monday. ";
  yield "It should arrive by Friday.";
}

const result = await session.speak(reply());
console.log(result.durationMs);

With the AI SDK, you can pass the textStream of a streamText call straight to speak.

Speak values safely with say

session.say is a tagged template. It speaks into the connected transport. When no transport is connected, it resolves with the same result as an awaited speak.

TypeScript
import { createSession } from "voiceai-sdk";
import { elevenlabs } from "voiceai-sdk/elevenlabs";

await using session = await createSession(elevenlabs());

const name = "<emphasis>Dana</emphasis>";
const result = await session.say`Hello ${name}, your table is ready.`;

Interpolated values are always spoken as plain text, never read as SSML. A value from a caller, a database or a model can't add pauses, change the voice or inject markup. In the example, the tags around the name are treated as text.

Voices

listVoices(provider) returns the voices a provider offers, as { id, name, language, gender? }. Set a default with the session's voice option, and override it for one call with the voice option of speak.

TypeScript
import { createSession, listVoices } from "voiceai-sdk";
import { elevenlabs } from "voiceai-sdk/elevenlabs";

const voices = await listVoices(elevenlabs());
const [first, second] = voices.filter((v) => v.language.startsWith("en"));

await using session = await createSession(elevenlabs(), { voice: first.id });

await session.speak("This is the session's voice.");
await session.speak("This is another voice.", { voice: second.id });

The CLI lists the same voices:

Bash
npx voiceai-sdk voices --provider elevenlabs

session.capabilities.voices tells you whether the provider lets you choose a voice.

Speed and format

speak takes two more options that apply to a single call:

  • speed makes the voice faster or slower than its normal pace.
  • format sets the encoding of the audio for this call, such as "opus", instead of the session's encoding.
TypeScript
import { createSession } from "voiceai-sdk";
import { cartesia } from "voiceai-sdk/cartesia";

await using session = await createSession(cartesia(), { encoding: "pcm16" });

const clip = await session.speak("Press one for sales.", { speed: 0.9, format: "opus" });

Which speeds and formats work depends on the provider. As with session options, one the provider can't honour fails with unsupported instead of being ignored. See Capabilities.

Next steps

Esc

Loading…

↑↓ navigate openCtrl J preview