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 | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 30 | using aidl::android::hardware::audio::effect::IEffect; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 31 | using aidl::android::hardware::audio::effect::kBassBoostSwImplUUID; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 32 | using aidl::android::hardware::audio::effect::State; |
| 33 | using aidl::android::media::audio::common::AudioUuid; |
| 34 | |
| 35 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 36 | std::shared_ptr<IEffect>* instanceSpp) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 37 | if (!in_impl_uuid || *in_impl_uuid != kBassBoostSwImplUUID) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 38 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 39 | return EX_ILLEGAL_ARGUMENT; |
| 40 | } |
| 41 | if (instanceSpp) { |
| 42 | *instanceSpp = ndk::SharedRefBase::make<BassBoostSw>(); |
| 43 | LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created"; |
| 44 | return EX_NONE; |
| 45 | } else { |
| 46 | LOG(ERROR) << __func__ << " invalid input parameter!"; |
| 47 | return EX_ILLEGAL_ARGUMENT; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | extern "C" binder_exception_t destroyEffect(const std::shared_ptr<IEffect>& instanceSp) { |
| 52 | if (!instanceSp) { |
| 53 | return EX_NONE; |
| 54 | } |
| 55 | State state; |
| 56 | ndk::ScopedAStatus status = instanceSp->getState(&state); |
| 57 | if (!status.isOk() || State::INIT != state) { |
| 58 | LOG(ERROR) << __func__ << " instance " << instanceSp.get() |
| 59 | << " in state: " << toString(state) << ", status: " << status.getDescription(); |
| 60 | return EX_ILLEGAL_STATE; |
| 61 | } |
| 62 | LOG(DEBUG) << __func__ << " instance " << instanceSp.get() << " destroyed"; |
| 63 | return EX_NONE; |
| 64 | } |
| 65 | |
| 66 | namespace aidl::android::hardware::audio::effect { |
| 67 | |
| 68 | ndk::ScopedAStatus BassBoostSw::getDescriptor(Descriptor* _aidl_return) { |
| 69 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 70 | *_aidl_return = kDescriptor; |
| 71 | return ndk::ScopedAStatus::ok(); |
| 72 | } |
| 73 | |
| 74 | ndk::ScopedAStatus BassBoostSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 75 | RETURN_IF(Parameter::Specific::bassBoost != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 76 | "EffectNotSupported"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 77 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 78 | |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 79 | auto& bbParam = specific.get<Parameter::Specific::bassBoost>(); |
| 80 | auto tag = bbParam.getTag(); |
| 81 | |
| 82 | switch (tag) { |
| 83 | case BassBoost::strengthPm: { |
| 84 | RETURN_IF(!mStrengthSupported, EX_ILLEGAL_ARGUMENT, "SettingStrengthNotSupported"); |
| 85 | |
| 86 | RETURN_IF(mContext->setBbStrengthPm(bbParam.get<BassBoost::strengthPm>()) != |
| 87 | RetCode::SUCCESS, |
| 88 | EX_ILLEGAL_ARGUMENT, "strengthPmNotSupported"); |
| 89 | return ndk::ScopedAStatus::ok(); |
| 90 | } |
| 91 | default: { |
| 92 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 93 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 94 | "BassBoostTagNotSupported"); |
| 95 | } |
| 96 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | ndk::ScopedAStatus BassBoostSw::getParameterSpecific(const Parameter::Id& id, |
| 100 | Parameter::Specific* specific) { |
| 101 | auto tag = id.getTag(); |
| 102 | RETURN_IF(Parameter::Id::bassBoostTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 103 | auto bbId = id.get<Parameter::Id::bassBoostTag>(); |
| 104 | auto bbIdTag = bbId.getTag(); |
| 105 | switch (bbIdTag) { |
| 106 | case BassBoost::Id::commonTag: |
| 107 | return getParameterBassBoost(bbId.get<BassBoost::Id::commonTag>(), specific); |
| 108 | default: |
| 109 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 110 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 111 | "BassBoostTagNotSupported"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | ndk::ScopedAStatus BassBoostSw::getParameterBassBoost(const BassBoost::Tag& tag, |
| 116 | Parameter::Specific* specific) { |
| 117 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 118 | BassBoost bbParam; |
| 119 | switch (tag) { |
| 120 | case BassBoost::strengthPm: { |
| 121 | bbParam.set<BassBoost::strengthPm>(mContext->getBbStrengthPm()); |
| 122 | break; |
| 123 | } |
| 124 | default: { |
| 125 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 126 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 127 | "BassBoostTagNotSupported"); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | specific->set<Parameter::Specific::bassBoost>(bbParam); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 132 | return ndk::ScopedAStatus::ok(); |
| 133 | } |
| 134 | |
| 135 | std::shared_ptr<EffectContext> BassBoostSw::createContext(const Parameter::Common& common) { |
| 136 | if (mContext) { |
| 137 | LOG(DEBUG) << __func__ << " context already exist"; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 138 | } else { |
| 139 | mContext = std::make_shared<BassBoostSwContext>(1 /* statusFmqDepth */, common); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 140 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 141 | return mContext; |
| 142 | } |
| 143 | |
| 144 | std::shared_ptr<EffectContext> BassBoostSw::getContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 145 | return mContext; |
| 146 | } |
| 147 | |
| 148 | RetCode BassBoostSw::releaseContext() { |
| 149 | if (mContext) { |
| 150 | mContext.reset(); |
| 151 | } |
| 152 | return RetCode::SUCCESS; |
| 153 | } |
| 154 | |
| 155 | // Processing method running in EffectWorker thread. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 156 | IEffect::Status BassBoostSw::effectProcessImpl(float* in, float* out, int samples) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 157 | // TODO: get data buffer and process. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 158 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 159 | for (int i = 0; i < samples; i++) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 160 | *out++ = *in++; |
| 161 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 162 | return {STATUS_OK, samples, samples}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | } // namespace aidl::android::hardware::audio::effect |