• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/logging.h>
20 #include <audio_processing.h>
21 #include <unordered_map>
22 
23 #include "PreProcessingTypes.h"
24 #include "effect-impl/EffectContext.h"
25 
26 namespace aidl::android::hardware::audio::effect {
27 
28 enum PreProcEffectState {
29     PRE_PROC_STATE_UNINITIALIZED,
30     PRE_PROC_STATE_INITIALIZED,
31     PRE_PROC_STATE_ACTIVE,
32 };
33 
34 class PreProcessingContext final : public EffectContext {
35   public:
PreProcessingContext(int statusDepth,const Parameter::Common & common,const PreProcessingEffectType & type)36     PreProcessingContext(int statusDepth, const Parameter::Common& common,
37                          const PreProcessingEffectType& type)
38         : EffectContext(statusDepth, common), mType(type) {
39         mState = PRE_PROC_STATE_UNINITIALIZED;
40     }
41     ~PreProcessingContext() = default;
42 
43     RetCode init(const Parameter::Common& common);
44     RetCode deInit();
45 
getPreProcessingType()46     PreProcessingEffectType getPreProcessingType() const { return mType; }
47 
48     RetCode enable();
49     RetCode disable();
50 
51     RetCode setCommon(const Parameter::Common& common) override;
52     void updateConfigs(const Parameter::Common& common);
53 
54     RetCode setAcousticEchoCancelerEchoDelay(int echoDelayUs);
55     int getAcousticEchoCancelerEchoDelay() const;
56     RetCode setAcousticEchoCancelerMobileMode(bool mobileMode);
57     bool getAcousticEchoCancelerMobileMode() const;
58 
59     RetCode setAutomaticGainControlV1TargetPeakLevel(int targetPeakLevel);
60     int getAutomaticGainControlV1TargetPeakLevel() const;
61     RetCode setAutomaticGainControlV1MaxCompressionGain(int maxCompressionGain);
62     int getAutomaticGainControlV1MaxCompressionGain() const;
63     RetCode setAutomaticGainControlV1EnableLimiter(bool enableLimiter);
64     bool getAutomaticGainControlV1EnableLimiter() const;
65 
66     RetCode setAutomaticGainControlV2DigitalGain(int gain);
67     int getAutomaticGainControlV2DigitalGain() const;
68     RetCode setAutomaticGainControlV2LevelEstimator(
69             AutomaticGainControlV2::LevelEstimator levelEstimator);
70     AutomaticGainControlV2::LevelEstimator getAutomaticGainControlV2LevelEstimator() const;
71     RetCode setAutomaticGainControlV2SaturationMargin(int saturationMargin);
72     int getAutomaticGainControlV2SaturationMargin() const;
73 
74     RetCode setNoiseSuppressionLevel(NoiseSuppression::Level level);
75     NoiseSuppression::Level getNoiseSuppressionLevel() const;
76 
77     IEffect::Status process(float* in, float* out, int samples);
78 
79   private:
80     static constexpr inline int kAgcDefaultTargetLevel = 3;
81     static constexpr inline int kAgcDefaultCompGain = 9;
82     static constexpr inline bool kAgcDefaultLimiter = true;
83     static constexpr inline webrtc::AudioProcessing::Config::NoiseSuppression::Level
84             kNsDefaultLevel = webrtc::AudioProcessing::Config::NoiseSuppression::kModerate;
85 
86     const PreProcessingEffectType mType;
87     PreProcEffectState mState;  // current state
88 
89     // handle on webRTC audio processing module (APM)
90     rtc::scoped_refptr<webrtc::AudioProcessing> mAudioProcessingModule;
91 
92     int mEnabledMsk;       // bit field containing IDs of enabled pre processors
93     int mProcessedMsk;     // bit field containing IDs of pre processors already
94                                               // processed in current round
95     int mRevEnabledMsk;    // bit field containing IDs of enabled pre processors
96                                               // with reverse channel
97     int mRevProcessedMsk;  // bit field containing IDs of pre processors with
98                            // reverse channel already processed in current round
99 
100     webrtc::StreamConfig mInputConfig;   // input stream configuration
101     webrtc::StreamConfig mOutputConfig;  // output stream configuration
102 
103     // Acoustic Echo Canceler
104     int mEchoDelayUs = 0;
105     bool mMobileMode = false;
106 
107     // Automatic Gain Control V1
108     int mTargetPeakLevel = 0;
109     int mMaxCompressionGain = 0;
110     bool mEnableLimiter = false;
111 
112     // Automatic Gain Control V2
113     int mDigitalGain = 0;
114     AutomaticGainControlV2::LevelEstimator mLevelEstimator =
115             AutomaticGainControlV2::LevelEstimator::RMS;
116     int mSaturationMargin = 2;
117 
118     // NoiseSuppression
119     NoiseSuppression::Level mLevel = NoiseSuppression::Level::LOW;
120 };
121 
122 }  // namespace aidl::android::hardware::audio::effect
123