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 | #define LOG_TAG "AHAL_BassBoostSw" |
| 21 | #include <Utils.h> |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 22 | #include <unordered_set> |
| 23 | |
| 24 | #include <android-base/logging.h> |
| 25 | #include <fmq/AidlMessageQueue.h> |
| 26 | |
| 27 | #include "BassBoostSw.h" |
| 28 | |
| 29 | using aidl::android::hardware::audio::effect::BassBoostSw; |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 30 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 31 | using aidl::android::hardware::audio::effect::IEffect; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 32 | using aidl::android::hardware::audio::effect::kBassBoostSwImplUUID; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 33 | using aidl::android::hardware::audio::effect::State; |
| 34 | using aidl::android::media::audio::common::AudioUuid; |
| 35 | |
| 36 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 37 | std::shared_ptr<IEffect>* instanceSpp) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 38 | if (!in_impl_uuid || *in_impl_uuid != kBassBoostSwImplUUID) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 39 | 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 Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 52 | extern "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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 56 | } |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 57 | *_aidl_return = BassBoostSw::kDescriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 58 | return EX_NONE; |
| 59 | } |
| 60 | |
| 61 | namespace aidl::android::hardware::audio::effect { |
| 62 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 63 | const std::string BassBoostSw::kEffectName = "BassBoostSw"; |
| 64 | const bool BassBoostSw::kStrengthSupported = true; |
Sham Rathod | 8411fd2 | 2022-12-27 10:27:03 +0530 | [diff] [blame] | 65 | const BassBoost::Capability BassBoostSw::kCapability = {.maxStrengthPm = 1000, |
| 66 | .strengthSupported = kStrengthSupported}; |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 67 | const Descriptor BassBoostSw::kDescriptor = { |
| 68 | .common = {.id = {.type = kBassBoostTypeUUID, |
| 69 | .uuid = kBassBoostSwImplUUID, |
Shraddha Basantwani | 3a2fb03 | 2022-11-22 11:04:35 +0530 | [diff] [blame] | 70 | .proxy = kBassBoostProxyUUID}, |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 71 | .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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 78 | ndk::ScopedAStatus BassBoostSw::getDescriptor(Descriptor* _aidl_return) { |
| 79 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 80 | *_aidl_return = kDescriptor; |
| 81 | return ndk::ScopedAStatus::ok(); |
| 82 | } |
| 83 | |
| 84 | ndk::ScopedAStatus BassBoostSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 85 | RETURN_IF(Parameter::Specific::bassBoost != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 86 | "EffectNotSupported"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 87 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 88 | |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 89 | auto& bbParam = specific.get<Parameter::Specific::bassBoost>(); |
| 90 | auto tag = bbParam.getTag(); |
| 91 | |
| 92 | switch (tag) { |
| 93 | case BassBoost::strengthPm: { |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 94 | RETURN_IF(!kStrengthSupported, EX_ILLEGAL_ARGUMENT, "SettingStrengthNotSupported"); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 95 | |
| 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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 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"); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 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 | |
| 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); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 142 | return ndk::ScopedAStatus::ok(); |
| 143 | } |
| 144 | |
| 145 | std::shared_ptr<EffectContext> BassBoostSw::createContext(const Parameter::Common& common) { |
| 146 | if (mContext) { |
| 147 | LOG(DEBUG) << __func__ << " context already exist"; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 148 | } else { |
| 149 | mContext = std::make_shared<BassBoostSwContext>(1 /* statusFmqDepth */, common); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 150 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 151 | return mContext; |
| 152 | } |
| 153 | |
| 154 | std::shared_ptr<EffectContext> BassBoostSw::getContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 155 | return mContext; |
| 156 | } |
| 157 | |
| 158 | RetCode BassBoostSw::releaseContext() { |
| 159 | if (mContext) { |
| 160 | mContext.reset(); |
| 161 | } |
| 162 | return RetCode::SUCCESS; |
| 163 | } |
| 164 | |
| 165 | // Processing method running in EffectWorker thread. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 166 | IEffect::Status BassBoostSw::effectProcessImpl(float* in, float* out, int samples) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 167 | // TODO: get data buffer and process. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 168 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 169 | for (int i = 0; i < samples; i++) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 170 | *out++ = *in++; |
| 171 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 172 | return {STATUS_OK, samples, samples}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Sham Rathod | 8411fd2 | 2022-12-27 10:27:03 +0530 | [diff] [blame] | 175 | RetCode 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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 185 | } // namespace aidl::android::hardware::audio::effect |