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