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 <unordered_map>
21 
22 #include "ReverbTypes.h"
23 #include "effect-impl/EffectContext.h"
24 
25 namespace aidl::android::hardware::audio::effect {
26 
27 enum VolumeMode {
28     VOLUME_OFF,
29     VOLUME_FLAT,
30     VOLUME_RAMP,
31 };
32 
33 struct LPFPair {
34     int roomHf;
35     int lpf;
36 };
37 
38 class ReverbContext final : public EffectContext {
39   public:
ReverbContext(int statusDepth,const Parameter::Common & common,const lvm::ReverbEffectType & type)40     ReverbContext(int statusDepth, const Parameter::Common& common,
41                   const lvm::ReverbEffectType& type)
42         : EffectContext(statusDepth, common), mType(type) {
43         LOG(DEBUG) << __func__ << type;
44         init();
45     }
~ReverbContext()46     ~ReverbContext() override {
47         LOG(DEBUG) << __func__;
48         deInit();
49     }
50 
51     RetCode init();
52     void deInit();
53 
54     RetCode enable();
55     RetCode disable();
56 
57     bool isAuxiliary();
58     bool isPreset();
59 
60     RetCode setPresetReverbPreset(const PresetReverb::Presets& preset);
getPresetReverbPreset()61     PresetReverb::Presets getPresetReverbPreset() const { return mNextPreset; }
62 
63     RetCode setEnvironmentalReverbRoomLevel(int roomLevel);
getEnvironmentalReverbRoomLevel()64     int getEnvironmentalReverbRoomLevel() const { return mRoomLevel; }
65     RetCode setEnvironmentalReverbRoomHfLevel(int roomHfLevel);
getEnvironmentalReverbRoomHfLevel()66     int getEnvironmentalReverbRoomHfLevel() const { return mRoomHfLevel; }
67     RetCode setEnvironmentalReverbDecayTime(int decayTime);
getEnvironmentalReverbDecayTime()68     int getEnvironmentalReverbDecayTime() const { return mDecayTime; }
69     RetCode setEnvironmentalReverbDecayHfRatio(int decayHfRatio);
getEnvironmentalReverbDecayHfRatio()70     int getEnvironmentalReverbDecayHfRatio() const { return mDecayHfRatio; }
71     RetCode setEnvironmentalReverbLevel(int level);
getEnvironmentalReverbLevel()72     int getEnvironmentalReverbLevel() const { return mLevel; }
73     RetCode setEnvironmentalReverbDelay(int delay);
getEnvironmentalReverbDelay()74     int getEnvironmentalReverbDelay() const { return mDelay; }
75     RetCode setEnvironmentalReverbDiffusion(int diffusion);
getEnvironmentalReverbDiffusion()76     int getEnvironmentalReverbDiffusion() const { return mDiffusion; }
77     RetCode setEnvironmentalReverbDensity(int density);
getEnvironmentalReverbDensity()78     int getEnvironmentalReverbDensity() const { return mDensity; }
79     RetCode setEnvironmentalReverbBypass(bool bypass);
getEnvironmentalReverbBypass()80     bool getEnvironmentalReverbBypass() const { return mBypass; }
81 
82     RetCode setVolumeStereo(const Parameter::VolumeStereo& volumeStereo) override;
getVolumeStereo()83     Parameter::VolumeStereo getVolumeStereo() override {
84         if (isAuxiliary()) {
85             return mVolumeStereo;
86         }
87         return {1.0f, 1.0f};
88     }
89 
setReflectionsDelay(int delay)90     RetCode setReflectionsDelay(int delay) {
91         mReflectionsDelayMs = delay;
92         return RetCode::SUCCESS;
93     }
getReflectionsDelay()94     bool getReflectionsDelay() const { return mReflectionsDelayMs; }
95 
setReflectionsLevel(int level)96     RetCode setReflectionsLevel(int level) {
97         mReflectionsLevelMb = level;
98         return RetCode::SUCCESS;
99     }
getReflectionsLevel()100     bool getReflectionsLevel() const { return mReflectionsLevelMb; }
101 
102     IEffect::Status process(float* in, float* out, int samples);
103 
104   private:
105     static constexpr inline float kUnitVolume = 1;
106     static constexpr inline float kSendLevel = 0.75f;
107     static constexpr inline int kDefaultLevel = 0;
108     static constexpr inline int kDefaultLPF = 23999;      /* Default low pass filter, in Hz */
109     static constexpr inline int kDefaultHPF = 50;         /* Default high pass filter, in Hz */
110     static constexpr inline int kDefaultDecayTime = 1490; /* Default Decay time, in ms */
111     static constexpr inline int kDefaultDensity = 100;    /* Default Echo density */
112     static constexpr inline int kDefaultDamping = 21;
113     static constexpr inline int kDefaultRoomSize = 100;
114 
115     static inline const std::vector<LPFPair> kLPFMapping = {
116             // Limit range to 50 for LVREV parameter range
117             {-10000, 50}, {-5000, 50},  {-4000, 50},  {-3000, 158}, {-2000, 502}, {-1000, 1666},
118             {-900, 1897}, {-800, 2169}, {-700, 2496}, {-600, 2895}, {-500, 3400}, {-400, 4066},
119             {-300, 5011}, {-200, 6537}, {-100, 9826}, {-99, 9881},  {-98, 9937},  {-97, 9994},
120             {-96, 10052}, {-95, 10111}, {-94, 10171}, {-93, 10231}, {-92, 10293}, {-91, 10356},
121             {-90, 10419}, {-89, 10484}, {-88, 10549}, {-87, 10616}, {-86, 10684}, {-85, 10753},
122             {-84, 10823}, {-83, 10895}, {-82, 10968}, {-81, 11042}, {-80, 11117}, {-79, 11194},
123             {-78, 11272}, {-77, 11352}, {-76, 11433}, {-75, 11516}, {-74, 11600}, {-73, 11686},
124             {-72, 11774}, {-71, 11864}, {-70, 11955}, {-69, 12049}, {-68, 12144}, {-67, 12242},
125             {-66, 12341}, {-65, 12443}, {-64, 12548}, {-63, 12654}, {-62, 12763}, {-61, 12875},
126             {-60, 12990}, {-59, 13107}, {-58, 13227}, {-57, 13351}, {-56, 13477}, {-55, 13607},
127             {-54, 13741}, {-53, 13878}, {-52, 14019}, {-51, 14164}, {-50, 14313}, {-49, 14467},
128             {-48, 14626}, {-47, 14789}, {-46, 14958}, {-45, 15132}, {-44, 15312}, {-43, 15498},
129             {-42, 15691}, {-41, 15890}, {-40, 16097}, {-39, 16311}, {-38, 16534}, {-37, 16766},
130             {-36, 17007}, {-35, 17259}, {-34, 17521}, {-33, 17795}, {-32, 18081}, {-31, 18381},
131             {-30, 18696}, {-29, 19027}, {-28, 19375}, {-27, 19742}, {-26, 20129}, {-25, 20540},
132             {-24, 20976}, {-23, 21439}, {-22, 21934}, {-21, 22463}, {-20, 23031}, {-19, 23643},
133             {-18, 23999}};
134 
135     static inline const std::vector<int> kLevelMapping = {
136             -12000, -4000, -3398, -3046, -2796, -2603, -2444, -2310, -2194, -2092, -2000, -1918,
137             -1842,  -1773, -1708, -1648, -1592, -1540, -1490, -1443, -1398, -1356, -1316, -1277,
138             -1240,  -1205, -1171, -1138, -1106, -1076, -1046, -1018, -990,  -963,  -938,  -912,
139             -888,   -864,  -841,  -818,  -796,  -775,  -754,  -734,  -714,  -694,  -675,  -656,
140             -638,   -620,  -603,  -585,  -568,  -552,  -536,  -520,  -504,  -489,  -474,  -459,
141             -444,   -430,  -416,  -402,  -388,  -375,  -361,  -348,  -335,  -323,  -310,  -298,
142             -286,   -274,  -262,  -250,  -239,  -228,  -216,  -205,  -194,  -184,  -173,  -162,
143             -152,   -142,  -132,  -121,  -112,  -102,  -92,   -82,   -73,   -64,   -54,   -45,
144             -36,    -27,   -18,   -9,    0};
145 
146     static inline std::unordered_map<PresetReverb::Presets, t_reverb_settings> mReverbPresets = {
147             {PresetReverb::Presets::NONE, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
148             {PresetReverb::Presets::SMALLROOM,
149              {-400, -600, 1100, 830, -400, 5, 500, 10, 1000, 1000}},
150             {PresetReverb::Presets::MEDIUMROOM,
151              {-400, -600, 1300, 830, -1000, 20, -200, 20, 1000, 1000}},
152             {PresetReverb::Presets::LARGEROOM,
153              {-400, -600, 1500, 830, -1600, 5, -1000, 40, 1000, 1000}},
154             {PresetReverb::Presets::MEDIUMHALL,
155              {-400, -600, 1800, 700, -1300, 15, -800, 30, 1000, 1000}},
156             {PresetReverb::Presets::LARGEHALL,
157              {-400, -600, 1800, 700, -2000, 30, -1400, 60, 1000, 1000}},
158             {PresetReverb::Presets::PLATE, {-400, -200, 1300, 900, 0, 2, 0, 10, 1000, 750}}};
159 
160     const lvm::ReverbEffectType mType;
161     bool mEnabled = false;
162     LVREV_Handle_t mInstance = LVM_NULL;
163 
164     int mRoomLevel = 0;
165     int mRoomHfLevel = 0;
166     int mDecayTime = 0;
167     int mDecayHfRatio = 0;
168     int mLevel = 0;
169     int mDelay = 0;
170     int mDiffusion = 0;
171     int mDensity = 0;
172     bool mBypass = 0;
173     int mReflectionsLevelMb = 0;
174     int mReflectionsDelayMs = 0;
175 
176     PresetReverb::Presets mPreset;
177     PresetReverb::Presets mNextPreset;
178 
179     int mSamplesToExitCount;
180 
181     Parameter::VolumeStereo mVolume;
182     Parameter::VolumeStereo mPrevVolume;
183     VolumeMode volumeMode;
184 
185     void initControlParameter(LVREV_ControlParams_st& params);
186     int16_t convertHfLevel(int hfLevel);
187     int convertLevel(int level);
188     void loadPreset();
189 };
190 
191 }  // namespace aidl::android::hardware::audio::effect
192