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