플러그인 사용 방법
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)
synthesizer가 생성되면, 다음 함수 중 하나를 호출하여 텍스트를 합성할 수 있습니다:
Text To Speech (By Name)
(TextToSpeechByName
in C++)Text To Speech (By Object)
(TextToSpeechByObject
in C++)
이름으로
- Blueprint
- C++
Text To Speech (By Name)
함수는 UE 5.4부터 Blueprint에서 더 편리하게 사용할 수 있습니다. 다운로드된 음성 모델의 드롭다운 목록에서 음성 모델을 선택할 수 있습니다. 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([](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;
}
화자 선택
두 가지 Text To Speech 기능 모두 선택적 화자 ID 매개변수를 수용합니다. 이는 여러 화자를 지원하는 음성 모델을 사용할 때 유용합니다. GetSpeakerCountFromVoiceModel 또는 GetSpeakerCountFromModelName 함수를 사용하여 선택한 음성 모델이 여러 화자를 지원하는지 확인할 수 있습니다. 여러 화자를 사용할 수 있는 경우, Text To Speech 기능을 호출할 때 원하는 화자 ID를 지정하면 됩니다. 일부 음성 모델은 다양한 옵션을 제공합니다 - 예를 들어, English LibriTTS는 900명 이상의 다양한 화자를 선택할 수 있습니다.
재생
On Speech Result
델리게이트는 PCM 데이터로 합성된 오디오를 float 형식(Blueprint에서는 바이트 배열 또는 C++에서는 TArray<uint8>
)으로 제공하며, 함께 샘플 레이트
와 채널 수
를 제공합니다. 필요에 따라 이 데이터를 처리할 수 있습니다.
재생을 위해, Runtime Audio Importer 플러그인을 사용하여 raw 오디오 데이터를 재생 가능한 사운드 웨이브로 변환하는 것이 권장됩니다. 하지만 이는 선택 사항이며, 원하는 경우 raw PCM 오디오 데이터를 자체 솔루션으로 처리할 수 있습니다.
Runtime Audio Importer를 사용하려면 ON Speech Result
델리게이트로부터 받은 RAW Buffer
, 샘플 레이트
, 채널 수
로 Import Audio From RAW Buffer
를 호출하여 사운드 웨이브를 생성하십시오.
사운드 웨이브가 준비되면, 이를 일반적인 사운드 웨이브처럼 언리얼 엔진에서 재생, 저장 게임 파일에 저장 또는 추가 처리할 수 있습니다.
- Blueprint
- C++
텍스트를 합성하 고 오디오를 재생하는 Blueprint 노드의 예제입니다 (복사 가능한 노드):
C++에서 텍스트를 합성하고 오디오를 재생하는 방법의 예제입니다:
// Assuming "Synthesizer" is a valid and referenced URuntimeTextToSpeech object (ensure it is not eligible for garbage collection during the callback)
// Ensure "this" is a valid and referenced UObject (must not be 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
Synthesizer->TextToSpeechByName(VoiceName, 0, TEXT("Text example 123"), FOnTTSResultDelegateFast::CreateLambda([this](URuntimeTextToSpeech* TextToSpeechInstance, bool bSuccess, const TArray<uint8>& AudioData, int32 SampleRate, int32 NumOfChannels)
{
if (!bSuccess)
{
UE_LOG(LogTemp, Error, TEXT("TextToSpeech failed"));
return;
}
// Create the Runtime Audio Importer to process the audio data
URuntimeAudioImporterLibrary* RuntimeAudioImporter = URuntimeAudioImporterLibrary::CreateRuntimeAudioImporter();
// Prevent the RuntimeAudioImporter from being garbage collected by adding it to the root (you can also use a UPROPERTY, TStrongObjectPtr, etc.)
RuntimeAudioImporter->AddToRoot();
RuntimeAudioImporter->OnResultNative.AddWeakLambda(RuntimeAudioImporter, [this](URuntimeAudioImporterLibrary* Importer, UImportedSoundWave* ImportedSoundWave, ERuntimeImportStatus Status)
{
// Once done, remove it from the root to allow garbage collection
Importer->RemoveFromRoot();
if (Status != ERuntimeImportStatus::SuccessfulImport)
{
UE_LOG(LogTemp, Error, TEXT("Failed to import audio, status: %s"), *UEnum::GetValueAsString(Status));
return;
}
// Play the imported sound wave (ensure a reference is kept to prevent garbage collection)
UGameplayStatics::PlaySound2D(GetWorld(), ImportedSoundWave);
});
RuntimeAudioImporter->ImportAudioFromRAWBuffer(AudioData, ERuntimeRAWAudioFormat::Float32, SampleRate, NumOfChannels);
}));
return;
}
Runtime Audio Importer 플러그인은 파일에 오디오 데이터를 내보내거나, SoundCue, MetaSound 등으로 전달하는 추가 기능도 제공합니다. 더 자세한 내용은 Runtime Audio Importer 문서를 확인하세요.