Synthesize Text
This section covers how to synthesize text using the plugin.
Finding a Voice
Before synthesizing text, we have to choose a voice. You can find the list of voice with samples here.
You can also call ListVoices
to get the list of voices available.
BlueprintsC++
// Lists voices from the API.
GoogleTTS::ListVoices(FGTTSVoicesCallback::CreateCallback([](TArray<FGTTSVoice> Voices, FGTTSError Error)
{
// Checks for error.
if (Error)
{
UE_LOG(LogTemp, Error, TEXT("Failed to list voices: %s"), *Error.Message);
}
// No error, the voices are available.
else
{
// Example: we iterate over the voices and print them to log.
for (const FGTTSVoice& Voice : Voices)
{
UE_LOG(LogTemp, Log, TEXT("Voice name: "), *Voice.Name);
}
}
}));

Synthesizing Text and Playing it
Voices can be synthesizing using Synthesize Voice
.
BlueprintsC++
const FString Text = TEXT("Hi guys, I am an American voice");
// Synthesize the text.
GoogleTTS::SynthesizeTextToSoundWave(
/* The voice we want to use. */
TEXT("en-US"),
/* Voice config. Here, we just use the default one. */
{},
/* The text to synthesize. */
Text,
/* Callback called when the text has been synthesized. */
FGTTSVoicesCallback::CreateLambda([](class USoundWave* SoundWave, FGTTSError Error)
{
// Checks for error.
if (Error)
{
UE_LOG(LogTemp, Error, TEXT("Failed to synthesize text: %s"), *Error.Message);
}
// Text was synthesized.
else
{
// Do something with SoundWave.
UGameplayStatics::PlaySound2D(WorldContext, SoundWave);
}
}));

If
Cached
istrue
, voices are cached and trying to synthesize the same text for the same voice won't use the API quota.