1135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*
2135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Copyright (C) 2008 The Android Open Source Project
3135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
4135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
5135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * you may not use this file except in compliance with the License.
6135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * You may obtain a copy of the License at
7135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
8135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
9135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
10135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Unless required by applicable law or agreed to in writing, software
11135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
12135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * See the License for the specific language governing permissions and
14135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * limitations under the License.
15135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
16135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
17135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#define LOG_TAG "EffectReverb"
18e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent//#define LOG_NDEBUG 0
19135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#include <cutils/log.h>
20be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent#include <stdlib.h>
21be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent#include <string.h>
22135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#include <stdbool.h>
23135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#include "EffectReverb.h"
24135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#include "EffectsMath.h"
25135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
26e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent// effect_handle_t interface implementation for reverb effect
27135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentconst struct effect_interface_s gReverbInterface = {
28135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        Reverb_Process,
29e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        Reverb_Command,
30ba7b8f881a9b6b21803752326d2932a3bd42d7cfEric Laurent        Reverb_GetDescriptor,
31ba7b8f881a9b6b21803752326d2932a3bd42d7cfEric Laurent        NULL
32135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent};
33135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
34135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Google auxiliary environmental reverb UUID: 1f0ae2e0-4ef7-11df-bc09-0002a5d5c51b
35135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic const effect_descriptor_t gAuxEnvReverbDescriptor = {
36135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        {0xc2e5d5f0, 0x94bd, 0x4763, 0x9cac, {0x4e, 0x23, 0x4d, 0x06, 0x83, 0x9e}},
37135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        {0x1f0ae2e0, 0x4ef7, 0x11df, 0xbc09, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
38e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        EFFECT_CONTROL_API_VERSION,
39ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        // flags other than EFFECT_FLAG_TYPE_AUXILIARY set for test purpose
40ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        EFFECT_FLAG_TYPE_AUXILIARY | EFFECT_FLAG_DEVICE_IND | EFFECT_FLAG_AUDIO_MODE_IND,
41ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        0, // TODO
42ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        33,
43135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        "Aux Environmental Reverb",
44e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        "The Android Open Source Project"
45135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent};
46135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
47135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Google insert environmental reverb UUID: aa476040-6342-11df-91a4-0002a5d5c51b
48135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic const effect_descriptor_t gInsertEnvReverbDescriptor = {
49135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        {0xc2e5d5f0, 0x94bd, 0x4763, 0x9cac, {0x4e, 0x23, 0x4d, 0x06, 0x83, 0x9e}},
50135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        {0xaa476040, 0x6342, 0x11df, 0x91a4, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
51e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        EFFECT_CONTROL_API_VERSION,
52135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST,
53ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        0, // TODO
54ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        33,
55135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        "Insert Environmental reverb",
56e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        "The Android Open Source Project"
57135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent};
58135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
59135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Google auxiliary preset reverb UUID: 63909320-53a6-11df-bdbd-0002a5d5c51b
60135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic const effect_descriptor_t gAuxPresetReverbDescriptor = {
61cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        {0x47382d60, 0xddd8, 0x11db, 0xbf3a, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
62135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        {0x63909320, 0x53a6, 0x11df, 0xbdbd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
63e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        EFFECT_CONTROL_API_VERSION,
64135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        EFFECT_FLAG_TYPE_AUXILIARY,
65ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        0, // TODO
66ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        33,
67135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        "Aux Preset Reverb",
68e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        "The Android Open Source Project"
69135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent};
70135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
71135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Google insert preset reverb UUID: d93dc6a0-6342-11df-b128-0002a5d5c51b
72135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic const effect_descriptor_t gInsertPresetReverbDescriptor = {
73cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        {0x47382d60, 0xddd8, 0x11db, 0xbf3a, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
74135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        {0xd93dc6a0, 0x6342, 0x11df, 0xb128, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
75e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        EFFECT_CONTROL_API_VERSION,
76135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST,
77ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        0, // TODO
78ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        33,
79135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        "Insert Preset Reverb",
80e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        "The Android Open Source Project"
81135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent};
82135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
83135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// gDescriptors contains pointers to all defined effect descriptor in this library
84135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic const effect_descriptor_t * const gDescriptors[] = {
85135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        &gAuxEnvReverbDescriptor,
86135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        &gInsertEnvReverbDescriptor,
87135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        &gAuxPresetReverbDescriptor,
88ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        &gInsertPresetReverbDescriptor
89135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent};
90135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
91135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
92135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Effect API implementation
93135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *--------------------------------------------------------------------------*/
94135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
95135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*--- Effect Library Interface Implementation ---*/
96135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
975e92a7861196ddae14638d4b7a63fc4892b7ef59Glenn Kastenint EffectCreate(const effect_uuid_t *uuid,
98ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        int32_t sessionId,
99ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        int32_t ioId,
100e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        effect_handle_t *pHandle) {
101135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int ret;
102135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int i;
103135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    reverb_module_t *module;
104135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    const effect_descriptor_t *desc;
105135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int aux = 0;
106135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int preset = 0;
107135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1083856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("EffectLibCreateEffect start");
109135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
110e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    if (pHandle == NULL || uuid == NULL) {
111135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return -EINVAL;
112135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
113135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
114135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    for (i = 0; gDescriptors[i] != NULL; i++) {
115135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        desc = gDescriptors[i];
116135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        if (memcmp(uuid, &desc->uuid, sizeof(effect_uuid_t))
117135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                == 0) {
118135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            break;
119135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        }
120135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
121135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
122135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (gDescriptors[i] == NULL) {
123135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return -ENOENT;
124135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
125135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
126135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    module = malloc(sizeof(reverb_module_t));
127135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
128135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    module->itfe = &gReverbInterface;
129135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
130e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent    module->context.mState = REVERB_STATE_UNINITIALIZED;
131e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent
132135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (memcmp(&desc->type, SL_IID_PRESETREVERB, sizeof(effect_uuid_t)) == 0) {
133135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        preset = 1;
134135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
135135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
136135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        aux = 1;
137135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
138135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    ret = Reverb_Init(module, aux, preset);
139135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (ret < 0) {
1405ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("EffectLibCreateEffect() init failed");
141135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        free(module);
142135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return ret;
143135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
144135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
145e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    *pHandle = (effect_handle_t) module;
146135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
147e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent    module->context.mState = REVERB_STATE_INITIALIZED;
148e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent
1493856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("EffectLibCreateEffect %p ,size %d", module, sizeof(reverb_module_t));
150135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
151135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return 0;
152135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
153135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
154e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurentint EffectRelease(effect_handle_t handle) {
155e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    reverb_module_t *pRvbModule = (reverb_module_t *)handle;
156135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1573856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("EffectLibReleaseEffect %p", handle);
158e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    if (handle == NULL) {
159135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return -EINVAL;
160135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
161135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
162e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent    pRvbModule->context.mState = REVERB_STATE_UNINITIALIZED;
163e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent
164135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    free(pRvbModule);
165135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return 0;
166135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
167135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1685e92a7861196ddae14638d4b7a63fc4892b7ef59Glenn Kastenint EffectGetDescriptor(const effect_uuid_t *uuid,
169e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent                        effect_descriptor_t *pDescriptor) {
170e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    int i;
171e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    int length = sizeof(gDescriptors) / sizeof(const effect_descriptor_t *);
172e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent
173e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    if (pDescriptor == NULL || uuid == NULL){
1743856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("EffectGetDescriptor() called with NULL pointer");
175e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        return -EINVAL;
176e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    }
177e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent
178e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    for (i = 0; i < length; i++) {
179e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        if (memcmp(uuid, &gDescriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
180a189a6883ee55cf62da1d7bf5bf5a8ab501938a4Glenn Kasten            *pDescriptor = *gDescriptors[i];
1813856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("EffectGetDescriptor - UUID matched Reverb type %d, UUID = %x",
182e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent                 i, gDescriptors[i]->uuid.timeLow);
183e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent            return 0;
184e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        }
185e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    }
186e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent
187e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    return -EINVAL;
188e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent} /* end EffectGetDescriptor */
189135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
190135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*--- Effect Control Interface Implementation ---*/
191135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
192e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurentstatic int Reverb_Process(effect_handle_t self, audio_buffer_t *inBuffer, audio_buffer_t *outBuffer) {
193135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    reverb_object_t *pReverb;
194135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int16_t *pSrc, *pDst;
195135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    reverb_module_t *pRvbModule = (reverb_module_t *)self;
196135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
197135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (pRvbModule == NULL) {
198135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return -EINVAL;
199135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
200135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
201135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (inBuffer == NULL || inBuffer->raw == NULL ||
202135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        outBuffer == NULL || outBuffer->raw == NULL ||
203135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        inBuffer->frameCount != outBuffer->frameCount) {
204135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return -EINVAL;
205135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
206135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
207135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb = (reverb_object_t*) &pRvbModule->context;
208135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
209e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent    if (pReverb->mState == REVERB_STATE_UNINITIALIZED) {
210e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        return -EINVAL;
211e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent    }
212e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent    if (pReverb->mState == REVERB_STATE_INITIALIZED) {
213e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        return -ENODATA;
214e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent    }
215e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent
216135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //if bypassed or the preset forces the signal to be completely dry
217cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent    if (pReverb->m_bBypass != 0) {
218ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        if (inBuffer->raw != outBuffer->raw) {
219ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            int16_t smp;
220ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            pSrc = inBuffer->s16;
221ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            pDst = outBuffer->s16;
222ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            size_t count = inBuffer->frameCount;
223ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            if (pRvbModule->config.inputCfg.channels == pRvbModule->config.outputCfg.channels) {
224ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent                count *= 2;
225ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent                while (count--) {
226ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent                    *pDst++ = *pSrc++;
227ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent                }
228ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            } else {
229ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent                while (count--) {
230ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent                    smp = *pSrc++;
231ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent                    *pDst++ = smp;
232ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent                    *pDst++ = smp;
233ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent                }
234ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            }
235135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        }
236135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return 0;
237135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
238135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
239135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (pReverb->m_nNextRoom != pReverb->m_nCurrentRoom) {
240135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        ReverbUpdateRoom(pReverb, true);
241135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
242135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
243135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pSrc = inBuffer->s16;
244135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pDst = outBuffer->s16;
245135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    size_t numSamples = outBuffer->frameCount;
246135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    while (numSamples) {
247135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        uint32_t processedSamples;
248135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        if (numSamples > (uint32_t) pReverb->m_nUpdatePeriodInSamples) {
249135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            processedSamples = (uint32_t) pReverb->m_nUpdatePeriodInSamples;
250135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        } else {
251135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            processedSamples = numSamples;
252135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        }
253135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
254135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        /* increment update counter */
255135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nUpdateCounter += (int16_t) processedSamples;
256135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        /* check if update counter needs to be reset */
257135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        if (pReverb->m_nUpdateCounter >= pReverb->m_nUpdatePeriodInSamples) {
258135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            /* update interval has elapsed, so reset counter */
259135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_nUpdateCounter -= pReverb->m_nUpdatePeriodInSamples;
260135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            ReverbUpdateXfade(pReverb, pReverb->m_nUpdatePeriodInSamples);
261135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
262135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        } /* end if m_nUpdateCounter >= update interval */
263135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
264135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        Reverb(pReverb, processedSamples, pDst, pSrc);
265135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
266135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        numSamples -= processedSamples;
267135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        if (pReverb->m_Aux) {
268ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            pSrc += processedSamples;
269135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        } else {
270135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pSrc += processedSamples * NUM_OUTPUT_CHANNELS;
271135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        }
272ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        pDst += processedSamples * NUM_OUTPUT_CHANNELS;
273135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
274135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
275135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return 0;
276135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
277135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
278e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent
279e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurentstatic int Reverb_Command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
28025f4395b932fa9859a6e91ba77c5d20d009da64aEric Laurent        void *pCmdData, uint32_t *replySize, void *pReplyData) {
281135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    reverb_module_t *pRvbModule = (reverb_module_t *) self;
282135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    reverb_object_t *pReverb;
283135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int retsize;
284135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
285e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent    if (pRvbModule == NULL ||
286e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent            pRvbModule->context.mState == REVERB_STATE_UNINITIALIZED) {
287135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return -EINVAL;
288135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
289135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
290135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb = (reverb_object_t*) &pRvbModule->context;
291135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
2923856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Reverb_Command command %d cmdSize %d",cmdCode, cmdSize);
293135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
294135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    switch (cmdCode) {
295135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    case EFFECT_CMD_INIT:
296135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        if (pReplyData == NULL || *replySize != sizeof(int)) {
297135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            return -EINVAL;
298135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        }
299135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        *(int *) pReplyData = Reverb_Init(pRvbModule, pReverb->m_Aux, pReverb->m_Preset);
300e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        if (*(int *) pReplyData == 0) {
301e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent            pRvbModule->context.mState = REVERB_STATE_INITIALIZED;
302e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        }
303135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
3043d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent    case EFFECT_CMD_SET_CONFIG:
305135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
306135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                || pReplyData == NULL || *replySize != sizeof(int)) {
307135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            return -EINVAL;
308135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        }
3093d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent        *(int *) pReplyData = Reverb_setConfig(pRvbModule,
310135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                (effect_config_t *)pCmdData, false);
311135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
3123d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent    case EFFECT_CMD_GET_CONFIG:
3133d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent        if (pReplyData == NULL || *replySize != sizeof(effect_config_t)) {
3143d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent            return -EINVAL;
3153d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent        }
3163d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent        Reverb_getConfig(pRvbModule, (effect_config_t *) pCmdData);
3173d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent        break;
318135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    case EFFECT_CMD_RESET:
319135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        Reverb_Reset(pReverb, false);
320135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
321135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    case EFFECT_CMD_GET_PARAM:
3223856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("Reverb_Command EFFECT_CMD_GET_PARAM pCmdData %p, *replySize %d, pReplyData: %p",pCmdData, *replySize, pReplyData);
323135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
324135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
325135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReplyData == NULL || *replySize < (int) sizeof(effect_param_t)) {
326135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            return -EINVAL;
327135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        }
328135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        effect_param_t *rep = (effect_param_t *) pReplyData;
329135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(int32_t));
3303856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("Reverb_Command EFFECT_CMD_GET_PARAM param %d, replySize %d",*(int32_t *)rep->data, rep->vsize);
331135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        rep->status = Reverb_getParameter(pReverb, *(int32_t *)rep->data, &rep->vsize,
332135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                rep->data + sizeof(int32_t));
333135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        *replySize = sizeof(effect_param_t) + sizeof(int32_t) + rep->vsize;
334135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
335135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    case EFFECT_CMD_SET_PARAM:
3363856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("Reverb_Command EFFECT_CMD_SET_PARAM cmdSize %d pCmdData %p, *replySize %d, pReplyData %p",
337135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                cmdSize, pCmdData, *replySize, pReplyData);
338135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        if (pCmdData == NULL || (cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)))
339135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                || pReplyData == NULL || *replySize != (int)sizeof(int32_t)) {
340135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            return -EINVAL;
341135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        }
342135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        effect_param_t *cmd = (effect_param_t *) pCmdData;
343135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        *(int *)pReplyData = Reverb_setParameter(pReverb, *(int32_t *)cmd->data,
344135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                cmd->vsize, cmd->data + sizeof(int32_t));
345135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
346ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent    case EFFECT_CMD_ENABLE:
347e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        if (pReplyData == NULL || *replySize != sizeof(int)) {
348e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent            return -EINVAL;
349e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        }
350e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        if (pReverb->mState != REVERB_STATE_INITIALIZED) {
351e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent            return -ENOSYS;
352e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        }
353e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        pReverb->mState = REVERB_STATE_ACTIVE;
3543856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("EFFECT_CMD_ENABLE() OK");
355e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        *(int *)pReplyData = 0;
356e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        break;
357ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent    case EFFECT_CMD_DISABLE:
358ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        if (pReplyData == NULL || *replySize != sizeof(int)) {
359ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            return -EINVAL;
360ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        }
361e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        if (pReverb->mState != REVERB_STATE_ACTIVE) {
362e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent            return -ENOSYS;
363e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        }
364e44b1efb293f878d4af8e824a6b3d070167bf2d6Eric Laurent        pReverb->mState = REVERB_STATE_INITIALIZED;
3653856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("EFFECT_CMD_DISABLE() OK");
366ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        *(int *)pReplyData = 0;
367ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        break;
368ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent    case EFFECT_CMD_SET_DEVICE:
369ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
370ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            return -EINVAL;
371ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        }
3723856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("Reverb_Command EFFECT_CMD_SET_DEVICE: 0x%08x", *(uint32_t *)pCmdData);
373ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        break;
374ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent    case EFFECT_CMD_SET_VOLUME: {
375ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        // audio output is always stereo => 2 channel volumes
376ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t) * 2) {
377ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            return -EINVAL;
378ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        }
379ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        float left = (float)(*(uint32_t *)pCmdData) / (1 << 24);
380ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        float right = (float)(*((uint32_t *)pCmdData + 1)) / (1 << 24);
3813856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("Reverb_Command EFFECT_CMD_SET_VOLUME: left %f, right %f ", left, right);
382ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        break;
383ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        }
384ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent    case EFFECT_CMD_SET_AUDIO_MODE:
385ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
386ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent            return -EINVAL;
387ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        }
3883856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("Reverb_Command EFFECT_CMD_SET_AUDIO_MODE: %d", *(uint32_t *)pCmdData);
389ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent        break;
390135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    default:
3915ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("Reverb_Command invalid command %d",cmdCode);
392135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return -EINVAL;
393135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
394135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
395135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return 0;
396135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
397135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
398e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurentint Reverb_GetDescriptor(effect_handle_t   self,
399e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent                                    effect_descriptor_t *pDescriptor)
400e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent{
401e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    reverb_module_t *pRvbModule = (reverb_module_t *) self;
402e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    reverb_object_t *pReverb;
403e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    const effect_descriptor_t *desc;
404e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent
405e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    if (pRvbModule == NULL ||
406e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent            pRvbModule->context.mState == REVERB_STATE_UNINITIALIZED) {
407e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        return -EINVAL;
408e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    }
409e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent
410e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    pReverb = (reverb_object_t*) &pRvbModule->context;
411e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent
412e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    if (pReverb->m_Aux) {
413e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        if (pReverb->m_Preset) {
414e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent            desc = &gAuxPresetReverbDescriptor;
415e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        } else {
416e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent            desc = &gAuxEnvReverbDescriptor;
417e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        }
418e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    } else {
419e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        if (pReverb->m_Preset) {
420e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent            desc = &gInsertPresetReverbDescriptor;
421e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        } else {
422e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent            desc = &gInsertEnvReverbDescriptor;
423e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        }
424e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    }
425e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent
426a189a6883ee55cf62da1d7bf5bf5a8ab501938a4Glenn Kasten    *pDescriptor = *desc;
427e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent
428e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    return 0;
429e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent}   /* end Reverb_getDescriptor */
430135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
431135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
432135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Reverb internal functions
433135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *--------------------------------------------------------------------------*/
434135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
435135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
436135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Reverb_Init()
437135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
438135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Purpose:
439135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Initialize reverb context and apply default parameters
440135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
441135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Inputs:
442135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  pRvbModule    - pointer to reverb effect module
443135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  aux           - indicates if the reverb is used as auxiliary (1) or insert (0)
444135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  preset        - indicates if the reverb is used in preset (1) or environmental (0) mode
445135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
446135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Outputs:
447135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
448135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Side Effects:
449135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
450135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
451135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
452135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
453135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentint Reverb_Init(reverb_module_t *pRvbModule, int aux, int preset) {
454135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int ret;
455135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
4563856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Reverb_Init module %p, aux: %d, preset: %d", pRvbModule,aux, preset);
457135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
458135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    memset(&pRvbModule->context, 0, sizeof(reverb_object_t));
459135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
460135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->context.m_Aux = (uint16_t)aux;
461135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->context.m_Preset = (uint16_t)preset;
462135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
463135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.inputCfg.samplingRate = 44100;
464135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (aux) {
465e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        pRvbModule->config.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
466135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    } else {
467e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        pRvbModule->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
468135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
469e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    pRvbModule->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
470135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.inputCfg.bufferProvider.getBuffer = NULL;
471135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.inputCfg.bufferProvider.releaseBuffer = NULL;
472135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.inputCfg.bufferProvider.cookie = NULL;
473135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
474135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.inputCfg.mask = EFFECT_CONFIG_ALL;
475135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.outputCfg.samplingRate = 44100;
476e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    pRvbModule->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
477e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    pRvbModule->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
478135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.outputCfg.bufferProvider.getBuffer = NULL;
479135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.outputCfg.bufferProvider.releaseBuffer = NULL;
480135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.outputCfg.bufferProvider.cookie = NULL;
481135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
482135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pRvbModule->config.outputCfg.mask = EFFECT_CONFIG_ALL;
483135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
4843d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent    ret = Reverb_setConfig(pRvbModule, &pRvbModule->config, true);
485135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (ret < 0) {
4863856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("Reverb_Init error %d on module %p", ret, pRvbModule);
487135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
488135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
489135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return ret;
490135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
491135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
492135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
4933d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent * Reverb_setConfig()
494135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
495135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Purpose:
496135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  Set input and output audio configuration.
497135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
498135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Inputs:
499135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  pRvbModule    - pointer to reverb effect module
500135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  pConfig       - pointer to effect_config_t structure containing input
501135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *              and output audio parameters configuration
502135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  init          - true if called from init function
503135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Outputs:
504135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
505135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Side Effects:
506135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
507135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
508135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
509135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
5103d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurentint Reverb_setConfig(reverb_module_t *pRvbModule, effect_config_t *pConfig,
511135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        bool init) {
512135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    reverb_object_t *pReverb = &pRvbModule->context;
513135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int bufferSizeInSamples;
514135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int updatePeriodInSamples;
515135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int xfadePeriodInSamples;
516135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
517135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Check configuration compatibility with build options
518135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (pConfig->inputCfg.samplingRate
519135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        != pConfig->outputCfg.samplingRate
520135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        || pConfig->outputCfg.channels != OUTPUT_CHANNELS
521e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        || pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT
522e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        || pConfig->outputCfg.format != AUDIO_FORMAT_PCM_16_BIT) {
5233d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent        ALOGV("Reverb_setConfig invalid config");
524135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return -EINVAL;
525135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
526e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent    if ((pReverb->m_Aux && (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_MONO)) ||
527e1315cf0b63b4c14a77046519e6b01f6f60d74b0Eric Laurent        (!pReverb->m_Aux && (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO))) {
5283d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent        ALOGV("Reverb_setConfig invalid config");
529135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return -EINVAL;
530135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
531135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
532a189a6883ee55cf62da1d7bf5bf5a8ab501938a4Glenn Kasten    pRvbModule->config = *pConfig;
533135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
534135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nSamplingRate = pRvbModule->config.outputCfg.samplingRate;
535135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
536135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    switch (pReverb->m_nSamplingRate) {
537135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    case 8000:
538135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nUpdatePeriodInBits = 5;
539135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        bufferSizeInSamples = 4096;
540135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nCosWT_5KHz = -23170;
541135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
542135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    case 16000:
543135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nUpdatePeriodInBits = 6;
544135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        bufferSizeInSamples = 8192;
545135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nCosWT_5KHz = -12540;
546135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
547135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    case 22050:
548135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nUpdatePeriodInBits = 7;
549135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        bufferSizeInSamples = 8192;
550135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nCosWT_5KHz = 4768;
551135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
552135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    case 32000:
553135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nUpdatePeriodInBits = 7;
554135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        bufferSizeInSamples = 16384;
555135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nCosWT_5KHz = 18205;
556135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
557135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    case 44100:
558135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nUpdatePeriodInBits = 8;
559135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        bufferSizeInSamples = 16384;
560135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nCosWT_5KHz = 24799;
561135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
562135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    case 48000:
563135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nUpdatePeriodInBits = 8;
564135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        bufferSizeInSamples = 16384;
565135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nCosWT_5KHz = 25997;
566135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        break;
567135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    default:
5683d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent        ALOGV("Reverb_setConfig invalid sampling rate %d", pReverb->m_nSamplingRate);
569135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return -EINVAL;
570135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
571135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
572135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Define a mask for circular addressing, so that array index
573135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // can wraparound and stay in array boundary of 0, 1, ..., (buffer size -1)
574135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // The buffer size MUST be a power of two
575135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nBufferMask = (int32_t) (bufferSizeInSamples - 1);
576135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    /* reverb parameters are updated every 2^(pReverb->m_nUpdatePeriodInBits) samples */
577135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    updatePeriodInSamples = (int32_t) (0x1L << pReverb->m_nUpdatePeriodInBits);
578135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    /*
579135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent     calculate the update counter by bitwise ANDING with this value to
580135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent     generate a 2^n modulo value
581135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent     */
582135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nUpdatePeriodInSamples = (int32_t) updatePeriodInSamples;
583135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
584135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    xfadePeriodInSamples = (int32_t) (REVERB_XFADE_PERIOD_IN_SECONDS
585135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            * (double) pReverb->m_nSamplingRate);
586135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
587135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // set xfade parameters
588135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nPhaseIncrement
589135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            = (int16_t) (65536 / ((int16_t) xfadePeriodInSamples
590135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                    / (int16_t) updatePeriodInSamples));
591135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
592135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (init) {
593135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        ReverbReadInPresets(pReverb);
594135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
595135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // for debugging purposes, allow noise generator
596135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_bUseNoise = true;
597135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
598135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // for debugging purposes, allow bypass
599cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        pReverb->m_bBypass = 0;
600135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
601135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nNextRoom = 1;
602135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
603135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nNoise = (int16_t) 0xABCD;
604135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
605135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
606135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    Reverb_Reset(pReverb, init);
607135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
608135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return 0;
609135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
610135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
611135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
6123d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent * Reverb_getConfig()
6133d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent *----------------------------------------------------------------------------
6143d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent * Purpose:
6153d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent *  Get input and output audio configuration.
6163d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent *
6173d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent * Inputs:
6183d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent *  pRvbModule    - pointer to reverb effect module
6193d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent *  pConfig       - pointer to effect_config_t structure containing input
6203d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent *              and output audio parameters configuration
6213d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent * Outputs:
6223d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent *
6233d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent * Side Effects:
6243d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent *
6253d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent *----------------------------------------------------------------------------
6263d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent */
6273d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent
6283d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurentvoid Reverb_getConfig(reverb_module_t *pRvbModule, effect_config_t *pConfig)
6293d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent{
630a189a6883ee55cf62da1d7bf5bf5a8ab501938a4Glenn Kasten    *pConfig = pRvbModule->config;
6313d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent}
6323d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent
6333d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent/*----------------------------------------------------------------------------
634135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Reverb_Reset()
635135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
636135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Purpose:
637135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  Reset internal states and clear delay lines.
638135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
639135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Inputs:
640135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  pReverb    - pointer to reverb context
641135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  init       - true if called from init function
642135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
643135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Outputs:
644135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
645135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Side Effects:
646135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
647135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
648135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
649135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
650135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentvoid Reverb_Reset(reverb_object_t *pReverb, bool init) {
651135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int bufferSizeInSamples = (int32_t) (pReverb->m_nBufferMask + 1);
652135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int maxApSamples;
653135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int maxDelaySamples;
654135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int maxEarlySamples;
655135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int ap1In;
656135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int delay0In;
657135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int delay1In;
658135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t i;
659135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    uint16_t nOffset;
660135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
661135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    maxApSamples = ((int32_t) (MAX_AP_TIME * pReverb->m_nSamplingRate) >> 16);
662135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    maxDelaySamples = ((int32_t) (MAX_DELAY_TIME * pReverb->m_nSamplingRate)
663135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            >> 16);
664135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    maxEarlySamples = ((int32_t) (MAX_EARLY_TIME * pReverb->m_nSamplingRate)
665135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            >> 16);
666135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
667135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    ap1In = (AP0_IN + maxApSamples + GUARD);
668135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    delay0In = (ap1In + maxApSamples + GUARD);
669135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    delay1In = (delay0In + maxDelaySamples + GUARD);
670135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // Define the max offsets for the end points of each section
671135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // i.e., we don't expect a given section's taps to go beyond
672135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // the following limits
673135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
674135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nEarly0in = (delay1In + maxDelaySamples + GUARD);
675135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nEarly1in = (pReverb->m_nEarly0in + maxEarlySamples + GUARD);
676135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
677135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_sAp0.m_zApIn = AP0_IN;
678135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
679135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_zD0In = delay0In;
680135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
681135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_sAp1.m_zApIn = ap1In;
682135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
683135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_zD1In = delay1In;
684135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
685135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_zOutLpfL = 0;
686135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_zOutLpfR = 0;
687135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
688135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nRevFbkR = 0;
689135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nRevFbkL = 0;
690135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
691135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // set base index into circular buffer
692135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nBaseIndex = 0;
693135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
694135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // clear the reverb delay line
695135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    for (i = 0; i < bufferSizeInSamples; i++) {
696135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nDelayLine[i] = 0;
697135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
698135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
699135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    ReverbUpdateRoom(pReverb, init);
700135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
701135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nUpdateCounter = 0;
702135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
703135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nPhase = -32768;
704135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
705135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nSin = 0;
706135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nCos = 0;
707135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nSinIncrement = 0;
708135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nCosIncrement = 0;
709135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
710135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // set delay tap lengths
711135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    nOffset = ReverbCalculateNoise(pReverb);
712135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
713135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_zD1Cross = pReverb->m_nDelay1Out - pReverb->m_nMaxExcursion
714135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            + nOffset;
715135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
716135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    nOffset = ReverbCalculateNoise(pReverb);
717135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
718135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_zD0Cross = pReverb->m_nDelay0Out - pReverb->m_nMaxExcursion
719135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            - nOffset;
720135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
721135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    nOffset = ReverbCalculateNoise(pReverb);
722135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
723135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_zD0Self = pReverb->m_nDelay0Out - pReverb->m_nMaxExcursion
724135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            - nOffset;
725135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
726135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    nOffset = ReverbCalculateNoise(pReverb);
727135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
728135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_zD1Self = pReverb->m_nDelay1Out - pReverb->m_nMaxExcursion
729135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            + nOffset;
730135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
731135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
732135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
733135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Reverb_getParameter()
734135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
735135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Purpose:
736135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Get a Reverb parameter
737135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
738135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Inputs:
739135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  pReverb       - handle to instance data
740135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  param         - parameter
741135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  pValue        - pointer to variable to hold retrieved value
742135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  pSize         - pointer to value size: maximum size as input
743135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
744135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Outputs:
745135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  *pValue updated with parameter value
746135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  *pSize updated with actual value size
747135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
748135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
749135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Side Effects:
750135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
751135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
752135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
753b302bd5d288be2d3363b80053ca2392560b00b25Ashok Bhatint Reverb_getParameter(reverb_object_t *pReverb, int32_t param, uint32_t *pSize,
754135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        void *pValue) {
755135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t *pValue32;
756135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int16_t *pValue16;
75723e1de74359f4bb1763aef0adfebe073122b032cEric Laurent    t_reverb_settings *pProperties;
758135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t i;
759135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t temp;
760135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t temp2;
761b302bd5d288be2d3363b80053ca2392560b00b25Ashok Bhat    uint32_t size;
762135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
763cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent    if (pReverb->m_Preset) {
764cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        if (param != REVERB_PARAM_PRESET || *pSize < sizeof(int16_t)) {
765cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            return -EINVAL;
766cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
767135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        size = sizeof(int16_t);
768cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        pValue16 = (int16_t *)pValue;
769cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        // REVERB_PRESET_NONE is mapped to bypass
770cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        if (pReverb->m_bBypass != 0) {
771cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue16 = (int16_t)REVERB_PRESET_NONE;
772cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        } else {
773cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue16 = (int16_t)(pReverb->m_nNextRoom + 1);
774cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
7753856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("get REVERB_PARAM_PRESET, preset %d", *pValue16);
776cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent    } else {
777cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        switch (param) {
778cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_ROOM_LEVEL:
779cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_ROOM_HF_LEVEL:
780cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DECAY_HF_RATIO:
781cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REFLECTIONS_LEVEL:
782cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REVERB_LEVEL:
783cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DIFFUSION:
784cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DENSITY:
785cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            size = sizeof(int16_t);
786cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            break;
787135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
788cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_BYPASS:
789cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DECAY_TIME:
790cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REFLECTIONS_DELAY:
791cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REVERB_DELAY:
792cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            size = sizeof(int32_t);
793cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            break;
794135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
795cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_PROPERTIES:
79623e1de74359f4bb1763aef0adfebe073122b032cEric Laurent            size = sizeof(t_reverb_settings);
797cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            break;
798135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
799cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        default:
800cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            return -EINVAL;
801cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
802135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
803cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        if (*pSize < size) {
804cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            return -EINVAL;
805cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
806135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
807cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        pValue32 = (int32_t *) pValue;
808cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        pValue16 = (int16_t *) pValue;
80923e1de74359f4bb1763aef0adfebe073122b032cEric Laurent        pProperties = (t_reverb_settings *) pValue;
810135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
811cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        switch (param) {
812cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_BYPASS:
813cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue32 = (int32_t) pReverb->m_bBypass;
814135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            break;
815135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
816cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_PROPERTIES:
817cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pValue16 = &pProperties->roomLevel;
818cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
819135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
820cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_ROOM_LEVEL:
821cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert m_nRoomLpfFwd to millibels
822cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (pReverb->m_nRoomLpfFwd << 15)
823cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    / (32767 - pReverb->m_nRoomLpfFbk);
824cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue16 = Effects_Linear16ToMillibels(temp);
825135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
8263856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_ROOM_LEVEL %d, gain %d, m_nRoomLpfFwd %d, m_nRoomLpfFbk %d", *pValue16, temp, pReverb->m_nRoomLpfFwd, pReverb->m_nRoomLpfFbk);
827135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
828cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_ROOM_LEVEL) {
829cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
830cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
831cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pValue16 = &pProperties->roomHFLevel;
832cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
833cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
834cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_ROOM_HF_LEVEL:
835cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // The ratio between linear gain at 0Hz and at 5000Hz for the room low pass is:
836cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // (1 + a1) / sqrt(a1^2 + 2*C*a1 + 1) where:
837cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // - a1 is minus the LP feedback gain: -pReverb->m_nRoomLpfFbk
838cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // - C is cos(2piWT) @ 5000Hz: pReverb->m_nCosWT_5KHz
839cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
840cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = MULT_EG1_EG1(pReverb->m_nRoomLpfFbk, pReverb->m_nRoomLpfFbk);
8413856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_ROOM_HF_LEVEL, a1^2 %d", temp);
842cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp2 = MULT_EG1_EG1(pReverb->m_nRoomLpfFbk, pReverb->m_nCosWT_5KHz)
843135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                    << 1;
8443856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_ROOM_HF_LEVEL, 2 Cos a1 %d", temp2);
845135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            temp = 32767 + temp - temp2;
8463856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_ROOM_HF_LEVEL, a1^2 + 2 Cos a1 + 1 %d", temp);
847135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            temp = Effects_Sqrt(temp) * 181;
8483856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_ROOM_HF_LEVEL, SQRT(a1^2 + 2 Cos a1 + 1) %d", temp);
849cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = ((32767 - pReverb->m_nRoomLpfFbk) << 15) / temp;
850cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
8513856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_ROOM_HF_LEVEL, gain %d, m_nRoomLpfFwd %d, m_nRoomLpfFbk %d", temp, pReverb->m_nRoomLpfFwd, pReverb->m_nRoomLpfFbk);
852cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
853cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue16 = Effects_Linear16ToMillibels(temp);
854cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
855cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_ROOM_HF_LEVEL) {
856cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
857cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
8583d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent            pValue32 = (int32_t *)&pProperties->decayTime;
859cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
860135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
861cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DECAY_TIME:
862cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Calculate reverb feedback path gain
863cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (pReverb->m_nRvbLpfFwd << 15) / (32767 - pReverb->m_nRvbLpfFbk);
864135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            temp = Effects_Linear16ToMillibels(temp);
865135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
866cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Calculate decay time: g = -6000 d/DT , g gain in millibels, d reverb delay, DT decay time
867cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (-6000 * pReverb->m_nLateDelay) / temp;
868135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
869cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert samples to ms
870cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue32 = (temp * 1000) / pReverb->m_nSamplingRate;
871135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
8723856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_DECAY_TIME, samples %d, ms %d", temp, *pValue32);
873135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
874cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_DECAY_TIME) {
875cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
876cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
877cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pValue16 = &pProperties->decayHFRatio;
878cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
879cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
880cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DECAY_HF_RATIO:
881cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // If r is the decay HF ratio  (r = REVERB_PARAM_DECAY_HF_RATIO/1000) we have:
882cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            //       DT_5000Hz = DT_0Hz * r
883cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            //  and  G_5000Hz = -6000 * d / DT_5000Hz and G_0Hz = -6000 * d / DT_0Hz in millibels so :
884cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // r = G_0Hz/G_5000Hz in millibels
885cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // The linear gain at 5000Hz is b0 / sqrt(a1^2 + 2*C*a1 + 1) where:
886cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // - a1 is minus the LP feedback gain: -pReverb->m_nRvbLpfFbk
887cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // - b0 is the LP forward gain: pReverb->m_nRvbLpfFwd
888cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // - C is cos(2piWT) @ 5000Hz: pReverb->m_nCosWT_5KHz
889cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (pReverb->m_nRvbLpfFbk == 0) {
890cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                *pValue16 = 1000;
8913856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("get REVERB_PARAM_DECAY_HF_RATIO, pReverb->m_nRvbLpfFbk == 0, ratio %d", *pValue16);
892cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            } else {
893cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = MULT_EG1_EG1(pReverb->m_nRvbLpfFbk, pReverb->m_nRvbLpfFbk);
894cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp2 = MULT_EG1_EG1(pReverb->m_nRvbLpfFbk, pReverb->m_nCosWT_5KHz)
895cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        << 1;
896cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = 32767 + temp - temp2;
897cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = Effects_Sqrt(temp) * 181;
898cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = (pReverb->m_nRvbLpfFwd << 15) / temp;
899cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                // The linear gain at 0Hz is b0 / (a1 + 1)
900cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp2 = (pReverb->m_nRvbLpfFwd << 15) / (32767
901cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        - pReverb->m_nRvbLpfFbk);
902cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
903cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = Effects_Linear16ToMillibels(temp);
904cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp2 = Effects_Linear16ToMillibels(temp2);
9053856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("get REVERB_PARAM_DECAY_HF_RATIO, gain 5KHz %d mB, gain DC %d mB", temp, temp2);
906cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
907cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                if (temp == 0)
908cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    temp = 1;
909cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = (int16_t) ((1000 * temp2) / temp);
910cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                if (temp > 1000)
911cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    temp = 1000;
912cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
913cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                *pValue16 = temp;
9143856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("get REVERB_PARAM_DECAY_HF_RATIO, ratio %d", *pValue16);
915cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
916135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
917cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_DECAY_HF_RATIO) {
918cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
919cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
920cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pValue16 = &pProperties->reflectionsLevel;
921cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
922135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
923cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REFLECTIONS_LEVEL:
924cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue16 = Effects_Linear16ToMillibels(pReverb->m_nEarlyGain);
925135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
9263856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_REFLECTIONS_LEVEL, %d", *pValue16);
927cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_REFLECTIONS_LEVEL) {
928cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
929cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
9303d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent            pValue32 = (int32_t *)&pProperties->reflectionsDelay;
931cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
932135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
933cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REFLECTIONS_DELAY:
934cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // convert samples to ms
935cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue32 = (pReverb->m_nEarlyDelay * 1000) / pReverb->m_nSamplingRate;
936135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
9373856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_REFLECTIONS_DELAY, samples %d, ms %d", pReverb->m_nEarlyDelay, *pValue32);
938135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
939cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_REFLECTIONS_DELAY) {
940cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
941cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
942cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pValue16 = &pProperties->reverbLevel;
943cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
944135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
945cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REVERB_LEVEL:
946cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert linear gain to millibels
947cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue16 = Effects_Linear16ToMillibels(pReverb->m_nLateGain << 2);
948135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
9493856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_REVERB_LEVEL %d", *pValue16);
950135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
951cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_REVERB_LEVEL) {
952cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
953cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
9543d5188bd6abe55898f10a0edf3c05aff8aa2ef67Eric Laurent            pValue32 = (int32_t *)&pProperties->reverbDelay;
955cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
956135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
957cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REVERB_DELAY:
958cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // convert samples to ms
959cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue32 = (pReverb->m_nLateDelay * 1000) / pReverb->m_nSamplingRate;
960135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
9613856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_REVERB_DELAY, samples %d, ms %d", pReverb->m_nLateDelay, *pValue32);
962135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
963cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_REVERB_DELAY) {
964cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
965cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
966cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pValue16 = &pProperties->diffusion;
967cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
968135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
969cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DIFFUSION:
970cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (int16_t) ((1000 * (pReverb->m_sAp0.m_nApGain - AP0_GAIN_BASE))
971cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    / AP0_GAIN_RANGE);
972135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
973cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (temp < 0)
974cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = 0;
975cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (temp > 1000)
976cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = 1000;
977135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
978cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue16 = temp;
9793856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_DIFFUSION, %d, AP0 gain %d", *pValue16, pReverb->m_sAp0.m_nApGain);
980135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
981cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_DIFFUSION) {
982cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
983cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
984cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pValue16 = &pProperties->density;
985cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
986135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
987cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DENSITY:
988cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Calculate AP delay in time units
989cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = ((pReverb->m_sAp0.m_zApOut - pReverb->m_sAp0.m_zApIn) << 16)
990cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    / pReverb->m_nSamplingRate;
991135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
992cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (int16_t) ((1000 * (temp - AP0_TIME_BASE)) / AP0_TIME_RANGE);
993135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
994cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (temp < 0)
995cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = 0;
996cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (temp > 1000)
997cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = 1000;
998135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
999cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            *pValue16 = temp;
1000cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
10013856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("get REVERB_PARAM_DENSITY, %d, AP0 delay smps %d", *pValue16, pReverb->m_sAp0.m_zApOut - pReverb->m_sAp0.m_zApIn);
1002cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            break;
1003cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1004cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        default:
1005cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            break;
1006cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
1007135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
1008135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1009cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent    *pSize = size;
1010cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
10113856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Reverb_getParameter, context %p, param %d, value %d",
1012135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb, param, *(int *)pValue);
1013135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1014135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return 0;
1015135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent} /* end Reverb_getParameter */
1016135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1017135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
1018135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Reverb_setParameter()
1019135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
1020135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Purpose:
1021135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Set a Reverb parameter
1022135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1023135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Inputs:
1024135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  pReverb       - handle to instance data
1025135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  param         - parameter
1026135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  pValue        - pointer to parameter value
1027135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *  size          - value size
1028135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1029135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Outputs:
1030135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1031135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1032135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Side Effects:
1033135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1034135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
1035135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
1036b302bd5d288be2d3363b80053ca2392560b00b25Ashok Bhatint Reverb_setParameter(reverb_object_t *pReverb, int32_t param, uint32_t size,
1037135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        void *pValue) {
1038135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t value32;
1039135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int16_t value16;
104023e1de74359f4bb1763aef0adfebe073122b032cEric Laurent    t_reverb_settings *pProperties;
1041135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t i;
1042135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t temp;
1043135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t temp2;
1044135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    reverb_preset_t *pPreset;
1045135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int maxSamples;
1046135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t averageDelay;
1047b302bd5d288be2d3363b80053ca2392560b00b25Ashok Bhat    uint32_t paramSize;
1048135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
10493856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Reverb_setParameter, context %p, param %d, value16 %d, value32 %d",
1050135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb, param, *(int16_t *)pValue, *(int32_t *)pValue);
1051135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1052cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent    if (pReverb->m_Preset) {
1053cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        if (param != REVERB_PARAM_PRESET || size != sizeof(int16_t)) {
1054135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            return -EINVAL;
1055cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
1056cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        value16 = *(int16_t *)pValue;
10573856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("set REVERB_PARAM_PRESET, preset %d", value16);
1058cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        if (value16 < REVERB_PRESET_NONE || value16 > REVERB_PRESET_PLATE) {
1059135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            return -EINVAL;
1060cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
1061cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        // REVERB_PRESET_NONE is mapped to bypass
1062cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        if (value16 == REVERB_PRESET_NONE) {
1063cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_bBypass = 1;
1064cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        } else {
1065cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_bBypass = 0;
1066cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nNextRoom = value16 - 1;
1067cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
1068cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent    } else {
1069cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        switch (param) {
1070cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_ROOM_LEVEL:
1071cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_ROOM_HF_LEVEL:
1072cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DECAY_HF_RATIO:
1073cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REFLECTIONS_LEVEL:
1074cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REVERB_LEVEL:
1075cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DIFFUSION:
1076cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DENSITY:
1077cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            paramSize = sizeof(int16_t);
1078cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            break;
1079135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1080cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_BYPASS:
1081cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DECAY_TIME:
1082cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REFLECTIONS_DELAY:
1083cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REVERB_DELAY:
1084cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            paramSize = sizeof(int32_t);
1085135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            break;
1086135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1087cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_PROPERTIES:
108823e1de74359f4bb1763aef0adfebe073122b032cEric Laurent            paramSize = sizeof(t_reverb_settings);
1089cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            break;
1090135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1091cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        default:
1092135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            return -EINVAL;
1093135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        }
1094135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1095cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        if (size != paramSize) {
1096135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            return -EINVAL;
1097cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
1098135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1099cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        if (paramSize == sizeof(int16_t)) {
1100cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value16 = *(int16_t *) pValue;
1101cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        } else if (paramSize == sizeof(int32_t)) {
1102cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value32 = *(int32_t *) pValue;
1103cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        } else {
110423e1de74359f4bb1763aef0adfebe073122b032cEric Laurent            pProperties = (t_reverb_settings *) pValue;
1105cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
1106135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1107cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        pPreset = &pReverb->m_sPreset.m_sPreset[pReverb->m_nNextRoom];
1108135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1109cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        switch (param) {
1110cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_BYPASS:
1111cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_bBypass = (uint16_t)value32;
1112135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            break;
1113135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1114cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_PROPERTIES:
1115cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value16 = pProperties->roomLevel;
1116cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
1117135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1118cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_ROOM_LEVEL:
1119cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert millibels to linear 16 bit signed => m_nRoomLpfFwd
1120cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (value16 > 0)
1121cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                return -EINVAL;
1122135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1123cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = Effects_MillibelsToLinear16(value16);
1124135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1125cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nRoomLpfFwd
1126cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    = MULT_EG1_EG1(temp, (32767 - pReverb->m_nRoomLpfFbk));
1127cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
11283856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_ROOM_LEVEL, gain %d, new m_nRoomLpfFwd %d, m_nRoomLpfFbk %d", temp, pReverb->m_nRoomLpfFwd, pReverb->m_nRoomLpfFbk);
1129cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_ROOM_LEVEL)
1130cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
1131cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value16 = pProperties->roomHFLevel;
1132cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
1133cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1134cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_ROOM_HF_LEVEL:
1135cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1136cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Limit to 0 , -40dB range because of low pass implementation
1137cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (value16 > 0 || value16 < -4000)
1138cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                return -EINVAL;
1139cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert attenuation @ 5000H expressed in millibels to => m_nRoomLpfFbk
1140cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // m_nRoomLpfFbk is -a1 where a1 is the solution of:
1141cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // a1^2 + 2*(C-dG^2)/(1-dG^2)*a1 + 1 = 0 where:
1142cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // - C is cos(2*pi*5000/Fs) (pReverb->m_nCosWT_5KHz)
1143cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // - dG is G0/Gf (G0 is the linear gain at DC and Gf is the wanted gain at 5000Hz)
1144cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1145cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Save current DC gain m_nRoomLpfFwd / (32767 - m_nRoomLpfFbk) to keep it unchanged
1146cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // while changing HF level
1147cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp2 = (pReverb->m_nRoomLpfFwd << 15) / (32767
1148cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    - pReverb->m_nRoomLpfFbk);
1149cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (value16 == 0) {
1150cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                pReverb->m_nRoomLpfFbk = 0;
1151cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            } else {
1152cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                int32_t dG2, b, delta;
1153cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1154cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                // dG^2
1155cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = Effects_MillibelsToLinear16(value16);
11563856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, HF gain %d", temp);
1157cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = (1 << 30) / temp;
11583856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, 1/ HF gain %d", temp);
1159cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                dG2 = (int32_t) (((int64_t) temp * (int64_t) temp) >> 15);
11603856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, 1/ HF gain ^ 2 %d", dG2);
1161cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                // b = 2*(C-dG^2)/(1-dG^2)
1162cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                b = (int32_t) ((((int64_t) 1 << (15 + 1))
1163cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        * ((int64_t) pReverb->m_nCosWT_5KHz - (int64_t) dG2))
1164cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        / ((int64_t) 32767 - (int64_t) dG2));
1165cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1166cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                // delta = b^2 - 4
1167cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                delta = (int32_t) ((((int64_t) b * (int64_t) b) >> 15) - (1 << (15
1168cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        + 2)));
1169cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
11703856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV_IF(delta > (1<<30), " delta overflow %d", delta);
1171cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
11723856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, dG2 %d, b %d, delta %d, m_nCosWT_5KHz %d", dG2, b, delta, pReverb->m_nCosWT_5KHz);
1173cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                // m_nRoomLpfFbk = -a1 = - (- b + sqrt(delta)) / 2
1174cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                pReverb->m_nRoomLpfFbk = (b - Effects_Sqrt(delta) * 181) >> 1;
1175cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
11763856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, olg DC gain %d new m_nRoomLpfFbk %d, old m_nRoomLpfFwd %d",
1177cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    temp2, pReverb->m_nRoomLpfFbk, pReverb->m_nRoomLpfFwd);
1178cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1179cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nRoomLpfFwd
1180cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    = MULT_EG1_EG1(temp2, (32767 - pReverb->m_nRoomLpfFbk));
11813856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_ROOM_HF_LEVEL, new m_nRoomLpfFwd %d", pReverb->m_nRoomLpfFwd);
1182cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1183cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_ROOM_HF_LEVEL)
1184cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
1185cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value32 = pProperties->decayTime;
1186cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
1187cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1188cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DECAY_TIME:
1189cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1190cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert milliseconds to => m_nRvbLpfFwd (function of m_nRvbLpfFbk)
1191cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // convert ms to samples
1192cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value32 = (value32 * pReverb->m_nSamplingRate) / 1000;
1193cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1194cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // calculate valid decay time range as a function of current reverb delay and
1195cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // max feed back gain. Min value <=> -40dB in one pass, Max value <=> feedback gain = -1 dB
1196cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Calculate attenuation for each round in late reverb given a total attenuation of -6000 millibels.
1197cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // g = -6000 d/DT , g gain in millibels, d reverb delay, DT decay time
1198cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            averageDelay = pReverb->m_nLateDelay - pReverb->m_nMaxExcursion;
1199cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            averageDelay += ((pReverb->m_sAp0.m_zApOut - pReverb->m_sAp0.m_zApIn)
1200cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    + (pReverb->m_sAp1.m_zApOut - pReverb->m_sAp1.m_zApIn)) >> 1;
1201cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1202cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (-6000 * averageDelay) / value32;
12033856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_DECAY_TIME, delay smps %d, DT smps %d, gain mB %d",averageDelay, value32, temp);
1204cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (temp < -4000 || temp > -100)
1205cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                return -EINVAL;
1206cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1207cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // calculate low pass gain by adding reverb input attenuation (pReverb->m_nLateGain) and substrating output
1208cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // xfade and sum gain (max +9dB)
1209cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp -= Effects_Linear16ToMillibels(pReverb->m_nLateGain) + 900;
1210cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = Effects_MillibelsToLinear16(temp);
1211135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1212cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // DC gain (temp) = b0 / (1 + a1) = pReverb->m_nRvbLpfFwd / (32767 - pReverb->m_nRvbLpfFbk)
1213cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nRvbLpfFwd
1214cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    = MULT_EG1_EG1(temp, (32767 - pReverb->m_nRvbLpfFbk));
1215135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
12163856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_DECAY_TIME, gain %d, new m_nRvbLpfFwd %d, old m_nRvbLpfFbk %d, reverb gain %d", temp, pReverb->m_nRvbLpfFwd, pReverb->m_nRvbLpfFbk, Effects_Linear16ToMillibels(pReverb->m_nLateGain));
1217135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1218cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_DECAY_TIME)
1219cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
1220cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value16 = pProperties->decayHFRatio;
1221cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
1222135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1223cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DECAY_HF_RATIO:
1224135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1225cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // We limit max value to 1000 because reverb filter is lowpass only
1226cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (value16 < 100 || value16 > 1000)
1227cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                return -EINVAL;
1228cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert per mille to => m_nLpfFwd, m_nLpfFbk
1229135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1230cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Save current DC gain m_nRoomLpfFwd / (32767 - m_nRoomLpfFbk) to keep it unchanged
1231cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // while changing HF level
1232cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp2 = (pReverb->m_nRvbLpfFwd << 15) / (32767 - pReverb->m_nRvbLpfFbk);
1233135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1234cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (value16 == 1000) {
1235cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                pReverb->m_nRvbLpfFbk = 0;
1236cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            } else {
1237cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                int32_t dG2, b, delta;
1238135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1239cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = Effects_Linear16ToMillibels(temp2);
1240cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                // G_5000Hz = G_DC * (1000/REVERB_PARAM_DECAY_HF_RATIO) in millibels
1241135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1242cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                value32 = ((int32_t) 1000 << 15) / (int32_t) value16;
12433856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("REVERB_PARAM_DECAY_HF_RATIO, DC gain %d, DC gain mB %d, 1000/R %d", temp2, temp, value32);
1244135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1245cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = (int32_t) (((int64_t) temp * (int64_t) value32) >> 15);
1246135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1247cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                if (temp < -4000) {
12483856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("REVERB_PARAM_DECAY_HF_RATIO HF gain overflow %d mB", temp);
1249cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    temp = -4000;
1250cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                }
1251135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1252cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = Effects_MillibelsToLinear16(temp);
12533856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("REVERB_PARAM_DECAY_HF_RATIO, HF gain %d", temp);
1254cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                // dG^2
1255cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = (temp2 << 15) / temp;
1256cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                dG2 = (int32_t) (((int64_t) temp * (int64_t) temp) >> 15);
1257135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1258cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                // b = 2*(C-dG^2)/(1-dG^2)
1259cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                b = (int32_t) ((((int64_t) 1 << (15 + 1))
1260cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        * ((int64_t) pReverb->m_nCosWT_5KHz - (int64_t) dG2))
1261cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        / ((int64_t) 32767 - (int64_t) dG2));
1262135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1263cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                // delta = b^2 - 4
1264cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                delta = (int32_t) ((((int64_t) b * (int64_t) b) >> 15) - (1 << (15
1265cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        + 2)));
1266135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1267cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                // m_nRoomLpfFbk = -a1 = - (- b + sqrt(delta)) / 2
1268cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                pReverb->m_nRvbLpfFbk = (b - Effects_Sqrt(delta) * 181) >> 1;
1269135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
12703856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("REVERB_PARAM_DECAY_HF_RATIO, dG2 %d, b %d, delta %d", dG2, b, delta);
1271135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1272cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
1273135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
12743856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_DECAY_HF_RATIO, gain %d, m_nRvbLpfFbk %d, m_nRvbLpfFwd %d", temp2, pReverb->m_nRvbLpfFbk, pReverb->m_nRvbLpfFwd);
1275135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1276cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nRvbLpfFwd
1277cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    = MULT_EG1_EG1(temp2, (32767 - pReverb->m_nRvbLpfFbk));
1278135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1279cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_DECAY_HF_RATIO)
1280cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
1281cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value16 = pProperties->reflectionsLevel;
1282cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
1283135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1284cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REFLECTIONS_LEVEL:
1285cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // We limit max value to 0 because gain is limited to 0dB
1286cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (value16 > 0 || value16 < -6000)
1287cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                return -EINVAL;
1288135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1289cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert millibels to linear 16 bit signed and recompute m_sEarlyL.m_nGain[i] and m_sEarlyR.m_nGain[i].
1290cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value16 = Effects_MillibelsToLinear16(value16);
1291cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            for (i = 0; i < REVERB_MAX_NUM_REFLECTIONS; i++) {
1292cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                pReverb->m_sEarlyL.m_nGain[i]
1293cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        = MULT_EG1_EG1(pPreset->m_sEarlyL.m_nGain[i],value16);
1294cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                pReverb->m_sEarlyR.m_nGain[i]
1295cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        = MULT_EG1_EG1(pPreset->m_sEarlyR.m_nGain[i],value16);
1296cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
1297cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nEarlyGain = value16;
12983856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_REFLECTIONS_LEVEL, m_nEarlyGain %d", pReverb->m_nEarlyGain);
1299cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1300cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_REFLECTIONS_LEVEL)
1301cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
1302cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value32 = pProperties->reflectionsDelay;
1303cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
1304cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1305cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REFLECTIONS_DELAY:
1306cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // We limit max value MAX_EARLY_TIME
1307cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // convert ms to time units
1308cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (value32 * 65536) / 1000;
1309cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (temp < 0 || temp > MAX_EARLY_TIME)
1310cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                return -EINVAL;
1311cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1312cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            maxSamples = (int32_t) (MAX_EARLY_TIME * pReverb->m_nSamplingRate)
1313cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    >> 16;
1314cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (temp * pReverb->m_nSamplingRate) >> 16;
1315cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            for (i = 0; i < REVERB_MAX_NUM_REFLECTIONS; i++) {
1316cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp2 = temp + (((int32_t) pPreset->m_sEarlyL.m_zDelay[i]
1317cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        * pReverb->m_nSamplingRate) >> 16);
1318cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                if (temp2 > maxSamples)
1319cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    temp2 = maxSamples;
1320cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                pReverb->m_sEarlyL.m_zDelay[i] = pReverb->m_nEarly0in + temp2;
1321cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp2 = temp + (((int32_t) pPreset->m_sEarlyR.m_zDelay[i]
1322cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                        * pReverb->m_nSamplingRate) >> 16);
1323cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                if (temp2 > maxSamples)
1324cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    temp2 = maxSamples;
1325cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                pReverb->m_sEarlyR.m_zDelay[i] = pReverb->m_nEarly1in + temp2;
1326cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
1327cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nEarlyDelay = temp;
1328cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
13293856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_REFLECTIONS_DELAY, m_nEarlyDelay smps %d max smp delay %d", pReverb->m_nEarlyDelay, maxSamples);
1330cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1331cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert milliseconds to sample count => m_nEarlyDelay
1332cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_REFLECTIONS_DELAY)
1333cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
1334cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value16 = pProperties->reverbLevel;
1335cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
1336cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1337cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REVERB_LEVEL:
1338cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // We limit max value to 0 because gain is limited to 0dB
1339cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (value16 > 0 || value16 < -6000)
1340cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                return -EINVAL;
1341cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert millibels to linear 16 bits (gange 0 - 8191) => m_nLateGain.
1342cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nLateGain = Effects_MillibelsToLinear16(value16) >> 2;
1343cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
13443856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_REVERB_LEVEL, m_nLateGain %d", pReverb->m_nLateGain);
1345cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1346cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_REVERB_LEVEL)
1347cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
1348cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value32 = pProperties->reverbDelay;
1349cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
1350cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1351cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_REVERB_DELAY:
1352cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // We limit max value to MAX_DELAY_TIME
1353cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // convert ms to time units
1354cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (value32 * 65536) / 1000;
1355cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (temp < 0 || temp > MAX_DELAY_TIME)
1356cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                return -EINVAL;
1357cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1358cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            maxSamples = (int32_t) (MAX_DELAY_TIME * pReverb->m_nSamplingRate)
1359cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    >> 16;
1360cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (temp * pReverb->m_nSamplingRate) >> 16;
1361cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if ((temp + pReverb->m_nMaxExcursion) > maxSamples) {
1362cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = maxSamples - pReverb->m_nMaxExcursion;
1363cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
1364cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (temp < pReverb->m_nMaxExcursion) {
1365cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = pReverb->m_nMaxExcursion;
1366cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            }
1367135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1368cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp -= pReverb->m_nLateDelay;
1369cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nDelay0Out += temp;
1370cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nDelay1Out += temp;
1371cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_nLateDelay += temp;
1372135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
13733856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_REVERB_DELAY, m_nLateDelay smps %d max smp delay %d", pReverb->m_nLateDelay, maxSamples);
1374135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1375cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert milliseconds to sample count => m_nDelay1Out + m_nMaxExcursion
1376cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_REVERB_DELAY)
1377cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
1378135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1379cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value16 = pProperties->diffusion;
1380cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
1381135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1382cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DIFFUSION:
1383cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (value16 < 0 || value16 > 1000)
1384cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                return -EINVAL;
1385135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1386cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert per mille to m_sAp0.m_nApGain, m_sAp1.m_nApGain
1387cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_sAp0.m_nApGain = AP0_GAIN_BASE + ((int32_t) value16
1388cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    * AP0_GAIN_RANGE) / 1000;
1389cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_sAp1.m_nApGain = AP1_GAIN_BASE + ((int32_t) value16
1390cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                    * AP1_GAIN_RANGE) / 1000;
1391135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
13923856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_DIFFUSION, m_sAp0.m_nApGain %d m_sAp1.m_nApGain %d", pReverb->m_sAp0.m_nApGain, pReverb->m_sAp1.m_nApGain);
1393135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1394cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (param == REVERB_PARAM_DIFFUSION)
1395cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                break;
1396135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1397cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            value16 = pProperties->density;
1398cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /* FALL THROUGH */
1399135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1400cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        case REVERB_PARAM_DENSITY:
1401cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (value16 < 0 || value16 > 1000)
1402cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                return -EINVAL;
1403135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1404cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            // Convert per mille to m_sAp0.m_zApOut, m_sAp1.m_zApOut
1405cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            maxSamples = (int32_t) (MAX_AP_TIME * pReverb->m_nSamplingRate) >> 16;
1406135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1407cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = AP0_TIME_BASE + ((int32_t) value16 * AP0_TIME_RANGE) / 1000;
1408cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /*lint -e{702} shift for performance */
1409cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (temp * pReverb->m_nSamplingRate) >> 16;
1410cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (temp > maxSamples)
1411cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = maxSamples;
1412cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_sAp0.m_zApOut = (uint16_t) (pReverb->m_sAp0.m_zApIn + temp);
1413135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
14143856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("REVERB_PARAM_DENSITY, Ap0 delay smps %d", temp);
1415135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1416cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = AP1_TIME_BASE + ((int32_t) value16 * AP1_TIME_RANGE) / 1000;
1417cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            /*lint -e{702} shift for performance */
1418cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            temp = (temp * pReverb->m_nSamplingRate) >> 16;
1419cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            if (temp > maxSamples)
1420cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent                temp = maxSamples;
1421cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            pReverb->m_sAp1.m_zApOut = (uint16_t) (pReverb->m_sAp1.m_zApIn + temp);
1422135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
14233856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Ap1 delay smps %d", temp);
1424135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1425cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            break;
1426135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1427cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        default:
1428cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent            break;
1429cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent        }
1430135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
1431cb28102ad3976339ec445afbcb4130f2991b38d7Eric Laurent
1432135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return 0;
1433135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent} /* end Reverb_setParameter */
1434135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1435135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
1436135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * ReverbUpdateXfade
1437135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
1438135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Purpose:
1439135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Update the xfade parameters as required
1440135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1441135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Inputs:
1442135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * nNumSamplesToAdd - number of samples to write to buffer
1443135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1444135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Outputs:
1445135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1446135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1447135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Side Effects:
1448135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * - xfade parameters will be changed
1449135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1450135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
1451135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
1452135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic int ReverbUpdateXfade(reverb_object_t *pReverb, int nNumSamplesToAdd) {
1453135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    uint16_t nOffset;
1454135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int16_t tempCos;
1455135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int16_t tempSin;
1456135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1457135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (pReverb->m_nXfadeCounter >= pReverb->m_nXfadeInterval) {
1458135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        /* update interval has elapsed, so reset counter */
1459135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nXfadeCounter = 0;
1460135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1461135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // Pin the sin,cos values to min / max values to ensure that the
1462135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // modulated taps' coefs are zero (thus no clicks)
1463135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        if (pReverb->m_nPhaseIncrement > 0) {
1464135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            // if phase increment > 0, then sin -> 1, cos -> 0
1465135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_nSin = 32767;
1466135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_nCos = 0;
1467135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1468135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            // reset the phase to match the sin, cos values
1469135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_nPhase = 32767;
1470135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1471135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            // modulate the cross taps because their tap coefs are zero
1472135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            nOffset = ReverbCalculateNoise(pReverb);
1473135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1474135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_zD1Cross = pReverb->m_nDelay1Out
1475135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                    - pReverb->m_nMaxExcursion + nOffset;
1476135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1477135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            nOffset = ReverbCalculateNoise(pReverb);
1478135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1479135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_zD0Cross = pReverb->m_nDelay0Out
1480135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                    - pReverb->m_nMaxExcursion - nOffset;
1481135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        } else {
1482135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            // if phase increment < 0, then sin -> 0, cos -> 1
1483135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_nSin = 0;
1484135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_nCos = 32767;
1485135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1486135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            // reset the phase to match the sin, cos values
1487135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_nPhase = -32768;
1488135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1489135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            // modulate the self taps because their tap coefs are zero
1490135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            nOffset = ReverbCalculateNoise(pReverb);
1491135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1492135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_zD0Self = pReverb->m_nDelay0Out
1493135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                    - pReverb->m_nMaxExcursion - nOffset;
1494135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1495135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            nOffset = ReverbCalculateNoise(pReverb);
1496135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1497135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pReverb->m_zD1Self = pReverb->m_nDelay1Out
1498135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                    - pReverb->m_nMaxExcursion + nOffset;
1499135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1500135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        } // end if-else (pReverb->m_nPhaseIncrement > 0)
1501135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1502135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // Reverse the direction of the sin,cos so that the
1503135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // tap whose coef was previously increasing now decreases
1504135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // and vice versa
1505135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nPhaseIncrement = -pReverb->m_nPhaseIncrement;
1506135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1507135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    } // end if counter >= update interval
1508135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1509135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //compute what phase will be next time
1510135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nPhase += pReverb->m_nPhaseIncrement;
1511135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1512135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //calculate what the new sin and cos need to reach by the next update
1513135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    ReverbCalculateSinCos(pReverb->m_nPhase, &tempSin, &tempCos);
1514135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1515135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //calculate the per-sample increment required to get there by the next update
1516135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    /*lint -e{702} shift for performance */
1517135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nSinIncrement = (tempSin - pReverb->m_nSin)
1518135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            >> pReverb->m_nUpdatePeriodInBits;
1519135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1520135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    /*lint -e{702} shift for performance */
1521135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nCosIncrement = (tempCos - pReverb->m_nCos)
1522135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            >> pReverb->m_nUpdatePeriodInBits;
1523135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1524135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    /* increment update counter */
1525135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nXfadeCounter += (uint16_t) nNumSamplesToAdd;
1526135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1527135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return 0;
1528135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1529135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent} /* end ReverbUpdateXfade */
1530135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1531135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
1532135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * ReverbCalculateNoise
1533135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
1534135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Purpose:
1535135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Calculate a noise sample and limit its value
1536135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1537135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Inputs:
1538135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * nMaxExcursion - noise value is limited to this value
1539135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * pnNoise - return new noise sample in this (not limited)
1540135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1541135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Outputs:
1542135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * new limited noise value
1543135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1544135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Side Effects:
1545135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * - *pnNoise noise value is updated
1546135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1547135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
1548135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
1549135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic uint16_t ReverbCalculateNoise(reverb_object_t *pReverb) {
1550135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int16_t nNoise = pReverb->m_nNoise;
1551135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1552135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // calculate new noise value
1553135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (pReverb->m_bUseNoise) {
1554135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nNoise = (int16_t) (nNoise * 5 + 1);
1555135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    } else {
1556135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nNoise = 0;
1557135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
1558135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1559135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    pReverb->m_nNoise = nNoise;
1560135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // return the limited noise value
1561135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return (pReverb->m_nMaxExcursion & nNoise);
1562135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1563135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent} /* end ReverbCalculateNoise */
1564135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1565135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
1566135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * ReverbCalculateSinCos
1567135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
1568135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Purpose:
1569135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Calculate a new sin and cosine value based on the given phase
1570135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1571135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Inputs:
1572135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * nPhase   - phase angle
1573135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * pnSin    - input old value, output new value
1574135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * pnCos    - input old value, output new value
1575135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1576135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Outputs:
1577135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1578135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Side Effects:
1579135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * - *pnSin, *pnCos are updated
1580135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1581135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
1582135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
1583135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic int ReverbCalculateSinCos(int16_t nPhase, int16_t *pnSin, int16_t *pnCos) {
1584135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t nTemp;
1585135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t nNetAngle;
1586135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1587135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    //  -1 <=  nPhase  < 1
1588135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // However, for the calculation, we need a value
1589135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // that ranges from -1/2 to +1/2, so divide the phase by 2
1590135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    /*lint -e{702} shift for performance */
1591135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    nNetAngle = nPhase >> 1;
1592135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1593135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    /*
1594135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent     Implement the following
1595135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent     sin(x) = (2-4*c)*x^2 + c + x
1596135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent     cos(x) = (2-4*c)*x^2 + c - x
1597135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1598135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent     where  c = 1/sqrt(2)
1599135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent     using the a0 + x*(a1 + x*a2) approach
1600135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent     */
1601135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1602135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    /* limit the input "angle" to be between -0.5 and +0.5 */
1603135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (nNetAngle > EG1_HALF) {
1604135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nNetAngle = EG1_HALF;
1605135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    } else if (nNetAngle < EG1_MINUS_HALF) {
1606135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nNetAngle = EG1_MINUS_HALF;
1607135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
1608135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1609135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    /* calculate sin */
1610135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    nTemp = EG1_ONE + MULT_EG1_EG1(REVERB_PAN_G2, nNetAngle);
1611135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    nTemp = REVERB_PAN_G0 + MULT_EG1_EG1(nTemp, nNetAngle);
1612135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    *pnSin = (int16_t) SATURATE_EG1(nTemp);
1613135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1614135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    /* calculate cos */
1615135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    nTemp = -EG1_ONE + MULT_EG1_EG1(REVERB_PAN_G2, nNetAngle);
1616135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    nTemp = REVERB_PAN_G0 + MULT_EG1_EG1(nTemp, nNetAngle);
1617135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    *pnCos = (int16_t) SATURATE_EG1(nTemp);
1618135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1619135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return 0;
1620135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent} /* end ReverbCalculateSinCos */
1621135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1622135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent/*----------------------------------------------------------------------------
1623135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Reverb
1624135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
1625135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Purpose:
1626135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * apply reverb to the given signal
1627135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1628135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Inputs:
1629135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * nNu
1630135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * pnSin    - input old value, output new value
1631135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * pnCos    - input old value, output new value
1632135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1633135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Outputs:
1634135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * number of samples actually reverberated
1635135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1636135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent * Side Effects:
1637135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *
1638135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent *----------------------------------------------------------------------------
1639135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent */
1640135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic int Reverb(reverb_object_t *pReverb, int nNumSamplesToAdd,
1641135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        short *pOutputBuffer, short *pInputBuffer) {
1642135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t i;
1643135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t nDelayOut0;
1644135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t nDelayOut1;
1645135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    uint16_t nBase;
1646135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1647135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    uint32_t nAddr;
1648135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t nTemp1;
1649135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t nTemp2;
1650135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t nApIn;
1651135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t nApOut;
1652135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1653135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t j;
1654135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t nEarlyOut;
1655135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1656135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    int32_t tempValue;
1657135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1658135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // get the base address
1659135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    nBase = pReverb->m_nBaseIndex;
1660135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1661135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    for (i = 0; i < nNumSamplesToAdd; i++) {
1662135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** Left Allpass - start
1663135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nApIn = *pInputBuffer;
1664135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        if (!pReverb->m_Aux) {
1665135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            pInputBuffer++;
1666135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        }
1667135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // store to early delay line
1668135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr = CIRCULAR(nBase, pReverb->m_nEarly0in, pReverb->m_nBufferMask);
1669135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nDelayLine[nAddr] = (short) nApIn;
1670135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1671135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // left input = (left dry * m_nLateGain) + right feedback from previous period
1672135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1673135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nApIn = SATURATE(nApIn + pReverb->m_nRevFbkR);
1674135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nApIn = MULT_EG1_EG1(nApIn, pReverb->m_nLateGain);
1675135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1676135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // fetch allpass delay line out
1677135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        //nAddr = CIRCULAR(nBase, psAp0->m_zApOut, pReverb->m_nBufferMask);
1678135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr
1679135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                = CIRCULAR(nBase, pReverb->m_sAp0.m_zApOut, pReverb->m_nBufferMask);
1680135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nDelayOut0 = pReverb->m_nDelayLine[nAddr];
1681135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1682135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate allpass feedforward; subtract the feedforward result
1683135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp1 = MULT_EG1_EG1(nApIn, pReverb->m_sAp0.m_nApGain);
1684135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nApOut = SATURATE(nDelayOut0 - nTemp1); // allpass output
1685135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1686135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate allpass feedback; add the feedback result
1687135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp1 = MULT_EG1_EG1(nApOut, pReverb->m_sAp0.m_nApGain);
1688135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp1 = SATURATE(nApIn + nTemp1);
1689135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1690135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // inject into allpass delay
1691135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr
1692135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                = CIRCULAR(nBase, pReverb->m_sAp0.m_zApIn, pReverb->m_nBufferMask);
1693135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nDelayLine[nAddr] = (short) nTemp1;
1694135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1695135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // inject allpass output into delay line
1696135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr = CIRCULAR(nBase, pReverb->m_zD0In, pReverb->m_nBufferMask);
1697135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nDelayLine[nAddr] = (short) nApOut;
1698135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1699135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** Left Allpass - end
1700135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1701135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** Right Allpass - start
1702135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nApIn = (*pInputBuffer++);
1703135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // store to early delay line
1704135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr = CIRCULAR(nBase, pReverb->m_nEarly1in, pReverb->m_nBufferMask);
1705135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nDelayLine[nAddr] = (short) nApIn;
1706135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1707135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // right input = (right dry * m_nLateGain) + left feedback from previous period
1708135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        /*lint -e{702} use shift for performance */
1709135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nApIn = SATURATE(nApIn + pReverb->m_nRevFbkL);
1710135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nApIn = MULT_EG1_EG1(nApIn, pReverb->m_nLateGain);
1711135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1712135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // fetch allpass delay line out
1713135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr
1714135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                = CIRCULAR(nBase, pReverb->m_sAp1.m_zApOut, pReverb->m_nBufferMask);
1715135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nDelayOut1 = pReverb->m_nDelayLine[nAddr];
1716135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1717135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate allpass feedforward; subtract the feedforward result
1718135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp1 = MULT_EG1_EG1(nApIn, pReverb->m_sAp1.m_nApGain);
1719135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nApOut = SATURATE(nDelayOut1 - nTemp1); // allpass output
1720135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1721135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate allpass feedback; add the feedback result
1722135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp1 = MULT_EG1_EG1(nApOut, pReverb->m_sAp1.m_nApGain);
1723135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp1 = SATURATE(nApIn + nTemp1);
1724135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1725135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // inject into allpass delay
1726135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr
1727135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                = CIRCULAR(nBase, pReverb->m_sAp1.m_zApIn, pReverb->m_nBufferMask);
1728135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nDelayLine[nAddr] = (short) nTemp1;
1729135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1730135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // inject allpass output into delay line
1731135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr = CIRCULAR(nBase, pReverb->m_zD1In, pReverb->m_nBufferMask);
1732135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nDelayLine[nAddr] = (short) nApOut;
1733135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1734135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** Right Allpass - end
1735135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1736135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** D0 output - start
1737135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // fetch delay line self out
1738135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr = CIRCULAR(nBase, pReverb->m_zD0Self, pReverb->m_nBufferMask);
1739135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nDelayOut0 = pReverb->m_nDelayLine[nAddr];
1740135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1741135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate delay line self out
1742135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp1 = MULT_EG1_EG1(nDelayOut0, pReverb->m_nSin);
1743135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1744135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // fetch delay line cross out
1745135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr = CIRCULAR(nBase, pReverb->m_zD1Cross, pReverb->m_nBufferMask);
1746135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nDelayOut0 = pReverb->m_nDelayLine[nAddr];
1747135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1748135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate delay line self out
1749135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp2 = MULT_EG1_EG1(nDelayOut0, pReverb->m_nCos);
1750135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1751135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate unfiltered delay out
1752135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nDelayOut0 = SATURATE(nTemp1 + nTemp2);
1753135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1754135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** D0 output - end
1755135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1756135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** D1 output - start
1757135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // fetch delay line self out
1758135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr = CIRCULAR(nBase, pReverb->m_zD1Self, pReverb->m_nBufferMask);
1759135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nDelayOut1 = pReverb->m_nDelayLine[nAddr];
1760135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1761135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate delay line self out
1762135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp1 = MULT_EG1_EG1(nDelayOut1, pReverb->m_nSin);
1763135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1764135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // fetch delay line cross out
1765135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nAddr = CIRCULAR(nBase, pReverb->m_zD0Cross, pReverb->m_nBufferMask);
1766135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nDelayOut1 = pReverb->m_nDelayLine[nAddr];
1767135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1768135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate delay line self out
1769135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp2 = MULT_EG1_EG1(nDelayOut1, pReverb->m_nCos);
1770135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1771135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate unfiltered delay out
1772135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nDelayOut1 = SATURATE(nTemp1 + nTemp2);
1773135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1774135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** D1 output - end
1775135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1776135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** mixer and feedback - start
1777135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // sum is fedback to right input (R + L)
1778135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nDelayOut0 = (short) SATURATE(nDelayOut0 + nDelayOut1);
1779135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1780135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // difference is feedback to left input (R - L)
1781135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        /*lint -e{685} lint complains that it can't saturate negative */
1782135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nDelayOut1 = (short) SATURATE(nDelayOut1 - nDelayOut0);
1783135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1784135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** mixer and feedback - end
1785135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1786135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate lowpass filter (mixer scale factor included in LPF feedforward)
1787135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp1 = MULT_EG1_EG1(nDelayOut0, pReverb->m_nRvbLpfFwd);
1788135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1789135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp2 = MULT_EG1_EG1(pReverb->m_nRevFbkL, pReverb->m_nRvbLpfFbk);
1790135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1791135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate filtered delay out and simultaneously update LPF state variable
1792135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // filtered delay output is stored in m_nRevFbkL
1793135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nRevFbkL = (short) SATURATE(nTemp1 + nTemp2);
1794135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1795135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate lowpass filter (mixer scale factor included in LPF feedforward)
1796135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp1 = MULT_EG1_EG1(nDelayOut1, pReverb->m_nRvbLpfFwd);
1797135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1798135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        nTemp2 = MULT_EG1_EG1(pReverb->m_nRevFbkR, pReverb->m_nRvbLpfFbk);
1799135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1800135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // calculate filtered delay out and simultaneously update LPF state variable
1801135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // filtered delay output is stored in m_nRevFbkR
1802135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        pReverb->m_nRevFbkR = (short) SATURATE(nTemp1 + nTemp2);
1803135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1804135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        // ********** start early reflection generator, left
1805135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        //psEarly = &(pReverb->m_sEarlyL);
1806135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1807135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1808135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        for (j = 0; j < REVERB_MAX_NUM_REFLECTIONS; j++) {
1809135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            // fetch delay line out
1810135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            //nAddr = CIRCULAR(nBase, psEarly->m_zDelay[j], pReverb->m_nBufferMask);
1811135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            nAddr
1812135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent                    = CIRCULAR(nBase, pReverb->m_sEarlyL.m_zDelay[j], pReverb->m_nBufferMask);
1813135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1814135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            nTemp1 = pReverb->m_nDelayLine[nAddr];
1815135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
1816135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent            // calculate reflection
1817135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent