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