跳转到主要内容

在原生模式下让它唱歌

原生模式的歌声合成通过 PHP FFI 直接调用 VOICEVOX CORE。 构建 ScoreNote 的方式与客户端模式一致。

前置准备

  1. 已完成 安装与配置
  2. 已配置好 VOICEVOX Core for PHP
  3. 已在 .env 中设置 VOICEVOX_CORE_PATH
VOICEVOX_CORE_PATH=/path/to/voicevox_core/
使用歌声模型时,请确认 config/voicevox.phpcore.vvms 中包含 s0.vvm

构建 ScoreNote

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),
]);

基本使用

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()
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 与音量,请务必按以下顺序执行。
->tap(function (SongAudioQuery $song) {
    $song->updateF0();
    $song->updateVolume();
})

与客户端模式的区别

项目客户端 Song原生 Song
引擎启动需要不需要
FFI不需要需要
API 入口Voicevox::song()song()
Score / Note通用通用
FFI 的机制请参见 PHP FFI;仅使用核心库时的流程请参见 VOICEVOX Core for PHP 用法

下一步阅读

Score 与 Note 详解

了解 Song 乐谱数据的实用管理方法。

客户端 Song

对比与 HTTP 客户端版的差异。

VOICEVOX for Laravel 概览

汇总查看各模式与相关页面。
最后修改于 2026年7月13日