1 /*
2  * Copyright (C) 2010-2010 NXP Software
3  * Copyright (C) 2009 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef LVM_FLOAT
18 typedef float LVM_FLOAT;
19 #endif
20 #define LOG_TAG "Reverb"
21 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array)[0])
22 //#define LOG_NDEBUG 0
23 
24 #include <assert.h>
25 #include <inttypes.h>
26 #include <new>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <audio_utils/primitives.h>
31 #include <log/log.h>
32 
33 #include "EffectReverb.h"
34 // from Reverb/lib
35 #include "LVREV.h"
36 
37 // effect_handle_t interface implementation for reverb
38 extern "C" const struct effect_interface_s gReverbInterface;
39 
40 #define LVM_ERROR_CHECK(LvmStatus, callingFunc, calledFunc){\
41         if ((LvmStatus) == LVREV_NULLADDRESS){\
42             ALOGV("\tLVREV_ERROR : Parameter error - "\
43                     "null pointer returned by %s in %s\n\n\n\n", callingFunc, calledFunc);\
44         }\
45         if ((LvmStatus) == LVREV_INVALIDNUMSAMPLES){\
46             ALOGV("\tLVREV_ERROR : Parameter error - "\
47                     "bad number of samples returned by %s in %s\n\n\n\n", callingFunc, calledFunc);\
48         }\
49         if ((LvmStatus) == LVREV_OUTOFRANGE){\
50             ALOGV("\tLVREV_ERROR : Parameter error - "\
51                     "out of range returned by %s in %s\n", callingFunc, calledFunc);\
52         }\
53     }
54 
55 // Namespaces
56 namespace android {
57 namespace {
58 
59 /************************************************************************************/
60 /*                                                                                  */
61 /* Preset definitions                                                               */
62 /*                                                                                  */
63 /************************************************************************************/
64 
65 const static t_reverb_settings sReverbPresets[] = {
66         // REVERB_PRESET_NONE: values are unused
67         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
68         // REVERB_PRESET_SMALLROOM
69         {-400, -600, 1100, 830, -400, 5, 500, 10, 1000, 1000},
70         // REVERB_PRESET_MEDIUMROOM
71         {-400, -600, 1300, 830, -1000, 20, -200, 20, 1000, 1000},
72         // REVERB_PRESET_LARGEROOM
73         {-400, -600, 1500, 830, -1600, 5, -1000, 40, 1000, 1000},
74         // REVERB_PRESET_MEDIUMHALL
75         {-400, -600, 1800, 700, -1300, 15, -800, 30, 1000, 1000},
76         // REVERB_PRESET_LARGEHALL
77         {-400, -600, 1800, 700, -2000, 30, -1400, 60, 1000, 1000},
78         // REVERB_PRESET_PLATE
79         {-400, -200, 1300, 900, 0, 2, 0, 10, 1000, 750},
80 };
81 
82 // NXP SW auxiliary environmental reverb
83 const effect_descriptor_t gAuxEnvReverbDescriptor = {
84         { 0xc2e5d5f0, 0x94bd, 0x4763, 0x9cac, { 0x4e, 0x23, 0x4d, 0x06, 0x83, 0x9e } },
85         { 0x4a387fc0, 0x8ab3, 0x11df, 0x8bad, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } },
86         EFFECT_CONTROL_API_VERSION,
87         EFFECT_FLAG_TYPE_AUXILIARY,
88         LVREV_CUP_LOAD_ARM9E,
89         LVREV_MEM_USAGE,
90         "Auxiliary Environmental Reverb",
91         "NXP Software Ltd.",
92 };
93 
94 // NXP SW insert environmental reverb
95 static const effect_descriptor_t gInsertEnvReverbDescriptor = {
96         {0xc2e5d5f0, 0x94bd, 0x4763, 0x9cac, {0x4e, 0x23, 0x4d, 0x06, 0x83, 0x9e}},
97         {0xc7a511a0, 0xa3bb, 0x11df, 0x860e, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
98         EFFECT_CONTROL_API_VERSION,
99         EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST | EFFECT_FLAG_VOLUME_CTRL,
100         LVREV_CUP_LOAD_ARM9E,
101         LVREV_MEM_USAGE,
102         "Insert Environmental Reverb",
103         "NXP Software Ltd.",
104 };
105 
106 // NXP SW auxiliary preset reverb
107 static const effect_descriptor_t gAuxPresetReverbDescriptor = {
108         {0x47382d60, 0xddd8, 0x11db, 0xbf3a, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
109         {0xf29a1400, 0xa3bb, 0x11df, 0x8ddc, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
110         EFFECT_CONTROL_API_VERSION,
111         EFFECT_FLAG_TYPE_AUXILIARY,
112         LVREV_CUP_LOAD_ARM9E,
113         LVREV_MEM_USAGE,
114         "Auxiliary Preset Reverb",
115         "NXP Software Ltd.",
116 };
117 
118 // NXP SW insert preset reverb
119 static const effect_descriptor_t gInsertPresetReverbDescriptor = {
120         {0x47382d60, 0xddd8, 0x11db, 0xbf3a, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
121         {0x172cdf00, 0xa3bc, 0x11df, 0xa72f, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
122         EFFECT_CONTROL_API_VERSION,
123         EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST | EFFECT_FLAG_VOLUME_CTRL,
124         LVREV_CUP_LOAD_ARM9E,
125         LVREV_MEM_USAGE,
126         "Insert Preset Reverb",
127         "NXP Software Ltd.",
128 };
129 
130 // gDescriptors contains pointers to all defined effect descriptor in this library
131 static const effect_descriptor_t * const gDescriptors[] = {
132         &gAuxEnvReverbDescriptor,
133         &gInsertEnvReverbDescriptor,
134         &gAuxPresetReverbDescriptor,
135         &gInsertPresetReverbDescriptor
136 };
137 
138 typedef     float               process_buffer_t; // process in float
139 
140 struct ReverbContext{
141     const struct effect_interface_s *itfe;
142     effect_config_t                 config;
143     LVREV_Handle_t                  hInstance;
144     int16_t                         SavedRoomLevel;
145     int16_t                         SavedHfLevel;
146     int16_t                         SavedDecayTime;
147     int16_t                         SavedDecayHfRatio;
148     int16_t                         SavedReverbLevel;
149     int16_t                         SavedDiffusion;
150     int16_t                         SavedDensity;
151     bool                            bEnabled;
152     LVM_Fs_en                       SampleRate;
153     process_buffer_t                *InFrames;
154     process_buffer_t                *OutFrames;
155     size_t                          bufferSizeIn;
156     size_t                          bufferSizeOut;
157     bool                            auxiliary;
158     bool                            preset;
159     uint16_t                        curPreset;
160     uint16_t                        nextPreset;
161     int                             SamplesToExitCount;
162     LVM_INT16                       leftVolume;
163     LVM_INT16                       rightVolume;
164     LVM_INT16                       prevLeftVolume;
165     LVM_INT16                       prevRightVolume;
166     int                             volumeMode;
167 };
168 
169 enum {
170     REVERB_VOLUME_OFF,
171     REVERB_VOLUME_FLAT,
172     REVERB_VOLUME_RAMP,
173 };
174 
175 #define REVERB_DEFAULT_PRESET REVERB_PRESET_NONE
176 
177 #define REVERB_SEND_LEVEL   0.75f // 0.75 in 4.12 format
178 #define REVERB_UNIT_VOLUME  (0x1000) // 1.0 in 4.12 format
179 
180 //--- local function prototypes
181 int  Reverb_init            (ReverbContext *pContext);
182 void Reverb_free            (ReverbContext *pContext);
183 int  Reverb_setConfig       (ReverbContext *pContext, effect_config_t *pConfig);
184 void Reverb_getConfig       (ReverbContext *pContext, effect_config_t *pConfig);
185 int  Reverb_setParameter    (ReverbContext *pContext, void *pParam, void *pValue, int vsize);
186 int  Reverb_getParameter    (ReverbContext *pContext,
187                              void          *pParam,
188                              uint32_t      *pValueSize,
189                              void          *pValue);
190 int Reverb_LoadPreset       (ReverbContext   *pContext);
191 int Reverb_paramValueSize   (int32_t param);
192 
193 /* Effect Library Interface Implementation */
194 
EffectCreate(const effect_uuid_t * uuid,int32_t sessionId __unused,int32_t ioId __unused,effect_handle_t * pHandle)195 extern "C" int EffectCreate(const effect_uuid_t *uuid,
196                             int32_t             sessionId __unused,
197                             int32_t             ioId __unused,
198                             effect_handle_t  *pHandle){
199     int ret;
200     int i;
201     int length = sizeof(gDescriptors) / sizeof(const effect_descriptor_t *);
202     const effect_descriptor_t *desc;
203 
204     ALOGV("\t\nEffectCreate start");
205 
206     if (pHandle == NULL || uuid == NULL){
207         ALOGV("\tLVM_ERROR : EffectCreate() called with NULL pointer");
208         return -EINVAL;
209     }
210 
211     for (i = 0; i < length; i++) {
212         desc = gDescriptors[i];
213         if (memcmp(uuid, &desc->uuid, sizeof(effect_uuid_t))
214                 == 0) {
215             ALOGV("\tEffectCreate - UUID matched Reverb type %d, UUID = %x", i, desc->uuid.timeLow);
216             break;
217         }
218     }
219 
220     if (i == length) {
221         return -ENOENT;
222     }
223 
224     ReverbContext *pContext = new ReverbContext;
225 
226     pContext->itfe      = &gReverbInterface;
227     pContext->hInstance = NULL;
228 
229     pContext->auxiliary = false;
230     if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY){
231         pContext->auxiliary = true;
232         ALOGV("\tEffectCreate - AUX");
233     }else{
234         ALOGV("\tEffectCreate - INS");
235     }
236 
237     pContext->preset = false;
238     if (memcmp(&desc->type, SL_IID_PRESETREVERB, sizeof(effect_uuid_t)) == 0) {
239         pContext->preset = true;
240         // force reloading preset at first call to process()
241         pContext->curPreset = REVERB_PRESET_LAST + 1;
242         pContext->nextPreset = REVERB_DEFAULT_PRESET;
243         ALOGV("\tEffectCreate - PRESET");
244     }else{
245         ALOGV("\tEffectCreate - ENVIRONMENTAL");
246     }
247 
248     ALOGV("\tEffectCreate - Calling Reverb_init");
249     ret = Reverb_init(pContext);
250 
251     if (ret < 0){
252         ALOGV("\tLVM_ERROR : EffectCreate() init failed");
253         delete pContext;
254         return ret;
255     }
256 
257     *pHandle = (effect_handle_t)pContext;
258 
259 
260     int channels = audio_channel_count_from_out_mask(pContext->config.inputCfg.channels);
261 
262     // Allocate memory for reverb process (*2 is for STEREO)
263     pContext->bufferSizeIn = LVREV_MAX_FRAME_SIZE * sizeof(process_buffer_t) * channels;
264     pContext->bufferSizeOut = LVREV_MAX_FRAME_SIZE * sizeof(process_buffer_t) * FCC_2;
265     pContext->InFrames  = (process_buffer_t *)calloc(pContext->bufferSizeIn, 1 /* size */);
266     pContext->OutFrames = (process_buffer_t *)calloc(pContext->bufferSizeOut, 1 /* size */);
267 
268     ALOGV("\tEffectCreate %p, size %zu", pContext, sizeof(ReverbContext));
269     ALOGV("\tEffectCreate end\n");
270     return 0;
271 } /* end EffectCreate */
272 
EffectRelease(effect_handle_t handle)273 extern "C" int EffectRelease(effect_handle_t handle){
274     ReverbContext * pContext = (ReverbContext *)handle;
275 
276     ALOGV("\tEffectRelease %p", handle);
277     if (pContext == NULL){
278         ALOGV("\tLVM_ERROR : EffectRelease called with NULL pointer");
279         return -EINVAL;
280     }
281 
282     free(pContext->InFrames);
283     free(pContext->OutFrames);
284     pContext->bufferSizeIn = 0;
285     pContext->bufferSizeOut = 0;
286     Reverb_free(pContext);
287     delete pContext;
288     return 0;
289 } /* end EffectRelease */
290 
EffectGetDescriptor(const effect_uuid_t * uuid,effect_descriptor_t * pDescriptor)291 extern "C" int EffectGetDescriptor(const effect_uuid_t *uuid,
292                                    effect_descriptor_t *pDescriptor) {
293     int i;
294     int length = sizeof(gDescriptors) / sizeof(const effect_descriptor_t *);
295 
296     if (pDescriptor == NULL || uuid == NULL){
297         ALOGV("EffectGetDescriptor() called with NULL pointer");
298         return -EINVAL;
299     }
300 
301     for (i = 0; i < length; i++) {
302         if (memcmp(uuid, &gDescriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
303             *pDescriptor = *gDescriptors[i];
304             ALOGV("EffectGetDescriptor - UUID matched Reverb type %d, UUID = %x",
305                  i, gDescriptors[i]->uuid.timeLow);
306             return 0;
307         }
308     }
309 
310     return -EINVAL;
311 } /* end EffectGetDescriptor */
312 
313 /* local functions */
314 #define CHECK_ARG(cond) {                     \
315     if (!(cond)) {                            \
316         ALOGV("\tLVM_ERROR : Invalid argument: "#cond);      \
317         return -EINVAL;                       \
318     }                                         \
319 }
320 
321 //----------------------------------------------------------------------------
322 // process()
323 //----------------------------------------------------------------------------
324 // Purpose:
325 // Apply the Reverb
326 //
327 // Inputs:
328 //  pIn:        pointer to stereo/mono float or 16 bit input data
329 //  pOut:       pointer to stereo float or 16 bit output data
330 //  frameCount: Frames to process
331 //  pContext:   effect engine context
332 //  strength    strength to be applied
333 //
334 //  Outputs:
335 //  pOut:       pointer to updated stereo 16 bit output data
336 //
337 //----------------------------------------------------------------------------
process(effect_buffer_t * pIn,effect_buffer_t * pOut,int frameCount,ReverbContext * pContext)338 int process( effect_buffer_t   *pIn,
339              effect_buffer_t   *pOut,
340              int           frameCount,
341              ReverbContext *pContext){
342 
343     int channels = audio_channel_count_from_out_mask(pContext->config.inputCfg.channels);
344     LVREV_ReturnStatus_en   LvmStatus = LVREV_SUCCESS;              /* Function call status */
345 
346     // Check that the input is either mono or stereo
347     if (!(channels == 1 || channels == FCC_2) ) {
348         ALOGE("\tLVREV_ERROR : process invalid PCM format");
349         return -EINVAL;
350     }
351 
352     size_t inSize = frameCount * sizeof(process_buffer_t) * channels;
353     size_t outSize = frameCount * sizeof(process_buffer_t) * FCC_2;
354     if (pContext->InFrames == NULL ||
355             pContext->bufferSizeIn < inSize) {
356         free(pContext->InFrames);
357         pContext->bufferSizeIn = inSize;
358         pContext->InFrames = (process_buffer_t *)calloc(1, pContext->bufferSizeIn);
359     }
360     if (pContext->OutFrames == NULL ||
361             pContext->bufferSizeOut < outSize) {
362         free(pContext->OutFrames);
363         pContext->bufferSizeOut = outSize;
364         pContext->OutFrames = (process_buffer_t *)calloc(1, pContext->bufferSizeOut);
365     }
366 
367 
368     // Check for NULL pointers
369     if ((pContext->InFrames == NULL) || (pContext->OutFrames == NULL)) {
370         ALOGE("\tLVREV_ERROR : process failed to allocate memory for temporary buffers ");
371         return -EINVAL;
372     }
373 
374 
375     if (pContext->preset && pContext->nextPreset != pContext->curPreset) {
376         Reverb_LoadPreset(pContext);
377     }
378 
379     if (pContext->auxiliary) {
380         static_assert(std::is_same<decltype(*pIn), decltype(*pContext->InFrames)>::value,
381                 "pIn and InFrames must be same type");
382         memcpy(pContext->InFrames, pIn, frameCount * channels * sizeof(*pIn));
383         } else {
384         // insert reverb input is always stereo
385         for (int i = 0; i < frameCount; i++) {
386             pContext->InFrames[2 * i] = (process_buffer_t)pIn[2 * i] * REVERB_SEND_LEVEL;
387             pContext->InFrames[2 * i + 1] = (process_buffer_t)pIn[2 * i + 1] * REVERB_SEND_LEVEL;
388         }
389     }
390 
391     if (pContext->preset && pContext->curPreset == REVERB_PRESET_NONE) {
392         memset(pContext->OutFrames, 0,
393                 frameCount * sizeof(*pContext->OutFrames) * FCC_2); //always stereo here
394     } else {
395         if(pContext->bEnabled == LVM_FALSE && pContext->SamplesToExitCount > 0) {
396             memset(pContext->InFrames, 0,
397                     frameCount * sizeof(*pContext->OutFrames) * channels);
398             ALOGV("\tZeroing %d samples per frame at the end of call", channels);
399         }
400 
401         /* Process the samples, producing a stereo output */
402         LvmStatus = LVREV_Process(pContext->hInstance,      /* Instance handle */
403                                   pContext->InFrames,     /* Input buffer */
404                                   pContext->OutFrames,    /* Output buffer */
405                                   frameCount);              /* Number of samples to read */
406     }
407 
408     LVM_ERROR_CHECK(LvmStatus, "LVREV_Process", "process")
409     if(LvmStatus != LVREV_SUCCESS) return -EINVAL;
410 
411     // Convert to 16 bits
412     if (pContext->auxiliary) {
413         // nothing to do here
414     } else {
415         for (int i = 0; i < frameCount * FCC_2; i++) { // always stereo here
416             // Mix with dry input
417             pContext->OutFrames[i] += pIn[i];
418         }
419         // apply volume with ramp if needed
420         if ((pContext->leftVolume != pContext->prevLeftVolume ||
421                 pContext->rightVolume != pContext->prevRightVolume) &&
422                 pContext->volumeMode == REVERB_VOLUME_RAMP) {
423             // FIXME: still using int16 volumes.
424             // For reference: REVERB_UNIT_VOLUME  (0x1000) // 1.0 in 4.12 format
425             float vl = (float)pContext->prevLeftVolume / 4096;
426             float incl = (((float)pContext->leftVolume / 4096) - vl) / frameCount;
427             float vr = (float)pContext->prevRightVolume / 4096;
428             float incr = (((float)pContext->rightVolume / 4096) - vr) / frameCount;
429 
430             for (int i = 0; i < frameCount; i++) {
431                 pContext->OutFrames[FCC_2 * i] *= vl;
432                 pContext->OutFrames[FCC_2 * i + 1] *= vr;
433 
434                 vl += incl;
435                 vr += incr;
436             }
437             pContext->prevLeftVolume = pContext->leftVolume;
438             pContext->prevRightVolume = pContext->rightVolume;
439         } else if (pContext->volumeMode != REVERB_VOLUME_OFF) {
440             if (pContext->leftVolume != REVERB_UNIT_VOLUME ||
441                 pContext->rightVolume != REVERB_UNIT_VOLUME) {
442                 for (int i = 0; i < frameCount; i++) {
443                     pContext->OutFrames[FCC_2 * i] *= ((float)pContext->leftVolume / 4096);
444                     pContext->OutFrames[FCC_2 * i + 1] *= ((float)pContext->rightVolume / 4096);
445                 }
446             }
447             pContext->prevLeftVolume = pContext->leftVolume;
448             pContext->prevRightVolume = pContext->rightVolume;
449             pContext->volumeMode = REVERB_VOLUME_RAMP;
450         }
451     }
452 
453 
454     // Accumulate if required
455     if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE){
456         //ALOGV("\tBuffer access is ACCUMULATE");
457         for (int i = 0; i < frameCount * FCC_2; i++) { // always stereo here
458             pOut[i] += pContext->OutFrames[i];
459         }
460     }else{
461         //ALOGV("\tBuffer access is WRITE");
462         memcpy(pOut, pContext->OutFrames, frameCount * sizeof(*pOut) * FCC_2);
463     }
464 
465     return 0;
466 }    /* end process */
467 
468 //----------------------------------------------------------------------------
469 // Reverb_free()
470 //----------------------------------------------------------------------------
471 // Purpose: Free all memory associated with the Bundle.
472 //
473 // Inputs:
474 //  pContext:   effect engine context
475 //
476 // Outputs:
477 //
478 //----------------------------------------------------------------------------
479 
Reverb_free(ReverbContext * pContext)480 void Reverb_free(ReverbContext *pContext){
481 
482     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;         /* Function call status */
483     LVREV_MemoryTable_st      MemTab;
484 
485     /* Free the algorithm memory */
486     LvmStatus = LVREV_GetMemoryTable(pContext->hInstance,
487                                    &MemTab,
488                                    LVM_NULL);
489 
490     LVM_ERROR_CHECK(LvmStatus, "LVM_GetMemoryTable", "Reverb_free")
491 
492     for (int i=0; i<LVM_NR_MEMORY_REGIONS; i++){
493         if (MemTab.Region[i].Size != 0){
494             if (MemTab.Region[i].pBaseAddress != NULL){
495                 free(MemTab.Region[i].pBaseAddress);
496             }else{
497                 ALOGV("\tLVM_ERROR : free() - trying to free with NULL pointer %" PRIu32 " bytes "
498                         "for region %u at %p ERROR\n",
499                         MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
500             }
501         }
502     }
503 }    /* end Reverb_free */
504 
505 //----------------------------------------------------------------------------
506 // Reverb_setConfig()
507 //----------------------------------------------------------------------------
508 // Purpose: Set input and output audio configuration.
509 //
510 // Inputs:
511 //  pContext:   effect engine context
512 //  pConfig:    pointer to effect_config_t structure holding input and output
513 //      configuration parameters
514 //
515 // Outputs:
516 //
517 //----------------------------------------------------------------------------
518 
Reverb_setConfig(ReverbContext * pContext,effect_config_t * pConfig)519 int Reverb_setConfig(ReverbContext *pContext, effect_config_t *pConfig){
520     LVM_Fs_en   SampleRate;
521     //ALOGV("\tReverb_setConfig start");
522 
523     CHECK_ARG(pContext != NULL);
524     CHECK_ARG(pConfig != NULL);
525 
526     CHECK_ARG(pConfig->inputCfg.samplingRate == pConfig->outputCfg.samplingRate);
527     CHECK_ARG(pConfig->inputCfg.format == pConfig->outputCfg.format);
528     CHECK_ARG((pContext->auxiliary && pConfig->inputCfg.channels == AUDIO_CHANNEL_OUT_MONO) ||
529               ((!pContext->auxiliary) && pConfig->inputCfg.channels == AUDIO_CHANNEL_OUT_STEREO));
530     CHECK_ARG(pConfig->outputCfg.channels == AUDIO_CHANNEL_OUT_STEREO);
531     CHECK_ARG(pConfig->outputCfg.accessMode == EFFECT_BUFFER_ACCESS_WRITE
532               || pConfig->outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE);
533     CHECK_ARG(pConfig->inputCfg.format == EFFECT_BUFFER_FORMAT);
534     //ALOGV("\tReverb_setConfig calling memcpy");
535     pContext->config = *pConfig;
536 
537     switch (pConfig->inputCfg.samplingRate) {
538     case 8000:
539         SampleRate = LVM_FS_8000;
540         break;
541     case 16000:
542         SampleRate = LVM_FS_16000;
543         break;
544     case 22050:
545         SampleRate = LVM_FS_22050;
546         break;
547     case 32000:
548         SampleRate = LVM_FS_32000;
549         break;
550     case 44100:
551         SampleRate = LVM_FS_44100;
552         break;
553     case 48000:
554         SampleRate = LVM_FS_48000;
555         break;
556     case 88200:
557         SampleRate = LVM_FS_88200;
558         break;
559     case 96000:
560         SampleRate = LVM_FS_96000;
561         break;
562     case 176400:
563         SampleRate = LVM_FS_176400;
564         break;
565     case 192000:
566         SampleRate = LVM_FS_192000;
567         break;
568     default:
569         ALOGV("\rReverb_setConfig invalid sampling rate %d", pConfig->inputCfg.samplingRate);
570         return -EINVAL;
571     }
572 
573     if (pContext->SampleRate != SampleRate) {
574 
575         LVREV_ControlParams_st    ActiveParams;
576         LVREV_ReturnStatus_en     LvmStatus = LVREV_SUCCESS;
577 
578         //ALOGV("\tReverb_setConfig change sampling rate to %d", SampleRate);
579 
580         /* Get the current settings */
581         LvmStatus = LVREV_GetControlParameters(pContext->hInstance,
582                                          &ActiveParams);
583 
584         LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "Reverb_setConfig")
585         if(LvmStatus != LVREV_SUCCESS) return -EINVAL;
586 
587         ActiveParams.SampleRate = SampleRate;
588 
589         LvmStatus = LVREV_SetControlParameters(pContext->hInstance, &ActiveParams);
590 
591         LVM_ERROR_CHECK(LvmStatus, "LVREV_SetControlParameters", "Reverb_setConfig")
592         if(LvmStatus != LVREV_SUCCESS) return -EINVAL;
593         //ALOGV("\tReverb_setConfig Succesfully called LVREV_SetControlParameters\n");
594         pContext->SampleRate = SampleRate;
595     }else{
596         //ALOGV("\tReverb_setConfig keep sampling rate at %d", SampleRate);
597     }
598 
599     //ALOGV("\tReverb_setConfig End");
600     return 0;
601 }   /* end Reverb_setConfig */
602 
603 //----------------------------------------------------------------------------
604 // Reverb_getConfig()
605 //----------------------------------------------------------------------------
606 // Purpose: Get input and output audio configuration.
607 //
608 // Inputs:
609 //  pContext:   effect engine context
610 //  pConfig:    pointer to effect_config_t structure holding input and output
611 //      configuration parameters
612 //
613 // Outputs:
614 //
615 //----------------------------------------------------------------------------
616 
Reverb_getConfig(ReverbContext * pContext,effect_config_t * pConfig)617 void Reverb_getConfig(ReverbContext *pContext, effect_config_t *pConfig)
618 {
619     *pConfig = pContext->config;
620 }   /* end Reverb_getConfig */
621 
622 //----------------------------------------------------------------------------
623 // Reverb_init()
624 //----------------------------------------------------------------------------
625 // Purpose: Initialize engine with default configuration
626 //
627 // Inputs:
628 //  pContext:   effect engine context
629 //
630 // Outputs:
631 //
632 //----------------------------------------------------------------------------
633 
Reverb_init(ReverbContext * pContext)634 int Reverb_init(ReverbContext *pContext){
635     ALOGV("\tReverb_init start");
636 
637     CHECK_ARG(pContext != NULL);
638 
639     if (pContext->hInstance != NULL){
640         Reverb_free(pContext);
641     }
642 
643     pContext->config.inputCfg.accessMode                    = EFFECT_BUFFER_ACCESS_READ;
644     if (pContext->auxiliary) {
645         pContext->config.inputCfg.channels                  = AUDIO_CHANNEL_OUT_MONO;
646     } else {
647         pContext->config.inputCfg.channels                  = AUDIO_CHANNEL_OUT_STEREO;
648     }
649     pContext->config.inputCfg.format                        = EFFECT_BUFFER_FORMAT;
650     pContext->config.inputCfg.samplingRate                  = 44100;
651     pContext->config.inputCfg.bufferProvider.getBuffer      = NULL;
652     pContext->config.inputCfg.bufferProvider.releaseBuffer  = NULL;
653     pContext->config.inputCfg.bufferProvider.cookie         = NULL;
654     pContext->config.inputCfg.mask                          = EFFECT_CONFIG_ALL;
655     pContext->config.outputCfg.accessMode                   = EFFECT_BUFFER_ACCESS_ACCUMULATE;
656     pContext->config.outputCfg.channels                     = AUDIO_CHANNEL_OUT_STEREO;
657     pContext->config.outputCfg.format                       = EFFECT_BUFFER_FORMAT;
658     pContext->config.outputCfg.samplingRate                 = 44100;
659     pContext->config.outputCfg.bufferProvider.getBuffer     = NULL;
660     pContext->config.outputCfg.bufferProvider.releaseBuffer = NULL;
661     pContext->config.outputCfg.bufferProvider.cookie        = NULL;
662     pContext->config.outputCfg.mask                         = EFFECT_CONFIG_ALL;
663 
664     pContext->leftVolume = REVERB_UNIT_VOLUME;
665     pContext->rightVolume = REVERB_UNIT_VOLUME;
666     pContext->prevLeftVolume = REVERB_UNIT_VOLUME;
667     pContext->prevRightVolume = REVERB_UNIT_VOLUME;
668     pContext->volumeMode = REVERB_VOLUME_FLAT;
669 
670     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;        /* Function call status */
671     LVREV_ControlParams_st    params;                         /* Control Parameters */
672     LVREV_InstanceParams_st   InstParams;                     /* Instance parameters */
673     LVREV_MemoryTable_st      MemTab;                         /* Memory allocation table */
674     bool                      bMallocFailure = LVM_FALSE;
675 
676     /* Set the capabilities */
677     InstParams.MaxBlockSize  = MAX_CALL_SIZE;
678     InstParams.SourceFormat  = LVM_STEREO;          // Max format, could be mono during process
679     InstParams.NumDelays     = LVREV_DELAYLINES_4;
680 
681     /* Allocate memory, forcing alignment */
682     LvmStatus = LVREV_GetMemoryTable(LVM_NULL,
683                                   &MemTab,
684                                   &InstParams);
685 
686     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetMemoryTable", "Reverb_init")
687     if(LvmStatus != LVREV_SUCCESS) return -EINVAL;
688 
689     ALOGV("\tCreateInstance Succesfully called LVM_GetMemoryTable\n");
690 
691     /* Allocate memory */
692     for (int i=0; i<LVM_NR_MEMORY_REGIONS; i++){
693         if (MemTab.Region[i].Size != 0){
694             MemTab.Region[i].pBaseAddress = malloc(MemTab.Region[i].Size);
695 
696             if (MemTab.Region[i].pBaseAddress == LVM_NULL){
697                 ALOGV("\tLVREV_ERROR :Reverb_init CreateInstance Failed to allocate %" PRIu32
698                         " bytes for region %u\n", MemTab.Region[i].Size, i );
699                 bMallocFailure = LVM_TRUE;
700             }else{
701                 ALOGV("\tReverb_init CreateInstance allocate %" PRIu32
702                         " bytes for region %u at %p\n",
703                         MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
704             }
705         }
706     }
707 
708     /* If one or more of the memory regions failed to allocate, free the regions that were
709      * succesfully allocated and return with an error
710      */
711     if(bMallocFailure == LVM_TRUE){
712         for (int i=0; i<LVM_NR_MEMORY_REGIONS; i++){
713             if (MemTab.Region[i].pBaseAddress == LVM_NULL){
714                 ALOGV("\tLVM_ERROR :Reverb_init CreateInstance Failed to allocate %" PRIu32
715                         " bytes for region %u - Not freeing\n", MemTab.Region[i].Size, i );
716             }else{
717                 ALOGV("\tLVM_ERROR :Reverb_init CreateInstance Failed: but allocated %" PRIu32
718                         " bytes for region %u at %p- free\n",
719                         MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
720                 free(MemTab.Region[i].pBaseAddress);
721             }
722         }
723         return -EINVAL;
724     }
725     ALOGV("\tReverb_init CreateInstance Succesfully malloc'd memory\n");
726 
727     /* Initialise */
728     pContext->hInstance = LVM_NULL;
729 
730     /* Init sets the instance handle */
731     LvmStatus = LVREV_GetInstanceHandle(&pContext->hInstance,
732                                         &MemTab,
733                                         &InstParams);
734 
735     LVM_ERROR_CHECK(LvmStatus, "LVM_GetInstanceHandle", "Reverb_init")
736     if(LvmStatus != LVREV_SUCCESS) return -EINVAL;
737 
738     ALOGV("\tReverb_init CreateInstance Succesfully called LVM_GetInstanceHandle\n");
739 
740     /* Set the initial process parameters */
741     /* General parameters */
742     params.OperatingMode  = LVM_MODE_ON;
743     params.SampleRate     = LVM_FS_44100;
744     pContext->SampleRate  = LVM_FS_44100;
745 
746     if(pContext->config.inputCfg.channels == AUDIO_CHANNEL_OUT_MONO){
747         params.SourceFormat   = LVM_MONO;
748     } else {
749         params.SourceFormat   = LVM_STEREO;
750     }
751 
752     /* Reverb parameters */
753     params.Level          = 0;
754     params.LPF            = 23999;
755     params.HPF            = 50;
756     params.T60            = 1490;
757     params.Density        = 100;
758     params.Damping        = 21;
759     params.RoomSize       = 100;
760 
761     pContext->SamplesToExitCount = (params.T60 * pContext->config.inputCfg.samplingRate)/1000;
762 
763     /* Saved strength is used to return the exact strength that was used in the set to the get
764      * because we map the original strength range of 0:1000 to 1:15, and this will avoid
765      * quantisation like effect when returning
766      */
767     pContext->SavedRoomLevel    = -6000;
768     pContext->SavedHfLevel      = 0;
769     pContext->bEnabled          = LVM_FALSE;
770     pContext->SavedDecayTime    = params.T60;
771     pContext->SavedDecayHfRatio = params.Damping*20;
772     pContext->SavedDensity      = params.RoomSize*10;
773     pContext->SavedDiffusion    = params.Density*10;
774     pContext->SavedReverbLevel  = -6000;
775 
776     /* Activate the initial settings */
777     LvmStatus = LVREV_SetControlParameters(pContext->hInstance,
778                                          &params);
779 
780     LVM_ERROR_CHECK(LvmStatus, "LVREV_SetControlParameters", "Reverb_init")
781     if(LvmStatus != LVREV_SUCCESS) return -EINVAL;
782 
783     ALOGV("\tReverb_init CreateInstance Succesfully called LVREV_SetControlParameters\n");
784     ALOGV("\tReverb_init End");
785     return 0;
786 }   /* end Reverb_init */
787 
788 //----------------------------------------------------------------------------
789 // ReverbConvertLevel()
790 //----------------------------------------------------------------------------
791 // Purpose:
792 // Convert level from OpenSL ES format to LVM format
793 //
794 // Inputs:
795 //  level       level to be applied
796 //
797 //----------------------------------------------------------------------------
798 
ReverbConvertLevel(int16_t level)799 int16_t ReverbConvertLevel(int16_t level){
800     static int16_t LevelArray[101] =
801     {
802        -12000, -4000,  -3398,  -3046,  -2796,  -2603,  -2444,  -2310,  -2194,  -2092,
803        -2000,  -1918,  -1842,  -1773,  -1708,  -1648,  -1592,  -1540,  -1490,  -1443,
804        -1398,  -1356,  -1316,  -1277,  -1240,  -1205,  -1171,  -1138,  -1106,  -1076,
805        -1046,  -1018,  -990,   -963,   -938,   -912,   -888,   -864,   -841,   -818,
806        -796,   -775,   -754,   -734,   -714,   -694,   -675,   -656,   -638,   -620,
807        -603,   -585,   -568,   -552,   -536,   -520,   -504,   -489,   -474,   -459,
808        -444,   -430,   -416,   -402,   -388,   -375,   -361,   -348,   -335,   -323,
809        -310,   -298,   -286,   -274,   -262,   -250,   -239,   -228,   -216,   -205,
810        -194,   -184,   -173,   -162,   -152,   -142,   -132,   -121,   -112,   -102,
811        -92,    -82,    -73,    -64,    -54,    -45,    -36,    -27,    -18,    -9,
812        0
813     };
814     int16_t i;
815 
816     for(i = 0; i < 101; i++)
817     {
818        if(level <= LevelArray[i])
819            break;
820     }
821     return i;
822 }
823 
824 //----------------------------------------------------------------------------
825 // ReverbConvertHFLevel()
826 //----------------------------------------------------------------------------
827 // Purpose:
828 // Convert level from OpenSL ES format to LVM format
829 //
830 // Inputs:
831 //  level       level to be applied
832 //
833 //----------------------------------------------------------------------------
834 
ReverbConvertHfLevel(int16_t Hflevel)835 int16_t ReverbConvertHfLevel(int16_t Hflevel){
836     int16_t i;
837 
838     static LPFPair_t LPFArray[97] =
839     {   // Limit range to 50 for LVREV parameter range
840         {-10000, 50}, { -5000, 50 }, { -4000, 50},  { -3000, 158}, { -2000, 502},
841         {-1000, 1666},{ -900, 1897}, { -800, 2169}, { -700, 2496}, { -600, 2895},
842         {-500, 3400}, { -400, 4066}, { -300, 5011}, { -200, 6537}, { -100,  9826},
843         {-99, 9881 }, { -98, 9937 }, { -97, 9994 }, { -96, 10052}, { -95, 10111},
844         {-94, 10171}, { -93, 10231}, { -92, 10293}, { -91, 10356}, { -90, 10419},
845         {-89, 10484}, { -88, 10549}, { -87, 10616}, { -86, 10684}, { -85, 10753},
846         {-84, 10823}, { -83, 10895}, { -82, 10968}, { -81, 11042}, { -80, 11117},
847         {-79, 11194}, { -78, 11272}, { -77, 11352}, { -76, 11433}, { -75, 11516},
848         {-74, 11600}, { -73, 11686}, { -72, 11774}, { -71, 11864}, { -70, 11955},
849         {-69, 12049}, { -68, 12144}, { -67, 12242}, { -66, 12341}, { -65, 12443},
850         {-64, 12548}, { -63, 12654}, { -62, 12763}, { -61, 12875}, { -60, 12990},
851         {-59, 13107}, { -58, 13227}, { -57, 13351}, { -56, 13477}, { -55, 13607},
852         {-54, 13741}, { -53, 13878}, { -52, 14019}, { -51, 14164}, { -50, 14313},
853         {-49, 14467}, { -48, 14626}, { -47, 14789}, { -46, 14958}, { -45, 15132},
854         {-44, 15312}, { -43, 15498}, { -42, 15691}, { -41, 15890}, { -40, 16097},
855         {-39, 16311}, { -38, 16534}, { -37, 16766}, { -36, 17007}, { -35, 17259},
856         {-34, 17521}, { -33, 17795}, { -32, 18081}, { -31, 18381}, { -30, 18696},
857         {-29, 19027}, { -28, 19375}, { -27, 19742}, { -26, 20129}, { -25, 20540},
858         {-24, 20976}, { -23, 21439}, { -22, 21934}, { -21, 22463}, { -20, 23031},
859         {-19, 23643}, { -18, 23999}
860     };
861 
862     for(i = 0; i < 96; i++)
863     {
864         if(Hflevel <= LPFArray[i].Room_HF)
865             break;
866     }
867     return LPFArray[i].LPF;
868 }
869 
870 //----------------------------------------------------------------------------
871 // ReverbSetRoomHfLevel()
872 //----------------------------------------------------------------------------
873 // Purpose:
874 // Apply the HF level to the Reverb. Must first be converted to LVM format
875 //
876 // Inputs:
877 //  pContext:   effect engine context
878 //  level       level to be applied
879 //
880 //----------------------------------------------------------------------------
881 
ReverbSetRoomHfLevel(ReverbContext * pContext,int16_t level)882 void ReverbSetRoomHfLevel(ReverbContext *pContext, int16_t level){
883     //ALOGV("\tReverbSetRoomHfLevel start (%d)", level);
884 
885     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
886     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
887 
888     /* Get the current settings */
889     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
890     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbSetRoomHfLevel")
891     //ALOGV("\tReverbSetRoomHfLevel Succesfully returned from LVM_GetControlParameters\n");
892     //ALOGV("\tReverbSetRoomHfLevel() just Got -> %d\n", ActiveParams.LPF);
893 
894     ActiveParams.LPF = ReverbConvertHfLevel(level);
895 
896     /* Activate the initial settings */
897     LvmStatus = LVREV_SetControlParameters(pContext->hInstance, &ActiveParams);
898     LVM_ERROR_CHECK(LvmStatus, "LVREV_SetControlParameters", "ReverbSetRoomHfLevel")
899     //ALOGV("\tReverbSetRoomhfLevel() just Set -> %d\n", ActiveParams.LPF);
900     pContext->SavedHfLevel = level;
901     //ALOGV("\tReverbSetHfRoomLevel end.. saving %d", pContext->SavedHfLevel);
902     return;
903 }
904 
905 //----------------------------------------------------------------------------
906 // ReverbGetRoomHfLevel()
907 //----------------------------------------------------------------------------
908 // Purpose:
909 // Get the level applied to the Revervb. Must first be converted to LVM format
910 //
911 // Inputs:
912 //  pContext:   effect engine context
913 //
914 //----------------------------------------------------------------------------
915 
ReverbGetRoomHfLevel(ReverbContext * pContext)916 int16_t ReverbGetRoomHfLevel(ReverbContext *pContext){
917     int16_t level;
918     //ALOGV("\tReverbGetRoomHfLevel start, saved level is %d", pContext->SavedHfLevel);
919 
920     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
921     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
922 
923     /* Get the current settings */
924     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
925     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbGetRoomHfLevel")
926     //ALOGV("\tReverbGetRoomHfLevel Succesfully returned from LVM_GetControlParameters\n");
927     //ALOGV("\tReverbGetRoomHfLevel() just Got -> %d\n", ActiveParams.LPF);
928 
929     level = ReverbConvertHfLevel(pContext->SavedHfLevel);
930 
931     //ALOGV("\tReverbGetRoomHfLevel() ActiveParams.LPFL %d, pContext->SavedHfLevel: %d, "
932     //     "converted level: %d\n", ActiveParams.LPF, pContext->SavedHfLevel, level);
933 
934     if((int16_t)ActiveParams.LPF != level){
935         ALOGV("\tLVM_ERROR : (ignore at start up) ReverbGetRoomHfLevel() has wrong level -> %d %d\n",
936                ActiveParams.Level, level);
937     }
938 
939     //ALOGV("\tReverbGetRoomHfLevel end");
940     return pContext->SavedHfLevel;
941 }
942 
943 //----------------------------------------------------------------------------
944 // ReverbSetReverbLevel()
945 //----------------------------------------------------------------------------
946 // Purpose:
947 // Apply the level to the Reverb. Must first be converted to LVM format
948 //
949 // Inputs:
950 //  pContext:   effect engine context
951 //  level       level to be applied
952 //
953 //----------------------------------------------------------------------------
954 
ReverbSetReverbLevel(ReverbContext * pContext,int16_t level)955 void ReverbSetReverbLevel(ReverbContext *pContext, int16_t level){
956     //ALOGV("\n\tReverbSetReverbLevel start (%d)", level);
957 
958     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
959     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
960     LVM_INT32                 CombinedLevel;             // Sum of room and reverb level controls
961 
962     /* Get the current settings */
963     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
964     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbSetReverbLevel")
965     //ALOGV("\tReverbSetReverbLevel Succesfully returned from LVM_GetControlParameters\n");
966     //ALOGV("\tReverbSetReverbLevel just Got -> %d\n", ActiveParams.Level);
967 
968     // needs to subtract max levels for both RoomLevel and ReverbLevel
969     CombinedLevel = (level + pContext->SavedRoomLevel)-LVREV_MAX_REVERB_LEVEL;
970     //ALOGV("\tReverbSetReverbLevel() CombinedLevel is %d = %d + %d\n",
971     //      CombinedLevel, level, pContext->SavedRoomLevel);
972 
973     ActiveParams.Level = ReverbConvertLevel(CombinedLevel);
974 
975     //ALOGV("\tReverbSetReverbLevel() Trying to set -> %d\n", ActiveParams.Level);
976 
977     /* Activate the initial settings */
978     LvmStatus = LVREV_SetControlParameters(pContext->hInstance, &ActiveParams);
979     LVM_ERROR_CHECK(LvmStatus, "LVREV_SetControlParameters", "ReverbSetReverbLevel")
980     //ALOGV("\tReverbSetReverbLevel() just Set -> %d\n", ActiveParams.Level);
981 
982     pContext->SavedReverbLevel = level;
983     //ALOGV("\tReverbSetReverbLevel end pContext->SavedReverbLevel is %d\n\n",
984     //     pContext->SavedReverbLevel);
985     return;
986 }
987 
988 //----------------------------------------------------------------------------
989 // ReverbGetReverbLevel()
990 //----------------------------------------------------------------------------
991 // Purpose:
992 // Get the level applied to the Revervb. Must first be converted to LVM format
993 //
994 // Inputs:
995 //  pContext:   effect engine context
996 //
997 //----------------------------------------------------------------------------
998 
ReverbGetReverbLevel(ReverbContext * pContext)999 int16_t ReverbGetReverbLevel(ReverbContext *pContext){
1000     int16_t level;
1001     //ALOGV("\tReverbGetReverbLevel start");
1002 
1003     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1004     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
1005     LVM_INT32                 CombinedLevel;             // Sum of room and reverb level controls
1006 
1007     /* Get the current settings */
1008     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1009     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbGetReverbLevel")
1010     //ALOGV("\tReverbGetReverbLevel Succesfully returned from LVM_GetControlParameters\n");
1011     //ALOGV("\tReverbGetReverbLevel() just Got -> %d\n", ActiveParams.Level);
1012 
1013     // needs to subtract max levels for both RoomLevel and ReverbLevel
1014     CombinedLevel = (pContext->SavedReverbLevel + pContext->SavedRoomLevel)-LVREV_MAX_REVERB_LEVEL;
1015 
1016     //ALOGV("\tReverbGetReverbLevel() CombinedLevel is %d = %d + %d\n",
1017     //CombinedLevel, pContext->SavedReverbLevel, pContext->SavedRoomLevel);
1018     level = ReverbConvertLevel(CombinedLevel);
1019 
1020     //ALOGV("\tReverbGetReverbLevel(): ActiveParams.Level: %d, pContext->SavedReverbLevel: %d, "
1021     //"pContext->SavedRoomLevel: %d, CombinedLevel: %d, converted level: %d\n",
1022     //ActiveParams.Level, pContext->SavedReverbLevel,pContext->SavedRoomLevel, CombinedLevel,level);
1023 
1024     if(ActiveParams.Level != level){
1025         ALOGV("\tLVM_ERROR : (ignore at start up) ReverbGetReverbLevel() has wrong level -> %d %d\n",
1026                 ActiveParams.Level, level);
1027     }
1028 
1029     //ALOGV("\tReverbGetReverbLevel end\n");
1030 
1031     return pContext->SavedReverbLevel;
1032 }
1033 
1034 //----------------------------------------------------------------------------
1035 // ReverbSetRoomLevel()
1036 //----------------------------------------------------------------------------
1037 // Purpose:
1038 // Apply the level to the Reverb. Must first be converted to LVM format
1039 //
1040 // Inputs:
1041 //  pContext:   effect engine context
1042 //  level       level to be applied
1043 //
1044 //----------------------------------------------------------------------------
1045 
ReverbSetRoomLevel(ReverbContext * pContext,int16_t level)1046 void ReverbSetRoomLevel(ReverbContext *pContext, int16_t level){
1047     //ALOGV("\tReverbSetRoomLevel start (%d)", level);
1048 
1049     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1050     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
1051     LVM_INT32                 CombinedLevel;             // Sum of room and reverb level controls
1052 
1053     /* Get the current settings */
1054     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1055     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbSetRoomLevel")
1056     //ALOGV("\tReverbSetRoomLevel Succesfully returned from LVM_GetControlParameters\n");
1057     //ALOGV("\tReverbSetRoomLevel() just Got -> %d\n", ActiveParams.Level);
1058 
1059     // needs to subtract max levels for both RoomLevel and ReverbLevel
1060     CombinedLevel = (level + pContext->SavedReverbLevel)-LVREV_MAX_REVERB_LEVEL;
1061     ActiveParams.Level = ReverbConvertLevel(CombinedLevel);
1062 
1063     /* Activate the initial settings */
1064     LvmStatus = LVREV_SetControlParameters(pContext->hInstance, &ActiveParams);
1065     LVM_ERROR_CHECK(LvmStatus, "LVREV_SetControlParameters", "ReverbSetRoomLevel")
1066     //ALOGV("\tReverbSetRoomLevel() just Set -> %d\n", ActiveParams.Level);
1067 
1068     pContext->SavedRoomLevel = level;
1069     //ALOGV("\tReverbSetRoomLevel end");
1070     return;
1071 }
1072 
1073 //----------------------------------------------------------------------------
1074 // ReverbGetRoomLevel()
1075 //----------------------------------------------------------------------------
1076 // Purpose:
1077 // Get the level applied to the Revervb. Must first be converted to LVM format
1078 //
1079 // Inputs:
1080 //  pContext:   effect engine context
1081 //
1082 //----------------------------------------------------------------------------
1083 
ReverbGetRoomLevel(ReverbContext * pContext)1084 int16_t ReverbGetRoomLevel(ReverbContext *pContext){
1085     int16_t level;
1086     //ALOGV("\tReverbGetRoomLevel start");
1087 
1088     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1089     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
1090     LVM_INT32                 CombinedLevel;             // Sum of room and reverb level controls
1091 
1092     /* Get the current settings */
1093     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1094     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbGetRoomLevel")
1095     //ALOGV("\tReverbGetRoomLevel Succesfully returned from LVM_GetControlParameters\n");
1096     //ALOGV("\tReverbGetRoomLevel() just Got -> %d\n", ActiveParams.Level);
1097 
1098     // needs to subtract max levels for both RoomLevel and ReverbLevel
1099     CombinedLevel = (pContext->SavedRoomLevel + pContext->SavedReverbLevel-LVREV_MAX_REVERB_LEVEL);
1100     level = ReverbConvertLevel(CombinedLevel);
1101 
1102     //ALOGV("\tReverbGetRoomLevel, Level = %d, pContext->SavedRoomLevel = %d, "
1103     //     "pContext->SavedReverbLevel = %d, CombinedLevel = %d, level = %d",
1104     //     ActiveParams.Level, pContext->SavedRoomLevel,
1105     //     pContext->SavedReverbLevel, CombinedLevel, level);
1106 
1107     if(ActiveParams.Level != level){
1108         ALOGV("\tLVM_ERROR : (ignore at start up) ReverbGetRoomLevel() has wrong level -> %d %d\n",
1109               ActiveParams.Level, level);
1110     }
1111 
1112     //ALOGV("\tReverbGetRoomLevel end");
1113     return pContext->SavedRoomLevel;
1114 }
1115 
1116 //----------------------------------------------------------------------------
1117 // ReverbSetDecayTime()
1118 //----------------------------------------------------------------------------
1119 // Purpose:
1120 // Apply the decay time to the Reverb.
1121 //
1122 // Inputs:
1123 //  pContext:   effect engine context
1124 //  time        decay to be applied
1125 //
1126 //----------------------------------------------------------------------------
1127 
ReverbSetDecayTime(ReverbContext * pContext,uint32_t time)1128 void ReverbSetDecayTime(ReverbContext *pContext, uint32_t time){
1129     //ALOGV("\tReverbSetDecayTime start (%d)", time);
1130 
1131     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1132     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
1133 
1134     /* Get the current settings */
1135     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1136     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbSetDecayTime")
1137     //ALOGV("\tReverbSetDecayTime Succesfully returned from LVM_GetControlParameters\n");
1138     //ALOGV("\tReverbSetDecayTime() just Got -> %d\n", ActiveParams.T60);
1139 
1140     if (time <= LVREV_MAX_T60) {
1141         ActiveParams.T60 = (LVM_UINT16)time;
1142     }
1143     else {
1144         ActiveParams.T60 = LVREV_MAX_T60;
1145     }
1146 
1147     /* Activate the initial settings */
1148     LvmStatus = LVREV_SetControlParameters(pContext->hInstance, &ActiveParams);
1149     LVM_ERROR_CHECK(LvmStatus, "LVREV_SetControlParameters", "ReverbSetDecayTime")
1150     //ALOGV("\tReverbSetDecayTime() just Set -> %d\n", ActiveParams.T60);
1151 
1152     pContext->SamplesToExitCount = (ActiveParams.T60 * pContext->config.inputCfg.samplingRate)/1000;
1153     //ALOGV("\tReverbSetDecayTime() just Set SamplesToExitCount-> %d\n",pContext->SamplesToExitCount);
1154     pContext->SavedDecayTime = (int16_t)time;
1155     //ALOGV("\tReverbSetDecayTime end");
1156     return;
1157 }
1158 
1159 //----------------------------------------------------------------------------
1160 // ReverbGetDecayTime()
1161 //----------------------------------------------------------------------------
1162 // Purpose:
1163 // Get the decay time applied to the Revervb.
1164 //
1165 // Inputs:
1166 //  pContext:   effect engine context
1167 //
1168 //----------------------------------------------------------------------------
1169 
ReverbGetDecayTime(ReverbContext * pContext)1170 uint32_t ReverbGetDecayTime(ReverbContext *pContext){
1171     //ALOGV("\tReverbGetDecayTime start");
1172 
1173     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1174     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
1175 
1176     /* Get the current settings */
1177     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1178     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbGetDecayTime")
1179     //ALOGV("\tReverbGetDecayTime Succesfully returned from LVM_GetControlParameters\n");
1180     //ALOGV("\tReverbGetDecayTime() just Got -> %d\n", ActiveParams.T60);
1181 
1182     if(ActiveParams.T60 != pContext->SavedDecayTime){
1183         // This will fail if the decay time is set to more than 7000
1184         ALOGV("\tLVM_ERROR : ReverbGetDecayTime() has wrong level -> %d %d\n",
1185          ActiveParams.T60, pContext->SavedDecayTime);
1186     }
1187 
1188     //ALOGV("\tReverbGetDecayTime end");
1189     return (uint32_t)ActiveParams.T60;
1190 }
1191 
1192 //----------------------------------------------------------------------------
1193 // ReverbSetDecayHfRatio()
1194 //----------------------------------------------------------------------------
1195 // Purpose:
1196 // Apply the HF decay ratio to the Reverb.
1197 //
1198 // Inputs:
1199 //  pContext:   effect engine context
1200 //  ratio       ratio to be applied
1201 //
1202 //----------------------------------------------------------------------------
1203 
ReverbSetDecayHfRatio(ReverbContext * pContext,int16_t ratio)1204 void ReverbSetDecayHfRatio(ReverbContext *pContext, int16_t ratio){
1205     //ALOGV("\tReverbSetDecayHfRatioe start (%d)", ratio);
1206 
1207     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1208     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;   /* Function call status */
1209 
1210     /* Get the current settings */
1211     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1212     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbSetDecayHfRatio")
1213     //ALOGV("\tReverbSetDecayHfRatio Succesfully returned from LVM_GetControlParameters\n");
1214     //ALOGV("\tReverbSetDecayHfRatio() just Got -> %d\n", ActiveParams.Damping);
1215 
1216     ActiveParams.Damping = (LVM_INT16)(ratio/20);
1217 
1218     /* Activate the initial settings */
1219     LvmStatus = LVREV_SetControlParameters(pContext->hInstance, &ActiveParams);
1220     LVM_ERROR_CHECK(LvmStatus, "LVREV_SetControlParameters", "ReverbSetDecayHfRatio")
1221     //ALOGV("\tReverbSetDecayHfRatio() just Set -> %d\n", ActiveParams.Damping);
1222 
1223     pContext->SavedDecayHfRatio = ratio;
1224     //ALOGV("\tReverbSetDecayHfRatio end");
1225     return;
1226 }
1227 
1228 //----------------------------------------------------------------------------
1229 // ReverbGetDecayHfRatio()
1230 //----------------------------------------------------------------------------
1231 // Purpose:
1232 // Get the HF decay ratio applied to the Revervb.
1233 //
1234 // Inputs:
1235 //  pContext:   effect engine context
1236 //
1237 //----------------------------------------------------------------------------
1238 
ReverbGetDecayHfRatio(ReverbContext * pContext)1239 int32_t ReverbGetDecayHfRatio(ReverbContext *pContext){
1240     //ALOGV("\tReverbGetDecayHfRatio start");
1241 
1242     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1243     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;   /* Function call status */
1244 
1245     /* Get the current settings */
1246     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1247     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbGetDecayHfRatio")
1248     //ALOGV("\tReverbGetDecayHfRatio Succesfully returned from LVM_GetControlParameters\n");
1249     //ALOGV("\tReverbGetDecayHfRatio() just Got -> %d\n", ActiveParams.Damping);
1250 
1251     if(ActiveParams.Damping != (LVM_INT16)(pContext->SavedDecayHfRatio / 20)){
1252         ALOGV("\tLVM_ERROR : ReverbGetDecayHfRatio() has wrong level -> %d %d\n",
1253          ActiveParams.Damping, pContext->SavedDecayHfRatio);
1254     }
1255 
1256     //ALOGV("\tReverbGetDecayHfRatio end");
1257     return pContext->SavedDecayHfRatio;
1258 }
1259 
1260 //----------------------------------------------------------------------------
1261 // ReverbSetDiffusion()
1262 //----------------------------------------------------------------------------
1263 // Purpose:
1264 // Apply the diffusion to the Reverb.
1265 //
1266 // Inputs:
1267 //  pContext:   effect engine context
1268 //  level        decay to be applied
1269 //
1270 //----------------------------------------------------------------------------
1271 
ReverbSetDiffusion(ReverbContext * pContext,int16_t level)1272 void ReverbSetDiffusion(ReverbContext *pContext, int16_t level){
1273     //ALOGV("\tReverbSetDiffusion start (%d)", level);
1274 
1275     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1276     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
1277 
1278     /* Get the current settings */
1279     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1280     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbSetDiffusion")
1281     //ALOGV("\tReverbSetDiffusion Succesfully returned from LVM_GetControlParameters\n");
1282     //ALOGV("\tReverbSetDiffusion() just Got -> %d\n", ActiveParams.Density);
1283 
1284     ActiveParams.Density = (LVM_INT16)(level/10);
1285 
1286     /* Activate the initial settings */
1287     LvmStatus = LVREV_SetControlParameters(pContext->hInstance, &ActiveParams);
1288     LVM_ERROR_CHECK(LvmStatus, "LVREV_SetControlParameters", "ReverbSetDiffusion")
1289     //ALOGV("\tReverbSetDiffusion() just Set -> %d\n", ActiveParams.Density);
1290 
1291     pContext->SavedDiffusion = level;
1292     //ALOGV("\tReverbSetDiffusion end");
1293     return;
1294 }
1295 
1296 //----------------------------------------------------------------------------
1297 // ReverbGetDiffusion()
1298 //----------------------------------------------------------------------------
1299 // Purpose:
1300 // Get the decay time applied to the Revervb.
1301 //
1302 // Inputs:
1303 //  pContext:   effect engine context
1304 //
1305 //----------------------------------------------------------------------------
1306 
ReverbGetDiffusion(ReverbContext * pContext)1307 int32_t ReverbGetDiffusion(ReverbContext *pContext){
1308     //ALOGV("\tReverbGetDiffusion start");
1309 
1310     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1311     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
1312     LVM_INT16                 Temp;
1313 
1314     /* Get the current settings */
1315     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1316     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbGetDiffusion")
1317     //ALOGV("\tReverbGetDiffusion Succesfully returned from LVM_GetControlParameters\n");
1318     //ALOGV("\tReverbGetDiffusion just Got -> %d\n", ActiveParams.Density);
1319 
1320     Temp = (LVM_INT16)(pContext->SavedDiffusion/10);
1321 
1322     if(ActiveParams.Density != Temp){
1323         ALOGV("\tLVM_ERROR : ReverbGetDiffusion invalid value %d %d", Temp, ActiveParams.Density);
1324     }
1325 
1326     //ALOGV("\tReverbGetDiffusion end");
1327     return pContext->SavedDiffusion;
1328 }
1329 
1330 //----------------------------------------------------------------------------
1331 // ReverbSetDensity()
1332 //----------------------------------------------------------------------------
1333 // Purpose:
1334 // Apply the density level the Reverb.
1335 //
1336 // Inputs:
1337 //  pContext:   effect engine context
1338 //  level        decay to be applied
1339 //
1340 //----------------------------------------------------------------------------
1341 
ReverbSetDensity(ReverbContext * pContext,int16_t level)1342 void ReverbSetDensity(ReverbContext *pContext, int16_t level){
1343     //ALOGV("\tReverbSetDensity start (%d)", level);
1344 
1345     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1346     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
1347 
1348     /* Get the current settings */
1349     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1350     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbSetDensity")
1351     //ALOGV("\tReverbSetDensity Succesfully returned from LVM_GetControlParameters\n");
1352     //ALOGV("\tReverbSetDensity just Got -> %d\n", ActiveParams.RoomSize);
1353 
1354     ActiveParams.RoomSize = (LVM_INT16)(((level * 99) / 1000) + 1);
1355 
1356     /* Activate the initial settings */
1357     LvmStatus = LVREV_SetControlParameters(pContext->hInstance, &ActiveParams);
1358     LVM_ERROR_CHECK(LvmStatus, "LVREV_SetControlParameters", "ReverbSetDensity")
1359     //ALOGV("\tReverbSetDensity just Set -> %d\n", ActiveParams.RoomSize);
1360 
1361     pContext->SavedDensity = level;
1362     //ALOGV("\tReverbSetDensity end");
1363     return;
1364 }
1365 
1366 //----------------------------------------------------------------------------
1367 // ReverbGetDensity()
1368 //----------------------------------------------------------------------------
1369 // Purpose:
1370 // Get the density level applied to the Revervb.
1371 //
1372 // Inputs:
1373 //  pContext:   effect engine context
1374 //
1375 //----------------------------------------------------------------------------
1376 
ReverbGetDensity(ReverbContext * pContext)1377 int32_t ReverbGetDensity(ReverbContext *pContext){
1378     //ALOGV("\tReverbGetDensity start");
1379 
1380     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1381     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
1382     LVM_INT16                 Temp;
1383     /* Get the current settings */
1384     LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
1385     LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "ReverbGetDensity")
1386     //ALOGV("\tReverbGetDensity Succesfully returned from LVM_GetControlParameters\n");
1387     //ALOGV("\tReverbGetDensity() just Got -> %d\n", ActiveParams.RoomSize);
1388 
1389     Temp = (LVM_INT16)(((pContext->SavedDensity * 99) / 1000) + 1);
1390 
1391     if(Temp != ActiveParams.RoomSize){
1392         ALOGV("\tLVM_ERROR : ReverbGetDensity invalid value %d %d", Temp, ActiveParams.RoomSize);
1393     }
1394 
1395     //ALOGV("\tReverbGetDensity end");
1396     return pContext->SavedDensity;
1397 }
1398 
1399 //----------------------------------------------------------------------------
1400 // Reverb_LoadPreset()
1401 //----------------------------------------------------------------------------
1402 // Purpose:
1403 // Load a the next preset
1404 //
1405 // Inputs:
1406 //  pContext         - handle to instance data
1407 //
1408 // Outputs:
1409 //
1410 // Side Effects:
1411 //
1412 //----------------------------------------------------------------------------
Reverb_LoadPreset(ReverbContext * pContext)1413 int Reverb_LoadPreset(ReverbContext   *pContext)
1414 {
1415     //TODO: add reflections delay, level and reverb delay when early reflections are
1416     // implemented
1417     pContext->curPreset = pContext->nextPreset;
1418 
1419     if (pContext->curPreset != REVERB_PRESET_NONE) {
1420         const t_reverb_settings *preset = &sReverbPresets[pContext->curPreset];
1421         ReverbSetRoomLevel(pContext, preset->roomLevel);
1422         ReverbSetRoomHfLevel(pContext, preset->roomHFLevel);
1423         ReverbSetDecayTime(pContext, preset->decayTime);
1424         ReverbSetDecayHfRatio(pContext, preset->decayHFRatio);
1425         //reflectionsLevel
1426         //reflectionsDelay
1427         ReverbSetReverbLevel(pContext, preset->reverbLevel);
1428         // reverbDelay
1429         ReverbSetDiffusion(pContext, preset->diffusion);
1430         ReverbSetDensity(pContext, preset->density);
1431     }
1432 
1433     return 0;
1434 }
1435 
1436 //----------------------------------------------------------------------------
1437 // Reverb_getParameter()
1438 //----------------------------------------------------------------------------
1439 // Purpose:
1440 // Get a Reverb parameter
1441 //
1442 // Inputs:
1443 //  pContext         - handle to instance data
1444 //  pParam           - pointer to parameter
1445 //  pValue           - pointer to variable to hold retrieved value
1446 //  pValueSize       - pointer to value size: maximum size as input
1447 //
1448 // Outputs:
1449 //  *pValue updated with parameter value
1450 //  *pValueSize updated with actual value size
1451 //
1452 //
1453 // Side Effects:
1454 //
1455 //----------------------------------------------------------------------------
1456 
Reverb_getParameter(ReverbContext * pContext,void * pParam,uint32_t * pValueSize,void * pValue)1457 int Reverb_getParameter(ReverbContext *pContext,
1458                         void          *pParam,
1459                         uint32_t      *pValueSize,
1460                         void          *pValue){
1461     int status = 0;
1462     int32_t *pParamTemp = (int32_t *)pParam;
1463     int32_t param = *pParamTemp++;
1464     t_reverb_settings *pProperties;
1465 
1466     //ALOGV("\tReverb_getParameter start");
1467     if (pContext->preset) {
1468         if (param != REVERB_PARAM_PRESET || *pValueSize < sizeof(uint16_t)) {
1469             return -EINVAL;
1470         }
1471 
1472         *(uint16_t *)pValue = pContext->nextPreset;
1473         ALOGV("get REVERB_PARAM_PRESET, preset %d", pContext->nextPreset);
1474         return 0;
1475     }
1476 
1477     switch (param){
1478         case REVERB_PARAM_ROOM_LEVEL:
1479             if (*pValueSize != sizeof(int16_t)){
1480                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize1 %d", *pValueSize);
1481                 return -EINVAL;
1482             }
1483             *pValueSize = sizeof(int16_t);
1484             break;
1485         case REVERB_PARAM_ROOM_HF_LEVEL:
1486             if (*pValueSize != sizeof(int16_t)){
1487                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize12 %d", *pValueSize);
1488                 return -EINVAL;
1489             }
1490             *pValueSize = sizeof(int16_t);
1491             break;
1492         case REVERB_PARAM_DECAY_TIME:
1493             if (*pValueSize != sizeof(uint32_t)){
1494                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize3 %d", *pValueSize);
1495                 return -EINVAL;
1496             }
1497             *pValueSize = sizeof(uint32_t);
1498             break;
1499         case REVERB_PARAM_DECAY_HF_RATIO:
1500             if (*pValueSize != sizeof(int16_t)){
1501                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize4 %d", *pValueSize);
1502                 return -EINVAL;
1503             }
1504             *pValueSize = sizeof(int16_t);
1505             break;
1506         case REVERB_PARAM_REFLECTIONS_LEVEL:
1507             if (*pValueSize != sizeof(int16_t)){
1508                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize5 %d", *pValueSize);
1509                 return -EINVAL;
1510             }
1511             *pValueSize = sizeof(int16_t);
1512             break;
1513         case REVERB_PARAM_REFLECTIONS_DELAY:
1514             if (*pValueSize != sizeof(uint32_t)){
1515                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize6 %d", *pValueSize);
1516                 return -EINVAL;
1517             }
1518             *pValueSize = sizeof(uint32_t);
1519             break;
1520         case REVERB_PARAM_REVERB_LEVEL:
1521             if (*pValueSize != sizeof(int16_t)){
1522                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize7 %d", *pValueSize);
1523                 return -EINVAL;
1524             }
1525             *pValueSize = sizeof(int16_t);
1526             break;
1527         case REVERB_PARAM_REVERB_DELAY:
1528             if (*pValueSize != sizeof(uint32_t)){
1529                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize8 %d", *pValueSize);
1530                 return -EINVAL;
1531             }
1532             *pValueSize = sizeof(uint32_t);
1533             break;
1534         case REVERB_PARAM_DIFFUSION:
1535             if (*pValueSize != sizeof(int16_t)){
1536                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize9 %d", *pValueSize);
1537                 return -EINVAL;
1538             }
1539             *pValueSize = sizeof(int16_t);
1540             break;
1541         case REVERB_PARAM_DENSITY:
1542             if (*pValueSize != sizeof(int16_t)){
1543                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize10 %d", *pValueSize);
1544                 return -EINVAL;
1545             }
1546             *pValueSize = sizeof(int16_t);
1547             break;
1548         case REVERB_PARAM_PROPERTIES:
1549             if (*pValueSize != sizeof(t_reverb_settings)){
1550                 ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid pValueSize11 %d", *pValueSize);
1551                 return -EINVAL;
1552             }
1553             *pValueSize = sizeof(t_reverb_settings);
1554             break;
1555 
1556         default:
1557             ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid param %d", param);
1558             return -EINVAL;
1559     }
1560 
1561     pProperties = (t_reverb_settings *) pValue;
1562 
1563     switch (param){
1564         case REVERB_PARAM_PROPERTIES:
1565             pProperties->roomLevel = ReverbGetRoomLevel(pContext);
1566             pProperties->roomHFLevel = ReverbGetRoomHfLevel(pContext);
1567             pProperties->decayTime = ReverbGetDecayTime(pContext);
1568             pProperties->decayHFRatio = ReverbGetDecayHfRatio(pContext);
1569             pProperties->reflectionsLevel = 0;
1570             pProperties->reflectionsDelay = 0;
1571             pProperties->reverbDelay = 0;
1572             pProperties->reverbLevel = ReverbGetReverbLevel(pContext);
1573             pProperties->diffusion = ReverbGetDiffusion(pContext);
1574             pProperties->density = ReverbGetDensity(pContext);
1575 
1576             ALOGV("\tReverb_getParameter() REVERB_PARAM_PROPERTIES Value is roomLevel        %d",
1577                 pProperties->roomLevel);
1578             ALOGV("\tReverb_getParameter() REVERB_PARAM_PROPERTIES Value is roomHFLevel      %d",
1579                 pProperties->roomHFLevel);
1580             ALOGV("\tReverb_getParameter() REVERB_PARAM_PROPERTIES Value is decayTime        %d",
1581                 pProperties->decayTime);
1582             ALOGV("\tReverb_getParameter() REVERB_PARAM_PROPERTIES Value is decayHFRatio     %d",
1583                 pProperties->decayHFRatio);
1584             ALOGV("\tReverb_getParameter() REVERB_PARAM_PROPERTIES Value is reflectionsLevel %d",
1585                 pProperties->reflectionsLevel);
1586             ALOGV("\tReverb_getParameter() REVERB_PARAM_PROPERTIES Value is reflectionsDelay %d",
1587                 pProperties->reflectionsDelay);
1588             ALOGV("\tReverb_getParameter() REVERB_PARAM_PROPERTIES Value is reverbDelay      %d",
1589                 pProperties->reverbDelay);
1590             ALOGV("\tReverb_getParameter() REVERB_PARAM_PROPERTIES Value is reverbLevel      %d",
1591                 pProperties->reverbLevel);
1592             ALOGV("\tReverb_getParameter() REVERB_PARAM_PROPERTIES Value is diffusion        %d",
1593                 pProperties->diffusion);
1594             ALOGV("\tReverb_getParameter() REVERB_PARAM_PROPERTIES Value is density          %d",
1595                 pProperties->density);
1596             break;
1597 
1598         case REVERB_PARAM_ROOM_LEVEL:
1599             *(int16_t *)pValue = ReverbGetRoomLevel(pContext);
1600 
1601             //ALOGV("\tReverb_getParameter() REVERB_PARAM_ROOM_LEVEL Value is %d",
1602             //        *(int16_t *)pValue);
1603             break;
1604         case REVERB_PARAM_ROOM_HF_LEVEL:
1605             *(int16_t *)pValue = ReverbGetRoomHfLevel(pContext);
1606 
1607             //ALOGV("\tReverb_getParameter() REVERB_PARAM_ROOM_HF_LEVEL Value is %d",
1608             //        *(int16_t *)pValue);
1609             break;
1610         case REVERB_PARAM_DECAY_TIME:
1611             *(uint32_t *)pValue = ReverbGetDecayTime(pContext);
1612 
1613             //ALOGV("\tReverb_getParameter() REVERB_PARAM_DECAY_TIME Value is %d",
1614             //        *(int32_t *)pValue);
1615             break;
1616         case REVERB_PARAM_DECAY_HF_RATIO:
1617             *(int16_t *)pValue = ReverbGetDecayHfRatio(pContext);
1618 
1619             //ALOGV("\tReverb_getParameter() REVERB_PARAM_DECAY_HF_RATION Value is %d",
1620             //        *(int16_t *)pValue);
1621             break;
1622         case REVERB_PARAM_REVERB_LEVEL:
1623              *(int16_t *)pValue = ReverbGetReverbLevel(pContext);
1624 
1625             //ALOGV("\tReverb_getParameter() REVERB_PARAM_REVERB_LEVEL Value is %d",
1626             //        *(int16_t *)pValue);
1627             break;
1628         case REVERB_PARAM_DIFFUSION:
1629             *(int16_t *)pValue = ReverbGetDiffusion(pContext);
1630 
1631             //ALOGV("\tReverb_getParameter() REVERB_PARAM_DECAY_DIFFUSION Value is %d",
1632             //        *(int16_t *)pValue);
1633             break;
1634         case REVERB_PARAM_DENSITY:
1635             *(int16_t *)pValue = ReverbGetDensity(pContext);
1636             //ALOGV("\tReverb_getParameter() REVERB_PARAM_DENSITY Value is %d",
1637             //        *(uint32_t *)pValue);
1638             break;
1639         case REVERB_PARAM_REFLECTIONS_LEVEL:
1640             *(uint16_t *)pValue = 0;
1641             break;
1642         case REVERB_PARAM_REFLECTIONS_DELAY:
1643         case REVERB_PARAM_REVERB_DELAY:
1644             *(uint32_t *)pValue = 0;
1645             break;
1646 
1647         default:
1648             ALOGV("\tLVM_ERROR : Reverb_getParameter() invalid param %d", param);
1649             status = -EINVAL;
1650             break;
1651     }
1652 
1653     //ALOGV("\tReverb_getParameter end");
1654     return status;
1655 } /* end Reverb_getParameter */
1656 
1657 //----------------------------------------------------------------------------
1658 // Reverb_setParameter()
1659 //----------------------------------------------------------------------------
1660 // Purpose:
1661 // Set a Reverb parameter
1662 //
1663 // Inputs:
1664 //  pContext         - handle to instance data
1665 //  pParam           - pointer to parameter
1666 //  pValue           - pointer to value
1667 //  vsize            - value size
1668 //
1669 // Outputs:
1670 //
1671 //----------------------------------------------------------------------------
1672 
Reverb_setParameter(ReverbContext * pContext,void * pParam,void * pValue,int vsize)1673 int Reverb_setParameter (ReverbContext *pContext, void *pParam, void *pValue, int vsize){
1674     int status = 0;
1675     int16_t level;
1676     int16_t ratio;
1677     uint32_t time;
1678     t_reverb_settings *pProperties;
1679     int32_t *pParamTemp = (int32_t *)pParam;
1680     int32_t param = *pParamTemp++;
1681 
1682     //ALOGV("\tReverb_setParameter start");
1683     if (pContext->preset) {
1684         if (param != REVERB_PARAM_PRESET) {
1685             return -EINVAL;
1686         }
1687         if (vsize < (int)sizeof(uint16_t)) {
1688             android_errorWriteLog(0x534e4554, "67647856");
1689             return -EINVAL;
1690         }
1691 
1692         uint16_t preset = *(uint16_t *)pValue;
1693         ALOGV("set REVERB_PARAM_PRESET, preset %d", preset);
1694         if (preset > REVERB_PRESET_LAST) {
1695             return -EINVAL;
1696         }
1697         pContext->nextPreset = preset;
1698         return 0;
1699     }
1700 
1701     if (vsize < Reverb_paramValueSize(param)) {
1702         android_errorWriteLog(0x534e4554, "63526567");
1703         return -EINVAL;
1704     }
1705 
1706     switch (param){
1707         case REVERB_PARAM_PROPERTIES:
1708             ALOGV("\tReverb_setParameter() REVERB_PARAM_PROPERTIES");
1709             pProperties = (t_reverb_settings *) pValue;
1710             ReverbSetRoomLevel(pContext, pProperties->roomLevel);
1711             ReverbSetRoomHfLevel(pContext, pProperties->roomHFLevel);
1712             ReverbSetDecayTime(pContext, pProperties->decayTime);
1713             ReverbSetDecayHfRatio(pContext, pProperties->decayHFRatio);
1714             ReverbSetReverbLevel(pContext, pProperties->reverbLevel);
1715             ReverbSetDiffusion(pContext, pProperties->diffusion);
1716             ReverbSetDensity(pContext, pProperties->density);
1717             break;
1718         case REVERB_PARAM_ROOM_LEVEL:
1719             level = *(int16_t *)pValue;
1720             //ALOGV("\tReverb_setParameter() REVERB_PARAM_ROOM_LEVEL value is %d", level);
1721             //ALOGV("\tReverb_setParameter() Calling ReverbSetRoomLevel");
1722             ReverbSetRoomLevel(pContext, level);
1723             //ALOGV("\tReverb_setParameter() Called ReverbSetRoomLevel");
1724            break;
1725         case REVERB_PARAM_ROOM_HF_LEVEL:
1726             level = *(int16_t *)pValue;
1727             //ALOGV("\tReverb_setParameter() REVERB_PARAM_ROOM_HF_LEVEL value is %d", level);
1728             //ALOGV("\tReverb_setParameter() Calling ReverbSetRoomHfLevel");
1729             ReverbSetRoomHfLevel(pContext, level);
1730             //ALOGV("\tReverb_setParameter() Called ReverbSetRoomHfLevel");
1731            break;
1732         case REVERB_PARAM_DECAY_TIME:
1733             time = *(uint32_t *)pValue;
1734             //ALOGV("\tReverb_setParameter() REVERB_PARAM_DECAY_TIME value is %d", time);
1735             //ALOGV("\tReverb_setParameter() Calling ReverbSetDecayTime");
1736             ReverbSetDecayTime(pContext, time);
1737             //ALOGV("\tReverb_setParameter() Called ReverbSetDecayTime");
1738            break;
1739         case REVERB_PARAM_DECAY_HF_RATIO:
1740             ratio = *(int16_t *)pValue;
1741             //ALOGV("\tReverb_setParameter() REVERB_PARAM_DECAY_HF_RATIO value is %d", ratio);
1742             //ALOGV("\tReverb_setParameter() Calling ReverbSetDecayHfRatio");
1743             ReverbSetDecayHfRatio(pContext, ratio);
1744             //ALOGV("\tReverb_setParameter() Called ReverbSetDecayHfRatio");
1745             break;
1746          case REVERB_PARAM_REVERB_LEVEL:
1747             level = *(int16_t *)pValue;
1748             //ALOGV("\tReverb_setParameter() REVERB_PARAM_REVERB_LEVEL value is %d", level);
1749             //ALOGV("\tReverb_setParameter() Calling ReverbSetReverbLevel");
1750             ReverbSetReverbLevel(pContext, level);
1751             //ALOGV("\tReverb_setParameter() Called ReverbSetReverbLevel");
1752            break;
1753         case REVERB_PARAM_DIFFUSION:
1754             ratio = *(int16_t *)pValue;
1755             //ALOGV("\tReverb_setParameter() REVERB_PARAM_DECAY_DIFFUSION value is %d", ratio);
1756             //ALOGV("\tReverb_setParameter() Calling ReverbSetDiffusion");
1757             ReverbSetDiffusion(pContext, ratio);
1758             //ALOGV("\tReverb_setParameter() Called ReverbSetDiffusion");
1759             break;
1760         case REVERB_PARAM_DENSITY:
1761             ratio = *(int16_t *)pValue;
1762             //ALOGV("\tReverb_setParameter() REVERB_PARAM_DECAY_DENSITY value is %d", ratio);
1763             //ALOGV("\tReverb_setParameter() Calling ReverbSetDensity");
1764             ReverbSetDensity(pContext, ratio);
1765             //ALOGV("\tReverb_setParameter() Called ReverbSetDensity");
1766             break;
1767            break;
1768         case REVERB_PARAM_REFLECTIONS_LEVEL:
1769         case REVERB_PARAM_REFLECTIONS_DELAY:
1770         case REVERB_PARAM_REVERB_DELAY:
1771             break;
1772         default:
1773             ALOGV("\tLVM_ERROR : Reverb_setParameter() invalid param %d", param);
1774             break;
1775     }
1776 
1777     //ALOGV("\tReverb_setParameter end");
1778     return status;
1779 } /* end Reverb_setParameter */
1780 
1781 /**
1782  * returns the size in bytes of the value of each environmental reverb parameter
1783  */
Reverb_paramValueSize(int32_t param)1784 int Reverb_paramValueSize(int32_t param) {
1785     switch (param) {
1786     case REVERB_PARAM_ROOM_LEVEL:
1787     case REVERB_PARAM_ROOM_HF_LEVEL:
1788     case REVERB_PARAM_REFLECTIONS_LEVEL:
1789     case REVERB_PARAM_REVERB_LEVEL:
1790         return sizeof(int16_t); // millibel
1791     case REVERB_PARAM_DECAY_TIME:
1792     case REVERB_PARAM_REFLECTIONS_DELAY:
1793     case REVERB_PARAM_REVERB_DELAY:
1794         return sizeof(uint32_t); // milliseconds
1795     case REVERB_PARAM_DECAY_HF_RATIO:
1796     case REVERB_PARAM_DIFFUSION:
1797     case REVERB_PARAM_DENSITY:
1798         return sizeof(int16_t); // permille
1799     case REVERB_PARAM_PROPERTIES:
1800         return sizeof(s_reverb_settings); // struct of all reverb properties
1801     }
1802     return sizeof(int32_t);
1803 }
1804 
1805 } // namespace
1806 } // namespace
1807 
1808 extern "C" {
1809 /* Effect Control Interface Implementation: Process */
Reverb_process(effect_handle_t self,audio_buffer_t * inBuffer,audio_buffer_t * outBuffer)1810 int Reverb_process(effect_handle_t   self,
1811                                  audio_buffer_t         *inBuffer,
1812                                  audio_buffer_t         *outBuffer){
1813     android::ReverbContext * pContext = (android::ReverbContext *) self;
1814     int    status = 0;
1815 
1816     if (pContext == NULL){
1817         ALOGV("\tLVM_ERROR : Reverb_process() ERROR pContext == NULL");
1818         return -EINVAL;
1819     }
1820     if (inBuffer == NULL  || inBuffer->raw == NULL  ||
1821             outBuffer == NULL || outBuffer->raw == NULL ||
1822             inBuffer->frameCount != outBuffer->frameCount){
1823         ALOGV("\tLVM_ERROR : Reverb_process() ERROR NULL INPUT POINTER OR FRAME COUNT IS WRONG");
1824         return -EINVAL;
1825     }
1826     //ALOGV("\tReverb_process() Calling process with %d frames", outBuffer->frameCount);
1827     /* Process all the available frames, block processing is handled internalLY by the LVM bundle */
1828     status = process(    inBuffer->f32,
1829                          outBuffer->f32,
1830                          outBuffer->frameCount,
1831                          pContext);
1832 
1833     if (pContext->bEnabled == LVM_FALSE) {
1834         if (pContext->SamplesToExitCount > 0) {
1835             // signed - unsigned will trigger integer overflow if result becomes negative.
1836             pContext->SamplesToExitCount -= (ssize_t)outBuffer->frameCount;
1837         } else {
1838             status = -ENODATA;
1839         }
1840     }
1841 
1842     return status;
1843 }   /* end Reverb_process */
1844 
1845 /* Effect Control Interface Implementation: Command */
Reverb_command(effect_handle_t self,uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t * replySize,void * pReplyData)1846 int Reverb_command(effect_handle_t  self,
1847                               uint32_t            cmdCode,
1848                               uint32_t            cmdSize,
1849                               void                *pCmdData,
1850                               uint32_t            *replySize,
1851                               void                *pReplyData){
1852     android::ReverbContext * pContext = (android::ReverbContext *) self;
1853     LVREV_ControlParams_st    ActiveParams;              /* Current control Parameters */
1854     LVREV_ReturnStatus_en     LvmStatus=LVREV_SUCCESS;     /* Function call status */
1855 
1856     if (pContext == NULL){
1857         ALOGV("\tLVM_ERROR : Reverb_command ERROR pContext == NULL");
1858         return -EINVAL;
1859     }
1860 
1861     //ALOGV("\tReverb_command INPUTS are: command %d cmdSize %d",cmdCode, cmdSize);
1862 
1863     switch (cmdCode){
1864         case EFFECT_CMD_INIT:
1865             //ALOGV("\tReverb_command cmdCode Case: "
1866             //        "EFFECT_CMD_INIT start");
1867 
1868             if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)){
1869                 ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
1870                         "EFFECT_CMD_INIT: ERROR");
1871                 return -EINVAL;
1872             }
1873             *(int *) pReplyData = 0;
1874             break;
1875 
1876         case EFFECT_CMD_SET_CONFIG:
1877             //ALOGV("\tReverb_command cmdCode Case: "
1878             //        "EFFECT_CMD_SET_CONFIG start");
1879             if (pCmdData == NULL || cmdSize != sizeof(effect_config_t) ||
1880                     pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
1881                 ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
1882                         "EFFECT_CMD_SET_CONFIG: ERROR");
1883                 return -EINVAL;
1884             }
1885             *(int *) pReplyData = android::Reverb_setConfig(pContext,
1886                                                             (effect_config_t *) pCmdData);
1887             break;
1888 
1889         case EFFECT_CMD_GET_CONFIG:
1890             if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(effect_config_t)) {
1891                 ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
1892                         "EFFECT_CMD_GET_CONFIG: ERROR");
1893                 return -EINVAL;
1894             }
1895 
1896             android::Reverb_getConfig(pContext, (effect_config_t *)pReplyData);
1897             break;
1898 
1899         case EFFECT_CMD_RESET:
1900             //ALOGV("\tReverb_command cmdCode Case: "
1901             //        "EFFECT_CMD_RESET start");
1902             Reverb_setConfig(pContext, &pContext->config);
1903             break;
1904 
1905         case EFFECT_CMD_GET_PARAM:{
1906             //ALOGV("\tReverb_command cmdCode Case: "
1907             //        "EFFECT_CMD_GET_PARAM start");
1908             effect_param_t *p = (effect_param_t *)pCmdData;
1909             if (pCmdData == nullptr) {
1910                 ALOGW("\tLVM_ERROR : pCmdData is NULL");
1911                 return -EINVAL;
1912             }
1913             if (SIZE_MAX - sizeof(effect_param_t) < (size_t)p->psize) {
1914                 android_errorWriteLog(0x534e4554, "26347509");
1915                 return -EINVAL;
1916             }
1917             if (cmdSize < sizeof(effect_param_t) ||
1918                     cmdSize < (sizeof(effect_param_t) + p->psize) ||
1919                     pReplyData == NULL || replySize == NULL ||
1920                     *replySize < (sizeof(effect_param_t) + p->psize)) {
1921                 ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
1922                         "EFFECT_CMD_GET_PARAM: ERROR");
1923                 return -EINVAL;
1924             }
1925 
1926             memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize);
1927 
1928             p = (effect_param_t *)pReplyData;
1929 
1930             int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
1931 
1932             p->status = android::Reverb_getParameter(pContext,
1933                                                          (void *)p->data,
1934                                                           &p->vsize,
1935                                                           p->data + voffset);
1936 
1937             *replySize = sizeof(effect_param_t) + voffset + p->vsize;
1938 
1939             //ALOGV("\tReverb_command EFFECT_CMD_GET_PARAM "
1940             //        "*pCmdData %d, *replySize %d, *pReplyData %d ",
1941             //        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
1942             //        *replySize,
1943             //        *(int16_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset));
1944 
1945         } break;
1946         case EFFECT_CMD_SET_PARAM:{
1947 
1948             //ALOGV("\tReverb_command cmdCode Case: "
1949             //        "EFFECT_CMD_SET_PARAM start");
1950             //ALOGV("\tReverb_command EFFECT_CMD_SET_PARAM param %d, *replySize %d, value %d ",
1951             //        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
1952             //        *replySize,
1953             //        *(int16_t *)((char *)pCmdData + sizeof(effect_param_t) + sizeof(int32_t)));
1954 
1955             if (pCmdData == NULL || (cmdSize < (sizeof(effect_param_t) + sizeof(int32_t))) ||
1956                     pReplyData == NULL ||  replySize == NULL || *replySize != sizeof(int32_t)) {
1957                 ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
1958                         "EFFECT_CMD_SET_PARAM: ERROR");
1959                 return -EINVAL;
1960             }
1961 
1962             effect_param_t *p = (effect_param_t *) pCmdData;
1963 
1964             if (p->psize != sizeof(int32_t)){
1965                 ALOGV("\t4LVM_ERROR : Reverb_command cmdCode Case: "
1966                         "EFFECT_CMD_SET_PARAM: ERROR, psize is not sizeof(int32_t)");
1967                 return -EINVAL;
1968             }
1969 
1970             //ALOGV("\tn5Reverb_command cmdSize is %d\n"
1971             //        "\tsizeof(effect_param_t) is  %d\n"
1972             //        "\tp->psize is %d\n"
1973             //        "\tp->vsize is %d"
1974             //        "\n",
1975             //        cmdSize, sizeof(effect_param_t), p->psize, p->vsize );
1976 
1977             *(int *)pReplyData = android::Reverb_setParameter(pContext,
1978                                                              (void *)p->data,
1979                                                               p->data + p->psize,
1980                                                               p->vsize);
1981         } break;
1982 
1983         case EFFECT_CMD_ENABLE:
1984             //ALOGV("\tReverb_command cmdCode Case: "
1985             //        "EFFECT_CMD_ENABLE start");
1986 
1987             if (pReplyData == NULL || *replySize != sizeof(int)){
1988                 ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
1989                         "EFFECT_CMD_ENABLE: ERROR");
1990                 return -EINVAL;
1991             }
1992             if(pContext->bEnabled == LVM_TRUE){
1993                  ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
1994                          "EFFECT_CMD_ENABLE: ERROR-Effect is already enabled");
1995                  return -EINVAL;
1996              }
1997             *(int *)pReplyData = 0;
1998             pContext->bEnabled = LVM_TRUE;
1999             /* Get the current settings */
2000             LvmStatus = LVREV_GetControlParameters(pContext->hInstance, &ActiveParams);
2001             LVM_ERROR_CHECK(LvmStatus, "LVREV_GetControlParameters", "EFFECT_CMD_ENABLE")
2002             pContext->SamplesToExitCount =
2003                     (ActiveParams.T60 * pContext->config.inputCfg.samplingRate)/1000;
2004             // force no volume ramp for first buffer processed after enabling the effect
2005             pContext->volumeMode = android::REVERB_VOLUME_FLAT;
2006             //ALOGV("\tEFFECT_CMD_ENABLE SamplesToExitCount = %d", pContext->SamplesToExitCount);
2007             break;
2008         case EFFECT_CMD_DISABLE:
2009             //ALOGV("\tReverb_command cmdCode Case: "
2010             //        "EFFECT_CMD_DISABLE start");
2011 
2012             if (pReplyData == NULL || *replySize != sizeof(int)){
2013                 ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
2014                         "EFFECT_CMD_DISABLE: ERROR");
2015                 return -EINVAL;
2016             }
2017             if(pContext->bEnabled == LVM_FALSE){
2018                  ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
2019                          "EFFECT_CMD_DISABLE: ERROR-Effect is not yet enabled");
2020                  return -EINVAL;
2021              }
2022             *(int *)pReplyData = 0;
2023             pContext->bEnabled = LVM_FALSE;
2024             break;
2025 
2026         case EFFECT_CMD_SET_VOLUME:
2027             if (pCmdData == NULL ||
2028                 cmdSize != 2 * sizeof(uint32_t)) {
2029                 ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
2030                         "EFFECT_CMD_SET_VOLUME: ERROR");
2031                 return -EINVAL;
2032             }
2033 
2034             if (pReplyData != NULL) { // we have volume control
2035                 pContext->leftVolume = (LVM_INT16)((*(uint32_t *)pCmdData + (1 << 11)) >> 12);
2036                 pContext->rightVolume = (LVM_INT16)((*((uint32_t *)pCmdData + 1) + (1 << 11)) >> 12);
2037                 *(uint32_t *)pReplyData = (1 << 24);
2038                 *((uint32_t *)pReplyData + 1) = (1 << 24);
2039                 if (pContext->volumeMode == android::REVERB_VOLUME_OFF) {
2040                     // force no volume ramp for first buffer processed after getting volume control
2041                     pContext->volumeMode = android::REVERB_VOLUME_FLAT;
2042                 }
2043             } else { // we don't have volume control
2044                 pContext->leftVolume = REVERB_UNIT_VOLUME;
2045                 pContext->rightVolume = REVERB_UNIT_VOLUME;
2046                 pContext->volumeMode = android::REVERB_VOLUME_OFF;
2047             }
2048             ALOGV("EFFECT_CMD_SET_VOLUME left %d, right %d mode %d",
2049                     pContext->leftVolume, pContext->rightVolume,  pContext->volumeMode);
2050             break;
2051 
2052         case EFFECT_CMD_SET_DEVICE:
2053         case EFFECT_CMD_SET_AUDIO_MODE:
2054         //ALOGV("\tReverb_command cmdCode Case: "
2055         //        "EFFECT_CMD_SET_DEVICE/EFFECT_CMD_SET_VOLUME/EFFECT_CMD_SET_AUDIO_MODE start");
2056             break;
2057 
2058         default:
2059             ALOGV("\tLVM_ERROR : Reverb_command cmdCode Case: "
2060                     "DEFAULT start %d ERROR",cmdCode);
2061             return -EINVAL;
2062     }
2063 
2064     //ALOGV("\tReverb_command end\n\n");
2065     return 0;
2066 }    /* end Reverb_command */
2067 
2068 /* Effect Control Interface Implementation: get_descriptor */
Reverb_getDescriptor(effect_handle_t self,effect_descriptor_t * pDescriptor)2069 int Reverb_getDescriptor(effect_handle_t   self,
2070                                     effect_descriptor_t *pDescriptor)
2071 {
2072     android::ReverbContext * pContext = (android::ReverbContext *)self;
2073     const effect_descriptor_t *desc;
2074 
2075     if (pContext == NULL || pDescriptor == NULL) {
2076         ALOGV("Reverb_getDescriptor() invalid param");
2077         return -EINVAL;
2078     }
2079 
2080     if (pContext->auxiliary) {
2081         if (pContext->preset) {
2082             desc = &android::gAuxPresetReverbDescriptor;
2083         } else {
2084             desc = &android::gAuxEnvReverbDescriptor;
2085         }
2086     } else {
2087         if (pContext->preset) {
2088             desc = &android::gInsertPresetReverbDescriptor;
2089         } else {
2090             desc = &android::gInsertEnvReverbDescriptor;
2091         }
2092     }
2093 
2094     *pDescriptor = *desc;
2095 
2096     return 0;
2097 }   /* end Reverb_getDescriptor */
2098 
2099 // effect_handle_t interface implementation for Reverb effect
2100 const struct effect_interface_s gReverbInterface = {
2101     Reverb_process,
2102     Reverb_command,
2103     Reverb_getDescriptor,
2104     NULL,
2105 };    /* end gReverbInterface */
2106 
2107 // This is the only symbol that needs to be exported
2108 __attribute__ ((visibility ("default")))
2109 audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
2110     .tag = AUDIO_EFFECT_LIBRARY_TAG,
2111     .version = EFFECT_LIBRARY_API_VERSION,
2112     .name = "Reverb Library",
2113     .implementor = "NXP Software Ltd.",
2114     .create_effect = android::EffectCreate,
2115     .release_effect = android::EffectRelease,
2116     .get_descriptor = android::EffectGetDescriptor,
2117 };
2118 
2119 }
2120