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

# Voice Cloned TTS (Realtime)

> Real-time cloned voice audio streaming via WebSocket.

## Overview

<Steps>
  <Step title="Step 1 — Generate a voice embedding">
    Upload a reference audio clip to get a `speaker_embedding`. See [Voice Clone Embeddings](/vachana/VC/voice-clone-embeddings).
  </Step>

  <Step title="Step 2 — Synthesize with your cloned voice — this page">
    Pass the `speaker_embedding` from Step 1 to this endpoint to stream cloned voice audio in real-time with the lowest latency.
  </Step>
</Steps>

Stream cloned voice audio in real-time with the lowest latency via WebSocket. Pass the `speaker_embedding` from [Voice Clone Embeddings](/vachana/VC/voice-clone-embeddings) to use your cloned voice. For simpler use cases, see [Voice Cloning REST](/vachana/VC/vc-inference) or [Voice Cloning Streaming](/vachana/VC/vc-sse).

## Endpoint

```
wss://api.vachana.ai/api/v1/tts
```

## Authentication

All Realtime connections require the following headers:

| Header         | Required | Description                     | Example             |
| -------------- | -------- | ------------------------------- | ------------------- |
| `Content-Type` | Yes      | Must be `application/json`      | `application/json`  |
| `X-API-Key-ID` | Yes      | Your API key for authentication | `<your-api-key-id>` |

## Request Format

Send a JSON message with the following structure:

```json theme={null}
{
  "text": "नमस्ते, आप कैसे हैं?",
  "model": "vachana-vc-v1",
  "audio_config": {
    "sample_rate": 44100,
    "encoding": "linear_pcm"
  },
  "speaker_embedding": {
    "embedding": "<your-embedding-string>",
    "shape": [1, 768],
    "dtype": "torch.bfloat16"
  }
}
```

## Response

The server streams audio data in real-time as binary chunks. Each chunk contains PCM audio data according to the specified `audio_config`.

## Example Usage

<CodeGroup>
  ```javascript JavaScript theme={null}
  const ws = new WebSocket("wss://api.vachana.ai/api/v1/tts", {
    headers: {
      "Content-Type": "application/json",
      "X-API-Key-ID": "<your-api-key>",
    },
  });

  ws.on("open", () => {
    const request = {
      text: "नमस्ते, आप कैसे हैं?",
      model: "vachana-vc-v1",
      audio_config: {
        sample_rate: 44100,
        encoding: "linear_pcm",
      },
      speaker_embedding: {
        embedding: "<your-embedding-string>",
        shape: [1, 768],
        dtype: "torch.bfloat16",
      },
    };

    ws.send(JSON.stringify(request));
  });

  ws.on("message", (data) => {
    // Handle audio chunks
    console.log("Received audio chunk:", data);
  });

  ws.on("error", (error) => {
    console.error("WebSocket error:", error);
  });

  ws.on("close", () => {
    console.log("WebSocket connection closed");
  });
  ```

  ```python Python theme={null}
  import websocket
  import json

  def on_message(ws, message):
      # Handle audio chunks
      print(f"Received audio chunk: {len(message)} bytes")

  def on_error(ws, error):
      print(f"Error: {error}")

  def on_close(ws, close_status_code, close_msg):
      print("WebSocket connection closed")

  def on_open(ws):
      request = {
          "text": "नमस्ते, आप कैसे हैं?",
          "model": "vachana-vc-v1",
          "audio_config": {
              "sample_rate": 44100,
              "encoding": "linear_pcm"
          },
          "speaker_embedding": {
              "embedding": "<your-embedding-string>",
              "shape": [1, 768],
              "dtype": "torch.bfloat16"
          }
      }
      ws.send(json.dumps(request))

  ws = websocket.WebSocketApp(
      "wss://api.vachana.ai/api/v1/tts",
      header={
          "Content-Type": "application/json",
          "X-API-Key-ID": "<your-api-key>"
      },
      on_open=on_open,
      on_message=on_message,
      on_error=on_error,
      on_close=on_close
  )

  ws.run_forever()
  ```
</CodeGroup>
