forked from krispai/webrtc-android-krisp-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrisp_processor.cc
255 lines (217 loc) · 6.46 KB
/
krisp_processor.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "krisp_processor.hpp"
#include <syslog.h>
#include "rtc_base/time_utils.h"
#include "inc/krisp-audio-sdk.hpp"
#include "inc/krisp-audio-sdk-nc.hpp"
#include "krisp_sdk.h"
namespace Krisp
{
KrispProcessor* KrispProcessor::_singleton = nullptr;
inline std::wstring convertMBString2WString(const std::string& str)
{
std::wstring w(str.begin(), str.end());
return w;
}
KrispProcessor::KrispProcessor() :
m_isEnabled(false),
m_session(nullptr),
m_sampleRate(KRISP_AUDIO_SAMPLING_RATE_16000HZ),
m_numberOfChannels(1),
m_lastTimeStamp(0),
m_bufferIn(),
m_bufferOut()
{
}
KrispProcessor::~KrispProcessor()
{
syslog(LOG_INFO,"KrispProcessor::~KrispProcessor()");
DeInit();
}
KrispProcessor* KrispProcessor::GetInstance()
{
if(_singleton == nullptr)
{
_singleton = new KrispProcessor();
}
return _singleton;
}
void KrispProcessor::DeInit() {
if (m_session)
{
KrispSDK::NcCloseSession(m_session);
m_session = nullptr;
}
KrispSDK::RemoveModel("default");
KrispSDK::GlobalDestroy();
KrispSDK::UnloadDll();
}
bool KrispProcessor::Init(const char* modelPath, const char* krispDllPath)
{
if (!KrispSDK::LoadDll(krispDllPath))
{
syslog(LOG_ERR, "KrispProcessor::Init: Unable to load Krisp DLL");
return false;
}
if (!KrispSDK::GlobalInit(nullptr))
{
syslog(LOG_ERR, "KrispProcessor::Init: Failed to initialize Krisp globals");
return false;
}
if (KrispSDK::SetModel(convertMBString2WString(modelPath).c_str(), "default") != 0)
{
syslog(LOG_ERR, "KrispProcessor::Init: Failed to set model file %s", modelPath);
return false;
}
return true;
}
bool KrispProcessor::Init(const void* modelAddr, unsigned int modelSize, const char* krispDllPath)
{
if (!KrispSDK::LoadDll(krispDllPath))
{
syslog(LOG_ERR, "KrispProcessor::Init: Unable to find Krisp DLL");
return false;
}
if (!KrispSDK::GlobalInit(nullptr))
{
syslog(LOG_ERR, "KrispProcessor::Init Failed to initialize Krisp globals");
return false;
}
if (KrispSDK::SetModelBlob(modelAddr, modelSize, "default") != 0)
{
syslog(LOG_ERR, "KrispProcessor::Init: Krisp failed to set model via blob api");
return false;
}
return true;
}
void KrispProcessor::Enable(bool isEnable)
{
m_isEnabled = isEnable;
}
bool KrispProcessor::IsEnabled() const
{
return m_isEnabled;
}
void KrispProcessor::Initialize(int sampleRate, int numberOfChannels)
{
syslog(LOG_INFO, "KrispProcessor::Initialize: sampleRate: %i\
numberOfChannels: %i", sampleRate, numberOfChannels);
m_numberOfChannels = numberOfChannels;
if (m_sampleRate != sampleRate || m_session == nullptr)
{
if (m_session)
{
KrispSDK::NcCloseSession(m_session);
}
m_session = CreateAudioSession(sampleRate);
m_sampleRate = sampleRate;
if (m_session == nullptr)
{
// TODO: throw a valid WebRTC exception for error handling
syslog(LOG_ERR, "KrispProcessor::Initialize: Failed creating Krisp AudioSession");
return;
}
}
}
void KrispProcessor::Process(webrtc::AudioBuffer* audioBuffer)
{
if(!KrispProcessor::IsEnabled())
{
syslog(LOG_DEBUG, "KrispProcessor::Process: Bypassing NoiseSuppressor::Process");
return;
}
auto now = rtc::TimeMillis();
if (now - m_lastTimeStamp > 10000)
{
syslog(LOG_INFO,"KrispProcessor::Process: Num Frames: %zu\
num Bands: %zu Num Channels: %zu ", audioBuffer->num_frames(),
audioBuffer->num_bands(), audioBuffer->num_channels());
m_lastTimeStamp = now;
}
int audioBufferSampleRate = audioBuffer->num_frames() * 100;
if(audioBufferSampleRate != m_sampleRate)
{
if (m_session)
{
KrispSDK::NcCloseSession(m_session);
}
m_session = CreateAudioSession(audioBufferSampleRate);
m_sampleRate = audioBufferSampleRate;
if (m_session == nullptr)
{
syslog(LOG_ERR, "KrispProcessor::Process: Failed creating AudioSession");
return;
}
}
constexpr size_t kNsFrameSize = 160;
size_t bufferSize = kNsFrameSize * audioBuffer->num_bands();
m_bufferIn.resize(bufferSize);
m_bufferOut.resize(bufferSize);
for (size_t i = 0; i < bufferSize; ++i)
{
m_bufferIn[i] = audioBuffer->channels()[0][i] / 32768.f;
}
auto returnCode = KrispSDK::NcCleanAmbientNoiseFloat(
m_session, m_bufferIn.data(), bufferSize,
m_bufferOut.data(), bufferSize);
if (returnCode != 0)
{
syslog(LOG_INFO, "KrispProcessor::Process: Krisp noise cleanup error");
return;
}
for (size_t i = 0; i < bufferSize; ++i)
{
audioBuffer->channels()[0][i] = m_bufferOut[i] * 32768.f;
}
}
std::string KrispProcessor::ToString() const
{
return "KrispAudioProcessor";
}
void KrispProcessor::SetRuntimeSetting(webrtc::AudioProcessing::RuntimeSetting setting)
{
}
static KrispAudioSamplingRate GetSampleRate(size_t sampleRate)
{
switch (sampleRate)
{
case 8000:
return KRISP_AUDIO_SAMPLING_RATE_8000HZ;
case 16000:
return KRISP_AUDIO_SAMPLING_RATE_16000HZ;
case 24000:
return KRISP_AUDIO_SAMPLING_RATE_24000HZ;
case 32000:
return KRISP_AUDIO_SAMPLING_RATE_32000HZ;
case 44100:
return KRISP_AUDIO_SAMPLING_RATE_44100HZ;
case 48000:
return KRISP_AUDIO_SAMPLING_RATE_48000HZ;
case 88200:
return KRISP_AUDIO_SAMPLING_RATE_88200HZ;
case 96000:
return KRISP_AUDIO_SAMPLING_RATE_96000HZ;
default:
syslog(LOG_INFO, "KrispProcessor::GetSampleRate: The input sampling rate: %zu \
is not supported. Using default 48khz.", sampleRate);
return KRISP_AUDIO_SAMPLING_RATE_48000HZ;
}
}
static KrispAudioFrameDuration GetFrameDuration(size_t duration)
{
switch (duration)
{
case 10:
return KRISP_AUDIO_FRAME_DURATION_10MS;
default:
syslog(LOG_INFO, "KrispProcessor::GetFrameDuration: Frame duration: %zu \
is not supported. Switching to default 10ms", duration);
return KRISP_AUDIO_FRAME_DURATION_10MS;
}
}
void * KrispProcessor::CreateAudioSession(int sampleRate)
{
auto krispSampleRate = GetSampleRate(sampleRate);
auto krispFrameDuration = GetFrameDuration(KRISP_AUDIO_FRAME_DURATION_10MS);
return KrispSDK::NcCreateSession(krispSampleRate, krispSampleRate, krispFrameDuration, "default");
}
}