1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef LOG_TAG
16 #define LOG_TAG "NapiAsrProcessingController"
17 #endif
18
19 #include "napi_asr_processing_controller.h"
20
21 #include <map>
22 #include <string>
23 #include "napi_audio_enum.h"
24 #include "napi_audio_error.h"
25 #include "napi_param_utils.h"
26 #include "audio_errors.h"
27 #include "audio_log.h"
28 #include "audio_utils.h"
29 #include "napi_audio_capturer.h"
30
31 namespace OHOS {
32 namespace AudioStandard {
33 using namespace std;
34 using namespace HiviewDFX;
35 static __thread napi_ref g_asrConstructor = nullptr;
36
37 static const std::map<int32_t, int32_t> ERR_MAP = {
38 {ERR_SYSTEM_PERMISSION_DENIED, NAPI_ERR_PERMISSION_DENIED},
39 {ERR_INVALID_PARAM, NAPI_ERR_INVALID_PARAM},
40 {ERROR, NAPI_ERR_UNSUPPORTED},
41 {-1, NAPI_ERR_UNSUPPORTED},
42 };
43
44 static const std::map<int32_t, std::string> ERR_INFO_MAP = {
45 {ERR_SYSTEM_PERMISSION_DENIED, "Caller is not a system application."},
46 {ERR_INVALID_PARAM, "Parameter verification failed. : The param of mode must be mode enum"},
47 {ERROR, "Operation not allowed."},
48 {-1, "Operation not allowed."},
49 };
50
GetResInt(int32_t errNum)51 static int32_t GetResInt(int32_t errNum)
52 {
53 auto it = ERR_MAP.find(errNum);
54 int32_t errInt = NAPI_ERR_UNSUPPORTED;
55 if (it != ERR_MAP.end()) {
56 errInt = it->second;
57 } else {
58 AUDIO_ERR_LOG("err not found.");
59 }
60 return errInt;
61 }
62
GetResStr(int32_t errNum)63 static std::string GetResStr(int32_t errNum)
64 {
65 auto it = ERR_INFO_MAP.find(errNum);
66 std::string errStr = "Operation not allowed.";
67 if (it != ERR_INFO_MAP.end()) {
68 errStr = it->second;
69 } else {
70 AUDIO_ERR_LOG("err not found.");
71 }
72 return errStr;
73 }
74
CheckCapturerValid(napi_env env,napi_value capturer)75 static bool CheckCapturerValid(napi_env env, napi_value capturer)
76 {
77 NapiAudioCapturer *napiCapturer = nullptr;
78
79 napi_status status = napi_unwrap(env, capturer, reinterpret_cast<void**>(&napiCapturer));
80 if (status != napi_ok) {
81 AUDIO_ERR_LOG("napi unwrap failed");
82 return false;
83 }
84
85 if (napiCapturer == nullptr) {
86 AUDIO_ERR_LOG("napi capturer is nullptr");
87 return false;
88 }
89
90 AudioCapturerInfo capturerInfo;
91 napiCapturer->audioCapturer_->GetCapturerInfo(capturerInfo);
92 if ((capturerInfo.sourceType != SourceType::SOURCE_TYPE_VOICE_RECOGNITION) &&
93 (capturerInfo.sourceType != SourceType::SOURCE_TYPE_WAKEUP) &&
94 (capturerInfo.sourceType != SourceType::SOURCE_TYPE_VOICE_CALL)) {
95 AUDIO_ERR_LOG("sourceType not valid. type : %{public}d", capturerInfo.sourceType);
96 return false;
97 }
98 return true;
99 }
100
NapiAsrProcessingController()101 NapiAsrProcessingController::NapiAsrProcessingController()
102 : audioMngr_(nullptr), env_(nullptr) {}
103
~NapiAsrProcessingController()104 NapiAsrProcessingController::~NapiAsrProcessingController()
105 {
106 }
107
GetParamWithSync(const napi_env & env,napi_callback_info info,size_t & argc,napi_value * args)108 NapiAsrProcessingController* NapiAsrProcessingController::GetParamWithSync(const napi_env& env,
109 napi_callback_info info, size_t& argc, napi_value* args)
110 {
111 napi_status status;
112 NapiAsrProcessingController* napiAsrProcessingController = nullptr;
113 napi_value jsThis = nullptr;
114
115 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
116 CHECK_AND_RETURN_RET_LOG(status == napi_ok && jsThis != nullptr, nullptr,
117 "GetParamWithSync fail to napi_get_cb_info");
118
119 status = napi_unwrap(env, jsThis, (void**)&napiAsrProcessingController);
120 CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "napi_unwrap failed");
121 CHECK_AND_RETURN_RET_LOG(napiAsrProcessingController != nullptr &&
122 napiAsrProcessingController->audioMngr_ != nullptr, napiAsrProcessingController,
123 "GetParamWithSync fail to napi_unwrap");
124 return napiAsrProcessingController;
125 }
126
InitNapiAsrProcessingController(napi_env env,napi_value & constructor)127 napi_status NapiAsrProcessingController::InitNapiAsrProcessingController(napi_env env, napi_value& constructor)
128 {
129 napi_property_descriptor audio_svc_mngr_properties[] = {
130 DECLARE_NAPI_FUNCTION("setAsrAecMode", SetAsrAecMode),
131 DECLARE_NAPI_FUNCTION("getAsrAecMode", GetAsrAecMode),
132 DECLARE_NAPI_FUNCTION("setAsrNoiseSuppressionMode", SetAsrNoiseSuppressionMode),
133 DECLARE_NAPI_FUNCTION("getAsrNoiseSuppressionMode", GetAsrNoiseSuppressionMode),
134 DECLARE_NAPI_FUNCTION("setAsrWhisperDetectionMode", SetAsrWhisperDetectionMode),
135 DECLARE_NAPI_FUNCTION("getAsrWhisperDetectionMode", GetAsrWhisperDetectionMode),
136 DECLARE_NAPI_FUNCTION("setAsrVoiceControlMode", SetAsrVoiceControlMode),
137 DECLARE_NAPI_FUNCTION("setAsrVoiceMuteMode", SetAsrVoiceMuteMode),
138 DECLARE_NAPI_FUNCTION("isWhispering", IsWhispering),
139 };
140
141 napi_status status = napi_define_class(env, NAPI_ASR_PROCESSING_CONTROLLER_CLASS_NAME.c_str(),
142 NAPI_AUTO_LENGTH, Construct, nullptr,
143 sizeof(audio_svc_mngr_properties) / sizeof(audio_svc_mngr_properties[PARAM0]),
144 audio_svc_mngr_properties, &constructor);
145 return status;
146 }
147
Init(napi_env env,napi_value exports)148 napi_value NapiAsrProcessingController::Init(napi_env env, napi_value exports)
149 {
150 AUDIO_DEBUG_LOG("Init");
151 napi_status status;
152 napi_value constructor;
153 napi_value result = nullptr;
154 const int32_t refCount = 1;
155
156 napi_property_descriptor static_prop[] = {
157 DECLARE_NAPI_STATIC_FUNCTION("createAsrProcessingController", CreateAsrProcessingController),
158 };
159
160 status = InitNapiAsrProcessingController(env, constructor);
161 CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "InitNapiAudioRenderer fail");
162
163 status = napi_create_reference(env, constructor, refCount, &g_asrConstructor);
164 CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "napi_create_reference fail");
165 status = napi_set_named_property(env, exports, NAPI_ASR_PROCESSING_CONTROLLER_CLASS_NAME.c_str(),
166 constructor);
167 CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "napi_set_named_property fail");
168 status = napi_define_properties(env, exports, sizeof(static_prop) / sizeof(static_prop[PARAM0]),
169 static_prop);
170 CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "napi_define_properties fail");
171 return exports;
172 }
173
Destructor(napi_env env,void * nativeObject,void * finalizeHint)174 void NapiAsrProcessingController::Destructor(napi_env env, void* nativeObject, void* finalizeHint)
175 {
176 if (nativeObject != nullptr) {
177 auto obj = static_cast<NapiAsrProcessingController*>(nativeObject);
178 ObjectRefMap<NapiAsrProcessingController>::DecreaseRef(obj);
179 AUDIO_DEBUG_LOG("delete obj done");
180 }
181 }
182
Construct(napi_env env,napi_callback_info info)183 napi_value NapiAsrProcessingController::Construct(napi_env env, napi_callback_info info)
184 {
185 napi_status status;
186 napi_value jsThis;
187 napi_value undefinedResult = nullptr;
188 NapiParamUtils::GetUndefinedValue(env);
189 size_t argCount = PARAM0;
190
191 status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
192 if (status == napi_ok) {
193 unique_ptr<NapiAsrProcessingController> managerNapi = make_unique<NapiAsrProcessingController>();
194 if (managerNapi != nullptr) {
195 ObjectRefMap<NapiAsrProcessingController>::Insert(managerNapi.get());
196 managerNapi->env_ = env;
197 managerNapi->audioMngr_ = AudioSystemManager::GetInstance();
198
199 status = napi_wrap(env, jsThis, static_cast<void*>(managerNapi.get()),
200 NapiAsrProcessingController::Destructor, nullptr, nullptr);
201 if (status != napi_ok) {
202 ObjectRefMap<NapiAsrProcessingController>::Erase(managerNapi.get());
203 return undefinedResult;
204 }
205 managerNapi.release();
206 return jsThis;
207 }
208 }
209 return undefinedResult;
210 }
211
CreateAsrProcessingControllerWrapper(napi_env env)212 napi_value NapiAsrProcessingController::CreateAsrProcessingControllerWrapper(napi_env env)
213 {
214 napi_status status;
215 napi_value result = nullptr;
216 napi_value constructor;
217
218 status = napi_get_reference_value(env, g_asrConstructor, &constructor);
219 if (status != napi_ok) {
220 AUDIO_ERR_LOG("napi get ref failed, status:%{public}d", status);
221 goto fail;
222 }
223 status = napi_new_instance(env, constructor, 0, nullptr, &result);
224 if (status != napi_ok) {
225 AUDIO_ERR_LOG("napi_new_instance failed, status:%{public}d", status);
226 goto fail;
227 }
228 return result;
229
230 fail:
231 napi_get_undefined(env, &result);
232 return result;
233 }
234
CreateAsrProcessingController(napi_env env,napi_callback_info info)235 napi_value NapiAsrProcessingController::CreateAsrProcessingController(napi_env env, napi_callback_info info)
236 {
237 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySelfPermission(),
238 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED), "No system permission");
239 size_t argc = ARGS_ONE;
240 napi_value argv[ARGS_ONE] = {};
241 napi_value thisVar = nullptr;
242 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
243 CHECK_AND_RETURN_RET_LOG(argc >= ARGS_ONE, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID,
244 "mandatory parameters are left unspecified"), "argCount invaild");
245 bool isCapturerValid = CheckCapturerValid(env, argv[PARAM0]);
246 CHECK_AND_RETURN_RET_LOG(isCapturerValid,
247 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_UNSUPPORTED), "Operation not allowed. ");
248 return NapiAsrProcessingController::CreateAsrProcessingControllerWrapper(env);
249 }
250
SetAsrAecMode(napi_env env,napi_callback_info info)251 napi_value NapiAsrProcessingController::SetAsrAecMode(napi_env env, napi_callback_info info)
252 {
253 napi_value result = nullptr;
254 size_t argc = ARGS_ONE;
255 napi_value argv[ARGS_ONE] = {};
256 auto *napiAsrController = GetParamWithSync(env, info, argc, argv);
257 CHECK_AND_RETURN_RET_LOG(argc >= ARGS_ONE, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID,
258 "mandatory parameters are left unspecified"), "argCount invaild");
259
260 int32_t asrAecMode = 0;
261 int32_t retMode = NapiParamUtils::GetValueInt32(env, asrAecMode, argv[PARAM0]);
262 CHECK_AND_RETURN_RET_LOG(retMode == 0, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
263 "parameter verification failed: The param of mode must be mode enum"), "Input parameter value error. ");
264 CHECK_AND_RETURN_RET_LOG(asrAecMode == 0 || asrAecMode == 1, NapiAudioError::ThrowErrorAndReturn(env,
265 NAPI_ERR_INVALID_PARAM, "parameter verification failed: The param of mode must be enum AsrAecMode"),
266 "Input parameter value error. ");
267 CHECK_AND_RETURN_RET_LOG(napiAsrController != nullptr, result, "napiAsrController is nullptr");
268 CHECK_AND_RETURN_RET_LOG(napiAsrController->audioMngr_ != nullptr, result, "audioMngr_ is nullptr");
269 int32_t res = napiAsrController->audioMngr_->SetAsrAecMode(static_cast<AsrAecMode>(asrAecMode));
270 CHECK_AND_RETURN_RET_LOG(res == 0, NapiAudioError::ThrowErrorAndReturn(env,
271 GetResInt(res), GetResStr(res)), "SetAsrAecMode fail");
272 bool setSuc = ((res == 0) ? true : false);
273 NapiParamUtils::SetValueBoolean(env, setSuc, result);
274 return result;
275 }
276
GetAsrAecMode(napi_env env,napi_callback_info info)277 napi_value NapiAsrProcessingController::GetAsrAecMode(napi_env env, napi_callback_info info)
278 {
279 napi_value result = nullptr;
280 size_t argc = PARAM0;
281 auto *napiAsrController = GetParamWithSync(env, info, argc, nullptr);
282 AsrAecMode asrAecMode;
283 CHECK_AND_RETURN_RET_LOG(napiAsrController != nullptr, result, "napiAsrController is nullptr");
284 CHECK_AND_RETURN_RET_LOG(napiAsrController->audioMngr_ != nullptr, result, "audioMngr_ is nullptr");
285 int32_t res = napiAsrController->audioMngr_->GetAsrAecMode(asrAecMode);
286 CHECK_AND_RETURN_RET_LOG(res == 0, NapiAudioError::ThrowErrorAndReturn(env,
287 GetResInt(res), GetResStr(res)), "GetAsrAecMode fail");
288 NapiParamUtils::SetValueInt32(env, int32_t(asrAecMode), result);
289 return result;
290 }
291
SetAsrNoiseSuppressionMode(napi_env env,napi_callback_info info)292 napi_value NapiAsrProcessingController::SetAsrNoiseSuppressionMode(napi_env env, napi_callback_info info)
293 {
294 napi_value result = nullptr;
295 size_t argc = ARGS_ONE;
296 napi_value argv[ARGS_ONE] = {};
297 auto *napiAsrController = GetParamWithSync(env, info, argc, argv);
298 CHECK_AND_RETURN_RET_LOG(argc >= ARGS_ONE, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID,
299 "mandatory parameters are left unspecified"), "argCount invaild");
300
301 int32_t asrNoiseSuppressionMode = 0;
302 int32_t asrVoiceControlModeMax = static_cast<int32_t>(AsrNoiseSuppressionMode::FAR_FIELD);
303 int32_t retMode = NapiParamUtils::GetValueInt32(env, asrNoiseSuppressionMode, argv[PARAM0]);
304 CHECK_AND_RETURN_RET_LOG(retMode == 0, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
305 "parameter verification failed: The param of mode must be mode enum"), "Input parameter value error. ");
306 CHECK_AND_RETURN_RET_LOG(asrNoiseSuppressionMode >= 0 && asrNoiseSuppressionMode <= asrVoiceControlModeMax,
307 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
308 "parameter verification failed: The param of mode must be enum AsrNoiseSuppressionMode"),
309 "Input parameter value error. ");
310 CHECK_AND_RETURN_RET_LOG(napiAsrController != nullptr, result, "napiAsrController is nullptr");
311 CHECK_AND_RETURN_RET_LOG(napiAsrController->audioMngr_ != nullptr, result, "audioMngr_ is nullptr");
312 int32_t res = napiAsrController->audioMngr_->SetAsrNoiseSuppressionMode(
313 static_cast<AsrNoiseSuppressionMode>(asrNoiseSuppressionMode));
314 CHECK_AND_RETURN_RET_LOG(res == 0, NapiAudioError::ThrowErrorAndReturn(env,
315 GetResInt(res), GetResStr(res)), "SetNSMode fail");
316 bool setSuc = ((res == 0) ? true : false);
317 NapiParamUtils::SetValueBoolean(env, setSuc, result);
318 return result;
319 }
320
GetAsrNoiseSuppressionMode(napi_env env,napi_callback_info info)321 napi_value NapiAsrProcessingController::GetAsrNoiseSuppressionMode(napi_env env, napi_callback_info info)
322 {
323 napi_value result = nullptr;
324 size_t argc = PARAM0;
325 auto *napiAsrController = GetParamWithSync(env, info, argc, nullptr);
326 AsrNoiseSuppressionMode asrNoiseSuppressionMode;
327 CHECK_AND_RETURN_RET_LOG(napiAsrController != nullptr, result, "napiAsrController is nullptr");
328 CHECK_AND_RETURN_RET_LOG(napiAsrController->audioMngr_ != nullptr, result, "audioMngr_ is nullptr");
329 int32_t res = napiAsrController->audioMngr_->GetAsrNoiseSuppressionMode(asrNoiseSuppressionMode);
330 CHECK_AND_RETURN_RET_LOG(res == 0, NapiAudioError::ThrowErrorAndReturn(env,
331 GetResInt(res), GetResStr(res)), "GetNSMode fail");
332 NapiParamUtils::SetValueInt32(env, int32_t(asrNoiseSuppressionMode), result);
333 return result;
334 }
335
SetAsrWhisperDetectionMode(napi_env env,napi_callback_info info)336 napi_value NapiAsrProcessingController::SetAsrWhisperDetectionMode(napi_env env, napi_callback_info info)
337 {
338 napi_value result = nullptr;
339 size_t argc = ARGS_ONE;
340 napi_value argv[ARGS_ONE] = {};
341 auto *napiAsrController = GetParamWithSync(env, info, argc, argv);
342 CHECK_AND_RETURN_RET_LOG(argc >= ARGS_ONE, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID),
343 "argCount invaild");
344
345 int32_t asrWhisperDetectionMode = 0;
346 int32_t retMode = NapiParamUtils::GetValueInt32(env, asrWhisperDetectionMode, argv[PARAM0]);
347 CHECK_AND_RETURN_RET_LOG(retMode == 0, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
348 "parameter verification failed: The param of mode must be mode enum"), "Input parameter value error. ");
349 CHECK_AND_RETURN_RET_LOG(asrWhisperDetectionMode == 0 || asrWhisperDetectionMode == 1,
350 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM), "Input parameter value error. ");
351 CHECK_AND_RETURN_RET_LOG(napiAsrController != nullptr, result, "napiAsrController is nullptr");
352 CHECK_AND_RETURN_RET_LOG(napiAsrController->audioMngr_ != nullptr, result, "audioMngr_ is nullptr");
353 int32_t res = napiAsrController->audioMngr_->SetAsrWhisperDetectionMode(
354 static_cast<AsrWhisperDetectionMode>(asrWhisperDetectionMode));
355 CHECK_AND_RETURN_RET_LOG(res == 0, NapiAudioError::ThrowErrorAndReturn(env,
356 GetResInt(res), GetResStr(res)), "SetAsrWhisperDetectionMode fail");
357 bool setSuc = ((res == 0) ? true : false);
358 NapiParamUtils::SetValueBoolean(env, setSuc, result);
359 return result;
360 }
361
GetAsrWhisperDetectionMode(napi_env env,napi_callback_info info)362 napi_value NapiAsrProcessingController::GetAsrWhisperDetectionMode(napi_env env, napi_callback_info info)
363 {
364 napi_value result = nullptr;
365 size_t argc = PARAM0;
366 auto *napiAsrController = GetParamWithSync(env, info, argc, nullptr);
367 AsrWhisperDetectionMode asrWhisperDetectionMode;
368 CHECK_AND_RETURN_RET_LOG(napiAsrController != nullptr, result, "napiAsrController is nullptr");
369 CHECK_AND_RETURN_RET_LOG(napiAsrController->audioMngr_ != nullptr, result, "audioMngr_ is nullptr");
370 int32_t res = napiAsrController->audioMngr_->GetAsrWhisperDetectionMode(asrWhisperDetectionMode);
371 CHECK_AND_RETURN_RET_LOG(res == 0, NapiAudioError::ThrowErrorAndReturn(env,
372 GetResInt(res), GetResStr(res)), "GetAsrWhisperDetectionMode fail");
373 NapiParamUtils::SetValueInt32(env, int32_t(asrWhisperDetectionMode), result);
374 return result;
375 }
376
SetAsrVoiceControlMode(napi_env env,napi_callback_info info)377 napi_value NapiAsrProcessingController::SetAsrVoiceControlMode(napi_env env, napi_callback_info info)
378 {
379 napi_value result = nullptr;
380 size_t argc = ARGS_TWO;
381 napi_value argv[ARGS_TWO] = {};
382 auto *napiAsrController = GetParamWithSync(env, info, argc, argv);
383 CHECK_AND_RETURN_RET_LOG(argc >= ARGS_TWO, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID,
384 "mandatory parameters are left unspecified"), "argCount invaild");
385
386 int32_t asrVoiceControlMode = 0;
387 bool on = false;
388 int32_t asrVoiceControlModeMax = static_cast<int32_t>(AsrVoiceControlMode::VOICE_TXRX_DECREASE);
389 int32_t retMode = NapiParamUtils::GetValueInt32(env, asrVoiceControlMode, argv[PARAM0]);
390 int32_t retBool = NapiParamUtils::GetValueBoolean(env, on, argv[PARAM1]);
391 CHECK_AND_RETURN_RET_LOG(retMode == 0, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
392 "parameter verification failed: The param of mode must be mode enum"), "Input parameter value error. ");
393 CHECK_AND_RETURN_RET_LOG(retBool == 0, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
394 "parameter verification failed: The param of mode must be bool"), "Input parameter value error. ");
395 CHECK_AND_RETURN_RET_LOG(asrVoiceControlMode >= 0 && asrVoiceControlMode <= asrVoiceControlModeMax,
396 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
397 "parameter verification failed: The param of mode must be enum AsrVoiceControlMode"),
398 "Input parameter value error. ");
399 CHECK_AND_RETURN_RET_LOG(napiAsrController != nullptr, result, "napiAsrController is nullptr");
400 CHECK_AND_RETURN_RET_LOG(napiAsrController->audioMngr_ != nullptr, result, "audioMngr_ is nullptr");
401 int32_t res = napiAsrController->audioMngr_->SetAsrVoiceControlMode(
402 static_cast<AsrVoiceControlMode>(asrVoiceControlMode), on);
403 CHECK_AND_RETURN_RET_LOG(res == 0, NapiAudioError::ThrowErrorAndReturn(env,
404 GetResInt(res), GetResStr(res)), "SetVCMode fail");
405 bool setSuc = ((res == 0) ? true : false);
406 NapiParamUtils::SetValueBoolean(env, setSuc, result);
407 return result;
408 }
409
SetAsrVoiceMuteMode(napi_env env,napi_callback_info info)410 napi_value NapiAsrProcessingController::SetAsrVoiceMuteMode(napi_env env, napi_callback_info info)
411 {
412 napi_value result = nullptr;
413 size_t argc = ARGS_TWO;
414 napi_value argv[ARGS_TWO] = {};
415 auto *napiAsrController = GetParamWithSync(env, info, argc, argv);
416 CHECK_AND_RETURN_RET_LOG(argc >= ARGS_TWO, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID,
417 "mandatory parameters are left unspecified"), "argCount invaild");
418
419 int32_t asrVoiceMuteMode = 0;
420 bool on = false;
421 int32_t asrVoiceMuteModeMax = static_cast<int32_t>(AsrVoiceMuteMode::OUTPUT_MUTE_EX);
422 int32_t retMode = NapiParamUtils::GetValueInt32(env, asrVoiceMuteMode, argv[PARAM0]);
423 int32_t retBool = NapiParamUtils::GetValueBoolean(env, on, argv[PARAM1]);
424 CHECK_AND_RETURN_RET_LOG(retMode == 0, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
425 "parameter verification failed: The param of mode must be mode enum"), "Input parameter value error. ");
426 CHECK_AND_RETURN_RET_LOG(retBool == 0, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
427 "parameter verification failed: The param of mode must be bool"), "Input parameter value error. ");
428 CHECK_AND_RETURN_RET_LOG(asrVoiceMuteMode >= 0 && asrVoiceMuteMode <= asrVoiceMuteModeMax,
429 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
430 "parameter verification failed: The param of mode must be enum AsrVoiceMuteMode"),
431 "Input parameter value error. ");
432 CHECK_AND_RETURN_RET_LOG(napiAsrController != nullptr, result, "napiAsrController is nullptr");
433 CHECK_AND_RETURN_RET_LOG(napiAsrController->audioMngr_ != nullptr, result, "audioMngr_ is nullptr");
434 int32_t res = napiAsrController->audioMngr_->SetAsrVoiceMuteMode(
435 static_cast<AsrVoiceMuteMode>(asrVoiceMuteMode), on);
436 CHECK_AND_RETURN_RET_LOG(res == 0, NapiAudioError::ThrowErrorAndReturn(env,
437 GetResInt(res), GetResStr(res)), "SetVMMode fail");
438 bool setSuc = ((res == 0) ? true : false);
439 NapiParamUtils::SetValueBoolean(env, setSuc, result);
440 return result;
441 }
442
IsWhispering(napi_env env,napi_callback_info info)443 napi_value NapiAsrProcessingController::IsWhispering(napi_env env, napi_callback_info info)
444 {
445 napi_value result = nullptr;
446 size_t argc = PARAM0;
447 auto *napiAsrController = GetParamWithSync(env, info, argc, nullptr);
448 CHECK_AND_RETURN_RET_LOG(napiAsrController != nullptr, result, "napiAsrController is nullptr");
449 CHECK_AND_RETURN_RET_LOG(napiAsrController->audioMngr_ != nullptr, result, "audioMngr_ is nullptr");
450 int32_t res = napiAsrController->audioMngr_->IsWhispering();
451 CHECK_AND_RETURN_RET_LOG(res == 0 || res == 1, NapiAudioError::ThrowErrorAndReturn(env,
452 GetResInt(res), GetResStr(res)), "IsWhispering fail");
453 bool setSuc = ((res == 0) ? true : false);
454 NapiParamUtils::SetValueBoolean(env, setSuc, result);
455 return result;
456 }
457 } // namespace AudioStandard
458 } // namespace OHOS