如何使用该插件
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及更早版本)
常规文本转语音
按名称选择
- 蓝图
- C++
从UE 5.4开始,Text To Speech (By Name)
函数在蓝图中使用更为便捷。它允许您从已下载模型的下拉列表中选择语音模型。在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;
}
按对象调用
- 蓝图
- C++
Text To Speech (By Object)
函数在所有版本的虚幻引擎中均可使用,但会将语音模型显示为资源引用下拉列表,这种方式不够直观。此方法适用于 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;
}
流式文本转语音
对于较长文本或需要实时处理生成音频数据的场景,您可以使用以下流式版本的文本转语音功能:
Streaming Text To Speech (By Name)
(C++中为StreamingTextToSpeechByName
)Streaming Text To Speech (By Object)
(C++中为StreamingTextToSpeechByObject
)
这些函数会在音频数据生成时以分块形式提供,允许立即处理而无需等待整个合成过程完成。这对于实时音频播放、实时可视化等需要增量处理语音数据的应用场景非常有用。
通过名称流式处理
- 蓝图
- C++
Streaming Text To Speech (By Name)
函数与常规版本类似,但会通过On Speech Chunk
委托以分块形式提供音频数据。