blob: 839c6dd88114e1c409aefeef86b8dffc626b57ec [file] [log] [blame]
Shunkai Yao725af212023-01-05 23:01:40 +00001/*
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 <android-base/thread_annotations.h>
20#include <audio_effects/effect_dynamicsprocessing.h>
21
22#include "effect-impl/EffectContext.h"
23
24#include <any>
25#include <cstddef>
26#include <dsp/DPBase.h>
27#include <dsp/DPFrequency.h>
28
29namespace aidl::android::hardware::audio::effect {
30
31enum DynamicsProcessingState {
32 DYNAMICS_PROCESSING_STATE_UNINITIALIZED,
33 DYNAMICS_PROCESSING_STATE_INITIALIZED,
34 DYNAMICS_PROCESSING_STATE_ACTIVE,
35};
36
37class DynamicsProcessingContext final : public EffectContext {
38 public:
39 DynamicsProcessingContext(int statusDepth, const Parameter::Common& common);
40 ~DynamicsProcessingContext();
41
42 RetCode enable();
43 RetCode disable();
44 void reset();
45
46 // override EffectContext::setCommon to update mChannelCount
47 RetCode setCommon(const Parameter::Common& common) override;
David Licb5bcc42023-11-13 16:01:48 +080048 RetCode setVolumeStereo(const Parameter::VolumeStereo& volumeStereo) override;
49 Parameter::VolumeStereo getVolumeStereo() override;
Shunkai Yao725af212023-01-05 23:01:40 +000050
51 RetCode setEngineArchitecture(const DynamicsProcessing::EngineArchitecture& engineArchitecture);
52 RetCode setPreEq(const std::vector<DynamicsProcessing::ChannelConfig>& eqChannels);
53 RetCode setPostEq(const std::vector<DynamicsProcessing::ChannelConfig>& eqChannels);
54 RetCode setPreEqBand(const std::vector<DynamicsProcessing::EqBandConfig>& eqBands);
55 RetCode setPostEqBand(const std::vector<DynamicsProcessing::EqBandConfig>& eqBands);
56 RetCode setMbc(const std::vector<DynamicsProcessing::ChannelConfig>& mbcChannels);
57 RetCode setMbcBand(const std::vector<DynamicsProcessing::MbcBandConfig>& eqBands);
58 RetCode setLimiter(const std::vector<DynamicsProcessing::LimiterConfig>& limiters);
59 RetCode setInputGain(const std::vector<DynamicsProcessing::InputGain>& gain);
60
61 DynamicsProcessing::EngineArchitecture getEngineArchitecture();
62 std::vector<DynamicsProcessing::ChannelConfig> getPreEq();
63 std::vector<DynamicsProcessing::ChannelConfig> getPostEq();
64 std::vector<DynamicsProcessing::EqBandConfig> getPreEqBand();
65 std::vector<DynamicsProcessing::EqBandConfig> getPostEqBand();
66 std::vector<DynamicsProcessing::ChannelConfig> getMbc();
67 std::vector<DynamicsProcessing::MbcBandConfig> getMbcBand();
68 std::vector<DynamicsProcessing::LimiterConfig> getLimiter();
69 std::vector<DynamicsProcessing::InputGain> getInputGain();
70
David Licb5bcc42023-11-13 16:01:48 +080071 IEffect::Status dpeProcess(float* in, float* out, int samples);
Shunkai Yao725af212023-01-05 23:01:40 +000072
73 private:
74 static constexpr float kPreferredProcessingDurationMs = 10.0f;
75 static constexpr int kBandCount = 5;
76 std::mutex mMutex;
Shunkai Yaocbe90d02023-12-14 02:35:55 +000077 int mChannelCount GUARDED_BY(mMutex) = 0;
Shunkai Yao725af212023-01-05 23:01:40 +000078 DynamicsProcessingState mState GUARDED_BY(mMutex) = DYNAMICS_PROCESSING_STATE_UNINITIALIZED;
79 std::unique_ptr<dp_fx::DPFrequency> mDpFreq GUARDED_BY(mMutex) = nullptr;
80 bool mEngineInited GUARDED_BY(mMutex) = false;
81 DynamicsProcessing::EngineArchitecture mEngineArchitecture GUARDED_BY(mMutex) = {
82 .resolutionPreference =
83 DynamicsProcessing::ResolutionPreference::FAVOR_FREQUENCY_RESOLUTION,
84 .preferredProcessingDurationMs = kPreferredProcessingDurationMs,
85 .preEqStage = {.inUse = true, .bandCount = kBandCount},
86 .postEqStage = {.inUse = true, .bandCount = kBandCount},
87 .mbcStage = {.inUse = true, .bandCount = kBandCount},
88 .limiterInUse = true,
89 };
90
91 enum class StageType { PREEQ, POSTEQ, MBC, LIMITER, INPUTGAIN };
92
93 void init();
94
95 void dpSetFreqDomainVariant_l(const DynamicsProcessing::EngineArchitecture& engine)
96 REQUIRES(mMutex);
97 dp_fx::DPChannel* getChannel_l(int ch) REQUIRES(mMutex);
98 dp_fx::DPEq* getPreEq_l(int ch) REQUIRES(mMutex);
99 dp_fx::DPEq* getPostEq_l(int ch) REQUIRES(mMutex);
100 dp_fx::DPMbc* getMbc_l(int ch) REQUIRES(mMutex);
101 dp_fx::DPLimiter* getLimiter_l(int ch) REQUIRES(mMutex);
102 dp_fx::DPBandStage* getStageWithType_l(StageType type, int ch) REQUIRES(mMutex);
103 dp_fx::DPEq* getEqWithType_l(StageType type, int ch) REQUIRES(mMutex);
104 template <typename D>
105 RetCode setDpChannels_l(const std::vector<DynamicsProcessing::ChannelConfig>& channels,
106 bool stageInUse, StageType type) REQUIRES(mMutex);
107 template <typename T /* BandConfig */>
Ram Mohanc89817d2023-03-14 21:39:26 +0530108 RetCode setBands_l(const std::vector<T>& bands, StageType type) REQUIRES(mMutex);
109 RetCode setDpChannelBand_l(const std::any& anyConfig, StageType type,
Shunkai Yao725af212023-01-05 23:01:40 +0000110 std::set<std::pair<int, int>>& chBandSet) REQUIRES(mMutex);
111
112 std::vector<DynamicsProcessing::EqBandConfig> getEqBandConfigs(StageType type);
113 std::vector<DynamicsProcessing::ChannelConfig> getChannelConfig(StageType type);
114
Ram Mohanc89817d2023-03-14 21:39:26 +0530115 template <typename T /* BandConfig */>
116 bool validateBandConfig(const std::vector<T>& bands, int maxChannel, int maxBand);
117 bool validateLimiterConfig(const std::vector<DynamicsProcessing::LimiterConfig>& cfgs,
118 int maxChannel);
119 bool validateInputGainConfig(const std::vector<DynamicsProcessing::InputGain>& cfgs,
120 int maxChannel);
Shunkai Yao725af212023-01-05 23:01:40 +0000121
Shunkai Yao725af212023-01-05 23:01:40 +0000122 inline bool validateChannel(int ch, int maxCh) { return ch >= 0 && ch < maxCh; }
123 inline bool validateBand(int band, int maxBand) { return band >= 0 && band < maxBand; }
Shunkai Yao725af212023-01-05 23:01:40 +0000124};
125
126} // namespace aidl::android::hardware::audio::effect