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