プラグインの使用方法
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)
シンセサイザーが作成されると、以下の関数のいずれかを呼び出してテキストを合成できます。
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データとして提供します(Blueprintsではバイト配列、C++ではTArray<uint8>
)、およびSample Rate
とNum Of Channels
を共に提供します。このデータは必要に応じて処理することができます。
再生には、Runtime Audio Importerプラグインを使用して、生のオーディオデータを再生可能なサウンドウェーブに変換することをお勧めします。ただし、これは任意であり、希望する場合は生のPCMオーディオデータを自分のソリューションで処理することもできます。
Runtime Audio Importerを使用するには、Import Audio From RAW Buffer
を呼び出し、RAW Buffer
(On Speech Result
デリゲートから)、Sample Rate
、およびNum Of Channels
を入力してサウンドウェーブを生成します。
サウンドウェーブが生成されたら、Unreal Engine内の通常のサウンドウェーブと同様に、再生したり、セーブゲームファイルに保存したり、さらなる処理を行うことができます。
- 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 documentationを参照してください。