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