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