1 /*
2 * Copyright (C) 2013 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 #define LOG_TAG "EffectLE"
18 //#define LOG_NDEBUG 0
19
20 #include <assert.h>
21 #include <math.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25
26 #include <new>
27
28 #include <log/log.h>
29
30 #include <audio_effects/effect_loudnessenhancer.h>
31 #include "dsp/core/dynamic_range_compression.h"
32
33 static constexpr audio_format_t kProcessFormat = AUDIO_FORMAT_PCM_FLOAT;
34
35 extern "C" {
36
37 // effect_handle_t interface implementation for LE effect
38 extern const struct effect_interface_s gLEInterface;
39
40 // AOSP Loudness Enhancer UUID: fa415329-2034-4bea-b5dc-5b381c8d1e2c
41 const effect_descriptor_t gLEDescriptor = {
42 {0xfe3199be, 0xaed0, 0x413f, 0x87bb, {0x11, 0x26, 0x0e, 0xb6, 0x3c, 0xf1}}, // type
43 {0xfa415329, 0x2034, 0x4bea, 0xb5dc, {0x5b, 0x38, 0x1c, 0x8d, 0x1e, 0x2c}}, // uuid
44 EFFECT_CONTROL_API_VERSION,
45 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST),
46 0, // TODO
47 1,
48 "Loudness Enhancer",
49 "The Android Open Source Project",
50 };
51
52 enum le_state_e {
53 LOUDNESS_ENHANCER_STATE_UNINITIALIZED,
54 LOUDNESS_ENHANCER_STATE_INITIALIZED,
55 LOUDNESS_ENHANCER_STATE_ACTIVE,
56 };
57
58 struct LoudnessEnhancerContext {
59 const struct effect_interface_s *mItfe;
60 effect_config_t mConfig;
61 uint8_t mState;
62 int32_t mTargetGainmB;// target gain in mB
63 // in this implementation, there is no coupling between the compression on the left and right
64 // channels
65 le_fx::AdaptiveDynamicRangeCompression* mCompressor;
66 };
67
68 //
69 //--- Local functions (not directly used by effect interface)
70 //
71
LE_reset(LoudnessEnhancerContext * pContext)72 void LE_reset(LoudnessEnhancerContext *pContext)
73 {
74 ALOGV(" > LE_reset(%p)", pContext);
75
76 if (pContext->mCompressor != NULL) {
77 float targetAmp = pow(10, pContext->mTargetGainmB/2000.0f); // mB to linear amplification
78 ALOGV("LE_reset(): Target gain=%dmB <=> factor=%.2fX", pContext->mTargetGainmB, targetAmp);
79 pContext->mCompressor->Initialize(targetAmp, pContext->mConfig.inputCfg.samplingRate);
80 } else {
81 ALOGE("LE_reset(%p): null compressors, can't apply target gain", pContext);
82 }
83 }
84
85 //----------------------------------------------------------------------------
86 // LE_setConfig()
87 //----------------------------------------------------------------------------
88 // Purpose: Set input and output audio configuration.
89 //
90 // Inputs:
91 // pContext: effect engine context
92 // pConfig: pointer to effect_config_t structure holding input and output
93 // configuration parameters
94 //
95 // Outputs:
96 //
97 //----------------------------------------------------------------------------
98
LE_setConfig(LoudnessEnhancerContext * pContext,effect_config_t * pConfig)99 int LE_setConfig(LoudnessEnhancerContext *pContext, effect_config_t *pConfig)
100 {
101 ALOGV("LE_setConfig(%p)", pContext);
102
103 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL;
104 if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL;
105 if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL;
106 if (audio_channel_count_from_out_mask(pConfig->inputCfg.channels) > FCC_LIMIT) {
107 return -EINVAL;
108 }
109 if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
110 pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
111 if (pConfig->inputCfg.format != kProcessFormat) return -EINVAL;
112
113 pContext->mConfig = *pConfig;
114
115 LE_reset(pContext);
116
117 return 0;
118 }
119
120
121 //----------------------------------------------------------------------------
122 // LE_getConfig()
123 //----------------------------------------------------------------------------
124 // Purpose: Get input and output audio configuration.
125 //
126 // Inputs:
127 // pContext: effect engine context
128 // pConfig: pointer to effect_config_t structure holding input and output
129 // configuration parameters
130 //
131 // Outputs:
132 //
133 //----------------------------------------------------------------------------
134
LE_getConfig(LoudnessEnhancerContext * pContext,effect_config_t * pConfig)135 void LE_getConfig(LoudnessEnhancerContext *pContext, effect_config_t *pConfig)
136 {
137 *pConfig = pContext->mConfig;
138 }
139
140
141 //----------------------------------------------------------------------------
142 // LE_init()
143 //----------------------------------------------------------------------------
144 // Purpose: Initialize engine with default configuration.
145 //
146 // Inputs:
147 // pContext: effect engine context
148 //
149 // Outputs:
150 //
151 //----------------------------------------------------------------------------
152
LE_init(LoudnessEnhancerContext * pContext)153 int LE_init(LoudnessEnhancerContext *pContext)
154 {
155 ALOGV("LE_init(%p)", pContext);
156
157 pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
158 pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
159 pContext->mConfig.inputCfg.format = kProcessFormat;
160 pContext->mConfig.inputCfg.samplingRate = 44100;
161 pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
162 pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
163 pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
164 pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
165 pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
166 pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
167 pContext->mConfig.outputCfg.format = kProcessFormat;
168 pContext->mConfig.outputCfg.samplingRate = 44100;
169 pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
170 pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
171 pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
172 pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
173
174 pContext->mTargetGainmB = LOUDNESS_ENHANCER_DEFAULT_TARGET_GAIN_MB;
175 float targetAmp = pow(10, pContext->mTargetGainmB/2000.0f); // mB to linear amplification
176 ALOGV("LE_init(): Target gain=%dmB <=> factor=%.2fX", pContext->mTargetGainmB, targetAmp);
177
178 if (pContext->mCompressor == NULL) {
179 pContext->mCompressor = new le_fx::AdaptiveDynamicRangeCompression();
180 pContext->mCompressor->Initialize(targetAmp, pContext->mConfig.inputCfg.samplingRate);
181 }
182
183 LE_setConfig(pContext, &pContext->mConfig);
184
185 return 0;
186 }
187
188 //
189 //--- Effect Library Interface Implementation
190 //
191
LELib_Create(const effect_uuid_t * uuid,int32_t sessionId __unused,int32_t ioId __unused,effect_handle_t * pHandle)192 int LELib_Create(const effect_uuid_t *uuid,
193 int32_t sessionId __unused,
194 int32_t ioId __unused,
195 effect_handle_t *pHandle) {
196 ALOGV("LELib_Create()");
197 int ret;
198
199 if (pHandle == NULL || uuid == NULL) {
200 return -EINVAL;
201 }
202
203 if (memcmp(uuid, &gLEDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
204 return -EINVAL;
205 }
206
207 LoudnessEnhancerContext *pContext = new LoudnessEnhancerContext;
208
209 pContext->mItfe = &gLEInterface;
210 pContext->mState = LOUDNESS_ENHANCER_STATE_UNINITIALIZED;
211
212 pContext->mCompressor = NULL;
213 ret = LE_init(pContext);
214 if (ret < 0) {
215 ALOGW("LELib_Create() init failed");
216 delete pContext;
217 return ret;
218 }
219
220 *pHandle = (effect_handle_t)pContext;
221
222 pContext->mState = LOUDNESS_ENHANCER_STATE_INITIALIZED;
223
224 ALOGV(" LELib_Create context is %p", pContext);
225
226 return 0;
227
228 }
229
LELib_Release(effect_handle_t handle)230 int LELib_Release(effect_handle_t handle) {
231 LoudnessEnhancerContext * pContext = (LoudnessEnhancerContext *)handle;
232
233 ALOGV("LELib_Release %p", handle);
234 if (pContext == NULL) {
235 return -EINVAL;
236 }
237 pContext->mState = LOUDNESS_ENHANCER_STATE_UNINITIALIZED;
238 if (pContext->mCompressor != NULL) {
239 delete pContext->mCompressor;
240 pContext->mCompressor = NULL;
241 }
242 delete pContext;
243
244 return 0;
245 }
246
LELib_GetDescriptor(const effect_uuid_t * uuid,effect_descriptor_t * pDescriptor)247 int LELib_GetDescriptor(const effect_uuid_t *uuid,
248 effect_descriptor_t *pDescriptor) {
249
250 if (pDescriptor == NULL || uuid == NULL){
251 ALOGV("LELib_GetDescriptor() called with NULL pointer");
252 return -EINVAL;
253 }
254
255 if (memcmp(uuid, &gLEDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
256 *pDescriptor = gLEDescriptor;
257 return 0;
258 }
259
260 return -EINVAL;
261 } /* end LELib_GetDescriptor */
262
263 //
264 //--- Effect Control Interface Implementation
265 //
LE_process(effect_handle_t self,audio_buffer_t * inBuffer,audio_buffer_t * outBuffer)266 int LE_process(
267 effect_handle_t self, audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
268 {
269 LoudnessEnhancerContext * pContext = (LoudnessEnhancerContext *)self;
270
271 if (pContext == NULL) {
272 return -EINVAL;
273 }
274
275 if (inBuffer == NULL || inBuffer->raw == NULL ||
276 outBuffer == NULL || outBuffer->raw == NULL ||
277 inBuffer->frameCount != outBuffer->frameCount ||
278 inBuffer->frameCount == 0) {
279 return -EINVAL;
280 }
281
282 //ALOGV("LE about to process %d samples", inBuffer->frameCount);
283 constexpr float scale = 1 << 15; // power of 2 is lossless conversion to int16_t range
284 constexpr float inverseScale = 1.f / scale;
285 const float inputAmp = pow(10, pContext->mTargetGainmB/2000.0f) * scale;
286 const size_t channelCount =
287 audio_channel_count_from_out_mask(pContext->mConfig.outputCfg.channels);
288 pContext->mCompressor->Compress(
289 channelCount, inputAmp, inverseScale, inBuffer->f32, inBuffer->frameCount);
290
291 if (inBuffer->raw != outBuffer->raw) {
292 const size_t sampleCount = outBuffer->frameCount * channelCount;
293 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
294 for (size_t i = 0; i < sampleCount; i++) {
295 outBuffer->f32[i] += inBuffer->f32[i];
296 }
297 } else {
298 memcpy(outBuffer->raw, inBuffer->raw, sampleCount * sizeof(float));
299 }
300 }
301 if (pContext->mState != LOUDNESS_ENHANCER_STATE_ACTIVE) {
302 return -ENODATA;
303 }
304 return 0;
305 }
306
LE_command(effect_handle_t self,uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t * replySize,void * pReplyData)307 int LE_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
308 void *pCmdData, uint32_t *replySize, void *pReplyData) {
309
310 LoudnessEnhancerContext * pContext = (LoudnessEnhancerContext *)self;
311
312 if (pContext == NULL || pContext->mState == LOUDNESS_ENHANCER_STATE_UNINITIALIZED) {
313 return -EINVAL;
314 }
315
316 // ALOGV("LE_command command %d cmdSize %d",cmdCode, cmdSize);
317 switch (cmdCode) {
318 case EFFECT_CMD_INIT:
319 if (pReplyData == NULL || *replySize != sizeof(int)) {
320 return -EINVAL;
321 }
322 *(int *) pReplyData = LE_init(pContext);
323 break;
324 case EFFECT_CMD_SET_CONFIG:
325 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
326 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
327 return -EINVAL;
328 }
329 *(int *) pReplyData = LE_setConfig(pContext,
330 (effect_config_t *) pCmdData);
331 break;
332 case EFFECT_CMD_GET_CONFIG:
333 if (pReplyData == NULL ||
334 *replySize != sizeof(effect_config_t)) {
335 return -EINVAL;
336 }
337 LE_getConfig(pContext, (effect_config_t *)pReplyData);
338 break;
339 case EFFECT_CMD_RESET:
340 LE_reset(pContext);
341 break;
342 case EFFECT_CMD_ENABLE:
343 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
344 return -EINVAL;
345 }
346 if (pContext->mState != LOUDNESS_ENHANCER_STATE_INITIALIZED) {
347 return -ENOSYS;
348 }
349 pContext->mState = LOUDNESS_ENHANCER_STATE_ACTIVE;
350 ALOGV("EFFECT_CMD_ENABLE() OK");
351 *(int *)pReplyData = 0;
352 break;
353 case EFFECT_CMD_DISABLE:
354 if (pReplyData == NULL || *replySize != sizeof(int)) {
355 return -EINVAL;
356 }
357 if (pContext->mState != LOUDNESS_ENHANCER_STATE_ACTIVE) {
358 return -ENOSYS;
359 }
360 pContext->mState = LOUDNESS_ENHANCER_STATE_INITIALIZED;
361 ALOGV("EFFECT_CMD_DISABLE() OK");
362 *(int *)pReplyData = 0;
363 break;
364 case EFFECT_CMD_GET_PARAM: {
365 if (pCmdData == NULL ||
366 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
367 pReplyData == NULL || replySize == NULL ||
368 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
369 return -EINVAL;
370 }
371 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
372 effect_param_t *p = (effect_param_t *)pReplyData;
373 p->status = 0;
374 *replySize = sizeof(effect_param_t) + sizeof(uint32_t);
375 if (p->psize != sizeof(uint32_t)) {
376 p->status = -EINVAL;
377 break;
378 }
379 switch (*(uint32_t *)p->data) {
380 case LOUDNESS_ENHANCER_PARAM_TARGET_GAIN_MB:
381 ALOGV("get target gain(mB) = %d", pContext->mTargetGainmB);
382 *((int32_t *)p->data + 1) = pContext->mTargetGainmB;
383 p->vsize = sizeof(int32_t);
384 *replySize += sizeof(int32_t);
385 break;
386 default:
387 p->status = -EINVAL;
388 }
389 } break;
390 case EFFECT_CMD_SET_PARAM: {
391 if (pCmdData == NULL ||
392 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
393 pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
394 return -EINVAL;
395 }
396 *(int32_t *)pReplyData = 0;
397 effect_param_t *p = (effect_param_t *)pCmdData;
398 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) {
399 *(int32_t *)pReplyData = -EINVAL;
400 break;
401 }
402 switch (*(uint32_t *)p->data) {
403 case LOUDNESS_ENHANCER_PARAM_TARGET_GAIN_MB:
404 pContext->mTargetGainmB = *((int32_t *)p->data + 1);
405 ALOGV("set target gain(mB) = %d", pContext->mTargetGainmB);
406 pContext->mCompressor->set_target_gain(pow(10, pContext->mTargetGainmB / 2000.f));
407 break;
408 default:
409 *(int32_t *)pReplyData = -EINVAL;
410 }
411 } break;
412 case EFFECT_CMD_SET_DEVICE:
413 case EFFECT_CMD_SET_VOLUME:
414 case EFFECT_CMD_SET_AUDIO_MODE:
415 break;
416
417 default:
418 ALOGW("LE_command invalid command %d",cmdCode);
419 return -EINVAL;
420 }
421
422 return 0;
423 }
424
425 /* Effect Control Interface Implementation: get_descriptor */
LE_getDescriptor(effect_handle_t self,effect_descriptor_t * pDescriptor)426 int LE_getDescriptor(effect_handle_t self,
427 effect_descriptor_t *pDescriptor)
428 {
429 LoudnessEnhancerContext * pContext = (LoudnessEnhancerContext *) self;
430
431 if (pContext == NULL || pDescriptor == NULL) {
432 ALOGV("LE_getDescriptor() invalid param");
433 return -EINVAL;
434 }
435
436 *pDescriptor = gLEDescriptor;
437
438 return 0;
439 } /* end LE_getDescriptor */
440
441 // effect_handle_t interface implementation for DRC effect
442 const struct effect_interface_s gLEInterface = {
443 LE_process,
444 LE_command,
445 LE_getDescriptor,
446 NULL,
447 };
448
449 // This is the only symbol that needs to be exported
450 __attribute__ ((visibility ("default")))
451 audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
452 .tag = AUDIO_EFFECT_LIBRARY_TAG,
453 .version = EFFECT_LIBRARY_API_VERSION,
454 .name = "Loudness Enhancer Library",
455 .implementor = "The Android Open Source Project",
456 .create_effect = LELib_Create,
457 .release_effect = LELib_Release,
458 .get_descriptor = LELib_GetDescriptor,
459 };
460
461 }; // extern "C"
462