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