1 /*
2 * Copyright (C) 2023 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 <cstdint>
18 #include <cstring>
19 #include <optional>
20 #define LOG_TAG "AidlConversionEnvReverb"
21 //#define LOG_NDEBUG 0
22
23 #include <error/expected_utils.h>
24 #include <media/AidlConversionCppNdk.h>
25 #include <media/AidlConversionNdk.h>
26 #include <media/AidlConversionEffect.h>
27 #include <system/audio_effects/effect_environmentalreverb.h>
28
29 #include <utils/Log.h>
30
31 #include "AidlConversionEnvReverb.h"
32
33 namespace android {
34 namespace effect {
35
36 using ::aidl::android::convertIntegral;
37 using ::aidl::android::getParameterSpecificField;
38 using ::aidl::android::aidl_utils::statusTFromBinderStatus;
39 using ::aidl::android::hardware::audio::effect::EnvironmentalReverb;
40 using ::aidl::android::hardware::audio::effect::Parameter;
41 using ::aidl::android::hardware::audio::effect::VendorExtension;
42 using ::android::status_t;
43 using utils::EffectParamReader;
44 using utils::EffectParamWriter;
45
46 /**
47 * Macro to get a parameter from effect_param_t wrapper and set it to AIDL effect.
48 *
49 * Return if there is any error, otherwise continue execution.
50 *
51 * @param param EffectParamReader, a reader wrapper of effect_param_t.
52 * @param aidlType Type of the AIDL parameter field, used to construct AIDL Parameter union.
53 * @param valueType Type of the value get from effect_param_t.
54 * @param tag The AIDL parameter union field tag.
55 */
56 #define SET_AIDL_PARAMETER(param, aidlType, valueType, tag) \
57 { \
58 Parameter aidlParam; \
59 valueType value; \
60 if (status_t status = param.readFromValue(&value); status != OK) { \
61 ALOGE("%s %s read from parameter failed, ret %d", __func__, #tag, status); \
62 return status; \
63 } \
64 aidlParam = MAKE_SPECIFIC_PARAMETER( \
65 EnvironmentalReverb, environmentalReverb, tag, \
66 VALUE_OR_RETURN_STATUS(aidl::android::convertIntegral<aidlType>(value))); \
67 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->setParameter(aidlParam))); \
68 }
69
70 /**
71 * Macro to get a parameter from AIDL effect and write the value to effect_param_t with wrapper.
72 *
73 * Return if there is any error, otherwise continue execution.
74 *
75 * @param param EffectParamWriter, a writer wrapper of effect_param_t.
76 * @param aidlType Type of the AIDL parameter field, used to construct AIDL Parameter union.
77 * @param valueType Type of the value get from effect_param_t.
78 * @param tag The AIDL parameter union field tag.
79 */
80 #define GET_AIDL_PARAMETER(param, aidltype, valueType, tag) \
81 { \
82 aidltype value; \
83 Parameter aidlParam; \
84 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(EnvironmentalReverb, environmentalReverbTag, \
85 EnvironmentalReverb::tag); \
86 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); \
87 value = VALUE_OR_RETURN_STATUS( \
88 GET_PARAMETER_SPECIFIC_FIELD(aidlParam, EnvironmentalReverb, environmentalReverb, \
89 EnvironmentalReverb::tag, std::decay_t<aidltype>)); \
90 if (status_t status = param.writeToValue((valueType*)&value); status != OK) { \
91 param.setStatus(status); \
92 ALOGE("%s %s write to parameter failed %d, ret %d", __func__, #tag, value, status); \
93 return status; \
94 } \
95 }
96
setParameter(EffectParamReader & param)97 status_t AidlConversionEnvReverb::setParameter(EffectParamReader& param) {
98 uint32_t type = 0;
99 if (status_t status = param.readFromParameter(&type); status != OK) {
100 ALOGE("%s failed to read type from %s, ret %d", __func__, param.toString().c_str(), status);
101 return BAD_VALUE;
102 }
103
104 switch (type) {
105 case REVERB_PARAM_ROOM_LEVEL: {
106 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
107 break;
108 }
109 case REVERB_PARAM_ROOM_HF_LEVEL: {
110 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
111 break;
112 }
113 case REVERB_PARAM_DECAY_TIME: {
114 SET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
115 break;
116 }
117 case REVERB_PARAM_DECAY_HF_RATIO: {
118 SET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
119 break;
120 }
121 case REVERB_PARAM_REFLECTIONS_LEVEL: {
122 SET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
123 break;
124 }
125 case REVERB_PARAM_REFLECTIONS_DELAY: {
126 SET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
127 break;
128 }
129 case REVERB_PARAM_REVERB_LEVEL: {
130 SET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
131 break;
132 }
133 case REVERB_PARAM_REVERB_DELAY: {
134 SET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
135 break;
136 }
137 case REVERB_PARAM_DIFFUSION: {
138 SET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
139 break;
140 }
141 case REVERB_PARAM_DENSITY: {
142 SET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
143 break;
144 }
145 case REVERB_PARAM_BYPASS: {
146 SET_AIDL_PARAMETER(param, bool, int32_t, bypass);
147 break;
148 }
149 case REVERB_PARAM_PROPERTIES: {
150 if (sizeof(t_reverb_settings) > param.getValueSize()) {
151 ALOGE("%s vsize %zu less than t_reverb_settings size %zu", __func__,
152 param.getValueSize(), sizeof(t_reverb_settings));
153 return BAD_VALUE;
154 }
155 // this sequency needs to be aligned with t_reverb_settings
156 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
157 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
158 SET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
159 SET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
160 SET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
161 SET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
162 SET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
163 SET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
164 SET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
165 SET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
166 break;
167 }
168 default: {
169 // for vendor extension, copy data area to the DefaultExtension, parameter ignored
170 VendorExtension ext = VALUE_OR_RETURN_STATUS(
171 aidl::android::legacy2aidl_EffectParameterReader_VendorExtension(param));
172 Parameter aidlParam = MAKE_SPECIFIC_PARAMETER(EnvironmentalReverb,
173 environmentalReverb, vendor, ext);
174 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->setParameter(aidlParam)));
175 break;
176 }
177 }
178 return OK;
179 }
180
getParameter(EffectParamWriter & param)181 status_t AidlConversionEnvReverb::getParameter(EffectParamWriter& param) {
182 uint32_t type = 0;
183 if (status_t status = param.readFromParameter(&type); status != OK) {
184 ALOGE("%s failed to read type from %s", __func__, param.toString().c_str());
185 param.setStatus(status);
186 return status;
187 }
188
189 switch (type) {
190 case REVERB_PARAM_ROOM_LEVEL: {
191 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
192 break;
193 }
194 case REVERB_PARAM_ROOM_HF_LEVEL: {
195 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
196 break;
197 }
198 case REVERB_PARAM_DECAY_TIME: {
199 GET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
200 break;
201 }
202 case REVERB_PARAM_DECAY_HF_RATIO: {
203 GET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
204 break;
205 }
206 case REVERB_PARAM_REFLECTIONS_LEVEL: {
207 GET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
208 break;
209 }
210 case REVERB_PARAM_REFLECTIONS_DELAY: {
211 GET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
212 break;
213 }
214 case REVERB_PARAM_REVERB_LEVEL: {
215 GET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
216 break;
217 }
218 case REVERB_PARAM_REVERB_DELAY: {
219 GET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
220 break;
221 }
222 case REVERB_PARAM_DIFFUSION: {
223 GET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
224 break;
225 }
226 case REVERB_PARAM_DENSITY: {
227 GET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
228 break;
229 }
230 case REVERB_PARAM_BYPASS: {
231 GET_AIDL_PARAMETER(param, bool, int32_t, bypass);
232 break;
233 }
234 case REVERB_PARAM_PROPERTIES: {
235 // this sequency needs to be aligned with t_reverb_settings
236 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
237 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
238 GET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
239 GET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
240 GET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
241 GET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
242 GET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
243 GET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
244 GET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
245 GET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
246 break;
247 }
248 default: {
249 VENDOR_EXTENSION_GET_AND_RETURN(EnvironmentalReverb, environmentalReverb, param);
250 }
251 }
252 return OK;
253 }
254
255 } // namespace effect
256 } // namespace android
257