blob: 0c7ebe15378716bc800c6227eee772bb316e8821 [file] [log] [blame]
Shunkai Yao6afc8552022-10-26 22:47:20 +00001/*
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
Shraddha Basantwanif627d802022-11-08 14:45:07 +053017#include <algorithm>
Shunkai Yao6afc8552022-10-26 22:47:20 +000018#include <cstddef>
Shraddha Basantwanif627d802022-11-08 14:45:07 +053019#include <memory>
Shunkai Yao6afc8552022-10-26 22:47:20 +000020#define LOG_TAG "AHAL_BassBoostSw"
21#include <Utils.h>
Shunkai Yao6afc8552022-10-26 22:47:20 +000022#include <unordered_set>
23
24#include <android-base/logging.h>
25#include <fmq/AidlMessageQueue.h>
26
27#include "BassBoostSw.h"
28
29using aidl::android::hardware::audio::effect::BassBoostSw;
Shunkai Yaoc12e0822022-12-12 07:13:58 +000030using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000031using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000032using aidl::android::hardware::audio::effect::kBassBoostSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000033using aidl::android::hardware::audio::effect::State;
34using aidl::android::media::audio::common::AudioUuid;
35
36extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
37 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000038 if (!in_impl_uuid || *in_impl_uuid != kBassBoostSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000039 LOG(ERROR) << __func__ << "uuid not supported";
40 return EX_ILLEGAL_ARGUMENT;
41 }
42 if (instanceSpp) {
43 *instanceSpp = ndk::SharedRefBase::make<BassBoostSw>();
44 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
45 return EX_NONE;
46 } else {
47 LOG(ERROR) << __func__ << " invalid input parameter!";
48 return EX_ILLEGAL_ARGUMENT;
49 }
50}
51
Shunkai Yaoc12e0822022-12-12 07:13:58 +000052extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
53 if (!in_impl_uuid || *in_impl_uuid != kBassBoostSwImplUUID) {
54 LOG(ERROR) << __func__ << "uuid not supported";
55 return EX_ILLEGAL_ARGUMENT;
Shunkai Yao6afc8552022-10-26 22:47:20 +000056 }
Shunkai Yaoc12e0822022-12-12 07:13:58 +000057 *_aidl_return = BassBoostSw::kDescriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000058 return EX_NONE;
59}
60
61namespace aidl::android::hardware::audio::effect {
62
Shunkai Yaoc12e0822022-12-12 07:13:58 +000063const std::string BassBoostSw::kEffectName = "BassBoostSw";
64const bool BassBoostSw::kStrengthSupported = true;
Sham Rathod8411fd22022-12-27 10:27:03 +053065const BassBoost::Capability BassBoostSw::kCapability = {.maxStrengthPm = 1000,
66 .strengthSupported = kStrengthSupported};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000067const Descriptor BassBoostSw::kDescriptor = {
68 .common = {.id = {.type = kBassBoostTypeUUID,
69 .uuid = kBassBoostSwImplUUID,
Shraddha Basantwani3a2fb032022-11-22 11:04:35 +053070 .proxy = kBassBoostProxyUUID},
Shunkai Yaoc12e0822022-12-12 07:13:58 +000071 .flags = {.type = Flags::Type::INSERT,
72 .insert = Flags::Insert::FIRST,
73 .volume = Flags::Volume::CTRL},
74 .name = BassBoostSw::kEffectName,
75 .implementor = "The Android Open Source Project"},
76 .capability = Capability::make<Capability::bassBoost>(BassBoostSw::kCapability)};
77
Shunkai Yao6afc8552022-10-26 22:47:20 +000078ndk::ScopedAStatus BassBoostSw::getDescriptor(Descriptor* _aidl_return) {
79 LOG(DEBUG) << __func__ << kDescriptor.toString();
80 *_aidl_return = kDescriptor;
81 return ndk::ScopedAStatus::ok();
82}
83
84ndk::ScopedAStatus BassBoostSw::setParameterSpecific(const Parameter::Specific& specific) {
85 RETURN_IF(Parameter::Specific::bassBoost != specific.getTag(), EX_ILLEGAL_ARGUMENT,
86 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000087 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
88
Shraddha Basantwanif627d802022-11-08 14:45:07 +053089 auto& bbParam = specific.get<Parameter::Specific::bassBoost>();
90 auto tag = bbParam.getTag();
91
92 switch (tag) {
93 case BassBoost::strengthPm: {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000094 RETURN_IF(!kStrengthSupported, EX_ILLEGAL_ARGUMENT, "SettingStrengthNotSupported");
Shraddha Basantwanif627d802022-11-08 14:45:07 +053095
96 RETURN_IF(mContext->setBbStrengthPm(bbParam.get<BassBoost::strengthPm>()) !=
97 RetCode::SUCCESS,
98 EX_ILLEGAL_ARGUMENT, "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 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000107}
108
109ndk::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");
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530113 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
125ndk::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);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000142 return ndk::ScopedAStatus::ok();
143}
144
145std::shared_ptr<EffectContext> BassBoostSw::createContext(const Parameter::Common& common) {
146 if (mContext) {
147 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530148 } else {
149 mContext = std::make_shared<BassBoostSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000150 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530151 return mContext;
152}
153
154std::shared_ptr<EffectContext> BassBoostSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000155 return mContext;
156}
157
158RetCode BassBoostSw::releaseContext() {
159 if (mContext) {
160 mContext.reset();
161 }
162 return RetCode::SUCCESS;
163}
164
165// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530166IEffect::Status BassBoostSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000167 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530168 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
169 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000170 *out++ = *in++;
171 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530172 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000173}
174
Sham Rathod8411fd22022-12-27 10:27:03 +0530175RetCode BassBoostSwContext::setBbStrengthPm(int strength) {
176 if (strength < 0 || strength > BassBoostSw::kCapability.maxStrengthPm) {
177 LOG(ERROR) << __func__ << " invalid strength: " << strength;
178 return RetCode::ERROR_ILLEGAL_PARAMETER;
179 }
180 // TODO : Add implementation to apply new strength
181 mStrength = strength;
182 return RetCode::SUCCESS;
183}
184
Shunkai Yao6afc8552022-10-26 22:47:20 +0000185} // namespace aidl::android::hardware::audio::effect