Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +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 | |
| 17 | #include <algorithm> |
| 18 | #include <cstddef> |
| 19 | #include <memory> |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 20 | #define LOG_TAG "AHAL_NoiseSuppressionSw" |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 21 | |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 22 | #define LOG_TAG "AHAL_NoiseSuppressionSw" |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 23 | #include <android-base/logging.h> |
| 24 | #include <fmq/AidlMessageQueue.h> |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 25 | #include <system/audio_effects/effect_uuid.h> |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 26 | |
| 27 | #include "NoiseSuppressionSw.h" |
| 28 | |
| 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::getEffectImplUuidNoiseSuppressionSw; |
| 31 | using aidl::android::hardware::audio::effect::getEffectTypeUuidNoiseSuppression; |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 32 | using aidl::android::hardware::audio::effect::IEffect; |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 33 | using aidl::android::hardware::audio::effect::NoiseSuppressionSw; |
| 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 | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 38 | if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidNoiseSuppressionSw()) { |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 39 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 40 | return EX_ILLEGAL_ARGUMENT; |
| 41 | } |
| 42 | if (instanceSpp) { |
| 43 | *instanceSpp = ndk::SharedRefBase::make<NoiseSuppressionSw>(); |
| 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 | |
| 52 | 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] | 53 | if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidNoiseSuppressionSw()) { |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 54 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 55 | return EX_ILLEGAL_ARGUMENT; |
| 56 | } |
| 57 | *_aidl_return = NoiseSuppressionSw::kDescriptor; |
| 58 | return EX_NONE; |
| 59 | } |
| 60 | |
| 61 | namespace aidl::android::hardware::audio::effect { |
| 62 | |
| 63 | const std::string NoiseSuppressionSw::kEffectName = "NoiseSuppressionSw"; |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 64 | const Descriptor NoiseSuppressionSw::kDescriptor = { |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 65 | .common = {.id = {.type = getEffectTypeUuidNoiseSuppression(), |
| 66 | .uuid = getEffectImplUuidNoiseSuppressionSw(), |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 67 | .proxy = std::nullopt}, |
Shunkai Yao | 12179c3 | 2023-10-18 01:15:37 +0000 | [diff] [blame^] | 68 | .flags = {.type = Flags::Type::PRE_PROC, |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 69 | .insert = Flags::Insert::FIRST, |
| 70 | .volume = Flags::Volume::CTRL}, |
| 71 | .name = NoiseSuppressionSw::kEffectName, |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 72 | .implementor = "The Android Open Source Project"}}; |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 73 | |
| 74 | ndk::ScopedAStatus NoiseSuppressionSw::getDescriptor(Descriptor* _aidl_return) { |
| 75 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 76 | *_aidl_return = kDescriptor; |
| 77 | return ndk::ScopedAStatus::ok(); |
| 78 | } |
| 79 | |
| 80 | ndk::ScopedAStatus NoiseSuppressionSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 81 | RETURN_IF(Parameter::Specific::noiseSuppression != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 82 | "EffectNotSupported"); |
| 83 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 84 | |
| 85 | auto& param = specific.get<Parameter::Specific::noiseSuppression>(); |
| 86 | auto tag = param.getTag(); |
| 87 | |
| 88 | switch (tag) { |
| 89 | case NoiseSuppression::level: { |
| 90 | RETURN_IF(mContext->setLevel(param.get<NoiseSuppression::level>()) != RetCode::SUCCESS, |
Shunkai Yao | 58aaf5b | 2023-02-06 07:25:09 +0000 | [diff] [blame] | 91 | EX_ILLEGAL_ARGUMENT, "levelNotSupported"); |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 92 | return ndk::ScopedAStatus::ok(); |
| 93 | } |
Shunkai Yao | 58aaf5b | 2023-02-06 07:25:09 +0000 | [diff] [blame] | 94 | case NoiseSuppression::type: { |
| 95 | RETURN_IF(mContext->setType(param.get<NoiseSuppression::type>()) != RetCode::SUCCESS, |
| 96 | EX_ILLEGAL_ARGUMENT, "typeNotSupported"); |
| 97 | return ndk::ScopedAStatus::ok(); |
| 98 | } |
| 99 | case NoiseSuppression::vendor: { |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 100 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 101 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 102 | EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported"); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | ndk::ScopedAStatus NoiseSuppressionSw::getParameterSpecific(const Parameter::Id& id, |
| 108 | Parameter::Specific* specific) { |
| 109 | auto tag = id.getTag(); |
| 110 | RETURN_IF(Parameter::Id::noiseSuppressionTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 111 | auto specificId = id.get<Parameter::Id::noiseSuppressionTag>(); |
| 112 | auto specificIdTag = specificId.getTag(); |
| 113 | switch (specificIdTag) { |
| 114 | case NoiseSuppression::Id::commonTag: |
| 115 | return getParameterNoiseSuppression(specificId.get<NoiseSuppression::Id::commonTag>(), |
| 116 | specific); |
Shunkai Yao | 58aaf5b | 2023-02-06 07:25:09 +0000 | [diff] [blame] | 117 | case NoiseSuppression::Id::vendorExtensionTag: { |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 118 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 119 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 120 | EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported"); |
Shunkai Yao | 58aaf5b | 2023-02-06 07:25:09 +0000 | [diff] [blame] | 121 | } |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | ndk::ScopedAStatus NoiseSuppressionSw::getParameterNoiseSuppression( |
| 126 | const NoiseSuppression::Tag& tag, Parameter::Specific* specific) { |
| 127 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 128 | NoiseSuppression param; |
| 129 | switch (tag) { |
| 130 | case NoiseSuppression::level: { |
| 131 | param.set<NoiseSuppression::level>(mContext->getLevel()); |
| 132 | break; |
| 133 | } |
Shunkai Yao | 58aaf5b | 2023-02-06 07:25:09 +0000 | [diff] [blame] | 134 | case NoiseSuppression::type: { |
| 135 | param.set<NoiseSuppression::type>(mContext->getType()); |
| 136 | break; |
| 137 | } |
| 138 | case NoiseSuppression::vendor: { |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 139 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 140 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 141 | EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported"); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | specific->set<Parameter::Specific::noiseSuppression>(param); |
| 146 | return ndk::ScopedAStatus::ok(); |
| 147 | } |
| 148 | |
| 149 | std::shared_ptr<EffectContext> NoiseSuppressionSw::createContext(const Parameter::Common& common) { |
| 150 | if (mContext) { |
| 151 | LOG(DEBUG) << __func__ << " context already exist"; |
| 152 | } else { |
| 153 | mContext = std::make_shared<NoiseSuppressionSwContext>(1 /* statusFmqDepth */, common); |
| 154 | } |
| 155 | return mContext; |
| 156 | } |
| 157 | |
| 158 | std::shared_ptr<EffectContext> NoiseSuppressionSw::getContext() { |
| 159 | return mContext; |
| 160 | } |
| 161 | |
| 162 | RetCode NoiseSuppressionSw::releaseContext() { |
| 163 | if (mContext) { |
| 164 | mContext.reset(); |
| 165 | } |
| 166 | return RetCode::SUCCESS; |
| 167 | } |
| 168 | |
| 169 | // Processing method running in EffectWorker thread. |
| 170 | IEffect::Status NoiseSuppressionSw::effectProcessImpl(float* in, float* out, int samples) { |
| 171 | // TODO: get data buffer and process. |
| 172 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 173 | for (int i = 0; i < samples; i++) { |
| 174 | *out++ = *in++; |
| 175 | } |
| 176 | return {STATUS_OK, samples, samples}; |
| 177 | } |
| 178 | |
| 179 | RetCode NoiseSuppressionSwContext::setLevel(NoiseSuppression::Level level) { |
| 180 | mLevel = level; |
| 181 | return RetCode::SUCCESS; |
| 182 | } |
| 183 | |
| 184 | NoiseSuppression::Level NoiseSuppressionSwContext::getLevel() { |
| 185 | return mLevel; |
| 186 | } |
| 187 | |
Shunkai Yao | 58aaf5b | 2023-02-06 07:25:09 +0000 | [diff] [blame] | 188 | RetCode NoiseSuppressionSwContext::setType(NoiseSuppression::Type type) { |
| 189 | mType = type; |
| 190 | return RetCode::SUCCESS; |
| 191 | } |
| 192 | |
Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame] | 193 | } // namespace aidl::android::hardware::audio::effect |