EffectReverb.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  1. /*
  2. * Copyright (C) 2008 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 "EffectReverb"
  17. //#define LOG_NDEBUG 0
  18. #include <stdbool.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <log/log.h>
  22. #include "EffectReverb.h"
  23. #include "EffectsMath.h"
  24. // effect_handle_t interface implementation for reverb effect
  25. const struct effect_interface_s gReverbInterface = {
  26. Reverb_Process,
  27. Reverb_Command,
  28. Reverb_GetDescriptor,
  29. NULL
  30. };
  31. // Google auxiliary environmental reverb UUID: 1f0ae2e0-4ef7-11df-bc09-0002a5d5c51b
  32. static const effect_descriptor_t gAuxEnvReverbDescriptor = {
  33. {0xc2e5d5f0, 0x94bd, 0x4763, 0x9cac, {0x4e, 0x23, 0x4d, 0x06, 0x83, 0x9e}},
  34. {0x1f0ae2e0, 0x4ef7, 0x11df, 0xbc09, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
  35. EFFECT_CONTROL_API_VERSION,
  36. // flags other than EFFECT_FLAG_TYPE_AUXILIARY set for test purpose
  37. EFFECT_FLAG_TYPE_AUXILIARY | EFFECT_FLAG_DEVICE_IND | EFFECT_FLAG_AUDIO_MODE_IND,
  38. 0, // TODO
  39. 33,
  40. "Aux Environmental Reverb",
  41. "The Android Open Source Project"
  42. };
  43. // Google insert environmental reverb UUID: aa476040-6342-11df-91a4-0002a5d5c51b
  44. static const effect_descriptor_t gInsertEnvReverbDescriptor = {
  45. {0xc2e5d5f0, 0x94bd, 0x4763, 0x9cac, {0x4e, 0x23, 0x4d, 0x06, 0x83, 0x9e}},
  46. {0xaa476040, 0x6342, 0x11df, 0x91a4, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
  47. EFFECT_CONTROL_API_VERSION,
  48. EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST,
  49. 0, // TODO
  50. 33,
  51. "Insert Environmental reverb",
  52. "The Android Open Source Project"
  53. };
  54. // Google auxiliary preset reverb UUID: 63909320-53a6-11df-bdbd-0002a5d5c51b
  55. static const effect_descriptor_t gAuxPresetReverbDescriptor = {
  56. {0x47382d60, 0xddd8, 0x11db, 0xbf3a, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
  57. {0x63909320, 0x53a6, 0x11df, 0xbdbd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
  58. EFFECT_CONTROL_API_VERSION,
  59. EFFECT_FLAG_TYPE_AUXILIARY,
  60. 0, // TODO
  61. 33,
  62. "Aux Preset Reverb",
  63. "The Android Open Source Project"
  64. };
  65. // Google insert preset reverb UUID: d93dc6a0-6342-11df-b128-0002a5d5c51b
  66. static const effect_descriptor_t gInsertPresetReverbDescriptor = {
  67. {0x47382d60, 0xddd8, 0x11db, 0xbf3a, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
  68. {0xd93dc6a0, 0x6342, 0x11df, 0xb128, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
  69. EFFECT_CONTROL_API_VERSION,
  70. EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST,
  71. 0, // TODO
  72. 33,
  73. "Insert Preset Reverb",
  74. "The Android Open Source Project"
  75. };
  76. // gDescriptors contains pointers to all defined effect descriptor in this library
  77. static const effect_descriptor_t * const gDescriptors[] = {
  78. &gAuxEnvReverbDescriptor,
  79. &gInsertEnvReverbDescriptor,
  80. &gAuxPresetReverbDescriptor,
  81. &gInsertPresetReverbDescriptor
  82. };
  83. /*----------------------------------------------------------------------------
  84. * Effect API implementation
  85. *--------------------------------------------------------------------------*/
  86. /*--- Effect Library Interface Implementation ---*/
  87. int EffectCreate(const effect_uuid_t *uuid,
  88. int32_t sessionId,
  89. int32_t ioId,
  90. effect_handle_t *pHandle) {
  91. int ret;
  92. int i;
  93. reverb_module_t *module;
  94. const effect_descriptor_t *desc;
  95. int aux = 0;
  96. int preset = 0;
  97. ALOGV("EffectLibCreateEffect start");
  98. if (pHandle == NULL || uuid == NULL) {
  99. return -EINVAL;
  100. }
  101. for (i = 0; gDescriptors[i] != NULL; i++) {
  102. desc = gDescriptors[i];
  103. if (memcmp(uuid, &desc->uuid, sizeof(effect_uuid_t))
  104. == 0) {
  105. break;
  106. }
  107. }
  108. if (gDescriptors[i] == NULL) {
  109. return -ENOENT;
  110. }
  111. module = malloc(sizeof(reverb_module_t));
  112. module->itfe = &gReverbInterface;
  113. module->context.mState = REVERB_STATE_UNINITIALIZED;
  114. if (memcmp(&desc->type, SL_IID_PRESETREVERB, sizeof(effect_uuid_t)) == 0) {
  115. preset = 1;
  116. }
  117. if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
  118. aux = 1;
  119. }
  120. ret = Reverb_Init(module, aux, preset);
  121. if (ret < 0) {
  122. ALOGW("EffectLibCreateEffect() init failed");
  123. free(module);
  124. return ret;
  125. }
  126. *pHandle = (effect_handle_t) module;
  127. module->context.mState = REVERB_STATE_INITIALIZED;
  128. ALOGV("EffectLibCreateEffect %p ,size %d", module, sizeof(reverb_module_t));
  129. return 0;
  130. }
  131. int EffectRelease(effect_handle_t handle) {
  132. reverb_module_t *pRvbModule = (reverb_module_t *)handle;
  133. ALOGV("EffectLibReleaseEffect %p", handle);
  134. if (handle == NULL) {
  135. return -EINVAL;
  136. }
  137. pRvbModule->context.mState = REVERB_STATE_UNINITIALIZED;
  138. free(pRvbModule);
  139. return 0;
  140. }
  141. int EffectGetDescriptor(const effect_uuid_t *uuid,
  142. effect_descriptor_t *pDescriptor) {
  143. int i;
  144. int length = sizeof(gDescriptors) / sizeof(const effect_descriptor_t *);
  145. if (pDescriptor == NULL || uuid == NULL){
  146. ALOGV("EffectGetDescriptor() called with NULL pointer");
  147. return -EINVAL;
  148. }
  149. for (i = 0; i < length; i++) {
  150. if (memcmp(uuid, &gDescriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
  151. *pDescriptor = *gDescriptors[i];
  152. ALOGV("EffectGetDescriptor - UUID matched Reverb type %d, UUID = %x",
  153. i, gDescriptors[i]->uuid.timeLow);
  154. return 0;
  155. }
  156. }
  157. return -EINVAL;
  158. } /* end EffectGetDescriptor */
  159. /*--- Effect Control Interface Implementation ---*/
  160. static int Reverb_Process(effect_handle_t self, audio_buffer_t *inBuffer, audio_buffer_t *outBuffer) {
  161. reverb_object_t *pReverb;
  162. int16_t *pSrc, *pDst;
  163. reverb_module_t *pRvbModule = (reverb_module_t *)self;
  164. if (pRvbModule == NULL) {
  165. return -EINVAL;
  166. }
  167. if (inBuffer == NULL || inBuffer->raw == NULL ||
  168. outBuffer == NULL || outBuffer->raw == NULL ||
  169. inBuffer->frameCount != outBuffer->frameCount) {
  170. return -EINVAL;
  171. }
  172. pReverb = (reverb_object_t*) &pRvbModule->context;
  173. if (pReverb->mState == REVERB_STATE_UNINITIALIZED) {
  174. return -EINVAL;
  175. }
  176. if (pReverb->mState == REVERB_STATE_INITIALIZED) {
  177. return -ENODATA;
  178. }
  179. //if bypassed or the preset forces the signal to be completely dry
  180. if (pReverb->m_bBypass != 0) {
  181. if (inBuffer->raw != outBuffer->raw) {
  182. int16_t smp;
  183. pSrc = inBuffer->s16;
  184. pDst = outBuffer->s16;
  185. size_t count = inBuffer->frameCount;
  186. if (pRvbModule->config.inputCfg.channels == pRvbModule->config.outputCfg.channels) {
  187. count *= 2;
  188. while (count--) {
  189. *pDst++ = *pSrc++;
  190. }
  191. } else {
  192. while (count--) {
  193. smp = *pSrc++;
  194. *pDst++ = smp;
  195. *pDst++ = smp;
  196. }
  197. }
  198. }
  199. return 0;
  200. }
  201. if (pReverb->m_nNextRoom != pReverb->m_nCurrentRoom) {
  202. ReverbUpdateRoom(pReverb, true);
  203. }
  204. pSrc = inBuffer->s16;
  205. pDst = outBuffer->s16;
  206. size_t numSamples = outBuffer->frameCount;
  207. while (numSamples) {
  208. uint32_t processedSamples;
  209. if (numSamples > (uint32_t) pReverb->m_nUpdatePeriodInSamples) {
  210. processedSamples = (uint32_t) pReverb->m_nUpdatePeriodInSamples;
  211. } else {
  212. processedSamples = numSamples;
  213. }
  214. /* increment update counter */
  215. pReverb->m_nUpdateCounter += (int16_t) processedSamples;
  216. /* check if update counter needs to be reset */
  217. if (pReverb->m_nUpdateCounter >= pReverb->m_nUpdatePeriodInSamples) {
  218. /* update interval has elapsed, so reset counter */
  219. pReverb->m_nUpdateCounter -= pReverb->m_nUpdatePeriodInSamples;
  220. ReverbUpdateXfade(pReverb, pReverb->m_nUpdatePeriodInSamples);
  221. } /* end if m_nUpdateCounter >= update interval */
  222. Reverb(pReverb, processedSamples, pDst, pSrc);
  223. numSamples -= processedSamples;
  224. if (pReverb->m_Aux) {
  225. pSrc += processedSamples;
  226. } else {
  227. pSrc += processedSamples * NUM_OUTPUT_CHANNELS;
  228. }
  229. pDst += processedSamples * NUM_OUTPUT_CHANNELS;
  230. }
  231. return 0;
  232. }
  233. static int Reverb_Command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
  234. void *pCmdData, uint32_t *replySize, void *pReplyData) {
  235. reverb_module_t *pRvbModule = (reverb_module_t *) self;
  236. reverb_object_t *pReverb;
  237. int retsize;
  238. if (pRvbModule == NULL ||
  239. pRvbModule->context.mState == REVERB_STATE_UNINITIALIZED) {
  240. return -EINVAL;
  241. }
  242. pReverb = (reverb_object_t*) &pRvbModule->context;
  243. ALOGV("Reverb_Command command %d cmdSize %d",cmdCode, cmdSize);
  244. switch (cmdCode) {
  245. case EFFECT_CMD_INIT:
  246. if (pReplyData == NULL || *replySize != sizeof(int)) {
  247. return -EINVAL;
  248. }
  249. *(int *) pReplyData = Reverb_Init(pRvbModule, pReverb->m_Aux, pReverb->m_Preset);
  250. if (*(int *) pReplyData == 0) {
  251. pRvbModule->context.mState = REVERB_STATE_INITIALIZED;
  252. }
  253. break;
  254. case EFFECT_CMD_SET_CONFIG:
  255. if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
  256. || pReplyData == NULL || *replySize != sizeof(int)) {
  257. return -EINVAL;
  258. }
  259. *(int *) pReplyData = Reverb_setConfig(pRvbModule,
  260. (effect_config_t *)pCmdData, false);
  261. break;
  262. case EFFECT_CMD_GET_CONFIG:
  263. if (pReplyData == NULL || *replySize != sizeof(effect_config_t)) {
  264. return -EINVAL;
  265. }
  266. Reverb_getConfig(pRvbModule, (effect_config_t *) pCmdData);
  267. break;
  268. case EFFECT_CMD_RESET:
  269. Reverb_Reset(pReverb, false);
  270. break;
  271. case EFFECT_CMD_GET_PARAM:
  272. ALOGV("Reverb_Command EFFECT_CMD_GET_PARAM pCmdData %p, *replySize %d, pReplyData: %p",pCmdData, *replySize, pReplyData);
  273. if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
  274. pReplyData == NULL || *replySize < (int) sizeof(effect_param_t)) {
  275. return -EINVAL;
  276. }
  277. effect_param_t *rep = (effect_param_t *) pReplyData;
  278. memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(int32_t));
  279. ALOGV("Reverb_Command EFFECT_CMD_GET_PARAM param %d, replySize %d",*(int32_t *)rep->data, rep->vsize);
  280. rep->status = Reverb_getParameter(pReverb, *(int32_t *)rep->data, &rep->vsize,
  281. rep->data + sizeof(int32_t));
  282. *replySize = sizeof(effect_param_t) + sizeof(int32_t) + rep->vsize;
  283. break;
  284. case EFFECT_CMD_SET_PARAM:
  285. ALOGV("Reverb_Command EFFECT_CMD_SET_PARAM cmdSize %d pCmdData %p, *replySize %d, pReplyData %p",
  286. cmdSize, pCmdData, *replySize, pReplyData);
  287. if (pCmdData == NULL || (cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)))
  288. || pReplyData == NULL || *replySize != (int)sizeof(int32_t)) {
  289. return -EINVAL;
  290. }
  291. effect_param_t *cmd = (effect_param_t *) pCmdData;
  292. *(int *)pReplyData = Reverb_setParameter(pReverb, *(int32_t *)cmd->data,
  293. cmd->vsize, cmd->data + sizeof(int32_t));
  294. break;
  295. case EFFECT_CMD_ENABLE:
  296. if (pReplyData == NULL || *replySize != sizeof(int)) {
  297. return -EINVAL;
  298. }
  299. if (pReverb->mState != REVERB_STATE_INITIALIZED) {
  300. return -ENOSYS;
  301. }
  302. pReverb->mState = REVERB_STATE_ACTIVE;
  303. ALOGV("EFFECT_CMD_ENABLE() OK");
  304. *(int *)pReplyData = 0;
  305. break;
  306. case EFFECT_CMD_DISABLE:
  307. if (pReplyData == NULL || *replySize != sizeof(int)) {
  308. return -EINVAL;
  309. }
  310. if (pReverb->mState != REVERB_STATE_ACTIVE) {
  311. return -ENOSYS;
  312. }
  313. pReverb->mState = REVERB_STATE_INITIALIZED;
  314. ALOGV("EFFECT_CMD_DISABLE() OK");
  315. *(int *)pReplyData = 0;
  316. break;
  317. case EFFECT_CMD_SET_DEVICE:
  318. if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
  319. return -EINVAL;
  320. }
  321. ALOGV("Reverb_Command EFFECT_CMD_SET_DEVICE: 0x%08x", *(uint32_t *)pCmdData);
  322. break;
  323. case EFFECT_CMD_SET_VOLUME: {
  324. // audio output is always stereo => 2 channel volumes
  325. if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t) * 2) {
  326. return -EINVAL;
  327. }
  328. float left = (float)(*(uint32_t *)pCmdData) / (1 << 24);
  329. float right = (float)(*((uint32_t *)pCmdData + 1)) / (1 << 24);
  330. ALOGV("Reverb_Command EFFECT_CMD_SET_VOLUME: left %f, right %f ", left, right);
  331. break;
  332. }
  333. case EFFECT_CMD_SET_AUDIO_MODE:
  334. if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
  335. return -EINVAL;
  336. }
  337. ALOGV("Reverb_Command EFFECT_CMD_SET_AUDIO_MODE: %d", *(uint32_t *)pCmdData);
  338. break;
  339. default:
  340. ALOGW("Reverb_Command invalid command %d",cmdCode);
  341. return -EINVAL;
  342. }
  343. return 0;
  344. }
  345. int Reverb_GetDescriptor(effect_handle_t self,
  346. effect_descriptor_t *pDescriptor)
  347. {
  348. reverb_module_t *pRvbModule = (reverb_module_t *) self;
  349. reverb_object_t *pReverb;
  350. const effect_descriptor_t *desc;
  351. if (pRvbModule == NULL ||
  352. pRvbModule->context.mState == REVERB_STATE_UNINITIALIZED) {
  353. return -EINVAL;
  354. }
  355. pReverb = (reverb_object_t*) &pRvbModule->context;
  356. if (pReverb->m_Aux) {
  357. if (pReverb->m_Preset) {
  358. desc = &gAuxPresetReverbDescriptor;
  359. } else {
  360. desc = &gAuxEnvReverbDescriptor;
  361. }
  362. } else {
  363. if (pReverb->m_Preset) {
  364. desc = &gInsertPresetReverbDescriptor;
  365. } else {
  366. desc = &gInsertEnvReverbDescriptor;
  367. }
  368. }
  369. *pDescriptor = *desc;
  370. return 0;
  371. } /* end Reverb_getDescriptor */
  372. /*----------------------------------------------------------------------------
  373. * Reverb internal functions
  374. *--------------------------------------------------------------------------*/
  375. /*----------------------------------------------------------------------------
  376. * Reverb_Init()
  377. *----------------------------------------------------------------------------
  378. * Purpose:
  379. * Initialize reverb context and apply default parameters
  380. *
  381. * Inputs:
  382. * pRvbModule - pointer to reverb effect module
  383. * aux - indicates if the reverb is used as auxiliary (1) or insert (0)
  384. * preset - indicates if the reverb is used in preset (1) or environmental (0) mode
  385. *
  386. * Outputs:
  387. *
  388. * Side Effects:
  389. *
  390. *----------------------------------------------------------------------------
  391. */
  392. int Reverb_Init(reverb_module_t *pRvbModule, int aux, int preset) {
  393. int ret;
  394. ALOGV("Reverb_Init module %p, aux: %d, preset: %d", pRvbModule,aux, preset);
  395. memset(&pRvbModule->context, 0, sizeof(reverb_object_t));
  396. pRvbModule->context.m_Aux = (uint16_t)aux;
  397. pRvbModule->context.m_Preset = (uint16_t)preset;
  398. pRvbModule->config.inputCfg.samplingRate = 44100;
  399. if (aux) {
  400. pRvbModule->config.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
  401. } else {
  402. pRvbModule->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
  403. }
  404. pRvbModule->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
  405. pRvbModule->config.inputCfg.bufferProvider.getBuffer = NULL;
  406. pRvbModule->config.inputCfg.bufferProvider.releaseBuffer = NULL;
  407. pRvbModule->config.inputCfg.bufferProvider.cookie = NULL;
  408. pRvbModule->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
  409. pRvbModule->config.inputCfg.mask = EFFECT_CONFIG_ALL;
  410. pRvbModule->config.outputCfg.samplingRate = 44100;
  411. pRvbModule->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
  412. pRvbModule->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
  413. pRvbModule->config.outputCfg.bufferProvider.getBuffer = NULL;
  414. pRvbModule->config.outputCfg.bufferProvider.releaseBuffer = NULL;
  415. pRvbModule->config.outputCfg.bufferProvider.cookie = NULL;
  416. pRvbModule->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
  417. pRvbModule->config.outputCfg.mask = EFFECT_CONFIG_ALL;
  418. ret = Reverb_setConfig(pRvbModule, &pRvbModule->config, true);
  419. if (ret < 0) {
  420. ALOGV("Reverb_Init error %d on module %p", ret, pRvbModule);
  421. }
  422. return ret;
  423. }
  424. /*----------------------------------------------------------------------------
  425. * Reverb_setConfig()
  426. *----------------------------------------------------------------------------
  427. * Purpose:
  428. * Set input and output audio configuration.
  429. *
  430. * Inputs:
  431. * pRvbModule - pointer to reverb effect module
  432. * pConfig - pointer to effect_config_t structure containing input
  433. * and output audio parameters configuration
  434. * init - true if called from init function
  435. * Outputs:
  436. *
  437. * Side Effects:
  438. *
  439. *----------------------------------------------------------------------------
  440. */
  441. int Reverb_setConfig(reverb_module_t *pRvbModule, effect_config_t *pConfig,
  442. bool init) {
  443. reverb_object_t *pReverb = &pRvbModule->context;
  444. int bufferSizeInSamples;
  445. int updatePeriodInSamples;
  446. int xfadePeriodInSamples;
  447. // Check configuration compatibility with build options
  448. if (pConfig->inputCfg.samplingRate
  449. != pConfig->outputCfg.samplingRate
  450. || pConfig->outputCfg.channels != OUTPUT_CHANNELS
  451. || pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT
  452. || pConfig->outputCfg.format != AUDIO_FORMAT_PCM_16_BIT) {
  453. ALOGV("Reverb_setConfig invalid config");
  454. return -EINVAL;
  455. }
  456. if ((pReverb->m_Aux && (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_MONO)) ||
  457. (!pReverb->m_Aux && (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO))) {
  458. ALOGV("Reverb_setConfig invalid config");
  459. return -EINVAL;
  460. }
  461. pRvbModule->config = *pConfig;
  462. pReverb->m_nSamplingRate = pRvbModule->config.outputCfg.samplingRate;
  463. switch (pReverb->m_nSamplingRate) {
  464. case 8000:
  465. pReverb->m_nUpdatePeriodInBits = 5;
  466. bufferSizeInSamples = 4096;
  467. pReverb->m_nCosWT_5KHz = -23170;
  468. break;
  469. case 16000:
  470. pReverb->m_nUpdatePeriodInBits = 6;
  471. bufferSizeInSamples = 8192;
  472. pReverb->m_nCosWT_5KHz = -12540;
  473. break;
  474. case 22050:
  475. pReverb->m_nUpdatePeriodInBits = 7;
  476. bufferSizeInSamples = 8192;
  477. pReverb->m_nCosWT_5KHz = 4768;
  478. break;
  479. case 32000:
  480. pReverb->m_nUpdatePeriodInBits = 7;
  481. bufferSizeInSamples = 16384;
  482. pReverb->m_nCosWT_5KHz = 18205;
  483. break;
  484. case 44100:
  485. pReverb->m_nUpdatePeriodInBits = 8;
  486. bufferSizeInSamples = 16384;
  487. pReverb->m_nCosWT_5KHz = 24799;
  488. break;
  489. case 48000:
  490. pReverb->m_nUpdatePeriodInBits = 8;
  491. bufferSizeInSamples = 16384;
  492. pReverb->m_nCosWT_5KHz = 25997;
  493. break;
  494. default:
  495. ALOGV("Reverb_setConfig invalid sampling rate %d", pReverb->m_nSamplingRate);
  496. return -EINVAL;
  497. }
  498. // Define a mask for circular addressing, so that array index
  499. // can wraparound and stay in array boundary of 0, 1, ..., (buffer size -1)
  500. // The buffer size MUST be a power of two
  501. pReverb->m_nBufferMask = (int32_t) (bufferSizeInSamples - 1);
  502. /* reverb parameters are updated every 2^(pReverb->m_nUpdatePeriodInBits) samples */
  503. updatePeriodInSamples = (int32_t) (0x1L << pReverb->m_nUpdatePeriodInBits);
  504. /*
  505. calculate the update counter by bitwise ANDING with this value to
  506. generate a 2^n modulo value
  507. */
  508. pReverb->m_nUpdatePeriodInSamples = (int32_t) updatePeriodInSamples;
  509. xfadePeriodInSamples = (int32_t) (REVERB_XFADE_PERIOD_IN_SECONDS
  510. * (double) pReverb->m_nSamplingRate);
  511. // set xfade parameters
  512. pReverb->m_nPhaseIncrement
  513. = (int16_t) (65536 / ((int16_t) xfadePeriodInSamples
  514. / (int16_t) updatePeriodInSamples));
  515. if (init) {
  516. ReverbReadInPresets(pReverb);
  517. // for debugging purposes, allow noise generator
  518. pReverb->m_bUseNoise = true;
  519. // for debugging purposes, allow bypass
  520. pReverb->m_bBypass = 0;
  521. pReverb->m_nNextRoom = 1;
  522. pReverb->m_nNoise = (int16_t) 0xABCD;
  523. }
  524. Reverb_Reset(pReverb, init);
  525. return 0;
  526. }
  527. /*----------------------------------------------------------------------------
  528. * Reverb_getConfig()
  529. *----------------------------------------------------------------------------
  530. * Purpose:
  531. * Get input and output audio configuration.
  532. *
  533. * Inputs:
  534. * pRvbModule - pointer to reverb effect module
  535. * pConfig - pointer to effect_config_t structure containing input
  536. * and output audio parameters configuration
  537. * Outputs:
  538. *
  539. * Side Effects:
  540. *
  541. *----------------------------------------------------------------------------
  542. */
  543. void Reverb_getConfig(reverb_module_t *pRvbModule, effect_config_t *pConfig)
  544. {
  545. *pConfig = pRvbModule->config;
  546. }
  547. /*----------------------------------------------------------------------------
  548. * Reverb_Reset()
  549. *----------------------------------------------------------------------------
  550. * Purpose:
  551. * Reset internal states and clear delay lines.
  552. *
  553. * Inputs:
  554. * pReverb - pointer to reverb context
  555. * init - true if called from init function
  556. *
  557. * Outputs:
  558. *
  559. * Side Effects:
  560. *
  561. *----------------------------------------------------------------------------
  562. */
  563. void Reverb_Reset(reverb_object_t *pReverb, bool init) {
  564. int bufferSizeInSamples = (int32_t) (pReverb->m_nBufferMask + 1);
  565. int maxApSamples;
  566. int maxDelaySamples;
  567. int maxEarlySamples;
  568. int ap1In;
  569. int delay0In;
  570. int delay1In;
  571. int32_t i;
  572. uint16_t nOffset;
  573. maxApSamples = ((int32_t) (MAX_AP_TIME * pReverb->m_nSamplingRate) >> 16);
  574. maxDelaySamples = ((int32_t) (MAX_DELAY_TIME * pReverb->m_nSamplingRate)
  575. >> 16);
  576. maxEarlySamples = ((int32_t) (MAX_EARLY_TIME * pReverb->m_nSamplingRate)
  577. >> 16);
  578. ap1In = (AP0_IN + maxApSamples + GUARD);
  579. delay0In = (ap1In + maxApSamples + GUARD);
  580. delay1In = (delay0In + maxDelaySamples + GUARD);
  581. // Define the max offsets for the end points of each section
  582. // i.e., we don't expect a given section's taps to go beyond
  583. // the following limits
  584. pReverb->m_nEarly0in = (delay1In + maxDelaySamples + GUARD);
  585. pReverb->m_nEarly1in = (pReverb->m_nEarly0in + maxEarlySamples + GUARD);
  586. pReverb->m_sAp0.m_zApIn = AP0_IN;
  587. pReverb->m_zD0In = delay0In;
  588. pReverb->m_sAp1.m_zApIn = ap1In;
  589. pReverb->m_zD1In = delay1In;
  590. pReverb->m_zOutLpfL = 0;
  591. pReverb->m_zOutLpfR = 0;
  592. pReverb->m_nRevFbkR = 0;
  593. pReverb->m_nRevFbkL = 0;
  594. // set base index into circular buffer
  595. pReverb->m_nBaseIndex = 0;
  596. // clear the reverb delay line
  597. for (i = 0; i < bufferSizeInSamples; i++) {
  598. pReverb->m_nDelayLine[i] = 0;
  599. }
  600. ReverbUpdateRoom(pReverb, init);
  601. pReverb->m_nUpdateCounter = 0;
  602. pReverb->m_nPhase = -32768;
  603. pReverb->m_nSin = 0;
  604. pReverb->m_nCos = 0;
  605. pReverb->m_nSinIncrement = 0;
  606. pReverb->m_nCosIncrement = 0;
  607. // set delay tap lengths
  608. nOffset = ReverbCalculateNoise(pReverb);
  609. pReverb->m_zD1Cross = pReverb->m_nDelay1Out - pReverb->m_nMaxExcursion
  610. + nOffset;
  611. nOffset = ReverbCalculateNoise(pReverb);
  612. pReverb->m_zD0Cross = pReverb->m_nDelay0Out - pReverb->m_nMaxExcursion
  613. - nOffset;
  614. nOffset = ReverbCalculateNoise(pReverb);
  615. pReverb->m_zD0Self = pReverb->m_nDelay0Out - pReverb->m_nMaxExcursion
  616. - nOffset;
  617. nOffset = ReverbCalculateNoise(pReverb);
  618. pReverb->m_zD1Self = pReverb->m_nDelay1Out - pReverb->m_nMaxExcursion
  619. + nOffset;
  620. }
  621. /*----------------------------------------------------------------------------
  622. * Reverb_getParameter()
  623. *----------------------------------------------------------------------------
  624. * Purpose:
  625. * Get a Reverb parameter
  626. *
  627. * Inputs:
  628. * pReverb - handle to instance data
  629. * param - parameter
  630. * pValue - pointer to variable to hold retrieved value
  631. * pSize - pointer to value size: maximum size as input
  632. *
  633. * Outputs:
  634. * *pValue updated with parameter value
  635. * *pSize updated with actual value size
  636. *
  637. *
  638. * Side Effects:
  639. *
  640. *----------------------------------------------------------------------------
  641. */
  642. int Reverb_getParameter(reverb_object_t *pReverb, int32_t param, uint32_t *pSize,
  643. void *pValue) {
  644. int32_t *pValue32;
  645. int16_t *pValue16;
  646. t_reverb_settings *pProperties;
  647. int32_t i;
  648. int32_t temp;
  649. int32_t temp2;
  650. uint32_t size;
  651. if (pReverb->m_Preset) {
  652. if (param != REVERB_PARAM_PRESET || *pSize < sizeof(int16_t)) {
  653. return -EINVAL;
  654. }
  655. size = sizeof(int16_t);
  656. pValue16 = (int16_t *)pValue;
  657. // REVERB_PRESET_NONE is mapped to bypass
  658. if (pReverb->m_bBypass != 0) {
  659. *pValue16 = (int16_t)REVERB_PRESET_NONE;
  660. } else {
  661. *pValue16 = (int16_t)(pReverb->m_nNextRoom + 1);
  662. }
  663. ALOGV("get REVERB_PARAM_PRESET, preset %d", *pValue16);
  664. } else {
  665. switch (param) {
  666. case REVERB_PARAM_ROOM_LEVEL:
  667. case REVERB_PARAM_ROOM_HF_LEVEL:
  668. case REVERB_PARAM_DECAY_HF_RATIO:
  669. case REVERB_PARAM_REFLECTIONS_LEVEL:
  670. case REVERB_PARAM_REVERB_LEVEL:
  671. case REVERB_PARAM_DIFFUSION:
  672. case REVERB_PARAM_DENSITY:
  673. size = sizeof(int16_t);
  674. break;
  675. case REVERB_PARAM_BYPASS:
  676. case REVERB_PARAM_DECAY_TIME:
  677. case REVERB_PARAM_REFLECTIONS_DELAY:
  678. case REVERB_PARAM_REVERB_DELAY:
  679. size = sizeof(int32_t);
  680. break;
  681. case REVERB_PARAM_PROPERTIES:
  682. size = sizeof(t_reverb_settings);
  683. break;
  684. default:
  685. return -EINVAL;
  686. }
  687. if (*pSize < size) {
  688. return -EINVAL;
  689. }
  690. pValue32 = (int32_t *) pValue;
  691. pValue16 = (int16_t *) pValue;
  692. pProperties = (t_reverb_settings *) pValue;
  693. switch (param) {
  694. case REVERB_PARAM_BYPASS:
  695. *pValue32 = (int32_t) pReverb->m_bBypass;
  696. break;
  697. case REVERB_PARAM_PROPERTIES:
  698. pValue16 = &pProperties->roomLevel;
  699. /* FALL THROUGH */
  700. case REVERB_PARAM_ROOM_LEVEL:
  701. // Convert m_nRoomLpfFwd to millibels
  702. temp = (pReverb->m_nRoomLpfFwd << 15)
  703. / (32767 - pReverb->m_nRoomLpfFbk);
  704. *pValue16 = Effects_Linear16ToMillibels(temp);
  705. ALOGV("get REVERB_PARAM_ROOM_LEVEL %d, gain %d, m_nRoomLpfFwd %d, m_nRoomLpfFbk %d", *pValue16, temp, pReverb->m_nRoomLpfFwd, pReverb->m_nRoomLpfFbk);
  706. if (param == REVERB_PARAM_ROOM_LEVEL) {
  707. break;
  708. }
  709. pValue16 = &pProperties->roomHFLevel;
  710. /* FALL THROUGH */
  711. case REVERB_PARAM_ROOM_HF_LEVEL:
  712. // The ratio between linear gain at 0Hz and at 5000Hz for the room low pass is:
  713. // (1 + a1) / sqrt(a1^2 + 2*C*a1 + 1) where:
  714. // - a1 is minus the LP feedback gain: -pReverb->m_nRoomLpfFbk
  715. // - C is cos(2piWT) @ 5000Hz: pReverb->m_nCosWT_5KHz
  716. temp = MULT_EG1_EG1(pReverb->m_nRoomLpfFbk, pReverb->m_nRoomLpfFbk);
  717. ALOGV("get REVERB_PARAM_ROOM_HF_LEVEL, a1^2 %d", temp);
  718. temp2 = MULT_EG1_EG1(pReverb->m_nRoomLpfFbk, pReverb->m_nCosWT_5KHz)
  719. << 1;
  720. ALOGV("get REVERB_PARAM_ROOM_HF_LEVEL, 2 Cos a1 %d", temp2);
  721. temp = 32767 + temp - temp2;
  722. ALOGV("get REVERB_PARAM_ROOM_HF_LEVEL, a1^2 + 2 Cos a1 + 1 %d", temp);
  723. temp = Effects_Sqrt(temp) * 181;
  724. ALOGV("get REVERB_PARAM_ROOM_HF_LEVEL, SQRT(a1^2 + 2 Cos a1 + 1) %d", temp);
  725. temp = ((32767 - pReverb->m_nRoomLpfFbk) << 15) / temp;
  726. ALOGV("get REVERB_PARAM_ROOM_HF_LEVEL, gain %d, m_nRoomLpfFwd %d, m_nRoomLpfFbk %d", temp, pReverb->m_nRoomLpfFwd, pReverb->m_nRoomLpfFbk);
  727. *pValue16 = Effects_Linear16ToMillibels(temp);
  728. if (param == REVERB_PARAM_ROOM_HF_LEVEL) {
  729. break;
  730. }
  731. pValue32 = (int32_t *)&pProperties->decayTime;
  732. /* FALL THROUGH */
  733. case REVERB_PARAM_DECAY_TIME:
  734. // Calculate reverb feedback path gain
  735. temp = (pReverb->m_nRvbLpfFwd << 15) / (32767 - pReverb->m_nRvbLpfFbk);
  736. temp = Effects_Linear16ToMillibels(temp);
  737. // Calculate decay time: g = -6000 d/DT , g gain in millibels, d reverb delay, DT decay time
  738. temp = (-6000 * pReverb->m_nLateDelay) / temp;
  739. // Convert samples to ms
  740. *pValue32 = (temp * 1000) / pReverb->m_nSamplingRate;
  741. ALOGV("get REVERB_PARAM_DECAY_TIME, samples %d, ms %d", temp, *pValue32);
  742. if (param == REVERB_PARAM_DECAY_TIME) {
  743. break;
  744. }
  745. pValue16 = &pProperties->decayHFRatio;
  746. /* FALL THROUGH */
  747. case REVERB_PARAM_DECAY_HF_RATIO:
  748. // If r is the decay HF ratio (r = REVERB_PARAM_DECAY_HF_RATIO/1000) we have:
  749. // DT_5000Hz = DT_0Hz * r
  750. // and G_5000Hz = -6000 * d / DT_5000Hz and G_0Hz = -6000 * d / DT_0Hz in millibels so :
  751. // r = G_0Hz/G_5000Hz in millibels
  752. // The linear gain at 5000Hz is b0 / sqrt(a1^2 + 2*C*a1 + 1) where:
  753. // - a1 is minus the LP feedback gain: -pReverb->m_nRvbLpfFbk
  754. // - b0 is the LP forward gain: pReverb->m_nRvbLpfFwd
  755. // - C is cos(2piWT) @ 5000Hz: pReverb->m_nCosWT_5KHz
  756. if (pReverb->m_nRvbLpfFbk == 0) {
  757. *pValue16 = 1000;
  758. ALOGV("get REVERB_PARAM_DECAY_HF_RATIO, pReverb->m_nRvbLpfFbk == 0, ratio %d", *pValue16);
  759. } else {
  760. temp = MULT_EG1_EG1(pReverb->m_nRvbLpfFbk, pReverb->m_nRvbLpfFbk);
  761. temp2 = MULT_EG1_EG1(pReverb->m_nRvbLpfFbk, pReverb->m_nCosWT_5KHz)
  762. << 1;
  763. temp = 32767 + temp - temp2;
  764. temp = Effects_Sqrt(temp) * 181;
  765. temp = (pReverb->m_nRvbLpfFwd << 15) / temp;
  766. // The linear gain at 0Hz is b0 / (a1 + 1)
  767. temp2 = (pReverb->m_nRvbLpfFwd << 15) / (32767
  768. - pReverb->m_nRvbLpfFbk);
  769. temp = Effects_Linear16ToMillibels(temp);
  770. temp2 = Effects_Linear16ToMillibels(temp2);
  771. ALOGV("get REVERB_PARAM_DECAY_HF_RATIO, gain 5KHz %d mB, gain DC %d mB", temp, temp2);
  772. if (temp == 0)
  773. temp = 1;
  774. temp = (int16_t) ((1000 * temp2) / temp);
  775. if (temp > 1000)
  776. temp = 1000;
  777. *pValue16 = temp;
  778. ALOGV("get REVERB_PARAM_DECAY_HF_RATIO, ratio %d", *pValue16);
  779. }
  780. if (param == REVERB_PARAM_DECAY_HF_RATIO) {
  781. break;
  782. }
  783. pValue16 = &pProperties->reflectionsLevel;
  784. /* FALL THROUGH */
  785. case REVERB_PARAM_REFLECTIONS_LEVEL:
  786. *pValue16 = Effects_Linear16ToMillibels(pReverb->m_nEarlyGain);
  787. ALOGV("get REVERB_PARAM_REFLECTIONS_LEVEL, %d", *pValue16);
  788. if (param == REVERB_PARAM_REFLECTIONS_LEVEL) {
  789. break;
  790. }
  791. pValue32 = (int32_t *)&pProperties->reflectionsDelay;
  792. /* FALL THROUGH */
  793. case REVERB_PARAM_REFLECTIONS_DELAY:
  794. // convert samples to ms
  795. *pValue32 = (pReverb->m_nEarlyDelay * 1000) / pReverb->m_nSamplingRate;
  796. ALOGV("get REVERB_PARAM_REFLECTIONS_DELAY, samples %d, ms %d", pReverb->m_nEarlyDelay, *pValue32);
  797. if (param == REVERB_PARAM_REFLECTIONS_DELAY) {
  798. break;
  799. }
  800. pValue16 = &pProperties->reverbLevel;
  801. /* FALL THROUGH */
  802. case REVERB_PARAM_REVERB_LEVEL:
  803. // Convert linear gain to millibels
  804. *pValue16 = Effects_Linear16ToMillibels(pReverb->m_nLateGain << 2);
  805. ALOGV("get REVERB_PARAM_REVERB_LEVEL %d", *pValue16);
  806. if (param == REVERB_PARAM_REVERB_LEVEL) {
  807. break;
  808. }
  809. pValue32 = (int32_t *)&pProperties->reverbDelay;
  810. /* FALL THROUGH */
  811. case REVERB_PARAM_REVERB_DELAY:
  812. // convert samples to ms
  813. *pValue32 = (pReverb->m_nLateDelay * 1000) / pReverb->m_nSamplingRate;
  814. ALOGV("get REVERB_PARAM_REVERB_DELAY, samples %d, ms %d", pReverb->m_nLateDelay, *pValue32);
  815. if (param == REVERB_PARAM_REVERB_DELAY) {
  816. break;
  817. }
  818. pValue16 = &pProperties->diffusion;
  819. /* FALL THROUGH */
  820. case REVERB_PARAM_DIFFUSION:
  821. temp = (int16_t) ((1000 * (pReverb->m_sAp0.m_nApGain - AP0_GAIN_BASE))
  822. / AP0_GAIN_RANGE);
  823. if (temp < 0)
  824. temp = 0;
  825. if (temp > 1000)
  826. temp = 1000;
  827. *pValue16 = temp;
  828. ALOGV("get REVERB_PARAM_DIFFUSION, %d, AP0 gain %d", *pValue16, pReverb->m_sAp0.m_nApGain);
  829. if (param == REVERB_PARAM_DIFFUSION) {
  830. break;
  831. }
  832. pValue16 = &pProperties->density;
  833. /* FALL THROUGH */
  834. case REVERB_PARAM_DENSITY:
  835. // Calculate AP delay in time units
  836. temp = ((pReverb->m_sAp0.m_zApOut - pReverb->m_sAp0.m_zApIn) << 16)
  837. / pReverb->m_nSamplingRate;
  838. temp = (int16_t) ((1000 * (temp - AP0_TIME_BASE)) / AP0_TIME_RANGE);
  839. if (temp < 0)
  840. temp = 0;
  841. if (temp > 1000)
  842. temp = 1000;
  843. *pValue16 = temp;
  844. ALOGV("get REVERB_PARAM_DENSITY, %d, AP0 delay smps %d", *pValue16, pReverb->m_sAp0.m_zApOut - pReverb->m_sAp0.m_zApIn);
  845. break;
  846. default:
  847. break;
  848. }
  849. }
  850. *pSize = size;
  851. ALOGV("Reverb_getParameter, context %p, param %d, value %d",
  852. pReverb, param, *(int *)pValue);
  853. return 0;
  854. } /* end Reverb_getParameter */
  855. /*----------------------------------------------------------------------------
  856. * Reverb_setParameter()
  857. *----------------------------------------------------------------------------
  858. * Purpose:
  859. * Set a Reverb parameter
  860. *
  861. * Inputs:
  862. * pReverb - handle to instance data
  863. * param - parameter
  864. * pValue - pointer to parameter value
  865. * size - value size
  866. *
  867. * Outputs:
  868. *
  869. *
  870. * Side Effects:
  871. *
  872. *----------------------------------------------------------------------------
  873. */
  874. int Reverb_setParameter(reverb_object_t *pReverb, int32_t param, uint32_t size,
  875. void *pValue) {
  876. int32_t value32;
  877. int16_t value16;
  878. t_reverb_settings *pProperties;
  879. int32_t i;
  880. int32_t temp;
  881. int32_t temp2;
  882. reverb_preset_t *pPreset;
  883. int maxSamples;
  884. int32_t averageDelay;
  885. uint32_t paramSize;
  886. ALOGV("Reverb_setParameter, context %p, param %d, value16 %d, value32 %d",
  887. pReverb, param, *(int16_t *)pValue, *(int32_t *)pValue);
  888. if (pReverb->m_Preset) {
  889. if (param != REVERB_PARAM_PRESET || size != sizeof(int16_t)) {
  890. return -EINVAL;
  891. }
  892. value16 = *(int16_t *)pValue;
  893. ALOGV("set REVERB_PARAM_PRESET, preset %d", value16);
  894. if (value16 < REVERB_PRESET_NONE || value16 > REVERB_PRESET_PLATE) {
  895. return -EINVAL;
  896. }
  897. // REVERB_PRESET_NONE is mapped to bypass
  898. if (value16 == REVERB_PRESET_NONE) {
  899. pReverb->m_bBypass = 1;
  900. } else {
  901. pReverb->m_bBypass = 0;
  902. pReverb->m_nNextRoom = value16 - 1;
  903. }
  904. } else {
  905. switch (param) {
  906. case REVERB_PARAM_ROOM_LEVEL:
  907. case REVERB_PARAM_ROOM_HF_LEVEL:
  908. case REVERB_PARAM_DECAY_HF_RATIO:
  909. case REVERB_PARAM_REFLECTIONS_LEVEL:
  910. case REVERB_PARAM_REVERB_LEVEL:
  911. case REVERB_PARAM_DIFFUSION:
  912. case REVERB_PARAM_DENSITY:
  913. paramSize = sizeof(int16_t);
  914. break;
  915. case REVERB_PARAM_BYPASS:
  916. case REVERB_PARAM_DECAY_TIME:
  917. case REVERB_PARAM_REFLECTIONS_DELAY:
  918. case REVERB_PARAM_REVERB_DELAY:
  919. paramSize = sizeof(int32_t);
  920. break;
  921. case REVERB_PARAM_PROPERTIES:
  922. paramSize = sizeof(t_reverb_settings);
  923. break;
  924. default:
  925. return -EINVAL;
  926. }
  927. if (size != paramSize) {
  928. return -EINVAL;
  929. }
  930. if (paramSize == sizeof(int16_t)) {
  931. value16 = *(int16_t *) pValue;
  932. } else if (paramSize == sizeof(int32_t)) {
  933. value32 = *(int32_t *) pValue;
  934. } else {
  935. pProperties = (t_reverb_settings *) pValue;
  936. }
  937. pPreset = &pReverb->m_sPreset.m_sPreset[pReverb->m_nNextRoom];
  938. switch (param) {
  939. case REVERB_PARAM_BYPASS:
  940. pReverb->m_bBypass = (uint16_t)value32;
  941. break;
  942. case REVERB_PARAM_PROPERTIES:
  943. value16 = pProperties->roomLevel;
  944. /* FALL THROUGH */
  945. case REVERB_PARAM_ROOM_LEVEL:
  946. // Convert millibels to linear 16 bit signed => m_nRoomLpfFwd
  947. if (value16 > 0)
  948. return -EINVAL;
  949. temp = Effects_MillibelsToLinear16(value16);
  950. pReverb->m_nRoomLpfFwd
  951. = MULT_EG1_EG1(temp, (32767 - pReverb->m_nRoomLpfFbk));
  952. ALOGV("REVERB_PARAM_ROOM_LEVEL, gain %d, new m_nRoomLpfFwd %d, m_nRoomLpfFbk %d", temp, pReverb->m_nRoomLpfFwd, pReverb->m_nRoomLpfFbk);
  953. if (param == REVERB_PARAM_ROOM_LEVEL)
  954. break;
  955. value16 = pProperties->roomHFLevel;
  956. /* FALL THROUGH */
  957. case REVERB_PARAM_ROOM_HF_LEVEL:
  958. // Limit to 0 , -40dB range because of low pass implementation
  959. if (value16 > 0 || value16 < -4000)
  960. return -EINVAL;
  961. // Convert attenuation @ 5000H expressed in millibels to => m_nRoomLpfFbk
  962. // m_nRoomLpfFbk is -a1 where a1 is the solution of:
  963. // a1^2 + 2*(C-dG^2)/(1-dG^2)*a1 + 1 = 0 where:
  964. // - C is cos(2*pi*5000/Fs) (pReverb->m_nCosWT_5KHz)
  965. // - dG is G0/Gf (G0 is the linear gain at DC and Gf is the wanted gain at 5000Hz)
  966. // Save current DC gain m_nRoomLpfFwd / (32767 - m_nRoomLpfFbk) to keep it unchanged
  967. // while changing HF level
  968. temp2 = (pReverb->m_nRoomLpfFwd << 15) / (32767
  969. - pReverb->m_nRoomLpfFbk);
  970. if (value16 == 0) {
  971. pReverb->m_nRoomLpfFbk = 0;
  972. } else {
  973. int32_t dG2, b, delta;
  974. // dG^2
  975. temp = Effects_MillibelsToLinear16(value16);
  976. ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, HF gain %d", temp);
  977. temp = (1 << 30) / temp;
  978. ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, 1/ HF gain %d", temp);
  979. dG2 = (int32_t) (((int64_t) temp * (int64_t) temp) >> 15);
  980. ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, 1/ HF gain ^ 2 %d", dG2);
  981. // b = 2*(C-dG^2)/(1-dG^2)
  982. b = (int32_t) ((((int64_t) 1 << (15 + 1))
  983. * ((int64_t) pReverb->m_nCosWT_5KHz - (int64_t) dG2))
  984. / ((int64_t) 32767 - (int64_t) dG2));
  985. // delta = b^2 - 4
  986. delta = (int32_t) ((((int64_t) b * (int64_t) b) >> 15) - (1 << (15
  987. + 2)));
  988. ALOGV_IF(delta > (1<<30), " delta overflow %d", delta);
  989. ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, dG2 %d, b %d, delta %d, m_nCosWT_5KHz %d", dG2, b, delta, pReverb->m_nCosWT_5KHz);
  990. // m_nRoomLpfFbk = -a1 = - (- b + sqrt(delta)) / 2
  991. pReverb->m_nRoomLpfFbk = (b - Effects_Sqrt(delta) * 181) >> 1;
  992. }
  993. ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, olg DC gain %d new m_nRoomLpfFbk %d, old m_nRoomLpfFwd %d",
  994. temp2, pReverb->m_nRoomLpfFbk, pReverb->m_nRoomLpfFwd);
  995. pReverb->m_nRoomLpfFwd
  996. = MULT_EG1_EG1(temp2, (32767 - pReverb->m_nRoomLpfFbk));
  997. ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, new m_nRoomLpfFwd %d", pReverb->m_nRoomLpfFwd);
  998. if (param == REVERB_PARAM_ROOM_HF_LEVEL)
  999. break;
  1000. value32 = pProperties->decayTime;
  1001. /* FALL THROUGH */
  1002. case REVERB_PARAM_DECAY_TIME:
  1003. // Convert milliseconds to => m_nRvbLpfFwd (function of m_nRvbLpfFbk)
  1004. // convert ms to samples
  1005. value32 = (value32 * pReverb->m_nSamplingRate) / 1000;
  1006. // calculate valid decay time range as a function of current reverb delay and
  1007. // max feed back gain. Min value <=> -40dB in one pass, Max value <=> feedback gain = -1 dB
  1008. // Calculate attenuation for each round in late reverb given a total attenuation of -6000 millibels.
  1009. // g = -6000 d/DT , g gain in millibels, d reverb delay, DT decay time
  1010. averageDelay = pReverb->m_nLateDelay - pReverb->m_nMaxExcursion;
  1011. averageDelay += ((pReverb->m_sAp0.m_zApOut - pReverb->m_sAp0.m_zApIn)
  1012. + (pReverb->m_sAp1.m_zApOut - pReverb->m_sAp1.m_zApIn)) >> 1;
  1013. temp = (-6000 * averageDelay) / value32;
  1014. ALOGV("REVERB_PARAM_DECAY_TIME, delay smps %d, DT smps %d, gain mB %d",averageDelay, value32, temp);
  1015. if (temp < -4000 || temp > -100)
  1016. return -EINVAL;
  1017. // calculate low pass gain by adding reverb input attenuation (pReverb->m_nLateGain) and substrating output
  1018. // xfade and sum gain (max +9dB)
  1019. temp -= Effects_Linear16ToMillibels(pReverb->m_nLateGain) + 900;
  1020. temp = Effects_MillibelsToLinear16(temp);
  1021. // DC gain (temp) = b0 / (1 + a1) = pReverb->m_nRvbLpfFwd / (32767 - pReverb->m_nRvbLpfFbk)
  1022. pReverb->m_nRvbLpfFwd
  1023. = MULT_EG1_EG1(temp, (32767 - pReverb->m_nRvbLpfFbk));
  1024. ALOGV("REVERB_PARAM_DECAY_TIME, gain %d, new m_nRvbLpfFwd %d, old m_nRvbLpfFbk %d, reverb gain %d", temp, pReverb->m_nRvbLpfFwd, pReverb->m_nRvbLpfFbk, Effects_Linear16ToMillibels(pReverb->m_nLateGain));
  1025. if (param == REVERB_PARAM_DECAY_TIME)
  1026. break;
  1027. value16 = pProperties->decayHFRatio;
  1028. /* FALL THROUGH */
  1029. case REVERB_PARAM_DECAY_HF_RATIO:
  1030. // We limit max value to 1000 because reverb filter is lowpass only
  1031. if (value16 < 100 || value16 > 1000)
  1032. return -EINVAL;
  1033. // Convert per mille to => m_nLpfFwd, m_nLpfFbk
  1034. // Save current DC gain m_nRoomLpfFwd / (32767 - m_nRoomLpfFbk) to keep it unchanged
  1035. // while changing HF level
  1036. temp2 = (pReverb->m_nRvbLpfFwd << 15) / (32767 - pReverb->m_nRvbLpfFbk);
  1037. if (value16 == 1000) {
  1038. pReverb->m_nRvbLpfFbk = 0;
  1039. } else {
  1040. int32_t dG2, b, delta;
  1041. temp = Effects_Linear16ToMillibels(temp2);
  1042. // G_5000Hz = G_DC * (1000/REVERB_PARAM_DECAY_HF_RATIO) in millibels
  1043. value32 = ((int32_t) 1000 << 15) / (int32_t) value16;
  1044. ALOGV("REVERB_PARAM_DECAY_HF_RATIO, DC gain %d, DC gain mB %d, 1000/R %d", temp2, temp, value32);
  1045. temp = (int32_t) (((int64_t) temp * (int64_t) value32) >> 15);
  1046. if (temp < -4000) {
  1047. ALOGV("REVERB_PARAM_DECAY_HF_RATIO HF gain overflow %d mB", temp);
  1048. temp = -4000;
  1049. }
  1050. temp = Effects_MillibelsToLinear16(temp);
  1051. ALOGV("REVERB_PARAM_DECAY_HF_RATIO, HF gain %d", temp);
  1052. // dG^2
  1053. temp = (temp2 << 15) / temp;
  1054. dG2 = (int32_t) (((int64_t) temp * (int64_t) temp) >> 15);
  1055. // b = 2*(C-dG^2)/(1-dG^2)
  1056. b = (int32_t) ((((int64_t) 1 << (15 + 1))
  1057. * ((int64_t) pReverb->m_nCosWT_5KHz - (int64_t) dG2))
  1058. / ((int64_t) 32767 - (int64_t) dG2));
  1059. // delta = b^2 - 4
  1060. delta = (int32_t) ((((int64_t) b * (int64_t) b) >> 15) - (1 << (15
  1061. + 2)));
  1062. // m_nRoomLpfFbk = -a1 = - (- b + sqrt(delta)) / 2
  1063. pReverb->m_nRvbLpfFbk = (b - Effects_Sqrt(delta) * 181) >> 1;
  1064. ALOGV("REVERB_PARAM_DECAY_HF_RATIO, dG2 %d, b %d, delta %d", dG2, b, delta);
  1065. }
  1066. ALOGV("REVERB_PARAM_DECAY_HF_RATIO, gain %d, m_nRvbLpfFbk %d, m_nRvbLpfFwd %d", temp2, pReverb->m_nRvbLpfFbk, pReverb->m_nRvbLpfFwd);
  1067. pReverb->m_nRvbLpfFwd
  1068. = MULT_EG1_EG1(temp2, (32767 - pReverb->m_nRvbLpfFbk));
  1069. if (param == REVERB_PARAM_DECAY_HF_RATIO)
  1070. break;
  1071. value16 = pProperties->reflectionsLevel;
  1072. /* FALL THROUGH */
  1073. case REVERB_PARAM_REFLECTIONS_LEVEL:
  1074. // We limit max value to 0 because gain is limited to 0dB
  1075. if (value16 > 0 || value16 < -6000)
  1076. return -EINVAL;
  1077. // Convert millibels to linear 16 bit signed and recompute m_sEarlyL.m_nGain[i] and m_sEarlyR.m_nGain[i].
  1078. value16 = Effects_MillibelsToLinear16(value16);
  1079. for (i = 0; i < REVERB_MAX_NUM_REFLECTIONS; i++) {
  1080. pReverb->m_sEarlyL.m_nGain[i]
  1081. = MULT_EG1_EG1(pPreset->m_sEarlyL.m_nGain[i],value16);
  1082. pReverb->m_sEarlyR.m_nGain[i]
  1083. = MULT_EG1_EG1(pPreset->m_sEarlyR.m_nGain[i],value16);
  1084. }
  1085. pReverb->m_nEarlyGain = value16;
  1086. ALOGV("REVERB_PARAM_REFLECTIONS_LEVEL, m_nEarlyGain %d", pReverb->m_nEarlyGain);
  1087. if (param == REVERB_PARAM_REFLECTIONS_LEVEL)
  1088. break;
  1089. value32 = pProperties->reflectionsDelay;
  1090. /* FALL THROUGH */
  1091. case REVERB_PARAM_REFLECTIONS_DELAY:
  1092. // We limit max value MAX_EARLY_TIME
  1093. // convert ms to time units
  1094. temp = (value32 * 65536) / 1000;
  1095. if (temp < 0 || temp > MAX_EARLY_TIME)
  1096. return -EINVAL;
  1097. maxSamples = (int32_t) (MAX_EARLY_TIME * pReverb->m_nSamplingRate)
  1098. >> 16;
  1099. temp = (temp * pReverb->m_nSamplingRate) >> 16;
  1100. for (i = 0; i < REVERB_MAX_NUM_REFLECTIONS; i++) {
  1101. temp2 = temp + (((int32_t) pPreset->m_sEarlyL.m_zDelay[i]
  1102. * pReverb->m_nSamplingRate) >> 16);
  1103. if (temp2 > maxSamples)
  1104. temp2 = maxSamples;
  1105. pReverb->m_sEarlyL.m_zDelay[i] = pReverb->m_nEarly0in + temp2;
  1106. temp2 = temp + (((int32_t) pPreset->m_sEarlyR.m_zDelay[i]
  1107. * pReverb->m_nSamplingRate) >> 16);
  1108. if (temp2 > maxSamples)
  1109. temp2 = maxSamples;
  1110. pReverb->m_sEarlyR.m_zDelay[i] = pReverb->m_nEarly1in + temp2;
  1111. }
  1112. pReverb->m_nEarlyDelay = temp;
  1113. ALOGV("REVERB_PARAM_REFLECTIONS_DELAY, m_nEarlyDelay smps %d max smp delay %d", pReverb->m_nEarlyDelay, maxSamples);
  1114. // Convert milliseconds to sample count => m_nEarlyDelay
  1115. if (param == REVERB_PARAM_REFLECTIONS_DELAY)
  1116. break;
  1117. value16 = pProperties->reverbLevel;
  1118. /* FALL THROUGH */
  1119. case REVERB_PARAM_REVERB_LEVEL:
  1120. // We limit max value to 0 because gain is limited to 0dB
  1121. if (value16 > 0 || value16 < -6000)
  1122. return -EINVAL;
  1123. // Convert millibels to linear 16 bits (gange 0 - 8191) => m_nLateGain.
  1124. pReverb->m_nLateGain = Effects_MillibelsToLinear16(value16) >> 2;
  1125. ALOGV("REVERB_PARAM_REVERB_LEVEL, m_nLateGain %d", pReverb->m_nLateGain);
  1126. if (param == REVERB_PARAM_REVERB_LEVEL)
  1127. break;
  1128. value32 = pProperties->reverbDelay;
  1129. /* FALL THROUGH */
  1130. case REVERB_PARAM_REVERB_DELAY:
  1131. // We limit max value to MAX_DELAY_TIME
  1132. // convert ms to time units
  1133. temp = (value32 * 65536) / 1000;
  1134. if (temp < 0 || temp > MAX_DELAY_TIME)
  1135. return -EINVAL;
  1136. maxSamples = (int32_t) (MAX_DELAY_TIME * pReverb->m_nSamplingRate)
  1137. >> 16;
  1138. temp = (temp * pReverb->m_nSamplingRate) >> 16;
  1139. if ((temp + pReverb->m_nMaxExcursion) > maxSamples) {
  1140. temp = maxSamples - pReverb->m_nMaxExcursion;
  1141. }
  1142. if (temp < pReverb->m_nMaxExcursion) {
  1143. temp = pReverb->m_nMaxExcursion;
  1144. }
  1145. temp -= pReverb->m_nLateDelay;
  1146. pReverb->m_nDelay0Out += temp;
  1147. pReverb->m_nDelay1Out += temp;
  1148. pReverb->m_nLateDelay += temp;
  1149. ALOGV("REVERB_PARAM_REVERB_DELAY, m_nLateDelay smps %d max smp delay %d", pReverb->m_nLateDelay, maxSamples);
  1150. // Convert milliseconds to sample count => m_nDelay1Out + m_nMaxExcursion
  1151. if (param == REVERB_PARAM_REVERB_DELAY)
  1152. break;
  1153. value16 = pProperties->diffusion;
  1154. /* FALL THROUGH */
  1155. case REVERB_PARAM_DIFFUSION:
  1156. if (value16 < 0 || value16 > 1000)
  1157. return -EINVAL;
  1158. // Convert per mille to m_sAp0.m_nApGain, m_sAp1.m_nApGain
  1159. pReverb->m_sAp0.m_nApGain = AP0_GAIN_BASE + ((int32_t) value16
  1160. * AP0_GAIN_RANGE) / 1000;
  1161. pReverb->m_sAp1.m_nApGain = AP1_GAIN_BASE + ((int32_t) value16
  1162. * AP1_GAIN_RANGE) / 1000;
  1163. ALOGV("REVERB_PARAM_DIFFUSION, m_sAp0.m_nApGain %d m_sAp1.m_nApGain %d", pReverb->m_sAp0.m_nApGain, pReverb->m_sAp1.m_nApGain);
  1164. if (param == REVERB_PARAM_DIFFUSION)
  1165. break;
  1166. value16 = pProperties->density;
  1167. /* FALL THROUGH */
  1168. case REVERB_PARAM_DENSITY:
  1169. if (value16 < 0 || value16 > 1000)
  1170. return -EINVAL;
  1171. // Convert per mille to m_sAp0.m_zApOut, m_sAp1.m_zApOut
  1172. maxSamples = (int32_t) (MAX_AP_TIME * pReverb->m_nSamplingRate) >> 16;
  1173. temp = AP0_TIME_BASE + ((int32_t) value16 * AP0_TIME_RANGE) / 1000;
  1174. /*lint -e{702} shift for performance */
  1175. temp = (temp * pReverb->m_nSamplingRate) >> 16;
  1176. if (temp > maxSamples)
  1177. temp = maxSamples;
  1178. pReverb->m_sAp0.m_zApOut = (uint16_t) (pReverb->m_sAp0.m_zApIn + temp);
  1179. ALOGV("REVERB_PARAM_DENSITY, Ap0 delay smps %d", temp);
  1180. temp = AP1_TIME_BASE + ((int32_t) value16 * AP1_TIME_RANGE) / 1000;
  1181. /*lint -e{702} shift for performance */
  1182. temp = (temp * pReverb->m_nSamplingRate) >> 16;
  1183. if (temp > maxSamples)
  1184. temp = maxSamples;
  1185. pReverb->m_sAp1.m_zApOut = (uint16_t) (pReverb->m_sAp1.m_zApIn + temp);
  1186. ALOGV("Ap1 delay smps %d", temp);
  1187. break;
  1188. default:
  1189. break;
  1190. }
  1191. }
  1192. return 0;
  1193. } /* end Reverb_setParameter */
  1194. /*----------------------------------------------------------------------------
  1195. * ReverbUpdateXfade
  1196. *----------------------------------------------------------------------------
  1197. * Purpose:
  1198. * Update the xfade parameters as required
  1199. *
  1200. * Inputs:
  1201. * nNumSamplesToAdd - number of samples to write to buffer
  1202. *
  1203. * Outputs:
  1204. *
  1205. *
  1206. * Side Effects:
  1207. * - xfade parameters will be changed
  1208. *
  1209. *----------------------------------------------------------------------------
  1210. */
  1211. static int ReverbUpdateXfade(reverb_object_t *pReverb, int nNumSamplesToAdd) {
  1212. uint16_t nOffset;
  1213. int16_t tempCos;
  1214. int16_t tempSin;
  1215. if (pReverb->m_nXfadeCounter >= pReverb->m_nXfadeInterval) {
  1216. /* update interval has elapsed, so reset counter */
  1217. pReverb->m_nXfadeCounter = 0;
  1218. // Pin the sin,cos values to min / max values to ensure that the
  1219. // modulated taps' coefs are zero (thus no clicks)
  1220. if (pReverb->m_nPhaseIncrement > 0) {
  1221. // if phase increment > 0, then sin -> 1, cos -> 0
  1222. pReverb->m_nSin = 32767;
  1223. pReverb->m_nCos = 0;
  1224. // reset the phase to match the sin, cos values
  1225. pReverb->m_nPhase = 32767;
  1226. // modulate the cross taps because their tap coefs are zero
  1227. nOffset = ReverbCalculateNoise(pReverb);
  1228. pReverb->m_zD1Cross = pReverb->m_nDelay1Out
  1229. - pReverb->m_nMaxExcursion + nOffset;
  1230. nOffset = ReverbCalculateNoise(pReverb);
  1231. pReverb->m_zD0Cross = pReverb->m_nDelay0Out
  1232. - pReverb->m_nMaxExcursion - nOffset;
  1233. } else {
  1234. // if phase increment < 0, then sin -> 0, cos -> 1
  1235. pReverb->m_nSin = 0;
  1236. pReverb->m_nCos = 32767;
  1237. // reset the phase to match the sin, cos values
  1238. pReverb->m_nPhase = -32768;
  1239. // modulate the self taps because their tap coefs are zero
  1240. nOffset = ReverbCalculateNoise(pReverb);
  1241. pReverb->m_zD0Self = pReverb->m_nDelay0Out
  1242. - pReverb->m_nMaxExcursion - nOffset;
  1243. nOffset = ReverbCalculateNoise(pReverb);
  1244. pReverb->m_zD1Self = pReverb->m_nDelay1Out
  1245. - pReverb->m_nMaxExcursion + nOffset;
  1246. } // end if-else (pReverb->m_nPhaseIncrement > 0)
  1247. // Reverse the direction of the sin,cos so that the
  1248. // tap whose coef was previously increasing now decreases
  1249. // and vice versa
  1250. pReverb->m_nPhaseIncrement = -pReverb->m_nPhaseIncrement;
  1251. } // end if counter >= update interval
  1252. //compute what phase will be next time
  1253. pReverb->m_nPhase += pReverb->m_nPhaseIncrement;
  1254. //calculate what the new sin and cos need to reach by the next update
  1255. ReverbCalculateSinCos(pReverb->m_nPhase, &tempSin, &tempCos);
  1256. //calculate the per-sample increment required to get there by the next update
  1257. /*lint -e{702} shift for performance */
  1258. pReverb->m_nSinIncrement = (tempSin - pReverb->m_nSin)
  1259. >> pReverb->m_nUpdatePeriodInBits;
  1260. /*lint -e{702} shift for performance */
  1261. pReverb->m_nCosIncrement = (tempCos - pReverb->m_nCos)
  1262. >> pReverb->m_nUpdatePeriodInBits;
  1263. /* increment update counter */
  1264. pReverb->m_nXfadeCounter += (uint16_t) nNumSamplesToAdd;
  1265. return 0;
  1266. } /* end ReverbUpdateXfade */
  1267. /*----------------------------------------------------------------------------
  1268. * ReverbCalculateNoise
  1269. *----------------------------------------------------------------------------
  1270. * Purpose:
  1271. * Calculate a noise sample and limit its value
  1272. *
  1273. * Inputs:
  1274. * nMaxExcursion - noise value is limited to this value
  1275. * pnNoise - return new noise sample in this (not limited)
  1276. *
  1277. * Outputs:
  1278. * new limited noise value
  1279. *
  1280. * Side Effects:
  1281. * - *pnNoise noise value is updated
  1282. *
  1283. *----------------------------------------------------------------------------
  1284. */
  1285. static uint16_t ReverbCalculateNoise(reverb_object_t *pReverb) {
  1286. int16_t nNoise = pReverb->m_nNoise;
  1287. // calculate new noise value
  1288. if (pReverb->m_bUseNoise) {
  1289. nNoise = (int16_t) (nNoise * 5 + 1);
  1290. } else {
  1291. nNoise = 0;
  1292. }
  1293. pReverb->m_nNoise = nNoise;
  1294. // return the limited noise value
  1295. return (pReverb->m_nMaxExcursion & nNoise);
  1296. } /* end ReverbCalculateNoise */
  1297. /*----------------------------------------------------------------------------
  1298. * ReverbCalculateSinCos
  1299. *----------------------------------------------------------------------------
  1300. * Purpose:
  1301. * Calculate a new sin and cosine value based on the given phase
  1302. *
  1303. * Inputs:
  1304. * nPhase - phase angle
  1305. * pnSin - input old value, output new value
  1306. * pnCos - input old value, output new value
  1307. *
  1308. * Outputs:
  1309. *
  1310. * Side Effects:
  1311. * - *pnSin, *pnCos are updated
  1312. *
  1313. *----------------------------------------------------------------------------
  1314. */
  1315. static int ReverbCalculateSinCos(int16_t nPhase, int16_t *pnSin, int16_t *pnCos) {
  1316. int32_t nTemp;
  1317. int32_t nNetAngle;
  1318. // -1 <= nPhase < 1
  1319. // However, for the calculation, we need a value
  1320. // that ranges from -1/2 to +1/2, so divide the phase by 2
  1321. /*lint -e{702} shift for performance */
  1322. nNetAngle = nPhase >> 1;
  1323. /*
  1324. Implement the following
  1325. sin(x) = (2-4*c)*x^2 + c + x
  1326. cos(x) = (2-4*c)*x^2 + c - x
  1327. where c = 1/sqrt(2)
  1328. using the a0 + x*(a1 + x*a2) approach
  1329. */
  1330. /* limit the input "angle" to be between -0.5 and +0.5 */
  1331. if (nNetAngle > EG1_HALF) {
  1332. nNetAngle = EG1_HALF;
  1333. } else if (nNetAngle < EG1_MINUS_HALF) {
  1334. nNetAngle = EG1_MINUS_HALF;
  1335. }
  1336. /* calculate sin */
  1337. nTemp = EG1_ONE + MULT_EG1_EG1(REVERB_PAN_G2, nNetAngle);
  1338. nTemp = REVERB_PAN_G0 + MULT_EG1_EG1(nTemp, nNetAngle);
  1339. *pnSin = (int16_t) SATURATE_EG1(nTemp);
  1340. /* calculate cos */
  1341. nTemp = -EG1_ONE + MULT_EG1_EG1(REVERB_PAN_G2, nNetAngle);
  1342. nTemp = REVERB_PAN_G0 + MULT_EG1_EG1(nTemp, nNetAngle);
  1343. *pnCos = (int16_t) SATURATE_EG1(nTemp);
  1344. return 0;
  1345. } /* end ReverbCalculateSinCos */
  1346. /*----------------------------------------------------------------------------
  1347. * Reverb
  1348. *----------------------------------------------------------------------------
  1349. * Purpose:
  1350. * apply reverb to the given signal
  1351. *
  1352. * Inputs:
  1353. * nNu
  1354. * pnSin - input old value, output new value
  1355. * pnCos - input old value, output new value
  1356. *
  1357. * Outputs:
  1358. * number of samples actually reverberated
  1359. *
  1360. * Side Effects:
  1361. *
  1362. *----------------------------------------------------------------------------
  1363. */
  1364. static int Reverb(reverb_object_t *pReverb, int nNumSamplesToAdd,
  1365. short *pOutputBuffer, short *pInputBuffer) {
  1366. int32_t i;
  1367. int32_t nDelayOut0;
  1368. int32_t nDelayOut1;
  1369. uint16_t nBase;
  1370. uint32_t nAddr;
  1371. int32_t nTemp1;
  1372. int32_t nTemp2;
  1373. int32_t nApIn;
  1374. int32_t nApOut;
  1375. int32_t j;
  1376. int32_t nEarlyOut;
  1377. int32_t tempValue;
  1378. // get the base address
  1379. nBase = pReverb->m_nBaseIndex;
  1380. for (i = 0; i < nNumSamplesToAdd; i++) {
  1381. // ********** Left Allpass - start
  1382. nApIn = *pInputBuffer;
  1383. if (!pReverb->m_Aux) {
  1384. pInputBuffer++;
  1385. }
  1386. // store to early delay line
  1387. nAddr = CIRCULAR(nBase, pReverb->m_nEarly0in, pReverb->m_nBufferMask);
  1388. pReverb->m_nDelayLine[nAddr] = (short) nApIn;
  1389. // left input = (left dry * m_nLateGain) + right feedback from previous period
  1390. nApIn = SATURATE(nApIn + pReverb->m_nRevFbkR);
  1391. nApIn = MULT_EG1_EG1(nApIn, pReverb->m_nLateGain);
  1392. // fetch allpass delay line out
  1393. //nAddr = CIRCULAR(nBase, psAp0->m_zApOut, pReverb->m_nBufferMask);
  1394. nAddr
  1395. = CIRCULAR(nBase, pReverb->m_sAp0.m_zApOut, pReverb->m_nBufferMask);
  1396. nDelayOut0 = pReverb->m_nDelayLine[nAddr];
  1397. // calculate allpass feedforward; subtract the feedforward result
  1398. nTemp1 = MULT_EG1_EG1(nApIn, pReverb->m_sAp0.m_nApGain);
  1399. nApOut = SATURATE(nDelayOut0 - nTemp1); // allpass output
  1400. // calculate allpass feedback; add the feedback result
  1401. nTemp1 = MULT_EG1_EG1(nApOut, pReverb->m_sAp0.m_nApGain);
  1402. nTemp1 = SATURATE(nApIn + nTemp1);
  1403. // inject into allpass delay
  1404. nAddr
  1405. = CIRCULAR(nBase, pReverb->m_sAp0.m_zApIn, pReverb->m_nBufferMask);
  1406. pReverb->m_nDelayLine[nAddr] = (short) nTemp1;
  1407. // inject allpass output into delay line
  1408. nAddr = CIRCULAR(nBase, pReverb->m_zD0In, pReverb->m_nBufferMask);
  1409. pReverb->m_nDelayLine[nAddr] = (short) nApOut;
  1410. // ********** Left Allpass - end
  1411. // ********** Right Allpass - start
  1412. nApIn = (*pInputBuffer++);
  1413. // store to early delay line
  1414. nAddr = CIRCULAR(nBase, pReverb->m_nEarly1in, pReverb->m_nBufferMask);
  1415. pReverb->m_nDelayLine[nAddr] = (short) nApIn;
  1416. // right input = (right dry * m_nLateGain) + left feedback from previous period
  1417. /*lint -e{702} use shift for performance */
  1418. nApIn = SATURATE(nApIn + pReverb->m_nRevFbkL);
  1419. nApIn = MULT_EG1_EG1(nApIn, pReverb->m_nLateGain);
  1420. // fetch allpass delay line out
  1421. nAddr
  1422. = CIRCULAR(nBase, pReverb->m_sAp1.m_zApOut, pReverb->m_nBufferMask);
  1423. nDelayOut1 = pReverb->m_nDelayLine[nAddr];
  1424. // calculate allpass feedforward; subtract the feedforward result
  1425. nTemp1 = MULT_EG1_EG1(nApIn, pReverb->m_sAp1.m_nApGain);
  1426. nApOut = SATURATE(nDelayOut1 - nTemp1); // allpass output
  1427. // calculate allpass feedback; add the feedback result
  1428. nTemp1 = MULT_EG1_EG1(nApOut, pReverb->m_sAp1.m_nApGain);
  1429. nTemp1 = SATURATE(nApIn + nTemp1);
  1430. // inject into allpass delay
  1431. nAddr
  1432. = CIRCULAR(nBase, pReverb->m_sAp1.m_zApIn, pReverb->m_nBufferMask);
  1433. pReverb->m_nDelayLine[nAddr] = (short) nTemp1;
  1434. // inject allpass output into delay line
  1435. nAddr = CIRCULAR(nBase, pReverb->m_zD1In, pReverb->m_nBufferMask);
  1436. pReverb->m_nDelayLine[nAddr] = (short) nApOut;
  1437. // ********** Right Allpass - end
  1438. // ********** D0 output - start
  1439. // fetch delay line self out
  1440. nAddr = CIRCULAR(nBase, pReverb->m_zD0Self, pReverb->m_nBufferMask);
  1441. nDelayOut0 = pReverb->m_nDelayLine[nAddr];
  1442. // calculate delay line self out
  1443. nTemp1 = MULT_EG1_EG1(nDelayOut0, pReverb->m_nSin);
  1444. // fetch delay line cross out
  1445. nAddr = CIRCULAR(nBase, pReverb->m_zD1Cross, pReverb->m_nBufferMask);
  1446. nDelayOut0 = pReverb->m_nDelayLine[nAddr];
  1447. // calculate delay line self out
  1448. nTemp2 = MULT_EG1_EG1(nDelayOut0, pReverb->m_nCos);
  1449. // calculate unfiltered delay out
  1450. nDelayOut0 = SATURATE(nTemp1 + nTemp2);
  1451. // ********** D0 output - end
  1452. // ********** D1 output - start
  1453. // fetch delay line self out
  1454. nAddr = CIRCULAR(nBase, pReverb->m_zD1Self, pReverb->m_nBufferMask);
  1455. nDelayOut1 = pReverb->m_nDelayLine[nAddr];
  1456. // calculate delay line self out
  1457. nTemp1 = MULT_EG1_EG1(nDelayOut1, pReverb->m_nSin);
  1458. // fetch delay line cross out
  1459. nAddr = CIRCULAR(nBase, pReverb->m_zD0Cross, pReverb->m_nBufferMask);
  1460. nDelayOut1 = pReverb->m_nDelayLine[nAddr];
  1461. // calculate delay line self out
  1462. nTemp2 = MULT_EG1_EG1(nDelayOut1, pReverb->m_nCos);
  1463. // calculate unfiltered delay out
  1464. nDelayOut1 = SATURATE(nTemp1 + nTemp2);
  1465. // ********** D1 output - end
  1466. // ********** mixer and feedback - start
  1467. // sum is fedback to right input (R + L)
  1468. nDelayOut0 = (short) SATURATE(nDelayOut0 + nDelayOut1);
  1469. // difference is feedback to left input (R - L)
  1470. /*lint -e{685} lint complains that it can't saturate negative */
  1471. nDelayOut1 = (short) SATURATE(nDelayOut1 - nDelayOut0);
  1472. // ********** mixer and feedback - end
  1473. // calculate lowpass filter (mixer scale factor included in LPF feedforward)
  1474. nTemp1 = MULT_EG1_EG1(nDelayOut0, pReverb->m_nRvbLpfFwd);
  1475. nTemp2 = MULT_EG1_EG1(pReverb->m_nRevFbkL, pReverb->m_nRvbLpfFbk);
  1476. // calculate filtered delay out and simultaneously update LPF state variable
  1477. // filtered delay output is stored in m_nRevFbkL
  1478. pReverb->m_nRevFbkL = (short) SATURATE(nTemp1 + nTemp2);
  1479. // calculate lowpass filter (mixer scale factor included in LPF feedforward)
  1480. nTemp1 = MULT_EG1_EG1(nDelayOut1, pReverb->m_nRvbLpfFwd);
  1481. nTemp2 = MULT_EG1_EG1(pReverb->m_nRevFbkR, pReverb->m_nRvbLpfFbk);
  1482. // calculate filtered delay out and simultaneously update LPF state variable
  1483. // filtered delay output is stored in m_nRevFbkR
  1484. pReverb->m_nRevFbkR = (short) SATURATE(nTemp1 + nTemp2);
  1485. // ********** start early reflection generator, left
  1486. //psEarly = &(pReverb->m_sEarlyL);
  1487. for (j = 0; j < REVERB_MAX_NUM_REFLECTIONS; j++) {
  1488. // fetch delay line out
  1489. //nAddr = CIRCULAR(nBase, psEarly->m_zDelay[j], pReverb->m_nBufferMask);
  1490. nAddr
  1491. = CIRCULAR(nBase, pReverb->m_sEarlyL.m_zDelay[j], pReverb->m_nBufferMask);
  1492. nTemp1 = pReverb->m_nDelayLine[nAddr];
  1493. // calculate reflection
  1494. //nTemp1 = MULT_EG1_EG1(nDelayOut0, psEarly->m_nGain[j]);
  1495. nTemp1 = MULT_EG1_EG1(nTemp1, pReverb->m_sEarlyL.m_nGain[j]);
  1496. nDelayOut0 = SATURATE(nDelayOut0 + nTemp1);
  1497. } // end for (j=0; j < REVERB_MAX_NUM_REFLECTIONS; j++)
  1498. // apply lowpass to early reflections and reverb output
  1499. //nTemp1 = MULT_EG1_EG1(nEarlyOut, psEarly->m_nRvbLpfFwd);
  1500. nTemp1 = MULT_EG1_EG1(nDelayOut0, pReverb->m_nRoomLpfFwd);
  1501. //nTemp2 = MULT_EG1_EG1(psEarly->m_zLpf, psEarly->m_nLpfFbk);
  1502. nTemp2 = MULT_EG1_EG1(pReverb->m_zOutLpfL, pReverb->m_nRoomLpfFbk);
  1503. // calculate filtered out and simultaneously update LPF state variable
  1504. // filtered output is stored in m_zOutLpfL
  1505. pReverb->m_zOutLpfL = (short) SATURATE(nTemp1 + nTemp2);
  1506. //sum with output buffer
  1507. tempValue = *pOutputBuffer;
  1508. *pOutputBuffer++ = (short) SATURATE(tempValue+pReverb->m_zOutLpfL);
  1509. // ********** end early reflection generator, left
  1510. // ********** start early reflection generator, right
  1511. //psEarly = &(pReverb->m_sEarlyR);
  1512. for (j = 0; j < REVERB_MAX_NUM_REFLECTIONS; j++) {
  1513. // fetch delay line out
  1514. nAddr
  1515. = CIRCULAR(nBase, pReverb->m_sEarlyR.m_zDelay[j], pReverb->m_nBufferMask);
  1516. nTemp1 = pReverb->m_nDelayLine[nAddr];
  1517. // calculate reflection
  1518. nTemp1 = MULT_EG1_EG1(nTemp1, pReverb->m_sEarlyR.m_nGain[j]);
  1519. nDelayOut1 = SATURATE(nDelayOut1 + nTemp1);
  1520. } // end for (j=0; j < REVERB_MAX_NUM_REFLECTIONS; j++)
  1521. // apply lowpass to early reflections
  1522. nTemp1 = MULT_EG1_EG1(nDelayOut1, pReverb->m_nRoomLpfFwd);
  1523. nTemp2 = MULT_EG1_EG1(pReverb->m_zOutLpfR, pReverb->m_nRoomLpfFbk);
  1524. // calculate filtered out and simultaneously update LPF state variable
  1525. // filtered output is stored in m_zOutLpfR
  1526. pReverb->m_zOutLpfR = (short) SATURATE(nTemp1 + nTemp2);
  1527. //sum with output buffer
  1528. tempValue = *pOutputBuffer;
  1529. *pOutputBuffer++ = (short) SATURATE(tempValue + pReverb->m_zOutLpfR);
  1530. // ********** end early reflection generator, right
  1531. // decrement base addr for next sample period
  1532. nBase--;
  1533. pReverb->m_nSin += pReverb->m_nSinIncrement;
  1534. pReverb->m_nCos += pReverb->m_nCosIncrement;
  1535. } // end for (i=0; i < nNumSamplesToAdd; i++)
  1536. // store the most up to date version
  1537. pReverb->m_nBaseIndex = nBase;
  1538. return 0;
  1539. } /* end Reverb */
  1540. /*----------------------------------------------------------------------------
  1541. * ReverbUpdateRoom
  1542. *----------------------------------------------------------------------------
  1543. * Purpose:
  1544. * Update the room's preset parameters as required
  1545. *
  1546. * Inputs:
  1547. *
  1548. * Outputs:
  1549. *
  1550. *
  1551. * Side Effects:
  1552. * - reverb paramters (fbk, fwd, etc) will be changed
  1553. * - m_nCurrentRoom := m_nNextRoom
  1554. *----------------------------------------------------------------------------
  1555. */
  1556. static int ReverbUpdateRoom(reverb_object_t *pReverb, bool fullUpdate) {
  1557. int temp;
  1558. int i;
  1559. int maxSamples;
  1560. int earlyDelay;
  1561. int earlyGain;
  1562. reverb_preset_t *pPreset =
  1563. &pReverb->m_sPreset.m_sPreset[pReverb->m_nNextRoom];
  1564. if (fullUpdate) {
  1565. pReverb->m_nRvbLpfFwd = pPreset->m_nRvbLpfFwd;
  1566. pReverb->m_nRvbLpfFbk = pPreset->m_nRvbLpfFbk;
  1567. pReverb->m_nEarlyGain = pPreset->m_nEarlyGain;
  1568. //stored as time based, convert to sample based
  1569. pReverb->m_nLateGain = pPreset->m_nLateGain;
  1570. pReverb->m_nRoomLpfFbk = pPreset->m_nRoomLpfFbk;
  1571. pReverb->m_nRoomLpfFwd = pPreset->m_nRoomLpfFwd;
  1572. // set the early reflections gains
  1573. earlyGain = pPreset->m_nEarlyGain;
  1574. for (i = 0; i < REVERB_MAX_NUM_REFLECTIONS; i++) {
  1575. pReverb->m_sEarlyL.m_nGain[i]
  1576. = MULT_EG1_EG1(pPreset->m_sEarlyL.m_nGain[i],earlyGain);
  1577. pReverb->m_sEarlyR.m_nGain[i]
  1578. = MULT_EG1_EG1(pPreset->m_sEarlyR.m_nGain[i],earlyGain);
  1579. }
  1580. pReverb->m_nMaxExcursion = pPreset->m_nMaxExcursion;
  1581. pReverb->m_sAp0.m_nApGain = pPreset->m_nAp0_ApGain;
  1582. pReverb->m_sAp1.m_nApGain = pPreset->m_nAp1_ApGain;
  1583. // set the early reflections delay
  1584. earlyDelay = ((int) pPreset->m_nEarlyDelay * pReverb->m_nSamplingRate)
  1585. >> 16;
  1586. pReverb->m_nEarlyDelay = earlyDelay;
  1587. maxSamples = (int32_t) (MAX_EARLY_TIME * pReverb->m_nSamplingRate)
  1588. >> 16;
  1589. for (i = 0; i < REVERB_MAX_NUM_REFLECTIONS; i++) {
  1590. //stored as time based, convert to sample based
  1591. temp = earlyDelay + (((int) pPreset->m_sEarlyL.m_zDelay[i]
  1592. * pReverb->m_nSamplingRate) >> 16);
  1593. if (temp > maxSamples)
  1594. temp = maxSamples;
  1595. pReverb->m_sEarlyL.m_zDelay[i] = pReverb->m_nEarly0in + temp;
  1596. //stored as time based, convert to sample based
  1597. temp = earlyDelay + (((int) pPreset->m_sEarlyR.m_zDelay[i]
  1598. * pReverb->m_nSamplingRate) >> 16);
  1599. if (temp > maxSamples)
  1600. temp = maxSamples;
  1601. pReverb->m_sEarlyR.m_zDelay[i] = pReverb->m_nEarly1in + temp;
  1602. }
  1603. maxSamples = (int32_t) (MAX_DELAY_TIME * pReverb->m_nSamplingRate)
  1604. >> 16;
  1605. //stored as time based, convert to sample based
  1606. /*lint -e{702} shift for performance */
  1607. temp = (pPreset->m_nLateDelay * pReverb->m_nSamplingRate) >> 16;
  1608. if ((temp + pReverb->m_nMaxExcursion) > maxSamples) {
  1609. temp = maxSamples - pReverb->m_nMaxExcursion;
  1610. }
  1611. temp -= pReverb->m_nLateDelay;
  1612. pReverb->m_nDelay0Out += temp;
  1613. pReverb->m_nDelay1Out += temp;
  1614. pReverb->m_nLateDelay += temp;
  1615. maxSamples = (int32_t) (MAX_AP_TIME * pReverb->m_nSamplingRate) >> 16;
  1616. //stored as time based, convert to absolute sample value
  1617. temp = pPreset->m_nAp0_ApOut;
  1618. /*lint -e{702} shift for performance */
  1619. temp = (temp * pReverb->m_nSamplingRate) >> 16;
  1620. if (temp > maxSamples)
  1621. temp = maxSamples;
  1622. pReverb->m_sAp0.m_zApOut = (uint16_t) (pReverb->m_sAp0.m_zApIn + temp);
  1623. //stored as time based, convert to absolute sample value
  1624. temp = pPreset->m_nAp1_ApOut;
  1625. /*lint -e{702} shift for performance */
  1626. temp = (temp * pReverb->m_nSamplingRate) >> 16;
  1627. if (temp > maxSamples)
  1628. temp = maxSamples;
  1629. pReverb->m_sAp1.m_zApOut = (uint16_t) (pReverb->m_sAp1.m_zApIn + temp);
  1630. //gpsReverbObject->m_sAp1.m_zApOut = pPreset->m_nAp1_ApOut;
  1631. }
  1632. //stored as time based, convert to sample based
  1633. temp = pPreset->m_nXfadeInterval;
  1634. /*lint -e{702} shift for performance */
  1635. temp = (temp * pReverb->m_nSamplingRate) >> 16;
  1636. pReverb->m_nXfadeInterval = (uint16_t) temp;
  1637. //gsReverbObject.m_nXfadeInterval = pPreset->m_nXfadeInterval;
  1638. pReverb->m_nXfadeCounter = pReverb->m_nXfadeInterval + 1; // force update on first iteration
  1639. pReverb->m_nCurrentRoom = pReverb->m_nNextRoom;
  1640. return 0;
  1641. } /* end ReverbUpdateRoom */
  1642. /*----------------------------------------------------------------------------
  1643. * ReverbReadInPresets()
  1644. *----------------------------------------------------------------------------
  1645. * Purpose: sets global reverb preset bank to defaults
  1646. *
  1647. * Inputs:
  1648. *
  1649. * Outputs:
  1650. *
  1651. *----------------------------------------------------------------------------
  1652. */
  1653. static int ReverbReadInPresets(reverb_object_t *pReverb) {
  1654. int preset;
  1655. // this is for test only. OpenSL ES presets are mapped to 4 presets.
  1656. // REVERB_PRESET_NONE is mapped to bypass
  1657. for (preset = 0; preset < REVERB_NUM_PRESETS; preset++) {
  1658. reverb_preset_t *pPreset = &pReverb->m_sPreset.m_sPreset[preset];
  1659. switch (preset + 1) {
  1660. case REVERB_PRESET_PLATE:
  1661. case REVERB_PRESET_SMALLROOM:
  1662. pPreset->m_nRvbLpfFbk = 5077;
  1663. pPreset->m_nRvbLpfFwd = 11076;
  1664. pPreset->m_nEarlyGain = 27690;
  1665. pPreset->m_nEarlyDelay = 1311;
  1666. pPreset->m_nLateGain = 8191;
  1667. pPreset->m_nLateDelay = 3932;
  1668. pPreset->m_nRoomLpfFbk = 3692;
  1669. pPreset->m_nRoomLpfFwd = 20474;
  1670. pPreset->m_sEarlyL.m_zDelay[0] = 1376;
  1671. pPreset->m_sEarlyL.m_nGain[0] = 22152;
  1672. pPreset->m_sEarlyL.m_zDelay[1] = 1462;
  1673. pPreset->m_sEarlyL.m_nGain[1] = 17537;
  1674. pPreset->m_sEarlyL.m_zDelay[2] = 0;
  1675. pPreset->m_sEarlyL.m_nGain[2] = 14768;
  1676. pPreset->m_sEarlyL.m_zDelay[3] = 1835;
  1677. pPreset->m_sEarlyL.m_nGain[3] = 14307;
  1678. pPreset->m_sEarlyL.m_zDelay[4] = 0;
  1679. pPreset->m_sEarlyL.m_nGain[4] = 13384;
  1680. pPreset->m_sEarlyR.m_zDelay[0] = 721;
  1681. pPreset->m_sEarlyR.m_nGain[0] = 20306;
  1682. pPreset->m_sEarlyR.m_zDelay[1] = 2621;
  1683. pPreset->m_sEarlyR.m_nGain[1] = 17537;
  1684. pPreset->m_sEarlyR.m_zDelay[2] = 0;
  1685. pPreset->m_sEarlyR.m_nGain[2] = 14768;
  1686. pPreset->m_sEarlyR.m_zDelay[3] = 0;
  1687. pPreset->m_sEarlyR.m_nGain[3] = 16153;
  1688. pPreset->m_sEarlyR.m_zDelay[4] = 0;
  1689. pPreset->m_sEarlyR.m_nGain[4] = 13384;
  1690. pPreset->m_nMaxExcursion = 127;
  1691. pPreset->m_nXfadeInterval = 6470; //6483;
  1692. pPreset->m_nAp0_ApGain = 14768;
  1693. pPreset->m_nAp0_ApOut = 792;
  1694. pPreset->m_nAp1_ApGain = 14777;
  1695. pPreset->m_nAp1_ApOut = 1191;
  1696. pPreset->m_rfu4 = 0;
  1697. pPreset->m_rfu5 = 0;
  1698. pPreset->m_rfu6 = 0;
  1699. pPreset->m_rfu7 = 0;
  1700. pPreset->m_rfu8 = 0;
  1701. pPreset->m_rfu9 = 0;
  1702. pPreset->m_rfu10 = 0;
  1703. break;
  1704. case REVERB_PRESET_MEDIUMROOM:
  1705. case REVERB_PRESET_LARGEROOM:
  1706. pPreset->m_nRvbLpfFbk = 5077;
  1707. pPreset->m_nRvbLpfFwd = 12922;
  1708. pPreset->m_nEarlyGain = 27690;
  1709. pPreset->m_nEarlyDelay = 1311;
  1710. pPreset->m_nLateGain = 8191;
  1711. pPreset->m_nLateDelay = 3932;
  1712. pPreset->m_nRoomLpfFbk = 3692;
  1713. pPreset->m_nRoomLpfFwd = 21703;
  1714. pPreset->m_sEarlyL.m_zDelay[0] = 1376;
  1715. pPreset->m_sEarlyL.m_nGain[0] = 22152;
  1716. pPreset->m_sEarlyL.m_zDelay[1] = 1462;
  1717. pPreset->m_sEarlyL.m_nGain[1] = 17537;
  1718. pPreset->m_sEarlyL.m_zDelay[2] = 0;
  1719. pPreset->m_sEarlyL.m_nGain[2] = 14768;
  1720. pPreset->m_sEarlyL.m_zDelay[3] = 1835;
  1721. pPreset->m_sEarlyL.m_nGain[3] = 14307;
  1722. pPreset->m_sEarlyL.m_zDelay[4] = 0;
  1723. pPreset->m_sEarlyL.m_nGain[4] = 13384;
  1724. pPreset->m_sEarlyR.m_zDelay[0] = 721;
  1725. pPreset->m_sEarlyR.m_nGain[0] = 20306;
  1726. pPreset->m_sEarlyR.m_zDelay[1] = 2621;
  1727. pPreset->m_sEarlyR.m_nGain[1] = 17537;
  1728. pPreset->m_sEarlyR.m_zDelay[2] = 0;
  1729. pPreset->m_sEarlyR.m_nGain[2] = 14768;
  1730. pPreset->m_sEarlyR.m_zDelay[3] = 0;
  1731. pPreset->m_sEarlyR.m_nGain[3] = 16153;
  1732. pPreset->m_sEarlyR.m_zDelay[4] = 0;
  1733. pPreset->m_sEarlyR.m_nGain[4] = 13384;
  1734. pPreset->m_nMaxExcursion = 127;
  1735. pPreset->m_nXfadeInterval = 6449;
  1736. pPreset->m_nAp0_ApGain = 15691;
  1737. pPreset->m_nAp0_ApOut = 774;
  1738. pPreset->m_nAp1_ApGain = 16317;
  1739. pPreset->m_nAp1_ApOut = 1155;
  1740. pPreset->m_rfu4 = 0;
  1741. pPreset->m_rfu5 = 0;
  1742. pPreset->m_rfu6 = 0;
  1743. pPreset->m_rfu7 = 0;
  1744. pPreset->m_rfu8 = 0;
  1745. pPreset->m_rfu9 = 0;
  1746. pPreset->m_rfu10 = 0;
  1747. break;
  1748. case REVERB_PRESET_MEDIUMHALL:
  1749. pPreset->m_nRvbLpfFbk = 6461;
  1750. pPreset->m_nRvbLpfFwd = 14307;
  1751. pPreset->m_nEarlyGain = 27690;
  1752. pPreset->m_nEarlyDelay = 1311;
  1753. pPreset->m_nLateGain = 8191;
  1754. pPreset->m_nLateDelay = 3932;
  1755. pPreset->m_nRoomLpfFbk = 3692;
  1756. pPreset->m_nRoomLpfFwd = 24569;
  1757. pPreset->m_sEarlyL.m_zDelay[0] = 1376;
  1758. pPreset->m_sEarlyL.m_nGain[0] = 22152;
  1759. pPreset->m_sEarlyL.m_zDelay[1] = 1462;
  1760. pPreset->m_sEarlyL.m_nGain[1] = 17537;
  1761. pPreset->m_sEarlyL.m_zDelay[2] = 0;
  1762. pPreset->m_sEarlyL.m_nGain[2] = 14768;
  1763. pPreset->m_sEarlyL.m_zDelay[3] = 1835;
  1764. pPreset->m_sEarlyL.m_nGain[3] = 14307;
  1765. pPreset->m_sEarlyL.m_zDelay[4] = 0;
  1766. pPreset->m_sEarlyL.m_nGain[4] = 13384;
  1767. pPreset->m_sEarlyR.m_zDelay[0] = 721;
  1768. pPreset->m_sEarlyR.m_nGain[0] = 20306;
  1769. pPreset->m_sEarlyR.m_zDelay[1] = 2621;
  1770. pPreset->m_sEarlyR.m_nGain[1] = 17537;
  1771. pPreset->m_sEarlyR.m_zDelay[2] = 0;
  1772. pPreset->m_sEarlyR.m_nGain[2] = 14768;
  1773. pPreset->m_sEarlyR.m_zDelay[3] = 0;
  1774. pPreset->m_sEarlyR.m_nGain[3] = 16153;
  1775. pPreset->m_sEarlyR.m_zDelay[4] = 0;
  1776. pPreset->m_sEarlyR.m_nGain[4] = 13384;
  1777. pPreset->m_nMaxExcursion = 127;
  1778. pPreset->m_nXfadeInterval = 6391;
  1779. pPreset->m_nAp0_ApGain = 15230;
  1780. pPreset->m_nAp0_ApOut = 708;
  1781. pPreset->m_nAp1_ApGain = 15547;
  1782. pPreset->m_nAp1_ApOut = 1023;
  1783. pPreset->m_rfu4 = 0;
  1784. pPreset->m_rfu5 = 0;
  1785. pPreset->m_rfu6 = 0;
  1786. pPreset->m_rfu7 = 0;
  1787. pPreset->m_rfu8 = 0;
  1788. pPreset->m_rfu9 = 0;
  1789. pPreset->m_rfu10 = 0;
  1790. break;
  1791. case REVERB_PRESET_LARGEHALL:
  1792. pPreset->m_nRvbLpfFbk = 8307;
  1793. pPreset->m_nRvbLpfFwd = 14768;
  1794. pPreset->m_nEarlyGain = 27690;
  1795. pPreset->m_nEarlyDelay = 1311;
  1796. pPreset->m_nLateGain = 8191;
  1797. pPreset->m_nLateDelay = 3932;
  1798. pPreset->m_nRoomLpfFbk = 3692;
  1799. pPreset->m_nRoomLpfFwd = 24569;
  1800. pPreset->m_sEarlyL.m_zDelay[0] = 1376;
  1801. pPreset->m_sEarlyL.m_nGain[0] = 22152;
  1802. pPreset->m_sEarlyL.m_zDelay[1] = 2163;
  1803. pPreset->m_sEarlyL.m_nGain[1] = 17537;
  1804. pPreset->m_sEarlyL.m_zDelay[2] = 0;
  1805. pPreset->m_sEarlyL.m_nGain[2] = 14768;
  1806. pPreset->m_sEarlyL.m_zDelay[3] = 1835;
  1807. pPreset->m_sEarlyL.m_nGain[3] = 14307;
  1808. pPreset->m_sEarlyL.m_zDelay[4] = 0;
  1809. pPreset->m_sEarlyL.m_nGain[4] = 13384;
  1810. pPreset->m_sEarlyR.m_zDelay[0] = 721;
  1811. pPreset->m_sEarlyR.m_nGain[0] = 20306;
  1812. pPreset->m_sEarlyR.m_zDelay[1] = 2621;
  1813. pPreset->m_sEarlyR.m_nGain[1] = 17537;
  1814. pPreset->m_sEarlyR.m_zDelay[2] = 0;
  1815. pPreset->m_sEarlyR.m_nGain[2] = 14768;
  1816. pPreset->m_sEarlyR.m_zDelay[3] = 0;
  1817. pPreset->m_sEarlyR.m_nGain[3] = 16153;
  1818. pPreset->m_sEarlyR.m_zDelay[4] = 0;
  1819. pPreset->m_sEarlyR.m_nGain[4] = 13384;
  1820. pPreset->m_nMaxExcursion = 127;
  1821. pPreset->m_nXfadeInterval = 6388;
  1822. pPreset->m_nAp0_ApGain = 15691;
  1823. pPreset->m_nAp0_ApOut = 711;
  1824. pPreset->m_nAp1_ApGain = 16317;
  1825. pPreset->m_nAp1_ApOut = 1029;
  1826. pPreset->m_rfu4 = 0;
  1827. pPreset->m_rfu5 = 0;
  1828. pPreset->m_rfu6 = 0;
  1829. pPreset->m_rfu7 = 0;
  1830. pPreset->m_rfu8 = 0;
  1831. pPreset->m_rfu9 = 0;
  1832. pPreset->m_rfu10 = 0;
  1833. break;
  1834. }
  1835. }
  1836. return 0;
  1837. }
  1838. audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
  1839. .tag = AUDIO_EFFECT_LIBRARY_TAG,
  1840. .version = EFFECT_LIBRARY_API_VERSION,
  1841. .name = "Test Equalizer Library",
  1842. .implementor = "The Android Open Source Project",
  1843. .create_effect = EffectCreate,
  1844. .release_effect = EffectRelease,
  1845. .get_descriptor = EffectGetDescriptor,
  1846. };