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 #ifdef SUPPORT_OLD_ENGINE
16 #undef LOG_TAG
17 #define LOG_TAG "AudioEnhanceChainAdapter"
18
19 #include "audio_enhance_chain_adapter.h"
20
21 #include <algorithm>
22 #include <map>
23
24 #include "audio_effect_log.h"
25 #include "audio_errors.h"
26 #include "audio_enhance_chain_manager.h"
27 #include "audio_effect_map.h"
28
29 using namespace OHOS::AudioStandard;
30
31 constexpr int32_t SAMPLE_FORMAT_U8 = 8;
32 constexpr int32_t SAMPLE_FORMAT_S16LE = 16;
33 constexpr int32_t SAMPLE_FORMAT_S24LE = 24;
34 constexpr int32_t SAMPLE_FORMAT_S32LE = 32;
35 constexpr int32_t SAMPLE_FORMAT_F32LE = 32;
36
37 namespace {
38 static const std::map<int32_t, pa_sample_format_t> FORMAT_CONVERT_MAP {
39 {SAMPLE_FORMAT_U8, PA_SAMPLE_U8},
40 {SAMPLE_FORMAT_S16LE, PA_SAMPLE_S16LE},
41 {SAMPLE_FORMAT_S24LE, PA_SAMPLE_S24LE},
42 {SAMPLE_FORMAT_S32LE, PA_SAMPLE_S32LE},
43 {SAMPLE_FORMAT_F32LE, PA_SAMPLE_FLOAT32LE },
44 };
45 }
46
ConvertFormat(uint8_t format)47 static pa_sample_format_t ConvertFormat(uint8_t format)
48 {
49 auto item = FORMAT_CONVERT_MAP.find(format);
50 if (item != FORMAT_CONVERT_MAP.end()) {
51 return item->second;
52 }
53 return PA_SAMPLE_INVALID;
54 }
55
EnhanceChainManagerCreateCb(const uint64_t sceneKeyCode,const struct DeviceAttrAdapter * adapter)56 int32_t EnhanceChainManagerCreateCb(const uint64_t sceneKeyCode, const struct DeviceAttrAdapter *adapter)
57 {
58 AudioEnhanceChainManager *audioEnhanceChainMananger = AudioEnhanceChainManager::GetInstance();
59 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainMananger != nullptr,
60 ERR_INVALID_HANDLE, "null audioEnhanceChainManager");
61 AudioEnhanceDeviceAttr deviceAttr = {};
62 deviceAttr.micRate = adapter->micRate;
63 deviceAttr.micChannels = adapter->micChannels;
64 deviceAttr.micFormat = adapter->micFormat;
65 if (adapter->needEc) {
66 deviceAttr.needEc = adapter->needEc;
67 deviceAttr.ecRate = adapter->ecRate;
68 deviceAttr.ecChannels = adapter->ecChannels;
69 deviceAttr.ecFormat = adapter->ecFormat;
70 } else {
71 deviceAttr.needEc = false;
72 }
73 if (adapter->needMicRef) {
74 deviceAttr.needMicRef = adapter->needMicRef;
75 deviceAttr.micRefRate = adapter->micRefRate;
76 deviceAttr.micRefChannels = adapter->micRefChannels;
77 deviceAttr.micRefFormat = adapter->micRefFormat;
78 } else {
79 deviceAttr.needMicRef = false;
80 }
81 return audioEnhanceChainMananger->CreateAudioEnhanceChainDynamic(sceneKeyCode, deviceAttr);
82 }
83
EnhanceChainManagerReleaseCb(const uint64_t sceneKeyCode)84 int32_t EnhanceChainManagerReleaseCb(const uint64_t sceneKeyCode)
85 {
86 AudioEnhanceChainManager *audioEnhanceChainMananger = AudioEnhanceChainManager::GetInstance();
87 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainMananger != nullptr,
88 ERR_INVALID_HANDLE, "null audioEnhanceChainManager");
89 return audioEnhanceChainMananger->ReleaseAudioEnhanceChainDynamic(sceneKeyCode);
90 }
91
EnhanceChainManagerExist(const uint64_t sceneKeyCode)92 bool EnhanceChainManagerExist(const uint64_t sceneKeyCode)
93 {
94 return true;
95 }
96
EnhanceChainManagerGetAlgoConfig(const uint64_t sceneKeyCode,pa_sample_spec * micSpec,pa_sample_spec * ecSpec,pa_sample_spec * micRefSpec)97 int32_t EnhanceChainManagerGetAlgoConfig(const uint64_t sceneKeyCode, pa_sample_spec *micSpec,
98 pa_sample_spec *ecSpec, pa_sample_spec *micRefSpec)
99 {
100 AudioEnhanceChainManager *audioEnhanceChainMananger = AudioEnhanceChainManager::GetInstance();
101 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainMananger != nullptr,
102 ERROR, "null audioEnhanceChainManager");
103 AudioBufferConfig micConfig = {};
104 AudioBufferConfig ecConfig = {};
105 AudioBufferConfig micRefConfig = {};
106
107 int32_t ret = audioEnhanceChainMananger->AudioEnhanceChainGetAlgoConfig(sceneKeyCode, micConfig, ecConfig,
108 micRefConfig);
109 if (ret != 0 || micConfig.samplingRate == 0) {
110 return ERROR;
111 }
112
113 micSpec->rate = micConfig.samplingRate;
114 micSpec->channels = static_cast<uint8_t>(micConfig.channels);
115 micSpec->format = ConvertFormat(micConfig.format);
116
117 ecSpec->rate = ecConfig.samplingRate;
118 ecSpec->channels = static_cast<uint8_t>(ecConfig.channels);
119 ecSpec->format = ConvertFormat(ecConfig.format);
120
121 micRefSpec->rate = micRefConfig.samplingRate;
122 micRefSpec->channels = static_cast<uint8_t>(micRefConfig.channels);
123 micRefSpec->format = ConvertFormat(micRefConfig.format);
124
125 return SUCCESS;
126 }
127
EnhanceChainManagerIsEmptyEnhanceChain(void)128 bool EnhanceChainManagerIsEmptyEnhanceChain(void)
129 {
130 return false;
131 }
132
EnhanceChainManagerInitEnhanceBuffer(void)133 int32_t EnhanceChainManagerInitEnhanceBuffer(void)
134 {
135 return SUCCESS;
136 }
137
CopyToEnhanceBufferAdapter(void * data,uint32_t length)138 int32_t CopyToEnhanceBufferAdapter(void *data, uint32_t length)
139 {
140 return SUCCESS;
141 }
142
CopyEcdataToEnhanceBufferAdapter(void * data,uint32_t length)143 int32_t CopyEcdataToEnhanceBufferAdapter(void *data, uint32_t length)
144 {
145 return SUCCESS;
146 }
147
CopyMicRefdataToEnhanceBufferAdapter(void * data,uint32_t length)148 int32_t CopyMicRefdataToEnhanceBufferAdapter(void *data, uint32_t length)
149 {
150 return SUCCESS;
151 }
152
CopyFromEnhanceBufferAdapter(void * data,uint32_t length)153 int32_t CopyFromEnhanceBufferAdapter(void *data, uint32_t length)
154 {
155 return SUCCESS;
156 }
157
EnhanceChainManagerProcess(const uint64_t sceneKeyCode,uint32_t length)158 int32_t EnhanceChainManagerProcess(const uint64_t sceneKeyCode, uint32_t length)
159 {
160 return SUCCESS;
161 }
162
EnhanceChainManagerProcessDefault(const uint32_t captureId,uint32_t length)163 int32_t EnhanceChainManagerProcessDefault(const uint32_t captureId, uint32_t length)
164 {
165 AudioEnhanceChainManager *audioEnhanceChainMananger = AudioEnhanceChainManager::GetInstance();
166 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainMananger != nullptr,
167 ERR_INVALID_HANDLE, "null audioEnhanceChainManager");
168 AUDIO_DEBUG_LOG("%{public}u default process success", captureId);
169 return SUCCESS;
170 }
171
GetSceneTypeCode(const char * sceneType,uint64_t * sceneTypeCode)172 int32_t GetSceneTypeCode(const char *sceneType, uint64_t *sceneTypeCode)
173 {
174 std::string sceneTypeString = "";
175 const std::unordered_map<AudioEnhanceScene, std::string> &audioEnhanceSupportedSceneTypes =
176 GetEnhanceSupportedSceneType();
177 if (sceneType) {
178 sceneTypeString = sceneType;
179 } else {
180 return ERROR;
181 }
182 auto item = std::find_if(audioEnhanceSupportedSceneTypes.begin(), audioEnhanceSupportedSceneTypes.end(),
183 [&sceneTypeString](const std::pair<AudioEnhanceScene, std::string>& element) -> bool {
184 return element.second == sceneTypeString;
185 });
186 if (item == audioEnhanceSupportedSceneTypes.end()) {
187 return ERROR;
188 }
189 *sceneTypeCode = static_cast<uint64_t>(item->first);
190 return SUCCESS;
191 }
192
EnhanceChainManagerSendInitCommand()193 int32_t EnhanceChainManagerSendInitCommand()
194 {
195 AudioEnhanceChainManager *audioEnhanceChainMananger = AudioEnhanceChainManager::GetInstance();
196 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainMananger != nullptr,
197 ERROR, "null audioEnhanceChainManager");
198 return audioEnhanceChainMananger->SendInitCommand();
199 }
200 #endif // SUPPORT_OLD_ENGINE