|
1 | 1 | // Copyright Epic Games, Inc. All Rights Reserved. |
2 | 2 |
|
3 | 3 | #include "UESpeechRecognitionModule.h" |
4 | | -#include "simpleson/json.h" |
5 | 4 | #include "Modules/ModuleManager.h" |
6 | 5 |
|
7 | | -void UESpeechRecognitionModule::StartupVosk() |
| 6 | +void UESpeechRecognitionModule::StartupUESpeechRecognitionModule() |
8 | 7 | { |
9 | | - const FString LibVoskPath = FPaths::Combine(*BasePluginDir, TEXT("Binaries/Win64/libvosk.dll")); |
10 | | - DynamicLibVoskHandle = FPlatformProcess::GetDllHandle(*LibVoskPath); |
| 8 | + const FString LibSpeechRecognitionModule = FPaths::Combine("E:/SmartCompanion/", TEXT("Binaries/Win64/SpeechRecognitionModule.dll")); |
| 9 | + DynamicLibSpeechRecognitionModuleHandle = FPlatformProcess::GetDllHandle(*LibSpeechRecognitionModule); |
11 | 10 |
|
12 | | - if (DynamicLibVoskHandle) |
| 11 | + if (DynamicLibSpeechRecognitionModuleHandle) |
13 | 12 | { |
14 | | - UE_LOG(LogTemp, Log, TEXT("libvosk.dll loaded successfully!")); |
| 13 | + UE_LOG(LogTemp, Log, TEXT("SpeechRecognitionModule.dll loaded successfully!")); |
15 | 14 | } |
16 | 15 | else |
17 | 16 | { |
18 | | - UE_LOG(LogTemp, Fatal, TEXT("libvosk.dll failed to load!")); |
| 17 | + UE_LOG(LogTemp, Fatal, TEXT("SpeechRecognitionModule.dll failed to load!")); |
19 | 18 | } |
20 | 19 | } |
21 | 20 |
|
22 | | -void UESpeechRecognitionModule::StatupPortAudio() |
| 21 | +void UESpeechRecognitionModule::ShutdownUESpeechRecognitionModule() |
23 | 22 | { |
24 | | - const FString LibPortAudioPath = FPaths::Combine(*BasePluginDir, TEXT("Binaries/Win64/portaudio_x64.dll")); |
25 | | - DynamicLibPortAudioHandle = FPlatformProcess::GetDllHandle(*LibPortAudioPath); |
26 | | - |
27 | | - if (DynamicLibPortAudioHandle) |
28 | | - { |
29 | | - UE_LOG(LogTemp, Log, TEXT("portaudio_x64.dll loaded successfully!")); |
30 | | - } |
31 | | - else |
32 | | - { |
33 | | - UE_LOG(LogTemp, Fatal, TEXT("portaudio_x64.dll failed to load!")); |
34 | | - } |
35 | | -} |
36 | | - |
37 | | -void UESpeechRecognitionModule::ShutdownVosk() |
38 | | -{ |
39 | | - if (DynamicLibVoskHandle) FPlatformProcess::FreeDllHandle(DynamicLibVoskHandle); |
40 | | - DynamicLibVoskHandle = nullptr; |
41 | | -} |
42 | | - |
43 | | -void UESpeechRecognitionModule::ShutdownPortAudio() |
44 | | -{ |
45 | | - if (DynamicLibPortAudioHandle) FPlatformProcess::FreeDllHandle(DynamicLibPortAudioHandle); |
46 | | - DynamicLibPortAudioHandle = nullptr; |
| 23 | + FPlatformProcess::FreeDllHandle(DynamicLibSpeechRecognitionModuleHandle); |
| 24 | + DynamicLibSpeechRecognitionModuleHandle = nullptr; |
47 | 25 | } |
48 | 26 |
|
49 | 27 | void UESpeechRecognitionModule::StartupModule() |
50 | 28 | { |
51 | | - //StartupVosk(); |
52 | | - //StatupPortAudio(); |
53 | | - |
54 | | - if (!InializeModelAndRecognizer()) return; |
55 | | - if (!InitializePortAudio()) return; |
56 | | - if (!SetAudioDevice()) return; |
57 | | - if (!OpenStream()) return; |
58 | | - if (!StartStream()) return; |
59 | | -} |
60 | | - |
61 | | -void UESpeechRecognitionModule::ShutdownModule() |
62 | | -{ |
63 | | - Pa_CloseStream(stream); |
64 | | - vosk_recognizer_free(recognizer); |
65 | | - vosk_model_free(model); |
66 | | - |
67 | | - //if (DynamicLibVoskHandle) ShutdownVosk(); |
68 | | - //if (DynamicLibPortAudioHandle) ShutdownPortAudio(); |
69 | | -} |
70 | | - |
71 | | -bool UESpeechRecognitionModule::InializeModelAndRecognizer() |
72 | | -{ |
73 | | - std::string path = baseDir + "\\Models\\Vosk\\vosk-model-small-en-us-0.15"; |
74 | | - |
75 | | - model = vosk_model_new(path.c_str()); |
76 | | - if (!model) |
77 | | - { |
78 | | - UE_LOG(LogTemp, Display, TEXT("vosk_model_new: error")); |
79 | | - return false; |
80 | | - } |
81 | | - |
82 | | - recognizer = vosk_recognizer_new(model, 16000.0); |
83 | | - if (!recognizer) |
84 | | - { |
85 | | - UE_LOG(LogTemp, Display, TEXT("vosk_recognizer_new: error")); |
86 | | - return false; |
87 | | - } |
88 | | - |
89 | | - return true; |
90 | | -} |
91 | | - |
92 | | -bool UESpeechRecognitionModule::InitializePortAudio() |
93 | | -{ |
94 | | - PaError err = Pa_Initialize(); |
95 | | - if (err != paNoError) |
96 | | - { |
97 | | - UE_LOG(LogTemp, Display, TEXT("Pa_Initialize: "), Pa_GetErrorText(err)); |
98 | | - return false; |
99 | | - } |
100 | | - |
101 | | - return true; |
102 | | -} |
103 | | - |
104 | | -bool UESpeechRecognitionModule::SetAudioDevice() |
105 | | -{ |
106 | | - inputParametrs.channelCount = 1; |
107 | | - inputParametrs.sampleFormat = paInt16; |
108 | | - inputParametrs.hostApiSpecificStreamInfo = nullptr; |
109 | | - inputParametrs.device = Pa_GetDefaultInputDevice(); |
| 29 | + StartupUESpeechRecognitionModule(); |
110 | 30 |
|
111 | | - if (inputParametrs.device == paNoDevice) |
112 | | - { |
113 | | - UE_LOG(LogTemp, Display, TEXT("Pa_GetDefaultInputDevice: no device")); |
114 | | - return false; |
115 | | - } |
116 | | - |
117 | | - return true; |
118 | | -} |
119 | | - |
120 | | -bool UESpeechRecognitionModule::OpenStream() |
121 | | -{ |
122 | | - PaError err = Pa_OpenStream(&stream, &inputParametrs, nullptr, 16000.0, 8192, 0, nullptr, nullptr); |
123 | | - if (err != paNoError) |
124 | | - { |
125 | | - UE_LOG(LogTemp, Display, TEXT("Pa_OpenStream: "), Pa_GetErrorText(err)); |
126 | | - return false; |
127 | | - } |
128 | | - |
129 | | - return true; |
130 | | -} |
131 | | - |
132 | | -bool UESpeechRecognitionModule::StartStream() |
133 | | -{ |
134 | | - PaError err = Pa_StartStream(stream); |
135 | | - if (err != paNoError) |
136 | | - { |
137 | | - UE_LOG(LogTemp, Display, TEXT("Pa_StartStream: "), Pa_GetErrorText(err)); |
138 | | - return false; |
139 | | - } |
| 31 | + Initialize = (const char*(*)())(FPlatformProcess::GetDllExport(DynamicLibSpeechRecognitionModuleHandle, TEXT("Initialize"))); |
| 32 | + Run = (const char* (*)())(FPlatformProcess::GetDllExport(DynamicLibSpeechRecognitionModuleHandle, TEXT("Run"))); |
| 33 | + Shutdown = (void(*)())(FPlatformProcess::GetDllExport(DynamicLibSpeechRecognitionModuleHandle, TEXT("Shutdown"))); |
140 | 34 |
|
141 | | - return true; |
| 35 | + FString resInitialize(Initialize()); |
| 36 | + UE_LOG(LogTemp, Display, TEXT("%s"), FString(resInitialize)); |
142 | 37 | } |
143 | 38 |
|
144 | | -std::string UESpeechRecognitionModule::Run() |
145 | | -{ |
146 | | - bool isCorrectRead = ReadDataFromStream(); |
147 | | - if (!isCorrectRead) return {}; |
148 | | - |
149 | | - std::string recognizedText = Recognize(); |
150 | | - return recognizedText; |
151 | | -} |
152 | | - |
153 | | -bool UESpeechRecognitionModule::ReadDataFromStream() |
154 | | -{ |
155 | | - PaError err = Pa_ReadStream(stream, (void*)data, SPEECH_BUFFER_SIZE / 2); |
156 | | - if (err != paNoError && err != paInputOverflowed) |
157 | | - { |
158 | | - UE_LOG(LogTemp, Display, TEXT("Pa_ReadStream: "), Pa_GetErrorText(err)); |
159 | | - return false; |
160 | | - } |
161 | | - |
162 | | - return true; |
163 | | -} |
164 | | - |
165 | | -std::string UESpeechRecognitionModule::Recognize() |
| 39 | +void UESpeechRecognitionModule::ShutdownModule() |
166 | 40 | { |
167 | | - if (vosk_recognizer_accept_waveform(recognizer, data, sizeof(data)) == -1) |
168 | | - { |
169 | | - UE_LOG(LogTemp, Display, TEXT("vosk_recognizer_accept_waveform: error")); |
170 | | - return {}; |
171 | | - } |
172 | | - |
173 | | - auto resRegonition(vosk_recognizer_result(recognizer)); |
174 | | - auto resJSON = json::jobject::parse(resRegonition); |
175 | | - |
176 | | - FString textFString(resJSON.get("text").c_str()); |
177 | | - UE_LOG(LogTemp, Display, TEXT("TEXT: %s"), *textFString); |
178 | | - |
179 | | - return resJSON.get("text"); |
| 41 | + Shutdown(); |
| 42 | + ShutdownUESpeechRecognitionModule(); |
180 | 43 | } |
181 | 44 |
|
182 | 45 | IMPLEMENT_MODULE(UESpeechRecognitionModule, UESpeechRecognition); |
|
0 commit comments