xref: /aosp_15_r20/frameworks/av/media/libeffects/lvm/wrapper/Aidl/BundleContext.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2022 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 <array>
21 #include <cstddef>
22 
23 #include "BundleTypes.h"
24 #include "effect-impl/EffectContext.h"
25 
26 namespace aidl::android::hardware::audio::effect {
27 
28 class BundleContext final : public EffectContext {
29   public:
30     BundleContext(int statusDepth, const Parameter::Common& common,
31                   const lvm::BundleEffectType& type);
32     ~BundleContext();
33 
34     RetCode init();
35     void deInit();
getBundleType()36     lvm::BundleEffectType getBundleType() const { return mType; }
37 
38     RetCode setCommon(const Parameter::Common& common) override;
39 
40     RetCode enable() override;
41     RetCode enableOperatingMode();
42     RetCode disable() override;
43     RetCode disableOperatingMode();
44 
45     bool isDeviceSupportedBassBoost(
46             const std::vector<aidl::android::media::audio::common::AudioDeviceDescription>&
47                     devices);
48     bool isDeviceSupportedVirtualizer(
49             const std::vector<aidl::android::media::audio::common::AudioDeviceDescription>&
50                     devices);
51     bool isConfigSupportedVirtualizer(
52             size_t channelCount,
53             const aidl::android::media::audio::common::AudioDeviceDescription& device);
54 
55     RetCode setOutputDevice(
56             const std::vector<aidl::android::media::audio::common::AudioDeviceDescription>& devices)
57             override;
58 
59     RetCode setEqualizerPreset(const std::size_t presetIdx);
getEqualizerPreset()60     int getEqualizerPreset() const { return mCurPresetIdx; }
61     RetCode setEqualizerBandLevels(const std::vector<Equalizer::BandLevel>& bandLevels);
62     std::vector<Equalizer::BandLevel> getEqualizerBandLevels() const;
63 
64     std::vector<int32_t> getEqualizerCenterFreqs();
65 
66     RetCode setBassBoostStrength(int strength);
getBassBoostStrength()67     int getBassBoostStrength() const { return mBassStrengthSaved; }
68 
69     RetCode setVolumeLevel(float level);
70     float getVolumeLevel() const;
71 
72     RetCode setVolumeMute(bool mute);
getVolumeMute()73     int getVolumeMute() const { return mMuteEnabled; }
74 
75     RetCode setVirtualizerStrength(int strength);
getVirtualizerStrength()76     int getVirtualizerStrength() const { return mVirtStrengthSaved; }
77 
78     RetCode setForcedDevice(
79             const ::aidl::android::media::audio::common::AudioDeviceDescription& device);
getForcedDevice()80     aidl::android::media::audio::common::AudioDeviceDescription getForcedDevice() const {
81         return mVirtualizerForcedDevice;
82     }
83     std::vector<Virtualizer::ChannelAngle> getSpeakerAngles(
84             const Virtualizer::SpeakerAnglesPayload payload);
85 
86     RetCode setVolumeStereo(const Parameter::VolumeStereo& volumeStereo) override;
getVolumeStereo()87     Parameter::VolumeStereo getVolumeStereo() override { return {1.0f, 1.0f}; }
88 
89     IEffect::Status process(float* in, float* out, int samples);
90 
91     IEffect::Status processEffect(float* in, float* out, int sampleToProcess);
92 
93   private:
94     const lvm::BundleEffectType mType;
95     bool mEnabled = false;
96     LVM_Handle_t mInstance;
97 
98     int mSamplesPerSecond = 0;
99     int mSamplesToExitCountEq = 0;
100     int mSamplesToExitCountBb = 0;
101     int mSamplesToExitCountVirt = 0;
102     int mFrameCount = 0;
103 
104     /* Bitmask whether drain is in progress due to disabling the effect.
105        The corresponding bit to an effect is set by 1 << lvm_effect_en. */
106     int mEffectInDrain = 0;
107 
108     /* Bitmask whether process() was called for a particular effect.
109        The corresponding bit to an effect is set by 1 << lvm_effect_en. */
110     int mEffectProcessCalled = 0;
111     int mNumberEffectsEnabled = 0;
112     int mNumberEffectsCalled = 0;
113     bool mFirstVolume = true;
114     // Bass
115     bool mBassTempDisabled = false;
116     int mBassStrengthSaved = 0;
117     // Equalizer
118     int mCurPresetIdx = lvm::PRESET_CUSTOM; /* Current preset being used */
119     std::array<int, lvm::MAX_NUM_BANDS> mBandGainmB; /* band gain in millibels */
120     // Virtualizer
121     int mVirtStrengthSaved = 0; /* Conversion between Get/Set */
122     bool mVirtualizerTempDisabled = false;
123     ::aidl::android::media::audio::common::AudioDeviceDescription mVirtualizerForcedDevice;
124     // Volume
125     float mLevelSaveddB = 0; /* for when mute is set, level must be saved */
126     float mVolumedB = 0;
127     bool mMuteEnabled = false; /* Must store as mute = -96dB level */
128 
129     RetCode initControlParameter(LVM_ControlParams_t& params) const;
130     void initHeadroomParameter(LVM_HeadroomParams_t& params) const;
131     RetCode limitLevel();
132     static float VolToDb(float vol);
133     LVM_INT16 LVC_ToDB_s32Tos16(LVM_INT32 Lin_fix) const;
134     RetCode updateControlParameter(const std::vector<Equalizer::BandLevel>& bandLevels);
135     bool isBandLevelIndexInRange(const std::vector<Equalizer::BandLevel>& bandLevels) const;
136     static LVM_EQNB_BandDef_t* getDefaultEqualizerBandDefs();
137     static LVM_HeadroomBandDef_t* getDefaultEqualizerHeadroomBanDefs();
138     RetCode applyCommonParameter(LVM_ControlParams_t& params) const;
139 };
140 
141 }  // namespace aidl::android::hardware::audio::effect
142 
143