> For clean Markdown of any page, append `.md` to the page URL.
> For a complete documentation index, see https://docs.sarvam.ai/llms.txt.
> For full documentation content in one file, see https://docs.sarvam.ai/llms-full.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.sarvam.ai/_mcp/server.

# How to set buffer size to start processing in Streaming TTS with `min_buffer_size`

> Define when the TTS engine should start processing text from the buffer.

The `min_buffer_size` parameter sets the **minimum number of characters** that must accumulate in the buffer before the TTS engine begins converting text into audio.
We buffer incoming text until it reaches this threshold before processing and chunking begins.

This parameter is available only in the **WebSocket Streaming API**. It is not supported in the REST API.

This helps balance latency with natural sentence completion during real-time TTS streaming.

### **Parameter Details**

* **Type:** Integer
* **Range:** `30` to `200`
* **Default:** `50`
* **Purpose:** Minimum character length that triggers buffer flushing and TTS processing.

***

### How It Works

* When the buffer reaches `min_buffer_size`, the text is **automatically processed** and streamed as audio.
* If the buffer does **not** reach the threshold, the text is **held** until:
  * More characters arrive, **or**
  * A **flush command** is sent.

### Manual Flush Option

A **flush command** forces the TTS engine to **immediately process** the current buffer — even if it hasn’t reached the `min_buffer_size`.

***

### Practical Example

Suppose `min_buffer_size = 50`, and you send an 80-character sentence in two parts:

| Input Chunk | Characters | Result                |
| ----------- | ---------- | --------------------- |
| First part  | 60         | Processed immediately |
| Second part | 20         | Held in buffer        |

To process the remaining 20 characters, send a **flush message**.

***

### **Example Streaming API code**

```python
import asyncio
import base64
from sarvamai import AsyncSarvamAI, AudioOutput
import websockets

async def tts_stream():
    client = AsyncSarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")

    async with client.text_to_speech_streaming.connect(model="bulbul:v3") as ws:
        await ws.configure(
            target_language_code="hi-IN", 
            speaker="shubh",
            min_buffer_size= 80
        )
        print("Sent configuration")

        text = (
            "भारत की संस्कृति विश्व की सबसे प्राचीन और समृद्ध संस्कृतियों में से एक है।"
            "यह विविधता, सहिष्णुता और परंपराओं का अद्भुत संगम है, "
            "जिसमें विभिन्न धर्म, भाषाएं, त्योहार, संगीत, नृत्य, वास्तुकला और जीवनशैली शामिल हैं।"
        )


        await ws.convert(text)
        print("Sent text message")

        await ws.flush()
        print("Flushed buffer")

        chunk_count = 0
        with open("output.mp3", "wb") as f:
            async for message in ws:
                if isinstance(message, AudioOutput):
                    chunk_count += 1
                    audio_chunk = base64.b64decode(message.data.audio)
                    f.write(audio_chunk)
                    f.flush()

        print(f"All {chunk_count} chunks saved to output.mp3")
        print("Audio generation complete")

        
        if hasattr(ws, "_websocket") and not ws._websocket.closed:
            await ws._websocket.close()
            print("WebSocket connection closed.")


if __name__ == "__main__":
    asyncio.run(tts_stream())

# --- Notebook/Colab usage ---
# await tts_stream()

```