blob: 46d9016653121be0238f544d135510c32caa8e2e [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),
69 MAKE_RANGE(EnvironmentalReverb, levelMb, -6000, 0),
70 MAKE_RANGE(EnvironmentalReverb, delayMs, 0, 65),
71 MAKE_RANGE(EnvironmentalReverb, diffusionPm, 0, 1000),
72 MAKE_RANGE(EnvironmentalReverb, densityPm, 0, 1000)};
73
74const Capability EnvReverbSw::kCapability = {
75 .range = Range::make<Range::environmentalReverb>(EnvReverbSw::kRanges)};
76
Shunkai Yaoc12e0822022-12-12 07:13:58 +000077const Descriptor EnvReverbSw::kDescriptor = {
78 .common = {.id = {.type = kEnvReverbTypeUUID,
79 .uuid = kEnvReverbSwImplUUID,
80 .proxy = std::nullopt},
81 .flags = {.type = Flags::Type::INSERT,
82 .insert = Flags::Insert::FIRST,
83 .volume = Flags::Volume::CTRL},
84 .name = EnvReverbSw::kEffectName,
85 .implementor = "The Android Open Source Project"},
Shunkai Yao87811022023-02-13 17:40:37 +000086 .capability = EnvReverbSw::kCapability};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000087
Shunkai Yao812d5b42022-11-16 18:08:50 +000088ndk::ScopedAStatus EnvReverbSw::getDescriptor(Descriptor* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000089 LOG(DEBUG) << __func__ << kDescriptor.toString();
90 *_aidl_return = kDescriptor;
91 return ndk::ScopedAStatus::ok();
92}
93
Shunkai Yao812d5b42022-11-16 18:08:50 +000094ndk::ScopedAStatus EnvReverbSw::setParameterSpecific(const Parameter::Specific& specific) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000095 RETURN_IF(Parameter::Specific::environmentalReverb != specific.getTag(), EX_ILLEGAL_ARGUMENT,
Shunkai Yao6afc8552022-10-26 22:47:20 +000096 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000097
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +053098 auto& erParam = specific.get<Parameter::Specific::environmentalReverb>();
Shunkai Yao87811022023-02-13 17:40:37 +000099 RETURN_IF(!inRange(erParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530100 auto tag = erParam.getTag();
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530101 switch (tag) {
102 case EnvironmentalReverb::roomLevelMb: {
103 RETURN_IF(mContext->setErRoomLevel(erParam.get<EnvironmentalReverb::roomLevelMb>()) !=
104 RetCode::SUCCESS,
105 EX_ILLEGAL_ARGUMENT, "setRoomLevelFailed");
106 return ndk::ScopedAStatus::ok();
107 }
108 case EnvironmentalReverb::roomHfLevelMb: {
109 RETURN_IF(
110 mContext->setErRoomHfLevel(erParam.get<EnvironmentalReverb::roomHfLevelMb>()) !=
111 RetCode::SUCCESS,
112 EX_ILLEGAL_ARGUMENT, "setRoomHfLevelFailed");
113 return ndk::ScopedAStatus::ok();
114 }
115 case EnvironmentalReverb::decayTimeMs: {
116 RETURN_IF(mContext->setErDecayTime(erParam.get<EnvironmentalReverb::decayTimeMs>()) !=
117 RetCode::SUCCESS,
118 EX_ILLEGAL_ARGUMENT, "setDecayTimeFailed");
119 return ndk::ScopedAStatus::ok();
120 }
121 case EnvironmentalReverb::decayHfRatioPm: {
122 RETURN_IF(
123 mContext->setErDecayHfRatio(
124 erParam.get<EnvironmentalReverb::decayHfRatioPm>()) != RetCode::SUCCESS,
125 EX_ILLEGAL_ARGUMENT, "setDecayHfRatioFailed");
126 return ndk::ScopedAStatus::ok();
127 }
128 case EnvironmentalReverb::levelMb: {
129 RETURN_IF(mContext->setErLevel(erParam.get<EnvironmentalReverb::levelMb>()) !=
130 RetCode::SUCCESS,
131 EX_ILLEGAL_ARGUMENT, "setLevelFailed");
132 return ndk::ScopedAStatus::ok();
133 }
134 case EnvironmentalReverb::delayMs: {
135 RETURN_IF(mContext->setErDelay(erParam.get<EnvironmentalReverb::delayMs>()) !=
136 RetCode::SUCCESS,
137 EX_ILLEGAL_ARGUMENT, "setDelayFailed");
138 return ndk::ScopedAStatus::ok();
139 }
140 case EnvironmentalReverb::diffusionPm: {
141 RETURN_IF(mContext->setErDiffusion(erParam.get<EnvironmentalReverb::diffusionPm>()) !=
142 RetCode::SUCCESS,
143 EX_ILLEGAL_ARGUMENT, "setDiffusionFailed");
144 return ndk::ScopedAStatus::ok();
145 }
146 case EnvironmentalReverb::densityPm: {
147 RETURN_IF(mContext->setErDensity(erParam.get<EnvironmentalReverb::densityPm>()) !=
148 RetCode::SUCCESS,
149 EX_ILLEGAL_ARGUMENT, "setDensityFailed");
150 return ndk::ScopedAStatus::ok();
151 }
152 case EnvironmentalReverb::bypass: {
153 RETURN_IF(mContext->setErBypass(erParam.get<EnvironmentalReverb::bypass>()) !=
154 RetCode::SUCCESS,
155 EX_ILLEGAL_ARGUMENT, "setBypassFailed");
156 return ndk::ScopedAStatus::ok();
157 }
158 default: {
159 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
160 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
161 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
162 }
163 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000164}
165
Shunkai Yao812d5b42022-11-16 18:08:50 +0000166ndk::ScopedAStatus EnvReverbSw::getParameterSpecific(const Parameter::Id& id,
167 Parameter::Specific* specific) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000168 auto tag = id.getTag();
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000169 RETURN_IF(Parameter::Id::environmentalReverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530170 auto erId = id.get<Parameter::Id::environmentalReverbTag>();
171 auto erIdTag = erId.getTag();
172 switch (erIdTag) {
173 case EnvironmentalReverb::Id::commonTag:
174 return getParameterEnvironmentalReverb(erId.get<EnvironmentalReverb::Id::commonTag>(),
175 specific);
176 default:
177 LOG(ERROR) << __func__ << " unsupported tag: " << toString(erIdTag);
178 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
179 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
180 }
181}
182
183ndk::ScopedAStatus EnvReverbSw::getParameterEnvironmentalReverb(const EnvironmentalReverb::Tag& tag,
184 Parameter::Specific* specific) {
185 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
186 EnvironmentalReverb erParam;
187 switch (tag) {
188 case EnvironmentalReverb::roomLevelMb: {
189 erParam.set<EnvironmentalReverb::roomLevelMb>(mContext->getErRoomLevel());
190 break;
191 }
192 case EnvironmentalReverb::roomHfLevelMb: {
193 erParam.set<EnvironmentalReverb::roomHfLevelMb>(mContext->getErRoomHfLevel());
194 break;
195 }
196 case EnvironmentalReverb::decayTimeMs: {
197 erParam.set<EnvironmentalReverb::decayTimeMs>(mContext->getErDecayTime());
198 break;
199 }
200 case EnvironmentalReverb::decayHfRatioPm: {
201 erParam.set<EnvironmentalReverb::decayHfRatioPm>(mContext->getErDecayHfRatio());
202 break;
203 }
204 case EnvironmentalReverb::levelMb: {
205 erParam.set<EnvironmentalReverb::levelMb>(mContext->getErLevel());
206 break;
207 }
208 case EnvironmentalReverb::delayMs: {
209 erParam.set<EnvironmentalReverb::delayMs>(mContext->getErDelay());
210 break;
211 }
212 case EnvironmentalReverb::diffusionPm: {
213 erParam.set<EnvironmentalReverb::diffusionPm>(mContext->getErDiffusion());
214 break;
215 }
216 case EnvironmentalReverb::densityPm: {
217 erParam.set<EnvironmentalReverb::densityPm>(mContext->getErDensity());
218 break;
219 }
220 case EnvironmentalReverb::bypass: {
221 erParam.set<EnvironmentalReverb::bypass>(mContext->getErBypass());
222 break;
223 }
224 default: {
225 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
226 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
227 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
228 }
229 }
230
231 specific->set<Parameter::Specific::environmentalReverb>(erParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000232 return ndk::ScopedAStatus::ok();
233}
234
Shunkai Yao812d5b42022-11-16 18:08:50 +0000235std::shared_ptr<EffectContext> EnvReverbSw::createContext(const Parameter::Common& common) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000236 if (mContext) {
237 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530238 } else {
239 mContext = std::make_shared<EnvReverbSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000240 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530241
242 return mContext;
243}
244
245std::shared_ptr<EffectContext> EnvReverbSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000246 return mContext;
247}
248
Shunkai Yao812d5b42022-11-16 18:08:50 +0000249RetCode EnvReverbSw::releaseContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000250 if (mContext) {
251 mContext.reset();
252 }
253 return RetCode::SUCCESS;
254}
255
256// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530257IEffect::Status EnvReverbSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000258 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530259 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
260 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000261 *out++ = *in++;
262 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530263 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000264}
265
Sham Rathode362a462023-01-05 18:46:21 +0530266RetCode EnvReverbSwContext::setErRoomLevel(int roomLevel) {
Sham Rathode362a462023-01-05 18:46:21 +0530267 mRoomLevel = roomLevel;
268 return RetCode::SUCCESS;
269}
270
271RetCode EnvReverbSwContext::setErRoomHfLevel(int roomHfLevel) {
Sham Rathode362a462023-01-05 18:46:21 +0530272 mRoomHfLevel = roomHfLevel;
273 return RetCode::SUCCESS;
274}
275
276RetCode EnvReverbSwContext::setErDecayTime(int decayTime) {
Sham Rathode362a462023-01-05 18:46:21 +0530277 mDecayTime = decayTime;
278 return RetCode::SUCCESS;
279}
280
281RetCode EnvReverbSwContext::setErDecayHfRatio(int decayHfRatio) {
Sham Rathode362a462023-01-05 18:46:21 +0530282 mDecayHfRatio = decayHfRatio;
283 return RetCode::SUCCESS;
284}
285
286RetCode EnvReverbSwContext::setErLevel(int level) {
Sham Rathode362a462023-01-05 18:46:21 +0530287 mLevel = level;
288 return RetCode::SUCCESS;
289}
290
291RetCode EnvReverbSwContext::setErDelay(int delay) {
Sham Rathode362a462023-01-05 18:46:21 +0530292 mDelay = delay;
293 return RetCode::SUCCESS;
294}
295
296RetCode EnvReverbSwContext::setErDiffusion(int diffusion) {
Sham Rathode362a462023-01-05 18:46:21 +0530297 mDiffusion = diffusion;
298 return RetCode::SUCCESS;
299}
300
301RetCode EnvReverbSwContext::setErDensity(int density) {
Sham Rathode362a462023-01-05 18:46:21 +0530302 mDensity = density;
303 return RetCode::SUCCESS;
304}
305
Shunkai Yao6afc8552022-10-26 22:47:20 +0000306} // namespace aidl::android::hardware::audio::effect