forked from krispai/webrtc-android-krisp-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrisp_sdk.cc
198 lines (171 loc) · 4.72 KB
/
krisp_sdk.cc
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
#include "krisp_sdk.h"
#include <stdarg.h>
#include <dlfcn.h>
#include <syslog.h>
#include <array>
namespace KrispSDK {
enum class KrispFunctionId
{
krispAudioGlobalInit = 0,
krispAudioGlobalDestroy = 1,
krispAudioSetModel = 2,
krispAudioSetModelBlob = 3,
krispAudioRemoveModel = 4,
krispAudioNcCreateSession = 5,
krispAudioNcCloseSession = 6,
krispAudioNcCleanAmbientNoiseFloat = 7
};
class KrispAudioSdkDllGate
{
public:
static KrispAudioSdkDllGate * singleton()
{
if (!_singleton)
{
_singleton = new KrispAudioSdkDllGate;
}
return _singleton;
}
bool LoadDll(const char* krispDllPath)
{
dlerror();
_dllHandle = dlopen(krispDllPath, RTLD_LAZY);
if (!_dllHandle) {
syslog(LOG_ERR, "KrispSDK::LoadDll: Failed to load the library = %s\n", krispDllPath);
return false;
}
syslog(LOG_INFO, "Krisp DLL loaded: %s", krispDllPath);
return LoadFunctions();
}
void UnloadDll()
{
if (_dllHandle)
{
dlclose(_dllHandle);
_dllHandle = nullptr;
}
for (auto & functionPtr : _functionPointers)
{
functionPtr = nullptr;
}
}
template<typename ReturnType, typename... Args>
ReturnType InvokeFunction(KrispFunctionId functionId, Args... args)
{
dlerror();
if (!_dllHandle) {
syslog(LOG_INFO, "InvokeFunction invalid DLL handle: %p", _dllHandle);
return ReturnType();
}
using FuncType = ReturnType (*)(Args...);
void * functionPtr = _functionPointers[static_cast<size_t>(functionId)];
FuncType function = reinterpret_cast<FuncType>(functionPtr);
return function(args...);
}
private:
bool LoadFunctions()
{
dlerror();
for (size_t functionId = 0; functionId < _functionCount; ++functionId)
{
const char * functionName = _functionNames[functionId];
syslog(LOG_INFO,"KrispAudioSdkDllGate::LoadFunctions dlsym the %s", functionName);
void * functionPtr = dlsym(_dllHandle, functionName);
const char* dlsym_error = dlerror();
if (dlsym_error) {
syslog(LOG_ERR, "KrispAudioSdkDllGate::LoadFunctions Error finding symbol: %s", dlsym_error);
return false;
}
_functionPointers[functionId] = functionPtr;
}
return true;
}
static KrispAudioSdkDllGate * _singleton;
void* _dllHandle = nullptr;
static constexpr unsigned int _functionCount = 8;
static constexpr std::array<const char *, _functionCount> _functionNames =
{
"krispAudioGlobalInit",
"krispAudioGlobalDestroy",
"krispAudioSetModel",
"krispAudioSetModelBlob",
"krispAudioRemoveModel",
"krispAudioNcCreateSession",
"krispAudioNcCloseSession",
"krispAudioNcCleanAmbientNoiseFloat"
};
std::array<void *, _functionCount> _functionPointers = {};
};
KrispAudioSdkDllGate * KrispAudioSdkDllGate::_singleton = nullptr;
template<typename ReturnType, typename... Args>
ReturnType InvokeFunction(KrispFunctionId functionId, Args... args)
{
return KrispAudioSdkDllGate::singleton()->InvokeFunction<ReturnType, Args...>(functionId, args...);
}
bool LoadDll(const char* krispDllPath)
{
return KrispAudioSdkDllGate::singleton()->LoadDll(krispDllPath);
}
void UnloadDll()
{
KrispAudioSdkDllGate::singleton()->UnloadDll();
}
bool GlobalInit(void* param)
{
int result = InvokeFunction<int>(KrispFunctionId::krispAudioGlobalInit, param);
return result == 0 ? true: false;
}
int SetModel(const wchar_t* weightFilePath, const char* modelName)
{
int result = InvokeFunction<int>(KrispFunctionId::krispAudioSetModel, weightFilePath, modelName);
return result;
}
int SetModelBlob(const void* modelAddress, unsigned int modelSize, const char* modelName)
{
int result = InvokeFunction<int>(KrispFunctionId::krispAudioSetModelBlob, modelAddress, modelSize, modelName);
return result;
}
int RemoveModel(const char* modelName)
{
return InvokeFunction<int>(KrispFunctionId::krispAudioRemoveModel, modelName);
}
int GlobalDestroy()
{
int result = InvokeFunction<int>(KrispFunctionId::krispAudioGlobalDestroy);
return result;
}
int NcCloseSession(void* session)
{
int result = InvokeFunction<int>(KrispFunctionId::krispAudioNcCloseSession, session);
return result;
}
KrispAudioSessionID NcCreateSession(
KrispAudioSamplingRate inputSampleRate,
KrispAudioSamplingRate outputSampleRate,
KrispAudioFrameDuration frameDuration,
const char* modelName)
{
KrispAudioSessionID result = InvokeFunction<KrispAudioSessionID>(KrispFunctionId::krispAudioNcCreateSession,
inputSampleRate,
outputSampleRate,
frameDuration,
modelName);
return result;
}
int NcCleanAmbientNoiseFloat(
KrispAudioSessionID pSession,
const float* pFrameIn,
unsigned int frameInSize,
float* pFrameOut,
unsigned int frameOutSize)
{
int result = InvokeFunction<int>(KrispFunctionId::krispAudioNcCleanAmbientNoiseFloat,
pSession,
pFrameIn,
frameInSize,
pFrameOut,
frameOutSize
);
return result;
}
}