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 IntellVoiceManager::GetInstance()->CreateIntellVoiceEngine(INTELL_VOICE_ENROLL, engine_);
41 }
42
~EnrollIntellVoiceEngine()43 EnrollIntellVoiceEngine::~EnrollIntellVoiceEngine()
44 {
45 INTELL_VOICE_LOG_INFO("enter");
46 IntellVoiceManager::GetInstance()->ReleaseIntellVoiceEngine(INTELL_VOICE_ENROLL);
47 }
48
Init(const EngineConfig & config)49 int32_t EnrollIntellVoiceEngine::Init(const EngineConfig &config)
50 {
51 INTELL_VOICE_LOG_INFO("enter");
52 if (engine_ == nullptr) {
53 INTELL_VOICE_LOG_ERROR("IntellVoiceEngine is null");
54 return -1;
55 }
56
57 engine_->SetParameter("language=" + config.language);
58 engine_->SetParameter("area=" + config.region);
59
60 IntellVoiceEngineInfo info = {};
61 info.wakeupPhrase = descriptor_->wakeupPhrase;
62 info.isPcmFromExternal = false;
63 info.minBufSize = MIN_BUFFER_SIZE;
64 info.sampleChannels = CHANNEL_CNT;
65 info.bitsPerSample = BITS_PER_SAMPLE;
66 info.sampleRate = SAMPLE_RATE;
67
68 return engine_->Attach(info);
69 }
70
Start(const bool & isLast)71 int32_t EnrollIntellVoiceEngine::Start(const bool &isLast)
72 {
73 INTELL_VOICE_LOG_INFO("enter");
74 if (engine_ == nullptr) {
75 INTELL_VOICE_LOG_ERROR("engine is null");
76 return -1;
77 }
78 return engine_->Start(isLast);
79 }
80
Stop()81 int32_t EnrollIntellVoiceEngine::Stop()
82 {
83 INTELL_VOICE_LOG_INFO("enter");
84 if (engine_ == nullptr) {
85 INTELL_VOICE_LOG_ERROR("engine is null");
86 return -1;
87 }
88 return engine_->Stop();
89 }
90
Commit()91 int32_t EnrollIntellVoiceEngine::Commit()
92 {
93 INTELL_VOICE_LOG_INFO("enter");
94 if (engine_ == nullptr) {
95 INTELL_VOICE_LOG_ERROR("engine is null");
96 return -1;
97 }
98 string keyValueList = "CommitEnrollment=true";
99 return engine_->SetParameter(keyValueList);
100 }
101
SetSensibility(const int32_t & sensibility)102 int32_t EnrollIntellVoiceEngine::SetSensibility(const int32_t &sensibility)
103 {
104 INTELL_VOICE_LOG_INFO("enter");
105 if (engine_ == nullptr) {
106 INTELL_VOICE_LOG_ERROR("engine is null");
107 return -1;
108 }
109 string keyValueList = "Sensibility=" + to_string(sensibility);
110 return engine_->SetParameter(keyValueList);
111 }
112
SetWakeupHapInfo(const WakeupHapInfo & info)113 int32_t EnrollIntellVoiceEngine::SetWakeupHapInfo(const WakeupHapInfo &info)
114 {
115 INTELL_VOICE_LOG_INFO("enter");
116 int32_t ret;
117 if (engine_ == nullptr) {
118 INTELL_VOICE_LOG_ERROR("engine is null");
119 return -1;
120 }
121 ret = engine_->SetParameter("wakeup_bundle_name=" + info.bundleName);
122 ret += engine_->SetParameter("wakeup_ability_name=" + info.abilityName);
123 return ret;
124 }
125
SetParameter(const string & key,const string & value)126 int32_t EnrollIntellVoiceEngine::SetParameter(const string &key, const string &value)
127 {
128 INTELL_VOICE_LOG_INFO("enter");
129 if (engine_ == nullptr) {
130 INTELL_VOICE_LOG_ERROR("engine is null");
131 return -1;
132 }
133 string keyValueList = key + "=" + value;
134 return engine_->SetParameter(keyValueList);
135 }
136
Evaluate(const std::string & word,EvaluationResultInfo & info)137 int32_t EnrollIntellVoiceEngine::Evaluate(const std::string &word, EvaluationResultInfo &info)
138 {
139 INTELL_VOICE_LOG_INFO("enter");
140 if (descriptor_ != nullptr) {
141 descriptor_->wakeupPhrase = word;
142 }
143 if (engine_ == nullptr) {
144 INTELL_VOICE_LOG_ERROR("engine_ is nullptr");
145 return -1;
146 }
147 return engine_->Evaluate(word, info);
148 }
149
Release()150 int32_t EnrollIntellVoiceEngine::Release()
151 {
152 INTELL_VOICE_LOG_INFO("enter");
153 if (engine_ == nullptr) {
154 INTELL_VOICE_LOG_ERROR("engine is null");
155 return -1;
156 }
157
158 return engine_->Detach();
159 }
160
SetCallback(shared_ptr<IIntellVoiceEngineEventCallback> callback)161 int32_t EnrollIntellVoiceEngine::SetCallback(shared_ptr<IIntellVoiceEngineEventCallback> callback)
162 {
163 INTELL_VOICE_LOG_INFO("enter");
164 if (engine_ == nullptr) {
165 INTELL_VOICE_LOG_ERROR("engine is null");
166 return -1;
167 }
168
169 callback_ = sptr<EngineCallbackInner>(new (std::nothrow) EngineCallbackInner(callback));
170 if (callback_ == nullptr) {
171 INTELL_VOICE_LOG_ERROR("callback_ is nullptr");
172 return -1;
173 }
174 engine_->SetCallback(callback_->AsObject());
175 return 0;
176 }
177 }
178 }