rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 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> |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 31 | #include <dsp/DPFrequency.h> |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 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_FIRST), |
| 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. |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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; |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 190 | pContext->mConfig.inputCfg.samplingRate = 48000; |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 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; |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 198 | pContext->mConfig.outputCfg.samplingRate = 48000; |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 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 | |
| 212 | void DP_changeVariant(DynamicsProcessingContext *pContext, int newVariant) { |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 213 | ALOGV("DP_changeVariant from %d to %d", pContext->mCurrentVariant, newVariant); |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 214 | switch(newVariant) { |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 215 | case VARIANT_FAVOR_FREQUENCY_RESOLUTION: { |
| 216 | pContext->mCurrentVariant = VARIANT_FAVOR_FREQUENCY_RESOLUTION; |
| 217 | delete pContext->mPDynamics; |
| 218 | pContext->mPDynamics = new dp_fx::DPFrequency(); |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 219 | break; |
| 220 | } |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 221 | default: { |
| 222 | ALOGW("DynamicsProcessing variant %d not available for creation", newVariant); |
| 223 | break; |
| 224 | } |
| 225 | } //switch |
| 226 | } |
| 227 | |
| 228 | static inline bool isPowerOf2(unsigned long n) { |
| 229 | return (n & (n - 1)) == 0; |
| 230 | } |
| 231 | |
| 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 | } |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // |
| 262 | //--- Effect Library Interface Implementation |
| 263 | // |
| 264 | |
| 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 | |
| 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 | |
| 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 | // |
| 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); |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 351 | |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 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 | int DP_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize, |
| 371 | void *pCmdData, uint32_t *replySize, void *pReplyData) { |
| 372 | |
| 373 | DynamicsProcessingContext * pContext = (DynamicsProcessingContext *)self; |
| 374 | |
| 375 | if (pContext == NULL || pContext->mState == DYNAMICS_PROCESSING_STATE_UNINITIALIZED) { |
| 376 | ALOGE("DP_command() called with NULL context or uninitialized state."); |
| 377 | return -EINVAL; |
| 378 | } |
| 379 | |
| 380 | ALOGV("DP_command command %d cmdSize %d",cmdCode, cmdSize); |
| 381 | switch (cmdCode) { |
| 382 | case EFFECT_CMD_INIT: |
| 383 | if (pReplyData == NULL || *replySize != sizeof(int)) { |
| 384 | ALOGE("EFFECT_CMD_INIT wrong replyData or repySize"); |
| 385 | return -EINVAL; |
| 386 | } |
| 387 | *(int *) pReplyData = DP_init(pContext); |
| 388 | break; |
| 389 | case EFFECT_CMD_SET_CONFIG: |
| 390 | if (pCmdData == NULL || cmdSize != sizeof(effect_config_t) |
| 391 | || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
| 392 | ALOGE("EFFECT_CMD_SET_CONFIG error with pCmdData, cmdSize, pReplyData or replySize"); |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | *(int *) pReplyData = DP_setConfig(pContext, |
| 396 | (effect_config_t *) pCmdData); |
| 397 | break; |
| 398 | case EFFECT_CMD_GET_CONFIG: |
| 399 | if (pReplyData == NULL || |
| 400 | *replySize != sizeof(effect_config_t)) { |
| 401 | ALOGE("EFFECT_CMD_GET_CONFIG wrong replyData or repySize"); |
| 402 | return -EINVAL; |
| 403 | } |
| 404 | DP_getConfig(pContext, (effect_config_t *)pReplyData); |
| 405 | break; |
| 406 | case EFFECT_CMD_RESET: |
| 407 | DP_reset(pContext); |
| 408 | break; |
| 409 | case EFFECT_CMD_ENABLE: |
| 410 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
| 411 | ALOGE("EFFECT_CMD_ENABLE wrong replyData or repySize"); |
| 412 | return -EINVAL; |
| 413 | } |
| 414 | if (pContext->mState != DYNAMICS_PROCESSING_STATE_INITIALIZED) { |
| 415 | ALOGE("EFFECT_CMD_ENABLE state not initialized"); |
| 416 | *(int *)pReplyData = -ENOSYS; |
| 417 | } else { |
| 418 | pContext->mState = DYNAMICS_PROCESSING_STATE_ACTIVE; |
| 419 | ALOGV("EFFECT_CMD_ENABLE() OK"); |
| 420 | *(int *)pReplyData = 0; |
| 421 | } |
| 422 | break; |
| 423 | case EFFECT_CMD_DISABLE: |
| 424 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
| 425 | ALOGE("EFFECT_CMD_DISABLE wrong replyData or repySize"); |
| 426 | return -EINVAL; |
| 427 | } |
| 428 | if (pContext->mState != DYNAMICS_PROCESSING_STATE_ACTIVE) { |
| 429 | ALOGE("EFFECT_CMD_DISABLE state not active"); |
| 430 | *(int *)pReplyData = -ENOSYS; |
| 431 | } else { |
| 432 | pContext->mState = DYNAMICS_PROCESSING_STATE_INITIALIZED; |
| 433 | ALOGV("EFFECT_CMD_DISABLE() OK"); |
| 434 | *(int *)pReplyData = 0; |
| 435 | } |
| 436 | break; |
| 437 | case EFFECT_CMD_GET_PARAM: { |
| 438 | if (pCmdData == NULL || pReplyData == NULL || replySize == NULL) { |
| 439 | ALOGE("null pCmdData or pReplyData or replySize"); |
| 440 | return -EINVAL; |
| 441 | } |
| 442 | effect_param_t *pEffectParam = (effect_param_t *) pCmdData; |
| 443 | uint32_t expectedCmdSize = DP_getParameterCmdSize(pEffectParam->psize, |
| 444 | pEffectParam->data); |
| 445 | if (cmdSize != expectedCmdSize || *replySize < expectedCmdSize) { |
| 446 | ALOGE("error cmdSize: %d, expetedCmdSize: %d, replySize: %d", |
| 447 | cmdSize, expectedCmdSize, *replySize); |
| 448 | return -EINVAL; |
| 449 | } |
| 450 | |
| 451 | ALOGVV("DP_command expectedCmdSize: %d", expectedCmdSize); |
| 452 | memcpy(pReplyData, pCmdData, expectedCmdSize); |
| 453 | effect_param_t *p = (effect_param_t *)pReplyData; |
| 454 | |
| 455 | uint32_t voffset = computeParamVOffset(p); |
| 456 | |
| 457 | p->status = DP_getParameter(pContext, |
| 458 | p->psize, |
| 459 | p->data, |
| 460 | &p->vsize, |
| 461 | p->data + voffset); |
| 462 | *replySize = sizeof(effect_param_t) + voffset + p->vsize; |
| 463 | |
| 464 | ALOGVV("DP_command replysize %u, status %d" , *replySize, p->status); |
| 465 | break; |
| 466 | } |
| 467 | case EFFECT_CMD_SET_PARAM: { |
| 468 | if (pCmdData == NULL || |
| 469 | cmdSize < (sizeof(effect_param_t) + sizeof(int32_t) + sizeof(int32_t)) || |
| 470 | pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) { |
| 471 | ALOGE("\tLVM_ERROR : DynamicsProcessing cmdCode Case: " |
| 472 | "EFFECT_CMD_SET_PARAM: ERROR"); |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | |
| 476 | effect_param_t * const p = (effect_param_t *) pCmdData; |
| 477 | const uint32_t voffset = computeParamVOffset(p); |
| 478 | |
| 479 | *(int *)pReplyData = DP_setParameter(pContext, |
| 480 | p->psize, |
| 481 | (void *)p->data, |
| 482 | p->vsize, |
| 483 | p->data + voffset); |
| 484 | break; |
| 485 | } |
| 486 | case EFFECT_CMD_SET_DEVICE: |
| 487 | case EFFECT_CMD_SET_VOLUME: |
| 488 | case EFFECT_CMD_SET_AUDIO_MODE: |
| 489 | break; |
| 490 | |
| 491 | default: |
| 492 | ALOGW("DP_command invalid command %d",cmdCode); |
| 493 | return -EINVAL; |
| 494 | } |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | //register expected cmd size |
| 500 | int DP_getParameterCmdSize(uint32_t paramSize, |
| 501 | void *pParam) { |
| 502 | if (paramSize < sizeof(int32_t)) { |
| 503 | return 0; |
| 504 | } |
| 505 | int32_t param = *(int32_t*)pParam; |
| 506 | switch(param) { |
| 507 | case DP_PARAM_GET_CHANNEL_COUNT: //paramcmd |
| 508 | case DP_PARAM_ENGINE_ARCHITECTURE: |
| 509 | //effect + param |
| 510 | return (int)(sizeof(effect_param_t) + sizeof(uint32_t)); |
| 511 | case DP_PARAM_INPUT_GAIN: //paramcmd + param |
| 512 | case DP_PARAM_LIMITER: |
| 513 | case DP_PARAM_PRE_EQ: |
| 514 | case DP_PARAM_POST_EQ: |
| 515 | case DP_PARAM_MBC: |
| 516 | //effect + param |
| 517 | return (int)(sizeof(effect_param_t) + 2 * sizeof(uint32_t)); |
| 518 | case DP_PARAM_PRE_EQ_BAND: |
| 519 | case DP_PARAM_POST_EQ_BAND: |
| 520 | case DP_PARAM_MBC_BAND: |
| 521 | return (int)(sizeof(effect_param_t) + 3 * sizeof(uint32_t)); |
| 522 | } |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | //helper function |
| 527 | bool DP_checkSizesInt(uint32_t paramSize, uint32_t valueSize, uint32_t expectedParams, |
| 528 | uint32_t expectedValues) { |
| 529 | if (paramSize < expectedParams * sizeof(int32_t)) { |
| 530 | ALOGE("Invalid paramSize: %u expected %u", paramSize, |
| 531 | (uint32_t) (expectedParams * sizeof(int32_t))); |
| 532 | return false; |
| 533 | } |
| 534 | if (valueSize < expectedValues * sizeof(int32_t)) { |
| 535 | ALOGE("Invalid valueSize %u expected %u", valueSize, |
| 536 | (uint32_t)(expectedValues * sizeof(int32_t))); |
| 537 | return false; |
| 538 | } |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | static dp_fx::DPChannel* DP_getChannel(DynamicsProcessingContext *pContext, |
| 543 | int32_t channel) { |
| 544 | if (pContext->mPDynamics == NULL) { |
| 545 | return NULL; |
| 546 | } |
| 547 | dp_fx::DPChannel *pChannel = pContext->mPDynamics->getChannel(channel); |
| 548 | ALOGE_IF(pChannel == NULL, "DPChannel NULL. invalid channel %d", channel); |
| 549 | return pChannel; |
| 550 | } |
| 551 | |
| 552 | static dp_fx::DPEq* DP_getEq(DynamicsProcessingContext *pContext, int32_t channel, |
| 553 | int32_t eqType) { |
| 554 | dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel); |
| 555 | if (pChannel == NULL) { |
| 556 | return NULL; |
| 557 | } |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 558 | dp_fx::DPEq *pEq = (eqType == DP_PARAM_PRE_EQ ? pChannel->getPreEq() : |
| 559 | (eqType == DP_PARAM_POST_EQ ? pChannel->getPostEq() : NULL)); |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 560 | ALOGE_IF(pEq == NULL,"DPEq NULL invalid eq"); |
| 561 | return pEq; |
| 562 | } |
| 563 | |
| 564 | static dp_fx::DPEqBand* DP_getEqBand(DynamicsProcessingContext *pContext, int32_t channel, |
| 565 | int32_t eqType, int32_t band) { |
| 566 | dp_fx::DPEq *pEq = DP_getEq(pContext, channel, eqType); |
| 567 | if (pEq == NULL) { |
| 568 | return NULL; |
| 569 | } |
| 570 | dp_fx::DPEqBand *pEqBand = pEq->getBand(band); |
| 571 | ALOGE_IF(pEqBand == NULL, "DPEqBand NULL. invalid band %d", band); |
| 572 | return pEqBand; |
| 573 | } |
| 574 | |
| 575 | static dp_fx::DPMbc* DP_getMbc(DynamicsProcessingContext *pContext, int32_t channel) { |
| 576 | dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel); |
| 577 | if (pChannel == NULL) { |
| 578 | return NULL; |
| 579 | } |
| 580 | dp_fx::DPMbc *pMbc = pChannel->getMbc(); |
| 581 | ALOGE_IF(pMbc == NULL, "DPMbc NULL invalid MBC"); |
| 582 | return pMbc; |
| 583 | } |
| 584 | |
| 585 | static dp_fx::DPMbcBand* DP_getMbcBand(DynamicsProcessingContext *pContext, int32_t channel, |
| 586 | int32_t band) { |
| 587 | dp_fx::DPMbc *pMbc = DP_getMbc(pContext, channel); |
| 588 | if (pMbc == NULL) { |
| 589 | return NULL; |
| 590 | } |
| 591 | dp_fx::DPMbcBand *pMbcBand = pMbc->getBand(band); |
| 592 | ALOGE_IF(pMbcBand == NULL, "pMbcBand NULL. invalid band %d", band); |
| 593 | return pMbcBand; |
| 594 | } |
| 595 | |
| 596 | int DP_getParameter(DynamicsProcessingContext *pContext, |
| 597 | uint32_t paramSize, |
| 598 | void *pParam, |
| 599 | uint32_t *pValueSize, |
| 600 | void *pValue) { |
| 601 | int status = 0; |
| 602 | int32_t *params = (int32_t *)pParam; |
| 603 | static_assert(sizeof(float) == sizeof(int32_t) && sizeof(float) == sizeof(value_t) && |
| 604 | alignof(float) == alignof(int32_t) && alignof(float) == alignof(value_t), |
| 605 | "Size/alignment mismatch for float/int32_t/value_t"); |
| 606 | value_t *values = reinterpret_cast<value_t*>(pValue); |
| 607 | |
| 608 | ALOGVV("%s start", __func__); |
| 609 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 610 | for (size_t i = 0; i < paramSize/sizeof(int32_t); i++) { |
| 611 | ALOGVV("Param[%zu] %d", i, params[i]); |
| 612 | } |
| 613 | #endif |
| 614 | if (paramSize < sizeof(int32_t)) { |
| 615 | ALOGE("%s invalid paramSize: %u", __func__, paramSize); |
| 616 | return -EINVAL; |
| 617 | } |
| 618 | const int32_t command = params[0]; |
| 619 | switch (command) { |
| 620 | case DP_PARAM_GET_CHANNEL_COUNT: { |
| 621 | if (!DP_checkSizesInt(paramSize,*pValueSize, 1 /*params*/, 1 /*values*/)) { |
| 622 | ALOGE("%s DP_PARAM_GET_CHANNEL_COUNT (cmd %d) invalid sizes.", __func__, command); |
| 623 | status = -EINVAL; |
| 624 | break; |
| 625 | } |
| 626 | *pValueSize = sizeof(uint32_t); |
| 627 | *(uint32_t *)pValue = (uint32_t)audio_channel_count_from_out_mask( |
| 628 | pContext->mConfig.inputCfg.channels); |
| 629 | ALOGVV("%s DP_PARAM_GET_CHANNEL_COUNT channels %d", __func__, *(int32_t *)pValue); |
| 630 | break; |
| 631 | } |
| 632 | case DP_PARAM_ENGINE_ARCHITECTURE: { |
| 633 | ALOGVV("engine architecture paramsize: %d valuesize %d",paramSize, *pValueSize); |
| 634 | if (!DP_checkSizesInt(paramSize, *pValueSize, 1 /*params*/, 9 /*values*/)) { |
| 635 | ALOGE("%s DP_PARAM_ENGINE_ARCHITECTURE (cmd %d) invalid sizes.", __func__, command); |
| 636 | status = -EINVAL; |
| 637 | break; |
| 638 | } |
| 639 | // Number[] params = { PARAM_ENGINE_ARCHITECTURE }; |
| 640 | // Number[] values = { 0 /*0 variant */, |
| 641 | // 0.0f /* 1 preferredFrameDuration */, |
| 642 | // 0 /*2 preEqInUse */, |
| 643 | // 0 /*3 preEqBandCount */, |
| 644 | // 0 /*4 mbcInUse */, |
| 645 | // 0 /*5 mbcBandCount*/, |
| 646 | // 0 /*6 postEqInUse */, |
| 647 | // 0 /*7 postEqBandCount */, |
| 648 | // 0 /*8 limiterInUse */}; |
| 649 | if (pContext->mPDynamics == NULL) { |
| 650 | ALOGE("%s DP_PARAM_ENGINE_ARCHITECTURE error mPDynamics is NULL", __func__); |
| 651 | status = -EINVAL; |
| 652 | break; |
| 653 | } |
| 654 | values[0].i = pContext->mCurrentVariant; |
| 655 | values[1].f = pContext->mPreferredFrameDuration; |
| 656 | values[2].i = pContext->mPDynamics->isPreEQInUse(); |
| 657 | values[3].i = pContext->mPDynamics->getPreEqBandCount(); |
| 658 | values[4].i = pContext->mPDynamics->isMbcInUse(); |
| 659 | values[5].i = pContext->mPDynamics->getMbcBandCount(); |
| 660 | values[6].i = pContext->mPDynamics->isPostEqInUse(); |
| 661 | values[7].i = pContext->mPDynamics->getPostEqBandCount(); |
| 662 | values[8].i = pContext->mPDynamics->isLimiterInUse(); |
| 663 | |
| 664 | *pValueSize = sizeof(value_t) * 9; |
| 665 | |
| 666 | ALOGVV(" variant %d, preferredFrameDuration: %f, preEqInuse %d, bands %d, mbcinuse %d," |
| 667 | "mbcbands %d, posteqInUse %d, bands %d, limiterinuse %d", |
| 668 | values[0].i, values[1].f, values[2].i, values[3].i, values[4].i, values[5].i, |
| 669 | values[6].i, values[7].i, values[8].i); |
| 670 | break; |
| 671 | } |
| 672 | case DP_PARAM_INPUT_GAIN: { |
| 673 | ALOGVV("engine get PARAM_INPUT_GAIN paramsize: %d valuesize %d",paramSize, *pValueSize); |
| 674 | if (!DP_checkSizesInt(paramSize, *pValueSize, 2 /*params*/, 1 /*values*/)) { |
| 675 | ALOGE("%s get PARAM_INPUT_GAIN invalid sizes.", __func__); |
| 676 | status = -EINVAL; |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | const int32_t channel = params[1]; |
| 681 | dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel); |
| 682 | if (pChannel == NULL) { |
| 683 | ALOGE("%s get PARAM_INPUT_GAIN invalid channel %d", __func__, channel); |
| 684 | status = -EINVAL; |
| 685 | break; |
| 686 | } |
| 687 | values[0].f = pChannel->getInputGain(); |
| 688 | *pValueSize = sizeof(value_t) * 1; |
| 689 | |
| 690 | ALOGVV(" channel: %d, input gain %f\n", channel, values[0].f); |
| 691 | break; |
| 692 | } |
| 693 | case DP_PARAM_PRE_EQ: |
| 694 | case DP_PARAM_POST_EQ: { |
| 695 | ALOGVV("engine get PARAM_*_EQ paramsize: %d valuesize %d",paramSize, *pValueSize); |
| 696 | if (!DP_checkSizesInt(paramSize, *pValueSize, 2 /*params*/, 3 /*values*/)) { |
| 697 | ALOGE("%s get PARAM_*_EQ (cmd %d) invalid sizes.", __func__, command); |
| 698 | status = -EINVAL; |
| 699 | break; |
| 700 | } |
| 701 | // Number[] params = {paramSet == PARAM_PRE_EQ ? PARAM_PRE_EQ : PARAM_POST_EQ, |
| 702 | // channelIndex}; |
| 703 | // Number[] values = {0 /*0 in use */, |
| 704 | // 0 /*1 enabled*/, |
| 705 | // 0 /*2 band count */}; |
| 706 | const int32_t channel = params[1]; |
| 707 | |
| 708 | dp_fx::DPEq *pEq = DP_getEq(pContext, channel, command); |
| 709 | if (pEq == NULL) { |
| 710 | ALOGE("%s get PARAM_*_EQ invalid eq", __func__); |
| 711 | status = -EINVAL; |
| 712 | break; |
| 713 | } |
| 714 | values[0].i = pEq->isInUse(); |
| 715 | values[1].i = pEq->isEnabled(); |
| 716 | values[2].i = pEq->getBandCount(); |
| 717 | *pValueSize = sizeof(value_t) * 3; |
| 718 | |
| 719 | ALOGVV(" %s channel: %d, inUse::%d, enabled:%d, bandCount:%d\n", |
| 720 | (command == DP_PARAM_PRE_EQ ? "preEq" : "postEq"), channel, |
| 721 | values[0].i, values[1].i, values[2].i); |
| 722 | break; |
| 723 | } |
| 724 | case DP_PARAM_PRE_EQ_BAND: |
| 725 | case DP_PARAM_POST_EQ_BAND: { |
| 726 | ALOGVV("engine get PARAM_*_EQ_BAND paramsize: %d valuesize %d",paramSize, *pValueSize); |
| 727 | if (!DP_checkSizesInt(paramSize, *pValueSize, 3 /*params*/, 3 /*values*/)) { |
| 728 | ALOGE("%s get PARAM_*_EQ_BAND (cmd %d) invalid sizes.", __func__, command); |
| 729 | status = -EINVAL; |
| 730 | break; |
| 731 | } |
| 732 | // Number[] params = {paramSet, |
| 733 | // channelIndex, |
| 734 | // bandIndex}; |
| 735 | // Number[] values = {(eqBand.isEnabled() ? 1 : 0), |
| 736 | // eqBand.getCutoffFrequency(), |
| 737 | // eqBand.getGain()}; |
| 738 | const int32_t channel = params[1]; |
| 739 | const int32_t band = params[2]; |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 740 | int eqCommand = (command == DP_PARAM_PRE_EQ_BAND ? DP_PARAM_PRE_EQ : |
| 741 | (command == DP_PARAM_POST_EQ_BAND ? DP_PARAM_POST_EQ : -1)); |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 742 | |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 743 | dp_fx::DPEqBand *pEqBand = DP_getEqBand(pContext, channel, eqCommand, band); |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 744 | if (pEqBand == NULL) { |
| 745 | ALOGE("%s get PARAM_*_EQ_BAND invalid channel %d or band %d", __func__, channel, band); |
| 746 | status = -EINVAL; |
| 747 | break; |
| 748 | } |
| 749 | |
| 750 | values[0].i = pEqBand->isEnabled(); |
| 751 | values[1].f = pEqBand->getCutoffFrequency(); |
| 752 | values[2].f = pEqBand->getGain(); |
| 753 | *pValueSize = sizeof(value_t) * 3; |
| 754 | |
| 755 | ALOGVV("%s channel: %d, band::%d, enabled:%d, cutoffFrequency:%f, gain%f\n", |
| 756 | (command == DP_PARAM_PRE_EQ_BAND ? "preEqBand" : "postEqBand"), channel, band, |
| 757 | values[0].i, values[1].f, values[2].f); |
| 758 | break; |
| 759 | } |
| 760 | case DP_PARAM_MBC: { |
| 761 | ALOGVV("engine get PDP_PARAM_MBC paramsize: %d valuesize %d",paramSize, *pValueSize); |
| 762 | if (!DP_checkSizesInt(paramSize, *pValueSize, 2 /*params*/, 3 /*values*/)) { |
| 763 | ALOGE("%s get PDP_PARAM_MBC (cmd %d) invalid sizes.", __func__, command); |
| 764 | status = -EINVAL; |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | // Number[] params = {PARAM_MBC, |
| 769 | // channelIndex}; |
| 770 | // Number[] values = {0 /*0 in use */, |
| 771 | // 0 /*1 enabled*/, |
| 772 | // 0 /*2 band count */}; |
| 773 | |
| 774 | const int32_t channel = params[1]; |
| 775 | |
| 776 | dp_fx::DPMbc *pMbc = DP_getMbc(pContext, channel); |
| 777 | if (pMbc == NULL) { |
| 778 | ALOGE("%s get PDP_PARAM_MBC invalid MBC", __func__); |
| 779 | status = -EINVAL; |
| 780 | break; |
| 781 | } |
| 782 | |
| 783 | values[0].i = pMbc->isInUse(); |
| 784 | values[1].i = pMbc->isEnabled(); |
| 785 | values[2].i = pMbc->getBandCount(); |
| 786 | *pValueSize = sizeof(value_t) * 3; |
| 787 | |
| 788 | ALOGVV("DP_PARAM_MBC channel: %d, inUse::%d, enabled:%d, bandCount:%d\n", channel, |
| 789 | values[0].i, values[1].i, values[2].i); |
| 790 | break; |
| 791 | } |
| 792 | case DP_PARAM_MBC_BAND: { |
| 793 | ALOGVV("engine get DP_PARAM_MBC_BAND paramsize: %d valuesize %d",paramSize, *pValueSize); |
| 794 | if (!DP_checkSizesInt(paramSize, *pValueSize, 3 /*params*/, 11 /*values*/)) { |
| 795 | ALOGE("%s get DP_PARAM_MBC_BAND (cmd %d) invalid sizes.", __func__, command); |
| 796 | status = -EINVAL; |
| 797 | break; |
| 798 | } |
| 799 | // Number[] params = {PARAM_MBC_BAND, |
| 800 | // channelIndex, |
| 801 | // bandIndex}; |
| 802 | // Number[] values = {0 /*0 enabled */, |
| 803 | // 0.0f /*1 cutoffFrequency */, |
| 804 | // 0.0f /*2 AttackTime */, |
| 805 | // 0.0f /*3 ReleaseTime */, |
| 806 | // 0.0f /*4 Ratio */, |
| 807 | // 0.0f /*5 Threshold */, |
| 808 | // 0.0f /*6 KneeWidth */, |
| 809 | // 0.0f /*7 NoiseGateThreshold */, |
| 810 | // 0.0f /*8 ExpanderRatio */, |
| 811 | // 0.0f /*9 PreGain */, |
| 812 | // 0.0f /*10 PostGain*/}; |
| 813 | |
| 814 | const int32_t channel = params[1]; |
| 815 | const int32_t band = params[2]; |
| 816 | |
| 817 | dp_fx::DPMbcBand *pMbcBand = DP_getMbcBand(pContext, channel, band); |
| 818 | if (pMbcBand == NULL) { |
| 819 | ALOGE("%s get PARAM_MBC_BAND invalid channel %d or band %d", __func__, channel, band); |
| 820 | status = -EINVAL; |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | values[0].i = pMbcBand->isEnabled(); |
| 825 | values[1].f = pMbcBand->getCutoffFrequency(); |
| 826 | values[2].f = pMbcBand->getAttackTime(); |
| 827 | values[3].f = pMbcBand->getReleaseTime(); |
| 828 | values[4].f = pMbcBand->getRatio(); |
| 829 | values[5].f = pMbcBand->getThreshold(); |
| 830 | values[6].f = pMbcBand->getKneeWidth(); |
| 831 | values[7].f = pMbcBand->getNoiseGateThreshold(); |
| 832 | values[8].f = pMbcBand->getExpanderRatio(); |
| 833 | values[9].f = pMbcBand->getPreGain(); |
| 834 | values[10].f = pMbcBand->getPostGain(); |
| 835 | |
| 836 | *pValueSize = sizeof(value_t) * 11; |
| 837 | ALOGVV(" mbcBand channel: %d, band::%d, enabled:%d, cutoffFrequency:%f, attackTime:%f," |
| 838 | "releaseTime:%f, ratio:%f, threshold:%f, kneeWidth:%f, noiseGateThreshold:%f," |
| 839 | "expanderRatio:%f, preGain:%f, postGain:%f\n", channel, band, values[0].i, |
| 840 | values[1].f, values[2].f, values[3].f, values[4].f, values[5].f, values[6].f, |
| 841 | values[7].f, values[8].f, values[9].f, values[10].f); |
| 842 | break; |
| 843 | } |
| 844 | case DP_PARAM_LIMITER: { |
| 845 | ALOGVV("engine get DP_PARAM_LIMITER paramsize: %d valuesize %d",paramSize, *pValueSize); |
| 846 | if (!DP_checkSizesInt(paramSize, *pValueSize, 2 /*params*/, 8 /*values*/)) { |
| 847 | ALOGE("%s DP_PARAM_LIMITER (cmd %d) invalid sizes.", __func__, command); |
| 848 | status = -EINVAL; |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | int32_t channel = params[1]; |
| 853 | // Number[] values = {0 /*0 in use (int)*/, |
| 854 | // 0 /*1 enabled (int)*/, |
| 855 | // 0 /*2 link group (int)*/, |
| 856 | // 0.0f /*3 attack time (float)*/, |
| 857 | // 0.0f /*4 release time (float)*/, |
| 858 | // 0.0f /*5 ratio (float)*/, |
| 859 | // 0.0f /*6 threshold (float)*/, |
| 860 | // 0.0f /*7 post gain(float)*/}; |
| 861 | dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel); |
| 862 | if (pChannel == NULL) { |
| 863 | ALOGE("%s DP_PARAM_LIMITER invalid channel %d", __func__, channel); |
| 864 | status = -EINVAL; |
| 865 | break; |
| 866 | } |
| 867 | dp_fx::DPLimiter *pLimiter = pChannel->getLimiter(); |
| 868 | if (pLimiter == NULL) { |
| 869 | ALOGE("%s DP_PARAM_LIMITER null LIMITER", __func__); |
| 870 | status = -EINVAL; |
| 871 | break; |
| 872 | } |
| 873 | values[0].i = pLimiter->isInUse(); |
| 874 | values[1].i = pLimiter->isEnabled(); |
| 875 | values[2].i = pLimiter->getLinkGroup(); |
| 876 | values[3].f = pLimiter->getAttackTime(); |
| 877 | values[4].f = pLimiter->getReleaseTime(); |
| 878 | values[5].f = pLimiter->getRatio(); |
| 879 | values[6].f = pLimiter->getThreshold(); |
| 880 | values[7].f = pLimiter->getPostGain(); |
| 881 | |
| 882 | *pValueSize = sizeof(value_t) * 8; |
| 883 | |
| 884 | ALOGVV(" Limiter channel: %d, inUse::%d, enabled:%d, linkgroup:%d attackTime:%f," |
| 885 | "releaseTime:%f, ratio:%f, threshold:%f, postGain:%f\n", |
| 886 | channel, values[0].i/*inUse*/, values[1].i/*enabled*/, values[2].i/*linkGroup*/, |
| 887 | values[3].f/*attackTime*/, values[4].f/*releaseTime*/, |
| 888 | values[5].f/*ratio*/, values[6].f/*threshold*/, |
| 889 | values[7].f/*postGain*/); |
| 890 | break; |
| 891 | } |
| 892 | default: |
| 893 | ALOGE("%s invalid param %d", __func__, params[0]); |
| 894 | status = -EINVAL; |
| 895 | break; |
| 896 | } |
| 897 | |
| 898 | ALOGVV("%s end param: %d, status: %d", __func__, params[0], status); |
| 899 | return status; |
| 900 | } /* end DP_getParameter */ |
| 901 | |
| 902 | int DP_setParameter(DynamicsProcessingContext *pContext, |
| 903 | uint32_t paramSize, |
| 904 | void *pParam, |
| 905 | uint32_t valueSize, |
| 906 | void *pValue) { |
| 907 | int status = 0; |
| 908 | int32_t *params = (int32_t *)pParam; |
| 909 | static_assert(sizeof(float) == sizeof(int32_t) && sizeof(float) == sizeof(value_t) && |
| 910 | alignof(float) == alignof(int32_t) && alignof(float) == alignof(value_t), |
| 911 | "Size/alignment mismatch for float/int32_t/value_t"); |
| 912 | value_t *values = reinterpret_cast<value_t*>(pValue); |
| 913 | |
| 914 | ALOGVV("%s start", __func__); |
| 915 | if (paramSize < sizeof(int32_t)) { |
| 916 | ALOGE("%s invalid paramSize: %u", __func__, paramSize); |
| 917 | return -EINVAL; |
| 918 | } |
| 919 | const int32_t command = params[0]; |
| 920 | switch (command) { |
| 921 | case DP_PARAM_ENGINE_ARCHITECTURE: { |
| 922 | ALOGVV("engine architecture paramsize: %d valuesize %d",paramSize, valueSize); |
| 923 | if (!DP_checkSizesInt(paramSize, valueSize, 1 /*params*/, 9 /*values*/)) { |
| 924 | ALOGE("%s DP_PARAM_ENGINE_ARCHITECTURE (cmd %d) invalid sizes.", __func__, command); |
| 925 | status = -EINVAL; |
| 926 | break; |
| 927 | } |
| 928 | // Number[] params = { PARAM_ENGINE_ARCHITECTURE }; |
| 929 | // Number[] values = { variant /* variant */, |
| 930 | // preferredFrameDuration, |
| 931 | // (preEqInUse ? 1 : 0), |
| 932 | // preEqBandCount, |
| 933 | // (mbcInUse ? 1 : 0), |
| 934 | // mbcBandCount, |
| 935 | // (postEqInUse ? 1 : 0), |
| 936 | // postEqBandCount, |
| 937 | // (limiterInUse ? 1 : 0)}; |
| 938 | const int32_t variant = values[0].i; |
| 939 | const float preferredFrameDuration = values[1].f; |
| 940 | const int32_t preEqInUse = values[2].i; |
| 941 | const int32_t preEqBandCount = values[3].i; |
| 942 | const int32_t mbcInUse = values[4].i; |
| 943 | const int32_t mbcBandCount = values[5].i; |
| 944 | const int32_t postEqInUse = values[6].i; |
| 945 | const int32_t postEqBandCount = values[7].i; |
| 946 | const int32_t limiterInUse = values[8].i; |
| 947 | ALOGVV("variant %d, preEqInuse %d, bands %d, mbcinuse %d, mbcbands %d, posteqInUse %d," |
| 948 | "bands %d, limiterinuse %d", variant, preEqInUse, preEqBandCount, mbcInUse, |
| 949 | mbcBandCount, postEqInUse, postEqBandCount, limiterInUse); |
| 950 | |
| 951 | //set variant (instantiate effect) |
| 952 | //initArchitecture for effect |
| 953 | DP_changeVariant(pContext, variant); |
| 954 | if (pContext->mPDynamics == NULL) { |
| 955 | ALOGE("%s DP_PARAM_ENGINE_ARCHITECTURE error setting variant %d", __func__, variant); |
| 956 | status = -EINVAL; |
| 957 | break; |
| 958 | } |
| 959 | pContext->mPreferredFrameDuration = preferredFrameDuration; |
| 960 | pContext->mPDynamics->init((uint32_t)audio_channel_count_from_out_mask( |
| 961 | pContext->mConfig.inputCfg.channels), |
| 962 | preEqInUse != 0, (uint32_t)preEqBandCount, |
| 963 | mbcInUse != 0, (uint32_t)mbcBandCount, |
| 964 | postEqInUse != 0, (uint32_t)postEqBandCount, |
| 965 | limiterInUse != 0); |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 966 | |
| 967 | DP_configureVariant(pContext, variant); |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 968 | break; |
| 969 | } |
| 970 | case DP_PARAM_INPUT_GAIN: { |
| 971 | ALOGVV("engine DP_PARAM_INPUT_GAIN paramsize: %d valuesize %d",paramSize, valueSize); |
| 972 | if (!DP_checkSizesInt(paramSize, valueSize, 2 /*params*/, 1 /*values*/)) { |
| 973 | ALOGE("%s DP_PARAM_INPUT_GAIN invalid sizes.", __func__); |
| 974 | status = -EINVAL; |
| 975 | break; |
| 976 | } |
| 977 | |
| 978 | const int32_t channel = params[1]; |
| 979 | dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel); |
| 980 | if (pChannel == NULL) { |
| 981 | ALOGE("%s DP_PARAM_INPUT_GAIN invalid channel %d", __func__, channel); |
| 982 | status = -EINVAL; |
| 983 | break; |
| 984 | } |
| 985 | const float gain = values[0].f; |
| 986 | ALOGVV("%s DP_PARAM_INPUT_GAIN channel %d, level %f", __func__, channel, gain); |
| 987 | pChannel->setInputGain(gain); |
| 988 | break; |
| 989 | } |
| 990 | case DP_PARAM_PRE_EQ: |
| 991 | case DP_PARAM_POST_EQ: { |
| 992 | ALOGVV("engine DP_PARAM_*_EQ paramsize: %d valuesize %d",paramSize, valueSize); |
| 993 | if (!DP_checkSizesInt(paramSize, valueSize, 2 /*params*/, 3 /*values*/)) { |
| 994 | ALOGE("%s DP_PARAM_*_EQ (cmd %d) invalid sizes.", __func__, command); |
| 995 | status = -EINVAL; |
| 996 | break; |
| 997 | } |
| 998 | // Number[] params = {paramSet, |
| 999 | // channelIndex}; |
| 1000 | // Number[] values = { (eq.isInUse() ? 1 : 0), |
| 1001 | // (eq.isEnabled() ? 1 : 0), |
| 1002 | // bandCount}; |
| 1003 | const int32_t channel = params[1]; |
| 1004 | |
| 1005 | const int32_t enabled = values[1].i; |
| 1006 | const int32_t bandCount = values[2].i; |
| 1007 | ALOGVV(" %s channel: %d, inUse::%d, enabled:%d, bandCount:%d\n", |
| 1008 | (command == DP_PARAM_PRE_EQ ? "preEq" : "postEq"), channel, values[0].i, |
| 1009 | values[2].i, bandCount); |
| 1010 | |
| 1011 | dp_fx::DPEq *pEq = DP_getEq(pContext, channel, command); |
| 1012 | if (pEq == NULL) { |
| 1013 | ALOGE("%s set PARAM_*_EQ invalid channel %d or command %d", __func__, channel, |
| 1014 | command); |
| 1015 | status = -EINVAL; |
| 1016 | break; |
| 1017 | } |
| 1018 | |
| 1019 | pEq->setEnabled(enabled != 0); |
| 1020 | //fail if bandcountis different? maybe. |
| 1021 | if ((int32_t)pEq->getBandCount() != bandCount) { |
| 1022 | ALOGW("%s warning, trying to set different bandcount from %d to %d", __func__, |
| 1023 | pEq->getBandCount(), bandCount); |
| 1024 | } |
| 1025 | break; |
| 1026 | } |
| 1027 | case DP_PARAM_PRE_EQ_BAND: |
| 1028 | case DP_PARAM_POST_EQ_BAND: { |
| 1029 | ALOGVV("engine set PARAM_*_EQ_BAND paramsize: %d valuesize %d",paramSize, valueSize); |
| 1030 | if (!DP_checkSizesInt(paramSize, valueSize, 3 /*params*/, 3 /*values*/)) { |
| 1031 | ALOGE("%s PARAM_*_EQ_BAND (cmd %d) invalid sizes.", __func__, command); |
| 1032 | status = -EINVAL; |
| 1033 | break; |
| 1034 | } |
| 1035 | // Number[] values = { channelIndex, |
| 1036 | // bandIndex, |
| 1037 | // (eqBand.isEnabled() ? 1 : 0), |
| 1038 | // eqBand.getCutoffFrequency(), |
| 1039 | // eqBand.getGain()}; |
| 1040 | |
| 1041 | // Number[] params = {paramSet, |
| 1042 | // channelIndex, |
| 1043 | // bandIndex}; |
| 1044 | // Number[] values = {(eqBand.isEnabled() ? 1 : 0), |
| 1045 | // eqBand.getCutoffFrequency(), |
| 1046 | // eqBand.getGain()}; |
| 1047 | |
| 1048 | const int32_t channel = params[1]; |
| 1049 | const int32_t band = params[2]; |
| 1050 | |
| 1051 | const int32_t enabled = values[0].i; |
| 1052 | const float cutoffFrequency = values[1].f; |
| 1053 | const float gain = values[2].f; |
| 1054 | |
| 1055 | |
| 1056 | ALOGVV(" %s channel: %d, band::%d, enabled:%d, cutoffFrequency:%f, gain%f\n", |
| 1057 | (command == DP_PARAM_PRE_EQ_BAND ? "preEqBand" : "postEqBand"), channel, band, |
| 1058 | enabled, cutoffFrequency, gain); |
| 1059 | |
rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 1060 | int eqCommand = (command == DP_PARAM_PRE_EQ_BAND ? DP_PARAM_PRE_EQ : |
| 1061 | (command == DP_PARAM_POST_EQ_BAND ? DP_PARAM_POST_EQ : -1)); |
| 1062 | dp_fx::DPEq *pEq = DP_getEq(pContext, channel, eqCommand); |
rago | 9f011fe | 2018-02-05 09:29:56 -0800 | [diff] [blame] | 1063 | if (pEq == NULL) { |
| 1064 | ALOGE("%s set PARAM_*_EQ_BAND invalid channel %d or command %d", __func__, channel, |
| 1065 | command); |
| 1066 | status = -EINVAL; |
| 1067 | break; |
| 1068 | } |
| 1069 | |
| 1070 | dp_fx::DPEqBand eqBand; |
| 1071 | eqBand.init(enabled != 0, cutoffFrequency, gain); |
| 1072 | pEq->setBand(band, eqBand); |
| 1073 | break; |
| 1074 | } |
| 1075 | case DP_PARAM_MBC: { |
| 1076 | ALOGVV("engine DP_PARAM_MBC paramsize: %d valuesize %d",paramSize, valueSize); |
| 1077 | if (!DP_checkSizesInt(paramSize, valueSize, 2 /*params*/, 3 /*values*/)) { |
| 1078 | ALOGE("%s DP_PARAM_MBC (cmd %d) invalid sizes.", __func__, command); |
| 1079 | status = -EINVAL; |
| 1080 | break; |
| 1081 | } |
| 1082 | // Number[] params = { PARAM_MBC, |
| 1083 | // channelIndex}; |
| 1084 | // Number[] values = {(mbc.isInUse() ? 1 : 0), |
| 1085 | // (mbc.isEnabled() ? 1 : 0), |
| 1086 | // bandCount}; |
| 1087 | const int32_t channel = params[1]; |
| 1088 | |
| 1089 | const int32_t enabled = values[1].i; |
| 1090 | const int32_t bandCount = values[2].i; |
| 1091 | ALOGVV("MBC channel: %d, inUse::%d, enabled:%d, bandCount:%d\n", channel, values[0].i, |
| 1092 | enabled, bandCount); |
| 1093 | |
| 1094 | dp_fx::DPMbc *pMbc = DP_getMbc(pContext, channel); |
| 1095 | if (pMbc == NULL) { |
| 1096 | ALOGE("%s set DP_PARAM_MBC invalid channel %d ", __func__, channel); |
| 1097 | status = -EINVAL; |
| 1098 | break; |
| 1099 | } |
| 1100 | |
| 1101 | pMbc->setEnabled(enabled != 0); |
| 1102 | //fail if bandcountis different? maybe. |
| 1103 | if ((int32_t)pMbc->getBandCount() != bandCount) { |
| 1104 | ALOGW("%s warning, trying to set different bandcount from %d to %d", __func__, |
| 1105 | pMbc->getBandCount(), bandCount); |
| 1106 | } |
| 1107 | break; |
| 1108 | } |
| 1109 | case DP_PARAM_MBC_BAND: { |
| 1110 | ALOGVV("engine set DP_PARAM_MBC_BAND paramsize: %d valuesize %d ",paramSize, valueSize); |
| 1111 | if (!DP_checkSizesInt(paramSize, valueSize, 3 /*params*/, 11 /*values*/)) { |
| 1112 | ALOGE("%s DP_PARAM_MBC_BAND: (cmd %d) invalid sizes.", __func__, command); |
| 1113 | status = -EINVAL; |
| 1114 | break; |
| 1115 | } |
| 1116 | // Number[] params = { PARAM_MBC_BAND, |
| 1117 | // channelIndex, |
| 1118 | // bandIndex}; |
| 1119 | // Number[] values = {(mbcBand.isEnabled() ? 1 : 0), |
| 1120 | // mbcBand.getCutoffFrequency(), |
| 1121 | // mbcBand.getAttackTime(), |
| 1122 | // mbcBand.getReleaseTime(), |
| 1123 | // mbcBand.getRatio(), |
| 1124 | // mbcBand.getThreshold(), |
| 1125 | // mbcBand.getKneeWidth(), |
| 1126 | // mbcBand.getNoiseGateThreshold(), |
| 1127 | // mbcBand.getExpanderRatio(), |
| 1128 | // mbcBand.getPreGain(), |
| 1129 | // mbcBand.getPostGain()}; |
| 1130 | |
| 1131 | const int32_t channel = params[1]; |
| 1132 | const int32_t band = params[2]; |
| 1133 | |
| 1134 | const int32_t enabled = values[0].i; |
| 1135 | const float cutoffFrequency = values[1].f; |
| 1136 | const float attackTime = values[2].f; |
| 1137 | const float releaseTime = values[3].f; |
| 1138 | const float ratio = values[4].f; |
| 1139 | const float threshold = values[5].f; |
| 1140 | const float kneeWidth = values[6].f; |
| 1141 | const float noiseGateThreshold = values[7].f; |
| 1142 | const float expanderRatio = values[8].f; |
| 1143 | const float preGain = values[9].f; |
| 1144 | const float postGain = values[10].f; |
| 1145 | |
| 1146 | ALOGVV(" mbcBand channel: %d, band::%d, enabled:%d, cutoffFrequency:%f, attackTime:%f," |
| 1147 | "releaseTime:%f, ratio:%f, threshold:%f, kneeWidth:%f, noiseGateThreshold:%f," |
| 1148 | "expanderRatio:%f, preGain:%f, postGain:%f\n", |
| 1149 | channel, band, enabled, cutoffFrequency, attackTime, releaseTime, ratio, |
| 1150 | threshold, kneeWidth, noiseGateThreshold, expanderRatio, preGain, postGain); |
| 1151 | |
| 1152 | dp_fx::DPMbc *pMbc = DP_getMbc(pContext, channel); |
| 1153 | if (pMbc == NULL) { |
| 1154 | ALOGE("%s set DP_PARAM_MBC_BAND invalid channel %d", __func__, channel); |
| 1155 | status = -EINVAL; |
| 1156 | break; |
| 1157 | } |
| 1158 | |
| 1159 | dp_fx::DPMbcBand mbcBand; |
| 1160 | mbcBand.init(enabled != 0, cutoffFrequency, attackTime, releaseTime, ratio, threshold, |
| 1161 | kneeWidth, noiseGateThreshold, expanderRatio, preGain, postGain); |
| 1162 | pMbc->setBand(band, mbcBand); |
| 1163 | break; |
| 1164 | } |
| 1165 | case DP_PARAM_LIMITER: { |
| 1166 | ALOGVV("engine DP_PARAM_LIMITER paramsize: %d valuesize %d",paramSize, valueSize); |
| 1167 | if (!DP_checkSizesInt(paramSize, valueSize, 2 /*params*/, 8 /*values*/)) { |
| 1168 | ALOGE("%s DP_PARAM_LIMITER (cmd %d) invalid sizes.", __func__, command); |
| 1169 | status = -EINVAL; |
| 1170 | break; |
| 1171 | } |
| 1172 | // Number[] params = { PARAM_LIMITER, |
| 1173 | // channelIndex}; |
| 1174 | // Number[] values = {(limiter.isInUse() ? 1 : 0), |
| 1175 | // (limiter.isEnabled() ? 1 : 0), |
| 1176 | // limiter.getLinkGroup(), |
| 1177 | // limiter.getAttackTime(), |
| 1178 | // limiter.getReleaseTime(), |
| 1179 | // limiter.getRatio(), |
| 1180 | // limiter.getThreshold(), |
| 1181 | // limiter.getPostGain()}; |
| 1182 | |
| 1183 | const int32_t channel = params[1]; |
| 1184 | |
| 1185 | const int32_t inUse = values[0].i; |
| 1186 | const int32_t enabled = values[1].i; |
| 1187 | const int32_t linkGroup = values[2].i; |
| 1188 | const float attackTime = values[3].f; |
| 1189 | const float releaseTime = values[4].f; |
| 1190 | const float ratio = values[5].f; |
| 1191 | const float threshold = values[6].f; |
| 1192 | const float postGain = values[7].f; |
| 1193 | |
| 1194 | ALOGVV(" Limiter channel: %d, inUse::%d, enabled:%d, linkgroup:%d attackTime:%f," |
| 1195 | "releaseTime:%f, ratio:%f, threshold:%f, postGain:%f\n", channel, inUse, |
| 1196 | enabled, linkGroup, attackTime, releaseTime, ratio, threshold, postGain); |
| 1197 | |
| 1198 | dp_fx::DPChannel * pChannel = DP_getChannel(pContext, channel); |
| 1199 | if (pChannel == NULL) { |
| 1200 | ALOGE("%s DP_PARAM_LIMITER invalid channel %d", __func__, channel); |
| 1201 | status = -EINVAL; |
| 1202 | break; |
| 1203 | } |
| 1204 | dp_fx::DPLimiter limiter; |
| 1205 | limiter.init(inUse != 0, enabled != 0, linkGroup, attackTime, releaseTime, ratio, |
| 1206 | threshold, postGain); |
| 1207 | pChannel->setLimiter(limiter); |
| 1208 | break; |
| 1209 | } |
| 1210 | default: |
| 1211 | ALOGE("%s invalid param %d", __func__, params[0]); |
| 1212 | status = -EINVAL; |
| 1213 | break; |
| 1214 | } |
| 1215 | |
| 1216 | ALOGVV("%s end param: %d, status: %d", __func__, params[0], status); |
| 1217 | return status; |
| 1218 | } /* end DP_setParameter */ |
| 1219 | |
| 1220 | /* Effect Control Interface Implementation: get_descriptor */ |
| 1221 | int DP_getDescriptor(effect_handle_t self, |
| 1222 | effect_descriptor_t *pDescriptor) |
| 1223 | { |
| 1224 | DynamicsProcessingContext * pContext = (DynamicsProcessingContext *) self; |
| 1225 | |
| 1226 | if (pContext == NULL || pDescriptor == NULL) { |
| 1227 | ALOGE("DP_getDescriptor() invalid param"); |
| 1228 | return -EINVAL; |
| 1229 | } |
| 1230 | |
| 1231 | *pDescriptor = gDPDescriptor; |
| 1232 | |
| 1233 | return 0; |
| 1234 | } /* end DP_getDescriptor */ |
| 1235 | |
| 1236 | |
| 1237 | // effect_handle_t interface implementation for Dynamics Processing effect |
| 1238 | const struct effect_interface_s gDPInterface = { |
| 1239 | DP_process, |
| 1240 | DP_command, |
| 1241 | DP_getDescriptor, |
| 1242 | NULL, |
| 1243 | }; |
| 1244 | |
| 1245 | extern "C" { |
| 1246 | // This is the only symbol that needs to be exported |
| 1247 | __attribute__ ((visibility ("default"))) |
| 1248 | audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = { |
| 1249 | .tag = AUDIO_EFFECT_LIBRARY_TAG, |
| 1250 | .version = EFFECT_LIBRARY_API_VERSION, |
| 1251 | .name = "Dynamics Processing Library", |
| 1252 | .implementor = "The Android Open Source Project", |
| 1253 | .create_effect = DPLib_Create, |
| 1254 | .release_effect = DPLib_Release, |
| 1255 | .get_descriptor = DPLib_GetDescriptor, |
| 1256 | }; |
| 1257 | |
| 1258 | }; // extern "C" |
| 1259 | |