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