blob: 29288cacf35b9fe89a3125161c2a795374ec4023 [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
Shunkai Yao6afc8552022-10-26 22:47:20 +000017#include <algorithm>
Mikhail Naganov872d4a62023-03-09 18:19:01 -080018#include <cstddef>
Shunkai Yao6afc8552022-10-26 22:47:20 +000019#include <unordered_set>
20
Mikhail Naganov872d4a62023-03-09 18:19:01 -080021#define LOG_TAG "AHAL_EnvReverbSw"
Shunkai Yao6afc8552022-10-26 22:47:20 +000022#include <android-base/logging.h>
23#include <fmq/AidlMessageQueue.h>
24
Shunkai Yao812d5b42022-11-16 18:08:50 +000025#include "EnvReverbSw.h"
Shunkai Yao6afc8552022-10-26 22:47:20 +000026
Shunkai Yaoc12e0822022-12-12 07:13:58 +000027using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yao812d5b42022-11-16 18:08:50 +000028using aidl::android::hardware::audio::effect::EnvReverbSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000029using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000030using aidl::android::hardware::audio::effect::kEnvReverbSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000031using aidl::android::hardware::audio::effect::State;
32using aidl::android::media::audio::common::AudioUuid;
33
34extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
35 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000036 if (!in_impl_uuid || *in_impl_uuid != kEnvReverbSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000037 LOG(ERROR) << __func__ << "uuid not supported";
38 return EX_ILLEGAL_ARGUMENT;
39 }
40 if (instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000041 *instanceSpp = ndk::SharedRefBase::make<EnvReverbSw>();
Shunkai Yao6afc8552022-10-26 22:47:20 +000042 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
43 return EX_NONE;
44 } else {
45 LOG(ERROR) << __func__ << " invalid input parameter!";
46 return EX_ILLEGAL_ARGUMENT;
47 }
48}
49
Shunkai Yaoc12e0822022-12-12 07:13:58 +000050extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
51 if (!in_impl_uuid || *in_impl_uuid != kEnvReverbSwImplUUID) {
52 LOG(ERROR) << __func__ << "uuid not supported";
53 return EX_ILLEGAL_ARGUMENT;
Shunkai Yao6afc8552022-10-26 22:47:20 +000054 }
Shunkai Yaoc12e0822022-12-12 07:13:58 +000055 *_aidl_return = EnvReverbSw::kDescriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000056 return EX_NONE;
57}
58
59namespace aidl::android::hardware::audio::effect {
60
Shunkai Yaoc12e0822022-12-12 07:13:58 +000061const std::string EnvReverbSw::kEffectName = "EnvReverbSw";
Shunkai Yao87811022023-02-13 17:40:37 +000062
63const std::vector<Range::EnvironmentalReverbRange> EnvReverbSw::kRanges = {
64 MAKE_RANGE(EnvironmentalReverb, roomLevelMb, -6000, 0),
65 MAKE_RANGE(EnvironmentalReverb, roomHfLevelMb, -4000, 0),
66 MAKE_RANGE(EnvironmentalReverb, decayTimeMs, 0, 7000),
67 MAKE_RANGE(EnvironmentalReverb, decayHfRatioPm, 100, 2000),
Shunkai Yao2ddafc22023-02-21 18:02:10 +000068 MAKE_RANGE(EnvironmentalReverb, reflectionsLevelMb, -6000, 0),
69 MAKE_RANGE(EnvironmentalReverb, reflectionsDelayMs, 0, 65),
Shunkai Yao87811022023-02-13 17:40:37 +000070 MAKE_RANGE(EnvironmentalReverb, levelMb, -6000, 0),
71 MAKE_RANGE(EnvironmentalReverb, delayMs, 0, 65),
72 MAKE_RANGE(EnvironmentalReverb, diffusionPm, 0, 1000),
73 MAKE_RANGE(EnvironmentalReverb, densityPm, 0, 1000)};
74
75const Capability EnvReverbSw::kCapability = {
76 .range = Range::make<Range::environmentalReverb>(EnvReverbSw::kRanges)};
77
Shunkai Yaoc12e0822022-12-12 07:13:58 +000078const Descriptor EnvReverbSw::kDescriptor = {
79 .common = {.id = {.type = kEnvReverbTypeUUID,
80 .uuid = kEnvReverbSwImplUUID,
81 .proxy = std::nullopt},
82 .flags = {.type = Flags::Type::INSERT,
83 .insert = Flags::Insert::FIRST,
84 .volume = Flags::Volume::CTRL},
85 .name = EnvReverbSw::kEffectName,
86 .implementor = "The Android Open Source Project"},
Shunkai Yao87811022023-02-13 17:40:37 +000087 .capability = EnvReverbSw::kCapability};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000088
Shunkai Yao812d5b42022-11-16 18:08:50 +000089ndk::ScopedAStatus EnvReverbSw::getDescriptor(Descriptor* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000090 LOG(DEBUG) << __func__ << kDescriptor.toString();
91 *_aidl_return = kDescriptor;
92 return ndk::ScopedAStatus::ok();
93}
94
Shunkai Yao812d5b42022-11-16 18:08:50 +000095ndk::ScopedAStatus EnvReverbSw::setParameterSpecific(const Parameter::Specific& specific) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000096 RETURN_IF(Parameter::Specific::environmentalReverb != specific.getTag(), EX_ILLEGAL_ARGUMENT,
Shunkai Yao6afc8552022-10-26 22:47:20 +000097 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000098
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +053099 auto& erParam = specific.get<Parameter::Specific::environmentalReverb>();
Shunkai Yao87811022023-02-13 17:40:37 +0000100 RETURN_IF(!inRange(erParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530101 auto tag = erParam.getTag();
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530102 switch (tag) {
103 case EnvironmentalReverb::roomLevelMb: {
104 RETURN_IF(mContext->setErRoomLevel(erParam.get<EnvironmentalReverb::roomLevelMb>()) !=
105 RetCode::SUCCESS,
106 EX_ILLEGAL_ARGUMENT, "setRoomLevelFailed");
107 return ndk::ScopedAStatus::ok();
108 }
109 case EnvironmentalReverb::roomHfLevelMb: {
110 RETURN_IF(
111 mContext->setErRoomHfLevel(erParam.get<EnvironmentalReverb::roomHfLevelMb>()) !=
112 RetCode::SUCCESS,
113 EX_ILLEGAL_ARGUMENT, "setRoomHfLevelFailed");
114 return ndk::ScopedAStatus::ok();
115 }
116 case EnvironmentalReverb::decayTimeMs: {
117 RETURN_IF(mContext->setErDecayTime(erParam.get<EnvironmentalReverb::decayTimeMs>()) !=
118 RetCode::SUCCESS,
119 EX_ILLEGAL_ARGUMENT, "setDecayTimeFailed");
120 return ndk::ScopedAStatus::ok();
121 }
122 case EnvironmentalReverb::decayHfRatioPm: {
123 RETURN_IF(
124 mContext->setErDecayHfRatio(
125 erParam.get<EnvironmentalReverb::decayHfRatioPm>()) != RetCode::SUCCESS,
126 EX_ILLEGAL_ARGUMENT, "setDecayHfRatioFailed");
127 return ndk::ScopedAStatus::ok();
128 }
Shunkai Yao2ddafc22023-02-21 18:02:10 +0000129 case EnvironmentalReverb::reflectionsLevelMb: {
130 RETURN_IF(mContext->setErReflectionsLevel(
131 erParam.get<EnvironmentalReverb::reflectionsLevelMb>()) !=
132 RetCode::SUCCESS,
133 EX_ILLEGAL_ARGUMENT, "setReflectionsLevelFailed");
134 return ndk::ScopedAStatus::ok();
135 }
136 case EnvironmentalReverb::reflectionsDelayMs: {
137 RETURN_IF(mContext->setErReflectionsDelay(
138 erParam.get<EnvironmentalReverb::reflectionsDelayMs>()) !=
139 RetCode::SUCCESS,
140 EX_ILLEGAL_ARGUMENT, "setReflectionsDelayFailed");
141 return ndk::ScopedAStatus::ok();
142 }
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530143 case EnvironmentalReverb::levelMb: {
144 RETURN_IF(mContext->setErLevel(erParam.get<EnvironmentalReverb::levelMb>()) !=
145 RetCode::SUCCESS,
146 EX_ILLEGAL_ARGUMENT, "setLevelFailed");
147 return ndk::ScopedAStatus::ok();
148 }
149 case EnvironmentalReverb::delayMs: {
150 RETURN_IF(mContext->setErDelay(erParam.get<EnvironmentalReverb::delayMs>()) !=
151 RetCode::SUCCESS,
152 EX_ILLEGAL_ARGUMENT, "setDelayFailed");
153 return ndk::ScopedAStatus::ok();
154 }
155 case EnvironmentalReverb::diffusionPm: {
156 RETURN_IF(mContext->setErDiffusion(erParam.get<EnvironmentalReverb::diffusionPm>()) !=
157 RetCode::SUCCESS,
158 EX_ILLEGAL_ARGUMENT, "setDiffusionFailed");
159 return ndk::ScopedAStatus::ok();
160 }
161 case EnvironmentalReverb::densityPm: {
162 RETURN_IF(mContext->setErDensity(erParam.get<EnvironmentalReverb::densityPm>()) !=
163 RetCode::SUCCESS,
164 EX_ILLEGAL_ARGUMENT, "setDensityFailed");
165 return ndk::ScopedAStatus::ok();
166 }
167 case EnvironmentalReverb::bypass: {
168 RETURN_IF(mContext->setErBypass(erParam.get<EnvironmentalReverb::bypass>()) !=
169 RetCode::SUCCESS,
170 EX_ILLEGAL_ARGUMENT, "setBypassFailed");
171 return ndk::ScopedAStatus::ok();
172 }
173 default: {
174 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
175 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
176 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
177 }
178 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000179}
180
Shunkai Yao812d5b42022-11-16 18:08:50 +0000181ndk::ScopedAStatus EnvReverbSw::getParameterSpecific(const Parameter::Id& id,
182 Parameter::Specific* specific) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000183 auto tag = id.getTag();
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000184 RETURN_IF(Parameter::Id::environmentalReverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530185 auto erId = id.get<Parameter::Id::environmentalReverbTag>();
186 auto erIdTag = erId.getTag();
187 switch (erIdTag) {
188 case EnvironmentalReverb::Id::commonTag:
189 return getParameterEnvironmentalReverb(erId.get<EnvironmentalReverb::Id::commonTag>(),
190 specific);
191 default:
192 LOG(ERROR) << __func__ << " unsupported tag: " << toString(erIdTag);
193 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
194 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
195 }
196}
197
198ndk::ScopedAStatus EnvReverbSw::getParameterEnvironmentalReverb(const EnvironmentalReverb::Tag& tag,
199 Parameter::Specific* specific) {
200 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
201 EnvironmentalReverb erParam;
202 switch (tag) {
203 case EnvironmentalReverb::roomLevelMb: {
204 erParam.set<EnvironmentalReverb::roomLevelMb>(mContext->getErRoomLevel());
205 break;
206 }
207 case EnvironmentalReverb::roomHfLevelMb: {
208 erParam.set<EnvironmentalReverb::roomHfLevelMb>(mContext->getErRoomHfLevel());
209 break;
210 }
211 case EnvironmentalReverb::decayTimeMs: {
212 erParam.set<EnvironmentalReverb::decayTimeMs>(mContext->getErDecayTime());
213 break;
214 }
215 case EnvironmentalReverb::decayHfRatioPm: {
216 erParam.set<EnvironmentalReverb::decayHfRatioPm>(mContext->getErDecayHfRatio());
217 break;
218 }
Shunkai Yao2ddafc22023-02-21 18:02:10 +0000219 case EnvironmentalReverb::reflectionsLevelMb: {
220 erParam.set<EnvironmentalReverb::reflectionsLevelMb>(mContext->getErReflectionsLevel());
221 break;
222 }
223 case EnvironmentalReverb::reflectionsDelayMs: {
224 erParam.set<EnvironmentalReverb::reflectionsDelayMs>(mContext->getErReflectionsDelay());
225 break;
226 }
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530227 case EnvironmentalReverb::levelMb: {
228 erParam.set<EnvironmentalReverb::levelMb>(mContext->getErLevel());
229 break;
230 }
231 case EnvironmentalReverb::delayMs: {
232 erParam.set<EnvironmentalReverb::delayMs>(mContext->getErDelay());
233 break;
234 }
235 case EnvironmentalReverb::diffusionPm: {
236 erParam.set<EnvironmentalReverb::diffusionPm>(mContext->getErDiffusion());
237 break;
238 }
239 case EnvironmentalReverb::densityPm: {
240 erParam.set<EnvironmentalReverb::densityPm>(mContext->getErDensity());
241 break;
242 }
243 case EnvironmentalReverb::bypass: {
244 erParam.set<EnvironmentalReverb::bypass>(mContext->getErBypass());
245 break;
246 }
247 default: {
248 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
249 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
250 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
251 }
252 }
253
254 specific->set<Parameter::Specific::environmentalReverb>(erParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000255 return ndk::ScopedAStatus::ok();
256}
257
Shunkai Yao812d5b42022-11-16 18:08:50 +0000258std::shared_ptr<EffectContext> EnvReverbSw::createContext(const Parameter::Common& common) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000259 if (mContext) {
260 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530261 } else {
262 mContext = std::make_shared<EnvReverbSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000263 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530264
265 return mContext;
266}
267
268std::shared_ptr<EffectContext> EnvReverbSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000269 return mContext;
270}
271
Shunkai Yao812d5b42022-11-16 18:08:50 +0000272RetCode EnvReverbSw::releaseContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000273 if (mContext) {
274 mContext.reset();
275 }
276 return RetCode::SUCCESS;
277}
278
279// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530280IEffect::Status EnvReverbSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000281 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530282 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
283 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000284 *out++ = *in++;
285 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530286 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000287}
288
Sham Rathode362a462023-01-05 18:46:21 +0530289RetCode EnvReverbSwContext::setErRoomLevel(int roomLevel) {
Sham Rathode362a462023-01-05 18:46:21 +0530290 mRoomLevel = roomLevel;
291 return RetCode::SUCCESS;
292}
293
294RetCode EnvReverbSwContext::setErRoomHfLevel(int roomHfLevel) {
Sham Rathode362a462023-01-05 18:46:21 +0530295 mRoomHfLevel = roomHfLevel;
296 return RetCode::SUCCESS;
297}
298
299RetCode EnvReverbSwContext::setErDecayTime(int decayTime) {
Sham Rathode362a462023-01-05 18:46:21 +0530300 mDecayTime = decayTime;
301 return RetCode::SUCCESS;
302}
303
304RetCode EnvReverbSwContext::setErDecayHfRatio(int decayHfRatio) {
Sham Rathode362a462023-01-05 18:46:21 +0530305 mDecayHfRatio = decayHfRatio;
306 return RetCode::SUCCESS;
307}
308
309RetCode EnvReverbSwContext::setErLevel(int level) {
Sham Rathode362a462023-01-05 18:46:21 +0530310 mLevel = level;
311 return RetCode::SUCCESS;
312}
313
314RetCode EnvReverbSwContext::setErDelay(int delay) {
Sham Rathode362a462023-01-05 18:46:21 +0530315 mDelay = delay;
316 return RetCode::SUCCESS;
317}
318
319RetCode EnvReverbSwContext::setErDiffusion(int diffusion) {
Sham Rathode362a462023-01-05 18:46:21 +0530320 mDiffusion = diffusion;
321 return RetCode::SUCCESS;
322}
323
324RetCode EnvReverbSwContext::setErDensity(int density) {
Sham Rathode362a462023-01-05 18:46:21 +0530325 mDensity = density;
326 return RetCode::SUCCESS;
327}
328
Shunkai Yao6afc8552022-10-26 22:47:20 +0000329} // namespace aidl::android::hardware::audio::effect