blob: 905dba4dfc04672c2e9539a1db556eb7201c0ce6 [file] [log] [blame]
Shunkai Yao6afc8552022-10-26 22:47:20 +00001/*
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 <cstddef>
Shunkai Yao812d5b42022-11-16 18:08:50 +000018#define LOG_TAG "AHAL_EnvReverbSw"
Shunkai Yao6afc8552022-10-26 22:47:20 +000019#include <Utils.h>
20#include <algorithm>
21#include <unordered_set>
22
23#include <android-base/logging.h>
24#include <fmq/AidlMessageQueue.h>
25
Shunkai Yao812d5b42022-11-16 18:08:50 +000026#include "EnvReverbSw.h"
Shunkai Yao6afc8552022-10-26 22:47:20 +000027
Shunkai Yaoc12e0822022-12-12 07:13:58 +000028using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yao812d5b42022-11-16 18:08:50 +000029using aidl::android::hardware::audio::effect::EnvReverbSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000030using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000031using aidl::android::hardware::audio::effect::kEnvReverbSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000032using aidl::android::hardware::audio::effect::State;
33using aidl::android::media::audio::common::AudioUuid;
34
35extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
36 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000037 if (!in_impl_uuid || *in_impl_uuid != kEnvReverbSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000038 LOG(ERROR) << __func__ << "uuid not supported";
39 return EX_ILLEGAL_ARGUMENT;
40 }
41 if (instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000042 *instanceSpp = ndk::SharedRefBase::make<EnvReverbSw>();
Shunkai Yao6afc8552022-10-26 22:47:20 +000043 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
Shunkai Yaoc12e0822022-12-12 07:13:58 +000051extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
52 if (!in_impl_uuid || *in_impl_uuid != kEnvReverbSwImplUUID) {
53 LOG(ERROR) << __func__ << "uuid not supported";
54 return EX_ILLEGAL_ARGUMENT;
Shunkai Yao6afc8552022-10-26 22:47:20 +000055 }
Shunkai Yaoc12e0822022-12-12 07:13:58 +000056 *_aidl_return = EnvReverbSw::kDescriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000057 return EX_NONE;
58}
59
60namespace aidl::android::hardware::audio::effect {
61
Shunkai Yaoc12e0822022-12-12 07:13:58 +000062const std::string EnvReverbSw::kEffectName = "EnvReverbSw";
Sham Rathode362a462023-01-05 18:46:21 +053063const EnvironmentalReverb::Capability EnvReverbSw::kCapability = {.minRoomLevelMb = -6000,
64 .maxRoomLevelMb = 0,
65 .minRoomHfLevelMb = -4000,
66 .maxRoomHfLevelMb = 0,
67 .maxDecayTimeMs = 7000,
68 .minDecayHfRatioPm = 100,
69 .maxDecayHfRatioPm = 2000,
70 .minLevelMb = -6000,
71 .maxLevelMb = 0,
72 .maxDelayMs = 65,
73 .maxDiffusionPm = 1000,
74 .maxDensityPm = 1000};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000075const Descriptor EnvReverbSw::kDescriptor = {
76 .common = {.id = {.type = kEnvReverbTypeUUID,
77 .uuid = kEnvReverbSwImplUUID,
78 .proxy = std::nullopt},
79 .flags = {.type = Flags::Type::INSERT,
80 .insert = Flags::Insert::FIRST,
81 .volume = Flags::Volume::CTRL},
82 .name = EnvReverbSw::kEffectName,
83 .implementor = "The Android Open Source Project"},
84 .capability = Capability::make<Capability::environmentalReverb>(EnvReverbSw::kCapability)};
85
Shunkai Yao812d5b42022-11-16 18:08:50 +000086ndk::ScopedAStatus EnvReverbSw::getDescriptor(Descriptor* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000087 LOG(DEBUG) << __func__ << kDescriptor.toString();
88 *_aidl_return = kDescriptor;
89 return ndk::ScopedAStatus::ok();
90}
91
Shunkai Yao812d5b42022-11-16 18:08:50 +000092ndk::ScopedAStatus EnvReverbSw::setParameterSpecific(const Parameter::Specific& specific) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000093 RETURN_IF(Parameter::Specific::environmentalReverb != specific.getTag(), EX_ILLEGAL_ARGUMENT,
Shunkai Yao6afc8552022-10-26 22:47:20 +000094 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000095
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +053096 auto& erParam = specific.get<Parameter::Specific::environmentalReverb>();
97 auto tag = erParam.getTag();
98
99 switch (tag) {
100 case EnvironmentalReverb::roomLevelMb: {
101 RETURN_IF(mContext->setErRoomLevel(erParam.get<EnvironmentalReverb::roomLevelMb>()) !=
102 RetCode::SUCCESS,
103 EX_ILLEGAL_ARGUMENT, "setRoomLevelFailed");
104 return ndk::ScopedAStatus::ok();
105 }
106 case EnvironmentalReverb::roomHfLevelMb: {
107 RETURN_IF(
108 mContext->setErRoomHfLevel(erParam.get<EnvironmentalReverb::roomHfLevelMb>()) !=
109 RetCode::SUCCESS,
110 EX_ILLEGAL_ARGUMENT, "setRoomHfLevelFailed");
111 return ndk::ScopedAStatus::ok();
112 }
113 case EnvironmentalReverb::decayTimeMs: {
114 RETURN_IF(mContext->setErDecayTime(erParam.get<EnvironmentalReverb::decayTimeMs>()) !=
115 RetCode::SUCCESS,
116 EX_ILLEGAL_ARGUMENT, "setDecayTimeFailed");
117 return ndk::ScopedAStatus::ok();
118 }
119 case EnvironmentalReverb::decayHfRatioPm: {
120 RETURN_IF(
121 mContext->setErDecayHfRatio(
122 erParam.get<EnvironmentalReverb::decayHfRatioPm>()) != RetCode::SUCCESS,
123 EX_ILLEGAL_ARGUMENT, "setDecayHfRatioFailed");
124 return ndk::ScopedAStatus::ok();
125 }
126 case EnvironmentalReverb::levelMb: {
127 RETURN_IF(mContext->setErLevel(erParam.get<EnvironmentalReverb::levelMb>()) !=
128 RetCode::SUCCESS,
129 EX_ILLEGAL_ARGUMENT, "setLevelFailed");
130 return ndk::ScopedAStatus::ok();
131 }
132 case EnvironmentalReverb::delayMs: {
133 RETURN_IF(mContext->setErDelay(erParam.get<EnvironmentalReverb::delayMs>()) !=
134 RetCode::SUCCESS,
135 EX_ILLEGAL_ARGUMENT, "setDelayFailed");
136 return ndk::ScopedAStatus::ok();
137 }
138 case EnvironmentalReverb::diffusionPm: {
139 RETURN_IF(mContext->setErDiffusion(erParam.get<EnvironmentalReverb::diffusionPm>()) !=
140 RetCode::SUCCESS,
141 EX_ILLEGAL_ARGUMENT, "setDiffusionFailed");
142 return ndk::ScopedAStatus::ok();
143 }
144 case EnvironmentalReverb::densityPm: {
145 RETURN_IF(mContext->setErDensity(erParam.get<EnvironmentalReverb::densityPm>()) !=
146 RetCode::SUCCESS,
147 EX_ILLEGAL_ARGUMENT, "setDensityFailed");
148 return ndk::ScopedAStatus::ok();
149 }
150 case EnvironmentalReverb::bypass: {
151 RETURN_IF(mContext->setErBypass(erParam.get<EnvironmentalReverb::bypass>()) !=
152 RetCode::SUCCESS,
153 EX_ILLEGAL_ARGUMENT, "setBypassFailed");
154 return ndk::ScopedAStatus::ok();
155 }
156 default: {
157 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
158 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
159 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
160 }
161 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000162}
163
Shunkai Yao812d5b42022-11-16 18:08:50 +0000164ndk::ScopedAStatus EnvReverbSw::getParameterSpecific(const Parameter::Id& id,
165 Parameter::Specific* specific) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000166 auto tag = id.getTag();
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000167 RETURN_IF(Parameter::Id::environmentalReverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530168 auto erId = id.get<Parameter::Id::environmentalReverbTag>();
169 auto erIdTag = erId.getTag();
170 switch (erIdTag) {
171 case EnvironmentalReverb::Id::commonTag:
172 return getParameterEnvironmentalReverb(erId.get<EnvironmentalReverb::Id::commonTag>(),
173 specific);
174 default:
175 LOG(ERROR) << __func__ << " unsupported tag: " << toString(erIdTag);
176 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
177 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
178 }
179}
180
181ndk::ScopedAStatus EnvReverbSw::getParameterEnvironmentalReverb(const EnvironmentalReverb::Tag& tag,
182 Parameter::Specific* specific) {
183 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
184 EnvironmentalReverb erParam;
185 switch (tag) {
186 case EnvironmentalReverb::roomLevelMb: {
187 erParam.set<EnvironmentalReverb::roomLevelMb>(mContext->getErRoomLevel());
188 break;
189 }
190 case EnvironmentalReverb::roomHfLevelMb: {
191 erParam.set<EnvironmentalReverb::roomHfLevelMb>(mContext->getErRoomHfLevel());
192 break;
193 }
194 case EnvironmentalReverb::decayTimeMs: {
195 erParam.set<EnvironmentalReverb::decayTimeMs>(mContext->getErDecayTime());
196 break;
197 }
198 case EnvironmentalReverb::decayHfRatioPm: {
199 erParam.set<EnvironmentalReverb::decayHfRatioPm>(mContext->getErDecayHfRatio());
200 break;
201 }
202 case EnvironmentalReverb::levelMb: {
203 erParam.set<EnvironmentalReverb::levelMb>(mContext->getErLevel());
204 break;
205 }
206 case EnvironmentalReverb::delayMs: {
207 erParam.set<EnvironmentalReverb::delayMs>(mContext->getErDelay());
208 break;
209 }
210 case EnvironmentalReverb::diffusionPm: {
211 erParam.set<EnvironmentalReverb::diffusionPm>(mContext->getErDiffusion());
212 break;
213 }
214 case EnvironmentalReverb::densityPm: {
215 erParam.set<EnvironmentalReverb::densityPm>(mContext->getErDensity());
216 break;
217 }
218 case EnvironmentalReverb::bypass: {
219 erParam.set<EnvironmentalReverb::bypass>(mContext->getErBypass());
220 break;
221 }
222 default: {
223 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
224 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
225 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
226 }
227 }
228
229 specific->set<Parameter::Specific::environmentalReverb>(erParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000230 return ndk::ScopedAStatus::ok();
231}
232
Shunkai Yao812d5b42022-11-16 18:08:50 +0000233std::shared_ptr<EffectContext> EnvReverbSw::createContext(const Parameter::Common& common) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000234 if (mContext) {
235 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530236 } else {
237 mContext = std::make_shared<EnvReverbSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000238 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530239
240 return mContext;
241}
242
243std::shared_ptr<EffectContext> EnvReverbSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000244 return mContext;
245}
246
Shunkai Yao812d5b42022-11-16 18:08:50 +0000247RetCode EnvReverbSw::releaseContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000248 if (mContext) {
249 mContext.reset();
250 }
251 return RetCode::SUCCESS;
252}
253
254// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530255IEffect::Status EnvReverbSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000256 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530257 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
258 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000259 *out++ = *in++;
260 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530261 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000262}
263
Sham Rathode362a462023-01-05 18:46:21 +0530264RetCode EnvReverbSwContext::setErRoomLevel(int roomLevel) {
265 if (roomLevel < EnvReverbSw::kCapability.minRoomLevelMb ||
266 roomLevel > EnvReverbSw::kCapability.maxRoomLevelMb) {
267 LOG(ERROR) << __func__ << " invalid roomLevel: " << roomLevel;
268 return RetCode::ERROR_ILLEGAL_PARAMETER;
269 }
270 // TODO : Add implementation to apply new room level
271 mRoomLevel = roomLevel;
272 return RetCode::SUCCESS;
273}
274
275RetCode EnvReverbSwContext::setErRoomHfLevel(int roomHfLevel) {
276 if (roomHfLevel < EnvReverbSw::kCapability.minRoomHfLevelMb ||
277 roomHfLevel > EnvReverbSw::kCapability.maxRoomHfLevelMb) {
278 LOG(ERROR) << __func__ << " invalid roomHfLevel: " << roomHfLevel;
279 return RetCode::ERROR_ILLEGAL_PARAMETER;
280 }
281 // TODO : Add implementation to apply new room HF level
282 mRoomHfLevel = roomHfLevel;
283 return RetCode::SUCCESS;
284}
285
286RetCode EnvReverbSwContext::setErDecayTime(int decayTime) {
287 if (decayTime < 0 || decayTime > EnvReverbSw::kCapability.maxDecayTimeMs) {
288 LOG(ERROR) << __func__ << " invalid decayTime: " << decayTime;
289 return RetCode::ERROR_ILLEGAL_PARAMETER;
290 }
291 // TODO : Add implementation to apply new decay time
292 mDecayTime = decayTime;
293 return RetCode::SUCCESS;
294}
295
296RetCode EnvReverbSwContext::setErDecayHfRatio(int decayHfRatio) {
297 if (decayHfRatio < EnvReverbSw::kCapability.minDecayHfRatioPm ||
298 decayHfRatio > EnvReverbSw::kCapability.maxDecayHfRatioPm) {
299 LOG(ERROR) << __func__ << " invalid decayHfRatio: " << decayHfRatio;
300 return RetCode::ERROR_ILLEGAL_PARAMETER;
301 }
302 // TODO : Add implementation to apply new decay HF ratio
303 mDecayHfRatio = decayHfRatio;
304 return RetCode::SUCCESS;
305}
306
307RetCode EnvReverbSwContext::setErLevel(int level) {
308 if (level < EnvReverbSw::kCapability.minLevelMb ||
309 level > EnvReverbSw::kCapability.maxLevelMb) {
310 LOG(ERROR) << __func__ << " invalid level: " << level;
311 return RetCode::ERROR_ILLEGAL_PARAMETER;
312 }
313 // TODO : Add implementation to apply new level
314 mLevel = level;
315 return RetCode::SUCCESS;
316}
317
318RetCode EnvReverbSwContext::setErDelay(int delay) {
319 if (delay < 0 || delay > EnvReverbSw::kCapability.maxDelayMs) {
320 LOG(ERROR) << __func__ << " invalid delay: " << delay;
321 return RetCode::ERROR_ILLEGAL_PARAMETER;
322 }
323 // TODO : Add implementation to apply new delay
324 mDelay = delay;
325 return RetCode::SUCCESS;
326}
327
328RetCode EnvReverbSwContext::setErDiffusion(int diffusion) {
329 if (diffusion < 0 || diffusion > EnvReverbSw::kCapability.maxDiffusionPm) {
330 LOG(ERROR) << __func__ << " invalid diffusion: " << diffusion;
331 return RetCode::ERROR_ILLEGAL_PARAMETER;
332 }
333 // TODO : Add implementation to apply new diffusion
334 mDiffusion = diffusion;
335 return RetCode::SUCCESS;
336}
337
338RetCode EnvReverbSwContext::setErDensity(int density) {
339 if (density < 0 || density > EnvReverbSw::kCapability.maxDensityPm) {
340 LOG(ERROR) << __func__ << " invalid density: " << density;
341 return RetCode::ERROR_ILLEGAL_PARAMETER;
342 }
343 // TODO : Add implementation to apply new density
344 mDensity = density;
345 return RetCode::SUCCESS;
346}
347
Shunkai Yao6afc8552022-10-26 22:47:20 +0000348} // namespace aidl::android::hardware::audio::effect