Skip to main content
def tts_websocket_speed(client: Cartesia) -> None:
    """Demonstrates changing speed mid-stream using generation_config."""
    output_format = {"container": "raw", "encoding": "pcm_f32le", "sample_rate": 44100}

    with client.tts.websocket_connect() as connection:
        ctx = connection.context(
            model_id="sonic-3",
            voice={"mode": "id", "id": "6ccbfb76-1fc6-48f7-b71d-91ac6298247b"},
            output_format=output_format
        )

        print("Sending normal speed text...")
        ctx.push("I am speaking at a normal pace. ")

        print("Sending fast speed text...")
        ctx.push("But now I am speaking much faster!", generation_config={"speed": 1.5})

        ctx.no_more_inputs()

        import datetime
        filename = f"tts_speed_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pcm"

        with open(filename, "wb") as f:
            for response in ctx.receive():
                if response.type == "chunk" and response.audio:
                    f.write(response.audio)

        print(f"Saved audio to {filename}")
        print(f"Play with: ffplay -f f32le -ar 44100 {filename}")
From cartesia-python/examples/examples.py:344

Run this example

cd cartesia-python
CARTESIA_API_KEY=YOUR_KEY python3 examples/examples.py tts_websocket_speed