blob: 15373fe2fa9acec936323d373ddc2a984c1565a8 [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";
Shunkai Yao87811022023-02-13 17:40:37 +000063
64const std::vector<Range::EnvironmentalReverbRange> EnvReverbSw::kRanges = {
65 MAKE_RANGE(EnvironmentalReverb, roomLevelMb, -6000, 0),
66 MAKE_RANGE(EnvironmentalReverb, roomHfLevelMb, -4000, 0),
67 MAKE_RANGE(EnvironmentalReverb, decayTimeMs, 0, 7000),
68 MAKE_RANGE(EnvironmentalReverb, decayHfRatioPm, 100, 2000),
Shunkai Yao2ddafc22023-02-21 18:02:10 +000069 MAKE_RANGE(EnvironmentalReverb, reflectionsLevelMb, -6000, 0),
70 MAKE_RANGE(EnvironmentalReverb, reflectionsDelayMs, 0, 65),
Shunkai Yao87811022023-02-13 17:40:37 +000071 MAKE_RANGE(EnvironmentalReverb, levelMb, -6000, 0),
72 MAKE_RANGE(EnvironmentalReverb, delayMs, 0, 65),
73 MAKE_RANGE(EnvironmentalReverb, diffusionPm, 0, 1000),
74 MAKE_RANGE(EnvironmentalReverb, densityPm, 0, 1000)};
75
76const Capability EnvReverbSw::kCapability = {
77 .range = Range::make<Range::environmentalReverb>(EnvReverbSw::kRanges)};
78
Shunkai Yaoc12e0822022-12-12 07:13:58 +000079const Descriptor EnvReverbSw::kDescriptor = {
80 .common = {.id = {.type = kEnvReverbTypeUUID,
81 .uuid = kEnvReverbSwImplUUID,
82 .proxy = std::nullopt},
83 .flags = {.type = Flags::Type::INSERT,
84 .insert = Flags::Insert::FIRST,
85 .volume = Flags::Volume::CTRL},
86 .name = EnvReverbSw::kEffectName,
87 .implementor = "The Android Open Source Project"},
Shunkai Yao87811022023-02-13 17:40:37 +000088 .capability = EnvReverbSw::kCapability};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000089
Shunkai Yao812d5b42022-11-16 18:08:50 +000090ndk::ScopedAStatus EnvReverbSw::getDescriptor(Descriptor* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000091 LOG(DEBUG) << __func__ << kDescriptor.toString();
92 *_aidl_return = kDescriptor;
93 return ndk::ScopedAStatus::ok();
94}
95
Shunkai Yao812d5b42022-11-16 18:08:50 +000096ndk::ScopedAStatus EnvReverbSw::setParameterSpecific(const Parameter::Specific& specific) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000097 RETURN_IF(Parameter::Specific::environmentalReverb != specific.getTag(), EX_ILLEGAL_ARGUMENT,
Shunkai Yao6afc8552022-10-26 22:47:20 +000098 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000099
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530100 auto& erParam = specific.get<Parameter::Specific::environmentalReverb>();
Shunkai Yao87811022023-02-13 17:40:37 +0000101 RETURN_IF(!inRange(erParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530102 auto tag = erParam.getTag();
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530103 switch (tag) {
104 case EnvironmentalReverb::roomLevelMb: {
105 RETURN_IF(mContext->setErRoomLevel(erParam.get<EnvironmentalReverb::roomLevelMb>()) !=
106 RetCode::SUCCESS,
107 EX_ILLEGAL_ARGUMENT, "setRoomLevelFailed");
108 return ndk::ScopedAStatus::ok();
109 }
110 case EnvironmentalReverb::roomHfLevelMb: {
111 RETURN_IF(
112 mContext->setErRoomHfLevel(erParam.get<EnvironmentalReverb::roomHfLevelMb>()) !=
113 RetCode::SUCCESS,
114 EX_ILLEGAL_ARGUMENT, "setRoomHfLevelFailed");
115 return ndk::ScopedAStatus::ok();
116 }
117 case EnvironmentalReverb::decayTimeMs: {
118 RETURN_IF(mContext->setErDecayTime(erParam.get<EnvironmentalReverb::decayTimeMs>()) !=
119 RetCode::SUCCESS,
120 EX_ILLEGAL_ARGUMENT, "setDecayTimeFailed");
121 return ndk::ScopedAStatus::ok();
122 }
123 case EnvironmentalReverb::decayHfRatioPm: {
124 RETURN_IF(
125 mContext->setErDecayHfRatio(
126 erParam.get<EnvironmentalReverb::decayHfRatioPm>()) != RetCode::SUCCESS,
127 EX_ILLEGAL_ARGUMENT, "setDecayHfRatioFailed");
128 return ndk::ScopedAStatus::ok();
129 }
Shunkai Yao2ddafc22023-02-21 18:02:10 +0000130 case EnvironmentalReverb::reflectionsLevelMb: {
131 RETURN_IF(mContext->setErReflectionsLevel(
132 erParam.get<EnvironmentalReverb::reflectionsLevelMb>()) !=
133 RetCode::SUCCESS,
134 EX_ILLEGAL_ARGUMENT, "setReflectionsLevelFailed");
135 return ndk::ScopedAStatus::ok();
136 }
137 case EnvironmentalReverb::reflectionsDelayMs: {
138 RETURN_IF(mContext->setErReflectionsDelay(
139 erParam.get<EnvironmentalReverb::reflectionsDelayMs>()) !=
140 RetCode::SUCCESS,
141 EX_ILLEGAL_ARGUMENT, "setReflectionsDelayFailed");
142 return ndk::ScopedAStatus::ok();
143 }
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530144 case EnvironmentalReverb::levelMb: {
145 RETURN_IF(mContext->setErLevel(erParam.get<EnvironmentalReverb::levelMb>()) !=
146 RetCode::SUCCESS,
147 EX_ILLEGAL_ARGUMENT, "setLevelFailed");
148 return ndk::ScopedAStatus::ok();
149 }
150 case EnvironmentalReverb::delayMs: {
151 RETURN_IF(mContext->setErDelay(erParam.get<EnvironmentalReverb::delayMs>()) !=
152 RetCode::SUCCESS,
153 EX_ILLEGAL_ARGUMENT, "setDelayFailed");
154 return ndk::ScopedAStatus::ok();
155 }
156 case EnvironmentalReverb::diffusionPm: {
157 RETURN_IF(mContext->setErDiffusion(erParam.get<EnvironmentalReverb::diffusionPm>()) !=
158 RetCode::SUCCESS,
159 EX_ILLEGAL_ARGUMENT, "setDiffusionFailed");
160 return ndk::ScopedAStatus::ok();
161 }
162 case EnvironmentalReverb::densityPm: {
163 RETURN_IF(mContext->setErDensity(erParam.get<EnvironmentalReverb::densityPm>()) !=
164 RetCode::SUCCESS,
165 EX_ILLEGAL_ARGUMENT, "setDensityFailed");
166 return ndk::ScopedAStatus::ok();
167 }
168 case EnvironmentalReverb::bypass: {
169 RETURN_IF(mContext->setErBypass(erParam.get<EnvironmentalReverb::bypass>()) !=
170 RetCode::SUCCESS,
171 EX_ILLEGAL_ARGUMENT, "setBypassFailed");
172 return ndk::ScopedAStatus::ok();
173 }
174 default: {
175 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
176 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
177 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
178 }
179 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000180}
181
Shunkai Yao812d5b42022-11-16 18:08:50 +0000182ndk::ScopedAStatus EnvReverbSw::getParameterSpecific(const Parameter::Id& id,
183 Parameter::Specific* specific) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000184 auto tag = id.getTag();
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000185 RETURN_IF(Parameter::Id::environmentalReverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530186 auto erId = id.get<Parameter::Id::environmentalReverbTag>();
187 auto erIdTag = erId.getTag();
188 switch (erIdTag) {
189 case EnvironmentalReverb::Id::commonTag:
190 return getParameterEnvironmentalReverb(erId.get<EnvironmentalReverb::Id::commonTag>(),
191 specific);
192 default:
193 LOG(ERROR) << __func__ << " unsupported tag: " << toString(erIdTag);
194 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
195 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
196 }
197}
198
199ndk::ScopedAStatus EnvReverbSw::getParameterEnvironmentalReverb(const EnvironmentalReverb::Tag& tag,
200 Parameter::Specific* specific) {
201 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
202 EnvironmentalReverb erParam;
203 switch (tag) {
204 case EnvironmentalReverb::roomLevelMb: {
205 erParam.set<EnvironmentalReverb::roomLevelMb>(mContext->getErRoomLevel());
206 break;
207 }
208 case EnvironmentalReverb::roomHfLevelMb: {
209 erParam.set<EnvironmentalReverb::roomHfLevelMb>(mContext->getErRoomHfLevel());
210 break;
211 }
212 case EnvironmentalReverb::decayTimeMs: {
213 erParam.set<EnvironmentalReverb::decayTimeMs>(mContext->getErDecayTime());
214 break;
215 }
216 case EnvironmentalReverb::decayHfRatioPm: {
217 erParam.set<EnvironmentalReverb::decayHfRatioPm>(mContext->getErDecayHfRatio());
218 break;
219 }
Shunkai Yao2ddafc22023-02-21 18:02:10 +0000220 case EnvironmentalReverb::reflectionsLevelMb: {
221 erParam.set<EnvironmentalReverb::reflectionsLevelMb>(mContext->getErReflectionsLevel());
222 break;
223 }
224 case EnvironmentalReverb::reflectionsDelayMs: {
225 erParam.set<EnvironmentalReverb::reflectionsDelayMs>(mContext->getErReflectionsDelay());
226 break;
227 }
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530228 case EnvironmentalReverb::levelMb: {
229 erParam.set<EnvironmentalReverb::levelMb>(mContext->getErLevel());
230 break;
231 }
232 case EnvironmentalReverb::delayMs: {
233 erParam.set<EnvironmentalReverb::delayMs>(mContext->getErDelay());
234 break;
235 }
236 case EnvironmentalReverb::diffusionPm: {
237 erParam.set<EnvironmentalReverb::diffusionPm>(mContext->getErDiffusion());
238 break;
239 }
240 case EnvironmentalReverb::densityPm: {
241 erParam.set<EnvironmentalReverb::densityPm>(mContext->getErDensity());
242 break;
243 }
244 case EnvironmentalReverb::bypass: {
245 erParam.set<EnvironmentalReverb::bypass>(mContext->getErBypass());
246 break;
247 }
248 default: {
249 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
250 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
251 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
252 }
253 }
254
255 specific->set<Parameter::Specific::environmentalReverb>(erParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000256 return ndk::ScopedAStatus::ok();
257}
258
Shunkai Yao812d5b42022-11-16 18:08:50 +0000259std::shared_ptr<EffectContext> EnvReverbSw::createContext(const Parameter::Common& common) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000260 if (mContext) {
261 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530262 } else {
263 mContext = std::make_shared<EnvReverbSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000264 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530265
266 return mContext;
267}
268
269std::shared_ptr<EffectContext> EnvReverbSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000270 return mContext;
271}
272
Shunkai Yao812d5b42022-11-16 18:08:50 +0000273RetCode EnvReverbSw::releaseContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000274 if (mContext) {
275 mContext.reset();
276 }
277 return RetCode::SUCCESS;
278}
279
280// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530281IEffect::Status EnvReverbSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000282 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530283 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
284 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000285 *out++ = *in++;
286 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530287 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000288}
289
Sham Rathode362a462023-01-05 18:46:21 +0530290RetCode EnvReverbSwContext::setErRoomLevel(int roomLevel) {
Sham Rathode362a462023-01-05 18:46:21 +0530291 mRoomLevel = roomLevel;
292 return RetCode::SUCCESS;
293}
294
295RetCode EnvReverbSwContext::setErRoomHfLevel(int roomHfLevel) {
Sham Rathode362a462023-01-05 18:46:21 +0530296 mRoomHfLevel = roomHfLevel;
297 return RetCode::SUCCESS;
298}
299
300RetCode EnvReverbSwContext::setErDecayTime(int decayTime) {
Sham Rathode362a462023-01-05 18:46:21 +0530301 mDecayTime = decayTime;
302 return RetCode::SUCCESS;
303}
304
305RetCode EnvReverbSwContext::setErDecayHfRatio(int decayHfRatio) {
Sham Rathode362a462023-01-05 18:46:21 +0530306 mDecayHfRatio = decayHfRatio;
307 return RetCode::SUCCESS;
308}
309
310RetCode EnvReverbSwContext::setErLevel(int level) {
Sham Rathode362a462023-01-05 18:46:21 +0530311 mLevel = level;
312 return RetCode::SUCCESS;
313}
314
315RetCode EnvReverbSwContext::setErDelay(int delay) {
Sham Rathode362a462023-01-05 18:46:21 +0530316 mDelay = delay;
317 return RetCode::SUCCESS;
318}
319
320RetCode EnvReverbSwContext::setErDiffusion(int diffusion) {
Sham Rathode362a462023-01-05 18:46:21 +0530321 mDiffusion = diffusion;
322 return RetCode::SUCCESS;
323}
324
325RetCode EnvReverbSwContext::setErDensity(int density) {
Sham Rathode362a462023-01-05 18:46:21 +0530326 mDensity = density;
327 return RetCode::SUCCESS;
328}
329
Shunkai Yao6afc8552022-10-26 22:47:20 +0000330} // namespace aidl::android::hardware::audio::effect