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 #include <algorithm>
18 #include <cstddef>
19 #include <memory>
20
21 #define LOG_TAG "AHAL_BassBoostSw"
22 #include <android-base/logging.h>
23 #include <fmq/AidlMessageQueue.h>
24 #include <system/audio_effects/effect_uuid.h>
25
26 #include "BassBoostSw.h"
27
28 using aidl::android::hardware::audio::effect::BassBoostSw;
29 using aidl::android::hardware::audio::effect::Descriptor;
30 using aidl::android::hardware::audio::effect::getEffectImplUuidBassBoostProxy;
31 using aidl::android::hardware::audio::effect::getEffectImplUuidBassBoostSw;
32 using aidl::android::hardware::audio::effect::getEffectTypeUuidBassBoost;
33 using aidl::android::hardware::audio::effect::IEffect;
34 using aidl::android::hardware::audio::effect::State;
35 using aidl::android::media::audio::common::AudioUuid;
36
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)37 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
38 std::shared_ptr<IEffect>* instanceSpp) {
39 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidBassBoostSw()) {
40 LOG(ERROR) << __func__ << "uuid not supported";
41 return EX_ILLEGAL_ARGUMENT;
42 }
43 if (instanceSpp) {
44 *instanceSpp = ndk::SharedRefBase::make<BassBoostSw>();
45 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
46 return EX_NONE;
47 } else {
48 LOG(ERROR) << __func__ << " invalid input parameter!";
49 return EX_ILLEGAL_ARGUMENT;
50 }
51 }
52
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)53 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
54 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidBassBoostSw()) {
55 LOG(ERROR) << __func__ << "uuid not supported";
56 return EX_ILLEGAL_ARGUMENT;
57 }
58 *_aidl_return = BassBoostSw::kDescriptor;
59 return EX_NONE;
60 }
61
62 namespace aidl::android::hardware::audio::effect {
63
64 const std::string BassBoostSw::kEffectName = "BassBoostSw";
65
66 const std::vector<Range::BassBoostRange> BassBoostSw::kRanges = {
67 MAKE_RANGE(BassBoost, strengthPm, 0, 1000)};
68 const Capability BassBoostSw::kCapability = {.range = {BassBoostSw::kRanges}};
69 const Descriptor BassBoostSw::kDescriptor = {
70 .common = {.id = {.type = getEffectTypeUuidBassBoost(),
71 .uuid = getEffectImplUuidBassBoostSw()},
72 .flags = {.type = Flags::Type::INSERT,
73 .insert = Flags::Insert::FIRST,
74 .volume = Flags::Volume::CTRL},
75 .name = BassBoostSw::kEffectName,
76 .implementor = "The Android Open Source Project"},
77 .capability = BassBoostSw::kCapability};
78
getDescriptor(Descriptor * _aidl_return)79 ndk::ScopedAStatus BassBoostSw::getDescriptor(Descriptor* _aidl_return) {
80 LOG(DEBUG) << __func__ << kDescriptor.toString();
81 *_aidl_return = kDescriptor;
82 return ndk::ScopedAStatus::ok();
83 }
84
setParameterSpecific(const Parameter::Specific & specific)85 ndk::ScopedAStatus BassBoostSw::setParameterSpecific(const Parameter::Specific& specific) {
86 RETURN_IF(Parameter::Specific::bassBoost != specific.getTag(), EX_ILLEGAL_ARGUMENT,
87 "EffectNotSupported");
88 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
89
90 auto& bbParam = specific.get<Parameter::Specific::bassBoost>();
91 RETURN_IF(!inRange(bbParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
92 auto tag = bbParam.getTag();
93
94 switch (tag) {
95 case BassBoost::strengthPm: {
96 const auto strength = bbParam.get<BassBoost::strengthPm>();
97 RETURN_IF(mContext->setBbStrengthPm(strength) != RetCode::SUCCESS, EX_ILLEGAL_ARGUMENT,
98 "strengthPmNotSupported");
99 return ndk::ScopedAStatus::ok();
100 }
101 default: {
102 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
103 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
104 "BassBoostTagNotSupported");
105 }
106 }
107 }
108
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)109 ndk::ScopedAStatus BassBoostSw::getParameterSpecific(const Parameter::Id& id,
110 Parameter::Specific* specific) {
111 auto tag = id.getTag();
112 RETURN_IF(Parameter::Id::bassBoostTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
113 auto bbId = id.get<Parameter::Id::bassBoostTag>();
114 auto bbIdTag = bbId.getTag();
115 switch (bbIdTag) {
116 case BassBoost::Id::commonTag:
117 return getParameterBassBoost(bbId.get<BassBoost::Id::commonTag>(), specific);
118 default:
119 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
120 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
121 "BassBoostTagNotSupported");
122 }
123 }
124
getParameterBassBoost(const BassBoost::Tag & tag,Parameter::Specific * specific)125 ndk::ScopedAStatus BassBoostSw::getParameterBassBoost(const BassBoost::Tag& tag,
126 Parameter::Specific* specific) {
127 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
128 BassBoost bbParam;
129 switch (tag) {
130 case BassBoost::strengthPm: {
131 bbParam.set<BassBoost::strengthPm>(mContext->getBbStrengthPm());
132 break;
133 }
134 default: {
135 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
136 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
137 "BassBoostTagNotSupported");
138 }
139 }
140
141 specific->set<Parameter::Specific::bassBoost>(bbParam);
142 return ndk::ScopedAStatus::ok();
143 }
144
createContext(const Parameter::Common & common)145 std::shared_ptr<EffectContext> BassBoostSw::createContext(const Parameter::Common& common) {
146 if (mContext) {
147 LOG(DEBUG) << __func__ << " context already exist";
148 } else {
149 mContext = std::make_shared<BassBoostSwContext>(1 /* statusFmqDepth */, common);
150 }
151 return mContext;
152 }
153
releaseContext()154 RetCode BassBoostSw::releaseContext() {
155 if (mContext) {
156 mContext.reset();
157 }
158 return RetCode::SUCCESS;
159 }
160
161 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)162 IEffect::Status BassBoostSw::effectProcessImpl(float* in, float* out, int samples) {
163 // TODO: get data buffer and process.
164 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
165 for (int i = 0; i < samples; i++) {
166 *out++ = *in++;
167 }
168 return {STATUS_OK, samples, samples};
169 }
170
setBbStrengthPm(int strength)171 RetCode BassBoostSwContext::setBbStrengthPm(int strength) {
172 mStrength = strength;
173 return RetCode::SUCCESS;
174 }
175
176 } // namespace aidl::android::hardware::audio::effect
177