# 音频接口

包含三个 OpenAI 兼容接口：

| 接口 | 用途 |
| --- | --- |
| `POST /v1/audio/speech` | 文本转语音 |
| `POST /v1/audio/transcriptions` | 语音转文字 |
| `POST /v1/audio/translations` | 语音翻译为英文 |

## 语音合成

**接口地址**：`POST https://YOUR_API_BASE_URL/v1/audio/speech`

- 鉴权：Bearer Token
- Content-Type：application/json

输入文本，返回音频二进制流。

### 请求参数

| 字段 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `model` | string | 是 | TTS 模型 ID |
| `input` | string | 是 | 要合成的文本，通常上限 4096 字符 |
| `voice` | string | 是 | 音色名称，由模型决定，如 `alloy`、`nova` |
| `response_format` | string | 否 | `mp3`（默认）、`opus`、`aac`、`flac`、`wav`、`pcm` |
| `speed` | number | 否 | 语速，通常 `0.25`–`4.0`，默认 `1.0` |
| `instructions` | string | 否 | 风格 / 情绪指令，模型支持时生效 |
| `stream_format` | string | 否 | `audio`（默认）或 `sse`；`sse` 时返回流式事件 |
| `metadata` | object | 否 | 自定义元数据（模型相关） |

### 请求示例

```bash
curl "https://YOUR_API_BASE_URL/v1/audio/speech" \
  -H "Authorization: Bearer sk-xxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "YOUR_TTS_MODEL",
    "input": "欢迎使用语音合成服务。",
    "voice": "alloy",
    "response_format": "mp3",
    "speed": 1.0
  }' \
  --output speech.mp3
```

```python
from openai import OpenAI
import os

client = OpenAI(
    api_key=os.environ["API_KEY"],
    base_url=os.environ["API_BASE"] + "/v1",
)

with client.audio.speech.with_streaming_response.create(
    model="YOUR_TTS_MODEL",
    voice="alloy",
    input="欢迎使用语音合成服务。",
    response_format="mp3",
) as response:
    response.stream_to_file("speech.mp3")
```

### 响应

成功时返回音频二进制，`Content-Type` 类似 `audio/mpeg`。请以二进制方式写入文件，不要按 JSON 解析。

若 `stream_format: "sse"`，返回 `text/event-stream`，事件中包含音频增量（Base64）与结束事件，具体字段由模型决定。

## 语音转录

**接口地址**：`POST https://YOUR_API_BASE_URL/v1/audio/transcriptions`

- 鉴权：Bearer Token
- Content-Type：multipart/form-data

上传音频文件，返回转写文本。

### 请求参数

| 字段 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `file` | file | 是 | 音频文件，支持 mp3、mp4、mpeg、mpga、m4a、wav、webm 等 |
| `model` | string | 是 | 转录模型 ID |
| `language` | string | 否 | ISO-639-1 语言代码，如 `zh`、`en`；可提升准确率 |
| `prompt` | string | 否 | 提示词，用于指定术语、风格或上下文 |
| `response_format` | string | 否 | `json`（默认）、`text`、`srt`、`verbose_json`、`vtt` |
| `temperature` | number | 否 | 采样温度，`0`–`1` |
| `timestamp_granularities[]` | string[] | 否 | `word`、`segment`；需配合 `verbose_json` |

### 请求示例

```bash
curl "https://YOUR_API_BASE_URL/v1/audio/transcriptions" \
  -H "Authorization: Bearer sk-xxxxxxxx" \
  -F "file=@meeting.mp3" \
  -F "model=YOUR_ASR_MODEL" \
  -F "language=zh" \
  -F "response_format=verbose_json" \
  -F "timestamp_granularities[]=segment"
```

```python
from openai import OpenAI
import os

client = OpenAI(
    api_key=os.environ["API_KEY"],
    base_url=os.environ["API_BASE"] + "/v1",
)

with open("meeting.mp3", "rb") as f:
    result = client.audio.transcriptions.create(
        model="YOUR_ASR_MODEL",
        file=f,
        language="zh",
        response_format="verbose_json",
    )
print(result.text)
```

### 响应

`response_format=json`：

```json
{
  "text": "今天的会议主要讨论了三件事。"
}
```

`response_format=verbose_json`：

```json
{
  "task": "transcribe",
  "language": "zh",
  "duration": 12.34,
  "text": "今天的会议主要讨论了三件事。",
  "segments": [
    {
      "id": 0,
      "seek": 0,
      "start": 0.0,
      "end": 4.2,
      "text": "今天的会议主要讨论了三件事。",
      "tokens": [],
      "temperature": 0.0,
      "avg_logprob": -0.21,
      "compression_ratio": 1.1,
      "no_speech_prob": 0.01
    }
  ]
}
```

`response_format=text`、`srt`、`vtt` 时直接返回纯文本字幕内容，`Content-Type` 为 `text/plain` 或字幕类型。

## 语音翻译

**接口地址**：`POST https://YOUR_API_BASE_URL/v1/audio/translations`

- 鉴权：Bearer Token
- Content-Type：multipart/form-data

把非英语语音翻译为英文文本。

| 字段 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `file` | file | 是 | 音频文件 |
| `model` | string | 是 | 翻译模型 ID |
| `prompt` | string | 否 | 提示词 |
| `response_format` | string | 否 | `json`（默认）、`text`、`srt`、`verbose_json`、`vtt` |
| `temperature` | number | 否 | 采样温度 |

```bash
curl "https://YOUR_API_BASE_URL/v1/audio/translations" \
  -H "Authorization: Bearer sk-xxxxxxxx" \
  -F "file=@chinese-audio.mp3" \
  -F "model=YOUR_ASR_MODEL" \
  -F "response_format=json"
```

响应：

```json
{
  "text": "The meeting mainly discussed three things today."
}
```

**提示**

翻译接口的输出语言固定为英文。若需要翻译成其他语言，请使用转录接口拿到原文，再调用文本模型翻译。

## 限制与错误

| 主题 | 说明 |
| --- | --- |
| 文件大小 | 受网关限制，通常 25 MB 左右；以实际返回为准 |
| 音频格式 | 建议 mp3 / wav / m4a；不支持的格式返回 `400` |
| 长音频 | 超过模型时长限制时需要先切分 |
| `413` | 文件过大 |
| `415` | 文件类型不支持 |
| `400` | 缺少 `file` / `model`，或格式参数不合法 |

完整错误结构见[错误与重试](https://docs.myaigf.cfd/guide/errors)。

## 相关阅读

- [文件与素材](https://docs.myaigf.cfd/api/files)
- [模型列表](https://docs.myaigf.cfd/api/models)：确认当前 Key 可访问该音频模型（音频模型通常标记为 `openai`）。
