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 #define LOG_TAG "AHAL_LoudnessEnhancerImpl"
18
19 #include <android-base/logging.h>
20 #include <system/audio_effects/effect_uuid.h>
21
22 #include "EffectLoudnessEnhancer.h"
23
24 using aidl::android::hardware::audio::common::getChannelCount;
25 using aidl::android::hardware::audio::effect::Descriptor;
26 using aidl::android::hardware::audio::effect::getEffectImplUuidLoudnessEnhancer;
27 using aidl::android::hardware::audio::effect::getEffectTypeUuidLoudnessEnhancer;
28 using aidl::android::hardware::audio::effect::IEffect;
29 using aidl::android::hardware::audio::effect::LoudnessEnhancerImpl;
30 using aidl::android::hardware::audio::effect::State;
31 using aidl::android::media::audio::common::AudioUuid;
32
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)33 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
34 std::shared_ptr<IEffect>* instanceSpp) {
35 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidLoudnessEnhancer()) {
36 LOG(ERROR) << __func__ << "uuid not supported";
37 return EX_ILLEGAL_ARGUMENT;
38 }
39 if (instanceSpp) {
40 *instanceSpp = ndk::SharedRefBase::make<LoudnessEnhancerImpl>();
41 return EX_NONE;
42 } else {
43 LOG(ERROR) << __func__ << " invalid input parameter!";
44 return EX_ILLEGAL_ARGUMENT;
45 }
46 }
47
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)48 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
49 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidLoudnessEnhancer()) {
50 LOG(ERROR) << __func__ << "uuid not supported";
51 return EX_ILLEGAL_ARGUMENT;
52 }
53 *_aidl_return = LoudnessEnhancerImpl::kDescriptor;
54 return EX_NONE;
55 }
56
57 namespace aidl::android::hardware::audio::effect {
58
59 const std::string LoudnessEnhancerImpl::kEffectName = "Loudness Enhancer";
60 const Descriptor LoudnessEnhancerImpl::kDescriptor = {
61 .common = {.id = {.type = getEffectTypeUuidLoudnessEnhancer(),
62 .uuid = getEffectImplUuidLoudnessEnhancer(),
63 .proxy = std::nullopt},
64 .flags = {.type = Flags::Type::INSERT, .insert = Flags::Insert::FIRST},
65 .name = LoudnessEnhancerImpl::kEffectName,
66 .implementor = "The Android Open Source Project"}};
67
getDescriptor(Descriptor * _aidl_return)68 ndk::ScopedAStatus LoudnessEnhancerImpl::getDescriptor(Descriptor* _aidl_return) {
69 RETURN_IF(!_aidl_return, EX_ILLEGAL_ARGUMENT, "Parameter:nullptr");
70 *_aidl_return = kDescriptor;
71 return ndk::ScopedAStatus::ok();
72 }
73
setParameterSpecific(const Parameter::Specific & specific)74 ndk::ScopedAStatus LoudnessEnhancerImpl::setParameterSpecific(const Parameter::Specific& specific) {
75 RETURN_IF(Parameter::Specific::loudnessEnhancer != specific.getTag(), EX_ILLEGAL_ARGUMENT,
76 "EffectNotSupported");
77 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
78
79 auto& leParam = specific.get<Parameter::Specific::loudnessEnhancer>();
80 auto tag = leParam.getTag();
81
82 switch (tag) {
83 case LoudnessEnhancer::gainMb: {
84 RETURN_IF(mContext->setLeGain(leParam.get<LoudnessEnhancer::gainMb>()) !=
85 RetCode::SUCCESS,
86 EX_ILLEGAL_ARGUMENT, "setGainMbFailed");
87 return ndk::ScopedAStatus::ok();
88 }
89 default: {
90 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
91 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
92 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
93 }
94 }
95 }
96
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)97 ndk::ScopedAStatus LoudnessEnhancerImpl::getParameterSpecific(const Parameter::Id& id,
98 Parameter::Specific* specific) {
99 RETURN_IF(!specific, EX_NULL_POINTER, "nullPtr");
100 auto tag = id.getTag();
101 RETURN_IF(Parameter::Id::loudnessEnhancerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
102 auto leId = id.get<Parameter::Id::loudnessEnhancerTag>();
103 auto leIdTag = leId.getTag();
104 switch (leIdTag) {
105 case LoudnessEnhancer::Id::commonTag:
106 return getParameterLoudnessEnhancer(leId.get<LoudnessEnhancer::Id::commonTag>(),
107 specific);
108 default:
109 LOG(ERROR) << __func__ << " unsupported tag: " << toString(leIdTag);
110 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
111 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
112 }
113 }
114
getParameterLoudnessEnhancer(const LoudnessEnhancer::Tag & tag,Parameter::Specific * specific)115 ndk::ScopedAStatus LoudnessEnhancerImpl::getParameterLoudnessEnhancer(
116 const LoudnessEnhancer::Tag& tag, Parameter::Specific* specific) {
117 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
118
119 LoudnessEnhancer leParam;
120 switch (tag) {
121 case LoudnessEnhancer::gainMb: {
122 leParam.set<LoudnessEnhancer::gainMb>(mContext->getLeGain());
123 break;
124 }
125 default: {
126 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
127 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
128 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
129 }
130 }
131
132 specific->set<Parameter::Specific::loudnessEnhancer>(leParam);
133 return ndk::ScopedAStatus::ok();
134 }
135
createContext(const Parameter::Common & common)136 std::shared_ptr<EffectContext> LoudnessEnhancerImpl::createContext(
137 const Parameter::Common& common) {
138 if (mContext) {
139 LOG(DEBUG) << __func__ << " context already exist";
140 return mContext;
141 }
142 const int channelCount = getChannelCount(common.input.base.channelMask);
143 if (FCC_LIMIT < channelCount) {
144 LOG(ERROR) << __func__
145 << " channelCount not supported: " << common.input.base.channelMask.toString();
146 return nullptr;
147 }
148
149 mContext = std::make_shared<LoudnessEnhancerContext>(1 /* statusFmqDepth */, common);
150 return mContext;
151 }
152
releaseContext()153 RetCode LoudnessEnhancerImpl::releaseContext() {
154 if (mContext) {
155 mContext->disable();
156 mContext->resetBuffer();
157 }
158 return RetCode::SUCCESS;
159 }
160
161 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)162 IEffect::Status LoudnessEnhancerImpl::effectProcessImpl(float* in, float* out, int samples) {
163 IEffect::Status status = {EX_NULL_POINTER, 0, 0};
164 RETURN_VALUE_IF(!mContext, status, "nullContext");
165 return mContext->process(in, out, samples);
166 }
167
168 } // namespace aidl::android::hardware::audio::effect
169