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

# 原生模式：Song（歌声合成） - VOICEVOX for Laravel

> 介绍在 VOICEVOX for Laravel 的原生模式下，通过 PHP FFI 直接调用 VOICEVOX CORE 进行歌声合成的方法。

## 在原生模式下让它唱歌

原生模式的歌声合成通过 PHP FFI 直接调用 VOICEVOX CORE。

构建 `Score` 与 `Note` 的方式与客户端模式一致。

## 前置准备

1. 已完成 [安装与配置](/zh/packages/laravel-voicevox/installation)
2. 已配置好 [VOICEVOX Core for PHP](/zh/packages/voicevox-core-php)
3. 已在 `.env` 中设置 `VOICEVOX_CORE_PATH`

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

使用歌声模型时，请确认 `config/voicevox.php` 的 `core.vvms` 中包含 `s0.vvm`。

## 构建 `Score` 与 `Note`

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

$score = Score::make([
    Note::make(length: 15),
    Note::make(length: Note::len(ticks: 480, bpm: 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),
]);
```

## 基本使用

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

$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 = song($score, teacher: 6000)
    ->generate(id: 3001);

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

使用相同的 `Score`，就能在客户端模式与原生模式中复用同一份乐谱。

## 使用 `tap()` 重新同步 F0 与音量

原生 Song 中 `tap()` 同样有用。调整完 `SongAudioQuery` 后可直接传给 `generate()`。

```php theme={null}
use Revolution\Voicevox\Song\SongAudioQuery;
use function Revolution\Voicevox\song;

$response = song($score, teacher: 6000)
    ->tap(function (SongAudioQuery $song) {
        $song->sync();
    })
    ->generate(id: 3001);
```

如需分别更新 F0 与音量，请务必按以下顺序执行。

```php theme={null}
->tap(function (SongAudioQuery $song) {
    $song->updateF0();
    $song->updateVolume();
})
```

## 与客户端模式的区别

| 项目               | 客户端 Song           | 原生 Song  |
| ---------------- | ------------------ | -------- |
| 引擎启动             | 需要                 | 不需要      |
| FFI              | 不需要                | 需要       |
| API 入口           | `Voicevox::song()` | `song()` |
| `Score` / `Note` | 通用                 | 通用       |

<Info>
  FFI 的机制请参见 [PHP FFI](/zh/advanced/ffi)；仅使用核心库时的流程请参见 [VOICEVOX Core for PHP 用法](/zh/packages/voicevox-core-php/usage)。
</Info>

## 下一步阅读

<Columns cols={3}>
  <Card title="Score 与 Note 详解" href="/zh/packages/laravel-voicevox/song-score-note" icon="list-music">
    了解 Song 乐谱数据的实用管理方法。
  </Card>

  <Card title="客户端 Song" href="/zh/packages/laravel-voicevox/client-song" icon="music">
    对比与 HTTP 客户端版的差异。
  </Card>

  <Card title="VOICEVOX for Laravel 概览" href="/zh/packages/laravel-voicevox" icon="book-open">
    汇总查看各模式与相关页面。
  </Card>
</Columns>


## Related topics

- [客户端模式：Song（歌声合成） - VOICEVOX for Laravel](/zh/packages/laravel-voicevox/client-song.md)
- [原生模式：Talk（文本语音合成） - VOICEVOX for Laravel](/zh/packages/laravel-voicevox/native-talk.md)
- [引擎 API 模式：Song（歌声合成） - VOICEVOX for Laravel](/zh/packages/laravel-voicevox/engine-song.md)
- [原生模式 - 预设 - VOICEVOX for Laravel](/zh/packages/laravel-voicevox/native-presets.md)
- [原生模式 - 用户词典 - VOICEVOX for Laravel](/zh/packages/laravel-voicevox/native-user-dict.md)
