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