Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame^] | 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 | #define LOG_TAG "AHAL_AutomaticGainControlV1Sw" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include "AutomaticGainControlV1Sw.h" |
| 22 | |
| 23 | using aidl::android::hardware::audio::effect::AutomaticGainControlV1Sw; |
| 24 | using aidl::android::hardware::audio::effect::Descriptor; |
| 25 | using aidl::android::hardware::audio::effect::IEffect; |
| 26 | using aidl::android::hardware::audio::effect::kAutomaticGainControlV1SwImplUUID; |
| 27 | using aidl::android::media::audio::common::AudioUuid; |
| 28 | |
| 29 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 30 | std::shared_ptr<IEffect>* instanceSpp) { |
| 31 | if (!in_impl_uuid || *in_impl_uuid != kAutomaticGainControlV1SwImplUUID) { |
| 32 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 33 | return EX_ILLEGAL_ARGUMENT; |
| 34 | } |
| 35 | if (instanceSpp) { |
| 36 | *instanceSpp = ndk::SharedRefBase::make<AutomaticGainControlV1Sw>(); |
| 37 | LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created"; |
| 38 | return EX_NONE; |
| 39 | } else { |
| 40 | LOG(ERROR) << __func__ << " invalid input parameter!"; |
| 41 | return EX_ILLEGAL_ARGUMENT; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
| 46 | if (!in_impl_uuid || *in_impl_uuid != kAutomaticGainControlV1SwImplUUID) { |
| 47 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 48 | return EX_ILLEGAL_ARGUMENT; |
| 49 | } |
| 50 | *_aidl_return = AutomaticGainControlV1Sw::kDescriptor; |
| 51 | return EX_NONE; |
| 52 | } |
| 53 | |
| 54 | namespace aidl::android::hardware::audio::effect { |
| 55 | |
| 56 | const std::string AutomaticGainControlV1Sw::kEffectName = "AutomaticGainControlV1Sw"; |
| 57 | |
| 58 | const std::vector<Range::AutomaticGainControlV1Range> AutomaticGainControlV1Sw::kRanges = { |
| 59 | MAKE_RANGE(AutomaticGainControlV1, targetPeakLevelDbFs, -3100, 0), |
| 60 | MAKE_RANGE(AutomaticGainControlV1, maxCompressionGainDb, 0, 9000)}; |
| 61 | |
| 62 | const Capability AutomaticGainControlV1Sw::kCapability = { |
| 63 | .range = AutomaticGainControlV1Sw::kRanges}; |
| 64 | |
| 65 | const Descriptor AutomaticGainControlV1Sw::kDescriptor = { |
| 66 | .common = {.id = {.type = kAutomaticGainControlV1TypeUUID, |
| 67 | .uuid = kAutomaticGainControlV1SwImplUUID, |
| 68 | .proxy = std::nullopt}, |
| 69 | .flags = {.type = Flags::Type::INSERT, |
| 70 | .insert = Flags::Insert::FIRST, |
| 71 | .volume = Flags::Volume::CTRL}, |
| 72 | .name = AutomaticGainControlV1Sw::kEffectName, |
| 73 | .implementor = "The Android Open Source Project"}, |
| 74 | .capability = AutomaticGainControlV1Sw::kCapability}; |
| 75 | |
| 76 | ndk::ScopedAStatus AutomaticGainControlV1Sw::getDescriptor(Descriptor* _aidl_return) { |
| 77 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 78 | *_aidl_return = kDescriptor; |
| 79 | return ndk::ScopedAStatus::ok(); |
| 80 | } |
| 81 | |
| 82 | ndk::ScopedAStatus AutomaticGainControlV1Sw::setParameterSpecific( |
| 83 | const Parameter::Specific& specific) { |
| 84 | RETURN_IF(Parameter::Specific::automaticGainControlV1 != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 85 | "EffectNotSupported"); |
| 86 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 87 | |
| 88 | auto& param = specific.get<Parameter::Specific::automaticGainControlV1>(); |
| 89 | RETURN_IF(!inRange(param, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange"); |
| 90 | auto tag = param.getTag(); |
| 91 | switch (tag) { |
| 92 | case AutomaticGainControlV1::targetPeakLevelDbFs: { |
| 93 | RETURN_IF(mContext->setTargetPeakLevel( |
| 94 | param.get<AutomaticGainControlV1::targetPeakLevelDbFs>()) != |
| 95 | RetCode::SUCCESS, |
| 96 | EX_ILLEGAL_ARGUMENT, "targetPeakLevelNotSupported"); |
| 97 | return ndk::ScopedAStatus::ok(); |
| 98 | } |
| 99 | case AutomaticGainControlV1::maxCompressionGainDb: { |
| 100 | RETURN_IF(mContext->setMaxCompressionGain( |
| 101 | param.get<AutomaticGainControlV1::maxCompressionGainDb>()) != |
| 102 | RetCode::SUCCESS, |
| 103 | EX_ILLEGAL_ARGUMENT, "maxCompressionGainNotSupported"); |
| 104 | return ndk::ScopedAStatus::ok(); |
| 105 | } |
| 106 | case AutomaticGainControlV1::enableLimiter: { |
| 107 | RETURN_IF( |
| 108 | mContext->setEnableLimiter( |
| 109 | param.get<AutomaticGainControlV1::enableLimiter>()) != RetCode::SUCCESS, |
| 110 | EX_ILLEGAL_ARGUMENT, "enableLimiterNotSupported"); |
| 111 | return ndk::ScopedAStatus::ok(); |
| 112 | } |
| 113 | default: { |
| 114 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 115 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 116 | EX_ILLEGAL_ARGUMENT, "AutomaticGainControlV1TagNotSupported"); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | ndk::ScopedAStatus AutomaticGainControlV1Sw::getParameterSpecific(const Parameter::Id& id, |
| 122 | Parameter::Specific* specific) { |
| 123 | auto tag = id.getTag(); |
| 124 | RETURN_IF(Parameter::Id::automaticGainControlV1Tag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 125 | auto specificId = id.get<Parameter::Id::automaticGainControlV1Tag>(); |
| 126 | auto specificIdTag = specificId.getTag(); |
| 127 | switch (specificIdTag) { |
| 128 | case AutomaticGainControlV1::Id::commonTag: |
| 129 | return getParameterAutomaticGainControlV1( |
| 130 | specificId.get<AutomaticGainControlV1::Id::commonTag>(), specific); |
| 131 | default: |
| 132 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 133 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 134 | EX_ILLEGAL_ARGUMENT, "AutomaticGainControlV1TagNotSupported"); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | ndk::ScopedAStatus AutomaticGainControlV1Sw::getParameterAutomaticGainControlV1( |
| 139 | const AutomaticGainControlV1::Tag& tag, Parameter::Specific* specific) { |
| 140 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 141 | AutomaticGainControlV1 param; |
| 142 | switch (tag) { |
| 143 | case AutomaticGainControlV1::targetPeakLevelDbFs: { |
| 144 | param.set<AutomaticGainControlV1::targetPeakLevelDbFs>(mContext->getTargetPeakLevel()); |
| 145 | break; |
| 146 | } |
| 147 | case AutomaticGainControlV1::maxCompressionGainDb: { |
| 148 | param.set<AutomaticGainControlV1::maxCompressionGainDb>( |
| 149 | mContext->getMaxCompressionGain()); |
| 150 | break; |
| 151 | } |
| 152 | case AutomaticGainControlV1::enableLimiter: { |
| 153 | param.set<AutomaticGainControlV1::enableLimiter>(mContext->getEnableLimiter()); |
| 154 | break; |
| 155 | } |
| 156 | default: { |
| 157 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 158 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 159 | EX_ILLEGAL_ARGUMENT, "AutomaticGainControlV1TagNotSupported"); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | specific->set<Parameter::Specific::automaticGainControlV1>(param); |
| 164 | return ndk::ScopedAStatus::ok(); |
| 165 | } |
| 166 | |
| 167 | std::shared_ptr<EffectContext> AutomaticGainControlV1Sw::createContext( |
| 168 | const Parameter::Common& common) { |
| 169 | if (mContext) { |
| 170 | LOG(DEBUG) << __func__ << " context already exist"; |
| 171 | } else { |
| 172 | mContext = |
| 173 | std::make_shared<AutomaticGainControlV1SwContext>(1 /* statusFmqDepth */, common); |
| 174 | } |
| 175 | return mContext; |
| 176 | } |
| 177 | |
| 178 | std::shared_ptr<EffectContext> AutomaticGainControlV1Sw::getContext() { |
| 179 | return mContext; |
| 180 | } |
| 181 | |
| 182 | RetCode AutomaticGainControlV1Sw::releaseContext() { |
| 183 | if (mContext) { |
| 184 | mContext.reset(); |
| 185 | } |
| 186 | return RetCode::SUCCESS; |
| 187 | } |
| 188 | |
| 189 | // Processing method running in EffectWorker thread. |
| 190 | IEffect::Status AutomaticGainControlV1Sw::effectProcessImpl(float* in, float* out, int samples) { |
| 191 | // TODO: get data buffer and process. |
| 192 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 193 | for (int i = 0; i < samples; i++) { |
| 194 | *out++ = *in++; |
| 195 | } |
| 196 | return {STATUS_OK, samples, samples}; |
| 197 | } |
| 198 | |
| 199 | RetCode AutomaticGainControlV1SwContext::setTargetPeakLevel(int targetPeakLevel) { |
| 200 | mTargetPeakLevel = targetPeakLevel; |
| 201 | return RetCode::SUCCESS; |
| 202 | } |
| 203 | |
| 204 | int AutomaticGainControlV1SwContext::getTargetPeakLevel() { |
| 205 | return mTargetPeakLevel; |
| 206 | } |
| 207 | |
| 208 | RetCode AutomaticGainControlV1SwContext::setMaxCompressionGain(int maxCompressionGain) { |
| 209 | mMaxCompressionGain = maxCompressionGain; |
| 210 | return RetCode::SUCCESS; |
| 211 | } |
| 212 | |
| 213 | int AutomaticGainControlV1SwContext::getMaxCompressionGain() { |
| 214 | return mMaxCompressionGain; |
| 215 | } |
| 216 | |
| 217 | RetCode AutomaticGainControlV1SwContext::setEnableLimiter(bool enableLimiter) { |
| 218 | mEnableLimiter = enableLimiter; |
| 219 | return RetCode::SUCCESS; |
| 220 | } |
| 221 | |
| 222 | bool AutomaticGainControlV1SwContext::getEnableLimiter() { |
| 223 | return mEnableLimiter; |
| 224 | } |
| 225 | |
| 226 | } // namespace aidl::android::hardware::audio::effect |