-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathSpeechComponent.h
228 lines (219 loc) · 8.63 KB
/
SpeechComponent.h
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* 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.
*/
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Sound/SoundWaveProcedural.h"
#include <aws/core/Aws.h>
#include "PollyClient.h"
#include <chrono>
#include "Runtime/Engine/Public/LatentActions.h"
#include "Viseme.h"
#include "VoiceId.h"
#include "SpeechComponent.generated.h"
DECLARE_LOG_CATEGORY_EXTERN(LogPollyMsg, Log, All);
/**
* Represents the multiple output pins that can be invoked
* on completion of the latent GenerateSpeech function.
*/
UENUM(BlueprintType)
enum class EGenerateSpeechExecPins : uint8 {
Success UMETA(DisplayName = "Success"),
Failure UMETA(DisplayName = "Failure")
};
/**
* Struct containing a single viseme and its corresponding timestamp returned by Polly
* to be used in setting CurrentViseme and CurrentVisemeDurationSeconds states
*/
struct VisemeEvent {
EViseme Viseme;
int TimeMilliseconds;
};
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class AMAZONPOLLYMETAHUMAN_API USpeechComponent : public UActorComponent
{
GENERATED_BODY()
public:
/**
* Default constructor.
*/
USpeechComponent();
/**
* Initializes the component. See UActorComponent::InitializeComponent for details.
*/
virtual void InitializeComponent() override;
/**
* Blueprint function for calling Polly API to generate Viseme/Audio data.
* One of the GenerateSpeech* functions must be called before StartSpeech() function.
* @param WorldContextObject description
* @param Text - the text to be synthesized by Polly (the maximum length of input text can be up to 3000 characters)
* @param VoiceId - enum for VoiceId for use in calling Polly
* @param LatentInfo description
* @param parameter-name description
*/
UFUNCTION(
BlueprintCallable,
Category = "Amazon Polly",
Meta = (
Latent,
LatentInfo = "LatentInfo",
HidePin = "WorldContextObject",
DefaultToSelf = "WorldContextObject",
ExpandEnumAsExecs = "EGenerateSpeechExecPins"
)
)
void GenerateSpeech(
UObject* WorldContextObject,
const FString Text,
const EVoiceId VoiceId,
struct FLatentActionInfo LatentInfo,
EGenerateSpeechExecPins& EGenerateSpeechExecPins
);
/**
* Starts the Animation playback and returns an Audio object to be played in Blueprints.
* GenerateSpeech function must be called beforehand.
* @return A USoundWaveProcedural object containing the audio synthesized from Polly
*/
UFUNCTION(BlueprintCallable, Category = "Amazon Polly")
USoundWaveProcedural* StartSpeech();
/**
* Returns the current viseme corresponding to the last invocation of StartSpeech. Assuming
* the sound returned by StartSpeech began playback immediately following its creation, the
* current viseme returned by this function should be synchronized to the current sound
* being played.
*
* @return the current viseme
*/
UFUNCTION(BlueprintPure, Category = "Amazon Polly")
EViseme GetCurrentViseme();
/**
* Returns true if the speech component is still playing a viseme sequence
*
* @return true if the speech component is still playing a viseme sequence
*/
UFUNCTION(BlueprintPure, Category = "Amazon Polly")
bool IsSpeaking();
protected:
/**
* Blueprint function for calling Polly API to generate Viseme/Audio data.
* One of the GenerateSpeech* functions must be called before StartSpeech() function.
* @param text - the text to be synthesized by Polly (the maximum length of input text can be up to 3000 characters)
* @param VoiceId - enum for VoiceId for use in calling Polly
*/
virtual void GenerateSpeechSync(const FString text, const EVoiceId VoiceId);
/**
* Each time this method is called it plays back the next viseme in the speech.
*/
virtual void PlayNextViseme();
/**
* Enum representing a single viseme state, to be used in setting a Viseme variable in Blueprints
*/
EViseme CurrentViseme;
/**
* Enum representing a VoiceId to set a voice when calling Polly
*/
EVoiceId Voice_ID;
/**
* Boolean flag to keep GenerateSpeech from being invocated while the Actor is speaking
*/
bool bIsSpeaking = false;
/**
* Index variable used to iterate through VisemeDataArray in UpdateVisemeAndDurationStates function
*/
int CurrentVisemeIndex;
/**
* Buffer containing audio data from Polly stream
*/
TArray<uint8> Audiobuffer;
/**
* PollyClient state for calling Polly SDK
*/
TUniquePtr<PollyClient> MyPollyClient;
/**
* Array containing custom data structure that holds viseme and time data for each Viseme from Polly
*/
TArray<VisemeEvent> VisemeEventArray;
private:
/**
* Calls the PollyClient to generate Polly Audio data
* @param text - the text synthesized by Polly
* @param VoiceId - the voice of the synthesized audio
* @return bool - boolean indicating success/failure of Polly call
*/
bool SynthesizeAudio(const FString& text, const EVoiceId VoiceId);
/**
* Calls the PollyClient to generate Polly Viseme data
* @param text - the text synthesized by Polly
* @param VoiceId - the voice of the synthesized data
* @return bool - boolean indicating success/failure of Polly call
*/
bool SynthesizeVisemes(const FString& text, const EVoiceId VoiceId);
/**
* Returns a PollyRequest that is configured to return pcm audio data with a given text and VoiceId
* @param text - the text to be synthesized (SetText)
* @param VoiceId - the VoiceId for the synthesized audio
* @return PollyRequest - the configured PollyRequest
*/
Aws::Polly::Model::SynthesizeSpeechRequest CreatePollyAudioRequest(const FString& text, const EVoiceId VoiceId) const;
/**
* Returns a PollyRequest that is configured to return viseme and timestamp data in a json format
* @param text - the text to be synthesized (SetText)
* @param VoiceId - the VoiceId for the synthesized audio
* @return PollyRequest - the configured PollyRequest
*/
Aws::Polly::Model::SynthesizeSpeechRequest CreatePollyVisemeRequest(const FString& text, const EVoiceId VoiceId) const;
/**
* Fills the VisemeEventArray with VisemeEvent objects containing visemes and corresponding timestamps
* @param VisemeJson - FString containing Polly json viseme data ( example: {"time":125,"type":"viseme","value":"k"} )
*/
void GenerateVisemeEvents(FString VisemeJson);
/**
* Returns a USoundWave object containing the Polly Audio for playback in Blueprints
* @return USoundWaveProcedural - Sound wave object containing Polly Audio
*/
USoundWaveProcedural* QueuePollyAudio();
/**
* Timer handle for use in creating/clearing timers
*/
FTimerHandle CountdownTimerHandle;
/*
* Clock representing the start time of the StartSpeech call
*/
std::chrono::steady_clock::time_point StartTimePoint;
/*
* Sets a timer with a delay for next call to PlayNextViseme
* @param CurrentVisemeDurationSeconds - the delay
*/
virtual void SetTimer(float CurrentVisemeDurationSeconds);
/*
* Clears the timer
*/
virtual void ClearTimer();
/*
* Initializes the UnrealPollyClient
*/
virtual void InitializePollyClient();
/*
* Mutex for thread-safe mutation of internal state.
*/
FCriticalSection Mutex;
// FGenerateSpeechAction is a friend class so that it can invoke the
// protected GenerateSpeechSync function in a separate thread, which
// is required to make GenerateSpeech a non-blocking latent function.
friend class FGenerateSpeechAction;
};