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