# Text-to-Speech (TTS) Endpoints

OpenAI-compatible text-to-speech: `POST /v1/audio/speech`.

### 1. Speech Synthesis
* **Path**: `/v1/audio/speech`
* **Request Method**: `POST`

#### Request Parameters
| Parameter | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| `model` | `string` | Yes | TTS or audio generation model name, e.g., `tts-1` (OpenAI), `seed-tts-2.0` (Volcengine Speech LLM), `seed-audio-1.0` (Volcengine Audio Generation) |
| `input` | `string` | Yes | The text to be synthesized into speech. The maximum length is determined by the specific model specifications |
| `voice` | `string` | Yes* | Voice identity/timbre. For Volcengine Ark, pass the speaker ID (e.g., `zh_female_vv_uranus_bigtts`). Optional for `seed-audio-1.0`, which also accepts `image_url`, `audio_urls`, `sample_rate`, `speech_rate`, `loudness_rate`, and `pitch_rate` on this endpoint and returns binary audio |
| `response_format` | `string` | No | Audio format. Options: `mp3` (default), `opus`, `aac`, `flac`, `wav`, `pcm` |
| `speed` | `number` | No | Speed adjustment factor (`0.25` to `4.0`, default is `1.0`) |

#### Calling Example
```bash
curl -X POST https://ai.xiwaplus.com/v1/audio/speech \
  -H "Authorization: Bearer sk-your_token" \
  -H "Content-Type: application/json" \
  -o output.mp3 \
  -d '{
    "model": "seed-tts-2.0",
    "input": "Hello, welcome to the unified intelligent text-to-speech system. Please enter the text you wish to synthesize below.",
    "voice": "zh_female_vv_uranus_bigtts",
    "response_format": "mp3"
  }'
```

> [!NOTE]
> The gateway returns a raw binary audio stream for this endpoint (HTTP binary response with `Content-Type` set to `audio/mpeg` or the corresponding audio format). It returns a binary audio stream that can be saved directly as a file for playback.

### 2. Volcengine Audio Generation Native API (POST /api/v3/tts/create)
* **Path**: `/api/v3/tts/create`
* **Request Method**: `POST`
* **Authentication**: `X-Api-Key: sk-your_token` or `Authorization: Bearer sk-your_token`

Designed for `seed-audio-1.0` (dialogue, sound effects, and ambient audio up to 120 seconds). Pass the model ID in `body.model` (header `X-Api-Resource-Id` is also supported).

#### Request Parameters
| Parameter | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| `model` | `string` | Yes | Set to `seed-audio-1.0` (or specify via the `X-Api-Resource-Id` header) |
| `text_prompt` | `string` | Yes | Audio generation prompt (clipped at 3000 characters by the gateway); **both preset `speaker` voices and reference audio clips in `references` must be explicitly cited in `text_prompt` in array order as `@音频1`, `@音频2`, `@音频3`** (e.g., `Speaker A (description) 声音@音频1`), otherwise the upstream returns `55001309` |
| `references` | `array` | No | Reference items: voice `{"speaker": "..."}`, image `{"image_url": "..."}` (or `image_bytes`, max 1), audio `{"audio_url": "..."}` (or `audio_data`, max 3). **Reference image cannot be combined with reference audio or `speaker`** (returns HTTP `400`); `speaker` can be combined with reference audio; **every `speaker` and audio item in `references` must be bound in order via `@音频N` in `text_prompt`** |
| `audio_config` | `object` | No | Audio settings: `format` (`mp3` default / `wav` / `pcm` / `ogg_opus`), `sample_rate` (`48000` fixed for `ogg_opus`; `8000`–`48000` with `40000` default for `wav`/`pcm`; `44100` default for `mp3`), `speech_rate` (`-50` to `100`, default `0`), `loudness_rate` (`-50` to `100`, default `0`), `pitch_rate` (`-12` to `12`, default `0`) |

#### Calling Example
```bash
curl -X POST https://ai.xiwaplus.com/api/v3/tts/create \
  -H "X-Api-Key: sk-your_token" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "seed-audio-1.0",
    "text_prompt": "Speaker A (gentle female voice) 声音@音频1\nIn soft midnight rain, Speaker A whispers: We are finally here.",
    "references": [
      { "speaker": "zh_female_vv_uranus_bigtts" }
    ],
    "audio_config": {
      "format": "mp3",
      "sample_rate": 44100,
      "speech_rate": 0,
      "loudness_rate": 0,
      "pitch_rate": 0
    }
  }'
```

> [!NOTE]
> `/api/v3/tts/create` passes through Volcengine's native `application/json` response (containing Base64 `audio` and/or temporary `url`, `original_duration`, `duration`, etc.) and bills strictly by `original_duration` seconds (unaffected by `speech_rate`). Use `POST /v1/audio/speech` if you want the gateway to decode or fetch and return a binary audio stream.

#### Troubleshooting: Binding `speaker` / Reference Audio with `@音频N` (Desensitized Case)
* **Error Symptom**: When `references` includes a `speaker` (or reference audio) without citing `@音频1` etc. in `text_prompt`, the upstream may fail with:
```json
{
  "code": 55001309,
  "message": "audio generate internal error"
}
```
* **Fix**: Preset `speaker` voices follow the exact same rule as reference audios—explicitly bind them to roles in `text_prompt` in array order using `@音频1`, `@音频2`, `@音频3` (omit if `references` is not provided).
* **Multi-speaker Desensitized Example** (for a single voice, keep only `references[0]` and bind `@音频1`):
```json
{
  "model": "seed-audio-1.0",
  "text_prompt": "Speaker A (adult male, deep steady voice) 声音@音频1, Speaker B (middle-aged male, slightly raspy voice) 声音@音频2\nA phone vibrates first, with light rain and ambient background noise. Speaker A asks seriously: \"Have you confirmed the proposal yet?\" Speaker B replies calmly: \"Everything is verified and ready to proceed as planned tomorrow.\" Light footsteps follow.",
  "references": [
    { "speaker": "zh_male_example_speaker_1" },
    { "speaker": "zh_male_example_speaker_2" }
  ],
  "audio_config": {
    "format": "mp3",
    "sample_rate": 48000,
    "speech_rate": 0,
    "loudness_rate": 0,
    "pitch_rate": 0
  }
}
```
* **Notes**:
  1. **1-to-1 Index Matching**: Match each item in `references` in exact order with `@音频1`, `@音频2`, `@音频3`—do not omit or over-cite.
  2. **Match Voice & Role Description**: Ensure the selected `speaker` gender and style align with the character description in `text_prompt`.