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