如何使用该插件
Runtime Text To Speech 插件使用可下载的语音模型将文本合成为语音。这些模型在编辑器内的插件设置中进行管理、下载,并打包以供运行时使用。请按照以下步骤开始使用。
编辑器端
按照此处的说明为您的项目下载合适的语音模型。您可以同时下载多个语音模型。
运行时端
使用 CreateRuntimeTextToSpeech
函数创建合成器。请确保您保持对它的引用(例如,在 Blueprints 中作为单独的变量,或在 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)
语音合成
该插件提供两种文本转语音合成模式:
- 常规文本转语音:合成整个文本并在完成后返回完整的音频
- 流式文本转语音:在音频生成时提供音频块,允 许实时处理
每种模式支持两种选择语音模型的方法:
- 按名称:通过名称选择语音模型(推荐用于 UE 5.4+)
- 按对象:通过直接引用选择语音模型(推荐用于 UE 5.3 及更早版本)
常规文本转语音
按名称
- Blueprint
- C++
从 UE 5.4 开始,Text To Speech (By Name)
函数在 Blueprints 中使用更加方便。它允许您从已下载模型的下列列表中选择语音模型。在 UE 5.3 及以下版本中,此下拉列表不会出现,因此如果您使用的是旧版本,则需要手动遍历 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([](URuntimeTextToSpeech* TextToSpeechInstance, 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;
}
通过对象 (By Object)
- 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([](URuntimeTextToSpeech* TextToSpeechInstance, 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;
}