# audiogen

> Agent 用來製作語音、音效與音樂並存成本機音訊檔，以及查詢聲音與語音模型的指令。

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

`audiogen` 用來製作音訊檔：以指定的聲音生成語音、依描述生成音效，以及依簡介生成音樂。它也能列出聲音與語音模型、從一個檔案執行多個請求、顯示團隊的 AI 用量，並檢查自身的設定。

## 執行位置

雲端團隊與 Mac 團隊中的 Agent 都能使用同一個指令。登入與 Agent 歸屬資訊來自 Agent 的環境，因此無須任何設定。它需要 `curl` 與 `jq`。請參閱[選擇 Agent 的執行位置](https://air.build/tw/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
```

| 指令 | 作用 |
| --- | --- |
| `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` 的寫法。

| 旗標 | 值 | 預設 | 意義 |
| --- | --- | --- | --- |
| `--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]
```

| 旗標 | 值 | 意義 |
| --- | --- | --- |
| `--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]
```

| 旗標 | 值 | 預設 | 意義 |
| --- | --- | --- | --- |
| `--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`：

| 旗標 | 值 | 意義 |
| --- | --- | --- |
| `--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." } }
  ]
}
```

| 鍵 | 位置 | 意義 |
| --- | --- | --- |
| `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` 使用的聲音。 |

| 旗標 | 意義 |
| --- | --- |
| `--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 額度的對照。此額度與 Agent 聊天共用。在終端機中：

```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`。導向管線時會印出 JSON，包含 `used_usd`、`included_usd`、`overage_usd`、`credit_balance_usd`、`used_percent`、`overage_credits`、`credits_left`、`credits_owed`、`has_capacity` 與 `period_end`。方案列於 [air.build/pricing](https://air.build/pricing/)；團隊在 App 中的用量請見設定 › [方案與用量](https://air.build/tw/docs/settings/plan-usage/)。

## docs 與 doctor

`audiogen docs` 會印出內附的指南。`--topic` 則改為印出一份內附文件：`commands/<resource>`、`recipes`、`models` 或 `common-errors`。無須登入即可使用。

`audiogen doctor` 會逐行檢查並標示 `✓`、`✗` 或 `!`：`curl`、`jq`、後端位址、團隊登入、Agent 身分、音訊服務是否回應、內附文件，以及終端機是否為互動式。導向管線時會印出 `{"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 檔確實是該格式。

## 結束代碼與錯誤

| 結束代碼 | 意義 | 處理方式 |
| --- | --- | --- |
| `0` | 成功。 | — |
| `1` | 非預期錯誤，或 `doctor` 檢查失敗。 | 執行 `audiogen doctor`。 |
| `2` | 用法或參數無效（服務判定為無效的內容也屬此類）。 | 閱讀訊息並修正旗標或內容欄位。 |
| `3` | 登入失敗，或指令不是在 AirBuild Agent 的環境中執行。 | 從 Agent 執行。 |
| `4` | AI 額度與積分已用完。 | 請團隊擁有者加購積分或變更方案。 |
| `5` | 請求遭內容政策拒絕。 | 修改文字或提示詞。 |
| `6` | 生成失敗：沒有音訊、不是音訊，或批次項目失敗。 | 再試一次，或修改請求。 |
| `7` | 網路或服務錯誤，包括服務不允許的格式或功能。 | 稍後再試，或改用其他格式。 |
| `8` | 找不到：聲音、模型或文件主題。 | 用 `voices search` 或 `models list` 確認 id。 |
| `9` | 服務忙碌中。 | 稍候再試。 |

訊息以 `Error:` 開頭。常見訊息：

| 訊息 | 結束代碼 |
| --- | --- |
| `--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
```
