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

# Audio formats

> Choose the output format for Text-to-Speech: WAV, raw PCM, MP3, OGG Opus, µ-law or A-law, with the sample rate and MP3 bitrate.

Use this when you need the audio in a particular format: a phone line, a web player, a mobile app, or files you store.

## Turn it on

Set `audio_config` in the request: `container` for the format, `sample_rate`, and `bitrate` for MP3. This example asks for a 24 kHz MP3:

```bash cURL theme={null}
curl --request POST \
  --url https://api.vachana.ai/api/v1/tts/inference \
  --header "Content-Type: application/json" \
  --header "X-API-Key-ID: $GNANI_API_KEY" \
  --data '{
    "text": "नमस्ते, आज मैं आपकी कैसे मदद कर सकती हूँ?",
    "voice": "Shivani",
    "model": "timbre-v3.0",
    "language": "hi-IN",
    "audio_config": {"container": "mp3", "sample_rate": 24000, "bitrate": "128k"}
  }' \
  --output speech.mp3
```

```python Python theme={null}
import os

import requests

response = requests.post(
    "https://api.vachana.ai/api/v1/tts/inference",
    headers={"X-API-Key-ID": os.environ["GNANI_API_KEY"]},
    json={
        "text": "नमस्ते, आज मैं आपकी कैसे मदद कर सकती हूँ?",
        "voice": "Shivani",
        "model": "timbre-v3.0",
        "language": "hi-IN",
        "audio_config": {"container": "mp3", "sample_rate": 24000, "bitrate": "128k"},
    },
    timeout=60,
)
response.raise_for_status()

with open("speech.mp3", "wb") as f:
    f.write(response.content)
```

```javascript Node.js theme={null}
import { writeFileSync } from "node:fs";

const response = await fetch("https://api.vachana.ai/api/v1/tts/inference", {
  method: "POST",
  headers: { "Content-Type": "application/json", "X-API-Key-ID": process.env.GNANI_API_KEY },
  body: JSON.stringify({
    text: "नमस्ते, आज मैं आपकी कैसे मदद कर सकती हूँ?",
    voice: "Shivani",
    model: "timbre-v3.0",
    language: "hi-IN",
    audio_config: { container: "mp3", sample_rate: 24000, bitrate: "128k" },
  }),
});
if (!response.ok) throw new Error(await response.text());

writeFileSync("speech.mp3", Buffer.from(await response.arrayBuffer()));
```

```python Python SDK theme={null}
import os
from gnani.tts import AudioConfig, GnaniTTSClient

client = GnaniTTSClient(api_key=os.environ["GNANI_API_KEY"])
audio = client.synthesize(
    "नमस्ते, आज मैं आपकी कैसे मदद कर सकती हूँ?",
    voice="Shivani",
    model="timbre-v3.0",
    language="hi-IN",
    audio_config=AudioConfig(container="mp3", sample_rate=24000, bitrate="128k"),
)

with open("speech.mp3", "wb") as f:
    f.write(audio)
```

Which format to use:

| You are | Use |
| - | - |
| Playing audio on a phone line, IVR or SIP trunk | `mulaw` or `alaw`, whichever your telephony platform expects. Always 8 kHz. |
| Playing audio in a browser or app | `mp3` or `ogg` |
| Feeding a player while the audio is still arriving | `raw` (16-bit PCM) on [Streaming](https://docs.gnani.ai/api/TTS/tts-sse.md) or [Realtime](https://docs.gnani.ai/api/TTS/tts-websocket.md) |
| Storing or editing audio | `wav` |
| Keeping files as small as possible | `ogg`, or `mp3` with a low `bitrate` |

## Examples

The same Hindi sentence (Timbre v3.0, voice Shivani) in each format. The sample rate and size are from the returned files.

| Format | Listen |
| - | - |
| WAV, 48 kHz | <audio controls preload="none" src="https://docs.gnani.ai/audio/samples/tts-format-wav-48k.wav" aria-label="WAV at 48 kHz" /> |
| WAV, 8 kHz | <audio controls preload="none" src="https://docs.gnani.ai/audio/samples/tts-format-wav-8k.wav" aria-label="WAV at 8 kHz" /> |
| MP3, 128 kbps | <audio controls preload="none" src="https://docs.gnani.ai/audio/samples/tts-format-mp3-128k.mp3" aria-label="MP3 at 128 kbps" /> |
| MP3, 32 kbps | <audio controls preload="none" src="https://docs.gnani.ai/audio/samples/tts-format-mp3-32k.mp3" aria-label="MP3 at 32 kbps" /> |
| OGG Opus | <audio controls preload="none" src="https://docs.gnani.ai/audio/samples/tts-format-ogg.ogg" aria-label="OGG Opus" /> |
| µ-law (decoded for playback) | <audio controls preload="none" src="https://docs.gnani.ai/audio/samples/tts-format-mulaw-decoded.wav" aria-label="µ-law, decoded for playback" /> |

| Request | What came back | Size per second |
| - | - | - |
| `wav`, 48,000 Hz | 16-bit PCM WAV, 48 kHz | about 96 KB |
| `wav`, 8,000 Hz | 16-bit PCM WAV, 8 kHz | about 16 KB |
| `raw`, 16,000 Hz | 16-bit PCM, no header | about 32 KB |
| `mp3`, 48,000 Hz, `128k` | MP3, 48 kHz, 128 kbps | about 16 KB |
| `mp3`, 24,000 Hz, `32k` | MP3, **22.05 kHz**, 32 kbps | about 4 KB |
| `ogg`, 24,000 Hz | OGG Opus, about 65 kbps | about 8 KB |
| `mulaw`, 48,000 Hz requested | G.711 µ-law, **8 kHz**, no header | about 8 KB |
| `alaw`, 48,000 Hz requested | G.711 A-law, **8 kHz**, no header | about 8 KB |

Two things the files show: µ-law and A-law came back at 8 kHz although 48 kHz was asked for, as documented; and the 32 kbps MP3 came back at 22.05 kHz although 24 kHz was asked for. See [Things to watch for](#things-to-watch-for).

## By interface

> [tab] REST
>
> [`POST /api/v1/tts/inference`](https://docs.gnani.ai/api/TTS/tts-inference) returns the whole audio as the response body, with the `Content-Type` of the format (table below). Save it as a file.

> [tab] Streaming (SSE)
>
> [`POST /api/v1/tts/sse`](https://docs.gnani.ai/api/TTS/tts-sse) sends the audio as base64 chunks in events. With `raw`, each chunk can go straight to a player as it arrives. With `wav`, `mp3` or `ogg`, join the decoded chunks in order to get one file.

> [tab] Realtime (WebSocket)
>
> [The TTS WebSocket](https://docs.gnani.ai/api/TTS/tts-websocket.md) sends base64 chunks in `audio` messages, with the same choice: `raw` for live playback, or join the chunks into a file.

## By model

Timbre v3.0 and Timbre v2.5 offer the same formats and sample rates. [Voice cloning](https://docs.gnani.ai/api/VC/vc-inference.md) lists WAV, raw PCM, MP3, OGG Opus and µ-law, at 8,000 to 44,100 Hz.

## Rules and limits

| `container` | `encoding` | What you get | `sample_rate` | `bitrate` | `Content-Type` |
| - | - | - | - | - | - |
| `wav` (default) | `linear_pcm` | WAV file with a header | 8,000 to 48,000 Hz | — | `audio/wav` |
| `raw` | `linear_pcm` | 16-bit PCM, no header | 8,000 to 48,000 Hz | — | `application/octet-stream` |
| `mp3` | — | MP3 file | 8,000 to 48,000 Hz | `32k`, `64k`, `96k`, `128k` (default), `192k` | `audio/mpeg` |
| `ogg` | — | OGG Opus file | 8,000 to 48,000 Hz | — | `audio/ogg` |
| `mulaw` | — | G.711 µ-law, no header | Always 8,000 Hz | — | `audio/basic` |
| `alaw` | — | G.711 A-law, no header | Always 8,000 Hz | — | `audio/alaw` |

* **`sample_rate`:** `8000`, `16000`, `22050`, `24000`, `44100` or `48000`. Default `48000`.
* **Other fields:** `num_channels` (default `1`) and `sample_width` in bytes (default `2`). `encoding` is not needed for `mp3`.
* **Equivalent settings:** `container: "raw"` with `encoding: "pcm_mulaw"` is the same as `container: "mulaw"`; with `pcm_alaw`, the same as `alaw`; `encoding: "oggopus"` with `raw` or `ogg` is the same as `container: "ogg"`.

## Things to watch for

* **A low MP3 bitrate lowers the sample rate.** With `bitrate: "32k"`, requests for 24,000 and 48,000 Hz both returned 22,050 Hz files; 16,000 Hz was kept, and 24,000 Hz was kept at `128k`. Read the sample rate from the file, not from your request. Other bitrates: TBC.
* **µ-law and A-law are always 8 kHz** and have no header. Most telephony platforms expect exactly this; to play one on a computer, decode it or wrap it in a WAV header first.
* **`raw` has no header either.** Keep the `sample_rate` you asked for: a player needs it to play the audio at the right speed.
* **OGG Opus plays at 48 kHz.** The file records the rate you asked for, but Opus players decode at 48 kHz. This is normal for Opus.
* **Higher is not always better.** A narrowband phone line carries 8 kHz audio however high the rate you request; ask for what the destination plays.
* **Python SDK:** `AudioConfig` defaults to a 48 kHz WAV. The SDK checks `container`, `sample_rate` and `bitrate` before sending.

## Related

> [card] [Phone audio (Speech-to-Text)](https://docs.gnani.ai/api/STT/phone-audio.md)
>
> The other side of the call: transcribing 8 kHz audio.

> [card] [Streaming (SSE)](https://docs.gnani.ai/api/TTS/tts-sse.md)
>
> Start playback before the whole audio is ready.

> [card] [Text-to-Speech (REST)](https://docs.gnani.ai/api/TTS/tts-inference.md)
>
> Every request field, with a request builder.

> [card] [Limits](https://docs.gnani.ai/api/TTS/limits.md)
>
> Text length, formats and rate limits.


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