> ## 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

**1. Step 1 — Generate a voice embedding**

Upload a reference audio clip to get a `speaker_embedding`. See [Voice Clone Embeddings](https://docs.gnani.ai/api/VC/voice-clone-embeddings.md).

**2. 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.

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

## 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 uses the same WebSocket message protocol as [TTS Realtime](https://docs.gnani.ai/api/TTS/tts-websocket.md). Send a JSON synthesis request; receive JSON-wrapped base64 audio chunks.

### Connection flow

1. Connect to `wss://api.vachana.ai/api/v1/tts` with `X-API-Key-ID`.
2. Send a JSON message with `text`, `model`, `speaker_embedding`, and `audio_config`.
3. Receive `start`, then `audio` chunks, then `complete`.
4. Decode each chunk's `data.audio` from base64 and append in order.

### Server messages

**`start`** — synthesis began:

```json theme={null}
{ "type": "start", "message": "Streaming started", "request_id": "req_abc123" }
```

**Audio chunk:**

```json theme={null}
{
  "type": "audio",
  "data": {
    "chunk_index": 1,
    "audio": "<base64-encoded audio bytes>",
    "is_final": false
  }
}
```

**`complete`** — all audio sent:

```json theme={null}
{
  "type": "complete",
  "data": {
    "chunk_index": 12,
    "audio": "",
    "is_final": true
  }
}
```

**`error`** — synthesis failed:

**Text-to-Speech** (WebSocket `error` message, and the `error` event on SSE) uses `type` and `message`:

```json theme={null}
{ "type": "error", "message": "Invalid text or audio configuration." }
```

The connection remains open after recoverable errors.

**Voice Cloning** (WebSocket) adds an `error` code:

```json theme={null}
{
  "type": "error",
  "error": "SYNTHESIS_FAILED",
  "message": "Human-readable error description"
}
```

> [!NOTE]
>
> Include a complete `audio_config` (`sample_rate`, `num_channels`, `sample_width`, `encoding`, `container`) matching [VC REST](https://docs.gnani.ai/api/VC/vc-inference.md) examples.

## Example Usage

```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) => {
  const msg = JSON.parse(data.toString());
  if (msg.type === "audio") {
    // msg.data.audio is base64-encoded audio; decode and append in order
    console.log("Received audio chunk:", msg.data.chunk_index);
  } else if (msg.type === "complete" || msg.type === "error") {
    ws.close(); // done: close instead of waiting for the idle timeout
  }
});

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
import base64

audio = bytearray()

def on_message(ws, message):
    msg = json.loads(message)
    if msg["type"] == "audio":
        audio.extend(base64.b64decode(msg["data"]["audio"]))
    elif msg["type"] == "complete":
        with open("audio.wav", "wb") as f:
            f.write(audio)
        print(f"Saved {len(audio)} bytes")
        ws.close()                     # done: close instead of waiting for the idle timeout
    elif msg["type"] == "error":
        print(f"Error: {msg}")
        ws.close()

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()
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.