# audiogen

> 智能体用来生成语音、音效和音乐并保存为本地音频文件，以及查询声音和语音模型的命令。

Source: https://air.build/cn/docs/cli/audiogen/

`audiogen` 生成音频文件：用指定声音朗读的语音、根据描述生成的音效，以及根据简述生成的音乐。它还能列出声音和语音模型、从一个文件批量运行多个请求、显示团队的 AI 用量，并检查自身的配置。

## 在哪里运行

云端团队和 Mac 团队的智能体都能使用同一个命令。登录凭据和智能体归属信息都来自智能体的运行环境，无需任何设置。它依赖 `curl` 和 `jq`。参见[选择智能体的运行位置](https://air.build/cn/docs/get-started/where-agents-run/)。

## 用法概要

```text
audiogen <resource> <method> [flags]
audiogen batch [--keep-going] [--dry-run] <spec.json>
audiogen balance
audiogen docs [--topic <name>]
audiogen doctor
audiogen --help | --version
audiogen <resource> <method> --help
```

| Command | 作用 |
| --- | --- |
| `text-to-speech convert`（别名 `tts convert`） | 用某个声音把文字转成语音。写入音频文件。 |
| `text-to-sound-effects convert`（别名 `sfx convert`） | 把描述转成音效。写入音频文件。 |
| `music compose` | 把简述（或作曲方案）转成音乐。写入音频文件。 |
| `music composition-plan create` | 把简述转成结构化方案，以 JSON 输出，可供 `music compose` 使用。 |
| `voices search` | 列出声音。 |
| `voices get` | 某个声音的详情。 |
| `models list` | 语音模型，附文本长度上限和支持的语言。 |
| `batch` | 从一个 JSON 文件运行多个请求。 |
| `balance` | 本周期的 AI 用量与方案 AI 额度的对比。 |
| `docs` | 输出内置的参考文档。 |
| `doctor` | 检查运行环境、登录状态以及音频服务是否有响应。 |

## 所有请求通用的参数

以下参数适用于 `text-to-speech convert`、`text-to-sound-effects convert`、`music compose` 和 `music composition-plan create`。所有带值的参数也都接受 `--flag=value` 写法。

| Flag | 取值 | 默认值 | 含义 |
| --- | --- | --- | --- |
| `--json` | JSON 对象，或 `-` 表示从 stdin 读取 | `{}` | 完整的请求体。写在它后面的字段参数会覆盖其中的成员。 |
| `-p`, `--param` | `key=value` | — | 再添加一个请求体字段。可重复。`true`、`false`、`null`、数字以及 JSON 数组或对象会转成相应类型；其他都是字符串。 |
| `--output-format` | 服务的编码字符串，例如 `mp3_44100_128`、`mp3_44100_192`、`mp3_48000_192`、`wav_24000`、`wav_44100`、`pcm_…`、`opus_…`、`ulaw_8000` | 语音和音效为 `mp3_44100_128`；音乐为 `auto`（按模型） | 音频格式。它也决定文件扩展名：`mp3_*` 和 `auto` → `.mp3`，`wav_*` → `.wav`，`pcm_*` → `.pcm`，`opus_*` → `.opus`，`ulaw_*`/`alaw_*` → `.raw`。 |
| `--output` | 文件路径 | 新文件夹 `$TMPDIR/audiogen.XXXXXX/` | 音频的写入位置。缺少的文件夹会自动创建。从不覆盖已有文件。 |
| `--quiet` | — | 关闭 | 减少 stderr 上的输出。 |
| `--yes`, `--no-open` | — | — | 接受但忽略。 |

请求体按以下顺序组装：先 `--json`，再字段参数，最后 `-p`。请求体最大 1 MB。

## text-to-speech convert

```text
audiogen text-to-speech convert --voice-id <id> --text "…" [--output <file>] [flags]
```

| Flag | 取值 | 含义 |
| --- | --- | --- |
| `--voice-id` | 声音 id（字母和数字） | 必填，除非给出了 `--voice-name`。id 可从 `audiogen voices search` 获取。 |
| `--voice-name` | 声音名称 | 改为按名称查找声音。匹配不区分大小写，也会匹配名称中 `-` 之前的部分。 |
| `--text` | 文本 | 必填，除非给出了 `--text-file`。 |
| `--text-file` | 路径 | 从文件读取文本。 |
| `--model-id` | `audiogen models list` 列出的 id | 语音模型。不填则使用服务的默认模型。 |
| `--language-code` | ISO 639-1 代码 | 适用于支持该参数的模型。 |
| `--seed` | 数字 | 尽量保证结果可重复。 |
| `--voice-settings` | 包含 `stability`、`similarity_boost`、`style`、`speed`、`use_speaker_boost` 的 JSON | 一次性设置所有声音参数。 |
| `--stability`, `--similarity-boost`, `--style`, `--speed`, `--use-speaker-boost` | 各一个值 | 各自设置一项声音参数。 |
| `--apply-text-normalization` | `auto`、`on`、`off` | 数字和缩写的朗读方式。 |
| `--previous-text`, `--next-text` | 文本 | 前后文，用于保持片段之间的连贯。 |

默认文件名为 `text-to-speech.<ext>`。

## text-to-sound-effects convert

```text
audiogen text-to-sound-effects convert --text "…" [--output <file>] [flags]
```

| Flag | 取值 | 默认值 | 含义 |
| --- | --- | --- | --- |
| `--text` | 描述 | 必填 | 声音是什么。 |
| `--duration-seconds` | 0.5–30 | 由模型决定 | 时长。 |
| `--prompt-influence` | 0–1 | 0.3 | 越高越贴近字面描述。 |
| `--loop` | — | 关闭 | 生成无缝循环。 |
| `--model-id` | 音效模型 id | 服务的默认模型 | 音效模型。 |

默认文件名为 `text-to-sound-effects.<ext>`。

## music compose 和 music composition-plan create

```text
audiogen music compose --prompt "…" [--output <file>] [flags]
audiogen music composition-plan create --prompt "…" [--music-length-ms <n>]
```

`music compose`：

| Flag | 取值 | 含义 |
| --- | --- | --- |
| `--prompt` | 简述 | 必填，除非给出了 `--composition-plan`。 |
| `--composition-plan` | JSON | 来自 `music composition-plan create` 的方案。 |
| `--music-length-ms` | 3000–600000 | 时长，单位为毫秒。 |
| `--force-instrumental` | — | 不含人声。 |
| `--model-id` | `music_v2`（当前版本）或 `music_v1`（服务的默认模型） | 音乐模型。 |
| `--seed` | 数字 | 可重复性。 |
| `--finetune-id` | id | 作为 `finetune_id` 传给服务。 |

默认文件名为 `music-compose.<ext>`。

`music composition-plan create` 接受 `--prompt`（必填）、`--music-length-ms`、`--model-id` 和 `--source-composition-plan`，以 JSON 输出方案，而不是写入音频。

## voices 和 models

```text
audiogen voices search [--search <text>] [--page-size <n>] [--next-page-token <t>]
                       [--category <c>] [--voice-type <t>] [--sort <s>] [--sort-direction <d>]
audiogen voices get --voice-id <id>
audiogen models list
```

`voices search` 和 `voices get` 原样输出服务返回的 JSON，其中包括每个声音的 `voice_id`、`name`、`category`、`labels` 和 `preview_url`。用上一页的 `next_page_token` 获取下一页。

`models list` 在终端中每个语音模型输出一行：id、每次请求的文本长度上限、支持的语言数量和名称。管道输出时输出完整 JSON。不列出音效和音乐模型。

## batch

```text
audiogen batch [--keep-going] [--dry-run] <spec.json>
```

按顺序运行一个 JSON 文件中的条目。文件是一个对象，其中的 `items` 数组不能为空：

```json title="sounds.json"
{
  "output_dir": "App/Resources/Sounds",
  "defaults": { "output_format": "wav_24000" },
  "items": [
    { "command": "text-to-sound-effects convert", "output": "tap.wav",
      "body": { "text": "short soft click", "duration_seconds": 0.5 } },
    { "command": "text-to-speech convert", "output": "welcome.mp3", "output_format": "mp3_44100_128",
      "voice_name": "VOICE_NAME", "body": { "text": "Welcome back." } }
  ]
}
```

| Key | 位置 | 含义 |
| --- | --- | --- |
| `output_dir` | 顶层，可选 | 相对的 `output` 路径会放在这个文件夹中。 |
| `defaults.output_format` | 顶层，可选 | 未设置格式的条目所用的格式。 |
| `command` | 条目 | `text-to-speech convert`、`text-to-sound-effects convert` 或 `music compose`。 |
| `body` | 条目 | 请求体，使用服务的字段名（与 `--json` 相同）。 |
| `output` | 条目 | 要写入的文件。 |
| `output_format` | 条目 | 该条目的格式。 |
| `voice_id`, `voice_name` | 条目 | 声音，用于 `text-to-speech convert`。 |

| Flag | 含义 |
| --- | --- |
| `--keep-going` | 某个条目失败后继续。不加时，第一个失败就会停止整个批处理。登录失败（退出码 3）或 AI 额度用完（退出码 4）始终会停止。 |
| `--dry-run` | 输出每个条目将要运行的 `audiogen` 命令，不实际运行。 |

stdout 是一个 JSON 对象：`items`（每项包含 `index`、`ok`，以及该条目的结果或 `exit_code`）、`ok`、`generated`、`failed` 和 `total_cost_usd`。在终端中输出一行摘要，以及每个文件一行。只要有条目失败，批处理就以 `6` 退出。

## balance

显示本周期的 AI 用量与方案 AI 额度的对比。该额度与智能体聊天共用。在终端中：

```text
AI usage: <percent>% of this period's plan allowance (<n> credits beyond the plan)
credits left: <n> (<n> credits owed)
```

方案不含 AI 额度时，第一行为 `AI usage: the plan includes no AI allowance, paid with credits`。额度和积分都用完时，会追加 `! AI allowance and credits exhausted — ask your user to add credits or upgrade the plan`。管道输出时，输出包含 `used_usd`、`included_usd`、`overage_usd`、`credit_balance_usd`、`used_percent`、`overage_credits`、`credits_left`、`credits_owed`、`has_capacity` 和 `period_end` 的 JSON。方案列表见 [air.build/pricing](https://air.build/pricing/)；在 App 中查看团队用量，参见设置 › [方案与用量](https://air.build/cn/docs/settings/plan-usage/)。

## docs 和 doctor

`audiogen docs` 输出内置指南。`--topic` 改为输出某一篇内置文档：`commands/<resource>`、`recipes`、`models` 或 `common-errors`。无需登录即可使用。

`audiogen doctor` 逐项检查，每项一行，标记为 `✓`、`✗` 或 `!`：`curl`、`jq`、后端地址、团队登录、智能体身份、音频服务是否有响应、内置文档，以及终端是否为交互式。管道输出时输出 `{"ok":…,"checks":[{"name","ok","detail","critical"}]}`。关键检查失败时以 `1` 退出。

## 输出

stdout 只承载结果；进度和错误输出到 stderr。在终端中结果是易读的文本；stdout 是管道时，结果是一行 JSON。

对于写入音频的请求，终端会显示文件的绝对路径；服务报告点数时还会显示 `vendor credits: <n>`。管道输出时：

```json
{"file":"/tmp/audiogen.Xy12/text-to-speech.mp3","output_format":"default","bytes":48213,"credits":<n>,"cost_usd":<n>,"request_id":"…","request":{…}}
```

`credits`、`cost_usd` 和 `request_id` 只在服务报告时出现。未指定格式时 `output_format` 为 `default`。保存前，`audiogen` 会检查 WAV 或 MP3 文件是否名副其实。

## 退出码和错误

| Exit code | 含义 | 处理方法 |
| --- | --- | --- |
| `0` | 成功。 | — |
| `1` | 意外错误，或 `doctor` 检查未通过。 | 运行 `audiogen doctor`。 |
| `2` | 用法或参数无效（也包括服务判定为无效的内容）。 | 阅读消息，修正参数或请求体字段。 |
| `3` | 登录失败，或命令不在 AirBuild 智能体的运行环境中运行。 | 由智能体来运行。 |
| `4` | AI 额度和积分已用完。 | 请团队拥有者充值积分或更换方案。 |
| `5` | 请求被内容政策拒绝。 | 修改文本或提示词。 |
| `6` | 生成失败：没有音频、返回的不是音频，或某个批处理条目失败。 | 重试，或修改请求。 |
| `7` | 网络或服务错误，包括服务不支持的格式或功能。 | 稍后重试，或换一种格式。 |
| `8` | 找不到：声音、模型或文档主题。 | 用 `voices search` 或 `models list` 检查 id。 |
| `9` | 服务繁忙。 | 稍等后重试。 |

消息以 `Error:` 开头。常见的有：

| Message | Exit |
| --- | --- |
| `--text (or --text-file) is required.` | 2 |
| `--voice-id (or --voice-name) is required; "audiogen voices search" lists them.` | 2 |
| `--prompt or --composition-plan is required.` | 2 |
| `--json must be a JSON object (or - for stdin).` | 2 |
| `--param expects key=value.` | 2 |
| `output already exists: PATH` | 2 |
| `the request body is larger than 1 MB.` | 2 |
| `unknown flag: … (see audiogen … --help)` | 2 |
| `no voice is named "NAME"; run "audiogen voices search".` | 8 |
| `the service returned no audio.` / `the service answered JSON instead of audio…` / `the service returned something that is not a WAV file (…)` | 6 |
| `could not reach the Airbuild creative service: …` | 7 |

服务自身的错误文本会原样作为消息传出。短暂的网络故障和繁忙应答会自动重试几次，之后 `audiogen` 才会放弃。

## 示例

```bash
# Find a voice, then speak a line with it
audiogen voices search --search "calm"
audiogen text-to-speech convert --voice-name "VOICE_NAME" --text "Welcome back." --output App/Resources/welcome.mp3

# A half-second UI sound as WAV
audiogen text-to-sound-effects convert --text "soft wooden tap, dry" --duration-seconds 0.5 \
  --output-format wav_24000 --output App/Resources/tap.wav

# A one-minute instrumental loop
audiogen music compose --prompt "calm lo-fi loop, 80 BPM" --music-length-ms 60000 \
  --force-instrumental --model-id music_v2

# Check a batch file, then run it
audiogen batch --dry-run sounds.json && audiogen batch sounds.json
```
