• 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 
23 #include "audio_effect.h"
24 
25 namespace OHOS {
26 namespace AudioStandard {
27 
28 struct EnhanceBuffer {
29     std::vector<uint8_t> micBufferIn;
30     std::vector<uint8_t> micBufferOut;
31     std::vector<uint8_t> ecBuffer;
32     std::vector<uint8_t> micRefBuffer;
33 };
34 
35 struct AlgoAttr {
36     uint32_t bitDepth;
37     uint32_t batchLen;
38     uint32_t byteLenPerFrame;
39 };
40 
41 struct AlgoCache {
42     std::vector<uint8_t> input;
43     std::vector<uint8_t> output;
44 };
45 
46 struct AudioEnhanceDeviceAttr {
47     uint32_t micRate;
48     uint32_t micChannels;
49     uint32_t micFormat;
50     bool needEc;
51     uint32_t ecRate;
52     uint32_t ecChannels;
53     uint32_t ecFormat;
54     bool needMicRef;
55     uint32_t micRefRate;
56     uint32_t micRefChannels;
57     uint32_t micRefFormat;
58 };
59 
60 struct AudioEnhanceParamAdapter {
61     uint32_t muteInfo;
62     uint32_t volumeInfo;
63     uint32_t foldState;
64     std::string preDevice;
65     std::string postDevice;
66     std::string sceneType;
67     std::string preDeviceName;
68 };
69 
70 class AudioEnhanceChain {
71 public:
72     AudioEnhanceChain(const std::string &scene, const AudioEnhanceParamAdapter &algoParam,
73         const AudioEnhanceDeviceAttr &deviceAttr, const bool defaultFlag);
74     ~AudioEnhanceChain();
75     int32_t AddEnhanceHandle(AudioEffectHandle handle, AudioEffectLibrary *libHandle, const std::string &enhance,
76         const std::string &property);
77     bool IsEmptyEnhanceHandles();
78     void GetAlgoConfig(AudioBufferConfig &micConfig, AudioBufferConfig &ecConfig, AudioBufferConfig &micRefConfig);
79     uint32_t GetAlgoBufferSize();
80     uint32_t GetAlgoBufferSizeEc();
81     uint32_t GetAlgoBufferSizeMicRef();
82     int32_t ApplyEnhanceChain(std::unique_ptr<EnhanceBuffer> &enhanceBuffer, uint32_t length);
83     int32_t SetEnhanceProperty(const std::string &effect, const std::string &property);
84     int32_t SetEnhanceParam(bool mute, uint32_t systemVol);
85     int32_t SetEnhanceParamToHandle(AudioEffectHandle handle);
86     bool IsDefaultChain();
87     int32_t SetInputDevice(const std::string &inputDevice, const std::string &deviceName);
88     int32_t SetFoldState(uint32_t foldState);
89     int32_t InitCommand();
90 
91 private:
92     void InitAudioEnhanceChain();
93     void InitDump();
94     void ReleaseEnhanceChain();
95     void WriteDumpFile(std::unique_ptr<EnhanceBuffer> &enhanceBuffer, uint32_t length);
96     int32_t GetOneFrameInputData(std::unique_ptr<EnhanceBuffer> &enhanceBuffer);
97     int32_t DeinterleaverData(uint8_t *src, uint32_t channel, uint8_t *dst, uint32_t offset);
98     int32_t SetPropertyToHandle(AudioEffectHandle handle, const std::string &property);
99     static AudioSampleFormat ConvertFormat(uint32_t format);
100 
101     bool setConfigFlag_;
102     std::mutex chainMutex_;
103     std::string sceneType_;
104     AlgoAttr algoAttr_;
105     AlgoConfig algoSupportedConfig_;
106     AlgoCache algoCache_;
107     AudioEnhanceParamAdapter algoParam_;
108     AudioEnhanceDeviceAttr deviceAttr_;
109     FILE *dumpFileIn_ = nullptr;
110     FILE *dumpFileOut_ = nullptr;
111     FILE *dumpFileDeinterLeaver_ = nullptr;
112     bool needEcFlag_;
113     bool needMicRefFlag_;
114     bool defaultFlag_;
115     std::vector<AudioEffectHandle> standByEnhanceHandles_;
116     std::vector<std::string> enhanceNames_;
117     std::vector<AudioEffectLibrary*> enhanceLibHandles_;
118     mutable int64_t volumeDataCount_ = 0;
119 };
120 
121 }  // namespace AudioStandard
122 }  // namespace OHOS
123 #endif // AUDIO_ENHANCE_CHAIN_H