> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Engine API 模式:Song(歌聲語音合成)- VOICEVOX for Laravel

> 說明如何在 Laravel 應用內的 VOICEVOX Engine 相容 API 使用歌聲合成 endpoint。

## 以 Engine API 讓角色歌唱

Engine API 模式也提供官方 VOICEVOX 引擎相容的歌聲 endpoint。

<Info>
  Engine API 的基本設定請先確認 [Engine API: Talk](/zh-TW/packages/laravel-voicevox/engine-talk)。
</Info>

## 事前準備

為使用歌聲模型,請確認 `config/voicevox.php` 的 `core.vvms` 中包含 `s0.vvm`(預設已包含)。

```dotenv theme={null}
VOICEVOX_CORE_PATH=/path/to/voicevox_core/
```

## 啟動 Laravel 版引擎

```shell theme={null}
php artisan serve --port=50513
```

## 產生歌聲語音

### 1. 建立 `sing_frame_audio_query`

```shell theme={null}
curl -s -X POST "http://127.0.0.1:50513/sing_frame_audio_query?speaker=6000" \
  -H "Content-Type: application/json" \
  -d '{
    "notes": [
      {"id": "a", "key": null, "frame_length": 15, "lyric": ""},
      {"id": "b", "key": 60, "frame_length": 94, "lyric": "ド"},
      {"id": "c", "key": 62, "frame_length": 94, "lyric": "レ"},
      {"id": "d", "key": 64, "frame_length": 187, "lyric": "ミ"},
      {"id": "e", "key": null, "frame_length": 2, "lyric": ""}
    ]
  }' \
  > frame_audio_query.json
```

`speaker` 請指定 `sing` 或 `singing_teacher` 類別的 style ID(例: 6000)。

### 2. 以 `frame_synthesis` 合成語音

```shell theme={null}
curl -s -X POST "http://127.0.0.1:50513/frame_synthesis?speaker=3001" \
  -H "Content-Type: application/json" \
  -d @frame_audio_query.json \
  > song.wav
```

`speaker` 請指定 `sing` 或 `frame_decode` 類別的 style ID(例: 3001)。

## 從 Laravel Client 使用

可指向 Laravel 版引擎使用 `Voicevox::song()`。

```php theme={null}
// config/voicevox.php
'client' => [
    'url' => env('VOICEVOX_URL', 'http://127.0.0.1:50513'),
],
```

```php theme={null}
use Revolution\Voicevox\Song\Note;
use Revolution\Voicevox\Song\Score;
use Revolution\Voicevox\Voicevox;

$score = Score::make([
    Note::make(length: 15),
    Note::make(length: Note::len(480, 120), lyric: 'ド', key: 60),
    Note::make(length: Note::len(480, 120), lyric: 'レ', key: 62),
    Note::make(length: Note::len(960, 120), lyric: 'ミ', key: 64),
    Note::make(length: 2),
]);

$response = Voicevox::song($score, teacher: 6000)->generate(id: 3001);

$response->storeAs('engine', 'song.wav');
```

## F0、音量調整 endpoint

```shell theme={null}
curl -s -X POST "http://127.0.0.1:50513/sing_frame_f0?speaker=6000" \
  -H "Content-Type: application/json" \
  -d '{"score": {...}, "frame_audio_query": {...}}' \
  > f0.json

curl -s -X POST "http://127.0.0.1:50513/sing_frame_volume?speaker=6000" \
  -H "Content-Type: application/json" \
  -d '{"score": {...}, "frame_audio_query": {...}}' \
  > volume.json
```

<Warning>
  若要分別更新 F0 與音量,請務必依 **F0 → 音量** 順序執行。
</Warning>

## Song 相關的對應狀況

| Endpoint                       | Laravel 版 | Fallback | 備註                 |
| ------------------------------ | --------- | -------- | ------------------ |
| `POST /sing_frame_audio_query` | ✅         | ✅        |                    |
| `POST /frame_synthesis`        | ✅         | ✅        |                    |
| `POST /sing_frame_f0`          | ✅         | ✅        |                    |
| `POST /sing_frame_volume`      | ✅         | ✅        | 依 f0 → volume 順序更新 |
| `GET /singers`                 | ✅         | ✅        |                    |
| `GET /singer_info`             | ✅         | ✅        | 需要安裝資源             |

## 下一步閱讀

<Columns cols={3}>
  <Card title="Score 與 Note 詳解" href="/zh-TW/packages/laravel-voicevox/song-score-note" icon="list-music">
    確認 Song API 使用的 `Score` / `Note` 詳細內容。
  </Card>

  <Card title="Engine API: Talk" href="/zh-TW/packages/laravel-voicevox/engine-talk" icon="message-circle">
    確認文字語音合成 endpoint 用法。
  </Card>

  <Card title="Client: Song" href="/zh-TW/packages/laravel-voicevox/client-song" icon="music">
    確認將相同譜面直接送到官方引擎的模式。
  </Card>
</Columns>


## Related topics

- [Client 模式:Song(歌聲語音合成)- VOICEVOX for Laravel](/zh-TW/packages/laravel-voicevox/client-song.md)
- [Native 模式:Song(歌聲語音合成)- VOICEVOX for Laravel](/zh-TW/packages/laravel-voicevox/native-song.md)
- [Engine API 模式:Talk(文字語音合成)- VOICEVOX for Laravel](/zh-TW/packages/laravel-voicevox/engine-talk.md)
- [Client 模式:Talk(文字語音合成)- VOICEVOX for Laravel](/zh-TW/packages/laravel-voicevox/client-talk.md)
- [Native 模式:Talk(文字語音合成)- VOICEVOX for Laravel](/zh-TW/packages/laravel-voicevox/native-talk.md)
