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 #include <system/audio_effects/effect_uuid.h>
21
22 #include "AutomaticGainControlV1Sw.h"
23
24 using aidl::android::hardware::audio::effect::AutomaticGainControlV1Sw;
25 using aidl::android::hardware::audio::effect::Descriptor;
26 using aidl::android::hardware::audio::effect::getEffectImplUuidAutomaticGainControlV1Sw;
27 using aidl::android::hardware::audio::effect::getEffectTypeUuidAutomaticGainControlV1;
28 using aidl::android::hardware::audio::effect::IEffect;
29 using aidl::android::media::audio::common::AudioUuid;
30
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)31 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
32 std::shared_ptr<IEffect>* instanceSpp) {
33 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidAutomaticGainControlV1Sw()) {
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
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)47 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
48 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidAutomaticGainControlV1Sw()) {
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 = {
68 .common = {.id = {.type = getEffectTypeUuidAutomaticGainControlV1(),
69 .uuid = getEffectImplUuidAutomaticGainControlV1Sw(),
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
getDescriptor(Descriptor * _aidl_return)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
setParameterSpecific(const Parameter::Specific & specific)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
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)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
getParameterAutomaticGainControlV1(const AutomaticGainControlV1::Tag & tag,Parameter::Specific * specific)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
createContext(const Parameter::Common & common)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
releaseContext()180 RetCode AutomaticGainControlV1Sw::releaseContext() {
181 if (mContext) {
182 mContext.reset();
183 }
184 return RetCode::SUCCESS;
185 }
186
187 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)188 IEffect::Status AutomaticGainControlV1Sw::effectProcessImpl(float* in, float* out, int samples) {
189 // TODO: get data buffer and process.
190 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
191 for (int i = 0; i < samples; i++) {
192 *out++ = *in++;
193 }
194 return {STATUS_OK, samples, samples};
195 }
196
setTargetPeakLevel(int targetPeakLevel)197 RetCode AutomaticGainControlV1SwContext::setTargetPeakLevel(int targetPeakLevel) {
198 mTargetPeakLevel = targetPeakLevel;
199 return RetCode::SUCCESS;
200 }
201
getTargetPeakLevel()202 int AutomaticGainControlV1SwContext::getTargetPeakLevel() {
203 return mTargetPeakLevel;
204 }
205
setMaxCompressionGain(int maxCompressionGain)206 RetCode AutomaticGainControlV1SwContext::setMaxCompressionGain(int maxCompressionGain) {
207 mMaxCompressionGain = maxCompressionGain;
208 return RetCode::SUCCESS;
209 }
210
getMaxCompressionGain()211 int AutomaticGainControlV1SwContext::getMaxCompressionGain() {
212 return mMaxCompressionGain;
213 }
214
setEnableLimiter(bool enableLimiter)215 RetCode AutomaticGainControlV1SwContext::setEnableLimiter(bool enableLimiter) {
216 mEnableLimiter = enableLimiter;
217 return RetCode::SUCCESS;
218 }
219
getEnableLimiter()220 bool AutomaticGainControlV1SwContext::getEnableLimiter() {
221 return mEnableLimiter;
222 }
223
224 } // namespace aidl::android::hardware::audio::effect
225