• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef AUDIO_EFFECT_CHAIN_MANAGER_H
18 #define AUDIO_EFFECT_CHAIN_MANAGER_H
19 
20 #include <cstdio>
21 #include <cstdint>
22 #include <cassert>
23 #include <cstdint>
24 #include <cstddef>
25 #include <map>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 #include <mutex>
30 
31 #include "audio_effect_chain_adapter.h"
32 #include "audio_effect.h"
33 
34 namespace OHOS {
35 namespace AudioStandard {
36 
37 const uint32_t NUM_SET_EFFECT_PARAM = 3;
38 const uint32_t DEFAULT_FRAMELEN = 1440;
39 const uint32_t DEFAULT_SAMPLE_RATE = 48000;
40 const uint32_t DEFAULT_NUM_CHANNEL = 2;
41 const uint32_t FACTOR_TWO = 2;
42 const std::string DEFAULT_DEVICE_SINK = "Speaker";
43 
44 class AudioEffectChain {
45 public:
46     AudioEffectChain(std::string scene);
47     ~AudioEffectChain();
48     std::string GetEffectMode();
49     void SetEffectMode(std::string mode);
50     void ReleaseEffectChain();
51     void AddEffectHandleBegin();
52     void AddEffectHandleEnd();
53     void AddEffectHandle(AudioEffectHandle effectHandle, AudioEffectLibrary *libHandle);
54     void SetEffectChain(std::vector<AudioEffectHandle> &effHandles, std::vector<AudioEffectLibrary *> &libHandles);
55     void ApplyEffectChain(float *bufIn, float *bufOut, uint32_t frameLen);
56     void SetIOBufferConfig(bool isInput, uint32_t samplingRate, uint32_t channels);
57     bool IsEmptyEffectHandles();
58     void Dump();
59     void IgnoreEffect(bool ignored);
60 private:
61     std::mutex reloadMutex;
62     std::string sceneType;
63     std::string effectMode;
64     std::vector<AudioEffectHandle> standByEffectHandles;
65     std::vector<AudioEffectLibrary*> libHandles;
66     AudioEffectConfig ioBufferConfig;
67     AudioBuffer audioBufIn;
68     AudioBuffer audioBufOut;
69     bool isEffectIgnored;
70 };
71 
72 class AudioEffectChainManager {
73 public:
74     AudioEffectChainManager();
75     ~AudioEffectChainManager();
76     static AudioEffectChainManager *GetInstance();
77     void InitAudioEffectChainManager(std::vector<EffectChain> &effectChains,
78         std::unordered_map<std::string, std::string> &map,
79         std::vector<std::unique_ptr<AudioEffectLibEntry>> &effectLibraryList);
80     int32_t CreateAudioEffectChain(std::string sceneType, BufferAttr *bufferAttr);
81     int32_t SetAudioEffectChain(std::string sceneType, std::string effectChain);
82     bool ExistAudioEffectChain(std::string sceneType, std::string effectMode);
83     int32_t ApplyAudioEffectChain(std::string sceneType, BufferAttr *bufferAttr);
84     int32_t SetOutputDeviceSink(int32_t device, std::string &sinkName);
85     std::string GetDeviceTypeName();
86     int32_t GetFrameLen();
87     int32_t SetFrameLen(int32_t frameLen);
88     void Dump();
89     void IgnoreEffectChain(bool ignored);
90 private:
91     std::map<std::string, AudioEffectLibEntry*> EffectToLibraryEntryMap_;
92     std::map<std::string, std::string> EffectToLibraryNameMap_;
93     std::map<std::string, std::vector<std::string>> EffectChainToEffectsMap_;
94     std::map<std::string, std::string> SceneTypeAndModeToEffectChainNameMap_;
95     std::map<std::string, AudioEffectChain*> SceneTypeToEffectChainMap_;
96     uint32_t frameLen_ = DEFAULT_FRAMELEN;
97     DeviceType deviceType_ = DEVICE_TYPE_SPEAKER;
98     std::string deviceSink_ = DEFAULT_DEVICE_SINK;
99     bool isInitialized_ = false;
100 };
101 
102 }  // namespace AudioStandard
103 }  // namespace OHOS
104 #endif // AUDIO_EFFECT_CHAIN_MANAGER_H
105