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>
20 #define LOG_TAG "AHAL_NoiseSuppressionSw"
21
22 #define LOG_TAG "AHAL_NoiseSuppressionSw"
23 #include <android-base/logging.h>
24 #include <fmq/AidlMessageQueue.h>
25 #include <system/audio_effects/effect_uuid.h>
26
27 #include "NoiseSuppressionSw.h"
28
29 using aidl::android::hardware::audio::effect::Descriptor;
30 using aidl::android::hardware::audio::effect::getEffectImplUuidNoiseSuppressionSw;
31 using aidl::android::hardware::audio::effect::getEffectTypeUuidNoiseSuppression;
32 using aidl::android::hardware::audio::effect::IEffect;
33 using aidl::android::hardware::audio::effect::NoiseSuppressionSw;
34 using aidl::android::media::audio::common::AudioUuid;
35
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)36 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
37 std::shared_ptr<IEffect>* instanceSpp) {
38 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidNoiseSuppressionSw()) {
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
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)52 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
53 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidNoiseSuppressionSw()) {
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";
64 const Descriptor NoiseSuppressionSw::kDescriptor = {
65 .common = {.id = {.type = getEffectTypeUuidNoiseSuppression(),
66 .uuid = getEffectImplUuidNoiseSuppressionSw(),
67 .proxy = std::nullopt},
68 .flags = {.type = Flags::Type::PRE_PROC,
69 .insert = Flags::Insert::FIRST,
70 .volume = Flags::Volume::NONE},
71 .name = NoiseSuppressionSw::kEffectName,
72 .implementor = "The Android Open Source Project"}};
73
getDescriptor(Descriptor * _aidl_return)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
setParameterSpecific(const Parameter::Specific & specific)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,
91 EX_ILLEGAL_ARGUMENT, "levelNotSupported");
92 return ndk::ScopedAStatus::ok();
93 }
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: {
100 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
101 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
102 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
103 }
104 }
105 }
106
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)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);
117 case NoiseSuppression::Id::vendorExtensionTag: {
118 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
119 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
120 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
121 }
122 }
123 }
124
getParameterNoiseSuppression(const NoiseSuppression::Tag & tag,Parameter::Specific * specific)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 }
134 case NoiseSuppression::type: {
135 param.set<NoiseSuppression::type>(mContext->getType());
136 break;
137 }
138 case NoiseSuppression::vendor: {
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
createContext(const Parameter::Common & common)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
releaseContext()158 RetCode NoiseSuppressionSw::releaseContext() {
159 if (mContext) {
160 mContext.reset();
161 }
162 return RetCode::SUCCESS;
163 }
164
165 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)166 IEffect::Status NoiseSuppressionSw::effectProcessImpl(float* in, float* out, int samples) {
167 // TODO: get data buffer and process.
168 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
169 for (int i = 0; i < samples; i++) {
170 *out++ = *in++;
171 }
172 return {STATUS_OK, samples, samples};
173 }
174
setLevel(NoiseSuppression::Level level)175 RetCode NoiseSuppressionSwContext::setLevel(NoiseSuppression::Level level) {
176 mLevel = level;
177 return RetCode::SUCCESS;
178 }
179
getLevel()180 NoiseSuppression::Level NoiseSuppressionSwContext::getLevel() {
181 return mLevel;
182 }
183
setType(NoiseSuppression::Type type)184 RetCode NoiseSuppressionSwContext::setType(NoiseSuppression::Type type) {
185 mType = type;
186 return RetCode::SUCCESS;
187 }
188
189 } // namespace aidl::android::hardware::audio::effect
190