1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <audio_effects/effect_dynamicsprocessing.h>
20 
21 #include "effect-impl/EffectContext.h"
22 
23 #include <any>
24 #include <cstddef>
25 #include <dsp/DPBase.h>
26 #include <dsp/DPFrequency.h>
27 
28 namespace aidl::android::hardware::audio::effect {
29 
30 enum DynamicsProcessingState {
31     DYNAMICS_PROCESSING_STATE_UNINITIALIZED,
32     DYNAMICS_PROCESSING_STATE_INITIALIZED,
33     DYNAMICS_PROCESSING_STATE_ACTIVE,
34 };
35 
36 class DynamicsProcessingContext final : public EffectContext {
37   public:
38     DynamicsProcessingContext(int statusDepth, const Parameter::Common& common);
39     ~DynamicsProcessingContext() = default;
40     RetCode enable();
41     RetCode disable();
42     void reset();
43 
44     // override EffectContext::setCommon to update mChannelCount
45     RetCode setCommon(const Parameter::Common& common) override;
46     RetCode setVolumeStereo(const Parameter::VolumeStereo& volumeStereo) override;
47     Parameter::VolumeStereo getVolumeStereo() override;
48 
49     RetCode setEngineArchitecture(const DynamicsProcessing::EngineArchitecture& engineArchitecture);
50     RetCode setPreEq(const std::vector<DynamicsProcessing::ChannelConfig>& eqChannels);
51     RetCode setPostEq(const std::vector<DynamicsProcessing::ChannelConfig>& eqChannels);
52     RetCode setPreEqBand(const std::vector<DynamicsProcessing::EqBandConfig>& eqBands);
53     RetCode setPostEqBand(const std::vector<DynamicsProcessing::EqBandConfig>& eqBands);
54     RetCode setMbc(const std::vector<DynamicsProcessing::ChannelConfig>& mbcChannels);
55     RetCode setMbcBand(const std::vector<DynamicsProcessing::MbcBandConfig>& eqBands);
56     RetCode setLimiter(const std::vector<DynamicsProcessing::LimiterConfig>& limiters);
57     RetCode setInputGain(const std::vector<DynamicsProcessing::InputGain>& gain);
58 
59     DynamicsProcessing::EngineArchitecture getEngineArchitecture();
60     std::vector<DynamicsProcessing::ChannelConfig> getPreEq();
61     std::vector<DynamicsProcessing::ChannelConfig> getPostEq();
62     std::vector<DynamicsProcessing::EqBandConfig> getPreEqBand();
63     std::vector<DynamicsProcessing::EqBandConfig> getPostEqBand();
64     std::vector<DynamicsProcessing::ChannelConfig> getMbc();
65     std::vector<DynamicsProcessing::MbcBandConfig> getMbcBand();
66     std::vector<DynamicsProcessing::LimiterConfig> getLimiter();
67     std::vector<DynamicsProcessing::InputGain> getInputGain();
68 
69     IEffect::Status dpeProcess(float* in, float* out, int samples);
70 
71   private:
72     static constexpr float kPreferredProcessingDurationMs = 10.0f;
73     static constexpr int kBandCount = 5;
74     int mChannelCount = 0;
75     DynamicsProcessingState mState = DYNAMICS_PROCESSING_STATE_UNINITIALIZED;
76     std::unique_ptr<dp_fx::DPFrequency> mDpFreq = nullptr;
77     bool mEngineInited = false;
78     DynamicsProcessing::EngineArchitecture mEngineArchitecture = {
79             .resolutionPreference =
80                     DynamicsProcessing::ResolutionPreference::FAVOR_FREQUENCY_RESOLUTION,
81             .preferredProcessingDurationMs = kPreferredProcessingDurationMs,
82             .preEqStage = {.inUse = true, .bandCount = kBandCount},
83             .postEqStage = {.inUse = true, .bandCount = kBandCount},
84             .mbcStage = {.inUse = true, .bandCount = kBandCount},
85             .limiterInUse = true,
86     };
87 
88     enum class StageType { PREEQ, POSTEQ, MBC, LIMITER, INPUTGAIN };
89 
90     void init();
91 
92     void dpSetFreqDomainVariant_l(const DynamicsProcessing::EngineArchitecture& engine);
93     dp_fx::DPChannel* getChannel_l(int ch);
94     dp_fx::DPEq* getPreEq_l(int ch);
95     dp_fx::DPEq* getPostEq_l(int ch);
96     dp_fx::DPMbc* getMbc_l(int ch);
97     dp_fx::DPLimiter* getLimiter_l(int ch);
98     dp_fx::DPBandStage* getStageWithType_l(StageType type, int ch);
99     dp_fx::DPEq* getEqWithType_l(StageType type, int ch);
100     template <typename D>
101     RetCode setDpChannels_l(const std::vector<DynamicsProcessing::ChannelConfig>& channels,
102                             StageType type);
103     template <typename T /* BandConfig */>
104     RetCode setBands_l(const std::vector<T>& bands, StageType type);
105     RetCode setDpChannelBand_l(const std::any& anyConfig, StageType type,
106                                std::set<std::pair<int, int>>& chBandSet);
107 
108     std::vector<DynamicsProcessing::EqBandConfig> getEqBandConfigs(StageType type);
109     std::vector<DynamicsProcessing::ChannelConfig> getChannelConfig(StageType type);
110 
111     template <typename T /* BandConfig */>
112     bool validateBandConfig(const std::vector<T>& bands, int maxChannel, int maxBand);
113     bool validateLimiterConfig(const std::vector<DynamicsProcessing::LimiterConfig>& cfgs,
114                                int maxChannel);
115     bool validateInputGainConfig(const std::vector<DynamicsProcessing::InputGain>& cfgs,
116                                  int maxChannel);
117 
validateChannel(int ch,int maxCh)118     inline bool validateChannel(int ch, int maxCh) { return ch >= 0 && ch < maxCh; }
validateBand(int band,int maxBand)119     inline bool validateBand(int band, int maxBand) { return band >= 0 && band < maxBand; }
120 };
121 
122 }  // namespace aidl::android::hardware::audio::effect