- Python
- Python (Async)
- TypeScript
def tts_websocket_continuations(client: Cartesia) -> None:
"""Streaming a transcript split into multiple parts, using continuations.
Useful for streaming transcripts generated by an LLM."""
with client.tts.websocket_connect() as ws:
ctx = ws.context(
model_id="sonic-latest",
voice={"mode": "id", "id": "6ccbfb76-1fc6-48f7-b71d-91ac6298247b"},
output_format={
"container": "raw",
"encoding": "pcm_s16le",
"sample_rate": 44100,
},
language="en",
)
for part in ["The road ", "goes ever ", "on and ", "on."]:
ctx.push(part)
ctx.no_more_inputs()
import datetime
filename = f"tts_websocket_continuations_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pcm"
# Write chunks to file as they arrive.
# You could also send chunks over the network, play them in real-time, etc.
with open(filename, "wb") as f:
for response in ctx.receive():
if response.type == "chunk" and response.audio:
f.write(response.audio)
elif response.type == "error":
print(f"error: {response.message or response.title}")
print(f"Saved audio to {filename}")
print(f"Play with:\n $ ffplay -f s16le -ar 44100 {filename}")
async def tts_websocket_continuations_async(client: AsyncCartesia) -> None:
"""Async streaming multiple transcripts with continuations."""
import datetime
transcripts = ["The only thing we have to fear ", "is ", "fear itself."]
async with client.tts.websocket_connect() as ws:
ctx = ws.context(
model_id="sonic-latest",
voice={"mode": "id", "id": "6ccbfb76-1fc6-48f7-b71d-91ac6298247b"},
output_format={"container": "raw", "encoding": "pcm_s16le", "sample_rate": 44100},
)
for transcript in transcripts:
await ctx.push(transcript)
await ctx.no_more_inputs()
filename = f"tts_ws_continuations_async_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pcm"
with open(filename, "wb") as f:
async for response in ctx.receive():
if response.type == "chunk" and response.audio:
f.write(response.audio)
elif response.type == "error":
print(f"error: {response.message or response.title}")
print(f"Saved audio to {filename}")
print(f"Play with: ffplay -f s16le -ar 44100 {filename}")
async function ttsWebsocketContinuations(client: Cartesia): Promise<void> {
const ws = await client.tts.websocket();
ws.on('error', (err) => console.error('WS error:', err.message));
const filename = `tts_websocket_continuations_${timestamp()}.pcm`;
const file = fs.createWriteStream(filename);
try {
const ctx = ws.context({
model_id: 'sonic-latest',
voice: { mode: 'id', id: '6ccbfb76-1fc6-48f7-b71d-91ac6298247b' },
output_format: { container: 'raw', encoding: 'pcm_f32le', sample_rate: 44100 },
language: 'en',
});
for (const part of ['The road ', 'goes ever ', 'on and ', 'on.']) {
await ctx.push({ transcript: part });
}
await ctx.no_more_inputs();
for await (const event of ctx.receive()) {
if (event.type === 'chunk') {
if (event.audio) file.write(event.audio);
} else if (event.type === 'error') {
throw new Error(`${event.title}: ${event.message}`);
}
}
console.log(`Saved audio to ${filename}`);
console.log(`Play with:\n $ ffplay -f f32le -ar 44100 ${filename}`);
} finally {
file.end();
ws.close();
}
}
Run this example
- Python
- Python (Async)
- TypeScript
git clone --branch v3.2.0 https://github.com/cartesia-ai/cartesia-python
cd cartesia-python
uv sync
CARTESIA_API_KEY=YOUR_KEY uv run examples/examples.py tts_websocket_continuations
git clone --branch v3.2.0 https://github.com/cartesia-ai/cartesia-python
cd cartesia-python
uv sync
CARTESIA_API_KEY=YOUR_KEY uv run examples/async_examples.py tts_websocket_continuations_async
git clone --branch v3.2.0 https://github.com/cartesia-ai/cartesia-js
cd cartesia-js
pnpm i
CARTESIA_API_KEY=YOUR_KEY pnpm tsn examples/node_examples.ts ttsWebsocketContinuations