如何使用插件
Runtime Text To Speech 插件使用可下载的语音模型将文本合成为语音。这些模型在编辑器内的插件设置中进行管理、下载,并打包以供运行时使用。按照以下步骤开始使用。
编辑器端
按照此处所述,为您的项目下载合适的语音模型。您可以同时下载多个语音模型。
运行时
使用 CreateRuntimeTextToSpeech
函数创建合成器。确保保持对其的引用(例如,在Blueprint中作为单独的变量或在 C++ 中为 UPROPERTY)以防止其被垃圾回收。
- Blueprint
- C++
// Create the Runtime Text To Speech synthesizer in C++
URuntimeTextToSpeech* Synthesizer = URuntimeTextToSpeech::CreateRuntimeTextToSpeech();
// Ensure the synthesizer is referenced correctly to prevent garbage collection (e.g. as a UPROPERTY)
一旦合成器创建完成,你可以调用以下任意功能来合成文本:
Text To Speech (By Name)
(TextToSpeechByName
in C++)Text To Speech (By Object)
(TextToSpeechByObject
in C++)
按名称
- Blueprint
- C++
从 UE 5.4 开始,Text To Speech (By Name)
函数在Blueprint中更为方便。它允许您从已下载模型的下拉列表中选择语音模型。在 低于 5.3 的 UE 版本 中,此下拉列表不会出现,因此如果您使用的是旧版本,您需要手动遍历 GetDownloadedVoiceModels
返回的语音模型数组,以选择您需要的模型。
在 C++ 中,由于缺少下拉列表,选择语音模型可能会稍微复杂一些。您可以使用 GetDownloadedVoiceModelNames
函数来检索已下载语音模型的名称,并选择您需要的模型。随后,您可以调用 TextToSpeechByName
函数,使用选定的语音模型名称来合成文本。
// Assuming "Synthesizer" is a valid and referenced URuntimeTextToSpeech object (ensure it is not eligible for garbage collection during the callback)
TArray<FName> DownloadedVoiceNames = URuntimeTTSLibrary::GetDownloadedVoiceModelNames();
// If there are downloaded voice models, use the first one to synthesize text, just as an example
if (DownloadedVoiceNames.Num() > 0)
{
const FName& VoiceName = DownloadedVoiceNames[0]; // Select the first available voice model
Synthesizer->TextToSpeechByName(VoiceName, 0, TEXT("Text example 123"), FOnTTSResultDelegateFast::CreateLambda([](bool bSuccess, const TArray<uint8>& AudioData, int32 SampleRate, int32 NumChannels)
{
UE_LOG(LogTemp, Log, TEXT("TextToSpeech result: %s, AudioData size: %d, SampleRate: %d, NumChannels: %d"), bSuccess ? TEXT("Success") : TEXT("Failed"), AudioData.Num(), SampleRate, NumChannels);
}));
return;
}
按对象
- Blueprint
- C++
Text To Speech (By Object)
函数适用于所有版本的 Unreal Engine,但将语音模型作为资产引用的下拉列表显示,直观性较差。此方法适用于 UE 5.3 及更早版本,或在项目中因任何原因需要直接引用语音模型资产的情况。
如果您已下载模型但无法看到它们,请打开 Voice Model 下拉菜单,点击设置(齿轮图标),然后启用 Show Plugin Content 和 Show Engine Content 以使模型可见。
在 C++ 中,由于缺少下拉列表,选择语音模型可能稍微复杂。您可以使用 GetDownloadedVoiceModelNames
函数检索已下载的语音模型名称并选择需要的模型。然后,可以调用 GetVoiceModelFromName
函数获取语音模型对象,并传递给 TextToSpeechByObject
函数以合成文本。
// Assuming "Synthesizer" is a valid and referenced URuntimeTextToSpeech object (ensure it is not eligible for garbage collection during the callback)
TArray<FName> DownloadedVoiceNames = URuntimeTTSLibrary::GetDownloadedVoiceModelNames();
// If there are downloaded voice models, use the first one to synthesize text, for example
if (DownloadedVoiceNames.Num() > 0)
{
const FName& VoiceName = DownloadedVoiceNames[0]; // Select the first available voice model
TSoftObjectPtr<URuntimeTTSModel> VoiceModel;
if (!URuntimeTTSLibrary::GetVoiceModelFromName(VoiceName, VoiceModel))
{
UE_LOG(LogTemp, Error, TEXT("Failed to get voice model from name: %s"), *VoiceName.ToString());
return;
}
Synthesizer->TextToSpeechByObject(VoiceModel, 0, TEXT("Text example 123"), FOnTTSResultDelegateFast::CreateLambda([](bool bSuccess, const TArray<uint8>& AudioData, int32 SampleRate, int32 NumChannels)
{
UE_LOG(LogTemp, Log, TEXT("TextToSpeech result: %s, AudioData size: %d, SampleRate: %d, NumChannels: %d"), bSuccess ? TEXT("Success") : TEXT("Failed"), AudioData.Num(), SampleRate, NumChannels);
}));
return;
}
发言者选择
两个文本到语音功能都接受一个可选的发言者 ID 参数,当使用支持多个发言者的语音模型时,这个参数非常有用。您可以使用 GetSpeakerCountFromVoiceModel 或 GetSpeakerCountFromModelName 函数检查所选语音模型是否支持多个发言者。如果可以选择多个发言者,只需在调用文本到语音功能时指定您想要的发言者 ID。一些语音模型提供了丰富的选项,例如,English LibriTTS 就包含了 超过 900 个不同的发言者 可供选择。
播放
On Speech Result
委托提供了合成后音频的 PCM 数据,以 float 格式(在 Blueprints 中为字节数组或在 C++ 中为 TArray<uint8>
),以及 Sample Rate
和 Num Of Channels
。您可以根据需要处理这些数据。
为了播放,推荐使用 Runtime Audio Importer 插件将原始音频数据转换为可播放的声音波。不过这不是必须的,您也可以用自己的解决方案处理原始 PCM 音频数据。