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