-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPlaythrough.h
202 lines (142 loc) · 6.5 KB
/
Playthrough.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
// Copyright 2022 Charisma Entertainment Ltd
#pragma once
#include "CharismaEvents.h"
#include "Client.h"
#include "CoreMinimal.h"
#include "Engine/EngineTypes.h"
#include "Interfaces/IHttpResponse.h"
#include "Math/UnrealMathUtility.h"
#include "MicrophoneCapture.h"
#include "Room.h"
#include "UObject/NoExportTypes.h"
#include "Playthrough.generated.h"
UENUM(BlueprintType, Category = "Charisma|Playthrough")
enum class ECharismaSpeechAudioFormat : uint8
{
None UMETA(DisplayName = "No speech"),
Mp3 UMETA(DisplayName = "mp3"),
Wav UMETA(DisplayName = "wav"),
Pcm UMETA(DisplayName = "pcm"),
Ogg UMETA(DisplayName = "ogg")
};
UENUM(BlueprintType, Category = "Charisma|Playthrough")
enum class ECharismaSpeechRecognitionService : uint8
{
Unified UMETA(DisplayName = "unified"),
Google UMETA(DisplayName = "unified:google"),
AWS UMETA(DisplayName = "unified:aws"),
Deepgram UMETA(DisplayName = "unified:deepgram")
};
UENUM(BlueprintType, Category = "Charisma|Playthrough")
enum class ECharismaPlaythroughConnectionState : uint8
{
Disconnected,
Connecting,
Reconnecting,
Connected,
};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMessageHistoryDelegate, const FCharismaMessageHistoryResponse&, MessageHistory);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FPlaythroughInfoDelegate, const FCharismaPlaythroughInfoResponse&, PlaythroughInfo);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FChangeConnectionStateDelegate, ECharismaPlaythroughConnectionState, PreviousConnectionState,
ECharismaPlaythroughConnectionState, ConnectionState);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FTypingDelegate, bool, IsTyping);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMessageDelegate, const FCharismaMessageEvent&, MessageEvent);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FErrorDelegate, const FCharismaErrorEvent&, ErrorEvent);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FPingSuccessDelegate);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FPingFailureDelegate);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FSpeechRecognitionResultDelegate, const FString&, Transcript, bool, IsFinal);
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class CHARISMAMODULE_API UPlaythrough : public UObject
{
GENERATED_BODY()
public:
UPlaythrough();
~UPlaythrough();
// Member
UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject"), Category = "Charisma|Playthrough")
static UPlaythrough* NewPlaythroughObject(UObject* WorldContextObject, const FString& Token, const FString& PlaythroughUuid);
static const nlohmann::json SdkInfo;
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough")
void Connect();
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough")
void Disconnect();
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough Events")
void Action(const FString& ConversationUuid, const FString& ActionName) const;
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough Events")
void Start(const FString& ConversationUuid, const int32 SceneIndex, const int32 StartGraphId,
const FString& StartGraphReferenceId,
const TArray<ECharismaSpeechAudioFormat> SpeechAudioFormat);
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough Events")
void Reply(const FString& ConversationUuid, const FString& Message) const;
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough Events")
void Resume(const FString& ConversationUuid) const;
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough Events")
void Tap(const FString& ConversationUuid) const;
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough Events")
void ToggleSpeech(const TArray<ECharismaSpeechAudioFormat> AudioFormat);
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough Events")
void Play() const;
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough Events")
void Pause() const;
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough Events")
void StartSpeechRecognition(bool& bWasSuccessful,
const ECharismaSpeechRecognitionService service = ECharismaSpeechRecognitionService::AWS,
const FString languageCode = "en-US",
const int32 sampleRate = 16000);
UFUNCTION(BlueprintCallable, Category = "Charisma|Playthrough Events")
void StopSpeechRecognition();
// Events
UPROPERTY(BlueprintAssignable, Category = "Charisma|Playthrough Events")
FChangeConnectionStateDelegate OnChangeConnectionState;
UPROPERTY(BlueprintAssignable, Category = "Charisma|Playthrough Events")
FMessageDelegate OnMessage;
UPROPERTY(BlueprintAssignable, Category = "Charisma|Playthrough Events")
FErrorDelegate OnError;
UPROPERTY(BlueprintAssignable, Category = "Charisma|Playthrough Events")
FTypingDelegate OnTyping;
UPROPERTY(BlueprintAssignable, Category = "Charisma|Playthrough Events")
FPingSuccessDelegate OnPingSuccess;
UPROPERTY(BlueprintAssignable, Category = "Charisma|Playthrough Events")
FPingFailureDelegate OnPingFailure;
UPROPERTY(BlueprintAssignable, Category = "Charisma|Playthrough Events")
FSpeechRecognitionResultDelegate OnSpeechRecognitionResult;
UPROPERTY(BlueprintReadWrite, Category = "Charisma|Playthrough")
TArray<FCharismaEmotion> PlaythroughEmotions;
UPROPERTY(BlueprintReadWrite, Category = "Charisma|Playthrough")
TArray<FCharismaMemory> PlaythroughMemories;
UPROPERTY(BlueprintReadWrite, Category = "Charisma|Playthrough")
float TimeBetweenPings = 2.0f;
UPROPERTY(BlueprintReadWrite, Category = "Charisma|Playthrough")
uint8 MinimumPingsToConsiderFailed = 3;
private:
UFUNCTION()
void ReconnectionFlow();
UFUNCTION()
void ReconnectionFlowCreate();
UFUNCTION()
void ReconnectionDelay();
UFUNCTION()
void FirePing();
UFUNCTION()
void ChangeConnectionState(ECharismaPlaythroughConnectionState NewConnectionState);
UFUNCTION()
FString GetSpeechRecognitionServiceString(const ECharismaSpeechRecognitionService Service);
// Member
SpeechConfig GetSpeechConfig(const TArray<ECharismaSpeechAudioFormat> AudioFormat) const;
void OnRoomJoined(TSharedPtr<Room<void>> Room);
// Properties
TSharedPtr<Client> ClientInstance;
TSharedPtr<Room<void>> RoomInstance;
FString CurToken;
FString CurPlaythroughUuid;
UObject* CurWorldContextObject;
uint8 PingCount = 0;
FTimerHandle PingTimerHandle;
uint8 ReconnectionTryCount = 0;
float ReconnectionTryDelay = 5.0f;
float ReconnectionTryJitter = 3.0f;
bool bCalledByDisconnect = false;
ECharismaPlaythroughConnectionState ConnectionState = ECharismaPlaythroughConnectionState::Disconnected;
TArray<ECharismaSpeechAudioFormat> SpeechAudioFormat;
TSharedPtr<UMicrophoneCapture> MicrophoneCaptureInstance;
};