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