EffectEqualizer.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * Copyright (C) 2009 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 "Equalizer"
  17. #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
  18. //
  19. #define LOG_NDEBUG 0
  20. #include <assert.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <new>
  24. #include <log/log.h>
  25. #include "AudioEqualizer.h"
  26. #include "AudioBiquadFilter.h"
  27. #include "AudioFormatAdapter.h"
  28. #include <audio_effects/effect_equalizer.h>
  29. // effect_handle_t interface implementation for equalizer effect
  30. extern "C" const struct effect_interface_s gEqualizerInterface;
  31. enum equalizer_state_e {
  32. EQUALIZER_STATE_UNINITIALIZED,
  33. EQUALIZER_STATE_INITIALIZED,
  34. EQUALIZER_STATE_ACTIVE,
  35. };
  36. namespace android {
  37. namespace {
  38. // Google Graphic Equalizer UUID: e25aa840-543b-11df-98a5-0002a5d5c51b
  39. const effect_descriptor_t gEqualizerDescriptor = {
  40. {0x0bed4300, 0xddd6, 0x11db, 0x8f34, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
  41. {0xe25aa840, 0x543b, 0x11df, 0x98a5, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
  42. EFFECT_CONTROL_API_VERSION,
  43. (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST),
  44. 0, // TODO
  45. 1,
  46. "Graphic Equalizer",
  47. "The Android Open Source Project",
  48. };
  49. /////////////////// BEGIN EQ PRESETS ///////////////////////////////////////////
  50. const int kNumBands = 5;
  51. const uint32_t gFreqs[kNumBands] = { 50000, 125000, 900000, 3200000, 6300000 };
  52. const uint32_t gBandwidths[kNumBands] = { 0, 3600, 3600, 2400, 0 };
  53. const AudioEqualizer::BandConfig gBandsClassic[kNumBands] = {
  54. { 300, gFreqs[0], gBandwidths[0] },
  55. { 400, gFreqs[1], gBandwidths[1] },
  56. { 0, gFreqs[2], gBandwidths[2] },
  57. { 200, gFreqs[3], gBandwidths[3] },
  58. { -300, gFreqs[4], gBandwidths[4] }
  59. };
  60. const AudioEqualizer::BandConfig gBandsJazz[kNumBands] = {
  61. { -600, gFreqs[0], gBandwidths[0] },
  62. { 200, gFreqs[1], gBandwidths[1] },
  63. { 400, gFreqs[2], gBandwidths[2] },
  64. { -400, gFreqs[3], gBandwidths[3] },
  65. { -600, gFreqs[4], gBandwidths[4] }
  66. };
  67. const AudioEqualizer::BandConfig gBandsPop[kNumBands] = {
  68. { 400, gFreqs[0], gBandwidths[0] },
  69. { -400, gFreqs[1], gBandwidths[1] },
  70. { 300, gFreqs[2], gBandwidths[2] },
  71. { -400, gFreqs[3], gBandwidths[3] },
  72. { 600, gFreqs[4], gBandwidths[4] }
  73. };
  74. const AudioEqualizer::BandConfig gBandsRock[kNumBands] = {
  75. { 700, gFreqs[0], gBandwidths[0] },
  76. { 400, gFreqs[1], gBandwidths[1] },
  77. { -400, gFreqs[2], gBandwidths[2] },
  78. { 400, gFreqs[3], gBandwidths[3] },
  79. { 200, gFreqs[4], gBandwidths[4] }
  80. };
  81. const AudioEqualizer::PresetConfig gEqualizerPresets[] = {
  82. { "Classic", gBandsClassic },
  83. { "Jazz", gBandsJazz },
  84. { "Pop", gBandsPop },
  85. { "Rock", gBandsRock }
  86. };
  87. /////////////////// END EQ PRESETS /////////////////////////////////////////////
  88. static const size_t kBufferSize = 32;
  89. typedef AudioFormatAdapter<AudioEqualizer, kBufferSize> FormatAdapter;
  90. struct EqualizerContext {
  91. const struct effect_interface_s *itfe;
  92. effect_config_t config;
  93. FormatAdapter adapter;
  94. AudioEqualizer * pEqualizer;
  95. uint32_t state;
  96. };
  97. //--- local function prototypes
  98. int Equalizer_init(EqualizerContext *pContext);
  99. int Equalizer_setConfig(EqualizerContext *pContext, effect_config_t *pConfig);
  100. int Equalizer_getParameter(AudioEqualizer * pEqualizer, int32_t *pParam, uint32_t *pValueSize, void *pValue);
  101. int Equalizer_setParameter(AudioEqualizer * pEqualizer, int32_t *pParam, void *pValue);
  102. //
  103. //--- Effect Library Interface Implementation
  104. //
  105. extern "C" int EffectCreate(const effect_uuid_t *uuid,
  106. int32_t sessionId,
  107. int32_t ioId,
  108. effect_handle_t *pHandle) {
  109. int ret;
  110. int i;
  111. ALOGV("EffectLibCreateEffect start");
  112. if (pHandle == NULL || uuid == NULL) {
  113. return -EINVAL;
  114. }
  115. if (memcmp(uuid, &gEqualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
  116. return -EINVAL;
  117. }
  118. EqualizerContext *pContext = new EqualizerContext;
  119. pContext->itfe = &gEqualizerInterface;
  120. pContext->pEqualizer = NULL;
  121. pContext->state = EQUALIZER_STATE_UNINITIALIZED;
  122. ret = Equalizer_init(pContext);
  123. if (ret < 0) {
  124. ALOGW("EffectLibCreateEffect() init failed");
  125. delete pContext;
  126. return ret;
  127. }
  128. *pHandle = (effect_handle_t)pContext;
  129. pContext->state = EQUALIZER_STATE_INITIALIZED;
  130. ALOGV("EffectLibCreateEffect %p, size %d",
  131. pContext, AudioEqualizer::GetInstanceSize(kNumBands)+sizeof(EqualizerContext));
  132. return 0;
  133. } /* end EffectCreate */
  134. extern "C" int EffectRelease(effect_handle_t handle) {
  135. EqualizerContext * pContext = (EqualizerContext *)handle;
  136. ALOGV("EffectLibReleaseEffect %p", handle);
  137. if (pContext == NULL) {
  138. return -EINVAL;
  139. }
  140. pContext->state = EQUALIZER_STATE_UNINITIALIZED;
  141. pContext->pEqualizer->free();
  142. delete pContext;
  143. return 0;
  144. } /* end EffectRelease */
  145. extern "C" int EffectGetDescriptor(const effect_uuid_t *uuid,
  146. effect_descriptor_t *pDescriptor) {
  147. if (pDescriptor == NULL || uuid == NULL){
  148. ALOGV("EffectGetDescriptor() called with NULL pointer");
  149. return -EINVAL;
  150. }
  151. if (memcmp(uuid, &gEqualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
  152. *pDescriptor = gEqualizerDescriptor;
  153. return 0;
  154. }
  155. return -EINVAL;
  156. } /* end EffectGetDescriptor */
  157. //
  158. //--- local functions
  159. //
  160. #define CHECK_ARG(cond) { \
  161. if (!(cond)) { \
  162. ALOGV("Invalid argument: "#cond); \
  163. return -EINVAL; \
  164. } \
  165. }
  166. //----------------------------------------------------------------------------
  167. // Equalizer_setConfig()
  168. //----------------------------------------------------------------------------
  169. // Purpose: Set input and output audio configuration.
  170. //
  171. // Inputs:
  172. // pContext: effect engine context
  173. // pConfig: pointer to effect_config_t structure holding input and output
  174. // configuration parameters
  175. //
  176. // Outputs:
  177. //
  178. //----------------------------------------------------------------------------
  179. int Equalizer_setConfig(EqualizerContext *pContext, effect_config_t *pConfig)
  180. {
  181. ALOGV("Equalizer_setConfig start");
  182. CHECK_ARG(pContext != NULL);
  183. CHECK_ARG(pConfig != NULL);
  184. CHECK_ARG(pConfig->inputCfg.samplingRate == pConfig->outputCfg.samplingRate);
  185. CHECK_ARG(pConfig->inputCfg.channels == pConfig->outputCfg.channels);
  186. CHECK_ARG(pConfig->inputCfg.format == pConfig->outputCfg.format);
  187. CHECK_ARG((pConfig->inputCfg.channels == AUDIO_CHANNEL_OUT_MONO) ||
  188. (pConfig->inputCfg.channels == AUDIO_CHANNEL_OUT_STEREO));
  189. CHECK_ARG(pConfig->outputCfg.accessMode == EFFECT_BUFFER_ACCESS_WRITE
  190. || pConfig->outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE);
  191. CHECK_ARG(pConfig->inputCfg.format == AUDIO_FORMAT_PCM_16_BIT);
  192. int channelCount;
  193. if (pConfig->inputCfg.channels == AUDIO_CHANNEL_OUT_MONO) {
  194. channelCount = 1;
  195. } else {
  196. channelCount = 2;
  197. }
  198. CHECK_ARG(channelCount <= AudioBiquadFilter::MAX_CHANNELS);
  199. pContext->config = *pConfig;
  200. pContext->pEqualizer->configure(channelCount,
  201. pConfig->inputCfg.samplingRate);
  202. pContext->adapter.configure(*pContext->pEqualizer, channelCount,
  203. pConfig->inputCfg.format,
  204. pConfig->outputCfg.accessMode);
  205. return 0;
  206. } // end Equalizer_setConfig
  207. //----------------------------------------------------------------------------
  208. // Equalizer_getConfig()
  209. //----------------------------------------------------------------------------
  210. // Purpose: Get input and output audio configuration.
  211. //
  212. // Inputs:
  213. // pContext: effect engine context
  214. // pConfig: pointer to effect_config_t structure holding input and output
  215. // configuration parameters
  216. //
  217. // Outputs:
  218. //
  219. //----------------------------------------------------------------------------
  220. void Equalizer_getConfig(EqualizerContext *pContext, effect_config_t *pConfig)
  221. {
  222. *pConfig = pContext->config;
  223. } // end Equalizer_getConfig
  224. //----------------------------------------------------------------------------
  225. // Equalizer_init()
  226. //----------------------------------------------------------------------------
  227. // Purpose: Initialize engine with default configuration and creates
  228. // AudioEqualizer instance.
  229. //
  230. // Inputs:
  231. // pContext: effect engine context
  232. //
  233. // Outputs:
  234. //
  235. //----------------------------------------------------------------------------
  236. int Equalizer_init(EqualizerContext *pContext)
  237. {
  238. int status;
  239. ALOGV("Equalizer_init start");
  240. CHECK_ARG(pContext != NULL);
  241. if (pContext->pEqualizer != NULL) {
  242. pContext->pEqualizer->free();
  243. }
  244. pContext->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
  245. pContext->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
  246. pContext->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
  247. pContext->config.inputCfg.samplingRate = 44100;
  248. pContext->config.inputCfg.bufferProvider.getBuffer = NULL;
  249. pContext->config.inputCfg.bufferProvider.releaseBuffer = NULL;
  250. pContext->config.inputCfg.bufferProvider.cookie = NULL;
  251. pContext->config.inputCfg.mask = EFFECT_CONFIG_ALL;
  252. pContext->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
  253. pContext->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
  254. pContext->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
  255. pContext->config.outputCfg.samplingRate = 44100;
  256. pContext->config.outputCfg.bufferProvider.getBuffer = NULL;
  257. pContext->config.outputCfg.bufferProvider.releaseBuffer = NULL;
  258. pContext->config.outputCfg.bufferProvider.cookie = NULL;
  259. pContext->config.outputCfg.mask = EFFECT_CONFIG_ALL;
  260. pContext->pEqualizer = AudioEqualizer::CreateInstance(
  261. NULL,
  262. kNumBands,
  263. AudioBiquadFilter::MAX_CHANNELS,
  264. 44100,
  265. gEqualizerPresets,
  266. ARRAY_SIZE(gEqualizerPresets));
  267. for (int i = 0; i < kNumBands; ++i) {
  268. pContext->pEqualizer->setFrequency(i, gFreqs[i]);
  269. pContext->pEqualizer->setBandwidth(i, gBandwidths[i]);
  270. }
  271. pContext->pEqualizer->enable(true);
  272. Equalizer_setConfig(pContext, &pContext->config);
  273. return 0;
  274. } // end Equalizer_init
  275. //----------------------------------------------------------------------------
  276. // Equalizer_getParameter()
  277. //----------------------------------------------------------------------------
  278. // Purpose:
  279. // Get a Equalizer parameter
  280. //
  281. // Inputs:
  282. // pEqualizer - handle to instance data
  283. // pParam - pointer to parameter
  284. // pValue - pointer to variable to hold retrieved value
  285. // pValueSize - pointer to value size: maximum size as input
  286. //
  287. // Outputs:
  288. // *pValue updated with parameter value
  289. // *pValueSize updated with actual value size
  290. //
  291. //
  292. // Side Effects:
  293. //
  294. //----------------------------------------------------------------------------
  295. int Equalizer_getParameter(AudioEqualizer * pEqualizer, int32_t *pParam, uint32_t *pValueSize, void *pValue)
  296. {
  297. int status = 0;
  298. int32_t param = *pParam++;
  299. int32_t param2;
  300. char *name;
  301. switch (param) {
  302. case EQ_PARAM_NUM_BANDS:
  303. case EQ_PARAM_CUR_PRESET:
  304. case EQ_PARAM_GET_NUM_OF_PRESETS:
  305. case EQ_PARAM_BAND_LEVEL:
  306. case EQ_PARAM_GET_BAND:
  307. if (*pValueSize < sizeof(int16_t)) {
  308. return -EINVAL;
  309. }
  310. *pValueSize = sizeof(int16_t);
  311. break;
  312. case EQ_PARAM_LEVEL_RANGE:
  313. if (*pValueSize < 2 * sizeof(int16_t)) {
  314. return -EINVAL;
  315. }
  316. *pValueSize = 2 * sizeof(int16_t);
  317. break;
  318. case EQ_PARAM_BAND_FREQ_RANGE:
  319. if (*pValueSize < 2 * sizeof(int32_t)) {
  320. return -EINVAL;
  321. }
  322. *pValueSize = 2 * sizeof(int32_t);
  323. break;
  324. case EQ_PARAM_CENTER_FREQ:
  325. if (*pValueSize < sizeof(int32_t)) {
  326. return -EINVAL;
  327. }
  328. *pValueSize = sizeof(int32_t);
  329. break;
  330. case EQ_PARAM_GET_PRESET_NAME:
  331. break;
  332. case EQ_PARAM_PROPERTIES:
  333. if (*pValueSize < (2 + kNumBands) * sizeof(uint16_t)) {
  334. return -EINVAL;
  335. }
  336. *pValueSize = (2 + kNumBands) * sizeof(uint16_t);
  337. break;
  338. default:
  339. return -EINVAL;
  340. }
  341. switch (param) {
  342. case EQ_PARAM_NUM_BANDS:
  343. *(uint16_t *)pValue = (uint16_t)kNumBands;
  344. ALOGV("Equalizer_getParameter() EQ_PARAM_NUM_BANDS %d", *(int16_t *)pValue);
  345. break;
  346. case EQ_PARAM_LEVEL_RANGE:
  347. *(int16_t *)pValue = -9600;
  348. *((int16_t *)pValue + 1) = 4800;
  349. ALOGV("Equalizer_getParameter() EQ_PARAM_LEVEL_RANGE min %d, max %d",
  350. *(int32_t *)pValue, *((int32_t *)pValue + 1));
  351. break;
  352. case EQ_PARAM_BAND_LEVEL:
  353. param2 = *pParam;
  354. if (param2 >= kNumBands) {
  355. status = -EINVAL;
  356. break;
  357. }
  358. *(int16_t *)pValue = (int16_t)pEqualizer->getGain(param2);
  359. ALOGV("Equalizer_getParameter() EQ_PARAM_BAND_LEVEL band %d, level %d",
  360. param2, *(int32_t *)pValue);
  361. break;
  362. case EQ_PARAM_CENTER_FREQ:
  363. param2 = *pParam;
  364. if (param2 >= kNumBands) {
  365. status = -EINVAL;
  366. break;
  367. }
  368. *(int32_t *)pValue = pEqualizer->getFrequency(param2);
  369. ALOGV("Equalizer_getParameter() EQ_PARAM_CENTER_FREQ band %d, frequency %d",
  370. param2, *(int32_t *)pValue);
  371. break;
  372. case EQ_PARAM_BAND_FREQ_RANGE:
  373. param2 = *pParam;
  374. if (param2 >= kNumBands) {
  375. status = -EINVAL;
  376. break;
  377. }
  378. pEqualizer->getBandRange(param2, *(uint32_t *)pValue, *((uint32_t *)pValue + 1));
  379. ALOGV("Equalizer_getParameter() EQ_PARAM_BAND_FREQ_RANGE band %d, min %d, max %d",
  380. param2, *(int32_t *)pValue, *((int32_t *)pValue + 1));
  381. break;
  382. case EQ_PARAM_GET_BAND:
  383. param2 = *pParam;
  384. *(uint16_t *)pValue = (uint16_t)pEqualizer->getMostRelevantBand(param2);
  385. ALOGV("Equalizer_getParameter() EQ_PARAM_GET_BAND frequency %d, band %d",
  386. param2, *(int32_t *)pValue);
  387. break;
  388. case EQ_PARAM_CUR_PRESET:
  389. *(uint16_t *)pValue = (uint16_t)pEqualizer->getPreset();
  390. ALOGV("Equalizer_getParameter() EQ_PARAM_CUR_PRESET %d", *(int32_t *)pValue);
  391. break;
  392. case EQ_PARAM_GET_NUM_OF_PRESETS:
  393. *(uint16_t *)pValue = (uint16_t)pEqualizer->getNumPresets();
  394. ALOGV("Equalizer_getParameter() EQ_PARAM_GET_NUM_OF_PRESETS %d", *(int16_t *)pValue);
  395. break;
  396. case EQ_PARAM_GET_PRESET_NAME:
  397. param2 = *pParam;
  398. if (param2 >= pEqualizer->getNumPresets()) {
  399. status = -EINVAL;
  400. break;
  401. }
  402. name = (char *)pValue;
  403. strncpy(name, pEqualizer->getPresetName(param2), *pValueSize - 1);
  404. name[*pValueSize - 1] = 0;
  405. *pValueSize = strlen(name) + 1;
  406. ALOGV("Equalizer_getParameter() EQ_PARAM_GET_PRESET_NAME preset %d, name %s len %d",
  407. param2, gEqualizerPresets[param2].name, *pValueSize);
  408. break;
  409. case EQ_PARAM_PROPERTIES: {
  410. int16_t *p = (int16_t *)pValue;
  411. ALOGV("Equalizer_getParameter() EQ_PARAM_PROPERTIES");
  412. p[0] = (int16_t)pEqualizer->getPreset();
  413. p[1] = (int16_t)kNumBands;
  414. for (int i = 0; i < kNumBands; i++) {
  415. p[2 + i] = (int16_t)pEqualizer->getGain(i);
  416. }
  417. } break;
  418. default:
  419. ALOGV("Equalizer_getParameter() invalid param %d", param);
  420. status = -EINVAL;
  421. break;
  422. }
  423. return status;
  424. } // end Equalizer_getParameter
  425. //----------------------------------------------------------------------------
  426. // Equalizer_setParameter()
  427. //----------------------------------------------------------------------------
  428. // Purpose:
  429. // Set a Equalizer parameter
  430. //
  431. // Inputs:
  432. // pEqualizer - handle to instance data
  433. // pParam - pointer to parameter
  434. // pValue - pointer to value
  435. //
  436. // Outputs:
  437. //
  438. //
  439. // Side Effects:
  440. //
  441. //----------------------------------------------------------------------------
  442. int Equalizer_setParameter (AudioEqualizer * pEqualizer, int32_t *pParam, void *pValue)
  443. {
  444. int status = 0;
  445. int32_t preset;
  446. int32_t band;
  447. int32_t level;
  448. int32_t param = *pParam++;
  449. switch (param) {
  450. case EQ_PARAM_CUR_PRESET:
  451. preset = (int32_t)(*(uint16_t *)pValue);
  452. ALOGV("setParameter() EQ_PARAM_CUR_PRESET %d", preset);
  453. if (preset < 0 || preset >= pEqualizer->getNumPresets()) {
  454. status = -EINVAL;
  455. break;
  456. }
  457. pEqualizer->setPreset(preset);
  458. pEqualizer->commit(true);
  459. break;
  460. case EQ_PARAM_BAND_LEVEL:
  461. band = *pParam;
  462. level = (int32_t)(*(int16_t *)pValue);
  463. ALOGV("setParameter() EQ_PARAM_BAND_LEVEL band %d, level %d", band, level);
  464. if (band >= kNumBands) {
  465. status = -EINVAL;
  466. break;
  467. }
  468. pEqualizer->setGain(band, level);
  469. pEqualizer->commit(true);
  470. break;
  471. case EQ_PARAM_PROPERTIES: {
  472. ALOGV("setParameter() EQ_PARAM_PROPERTIES");
  473. int16_t *p = (int16_t *)pValue;
  474. if ((int)p[0] >= pEqualizer->getNumPresets()) {
  475. status = -EINVAL;
  476. break;
  477. }
  478. if (p[0] >= 0) {
  479. pEqualizer->setPreset((int)p[0]);
  480. } else {
  481. if ((int)p[1] != kNumBands) {
  482. status = -EINVAL;
  483. break;
  484. }
  485. for (int i = 0; i < kNumBands; i++) {
  486. pEqualizer->setGain(i, (int32_t)p[2 + i]);
  487. }
  488. }
  489. pEqualizer->commit(true);
  490. } break;
  491. default:
  492. ALOGV("setParameter() invalid param %d", param);
  493. status = -EINVAL;
  494. break;
  495. }
  496. return status;
  497. } // end Equalizer_setParameter
  498. } // namespace
  499. } // namespace
  500. //
  501. //--- Effect Control Interface Implementation
  502. //
  503. extern "C" int Equalizer_process(effect_handle_t self, audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
  504. {
  505. android::EqualizerContext * pContext = (android::EqualizerContext *) self;
  506. if (pContext == NULL) {
  507. return -EINVAL;
  508. }
  509. if (inBuffer == NULL || inBuffer->raw == NULL ||
  510. outBuffer == NULL || outBuffer->raw == NULL ||
  511. inBuffer->frameCount != outBuffer->frameCount) {
  512. return -EINVAL;
  513. }
  514. if (pContext->state == EQUALIZER_STATE_UNINITIALIZED) {
  515. return -EINVAL;
  516. }
  517. if (pContext->state == EQUALIZER_STATE_INITIALIZED) {
  518. return -ENODATA;
  519. }
  520. pContext->adapter.process(inBuffer->raw, outBuffer->raw, outBuffer->frameCount);
  521. return 0;
  522. } // end Equalizer_process
  523. extern "C" int Equalizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
  524. void *pCmdData, uint32_t *replySize, void *pReplyData) {
  525. android::EqualizerContext * pContext = (android::EqualizerContext *) self;
  526. int retsize;
  527. if (pContext == NULL || pContext->state == EQUALIZER_STATE_UNINITIALIZED) {
  528. return -EINVAL;
  529. }
  530. android::AudioEqualizer * pEqualizer = pContext->pEqualizer;
  531. ALOGV("Equalizer_command command %d cmdSize %d",cmdCode, cmdSize);
  532. switch (cmdCode) {
  533. case EFFECT_CMD_INIT:
  534. if (pReplyData == NULL || *replySize != sizeof(int)) {
  535. return -EINVAL;
  536. }
  537. *(int *) pReplyData = Equalizer_init(pContext);
  538. break;
  539. case EFFECT_CMD_SET_CONFIG:
  540. if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
  541. || pReplyData == NULL || *replySize != sizeof(int)) {
  542. return -EINVAL;
  543. }
  544. *(int *) pReplyData = Equalizer_setConfig(pContext,
  545. (effect_config_t *) pCmdData);
  546. break;
  547. case EFFECT_CMD_GET_CONFIG:
  548. if (pReplyData == NULL || *replySize != sizeof(effect_config_t)) {
  549. return -EINVAL;
  550. }
  551. Equalizer_getConfig(pContext, (effect_config_t *) pCmdData);
  552. break;
  553. case EFFECT_CMD_RESET:
  554. Equalizer_setConfig(pContext, &pContext->config);
  555. break;
  556. case EFFECT_CMD_GET_PARAM: {
  557. if (pCmdData == NULL || cmdSize < (sizeof(effect_param_t) + sizeof(int32_t)) ||
  558. pReplyData == NULL || *replySize < (sizeof(effect_param_t) + sizeof(int32_t))) {
  559. return -EINVAL;
  560. }
  561. effect_param_t *p = (effect_param_t *)pCmdData;
  562. memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize);
  563. p = (effect_param_t *)pReplyData;
  564. int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
  565. p->status = android::Equalizer_getParameter(pEqualizer, (int32_t *)p->data, &p->vsize,
  566. p->data + voffset);
  567. *replySize = sizeof(effect_param_t) + voffset + p->vsize;
  568. ALOGV("Equalizer_command EFFECT_CMD_GET_PARAM *pCmdData %d, *replySize %d, *pReplyData %08x %08x",
  569. *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)), *replySize,
  570. *(int32_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset),
  571. *(int32_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset + sizeof(int32_t)));
  572. } break;
  573. case EFFECT_CMD_SET_PARAM: {
  574. ALOGV("Equalizer_command EFFECT_CMD_SET_PARAM cmdSize %d pCmdData %p, *replySize %d, pReplyData %p",
  575. cmdSize, pCmdData, *replySize, pReplyData);
  576. if (pCmdData == NULL || cmdSize < (sizeof(effect_param_t) + sizeof(int32_t)) ||
  577. pReplyData == NULL || *replySize != sizeof(int32_t)) {
  578. return -EINVAL;
  579. }
  580. effect_param_t *p = (effect_param_t *) pCmdData;
  581. *(int *)pReplyData = android::Equalizer_setParameter(pEqualizer, (int32_t *)p->data,
  582. p->data + p->psize);
  583. } break;
  584. case EFFECT_CMD_ENABLE:
  585. if (pReplyData == NULL || *replySize != sizeof(int)) {
  586. return -EINVAL;
  587. }
  588. if (pContext->state != EQUALIZER_STATE_INITIALIZED) {
  589. return -ENOSYS;
  590. }
  591. pContext->state = EQUALIZER_STATE_ACTIVE;
  592. ALOGV("EFFECT_CMD_ENABLE() OK");
  593. *(int *)pReplyData = 0;
  594. break;
  595. case EFFECT_CMD_DISABLE:
  596. if (pReplyData == NULL || *replySize != sizeof(int)) {
  597. return -EINVAL;
  598. }
  599. if (pContext->state != EQUALIZER_STATE_ACTIVE) {
  600. return -ENOSYS;
  601. }
  602. pContext->state = EQUALIZER_STATE_INITIALIZED;
  603. ALOGV("EFFECT_CMD_DISABLE() OK");
  604. *(int *)pReplyData = 0;
  605. break;
  606. case EFFECT_CMD_SET_DEVICE:
  607. case EFFECT_CMD_SET_VOLUME:
  608. case EFFECT_CMD_SET_AUDIO_MODE:
  609. break;
  610. default:
  611. ALOGW("Equalizer_command invalid command %d",cmdCode);
  612. return -EINVAL;
  613. }
  614. return 0;
  615. }
  616. extern "C" int Equalizer_getDescriptor(effect_handle_t self,
  617. effect_descriptor_t *pDescriptor)
  618. {
  619. android::EqualizerContext * pContext = (android::EqualizerContext *) self;
  620. if (pContext == NULL || pDescriptor == NULL) {
  621. ALOGV("Equalizer_getDescriptor() invalid param");
  622. return -EINVAL;
  623. }
  624. *pDescriptor = android::gEqualizerDescriptor;
  625. return 0;
  626. }
  627. // effect_handle_t interface implementation for equalizer effect
  628. const struct effect_interface_s gEqualizerInterface = {
  629. Equalizer_process,
  630. Equalizer_command,
  631. Equalizer_getDescriptor,
  632. NULL
  633. };
  634. audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
  635. tag : AUDIO_EFFECT_LIBRARY_TAG,
  636. version : EFFECT_LIBRARY_API_VERSION,
  637. name : "Test Equalizer Library",
  638. implementor : "The Android Open Source Project",
  639. create_effect : android::EffectCreate,
  640. release_effect : android::EffectRelease,
  641. get_descriptor : android::EffectGetDescriptor,
  642. };