1 /*
2  * Copyright (C) 2018 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 "EffectDP"
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 #include <new>
26 
27 #include <log/log.h>
28 #include <sys/param.h>
29 
30 #include <audio_effects/effect_dynamicsprocessing.h>
31 #include <dsp/DPBase.h>
32 #include <dsp/DPFrequency.h>
33 
34 //#define VERY_VERY_VERBOSE_LOGGING
35 #ifdef VERY_VERY_VERBOSE_LOGGING
36 #define ALOGVV ALOGV
37 #else
38 #define ALOGVV(a...) do { } while (false)
39 #endif
40 
41 // union to hold command values
42 using value_t = union {
43     int32_t i;
44     float f;
45 };
46 
47 // effect_handle_t interface implementation for DP effect
48 extern const struct effect_interface_s gDPInterface;
49 
50 // AOSP Dynamics Processing UUID: e0e6539b-1781-7261-676f-6d7573696340
51 const effect_descriptor_t gDPDescriptor = {
52         {0x7261676f, 0x6d75, 0x7369, 0x6364, {0x28, 0xe2, 0xfd, 0x3a, 0xc3, 0x9e}}, // type
53         {0xe0e6539b, 0x1781, 0x7261, 0x676f, {0x6d, 0x75, 0x73, 0x69, 0x63, 0x40}}, // uuid
54         EFFECT_CONTROL_API_VERSION,
55         (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST | EFFECT_FLAG_VOLUME_CTRL),
56         0, // TODO
57         1,
58         "Dynamics Processing",
59         "The Android Open Source Project",
60 };
61 
62 enum dp_state_e {
63     DYNAMICS_PROCESSING_STATE_UNINITIALIZED,
64     DYNAMICS_PROCESSING_STATE_INITIALIZED,
65     DYNAMICS_PROCESSING_STATE_ACTIVE,
66 };
67 
68 struct DynamicsProcessingContext {
69     const struct effect_interface_s *mItfe;
70     effect_config_t mConfig;
71     uint8_t mState;
72 
73     dp_fx::DPBase * mPDynamics; //the effect (or current effect)
74     int32_t mCurrentVariant;
75     float mPreferredFrameDuration;
76 };
77 
78 // The value offset of an effect parameter is computed by rounding up
79 // the parameter size to the next 32 bit alignment.
computeParamVOffset(const effect_param_t * p)80 static inline uint32_t computeParamVOffset(const effect_param_t *p) {
81     return ((p->psize + sizeof(int32_t) - 1) / sizeof(int32_t)) *
82             sizeof(int32_t);
83 }
84 
85 //--- local function prototypes
86 int DP_setParameter(DynamicsProcessingContext *pContext,
87         uint32_t paramSize,
88         void *pParam,
89         uint32_t valueSize,
90         void *pValue);
91 int DP_getParameter(DynamicsProcessingContext *pContext,
92         uint32_t paramSize,
93         void *pParam,
94         uint32_t *pValueSize,
95         void *pValue);
96 int DP_getParameterCmdSize(uint32_t paramSize,
97         void *pParam);
98 void DP_expectedParamValueSizes(uint32_t paramSize,
99         void *pParam,
100         bool isSet,
101         uint32_t *pCmdSize,
102         uint32_t *pValueSize);
103 //
104 //--- Local functions (not directly used by effect interface)
105 //
106 
DP_reset(DynamicsProcessingContext * pContext)107 void DP_reset(DynamicsProcessingContext *pContext)
108 {
109     ALOGV("> DP_reset(%p)", pContext);
110     if (pContext->mPDynamics != NULL) {
111         pContext->mPDynamics->reset();
112     } else {
113         ALOGE("DP_reset(%p): null DynamicsProcessing", pContext);
114     }
115 }
116 
117 //----------------------------------------------------------------------------
118 // DP_setConfig()
119 //----------------------------------------------------------------------------
120 // Purpose: Set input and output audio configuration.
121 //
122 // Inputs:
123 //  pContext:   effect engine context
124 //  pConfig:    pointer to effect_config_t structure holding input and output
125 //      configuration parameters
126 //
127 // Outputs:
128 //
129 //----------------------------------------------------------------------------
130 
DP_setConfig(DynamicsProcessingContext * pContext,effect_config_t * pConfig)131 int DP_setConfig(DynamicsProcessingContext *pContext, effect_config_t *pConfig)
132 {
133     ALOGV("DP_setConfig(%p)", pContext);
134 
135     if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL;
136     if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL;
137     if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL;
138     if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
139             pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
140     if (pConfig->inputCfg.format != AUDIO_FORMAT_PCM_FLOAT) return -EINVAL;
141 
142     pContext->mConfig = *pConfig;
143 
144     DP_reset(pContext);
145 
146     return 0;
147 }
148 
149 //----------------------------------------------------------------------------
150 // DP_getConfig()
151 //----------------------------------------------------------------------------
152 // Purpose: Get input and output audio configuration.
153 //
154 // Inputs:
155 //  pContext:   effect engine context
156 //  pConfig:    pointer to effect_config_t structure holding input and output
157 //      configuration parameters
158 //
159 // Outputs:
160 //
161 //----------------------------------------------------------------------------
162 
DP_getConfig(DynamicsProcessingContext * pContext,effect_config_t * pConfig)163 void DP_getConfig(DynamicsProcessingContext *pContext, effect_config_t *pConfig)
164 {
165     *pConfig = pContext->mConfig;
166 }
167 
168 //----------------------------------------------------------------------------
169 // DP_init()
170 //----------------------------------------------------------------------------
171 // Purpose: Initialize engine with default configuration.
172 //
173 // Inputs:
174 //  pContext:   effect engine context
175 //
176 // Outputs:
177 //
178 //----------------------------------------------------------------------------
179 
DP_init(DynamicsProcessingContext * pContext)180 int DP_init(DynamicsProcessingContext *pContext)
181 {
182     ALOGV("DP_init(%p)", pContext);
183 
184     pContext->mItfe = &gDPInterface;
185     pContext->mPDynamics = NULL;
186     pContext->mState = DYNAMICS_PROCESSING_STATE_UNINITIALIZED;
187 
188     pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
189     pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
190     pContext->mConfig.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
191     pContext->mConfig.inputCfg.samplingRate = 48000;
192     pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
193     pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
194     pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
195     pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
196     pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
197     pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
198     pContext->mConfig.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
199     pContext->mConfig.outputCfg.samplingRate = 48000;
200     pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
201     pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
202     pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
203     pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
204 
205     pContext->mCurrentVariant = -1; //none
206     pContext->mPreferredFrameDuration = 0; //none
207 
208     DP_setConfig(pContext, &pContext->mConfig);
209     pContext->mState = DYNAMICS_PROCESSING_STATE_INITIALIZED;
210     return 0;
211 }
212 
DP_changeVariant(DynamicsProcessingContext * pContext,int newVariant)213 void DP_changeVariant(DynamicsProcessingContext *pContext, int newVariant) {
214     ALOGV("DP_changeVariant from %d to %d", pContext->mCurrentVariant, newVariant);
215     switch(newVariant) {
216     case VARIANT_FAVOR_FREQUENCY_RESOLUTION: {
217         pContext->mCurrentVariant = VARIANT_FAVOR_FREQUENCY_RESOLUTION;
218         delete pContext->mPDynamics;
219         pContext->mPDynamics = new dp_fx::DPFrequency();
220         break;
221     }
222     default: {
223         ALOGW("DynamicsProcessing variant %d not available for creation", newVariant);
224         break;
225     }
226     } //switch
227 }
228 
DP_configureVariant(DynamicsProcessingContext * pContext,int newVariant)229 void DP_configureVariant(DynamicsProcessingContext *pContext, int newVariant) {
230     ALOGV("DP_configureVariant %d", newVariant);
231     switch(newVariant) {
232     case VARIANT_FAVOR_FREQUENCY_RESOLUTION: {
233         int32_t minBlockSize = (int32_t)dp_fx::DPFrequency::getMinBockSize();
234         int32_t desiredBlock = pContext->mPreferredFrameDuration *
235                 pContext->mConfig.inputCfg.samplingRate / 1000.0f;
236         int32_t currentBlock = desiredBlock;
237         ALOGV(" sampling rate: %d, desiredBlock size %0.2f (%d) samples",
238                 pContext->mConfig.inputCfg.samplingRate, pContext->mPreferredFrameDuration,
239                 desiredBlock);
240         if (desiredBlock < minBlockSize) {
241             currentBlock = minBlockSize;
242         } else if (!powerof2(desiredBlock)) {
243             //find next highest power of 2.
244             currentBlock = 1 << (32 - __builtin_clz(desiredBlock));
245         }
246         ((dp_fx::DPFrequency*)pContext->mPDynamics)->configure(currentBlock,
247                 currentBlock/2,
248                 pContext->mConfig.inputCfg.samplingRate);
249         break;
250     }
251     default: {
252         ALOGE("DynamicsProcessing variant %d not available to configure", newVariant);
253         break;
254     }
255     }
256 }
257 
258 //
259 //--- Effect Library Interface Implementation
260 //
261 
DPLib_Release(effect_handle_t handle)262 int DPLib_Release(effect_handle_t handle) {
263     DynamicsProcessingContext * pContext = (DynamicsProcessingContext *)handle;
264 
265     ALOGV("DPLib_Release %p", handle);
266     if (pContext == NULL) {
267         return -EINVAL;
268     }
269     delete pContext->mPDynamics;
270     delete pContext;
271 
272     return 0;
273 }
274 
DPLib_Create(const effect_uuid_t * uuid,int32_t sessionId __unused,int32_t ioId __unused,effect_handle_t * pHandle)275 int DPLib_Create(const effect_uuid_t *uuid,
276                          int32_t sessionId __unused,
277                          int32_t ioId __unused,
278                          effect_handle_t *pHandle) {
279     ALOGV("DPLib_Create()");
280 
281     if (pHandle == NULL || uuid == NULL) {
282         return -EINVAL;
283     }
284 
285     if (memcmp(uuid, &gDPDescriptor.uuid, sizeof(*uuid)) != 0) {
286         return -EINVAL;
287     }
288 
289     DynamicsProcessingContext *pContext = new DynamicsProcessingContext;
290     *pHandle = (effect_handle_t)pContext;
291     int ret = DP_init(pContext);
292     if (ret < 0) {
293         ALOGW("DPLib_Create() init failed");
294         DPLib_Release(*pHandle);
295         return ret;
296     }
297 
298     ALOGV("DPLib_Create context is %p", pContext);
299     return 0;
300 }
301 
DPLib_GetDescriptor(const effect_uuid_t * uuid,effect_descriptor_t * pDescriptor)302 int DPLib_GetDescriptor(const effect_uuid_t *uuid,
303                                 effect_descriptor_t *pDescriptor) {
304 
305     if (pDescriptor == NULL || uuid == NULL){
306         ALOGE("DPLib_GetDescriptor() called with NULL pointer");
307         return -EINVAL;
308     }
309 
310     if (memcmp(uuid, &gDPDescriptor.uuid, sizeof(*uuid)) == 0) {
311         *pDescriptor = gDPDescriptor;
312         return 0;
313     }
314 
315     return -EINVAL;
316 } /* end DPLib_GetDescriptor */
317 
318 //
319 //--- Effect Control Interface Implementation
320 //
DP_process(effect_handle_t self,audio_buffer_t * inBuffer,audio_buffer_t * outBuffer)321 int DP_process(effect_handle_t self, audio_buffer_t *inBuffer,
322         audio_buffer_t *outBuffer) {
323     DynamicsProcessingContext * pContext = (DynamicsProcessingContext *)self;
324 
325     if (pContext == NULL) {
326         ALOGE("DP_process() called with NULL context");
327         return -EINVAL;
328     }
329 
330     if (inBuffer == NULL || inBuffer->raw == NULL ||
331         outBuffer == NULL || outBuffer->raw == NULL ||
332         inBuffer->frameCount != outBuffer->frameCount ||
333         inBuffer->frameCount == 0) {
334         ALOGE("inBuffer or outBuffer are NULL or have problems with frame count");
335         return -EINVAL;
336     }
337     if (pContext->mState != DYNAMICS_PROCESSING_STATE_ACTIVE) {
338         ALOGE("mState is not DYNAMICS_PROCESSING_STATE_ACTIVE. Current mState %d",
339                 pContext->mState);
340         return -ENODATA;
341     }
342     //if dynamics exist...
343     if (pContext->mPDynamics != NULL) {
344         int32_t channelCount = (int32_t)audio_channel_count_from_out_mask(
345                         pContext->mConfig.inputCfg.channels);
346         pContext->mPDynamics->processSamples(inBuffer->f32, inBuffer->f32,
347                 inBuffer->frameCount * channelCount);
348 
349         if (inBuffer->raw != outBuffer->raw) {
350             if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
351                 for (size_t i = 0; i < outBuffer->frameCount * channelCount; i++) {
352                     outBuffer->f32[i] += inBuffer->f32[i];
353                 }
354             } else {
355                 memcpy(outBuffer->raw, inBuffer->raw,
356                         outBuffer->frameCount * channelCount * sizeof(float));
357             }
358         }
359     } else {
360         //do nothing. no effect created yet. warning.
361         ALOGW("Warning: no DynamicsProcessing engine available");
362         return -EINVAL;
363     }
364     return 0;
365 }
366 
367 //helper function
DP_checkSizesInt(uint32_t paramSize,uint32_t valueSize,uint32_t expectedParams,uint32_t expectedValues)368 bool DP_checkSizesInt(uint32_t paramSize, uint32_t valueSize, uint32_t expectedParams,
369         uint32_t expectedValues) {
370     if (paramSize < expectedParams * sizeof(int32_t)) {
371         ALOGE("Invalid paramSize: %u expected %u", paramSize,
372                 (uint32_t)(expectedParams * sizeof(int32_t)));
373         return false;
374     }
375     if (valueSize < expectedValues * sizeof(int32_t)) {
376         ALOGE("Invalid valueSize %u expected %u", valueSize,
377                 (uint32_t)(expectedValues * sizeof(int32_t)));
378         return false;
379     }
380     return true;
381 }
382 
DP_getChannel(DynamicsProcessingContext * pContext,int32_t channel)383 static dp_fx::DPChannel* DP_getChannel(DynamicsProcessingContext *pContext,
384         int32_t channel) {
385     if (pContext->mPDynamics == NULL) {
386         return NULL;
387     }
388     dp_fx::DPChannel *pChannel = pContext->mPDynamics->getChannel(channel);
389     ALOGE_IF(pChannel == NULL, "DPChannel NULL. invalid channel %d", channel);
390     return pChannel;
391 }
392 
DP_getEq(DynamicsProcessingContext * pContext,int32_t channel,int32_t eqType)393 static dp_fx::DPEq* DP_getEq(DynamicsProcessingContext *pContext, int32_t channel,
394         int32_t eqType) {
395     dp_fx::DPChannel *pChannel = DP_getChannel(pContext, channel);
396     if (pChannel == NULL) {
397         return NULL;
398     }
399     dp_fx::DPEq *pEq = (eqType == DP_PARAM_PRE_EQ ? pChannel->getPreEq() :
400             (eqType == DP_PARAM_POST_EQ ? pChannel->getPostEq() : NULL));
401     ALOGE_IF(pEq == NULL,"DPEq NULL invalid eq");
402     return pEq;
403 }
404 
DP_getEqBand(DynamicsProcessingContext * pContext,int32_t channel,int32_t eqType,int32_t band)405 static dp_fx::DPEqBand* DP_getEqBand(DynamicsProcessingContext *pContext, int32_t channel,
406         int32_t eqType, int32_t band) {
407     dp_fx::DPEq *pEq = DP_getEq(pContext, channel, eqType);
408     if (pEq == NULL) {
409         return NULL;
410     }
411     dp_fx::DPEqBand *pEqBand = pEq->getBand(band);
412     ALOGE_IF(pEqBand == NULL, "DPEqBand NULL. invalid band %d", band);
413     return pEqBand;
414 }
415 
DP_getMbc(DynamicsProcessingContext * pContext,int32_t channel)416 static dp_fx::DPMbc* DP_getMbc(DynamicsProcessingContext *pContext, int32_t channel) {
417     dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel);
418     if (pChannel == NULL) {
419         return NULL;
420     }
421     dp_fx::DPMbc *pMbc = pChannel->getMbc();
422     ALOGE_IF(pMbc == NULL, "DPMbc NULL invalid MBC");
423     return pMbc;
424 }
425 
DP_getMbcBand(DynamicsProcessingContext * pContext,int32_t channel,int32_t band)426 static dp_fx::DPMbcBand* DP_getMbcBand(DynamicsProcessingContext *pContext, int32_t channel,
427         int32_t band) {
428     dp_fx::DPMbc *pMbc = DP_getMbc(pContext, channel);
429     if (pMbc == NULL) {
430         return NULL;
431     }
432     dp_fx::DPMbcBand *pMbcBand = pMbc->getBand(band);
433     ALOGE_IF(pMbcBand == NULL, "pMbcBand NULL. invalid band %d", band);
434     return pMbcBand;
435 }
436 
DP_command(effect_handle_t self,uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t * replySize,void * pReplyData)437 int DP_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
438         void *pCmdData, uint32_t *replySize, void *pReplyData) {
439 
440     DynamicsProcessingContext * pContext = (DynamicsProcessingContext *)self;
441 
442     if (pContext == NULL || pContext->mState == DYNAMICS_PROCESSING_STATE_UNINITIALIZED) {
443         ALOGE("DP_command() called with NULL context or uninitialized state.");
444         return -EINVAL;
445     }
446 
447     ALOGV("DP_command command %d cmdSize %d",cmdCode, cmdSize);
448     switch (cmdCode) {
449     case EFFECT_CMD_INIT:
450         if (pReplyData == NULL || *replySize != sizeof(int)) {
451             ALOGE("EFFECT_CMD_INIT wrong replyData or repySize");
452             return -EINVAL;
453         }
454         *(int *) pReplyData = DP_init(pContext);
455         break;
456     case EFFECT_CMD_SET_CONFIG:
457         if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
458                 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
459             ALOGE("EFFECT_CMD_SET_CONFIG error with pCmdData, cmdSize, pReplyData or replySize");
460             return -EINVAL;
461         }
462         *(int *) pReplyData = DP_setConfig(pContext,
463                 (effect_config_t *) pCmdData);
464         break;
465     case EFFECT_CMD_GET_CONFIG:
466         if (pReplyData == NULL ||
467             *replySize != sizeof(effect_config_t)) {
468             ALOGE("EFFECT_CMD_GET_CONFIG wrong replyData or repySize");
469             return -EINVAL;
470         }
471         DP_getConfig(pContext, (effect_config_t *)pReplyData);
472         break;
473     case EFFECT_CMD_RESET:
474         DP_reset(pContext);
475         break;
476     case EFFECT_CMD_ENABLE:
477         if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
478             ALOGE("EFFECT_CMD_ENABLE wrong replyData or repySize");
479             return -EINVAL;
480         }
481         if (pContext->mState != DYNAMICS_PROCESSING_STATE_INITIALIZED) {
482             ALOGE("EFFECT_CMD_ENABLE state not initialized");
483             *(int *)pReplyData = -ENOSYS;
484         } else {
485             pContext->mState = DYNAMICS_PROCESSING_STATE_ACTIVE;
486             ALOGV("EFFECT_CMD_ENABLE() OK");
487             *(int *)pReplyData = 0;
488         }
489         break;
490     case EFFECT_CMD_DISABLE:
491         if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
492             ALOGE("EFFECT_CMD_DISABLE wrong replyData or repySize");
493             return -EINVAL;
494         }
495         if (pContext->mState != DYNAMICS_PROCESSING_STATE_ACTIVE) {
496             ALOGE("EFFECT_CMD_DISABLE state not active");
497             *(int *)pReplyData = -ENOSYS;
498         } else {
499             pContext->mState = DYNAMICS_PROCESSING_STATE_INITIALIZED;
500             ALOGV("EFFECT_CMD_DISABLE() OK");
501             *(int *)pReplyData = 0;
502         }
503         break;
504     case EFFECT_CMD_GET_PARAM: {
505         if (pCmdData == NULL || pReplyData == NULL || replySize == NULL) {
506             ALOGE("null pCmdData or pReplyData or replySize");
507             return -EINVAL;
508         }
509         effect_param_t *pEffectParam = (effect_param_t *) pCmdData;
510         uint32_t expectedCmdSize = DP_getParameterCmdSize(pEffectParam->psize,
511                 pEffectParam->data);
512         if (cmdSize != expectedCmdSize || *replySize < expectedCmdSize) {
513             ALOGE("error cmdSize: %d, expetedCmdSize: %d, replySize: %d",
514                     cmdSize, expectedCmdSize, *replySize);
515             return -EINVAL;
516         }
517 
518         ALOGVV("DP_command expectedCmdSize: %d", expectedCmdSize);
519         memcpy(pReplyData, pCmdData, expectedCmdSize);
520         effect_param_t *p = (effect_param_t *)pReplyData;
521 
522         uint32_t voffset = computeParamVOffset(p);
523 
524         p->status = DP_getParameter(pContext,
525                 p->psize,
526                 p->data,
527                 &p->vsize,
528                 p->data + voffset);
529         *replySize = sizeof(effect_param_t) + voffset + p->vsize;
530 
531         ALOGVV("DP_command replysize %u, status %d" , *replySize, p->status);
532         break;
533     }
534     case EFFECT_CMD_SET_PARAM: {
535         if (pCmdData == NULL ||
536                 cmdSize < (sizeof(effect_param_t) + sizeof(int32_t) + sizeof(int32_t)) ||
537                 pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
538             ALOGE("\tLVM_ERROR : DynamicsProcessing cmdCode Case: "
539                     "EFFECT_CMD_SET_PARAM: ERROR");
540             return -EINVAL;
541         }
542 
543         effect_param_t * const p = (effect_param_t *) pCmdData;
544         const uint32_t voffset = computeParamVOffset(p);
545 
546         *(int *)pReplyData = DP_setParameter(pContext,
547                 p->psize,
548                 (void *)p->data,
549                 p->vsize,
550                 p->data + voffset);
551         break;
552     }
553     case EFFECT_CMD_SET_VOLUME: {
554         ALOGV("EFFECT_CMD_SET_VOLUME");
555         // if pReplyData is NULL, VOL_CTRL is delegated to another effect
556         if (pReplyData == NULL || replySize == NULL || *replySize < ((int)sizeof(int32_t) * 2)) {
557             ALOGV("no VOLUME data to return");
558             break;
559         }
560         if (pCmdData == NULL || cmdSize < ((int)sizeof(uint32_t) * 2)) {
561             ALOGE("\tLVM_ERROR : DynamicsProcessing EFFECT_CMD_SET_VOLUME ERROR");
562             return -EINVAL;
563         }
564 
565         const int32_t unityGain = 1 << 24;
566         //channel count
567         int32_t channelCount = (int32_t)audio_channel_count_from_out_mask(
568                 pContext->mConfig.inputCfg.channels);
569         for (int32_t ch = 0; ch < channelCount; ch++) {
570 
571             dp_fx::DPChannel * pChannel = DP_getChannel(pContext, ch);
572             if (pChannel == NULL) {
573                 ALOGE("%s EFFECT_CMD_SET_VOLUME invalid channel %d", __func__, ch);
574                 return -EINVAL;
575                 break;
576             }
577 
578             int32_t offset = ch;
579             if (ch > 1) {
580                 // FIXME: limited to 2 unique channels. If more channels present, use value for
581                 // first channel
582                 offset = 0;
583             }
584             const float gain = (float)*((uint32_t *)pCmdData + offset) / unityGain;
585             const float gainDb = linearToDb(gain);
586             ALOGVV("%s EFFECT_CMD_SET_VOLUME channel %d, engine outputlevel %f (%0.2f dB)",
587                     __func__, ch, gain, gainDb);
588             pChannel->setOutputGain(gainDb);
589         }
590 
591         const int32_t  volRet[2] = {unityGain, unityGain}; // Apply no volume before effect.
592         memcpy(pReplyData, volRet, sizeof(volRet));
593         break;
594     }
595     case EFFECT_CMD_SET_DEVICE:
596     case EFFECT_CMD_SET_AUDIO_MODE:
597         break;
598 
599     default:
600         ALOGW("DP_command invalid command %d",cmdCode);
601         return -EINVAL;
602     }
603 
604     return 0;
605 }
606 
607 //register expected cmd size
DP_getParameterCmdSize(uint32_t paramSize,void * pParam)608 int DP_getParameterCmdSize(uint32_t paramSize,
609         void *pParam) {
610     if (paramSize < sizeof(int32_t)) {
611         return 0;
612     }
613     int32_t param = *(int32_t*)pParam;
614     switch(param) {
615     case DP_PARAM_GET_CHANNEL_COUNT: //paramcmd
616     case DP_PARAM_ENGINE_ARCHITECTURE:
617         //effect + param
618         return (int)(sizeof(effect_param_t) + sizeof(uint32_t));
619     case DP_PARAM_INPUT_GAIN: //paramcmd + param
620     case DP_PARAM_LIMITER:
621     case DP_PARAM_PRE_EQ:
622     case DP_PARAM_POST_EQ:
623     case DP_PARAM_MBC:
624         //effect + param
625         return (int)(sizeof(effect_param_t) + 2 * sizeof(uint32_t));
626     case DP_PARAM_PRE_EQ_BAND:
627     case DP_PARAM_POST_EQ_BAND:
628     case DP_PARAM_MBC_BAND:
629         return (int)(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
630     }
631     return 0;
632 }
633 
DP_getParameter(DynamicsProcessingContext * pContext,uint32_t paramSize,void * pParam,uint32_t * pValueSize,void * pValue)634 int DP_getParameter(DynamicsProcessingContext *pContext,
635                            uint32_t paramSize,
636                            void *pParam,
637                            uint32_t *pValueSize,
638                            void *pValue) {
639     int status = 0;
640     int32_t *params = (int32_t *)pParam;
641     static_assert(sizeof(float) == sizeof(int32_t) && sizeof(float) == sizeof(value_t) &&
642             alignof(float) == alignof(int32_t) && alignof(float) == alignof(value_t),
643             "Size/alignment mismatch for float/int32_t/value_t");
644     value_t *values = reinterpret_cast<value_t*>(pValue);
645 
646     ALOGVV("%s start", __func__);
647 #ifdef VERY_VERY_VERBOSE_LOGGING
648     for (size_t i = 0; i < paramSize/sizeof(int32_t); i++) {
649         ALOGVV("Param[%zu] %d", i, params[i]);
650     }
651 #endif
652     if (paramSize < sizeof(int32_t)) {
653         ALOGE("%s invalid paramSize: %u", __func__, paramSize);
654         return -EINVAL;
655     }
656     const int32_t command = params[0];
657     switch (command) {
658     case DP_PARAM_GET_CHANNEL_COUNT: {
659         if (!DP_checkSizesInt(paramSize,*pValueSize, 1 /*params*/, 1 /*values*/)) {
660             ALOGE("%s DP_PARAM_GET_CHANNEL_COUNT (cmd %d) invalid sizes.", __func__, command);
661             status = -EINVAL;
662             break;
663         }
664         *pValueSize = sizeof(uint32_t);
665         *(uint32_t *)pValue = (uint32_t)audio_channel_count_from_out_mask(
666                 pContext->mConfig.inputCfg.channels);
667         ALOGVV("%s DP_PARAM_GET_CHANNEL_COUNT channels %d", __func__, *(int32_t *)pValue);
668         break;
669     }
670     case DP_PARAM_ENGINE_ARCHITECTURE: {
671         ALOGVV("engine architecture paramsize: %d valuesize %d",paramSize, *pValueSize);
672         if (!DP_checkSizesInt(paramSize, *pValueSize, 1 /*params*/, 9 /*values*/)) {
673             ALOGE("%s DP_PARAM_ENGINE_ARCHITECTURE (cmd %d) invalid sizes.", __func__, command);
674             status = -EINVAL;
675             break;
676         }
677 //        Number[] params = { PARAM_ENGINE_ARCHITECTURE };
678 //        Number[] values = { 0 /*0 variant */,
679 //                0.0f /* 1 preferredFrameDuration */,
680 //                0 /*2 preEqInUse */,
681 //                0 /*3 preEqBandCount */,
682 //                0 /*4 mbcInUse */,
683 //                0 /*5 mbcBandCount*/,
684 //                0 /*6 postEqInUse */,
685 //                0 /*7 postEqBandCount */,
686 //                0 /*8 limiterInUse */};
687         if (pContext->mPDynamics == NULL) {
688             ALOGE("%s DP_PARAM_ENGINE_ARCHITECTURE error mPDynamics is NULL", __func__);
689             status = -EINVAL;
690             break;
691         }
692         values[0].i = pContext->mCurrentVariant;
693         values[1].f = pContext->mPreferredFrameDuration;
694         values[2].i = pContext->mPDynamics->isPreEQInUse();
695         values[3].i = pContext->mPDynamics->getPreEqBandCount();
696         values[4].i = pContext->mPDynamics->isMbcInUse();
697         values[5].i = pContext->mPDynamics->getMbcBandCount();
698         values[6].i = pContext->mPDynamics->isPostEqInUse();
699         values[7].i = pContext->mPDynamics->getPostEqBandCount();
700         values[8].i = pContext->mPDynamics->isLimiterInUse();
701 
702         *pValueSize = sizeof(value_t) * 9;
703 
704         ALOGVV(" variant %d, preferredFrameDuration: %f, preEqInuse %d, bands %d, mbcinuse %d,"
705                 "mbcbands %d, posteqInUse %d, bands %d, limiterinuse %d",
706                 values[0].i, values[1].f, values[2].i, values[3].i, values[4].i, values[5].i,
707                 values[6].i, values[7].i, values[8].i);
708         break;
709     }
710     case DP_PARAM_INPUT_GAIN: {
711         ALOGVV("engine get PARAM_INPUT_GAIN paramsize: %d valuesize %d",paramSize, *pValueSize);
712         if (!DP_checkSizesInt(paramSize, *pValueSize, 2 /*params*/, 1 /*values*/)) {
713             ALOGE("%s get PARAM_INPUT_GAIN invalid sizes.", __func__);
714             status = -EINVAL;
715             break;
716         }
717 
718         const int32_t channel = params[1];
719         dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel);
720         if (pChannel == NULL) {
721             ALOGE("%s get PARAM_INPUT_GAIN invalid channel %d", __func__, channel);
722             status = -EINVAL;
723             break;
724         }
725         values[0].f = pChannel->getInputGain();
726         *pValueSize = sizeof(value_t) * 1;
727 
728         ALOGVV(" channel: %d, input gain %f\n", channel, values[0].f);
729         break;
730     }
731     case DP_PARAM_PRE_EQ:
732     case DP_PARAM_POST_EQ: {
733         ALOGVV("engine get PARAM_*_EQ paramsize: %d valuesize %d",paramSize, *pValueSize);
734         if (!DP_checkSizesInt(paramSize, *pValueSize, 2 /*params*/, 3 /*values*/)) {
735             ALOGE("%s get PARAM_*_EQ (cmd %d) invalid sizes.", __func__, command);
736             status = -EINVAL;
737             break;
738         }
739 //        Number[] params = {paramSet == PARAM_PRE_EQ ? PARAM_PRE_EQ : PARAM_POST_EQ,
740 //                       channelIndex};
741 //               Number[] values = {0 /*0 in use */,
742 //                                   0 /*1 enabled*/,
743 //                                   0 /*2 band count */};
744         const int32_t channel = params[1];
745 
746         dp_fx::DPEq *pEq = DP_getEq(pContext, channel, command);
747         if (pEq == NULL) {
748             ALOGE("%s get PARAM_*_EQ invalid eq", __func__);
749             status = -EINVAL;
750             break;
751         }
752         values[0].i = pEq->isInUse();
753         values[1].i = pEq->isEnabled();
754         values[2].i = pEq->getBandCount();
755         *pValueSize = sizeof(value_t) * 3;
756 
757         ALOGVV(" %s channel: %d, inUse::%d, enabled:%d, bandCount:%d\n",
758                 (command == DP_PARAM_PRE_EQ ? "preEq" : "postEq"), channel,
759                 values[0].i, values[1].i, values[2].i);
760         break;
761     }
762     case DP_PARAM_PRE_EQ_BAND:
763     case DP_PARAM_POST_EQ_BAND: {
764         ALOGVV("engine get PARAM_*_EQ_BAND paramsize: %d valuesize %d",paramSize, *pValueSize);
765         if (!DP_checkSizesInt(paramSize, *pValueSize, 3 /*params*/, 3 /*values*/)) {
766             ALOGE("%s get PARAM_*_EQ_BAND (cmd %d) invalid sizes.", __func__, command);
767             status = -EINVAL;
768             break;
769         }
770 //        Number[] params = {paramSet,
771 //                channelIndex,
772 //                bandIndex};
773 //        Number[] values = {(eqBand.isEnabled() ? 1 : 0),
774 //              eqBand.getCutoffFrequency(),
775 //              eqBand.getGain()};
776         const int32_t channel = params[1];
777         const int32_t band = params[2];
778         int eqCommand = (command == DP_PARAM_PRE_EQ_BAND ? DP_PARAM_PRE_EQ :
779                 (command == DP_PARAM_POST_EQ_BAND ? DP_PARAM_POST_EQ : -1));
780 
781         dp_fx::DPEqBand *pEqBand = DP_getEqBand(pContext, channel, eqCommand, band);
782         if (pEqBand == NULL) {
783             ALOGE("%s get PARAM_*_EQ_BAND invalid channel %d or band %d", __func__, channel, band);
784             status = -EINVAL;
785             break;
786         }
787 
788         values[0].i = pEqBand->isEnabled();
789         values[1].f = pEqBand->getCutoffFrequency();
790         values[2].f = pEqBand->getGain();
791         *pValueSize = sizeof(value_t) * 3;
792 
793         ALOGVV("%s channel: %d, band::%d, enabled:%d, cutoffFrequency:%f, gain%f\n",
794                 (command == DP_PARAM_PRE_EQ_BAND ? "preEqBand" : "postEqBand"), channel, band,
795                 values[0].i, values[1].f, values[2].f);
796         break;
797     }
798     case DP_PARAM_MBC: {
799         ALOGVV("engine get PDP_PARAM_MBC paramsize: %d valuesize %d",paramSize, *pValueSize);
800         if (!DP_checkSizesInt(paramSize, *pValueSize, 2 /*params*/, 3 /*values*/)) {
801             ALOGE("%s get PDP_PARAM_MBC (cmd %d) invalid sizes.", __func__, command);
802             status = -EINVAL;
803             break;
804         }
805 
806 //           Number[] params = {PARAM_MBC,
807 //                    channelIndex};
808 //            Number[] values = {0 /*0 in use */,
809 //                                0 /*1 enabled*/,
810 //                                0 /*2 band count */};
811 
812         const int32_t channel = params[1];
813 
814         dp_fx::DPMbc *pMbc = DP_getMbc(pContext, channel);
815         if (pMbc == NULL) {
816             ALOGE("%s get PDP_PARAM_MBC invalid MBC", __func__);
817             status = -EINVAL;
818             break;
819         }
820 
821         values[0].i = pMbc->isInUse();
822         values[1].i = pMbc->isEnabled();
823         values[2].i = pMbc->getBandCount();
824         *pValueSize = sizeof(value_t) * 3;
825 
826         ALOGVV("DP_PARAM_MBC channel: %d, inUse::%d, enabled:%d, bandCount:%d\n", channel,
827                 values[0].i, values[1].i, values[2].i);
828         break;
829     }
830     case DP_PARAM_MBC_BAND: {
831         ALOGVV("engine get DP_PARAM_MBC_BAND paramsize: %d valuesize %d",paramSize, *pValueSize);
832         if (!DP_checkSizesInt(paramSize, *pValueSize, 3 /*params*/, 11 /*values*/)) {
833             ALOGE("%s get DP_PARAM_MBC_BAND (cmd %d) invalid sizes.", __func__, command);
834             status = -EINVAL;
835             break;
836         }
837 //        Number[] params = {PARAM_MBC_BAND,
838 //                        channelIndex,
839 //                        bandIndex};
840 //                Number[] values = {0 /*0 enabled */,
841 //                        0.0f /*1 cutoffFrequency */,
842 //                        0.0f /*2 AttackTime */,
843 //                        0.0f /*3 ReleaseTime */,
844 //                        0.0f /*4 Ratio */,
845 //                        0.0f /*5 Threshold */,
846 //                        0.0f /*6 KneeWidth */,
847 //                        0.0f /*7 NoiseGateThreshold */,
848 //                        0.0f /*8 ExpanderRatio */,
849 //                        0.0f /*9 PreGain */,
850 //                        0.0f /*10 PostGain*/};
851 
852         const int32_t channel = params[1];
853         const int32_t band = params[2];
854 
855         dp_fx::DPMbcBand *pMbcBand = DP_getMbcBand(pContext, channel, band);
856         if (pMbcBand == NULL) {
857             ALOGE("%s get PARAM_MBC_BAND invalid channel %d or band %d", __func__, channel, band);
858             status = -EINVAL;
859             break;
860         }
861 
862         values[0].i = pMbcBand->isEnabled();
863         values[1].f = pMbcBand->getCutoffFrequency();
864         values[2].f = pMbcBand->getAttackTime();
865         values[3].f = pMbcBand->getReleaseTime();
866         values[4].f = pMbcBand->getRatio();
867         values[5].f = pMbcBand->getThreshold();
868         values[6].f = pMbcBand->getKneeWidth();
869         values[7].f = pMbcBand->getNoiseGateThreshold();
870         values[8].f = pMbcBand->getExpanderRatio();
871         values[9].f = pMbcBand->getPreGain();
872         values[10].f = pMbcBand->getPostGain();
873 
874         *pValueSize = sizeof(value_t) * 11;
875         ALOGVV(" mbcBand channel: %d, band::%d, enabled:%d, cutoffFrequency:%f, attackTime:%f,"
876                 "releaseTime:%f, ratio:%f, threshold:%f, kneeWidth:%f, noiseGateThreshold:%f,"
877                 "expanderRatio:%f, preGain:%f, postGain:%f\n", channel, band, values[0].i,
878                 values[1].f, values[2].f, values[3].f, values[4].f, values[5].f, values[6].f,
879                 values[7].f, values[8].f, values[9].f, values[10].f);
880         break;
881     }
882     case DP_PARAM_LIMITER: {
883         ALOGVV("engine get DP_PARAM_LIMITER paramsize: %d valuesize %d",paramSize, *pValueSize);
884         if (!DP_checkSizesInt(paramSize, *pValueSize, 2 /*params*/, 8 /*values*/)) {
885             ALOGE("%s DP_PARAM_LIMITER (cmd %d) invalid sizes.", __func__, command);
886             status = -EINVAL;
887             break;
888         }
889 
890         int32_t channel = params[1];
891 //      Number[] values = {0 /*0 in use (int)*/,
892 //              0 /*1 enabled (int)*/,
893 //              0 /*2 link group (int)*/,
894 //              0.0f /*3 attack time (float)*/,
895 //              0.0f /*4 release time (float)*/,
896 //              0.0f /*5 ratio (float)*/,
897 //              0.0f /*6 threshold (float)*/,
898 //              0.0f /*7 post gain(float)*/};
899         dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel);
900         if (pChannel == NULL) {
901             ALOGE("%s DP_PARAM_LIMITER invalid channel %d", __func__, channel);
902             status = -EINVAL;
903             break;
904         }
905         dp_fx::DPLimiter *pLimiter = pChannel->getLimiter();
906         if (pLimiter == NULL) {
907             ALOGE("%s DP_PARAM_LIMITER null LIMITER", __func__);
908             status = -EINVAL;
909             break;
910         }
911         values[0].i = pLimiter->isInUse();
912         values[1].i = pLimiter->isEnabled();
913         values[2].i = pLimiter->getLinkGroup();
914         values[3].f = pLimiter->getAttackTime();
915         values[4].f = pLimiter->getReleaseTime();
916         values[5].f = pLimiter->getRatio();
917         values[6].f = pLimiter->getThreshold();
918         values[7].f = pLimiter->getPostGain();
919 
920         *pValueSize = sizeof(value_t) * 8;
921 
922         ALOGVV(" Limiter channel: %d, inUse::%d, enabled:%d, linkgroup:%d attackTime:%f,"
923                 "releaseTime:%f, ratio:%f, threshold:%f, postGain:%f\n",
924                 channel, values[0].i/*inUse*/, values[1].i/*enabled*/, values[2].i/*linkGroup*/,
925                 values[3].f/*attackTime*/, values[4].f/*releaseTime*/,
926                 values[5].f/*ratio*/, values[6].f/*threshold*/,
927                 values[7].f/*postGain*/);
928         break;
929     }
930     default:
931         ALOGE("%s invalid param %d", __func__, params[0]);
932         status = -EINVAL;
933         break;
934     }
935 
936     ALOGVV("%s end param: %d, status: %d", __func__, params[0], status);
937     return status;
938 } /* end DP_getParameter */
939 
DP_setParameter(DynamicsProcessingContext * pContext,uint32_t paramSize,void * pParam,uint32_t valueSize,void * pValue)940 int DP_setParameter(DynamicsProcessingContext *pContext,
941                            uint32_t paramSize,
942                            void *pParam,
943                            uint32_t valueSize,
944                            void *pValue) {
945     int status = 0;
946     int32_t *params = (int32_t *)pParam;
947     static_assert(sizeof(float) == sizeof(int32_t) && sizeof(float) == sizeof(value_t) &&
948             alignof(float) == alignof(int32_t) && alignof(float) == alignof(value_t),
949             "Size/alignment mismatch for float/int32_t/value_t");
950     value_t *values = reinterpret_cast<value_t*>(pValue);
951 
952     ALOGVV("%s start", __func__);
953     if (paramSize < sizeof(int32_t)) {
954         ALOGE("%s invalid paramSize: %u", __func__, paramSize);
955         return -EINVAL;
956     }
957     const int32_t command = params[0];
958     switch (command) {
959     case DP_PARAM_ENGINE_ARCHITECTURE: {
960         ALOGVV("engine architecture paramsize: %d valuesize %d",paramSize, valueSize);
961         if (!DP_checkSizesInt(paramSize, valueSize, 1 /*params*/, 9 /*values*/)) {
962             ALOGE("%s DP_PARAM_ENGINE_ARCHITECTURE (cmd %d) invalid sizes.", __func__, command);
963             status = -EINVAL;
964             break;
965         }
966 //        Number[] params = { PARAM_ENGINE_ARCHITECTURE };
967 //        Number[] values = { variant /* variant */,
968 //                preferredFrameDuration,
969 //                (preEqInUse ? 1 : 0),
970 //                preEqBandCount,
971 //                (mbcInUse ? 1 : 0),
972 //                mbcBandCount,
973 //                (postEqInUse ? 1 : 0),
974 //                postEqBandCount,
975 //                (limiterInUse ? 1 : 0)};
976         const int32_t variant = values[0].i;
977         const float preferredFrameDuration = values[1].f;
978         const int32_t preEqInUse = values[2].i;
979         const int32_t preEqBandCount = values[3].i;
980         const int32_t mbcInUse = values[4].i;
981         const int32_t mbcBandCount = values[5].i;
982         const int32_t postEqInUse = values[6].i;
983         const int32_t postEqBandCount = values[7].i;
984         const int32_t limiterInUse = values[8].i;
985         ALOGVV("variant %d, preEqInuse %d, bands %d, mbcinuse %d, mbcbands %d, posteqInUse %d,"
986                 "bands %d, limiterinuse %d", variant, preEqInUse, preEqBandCount, mbcInUse,
987                 mbcBandCount, postEqInUse, postEqBandCount, limiterInUse);
988 
989         //set variant (instantiate effect)
990         //initArchitecture for effect
991         DP_changeVariant(pContext, variant);
992         if (pContext->mPDynamics == NULL) {
993             ALOGE("%s DP_PARAM_ENGINE_ARCHITECTURE error setting variant %d", __func__, variant);
994             status = -EINVAL;
995             break;
996         }
997         pContext->mPreferredFrameDuration = preferredFrameDuration;
998         pContext->mPDynamics->init((uint32_t)audio_channel_count_from_out_mask(
999                 pContext->mConfig.inputCfg.channels),
1000                 preEqInUse != 0, (uint32_t)preEqBandCount,
1001                 mbcInUse != 0, (uint32_t)mbcBandCount,
1002                 postEqInUse != 0, (uint32_t)postEqBandCount,
1003                 limiterInUse != 0);
1004 
1005         DP_configureVariant(pContext, variant);
1006         break;
1007     }
1008     case DP_PARAM_INPUT_GAIN: {
1009         ALOGVV("engine DP_PARAM_INPUT_GAIN paramsize: %d valuesize %d",paramSize, valueSize);
1010         if (!DP_checkSizesInt(paramSize, valueSize, 2 /*params*/, 1 /*values*/)) {
1011             ALOGE("%s DP_PARAM_INPUT_GAIN invalid sizes.", __func__);
1012             status = -EINVAL;
1013             break;
1014         }
1015 
1016         const int32_t channel = params[1];
1017         dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel);
1018         if (pChannel == NULL) {
1019             ALOGE("%s DP_PARAM_INPUT_GAIN invalid channel %d", __func__, channel);
1020             status = -EINVAL;
1021             break;
1022         }
1023         const float gain = values[0].f;
1024         ALOGVV("%s DP_PARAM_INPUT_GAIN channel %d, level %f", __func__, channel, gain);
1025         pChannel->setInputGain(gain);
1026         break;
1027     }
1028     case DP_PARAM_PRE_EQ:
1029     case DP_PARAM_POST_EQ: {
1030         ALOGVV("engine DP_PARAM_*_EQ paramsize: %d valuesize %d",paramSize, valueSize);
1031         if (!DP_checkSizesInt(paramSize, valueSize, 2 /*params*/, 3 /*values*/)) {
1032             ALOGE("%s DP_PARAM_*_EQ (cmd %d) invalid sizes.", __func__, command);
1033             status = -EINVAL;
1034             break;
1035         }
1036 //        Number[] params = {paramSet,
1037 //                channelIndex};
1038 //        Number[] values = { (eq.isInUse() ? 1 : 0),
1039 //                (eq.isEnabled() ? 1 : 0),
1040 //                bandCount};
1041         const int32_t channel = params[1];
1042 
1043         const int32_t enabled = values[1].i;
1044         const int32_t bandCount = values[2].i;
1045         ALOGVV(" %s channel: %d, inUse::%d, enabled:%d, bandCount:%d\n",
1046                 (command == DP_PARAM_PRE_EQ ? "preEq" : "postEq"), channel, values[0].i,
1047                 values[2].i, bandCount);
1048 
1049         dp_fx::DPEq *pEq = DP_getEq(pContext, channel, command);
1050         if (pEq == NULL) {
1051             ALOGE("%s set PARAM_*_EQ invalid channel %d or command %d", __func__, channel,
1052                     command);
1053             status = -EINVAL;
1054             break;
1055         }
1056 
1057         pEq->setEnabled(enabled != 0);
1058         //fail if bandcountis different? maybe.
1059         if ((int32_t)pEq->getBandCount() != bandCount) {
1060             ALOGW("%s warning, trying to set different bandcount from %d to %d", __func__,
1061                     pEq->getBandCount(), bandCount);
1062         }
1063         break;
1064     }
1065     case DP_PARAM_PRE_EQ_BAND:
1066     case DP_PARAM_POST_EQ_BAND: {
1067         ALOGVV("engine set PARAM_*_EQ_BAND paramsize: %d valuesize %d",paramSize, valueSize);
1068         if (!DP_checkSizesInt(paramSize, valueSize, 3 /*params*/, 3 /*values*/)) {
1069             ALOGE("%s PARAM_*_EQ_BAND (cmd %d) invalid sizes.", __func__, command);
1070             status = -EINVAL;
1071             break;
1072         }
1073 //        Number[] values = { channelIndex,
1074 //                bandIndex,
1075 //                (eqBand.isEnabled() ? 1 : 0),
1076 //                eqBand.getCutoffFrequency(),
1077 //                eqBand.getGain()};
1078 
1079 //        Number[] params = {paramSet,
1080 //                channelIndex,
1081 //                bandIndex};
1082 //        Number[] values = {(eqBand.isEnabled() ? 1 : 0),
1083 //              eqBand.getCutoffFrequency(),
1084 //              eqBand.getGain()};
1085 
1086         const int32_t channel = params[1];
1087         const int32_t band = params[2];
1088 
1089         const int32_t enabled = values[0].i;
1090         const float cutoffFrequency = values[1].f;
1091         const float gain = values[2].f;
1092 
1093 
1094         ALOGVV(" %s channel: %d, band::%d, enabled:%d, cutoffFrequency:%f, gain%f\n",
1095                 (command == DP_PARAM_PRE_EQ_BAND ? "preEqBand" : "postEqBand"), channel, band,
1096                 enabled, cutoffFrequency, gain);
1097 
1098         int eqCommand = (command == DP_PARAM_PRE_EQ_BAND ? DP_PARAM_PRE_EQ :
1099                 (command == DP_PARAM_POST_EQ_BAND ? DP_PARAM_POST_EQ : -1));
1100         dp_fx::DPEq *pEq = DP_getEq(pContext, channel, eqCommand);
1101         if (pEq == NULL) {
1102             ALOGE("%s set PARAM_*_EQ_BAND invalid channel %d or command %d", __func__, channel,
1103                     command);
1104             status = -EINVAL;
1105             break;
1106         }
1107 
1108         dp_fx::DPEqBand eqBand;
1109         eqBand.init(enabled != 0, cutoffFrequency, gain);
1110         pEq->setBand(band, eqBand);
1111         break;
1112     }
1113     case DP_PARAM_MBC: {
1114         ALOGVV("engine DP_PARAM_MBC paramsize: %d valuesize %d",paramSize, valueSize);
1115         if (!DP_checkSizesInt(paramSize, valueSize, 2 /*params*/, 3 /*values*/)) {
1116             ALOGE("%s DP_PARAM_MBC (cmd %d) invalid sizes.", __func__, command);
1117             status = -EINVAL;
1118             break;
1119         }
1120 //            Number[] params = { PARAM_MBC,
1121 //                    channelIndex};
1122 //            Number[] values = {(mbc.isInUse() ? 1 : 0),
1123 //                    (mbc.isEnabled() ? 1 : 0),
1124 //                    bandCount};
1125         const int32_t channel = params[1];
1126 
1127         const int32_t enabled = values[1].i;
1128         const int32_t bandCount = values[2].i;
1129         ALOGVV("MBC channel: %d, inUse::%d, enabled:%d, bandCount:%d\n", channel, values[0].i,
1130                 enabled, bandCount);
1131 
1132         dp_fx::DPMbc *pMbc = DP_getMbc(pContext, channel);
1133         if (pMbc == NULL) {
1134             ALOGE("%s set DP_PARAM_MBC invalid channel %d ", __func__, channel);
1135             status = -EINVAL;
1136             break;
1137         }
1138 
1139         pMbc->setEnabled(enabled != 0);
1140         //fail if bandcountis different? maybe.
1141         if ((int32_t)pMbc->getBandCount() != bandCount) {
1142             ALOGW("%s warning, trying to set different bandcount from %d to %d", __func__,
1143                     pMbc->getBandCount(), bandCount);
1144         }
1145         break;
1146     }
1147     case DP_PARAM_MBC_BAND: {
1148         ALOGVV("engine set DP_PARAM_MBC_BAND paramsize: %d valuesize %d ",paramSize, valueSize);
1149         if (!DP_checkSizesInt(paramSize, valueSize, 3 /*params*/, 11 /*values*/)) {
1150             ALOGE("%s DP_PARAM_MBC_BAND: (cmd %d) invalid sizes.", __func__, command);
1151             status = -EINVAL;
1152             break;
1153         }
1154 //        Number[] params = { PARAM_MBC_BAND,
1155 //                channelIndex,
1156 //                bandIndex};
1157 //        Number[] values = {(mbcBand.isEnabled() ? 1 : 0),
1158 //                mbcBand.getCutoffFrequency(),
1159 //                mbcBand.getAttackTime(),
1160 //                mbcBand.getReleaseTime(),
1161 //                mbcBand.getRatio(),
1162 //                mbcBand.getThreshold(),
1163 //                mbcBand.getKneeWidth(),
1164 //                mbcBand.getNoiseGateThreshold(),
1165 //                mbcBand.getExpanderRatio(),
1166 //                mbcBand.getPreGain(),
1167 //                mbcBand.getPostGain()};
1168 
1169         const int32_t channel = params[1];
1170         const int32_t band = params[2];
1171 
1172         const int32_t enabled = values[0].i;
1173         const float cutoffFrequency = values[1].f;
1174         const float attackTime = values[2].f;
1175         const float releaseTime = values[3].f;
1176         const float ratio = values[4].f;
1177         const float threshold = values[5].f;
1178         const float kneeWidth = values[6].f;
1179         const float noiseGateThreshold = values[7].f;
1180         const float expanderRatio = values[8].f;
1181         const float preGain = values[9].f;
1182         const float postGain = values[10].f;
1183 
1184         ALOGVV(" mbcBand channel: %d, band::%d, enabled:%d, cutoffFrequency:%f, attackTime:%f,"
1185                 "releaseTime:%f, ratio:%f, threshold:%f, kneeWidth:%f, noiseGateThreshold:%f,"
1186                 "expanderRatio:%f, preGain:%f, postGain:%f\n",
1187                 channel, band, enabled, cutoffFrequency, attackTime, releaseTime, ratio,
1188                 threshold, kneeWidth, noiseGateThreshold, expanderRatio, preGain, postGain);
1189 
1190         dp_fx::DPMbc *pMbc = DP_getMbc(pContext, channel);
1191         if (pMbc == NULL) {
1192             ALOGE("%s set DP_PARAM_MBC_BAND invalid channel %d", __func__, channel);
1193             status = -EINVAL;
1194             break;
1195         }
1196 
1197         dp_fx::DPMbcBand mbcBand;
1198         mbcBand.init(enabled != 0, cutoffFrequency, attackTime, releaseTime, ratio, threshold,
1199                 kneeWidth, noiseGateThreshold, expanderRatio, preGain, postGain);
1200         pMbc->setBand(band, mbcBand);
1201         break;
1202     }
1203     case DP_PARAM_LIMITER: {
1204         ALOGVV("engine DP_PARAM_LIMITER paramsize: %d valuesize %d",paramSize, valueSize);
1205         if (!DP_checkSizesInt(paramSize, valueSize, 2 /*params*/, 8 /*values*/)) {
1206             ALOGE("%s DP_PARAM_LIMITER (cmd %d) invalid sizes.", __func__, command);
1207             status = -EINVAL;
1208             break;
1209         }
1210 //            Number[] params = { PARAM_LIMITER,
1211 //                             channelIndex};
1212 //                     Number[] values = {(limiter.isInUse() ? 1 : 0),
1213 //                             (limiter.isEnabled() ? 1 : 0),
1214 //                             limiter.getLinkGroup(),
1215 //                             limiter.getAttackTime(),
1216 //                             limiter.getReleaseTime(),
1217 //                             limiter.getRatio(),
1218 //                             limiter.getThreshold(),
1219 //                             limiter.getPostGain()};
1220 
1221         const int32_t channel = params[1];
1222 
1223         const int32_t inUse = values[0].i;
1224         const int32_t enabled = values[1].i;
1225         const int32_t linkGroup = values[2].i;
1226         const float attackTime = values[3].f;
1227         const float releaseTime = values[4].f;
1228         const float ratio = values[5].f;
1229         const float threshold = values[6].f;
1230         const float postGain = values[7].f;
1231 
1232         ALOGVV(" Limiter channel: %d, inUse::%d, enabled:%d, linkgroup:%d attackTime:%f,"
1233                 "releaseTime:%f, ratio:%f, threshold:%f, postGain:%f\n", channel, inUse,
1234                 enabled, linkGroup, attackTime, releaseTime, ratio, threshold, postGain);
1235 
1236         dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel);
1237         if (pChannel == NULL) {
1238             ALOGE("%s DP_PARAM_LIMITER invalid channel %d", __func__, channel);
1239             status = -EINVAL;
1240             break;
1241         }
1242         dp_fx::DPLimiter limiter;
1243         limiter.init(inUse != 0, enabled != 0, linkGroup, attackTime, releaseTime, ratio,
1244                 threshold, postGain);
1245         pChannel->setLimiter(limiter);
1246         break;
1247     }
1248     default:
1249         ALOGE("%s invalid param %d", __func__, params[0]);
1250         status = -EINVAL;
1251         break;
1252     }
1253 
1254     ALOGVV("%s end param: %d, status: %d", __func__, params[0], status);
1255     return status;
1256 } /* end DP_setParameter */
1257 
1258 /* Effect Control Interface Implementation: get_descriptor */
DP_getDescriptor(effect_handle_t self,effect_descriptor_t * pDescriptor)1259 int DP_getDescriptor(effect_handle_t self,
1260         effect_descriptor_t *pDescriptor)
1261 {
1262     DynamicsProcessingContext * pContext = (DynamicsProcessingContext *) self;
1263 
1264     if (pContext == NULL || pDescriptor == NULL) {
1265         ALOGE("DP_getDescriptor() invalid param");
1266         return -EINVAL;
1267     }
1268 
1269     *pDescriptor = gDPDescriptor;
1270 
1271     return 0;
1272 } /* end DP_getDescriptor */
1273 
1274 
1275 // effect_handle_t interface implementation for Dynamics Processing effect
1276 const struct effect_interface_s gDPInterface = {
1277         DP_process,
1278         DP_command,
1279         DP_getDescriptor,
1280         NULL,
1281 };
1282 
1283 extern "C" {
1284 // This is the only symbol that needs to be exported
1285 __attribute__ ((visibility ("default")))
1286 audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
1287     .tag = AUDIO_EFFECT_LIBRARY_TAG,
1288     .version = EFFECT_LIBRARY_API_VERSION,
1289     .name = "Dynamics Processing Library",
1290     .implementor = "The Android Open Source Project",
1291     .create_effect = DPLib_Create,
1292     .release_effect = DPLib_Release,
1293     .get_descriptor = DPLib_GetDescriptor,
1294 };
1295 
1296 }; // extern "C"
1297