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; |
| 65 | const BassBoost::Capability BassBoostSw::kCapability = {.strengthSupported = kStrengthSupported}; |
| 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"}, |
| 75 | .capability = Capability::make<Capability::bassBoost>(BassBoostSw::kCapability)}; |
| 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>(); |
| 89 | auto tag = bbParam.getTag(); |
| 90 | |
| 91 | switch (tag) { |
| 92 | case BassBoost::strengthPm: { |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 93 | RETURN_IF(!kStrengthSupported, EX_ILLEGAL_ARGUMENT, "SettingStrengthNotSupported"); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 94 | |
| 95 | RETURN_IF(mContext->setBbStrengthPm(bbParam.get<BassBoost::strengthPm>()) != |
| 96 | RetCode::SUCCESS, |
| 97 | EX_ILLEGAL_ARGUMENT, "strengthPmNotSupported"); |
| 98 | return ndk::ScopedAStatus::ok(); |
| 99 | } |
| 100 | default: { |
| 101 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 102 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 103 | "BassBoostTagNotSupported"); |
| 104 | } |
| 105 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | ndk::ScopedAStatus BassBoostSw::getParameterSpecific(const Parameter::Id& id, |
| 109 | Parameter::Specific* specific) { |
| 110 | auto tag = id.getTag(); |
| 111 | RETURN_IF(Parameter::Id::bassBoostTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 112 | auto bbId = id.get<Parameter::Id::bassBoostTag>(); |
| 113 | auto bbIdTag = bbId.getTag(); |
| 114 | switch (bbIdTag) { |
| 115 | case BassBoost::Id::commonTag: |
| 116 | return getParameterBassBoost(bbId.get<BassBoost::Id::commonTag>(), specific); |
| 117 | default: |
| 118 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 119 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 120 | "BassBoostTagNotSupported"); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | ndk::ScopedAStatus BassBoostSw::getParameterBassBoost(const BassBoost::Tag& tag, |
| 125 | Parameter::Specific* specific) { |
| 126 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 127 | BassBoost bbParam; |
| 128 | switch (tag) { |
| 129 | case BassBoost::strengthPm: { |
| 130 | bbParam.set<BassBoost::strengthPm>(mContext->getBbStrengthPm()); |
| 131 | break; |
| 132 | } |
| 133 | default: { |
| 134 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 135 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 136 | "BassBoostTagNotSupported"); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | specific->set<Parameter::Specific::bassBoost>(bbParam); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 141 | return ndk::ScopedAStatus::ok(); |
| 142 | } |
| 143 | |
| 144 | std::shared_ptr<EffectContext> BassBoostSw::createContext(const Parameter::Common& common) { |
| 145 | if (mContext) { |
| 146 | LOG(DEBUG) << __func__ << " context already exist"; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 147 | } else { |
| 148 | mContext = std::make_shared<BassBoostSwContext>(1 /* statusFmqDepth */, common); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 149 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 150 | return mContext; |
| 151 | } |
| 152 | |
| 153 | std::shared_ptr<EffectContext> BassBoostSw::getContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 154 | return mContext; |
| 155 | } |
| 156 | |
| 157 | RetCode BassBoostSw::releaseContext() { |
| 158 | if (mContext) { |
| 159 | mContext.reset(); |
| 160 | } |
| 161 | return RetCode::SUCCESS; |
| 162 | } |
| 163 | |
| 164 | // Processing method running in EffectWorker thread. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 165 | IEffect::Status BassBoostSw::effectProcessImpl(float* in, float* out, int samples) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 166 | // TODO: get data buffer and process. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 167 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 168 | for (int i = 0; i < samples; i++) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 169 | *out++ = *in++; |
| 170 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 171 | return {STATUS_OK, samples, samples}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | } // namespace aidl::android::hardware::audio::effect |