> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cartesia.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# WebSocket Speed Control

> Demonstrates changing speed mid-stream using generation_config.

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    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.5",
                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](https://github.com/cartesia-ai/cartesia-python/blob/main/examples/examples.py#L344)
  </Tab>

  <Tab title="Python (Async)">
    ```python theme={null}
    async def tts_websocket_speed_async(client: AsyncCartesia) -> None:
        """Demonstrates changing speed mid-stream using generation_config."""
        output_format = {"container": "raw", "encoding": "pcm_f32le", "sample_rate": 44100}

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

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

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

            await ctx.no_more_inputs()

            import datetime
            filename = f"tts_speed_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)

            print(f"Saved audio to {filename}")
            print(f"Play with: ffplay -f f32le -ar 44100 {filename}")
    ```

    From [cartesia-python/examples/async\_examples.py:258](https://github.com/cartesia-ai/cartesia-python/blob/main/examples/async_examples.py#L258)
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    async function ttsWebsocketSpeed(client: Cartesia): Promise<void> {
      /** Demonstrates changing speed mid-stream using generation_config. */
      const ws = await client.tts.websocket();
      ws.on('error', (err) => console.error('WS error:', err.message));

      const ctx = ws.context({
        model_id: 'sonic-3.5',
        voice: { mode: 'id', id: '6ccbfb76-1fc6-48f7-b71d-91ac6298247b' },
        output_format: { container: 'raw', encoding: 'pcm_f32le', sample_rate: 44100 },
      });

      console.log('Sending normal speed text...');
      await ctx.push({ transcript: 'I am speaking at a normal pace. ' });

      console.log('Sending fast speed text...');
      await ctx.send({
        model_id: 'sonic-3.5',
        voice: { mode: 'id', id: '6ccbfb76-1fc6-48f7-b71d-91ac6298247b' },
        output_format: { container: 'raw', encoding: 'pcm_f32le', sample_rate: 44100 },
        transcript: 'But now I am speaking much faster!',
        continue: true,
        generation_config: { speed: 1.5 },
      });

      await ctx.no_more_inputs();

      const filename = `tts_speed_${timestamp()}.pcm`;
      const file = fs.createWriteStream(filename);

      for await (const event of ctx.receive()) {
        if (event.type === 'chunk') {
          if (event.audio) file.write(event.audio);
        }
      }

      file.end();
      ws.close();
      console.log(`Saved audio to ${filename}`);
      console.log(`Play with: ffplay -f f32le -ar 44100 ${filename}`);
    }
    ```

    From [cartesia-js/examples/node\_examples.ts:198](https://github.com/cartesia-ai/cartesia-js/blob/main/examples/node_examples.ts#L198)
  </Tab>
</Tabs>

## Run this example

<Tabs>
  <Tab title="Python">
    ```sh theme={null}
    cd cartesia-python
    CARTESIA_API_KEY=YOUR_KEY python3 examples/examples.py tts_websocket_speed
    ```
  </Tab>

  <Tab title="Python (Async)">
    ```sh theme={null}
    cd cartesia-python
    CARTESIA_API_KEY=YOUR_KEY python3 examples/async_examples.py tts_websocket_speed_async
    ```
  </Tab>

  <Tab title="TypeScript">
    ```sh theme={null}
    cd cartesia-js
    CARTESIA_API_KEY=YOUR_KEY npx ts-node examples/node_examples.ts ttsWebsocketSpeed
    ```
  </Tab>
</Tabs>
