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