1 /*
2 * Copyright (c) 2023 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
16 #include "enroll_intell_voice_engine.h"
17 #include "i_intell_voice_engine.h"
18 #include "intell_voice_manager.h"
19 #include "intell_voice_log.h"
20
21 #define LOG_TAG "EnrollIntellVoiceEngine"
22 using namespace std;
23 using namespace OHOS::IntellVoiceEngine;
24
25 namespace OHOS {
26 namespace IntellVoice {
27 static constexpr int32_t MIN_BUFFER_SIZE = 1280; // 16 * 2 * 40ms
28 static constexpr int32_t CHANNEL_CNT = 1;
29 static constexpr int32_t BITS_PER_SAMPLE = 16;
30 static constexpr int32_t SAMPLE_RATE = 16000;
31
EnrollIntellVoiceEngine(const EnrollIntelligentVoiceEngineDescriptor & descriptor)32 EnrollIntellVoiceEngine::EnrollIntellVoiceEngine(const EnrollIntelligentVoiceEngineDescriptor &descriptor)
33 {
34 INTELL_VOICE_LOG_INFO("enter");
35
36 descriptor_ = make_unique<EnrollIntelligentVoiceEngineDescriptor>();
37 if (descriptor_ != nullptr) {
38 descriptor_->wakeupPhrase = descriptor.wakeupPhrase;
39 }
40 auto mgr = IntellVoiceManager::GetInstance();
41 if (mgr != nullptr) {
42 result_ = mgr->CreateIntellVoiceEngine(INTELL_VOICE_ENROLL, engine_);
43 if (result_ != INTELLIGENT_VOICE_SUCCESS) {
44 INTELL_VOICE_LOG_ERROR("create enroll engine failed, ret:%{public}d", result_);
45 }
46 } else {
47 INTELL_VOICE_LOG_ERROR("mgr is nullptr");
48 result_ = INTELLIGENT_VOICE_SYSTEM_ERROR;
49 }
50 }
51
~EnrollIntellVoiceEngine()52 EnrollIntellVoiceEngine::~EnrollIntellVoiceEngine()
53 {
54 INTELL_VOICE_LOG_INFO("enter");
55 auto mgr = IntellVoiceManager::GetInstance();
56 if (mgr != nullptr) {
57 mgr->ReleaseIntellVoiceEngine(INTELL_VOICE_ENROLL);
58 }
59 }
60
Init(const EngineConfig & config)61 int32_t EnrollIntellVoiceEngine::Init(const EngineConfig &config)
62 {
63 INTELL_VOICE_LOG_INFO("enter");
64 if (engine_ == nullptr) {
65 INTELL_VOICE_LOG_ERROR("IntellVoiceEngine is null");
66 return -1;
67 }
68
69 engine_->SetParameter("language=" + config.language);
70 engine_->SetParameter("area=" + config.region);
71
72 IntellVoiceEngineInfo info = {};
73 info.wakeupPhrase = descriptor_->wakeupPhrase;
74 info.isPcmFromExternal = false;
75 info.minBufSize = MIN_BUFFER_SIZE;
76 info.sampleChannels = CHANNEL_CNT;
77 info.bitsPerSample = BITS_PER_SAMPLE;
78 info.sampleRate = SAMPLE_RATE;
79
80 return engine_->Attach(info);
81 }
82
Start(const bool & isLast)83 int32_t EnrollIntellVoiceEngine::Start(const bool &isLast)
84 {
85 INTELL_VOICE_LOG_INFO("enter");
86 if (engine_ == nullptr) {
87 INTELL_VOICE_LOG_ERROR("engine is null");
88 return -1;
89 }
90 return engine_->Start(isLast);
91 }
92
Stop()93 int32_t EnrollIntellVoiceEngine::Stop()
94 {
95 INTELL_VOICE_LOG_INFO("enter");
96 if (engine_ == nullptr) {
97 INTELL_VOICE_LOG_ERROR("engine is null");
98 return -1;
99 }
100 return engine_->Stop();
101 }
102
Commit()103 int32_t EnrollIntellVoiceEngine::Commit()
104 {
105 INTELL_VOICE_LOG_INFO("enter");
106 if (engine_ == nullptr) {
107 INTELL_VOICE_LOG_ERROR("engine is null");
108 return -1;
109 }
110 string keyValueList = "CommitEnrollment=true";
111 return engine_->SetParameter(keyValueList);
112 }
113
SetSensibility(const int32_t & sensibility)114 int32_t EnrollIntellVoiceEngine::SetSensibility(const int32_t &sensibility)
115 {
116 INTELL_VOICE_LOG_INFO("enter");
117 if (engine_ == nullptr) {
118 INTELL_VOICE_LOG_ERROR("engine is null");
119 return -1;
120 }
121 string keyValueList = "Sensibility=" + to_string(sensibility);
122 return engine_->SetParameter(keyValueList);
123 }
124
SetWakeupHapInfo(const WakeupHapInfo & info)125 int32_t EnrollIntellVoiceEngine::SetWakeupHapInfo(const WakeupHapInfo &info)
126 {
127 INTELL_VOICE_LOG_INFO("enter");
128 int32_t ret;
129 if (engine_ == nullptr) {
130 INTELL_VOICE_LOG_ERROR("engine is null");
131 return -1;
132 }
133 ret = engine_->SetParameter("wakeup_bundle_name=" + info.bundleName);
134 ret += engine_->SetParameter("wakeup_ability_name=" + info.abilityName);
135 return ret;
136 }
137
SetParameter(const string & key,const string & value)138 int32_t EnrollIntellVoiceEngine::SetParameter(const string &key, const string &value)
139 {
140 INTELL_VOICE_LOG_INFO("enter");
141 if (engine_ == nullptr) {
142 INTELL_VOICE_LOG_ERROR("engine is null");
143 return -1;
144 }
145 string keyValueList = key + "=" + value;
146 return engine_->SetParameter(keyValueList);
147 }
148
GetParameter(const std::string & key)149 std::string EnrollIntellVoiceEngine::GetParameter(const std::string &key)
150 {
151 INTELL_VOICE_LOG_INFO("enter");
152 if (engine_ == nullptr) {
153 INTELL_VOICE_LOG_ERROR("engine is null");
154 return "";
155 }
156 return engine_->GetParameter(key);
157 }
158
Evaluate(const std::string & word,EvaluationResult & result)159 int32_t EnrollIntellVoiceEngine::Evaluate(const std::string &word, EvaluationResult &result)
160 {
161 INTELL_VOICE_LOG_INFO("enter");
162 if (descriptor_ != nullptr) {
163 descriptor_->wakeupPhrase = word;
164 }
165 if (engine_ == nullptr) {
166 INTELL_VOICE_LOG_ERROR("engine_ is nullptr");
167 return -1;
168 }
169 return engine_->Evaluate(word, result);
170 }
171
Release()172 int32_t EnrollIntellVoiceEngine::Release()
173 {
174 INTELL_VOICE_LOG_INFO("enter");
175 if (engine_ == nullptr) {
176 INTELL_VOICE_LOG_ERROR("engine is null");
177 return -1;
178 }
179
180 return engine_->Detach();
181 }
182
SetCallback(shared_ptr<IIntellVoiceEngineEventCallback> callback)183 int32_t EnrollIntellVoiceEngine::SetCallback(shared_ptr<IIntellVoiceEngineEventCallback> callback)
184 {
185 INTELL_VOICE_LOG_INFO("enter");
186 if (engine_ == nullptr) {
187 INTELL_VOICE_LOG_ERROR("engine is null");
188 return -1;
189 }
190
191 callback_ = sptr<EngineCallbackInner>(new (std::nothrow) EngineCallbackInner(callback));
192 if (callback_ == nullptr) {
193 INTELL_VOICE_LOG_ERROR("callback_ is nullptr");
194 return -1;
195 }
196 engine_->SetCallback(callback_->AsObject());
197 return 0;
198 }
199 }
200 }