• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef AUDIO_ENHANCE_CHAIN_H
17 #define AUDIO_ENHANCE_CHAIN_H
18 
19 #include <vector>
20 #include <mutex>
21 #include <map>
22 #include <memory>
23 
24 #include "audio_effect.h"
25 #include "audio_effect_common.h"
26 #include "thread_handler.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 struct EnhanceModulePara {
31     std::string enhanceName;
32     std::string enhanceProp;
33     std::string libName;
34     AudioEffectLibrary *libHandle { nullptr };
35 };
36 
37 struct EnhanceModule {
38     std::string enhanceName;
39     AudioEffectHandle enhanceHandle { nullptr };
40     AudioEffectLibrary *libHandle { nullptr };
41 };
42 
43 struct EnhanceBuffer {
44     std::vector<uint8_t> micBuffer;
45     std::vector<uint8_t> ecBuffer;
46     std::vector<uint8_t> micRefBuffer;
47 };
48 
49 struct AlgoAttr {
50     uint32_t bitDepth;
51     uint32_t batchLen;
52     uint32_t byteLenPerFrame;
53 };
54 
55 struct AlgoCache {
56     std::vector<uint8_t> input;
57     std::vector<uint8_t> output;
58 };
59 
60 struct AudioEnhanceParamAdapter {
61     uint32_t muteInfo { 0 };
62     uint32_t volumeInfo { 0 };
63     uint32_t foldState { FOLD_STATE_MIDDLE };
64     std::string preDevice;
65     std::string postDevice;
66     std::string sceneType;
67     std::string preDeviceName;
68 };
69 
70 class AudioEnhanceChain : public std::enable_shared_from_this<AudioEnhanceChain> {
71 public:
72     AudioEnhanceChain(uint64_t chainId, const std::string &scene, ScenePriority scenePriority,
73         const AudioEnhanceParamAdapter &algoParam, const AudioEnhanceDeviceAttr &deviceAttr);
74     ~AudioEnhanceChain();
75 
76     bool IsEmptyEnhanceHandles();
77     void GetAlgoConfig(AudioBufferConfig &micConfig, AudioBufferConfig &ecConfig, AudioBufferConfig &micRefConfig);
78     uint64_t GetChainId(void) const;
79     ScenePriority GetScenePriority(void) const;
80     int32_t CreateAllEnhanceModule(const std::vector<EnhanceModulePara> &moduleParas);
81     int32_t SetEnhanceProperty(const std::string &effect, const std::string &property);
82     int32_t SetEnhanceParam(bool mute, uint32_t systemVol);
83     int32_t SetInputDevice(const std::string &inputDevice, const std::string &deviceName);
84     int32_t SetFoldState(uint32_t foldState);
85     int32_t SetThreadHandler(const std::shared_ptr<ThreadHandler> &threadHandler);
86     int32_t GetOutputDataFromChain(void *buf, size_t bufSize);
87     int32_t ApplyEnhanceChain(const EnhanceTransBuffer &transBuf);
88     int32_t InitCommand();
89 
90 private:
91     void InitAudioEnhanceChain();
92     void ReleaseAllEnhanceModule();
93     int32_t DeinterleaverData(uint8_t *src, uint32_t channel, uint8_t *dst, uint32_t offset);
94     int32_t SetPropertyToHandle(AudioEffectHandle handle, const std::string &property);
95     int32_t SetEnhanceParamToHandle(AudioEffectHandle handle);
96     int32_t PrepareChainInputData(void);
97     int32_t ProcessInitCommand(void);
98     int32_t ProcessSetFoldState(uint32_t foldState);
99     int32_t ProcessSetEnhanceParam(bool mute, uint32_t systemVol);
100     int32_t ProcessCreateAllEnhanceModule(const std::vector<EnhanceModulePara> &moduleParas);
101     int32_t ProcessSetInputDevice(const std::string &inputDevice, const std::string &deviceName);
102     int32_t ProcessSetEnhanceProperty(const std::string &enhance, const std::string &property);
103     int32_t ProcessApplyEnhanceChain(void);
104     int32_t WriteChainOutputData(void *buf, size_t bufSize);
105     int32_t CacheChainInputData(const EnhanceTransBuffer &transBuf);
106     int32_t InitSingleEnhanceModule(AudioEffectHandle enhanceHandle, const std::string &enhanceProp);
107     void ScheduleAudioTask(const ThreadHandler::Task &task);
108 
109     AlgoAttr algoAttr_;
110     AlgoConfig algoSupportedConfig_;
111     AlgoCache algoCache_;
112     EnhanceBuffer enhanceBuf_;
113     uint64_t chainId_ { 0 };
114     std::string sceneType_;
115     ScenePriority scenePriority_ { DEFAULT_SCENE };
116     AudioEnhanceParamAdapter algoParam_;
117     AudioEnhanceDeviceAttr deviceAttr_;
118     bool needEcFlag_ { false };
119     bool needMicRefFlag_ { false };
120     bool hasTask_ { false };
121     bool chainIsReady_ { false };
122     std::vector<EnhanceModule> enhanceModules_;
123     std::vector<uint8_t> outputCache_;
124     std::shared_ptr<ThreadHandler> threadHandler_ { nullptr };
125     std::mutex chainMutex_;
126     FILE *dumpFileIn_ { nullptr };
127     FILE *dumpFileOut_ { nullptr };
128     std::string traceTagIn_;
129     std::string traceTagOut_;
130     AudioStreamInfo dfxStreamInfo_ {};
131     mutable int64_t volumeDataCountIn_ { 0 };
132     mutable int64_t volumeDataCountOut_ { 0 };
133 };
134 
135 } // namespace AudioStandard
136 } // namespace OHOS
137 #endif // AUDIO_ENHANCE_CHAIN_H