blob: 9d7159a4525e3f118e3171fc79a8e528ea8002f9 [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";
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +053063const EnvironmentalReverb::Capability EnvReverbSw::kCapability = {
64 .maxDecayTimeMs = EnvironmentalReverb::MAX_DECAY_TIME_MS};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000065const Descriptor EnvReverbSw::kDescriptor = {
66 .common = {.id = {.type = kEnvReverbTypeUUID,
67 .uuid = kEnvReverbSwImplUUID,
68 .proxy = std::nullopt},
69 .flags = {.type = Flags::Type::INSERT,
70 .insert = Flags::Insert::FIRST,
71 .volume = Flags::Volume::CTRL},
72 .name = EnvReverbSw::kEffectName,
73 .implementor = "The Android Open Source Project"},
74 .capability = Capability::make<Capability::environmentalReverb>(EnvReverbSw::kCapability)};
75
Shunkai Yao812d5b42022-11-16 18:08:50 +000076ndk::ScopedAStatus EnvReverbSw::getDescriptor(Descriptor* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000077 LOG(DEBUG) << __func__ << kDescriptor.toString();
78 *_aidl_return = kDescriptor;
79 return ndk::ScopedAStatus::ok();
80}
81
Shunkai Yao812d5b42022-11-16 18:08:50 +000082ndk::ScopedAStatus EnvReverbSw::setParameterSpecific(const Parameter::Specific& specific) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000083 RETURN_IF(Parameter::Specific::environmentalReverb != specific.getTag(), EX_ILLEGAL_ARGUMENT,
Shunkai Yao6afc8552022-10-26 22:47:20 +000084 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000085
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +053086 auto& erParam = specific.get<Parameter::Specific::environmentalReverb>();
87 auto tag = erParam.getTag();
88
89 switch (tag) {
90 case EnvironmentalReverb::roomLevelMb: {
91 RETURN_IF(mContext->setErRoomLevel(erParam.get<EnvironmentalReverb::roomLevelMb>()) !=
92 RetCode::SUCCESS,
93 EX_ILLEGAL_ARGUMENT, "setRoomLevelFailed");
94 return ndk::ScopedAStatus::ok();
95 }
96 case EnvironmentalReverb::roomHfLevelMb: {
97 RETURN_IF(
98 mContext->setErRoomHfLevel(erParam.get<EnvironmentalReverb::roomHfLevelMb>()) !=
99 RetCode::SUCCESS,
100 EX_ILLEGAL_ARGUMENT, "setRoomHfLevelFailed");
101 return ndk::ScopedAStatus::ok();
102 }
103 case EnvironmentalReverb::decayTimeMs: {
104 RETURN_IF(mContext->setErDecayTime(erParam.get<EnvironmentalReverb::decayTimeMs>()) !=
105 RetCode::SUCCESS,
106 EX_ILLEGAL_ARGUMENT, "setDecayTimeFailed");
107 return ndk::ScopedAStatus::ok();
108 }
109 case EnvironmentalReverb::decayHfRatioPm: {
110 RETURN_IF(
111 mContext->setErDecayHfRatio(
112 erParam.get<EnvironmentalReverb::decayHfRatioPm>()) != RetCode::SUCCESS,
113 EX_ILLEGAL_ARGUMENT, "setDecayHfRatioFailed");
114 return ndk::ScopedAStatus::ok();
115 }
116 case EnvironmentalReverb::levelMb: {
117 RETURN_IF(mContext->setErLevel(erParam.get<EnvironmentalReverb::levelMb>()) !=
118 RetCode::SUCCESS,
119 EX_ILLEGAL_ARGUMENT, "setLevelFailed");
120 return ndk::ScopedAStatus::ok();
121 }
122 case EnvironmentalReverb::delayMs: {
123 RETURN_IF(mContext->setErDelay(erParam.get<EnvironmentalReverb::delayMs>()) !=
124 RetCode::SUCCESS,
125 EX_ILLEGAL_ARGUMENT, "setDelayFailed");
126 return ndk::ScopedAStatus::ok();
127 }
128 case EnvironmentalReverb::diffusionPm: {
129 RETURN_IF(mContext->setErDiffusion(erParam.get<EnvironmentalReverb::diffusionPm>()) !=
130 RetCode::SUCCESS,
131 EX_ILLEGAL_ARGUMENT, "setDiffusionFailed");
132 return ndk::ScopedAStatus::ok();
133 }
134 case EnvironmentalReverb::densityPm: {
135 RETURN_IF(mContext->setErDensity(erParam.get<EnvironmentalReverb::densityPm>()) !=
136 RetCode::SUCCESS,
137 EX_ILLEGAL_ARGUMENT, "setDensityFailed");
138 return ndk::ScopedAStatus::ok();
139 }
140 case EnvironmentalReverb::bypass: {
141 RETURN_IF(mContext->setErBypass(erParam.get<EnvironmentalReverb::bypass>()) !=
142 RetCode::SUCCESS,
143 EX_ILLEGAL_ARGUMENT, "setBypassFailed");
144 return ndk::ScopedAStatus::ok();
145 }
146 default: {
147 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
148 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
149 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
150 }
151 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000152}
153
Shunkai Yao812d5b42022-11-16 18:08:50 +0000154ndk::ScopedAStatus EnvReverbSw::getParameterSpecific(const Parameter::Id& id,
155 Parameter::Specific* specific) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000156 auto tag = id.getTag();
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000157 RETURN_IF(Parameter::Id::environmentalReverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Shraddha Basantwanidbb0ed62022-11-17 20:32:18 +0530158 auto erId = id.get<Parameter::Id::environmentalReverbTag>();
159 auto erIdTag = erId.getTag();
160 switch (erIdTag) {
161 case EnvironmentalReverb::Id::commonTag:
162 return getParameterEnvironmentalReverb(erId.get<EnvironmentalReverb::Id::commonTag>(),
163 specific);
164 default:
165 LOG(ERROR) << __func__ << " unsupported tag: " << toString(erIdTag);
166 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
167 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
168 }
169}
170
171ndk::ScopedAStatus EnvReverbSw::getParameterEnvironmentalReverb(const EnvironmentalReverb::Tag& tag,
172 Parameter::Specific* specific) {
173 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
174 EnvironmentalReverb erParam;
175 switch (tag) {
176 case EnvironmentalReverb::roomLevelMb: {
177 erParam.set<EnvironmentalReverb::roomLevelMb>(mContext->getErRoomLevel());
178 break;
179 }
180 case EnvironmentalReverb::roomHfLevelMb: {
181 erParam.set<EnvironmentalReverb::roomHfLevelMb>(mContext->getErRoomHfLevel());
182 break;
183 }
184 case EnvironmentalReverb::decayTimeMs: {
185 erParam.set<EnvironmentalReverb::decayTimeMs>(mContext->getErDecayTime());
186 break;
187 }
188 case EnvironmentalReverb::decayHfRatioPm: {
189 erParam.set<EnvironmentalReverb::decayHfRatioPm>(mContext->getErDecayHfRatio());
190 break;
191 }
192 case EnvironmentalReverb::levelMb: {
193 erParam.set<EnvironmentalReverb::levelMb>(mContext->getErLevel());
194 break;
195 }
196 case EnvironmentalReverb::delayMs: {
197 erParam.set<EnvironmentalReverb::delayMs>(mContext->getErDelay());
198 break;
199 }
200 case EnvironmentalReverb::diffusionPm: {
201 erParam.set<EnvironmentalReverb::diffusionPm>(mContext->getErDiffusion());
202 break;
203 }
204 case EnvironmentalReverb::densityPm: {
205 erParam.set<EnvironmentalReverb::densityPm>(mContext->getErDensity());
206 break;
207 }
208 case EnvironmentalReverb::bypass: {
209 erParam.set<EnvironmentalReverb::bypass>(mContext->getErBypass());
210 break;
211 }
212 default: {
213 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
214 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
215 EX_ILLEGAL_ARGUMENT, "EnvironmentalReverbTagNotSupported");
216 }
217 }
218
219 specific->set<Parameter::Specific::environmentalReverb>(erParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000220 return ndk::ScopedAStatus::ok();
221}
222
Shunkai Yao812d5b42022-11-16 18:08:50 +0000223std::shared_ptr<EffectContext> EnvReverbSw::createContext(const Parameter::Common& common) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000224 if (mContext) {
225 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530226 } else {
227 mContext = std::make_shared<EnvReverbSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000228 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530229
230 return mContext;
231}
232
233std::shared_ptr<EffectContext> EnvReverbSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000234 return mContext;
235}
236
Shunkai Yao812d5b42022-11-16 18:08:50 +0000237RetCode EnvReverbSw::releaseContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000238 if (mContext) {
239 mContext.reset();
240 }
241 return RetCode::SUCCESS;
242}
243
244// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530245IEffect::Status EnvReverbSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000246 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530247 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
248 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000249 *out++ = *in++;
250 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530251 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000252}
253
254} // namespace aidl::android::hardware::audio::effect