forked from kris-work/amazon-polly-metahumans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeechComponent.cpp
213 lines (193 loc) · 9 KB
/
SpeechComponent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "SpeechComponent.h"
#include <fstream>
#include "Json.h"
#include "UObject/Class.h"
#include "TimerManager.h"
#include "Kismet/GameplayStatics.h"
#include <aws/core/auth/AWSCredentialsProviderChain.h>
#include <aws/core/client/AWSClient.h>
#include "UnrealAWSUtils.h"
#include "GenerateSpeechAction.h"
using UnrealAWSUtils::AwsStringToFString;
using UnrealAWSUtils::FStringToAwsString;
DEFINE_LOG_CATEGORY(LogPollyMsg);
USpeechComponent::USpeechComponent() {
// Set this component to be initialized when the game starts, and to be ticked every frame.
// You can turn these features off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = false;
bWantsInitializeComponent = true;
}
void USpeechComponent::InitializeComponent() {
Super::InitializeComponent();
InitializePollyClient();
}
void USpeechComponent::GenerateSpeech(
UObject* WorldContextObject,
const FString Text,
const EVoiceId VoiceId,
struct FLatentActionInfo LatentActionInfo,
EGenerateSpeechExecPins& GenerateSpeechExecPins
) {
if (UWorld* World = GEngine->GetWorldFromContextObjectChecked(WorldContextObject)) {
FLatentActionManager& LatentActionManager = World->GetLatentActionManager();
UObject* CallbackTarget = LatentActionInfo.CallbackTarget;
int32 UUID = LatentActionInfo.UUID;
if (LatentActionManager.FindExistingAction<FPendingLatentAction>(CallbackTarget, UUID) == NULL) {
// LatentActionManager takes ownership of FGenerateSpeechAction (it calls delete).
LatentActionManager.AddNewAction(CallbackTarget, UUID, new FGenerateSpeechAction(LatentActionInfo, this, Text, VoiceId, GenerateSpeechExecPins));
}
}
}
USoundWaveProcedural* USpeechComponent::StartSpeech() {
FScopeLock lock(&Mutex);
if (VisemeEventArray.Num() == 0) {
UE_LOG(LogPollyMsg, Error, TEXT("Failed to start speech. GenerateSpeech must be invoked before StartSpeech."));
return nullptr;
}
else {
bIsSpeaking = true;
CurrentVisemeIndex = 0;
CurrentViseme = VisemeEventArray[CurrentVisemeIndex].Viseme;
StartTimePoint = std::chrono::steady_clock::now();
float CurrentVisemeDurationSeconds = VisemeEventArray[CurrentVisemeIndex].TimeMilliseconds / 1000.0f;
SetTimer(CurrentVisemeDurationSeconds);
return QueuePollyAudio();
}
}
EViseme USpeechComponent::GetCurrentViseme() {
FScopeLock lock(&Mutex);
return CurrentViseme;
}
bool USpeechComponent::IsSpeaking() {
FScopeLock lock(&Mutex);
return bIsSpeaking;
}
void USpeechComponent::PlayNextViseme() {
FScopeLock lock(&Mutex);
CurrentVisemeIndex++;
ClearTimer();
if (CurrentVisemeIndex == VisemeEventArray.Num() || VisemeEventArray.Num() == 0) {
bIsSpeaking = false;
return;
}
else {
CurrentViseme = VisemeEventArray[CurrentVisemeIndex].Viseme;
auto CurrentTimePoint = std::chrono::steady_clock::now();
float SecondsSinceStart = std::chrono::duration<float, std::milli>(CurrentTimePoint - StartTimePoint).count() / 1000.0f;
float CurrentVisemeDurationSeconds = fmaxf(VisemeEventArray[CurrentVisemeIndex].TimeMilliseconds / 1000.0f - SecondsSinceStart, 0);
SetTimer(CurrentVisemeDurationSeconds);
}
}
void USpeechComponent::GenerateSpeechSync(const FString Text, const EVoiceId VoiceId) {
if (Text.IsEmpty()) {
UE_LOG(LogPollyMsg, Error, TEXT("Cannot generate speech (check input text)."));
return;
}
if (IsSpeaking()) {
UE_LOG(LogPollyMsg, Error, TEXT("Cannot generate speech during playback."));
return;
}
if (SynthesizeAudio(Text, VoiceId) && SynthesizeVisemes(Text, VoiceId)) {
UE_LOG(LogPollyMsg, Display, TEXT("Polly called successfully!"));
}
}
bool USpeechComponent::SynthesizeAudio(const FString& Text, const EVoiceId VoiceId) {
PollyOutcome PollyAudioOutcome = MyPollyClient->SynthesizeSpeech(CreatePollyAudioRequest(Text, VoiceId));
if (PollyAudioOutcome.IsSuccess) {
FScopeLock lock(&Mutex);
Audiobuffer = PollyAudioOutcome.StreamBuffer;
}
else {
UE_LOG(LogPollyMsg, Error, TEXT("Polly failed to generate audio file. Error: %s"), *AwsStringToFString(PollyAudioOutcome.PollyErrorMsg));
}
return PollyAudioOutcome.IsSuccess;
}
bool USpeechComponent::SynthesizeVisemes(const FString& Text, const EVoiceId VoiceId) {
PollyOutcome PollyVisemeOutcome = MyPollyClient->SynthesizeSpeech(CreatePollyVisemeRequest(Text, VoiceId));
if (PollyVisemeOutcome.IsSuccess) {
FScopeLock lock(&Mutex);
FString VisemeJson;
FFileHelper::BufferToString(VisemeJson, PollyVisemeOutcome.StreamBuffer.GetData(), PollyVisemeOutcome.StreamBuffer.Num());
GenerateVisemeEvents(VisemeJson);
}
else {
UE_LOG(LogPollyMsg, Error, TEXT("Polly failed to generate visemes. Error: %s"), *AwsStringToFString(PollyVisemeOutcome.PollyErrorMsg));
}
return PollyVisemeOutcome.IsSuccess;
}
USoundWaveProcedural* USpeechComponent::QueuePollyAudio() {
USoundWaveProcedural* PollyAudio = NewObject<USoundWaveProcedural>();
PollyAudio->SetSampleRate(16000);
PollyAudio->NumChannels = 1;
PollyAudio->DecompressionType = DTYPE_Procedural;
int32 BitRate = 16 * PollyAudio->NumChannels * PollyAudio->GetSampleRateForCurrentPlatform();
PollyAudio->Duration = Audiobuffer.Num() * 8.0f / BitRate;
PollyAudio->QueueAudio(Audiobuffer.GetData(), Audiobuffer.Num());
return PollyAudio;
}
Aws::Polly::Model::SynthesizeSpeechRequest USpeechComponent::CreatePollyAudioRequest(const FString& Text, const EVoiceId VoiceId) const {
Aws::Polly::Model::SynthesizeSpeechRequest PollyRequest;
PollyRequest.SetText(FStringToAwsString(Text));
PollyRequest.SetVoiceId(ToPollyVoiceId(VoiceId));
PollyRequest.SetEngine(ToPollyVoiceEngine(VoiceId));
PollyRequest.SetOutputFormat(Aws::Polly::Model::OutputFormat::pcm);
return PollyRequest;
}
Aws::Polly::Model::SynthesizeSpeechRequest USpeechComponent::CreatePollyVisemeRequest(const FString& Text, const EVoiceId VoiceId) const {
Aws::Polly::Model::SynthesizeSpeechRequest PollyRequest;
PollyRequest.SetText(FStringToAwsString(Text));
PollyRequest.SetVoiceId(ToPollyVoiceId(VoiceId));
PollyRequest.SetEngine(ToPollyVoiceEngine(VoiceId));
PollyRequest.SetOutputFormat(Aws::Polly::Model::OutputFormat::json);
PollyRequest.AddSpeechMarkTypes(Aws::Polly::Model::SpeechMarkType::viseme);
return PollyRequest;
}
void USpeechComponent::GenerateVisemeEvents(FString VisemeJson) {
VisemeEventArray = {};
TArray<FString> VisemeStrings;
VisemeJson.ParseIntoArray(VisemeStrings, TEXT("\n"), true);
for (FString VisemeSet : VisemeStrings) {
TSharedPtr<FJsonObject> JsonParsed = MakeShareable(new FJsonObject);
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(VisemeSet);
FString OutString;
double OutNumber;
if (FJsonSerializer::Deserialize(JsonReader, JsonParsed) && JsonParsed->TryGetStringField("value", OutString) && JsonParsed->TryGetNumberField("time", OutNumber)) {
VisemeEvent CurrentVisemeEvent;
CurrentVisemeEvent.Viseme = GetVisemeValueFromString(JsonParsed->GetStringField("value"));
CurrentVisemeEvent.TimeMilliseconds = JsonParsed->GetIntegerField("time");
VisemeEventArray.Add(CurrentVisemeEvent);
}
else {
UE_LOG(LogPollyMsg, Error, TEXT("Failed to parse json formatted viseme sequence returned by Amazon Polly."));
VisemeEventArray = {};
Audiobuffer.Empty();
break;
}
}
}
void USpeechComponent::InitializePollyClient() {
MyPollyClient = MakeUnique<PollyClient>();
}
void USpeechComponent::SetTimer(float CurrentVisemeDurationSeconds) {
GetWorld()->GetTimerManager().SetTimer(CountdownTimerHandle, this, &USpeechComponent::PlayNextViseme, 1.0f, true, CurrentVisemeDurationSeconds);
}
void USpeechComponent::ClearTimer() {
GetWorld()->GetTimerManager().ClearTimer(CountdownTimerHandle);
}