EffectDynamicsProcessing.cpp 49 KB

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