- Python
- Python (Async)
def tts_sse_with_timestamps(client: Cartesia) -> None:
"""SSE streaming with word timestamps."""
stream = client.tts.generate_sse(
model_id="sonic-3",
transcript="Hello, world!",
voice={"mode": "id", "id": "6ccbfb76-1fc6-48f7-b71d-91ac6298247b"},
output_format={"container": "raw", "encoding": "pcm_f32le", "sample_rate": 44100},
add_timestamps=True,
)
import datetime
filename = f"tts_sse_with_timestamps_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pcm"
with open(filename, "wb") as f:
for event in stream:
if event.type == "timestamps":
wt = event.word_timestamps
if wt:
print(f"Words: {wt.words}, Starts: {wt.start}, Ends: {wt.end}")
elif event.type == "chunk":
if event.audio:
f.write(event.audio)
elif event.type == "done":
break
elif event.type == "error":
raise Exception(f"Error: {event.error}")
print(f"Saved audio to {filename}")
print(f"Play with: ffplay -f f32le -ar 44100 {filename}")
async def tts_sse_with_timestamps_async(client: AsyncCartesia) -> None:
"""SSE streaming with word timestamps."""
stream = await client.tts.generate_sse(
model_id="sonic-3",
transcript="Hello, world!",
voice={"mode": "id", "id": "6ccbfb76-1fc6-48f7-b71d-91ac6298247b"},
output_format={"container": "raw", "encoding": "pcm_f32le", "sample_rate": 44100},
add_timestamps=True,
)
filename = f"tts_sse_timestamps_async_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pcm"
with open(filename, "wb") as f:
async for event in stream:
if event.type == "timestamps":
wt = event.word_timestamps
if wt:
print(f"Words: {wt.words}, Starts: {wt.start}, Ends: {wt.end}")
elif event.type == "chunk":
if event.audio:
f.write(event.audio)
elif event.type == "done":
break
elif event.type == "error":
raise Exception(f"Error: {event.error}")
print(f"Saved audio to {filename}")
print(f"Play with: ffplay -f f32le -ar 44100 {filename}")
Run this example
- Python
- Python (Async)
cd cartesia-python
CARTESIA_API_KEY=YOUR_KEY python3 examples/examples.py tts_sse_with_timestamps
cd cartesia-python
CARTESIA_API_KEY=YOUR_KEY python3 examples/async_examples.py tts_sse_with_timestamps_async