在客户端模式下让它唱歌
客户端模式的歌声合成使用官方 VOICEVOX 引擎的 sing_frame_audio_query 与 frame_synthesis API。
与 Talk 不同,需要先构建 Score 与 Note。
前置准备
docker pull voicevox/voicevox_engine:cpu-latest
docker run --rm -p '127.0.0.1:50021:50021' voicevox/voicevox_engine:cpu-latest
构建 Score 与 Note
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),
]);
| 参数 | 类型 | 说明 |
|---|
length | int | 帧长 |
lyric | string | 歌词。为空字符串或省略时表示休止符 |
key | int|null | MIDI 音符编号 |
使用 Note::len($ticks, $bpm) 可以从 MIDI 的 ticks 与 BPM 计算帧长。将四分音符视为 480 ticks 更易理解。
基本使用
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('client', 'song.wav');
teacher 与 id
teacher:传递给 sing_frame_audio_query 的 Style ID。目前只存在 6000 一个,因此默认为 6000。
generate(id:):传递给 frame_synthesis 的 Style ID
可通过 singers() 获取可用的歌声说话人列表。
use Revolution\Voicevox\Voicevox;
$singers = Voicevox::singers();
使用 tap() 调整 F0 与音量
在 Song 中,tap() 的用途尤为清晰,可以在调整 SongAudioQuery 后再传给 generate()。
use Revolution\Voicevox\Client\SongAudioQuery;
use Revolution\Voicevox\Voicevox;
$response = Voicevox::song($score, teacher: 6000)
->tap(function (SongAudioQuery $song) {
$song->sync();
})
->generate(id: 3001);
如需分别更新 F0 与音量,请按 F0 → 音量 的顺序执行。
->tap(function (SongAudioQuery $song) {
$song->updateF0();
$song->updateVolume();
})
修改 Score 后想重新同步时,tap() 也非常方便。
->tap(function (SongAudioQuery $song) {
$song->score->notes[2] = Note::make(length: Note::len(480, 120), lyric: 'ファ', key: 65);
$song->sync();
})
下一步阅读
Score 与 Note 详解
了解帧长计算与 JSON 管理模式。
原生 Song
在通过 FFI 的原生模式下使用相同的 Score。
VOICEVOX Core for PHP 用法
了解仅使用核心库时的语音合成流程。