EffectBundle.cpp revision d7d013446a64c6de9f0f2dfe098a721b140e0b48
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
18typedef float LVM_FLOAT;
19#endif
20#define LOG_TAG "Bundle"
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 <log/log.h>
31
32#include "EffectBundle.h"
33#include "math.h"
34
35// effect_handle_t interface implementation for bass boost
36extern "C" const struct effect_interface_s gLvmEffectInterface;
37
38#define LVM_ERROR_CHECK(LvmStatus, callingFunc, calledFunc){\
39        if ((LvmStatus) == LVM_NULLADDRESS){\
40            ALOGV("\tLVM_ERROR : Parameter error - "\
41                    "null pointer returned by %s in %s\n\n\n\n", callingFunc, calledFunc);\
42        }\
43        if ((LvmStatus) == LVM_ALIGNMENTERROR){\
44            ALOGV("\tLVM_ERROR : Parameter error - "\
45                    "bad alignment returned by %s in %s\n\n\n\n", callingFunc, calledFunc);\
46        }\
47        if ((LvmStatus) == LVM_INVALIDNUMSAMPLES){\
48            ALOGV("\tLVM_ERROR : Parameter error - "\
49                    "bad number of samples returned by %s in %s\n\n\n\n", callingFunc, calledFunc);\
50        }\
51        if ((LvmStatus) == LVM_OUTOFRANGE){\
52            ALOGV("\tLVM_ERROR : Parameter error - "\
53                    "out of range returned by %s in %s\n", callingFunc, calledFunc);\
54        }\
55    }
56
57
58static inline int16_t clamp16(int32_t sample)
59{
60    // check overflow for both positive and negative values:
61    // all bits above short range must me equal to sign bit
62    if ((sample>>15) ^ (sample>>31))
63        sample = 0x7FFF ^ (sample>>31);
64    return sample;
65}
66
67// Namespaces
68namespace android {
69namespace {
70
71// Flag to allow a one time init of global memory, only happens on first call ever
72int LvmInitFlag = LVM_FALSE;
73SessionContext GlobalSessionMemory[LVM_MAX_SESSIONS];
74int SessionIndex[LVM_MAX_SESSIONS];
75
76/* local functions */
77#define CHECK_ARG(cond) {                     \
78    if (!(cond)) {                            \
79        ALOGV("\tLVM_ERROR : Invalid argument: "#cond);      \
80        return -EINVAL;                       \
81    }                                         \
82}
83
84
85// NXP SW BassBoost UUID
86const effect_descriptor_t gBassBoostDescriptor = {
87        {0x0634f220, 0xddd4, 0x11db, 0xa0fc, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }},
88        {0x8631f300, 0x72e2, 0x11df, 0xb57e, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
89        EFFECT_CONTROL_API_VERSION,
90        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST | EFFECT_FLAG_DEVICE_IND
91        | EFFECT_FLAG_VOLUME_CTRL),
92        BASS_BOOST_CUP_LOAD_ARM9E,
93        BUNDLE_MEM_USAGE,
94        "Dynamic Bass Boost",
95        "NXP Software Ltd.",
96};
97
98// NXP SW Virtualizer UUID
99const effect_descriptor_t gVirtualizerDescriptor = {
100        {0x37cc2c00, 0xdddd, 0x11db, 0x8577, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
101        {0x1d4033c0, 0x8557, 0x11df, 0x9f2d, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
102        EFFECT_CONTROL_API_VERSION,
103        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST | EFFECT_FLAG_DEVICE_IND
104        | EFFECT_FLAG_VOLUME_CTRL),
105        VIRTUALIZER_CUP_LOAD_ARM9E,
106        BUNDLE_MEM_USAGE,
107        "Virtualizer",
108        "NXP Software Ltd.",
109};
110
111// NXP SW Equalizer UUID
112const effect_descriptor_t gEqualizerDescriptor = {
113        {0x0bed4300, 0xddd6, 0x11db, 0x8f34, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
114        {0xce772f20, 0x847d, 0x11df, 0xbb17, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid Eq NXP
115        EFFECT_CONTROL_API_VERSION,
116        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST | EFFECT_FLAG_VOLUME_CTRL),
117        EQUALIZER_CUP_LOAD_ARM9E,
118        BUNDLE_MEM_USAGE,
119        "Equalizer",
120        "NXP Software Ltd.",
121};
122
123// NXP SW Volume UUID
124const effect_descriptor_t gVolumeDescriptor = {
125        {0x09e8ede0, 0xddde, 0x11db, 0xb4f6, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }},
126        {0x119341a0, 0x8469, 0x11df, 0x81f9, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }}, //uuid VOL NXP
127        EFFECT_CONTROL_API_VERSION,
128        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST | EFFECT_FLAG_VOLUME_CTRL),
129        VOLUME_CUP_LOAD_ARM9E,
130        BUNDLE_MEM_USAGE,
131        "Volume",
132        "NXP Software Ltd.",
133};
134
135//--- local function prototypes
136void LvmGlobalBundle_init      (void);
137int  LvmBundle_init            (EffectContext *pContext);
138int  LvmEffect_enable          (EffectContext *pContext);
139int  LvmEffect_disable         (EffectContext *pContext);
140void LvmEffect_free            (EffectContext *pContext);
141int  Effect_setConfig          (EffectContext *pContext, effect_config_t *pConfig);
142void Effect_getConfig          (EffectContext *pContext, effect_config_t *pConfig);
143int  BassBoost_setParameter    (EffectContext *pContext, void *pParam, void *pValue);
144int  BassBoost_getParameter    (EffectContext *pContext,
145                               void           *pParam,
146                               uint32_t       *pValueSize,
147                               void           *pValue);
148int  Virtualizer_setParameter  (EffectContext *pContext, void *pParam, void *pValue);
149int  Virtualizer_getParameter  (EffectContext *pContext,
150                               void           *pParam,
151                               uint32_t       *pValueSize,
152                               void           *pValue);
153int  Equalizer_setParameter    (EffectContext *pContext,
154                               void *pParam,
155                               uint32_t valueSize,
156                               void *pValue);
157int  Equalizer_getParameter    (EffectContext *pContext,
158                                void          *pParam,
159                                uint32_t      *pValueSize,
160                                void          *pValue);
161int  Volume_setParameter       (EffectContext *pContext, void *pParam, void *pValue);
162int  Volume_getParameter       (EffectContext *pContext,
163                                void          *pParam,
164                                uint32_t      *pValueSize,
165                                void          *pValue);
166int Effect_setEnabled(EffectContext *pContext, bool enabled);
167
168/* Effect Library Interface Implementation */
169
170extern "C" int EffectCreate(const effect_uuid_t *uuid,
171                            int32_t             sessionId,
172                            int32_t             ioId __unused,
173                            effect_handle_t  *pHandle){
174    int ret = 0;
175    int sessionNo;
176    int i;
177    EffectContext *pContext = NULL;
178    bool newBundle = false;
179    SessionContext *pSessionContext;
180
181    ALOGV("\n\tEffectCreate start session %d", sessionId);
182
183    if (pHandle == NULL || uuid == NULL){
184        ALOGV("\tLVM_ERROR : EffectCreate() called with NULL pointer");
185        ret = -EINVAL;
186        goto exit;
187    }
188
189    if(LvmInitFlag == LVM_FALSE){
190        LvmInitFlag = LVM_TRUE;
191        ALOGV("\tEffectCreate - Initializing all global memory");
192        LvmGlobalBundle_init();
193    }
194
195    // Find next available sessionNo
196    for(i=0; i<LVM_MAX_SESSIONS; i++){
197        if((SessionIndex[i] == LVM_UNUSED_SESSION)||(SessionIndex[i] == sessionId)){
198            sessionNo       = i;
199            SessionIndex[i] = sessionId;
200            ALOGV("\tEffectCreate: Allocating SessionNo %d for SessionId %d\n", sessionNo,sessionId);
201            break;
202        }
203    }
204
205    if(i==LVM_MAX_SESSIONS){
206        ALOGV("\tLVM_ERROR : Cannot find memory to allocate for current session");
207        ret = -EINVAL;
208        goto exit;
209    }
210
211    pContext = new EffectContext;
212
213    // If this is the first create in this session
214    if(GlobalSessionMemory[sessionNo].bBundledEffectsEnabled == LVM_FALSE){
215        ALOGV("\tEffectCreate - This is the first effect in current sessionId %d sessionNo %d",
216                sessionId, sessionNo);
217
218        GlobalSessionMemory[sessionNo].bBundledEffectsEnabled = LVM_TRUE;
219        GlobalSessionMemory[sessionNo].pBundledContext        = new BundledEffectContext;
220        newBundle = true;
221
222        pContext->pBundledContext = GlobalSessionMemory[sessionNo].pBundledContext;
223        pContext->pBundledContext->SessionNo                = sessionNo;
224        pContext->pBundledContext->SessionId                = sessionId;
225        pContext->pBundledContext->hInstance                = NULL;
226        pContext->pBundledContext->bVolumeEnabled           = LVM_FALSE;
227        pContext->pBundledContext->bEqualizerEnabled        = LVM_FALSE;
228        pContext->pBundledContext->bBassEnabled             = LVM_FALSE;
229        pContext->pBundledContext->bBassTempDisabled        = LVM_FALSE;
230        pContext->pBundledContext->bVirtualizerEnabled      = LVM_FALSE;
231        pContext->pBundledContext->bVirtualizerTempDisabled = LVM_FALSE;
232        pContext->pBundledContext->nOutputDevice            = AUDIO_DEVICE_NONE;
233        pContext->pBundledContext->nVirtualizerForcedDevice = AUDIO_DEVICE_NONE;
234        pContext->pBundledContext->NumberEffectsEnabled     = 0;
235        pContext->pBundledContext->NumberEffectsCalled      = 0;
236        pContext->pBundledContext->firstVolume              = LVM_TRUE;
237        pContext->pBundledContext->volume                   = 0;
238
239        #ifdef LVM_PCM
240        char fileName[256];
241        snprintf(fileName, 256, "/data/tmp/bundle_%p_pcm_in.pcm", pContext->pBundledContext);
242        pContext->pBundledContext->PcmInPtr = fopen(fileName, "w");
243        if (pContext->pBundledContext->PcmInPtr == NULL) {
244            ALOGV("cannot open %s", fileName);
245            ret = -EINVAL;
246            goto exit;
247        }
248
249        snprintf(fileName, 256, "/data/tmp/bundle_%p_pcm_out.pcm", pContext->pBundledContext);
250        pContext->pBundledContext->PcmOutPtr = fopen(fileName, "w");
251        if (pContext->pBundledContext->PcmOutPtr == NULL) {
252            ALOGV("cannot open %s", fileName);
253            fclose(pContext->pBundledContext->PcmInPtr);
254           pContext->pBundledContext->PcmInPtr = NULL;
255           ret = -EINVAL;
256           goto exit;
257        }
258        #endif
259
260        /* Saved strength is used to return the exact strength that was used in the set to the get
261         * because we map the original strength range of 0:1000 to 1:15, and this will avoid
262         * quantisation like effect when returning
263         */
264        pContext->pBundledContext->BassStrengthSaved        = 0;
265        pContext->pBundledContext->VirtStrengthSaved        = 0;
266        pContext->pBundledContext->CurPreset                = PRESET_CUSTOM;
267        pContext->pBundledContext->levelSaved               = 0;
268        pContext->pBundledContext->bMuteEnabled             = LVM_FALSE;
269        pContext->pBundledContext->bStereoPositionEnabled   = LVM_FALSE;
270        pContext->pBundledContext->positionSaved            = 0;
271        pContext->pBundledContext->workBuffer               = NULL;
272        pContext->pBundledContext->frameCount               = -1;
273        pContext->pBundledContext->SamplesToExitCountVirt   = 0;
274        pContext->pBundledContext->SamplesToExitCountBb     = 0;
275        pContext->pBundledContext->SamplesToExitCountEq     = 0;
276#ifdef BUILD_FLOAT
277        pContext->pBundledContext->pInputBuffer             = NULL;
278        pContext->pBundledContext->pOutputBuffer            = NULL;
279#endif
280        for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
281            pContext->pBundledContext->bandGaindB[i] = EQNB_5BandSoftPresets[i];
282        }
283
284        ALOGV("\tEffectCreate - Calling LvmBundle_init");
285        ret = LvmBundle_init(pContext);
286
287        if (ret < 0){
288            ALOGV("\tLVM_ERROR : EffectCreate() Bundle init failed");
289            goto exit;
290        }
291    }
292    else{
293        ALOGV("\tEffectCreate - Assigning memory for previously created effect on sessionNo %d",
294                sessionNo);
295        pContext->pBundledContext =
296                GlobalSessionMemory[sessionNo].pBundledContext;
297    }
298    ALOGV("\tEffectCreate - pBundledContext is %p", pContext->pBundledContext);
299
300    pSessionContext = &GlobalSessionMemory[pContext->pBundledContext->SessionNo];
301
302    // Create each Effect
303    if (memcmp(uuid, &gBassBoostDescriptor.uuid, sizeof(effect_uuid_t)) == 0){
304        // Create Bass Boost
305        ALOGV("\tEffectCreate - Effect to be created is LVM_BASS_BOOST");
306        pSessionContext->bBassInstantiated = LVM_TRUE;
307        pContext->pBundledContext->SamplesToExitCountBb = 0;
308
309        pContext->itfe       = &gLvmEffectInterface;
310        pContext->EffectType = LVM_BASS_BOOST;
311    } else if (memcmp(uuid, &gVirtualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0){
312        // Create Virtualizer
313        ALOGV("\tEffectCreate - Effect to be created is LVM_VIRTUALIZER");
314        pSessionContext->bVirtualizerInstantiated=LVM_TRUE;
315        pContext->pBundledContext->SamplesToExitCountVirt = 0;
316
317        pContext->itfe       = &gLvmEffectInterface;
318        pContext->EffectType = LVM_VIRTUALIZER;
319    } else if (memcmp(uuid, &gEqualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0){
320        // Create Equalizer
321        ALOGV("\tEffectCreate - Effect to be created is LVM_EQUALIZER");
322        pSessionContext->bEqualizerInstantiated = LVM_TRUE;
323        pContext->pBundledContext->SamplesToExitCountEq = 0;
324
325        pContext->itfe       = &gLvmEffectInterface;
326        pContext->EffectType = LVM_EQUALIZER;
327    } else if (memcmp(uuid, &gVolumeDescriptor.uuid, sizeof(effect_uuid_t)) == 0){
328        // Create Volume
329        ALOGV("\tEffectCreate - Effect to be created is LVM_VOLUME");
330        pSessionContext->bVolumeInstantiated = LVM_TRUE;
331
332        pContext->itfe       = &gLvmEffectInterface;
333        pContext->EffectType = LVM_VOLUME;
334    }
335    else{
336        ALOGV("\tLVM_ERROR : EffectCreate() invalid UUID");
337        ret = -EINVAL;
338        goto exit;
339    }
340
341exit:
342    if (ret != 0) {
343        if (pContext != NULL) {
344            if (newBundle) {
345                GlobalSessionMemory[sessionNo].bBundledEffectsEnabled = LVM_FALSE;
346                SessionIndex[sessionNo] = LVM_UNUSED_SESSION;
347                delete pContext->pBundledContext;
348            }
349            delete pContext;
350        }
351        if (pHandle != NULL)
352          *pHandle = (effect_handle_t)NULL;
353    } else {
354      if (pHandle != NULL)
355        *pHandle = (effect_handle_t)pContext;
356    }
357    ALOGV("\tEffectCreate end..\n\n");
358    return ret;
359} /* end EffectCreate */
360
361extern "C" int EffectRelease(effect_handle_t handle){
362    ALOGV("\n\tEffectRelease start %p", handle);
363    EffectContext * pContext = (EffectContext *)handle;
364
365    ALOGV("\tEffectRelease start handle: %p, context %p", handle, pContext->pBundledContext);
366    if (pContext == NULL){
367        ALOGV("\tLVM_ERROR : EffectRelease called with NULL pointer");
368        return -EINVAL;
369    }
370
371    SessionContext *pSessionContext = &GlobalSessionMemory[pContext->pBundledContext->SessionNo];
372
373    // Clear the instantiated flag for the effect
374    // protect agains the case where an effect is un-instantiated without being disabled
375    if(pContext->EffectType == LVM_BASS_BOOST) {
376        ALOGV("\tEffectRelease LVM_BASS_BOOST Clearing global intstantiated flag");
377        pSessionContext->bBassInstantiated = LVM_FALSE;
378        if(pContext->pBundledContext->SamplesToExitCountBb > 0){
379            pContext->pBundledContext->NumberEffectsEnabled--;
380        }
381        pContext->pBundledContext->SamplesToExitCountBb = 0;
382    } else if(pContext->EffectType == LVM_VIRTUALIZER) {
383        ALOGV("\tEffectRelease LVM_VIRTUALIZER Clearing global intstantiated flag");
384        pSessionContext->bVirtualizerInstantiated = LVM_FALSE;
385        if(pContext->pBundledContext->SamplesToExitCountVirt > 0){
386            pContext->pBundledContext->NumberEffectsEnabled--;
387        }
388        pContext->pBundledContext->SamplesToExitCountVirt = 0;
389    } else if(pContext->EffectType == LVM_EQUALIZER) {
390        ALOGV("\tEffectRelease LVM_EQUALIZER Clearing global intstantiated flag");
391        pSessionContext->bEqualizerInstantiated =LVM_FALSE;
392        if(pContext->pBundledContext->SamplesToExitCountEq > 0){
393            pContext->pBundledContext->NumberEffectsEnabled--;
394        }
395        pContext->pBundledContext->SamplesToExitCountEq = 0;
396    } else if(pContext->EffectType == LVM_VOLUME) {
397        ALOGV("\tEffectRelease LVM_VOLUME Clearing global intstantiated flag");
398        pSessionContext->bVolumeInstantiated = LVM_FALSE;
399        if (pContext->pBundledContext->bVolumeEnabled == LVM_TRUE){
400            pContext->pBundledContext->NumberEffectsEnabled--;
401        }
402    } else {
403        ALOGV("\tLVM_ERROR : EffectRelease : Unsupported effect\n\n\n\n\n\n\n");
404    }
405
406    // Disable effect, in this case ignore errors (return codes)
407    // if an effect has already been disabled
408    Effect_setEnabled(pContext, LVM_FALSE);
409
410    // if all effects are no longer instantiaed free the lvm memory and delete BundledEffectContext
411    if ((pSessionContext->bBassInstantiated == LVM_FALSE) &&
412            (pSessionContext->bVolumeInstantiated == LVM_FALSE) &&
413            (pSessionContext->bEqualizerInstantiated ==LVM_FALSE) &&
414            (pSessionContext->bVirtualizerInstantiated==LVM_FALSE))
415    {
416        #ifdef LVM_PCM
417        if (pContext->pBundledContext->PcmInPtr != NULL) {
418            fclose(pContext->pBundledContext->PcmInPtr);
419            pContext->pBundledContext->PcmInPtr = NULL;
420        }
421        if (pContext->pBundledContext->PcmOutPtr != NULL) {
422            fclose(pContext->pBundledContext->PcmOutPtr);
423            pContext->pBundledContext->PcmOutPtr = NULL;
424        }
425        #endif
426
427
428        // Clear the SessionIndex
429        for(int i=0; i<LVM_MAX_SESSIONS; i++){
430            if(SessionIndex[i] == pContext->pBundledContext->SessionId){
431                SessionIndex[i] = LVM_UNUSED_SESSION;
432                ALOGV("\tEffectRelease: Clearing SessionIndex SessionNo %d for SessionId %d\n",
433                        i, pContext->pBundledContext->SessionId);
434                break;
435            }
436        }
437
438        ALOGV("\tEffectRelease: All effects are no longer instantiated\n");
439        pSessionContext->bBundledEffectsEnabled = LVM_FALSE;
440        pSessionContext->pBundledContext = LVM_NULL;
441        ALOGV("\tEffectRelease: Freeing LVM Bundle memory\n");
442        LvmEffect_free(pContext);
443        ALOGV("\tEffectRelease: Deleting LVM Bundle context %p\n", pContext->pBundledContext);
444        if (pContext->pBundledContext->workBuffer != NULL) {
445            free(pContext->pBundledContext->workBuffer);
446        }
447#ifdef BUILD_FLOAT
448        if (pContext->pBundledContext->pInputBuffer != NULL) {
449            free(pContext->pBundledContext->pInputBuffer);
450        }
451        if (pContext->pBundledContext->pOutputBuffer != NULL) {
452            free(pContext->pBundledContext->pOutputBuffer);
453        }
454#endif
455        delete pContext->pBundledContext;
456        pContext->pBundledContext = LVM_NULL;
457    }
458    // free the effect context for current effect
459    delete pContext;
460
461    ALOGV("\tEffectRelease end\n");
462    return 0;
463
464} /* end EffectRelease */
465
466extern "C" int EffectGetDescriptor(const effect_uuid_t *uuid,
467                                   effect_descriptor_t *pDescriptor) {
468    const effect_descriptor_t *desc = NULL;
469
470    if (pDescriptor == NULL || uuid == NULL){
471        ALOGV("EffectGetDescriptor() called with NULL pointer");
472        return -EINVAL;
473    }
474
475    if (memcmp(uuid, &gBassBoostDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
476        desc = &gBassBoostDescriptor;
477    } else if (memcmp(uuid, &gVirtualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
478        desc = &gVirtualizerDescriptor;
479    } else if (memcmp(uuid, &gEqualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
480        desc = &gEqualizerDescriptor;
481    } else if (memcmp(uuid, &gVolumeDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
482        desc = &gVolumeDescriptor;
483    }
484
485    if (desc == NULL) {
486        return  -EINVAL;
487    }
488
489    *pDescriptor = *desc;
490
491    return 0;
492} /* end EffectGetDescriptor */
493
494void LvmGlobalBundle_init(){
495    ALOGV("\tLvmGlobalBundle_init start");
496    for(int i=0; i<LVM_MAX_SESSIONS; i++){
497        GlobalSessionMemory[i].bBundledEffectsEnabled   = LVM_FALSE;
498        GlobalSessionMemory[i].bVolumeInstantiated      = LVM_FALSE;
499        GlobalSessionMemory[i].bEqualizerInstantiated   = LVM_FALSE;
500        GlobalSessionMemory[i].bBassInstantiated        = LVM_FALSE;
501        GlobalSessionMemory[i].bVirtualizerInstantiated = LVM_FALSE;
502        GlobalSessionMemory[i].pBundledContext          = LVM_NULL;
503
504        SessionIndex[i] = LVM_UNUSED_SESSION;
505    }
506    return;
507}
508//----------------------------------------------------------------------------
509// LvmBundle_init()
510//----------------------------------------------------------------------------
511// Purpose: Initialize engine with default configuration, creates instance
512// with all effects disabled.
513//
514// Inputs:
515//  pContext:   effect engine context
516//
517// Outputs:
518//
519//----------------------------------------------------------------------------
520
521int LvmBundle_init(EffectContext *pContext){
522    ALOGV("\tLvmBundle_init start");
523
524    pContext->config.inputCfg.accessMode                    = EFFECT_BUFFER_ACCESS_READ;
525    pContext->config.inputCfg.channels                      = AUDIO_CHANNEL_OUT_STEREO;
526    pContext->config.inputCfg.format                        = AUDIO_FORMAT_PCM_16_BIT;
527    pContext->config.inputCfg.samplingRate                  = 44100;
528    pContext->config.inputCfg.bufferProvider.getBuffer      = NULL;
529    pContext->config.inputCfg.bufferProvider.releaseBuffer  = NULL;
530    pContext->config.inputCfg.bufferProvider.cookie         = NULL;
531    pContext->config.inputCfg.mask                          = EFFECT_CONFIG_ALL;
532    pContext->config.outputCfg.accessMode                   = EFFECT_BUFFER_ACCESS_ACCUMULATE;
533    pContext->config.outputCfg.channels                     = AUDIO_CHANNEL_OUT_STEREO;
534    pContext->config.outputCfg.format                       = AUDIO_FORMAT_PCM_16_BIT;
535    pContext->config.outputCfg.samplingRate                 = 44100;
536    pContext->config.outputCfg.bufferProvider.getBuffer     = NULL;
537    pContext->config.outputCfg.bufferProvider.releaseBuffer = NULL;
538    pContext->config.outputCfg.bufferProvider.cookie        = NULL;
539    pContext->config.outputCfg.mask                         = EFFECT_CONFIG_ALL;
540
541    CHECK_ARG(pContext != NULL);
542
543    if (pContext->pBundledContext->hInstance != NULL){
544        ALOGV("\tLvmBundle_init pContext->pBassBoost != NULL "
545                "-> Calling pContext->pBassBoost->free()");
546
547        LvmEffect_free(pContext);
548
549        ALOGV("\tLvmBundle_init pContext->pBassBoost != NULL "
550                "-> Called pContext->pBassBoost->free()");
551    }
552
553    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;          /* Function call status */
554    LVM_ControlParams_t     params;                         /* Control Parameters */
555    LVM_InstParams_t        InstParams;                     /* Instance parameters */
556    LVM_EQNB_BandDef_t      BandDefs[MAX_NUM_BANDS];        /* Equaliser band definitions */
557    LVM_HeadroomParams_t    HeadroomParams;                 /* Headroom parameters */
558    LVM_HeadroomBandDef_t   HeadroomBandDef[LVM_HEADROOM_MAX_NBANDS];
559    LVM_MemTab_t            MemTab;                         /* Memory allocation table */
560    bool                    bMallocFailure = LVM_FALSE;
561
562    /* Set the capabilities */
563    InstParams.BufferMode       = LVM_UNMANAGED_BUFFERS;
564    InstParams.MaxBlockSize     = MAX_CALL_SIZE;
565    InstParams.EQNB_NumBands    = MAX_NUM_BANDS;
566    InstParams.PSA_Included     = LVM_PSA_ON;
567
568    /* Allocate memory, forcing alignment */
569    LvmStatus = LVM_GetMemoryTable(LVM_NULL,
570                                  &MemTab,
571                                  &InstParams);
572
573    LVM_ERROR_CHECK(LvmStatus, "LVM_GetMemoryTable", "LvmBundle_init")
574    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
575
576    ALOGV("\tCreateInstance Succesfully called LVM_GetMemoryTable\n");
577
578    /* Allocate memory */
579    for (int i=0; i<LVM_NR_MEMORY_REGIONS; i++){
580        if (MemTab.Region[i].Size != 0){
581            MemTab.Region[i].pBaseAddress = malloc(MemTab.Region[i].Size);
582
583            if (MemTab.Region[i].pBaseAddress == LVM_NULL){
584                ALOGV("\tLVM_ERROR :LvmBundle_init CreateInstance Failed to allocate %" PRIu32
585                        " bytes for region %u\n", MemTab.Region[i].Size, i );
586                bMallocFailure = LVM_TRUE;
587            }else{
588                ALOGV("\tLvmBundle_init CreateInstance allocated %" PRIu32
589                        " bytes for region %u at %p\n",
590                        MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
591            }
592        }
593    }
594
595    /* If one or more of the memory regions failed to allocate, free the regions that were
596     * succesfully allocated and return with an error
597     */
598    if(bMallocFailure == LVM_TRUE){
599        for (int i=0; i<LVM_NR_MEMORY_REGIONS; i++){
600            if (MemTab.Region[i].pBaseAddress == LVM_NULL){
601                ALOGV("\tLVM_ERROR :LvmBundle_init CreateInstance Failed to allocate %" PRIu32
602                        " bytes for region %u Not freeing\n", MemTab.Region[i].Size, i );
603            }else{
604                ALOGV("\tLVM_ERROR :LvmBundle_init CreateInstance Failed: but allocated %" PRIu32
605                     " bytes for region %u at %p- free\n",
606                     MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
607                free(MemTab.Region[i].pBaseAddress);
608            }
609        }
610        return -EINVAL;
611    }
612    ALOGV("\tLvmBundle_init CreateInstance Succesfully malloc'd memory\n");
613
614    /* Initialise */
615    pContext->pBundledContext->hInstance = LVM_NULL;
616
617    /* Init sets the instance handle */
618    LvmStatus = LVM_GetInstanceHandle(&pContext->pBundledContext->hInstance,
619                                      &MemTab,
620                                      &InstParams);
621
622    LVM_ERROR_CHECK(LvmStatus, "LVM_GetInstanceHandle", "LvmBundle_init")
623    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
624
625    ALOGV("\tLvmBundle_init CreateInstance Succesfully called LVM_GetInstanceHandle\n");
626
627    /* Set the initial process parameters */
628    /* General parameters */
629    params.OperatingMode          = LVM_MODE_ON;
630    params.SampleRate             = LVM_FS_44100;
631    params.SourceFormat           = LVM_STEREO;
632    params.SpeakerType            = LVM_HEADPHONES;
633
634    pContext->pBundledContext->SampleRate = LVM_FS_44100;
635
636    /* Concert Sound parameters */
637    params.VirtualizerOperatingMode   = LVM_MODE_OFF;
638    params.VirtualizerType            = LVM_CONCERTSOUND;
639    params.VirtualizerReverbLevel     = 100;
640    params.CS_EffectLevel             = LVM_CS_EFFECT_NONE;
641
642    /* N-Band Equaliser parameters */
643    params.EQNB_OperatingMode     = LVM_EQNB_OFF;
644    params.EQNB_NBands            = FIVEBAND_NUMBANDS;
645    params.pEQNB_BandDefinition   = &BandDefs[0];
646
647    for (int i=0; i<FIVEBAND_NUMBANDS; i++)
648    {
649        BandDefs[i].Frequency = EQNB_5BandPresetsFrequencies[i];
650        BandDefs[i].QFactor   = EQNB_5BandPresetsQFactors[i];
651        BandDefs[i].Gain      = EQNB_5BandSoftPresets[i];
652    }
653
654    /* Volume Control parameters */
655    params.VC_EffectLevel         = 0;
656    params.VC_Balance             = 0;
657
658    /* Treble Enhancement parameters */
659    params.TE_OperatingMode       = LVM_TE_OFF;
660    params.TE_EffectLevel         = 0;
661
662    /* PSA Control parameters */
663    params.PSA_Enable             = LVM_PSA_OFF;
664    params.PSA_PeakDecayRate      = (LVM_PSA_DecaySpeed_en)0;
665
666    /* Bass Enhancement parameters */
667    params.BE_OperatingMode       = LVM_BE_OFF;
668    params.BE_EffectLevel         = 0;
669    params.BE_CentreFreq          = LVM_BE_CENTRE_90Hz;
670    params.BE_HPF                 = LVM_BE_HPF_ON;
671
672    /* PSA Control parameters */
673    params.PSA_Enable             = LVM_PSA_OFF;
674    params.PSA_PeakDecayRate      = LVM_PSA_SPEED_MEDIUM;
675
676    /* TE Control parameters */
677    params.TE_OperatingMode       = LVM_TE_OFF;
678    params.TE_EffectLevel         = 0;
679
680    /* Activate the initial settings */
681    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance,
682                                         &params);
683
684    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmBundle_init")
685    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
686
687    ALOGV("\tLvmBundle_init CreateInstance Succesfully called LVM_SetControlParameters\n");
688
689    /* Set the headroom parameters */
690    HeadroomBandDef[0].Limit_Low          = 20;
691    HeadroomBandDef[0].Limit_High         = 4999;
692    HeadroomBandDef[0].Headroom_Offset    = 0;
693    HeadroomBandDef[1].Limit_Low          = 5000;
694    HeadroomBandDef[1].Limit_High         = 24000;
695    HeadroomBandDef[1].Headroom_Offset    = 0;
696    HeadroomParams.pHeadroomDefinition    = &HeadroomBandDef[0];
697    HeadroomParams.Headroom_OperatingMode = LVM_HEADROOM_ON;
698    HeadroomParams.NHeadroomBands         = 2;
699
700    LvmStatus = LVM_SetHeadroomParams(pContext->pBundledContext->hInstance,
701                                      &HeadroomParams);
702
703    LVM_ERROR_CHECK(LvmStatus, "LVM_SetHeadroomParams", "LvmBundle_init")
704    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
705
706    ALOGV("\tLvmBundle_init CreateInstance Succesfully called LVM_SetHeadroomParams\n");
707    ALOGV("\tLvmBundle_init End");
708    return 0;
709}   /* end LvmBundle_init */
710
711#ifdef BUILD_FLOAT
712/**********************************************************************************
713   FUNCTION INT16LTOFLOAT
714***********************************************************************************/
715// Todo: need to write function descriptor
716static void Int16ToFloat(const LVM_INT16 *src, LVM_FLOAT *dst, size_t n) {
717    size_t ii;
718    src += n-1;
719    dst += n-1;
720    for (ii = n; ii != 0; ii--) {
721        *dst = ((LVM_FLOAT)((LVM_INT16)*src)) / 32768.0f;
722        src--;
723        dst--;
724    }
725    return;
726}
727/**********************************************************************************
728   FUNCTION FLOATTOINT16_SAT
729***********************************************************************************/
730// Todo : Need to write function descriptor
731static void FloatToInt16_SAT(const LVM_FLOAT *src, LVM_INT16 *dst, size_t n) {
732    size_t ii;
733    LVM_INT32 temp;
734
735    src += n-1;
736    dst += n-1;
737    for (ii = n; ii != 0; ii--) {
738        temp = (LVM_INT32)((*src) * 32768.0f);
739        if (temp >= 32767) {
740            *dst = 32767;
741        } else if (temp <= -32768) {
742            *dst = -32768;
743        } else {
744            *dst = (LVM_INT16)temp;
745        }
746        src--;
747        dst--;
748    }
749    return;
750}
751#endif
752//----------------------------------------------------------------------------
753// LvmBundle_process()
754//----------------------------------------------------------------------------
755// Purpose:
756// Apply LVM Bundle effects
757//
758// Inputs:
759//  pIn:        pointer to stereo 16 bit input data
760//  pOut:       pointer to stereo 16 bit output data
761//  frameCount: Frames to process
762//  pContext:   effect engine context
763//  strength    strength to be applied
764//
765//  Outputs:
766//  pOut:       pointer to updated stereo 16 bit output data
767//
768//----------------------------------------------------------------------------
769#ifdef BUILD_FLOAT
770int LvmBundle_process(LVM_INT16        *pIn,
771                      LVM_INT16        *pOut,
772                      int              frameCount,
773                      EffectContext    *pContext){
774
775
776    //LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
777    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
778    LVM_INT16               *pOutTmp;
779    LVM_FLOAT               *pInputBuff;
780    LVM_FLOAT               *pOutputBuff;
781
782    if (pContext->pBundledContext->pInputBuffer == NULL ||
783            pContext->pBundledContext->frameCount < frameCount) {
784        if (pContext->pBundledContext->pInputBuffer != NULL) {
785            free(pContext->pBundledContext->pInputBuffer);
786        }
787        pContext->pBundledContext->pInputBuffer = (LVM_FLOAT *)malloc(frameCount * \
788                                                                      sizeof(LVM_FLOAT) * FCC_2);
789    }
790
791    if (pContext->pBundledContext->pOutputBuffer == NULL ||
792            pContext->pBundledContext->frameCount < frameCount) {
793        if (pContext->pBundledContext->pOutputBuffer != NULL) {
794            free(pContext->pBundledContext->pOutputBuffer);
795        }
796        pContext->pBundledContext->pOutputBuffer = (LVM_FLOAT *)malloc(frameCount * \
797                                                                       sizeof(LVM_FLOAT) * FCC_2);
798    }
799
800    if ((pContext->pBundledContext->pInputBuffer == NULL) ||
801                                    (pContext->pBundledContext->pOutputBuffer == NULL)) {
802        ALOGV("LVM_ERROR : LvmBundle_process memory allocation for float buffer's failed");
803        return -EINVAL;
804    }
805
806    pInputBuff = pContext->pBundledContext->pInputBuffer;
807    pOutputBuff = pContext->pBundledContext->pOutputBuffer;
808
809    if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_WRITE){
810        pOutTmp = pOut;
811    } else if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE){
812        if (pContext->pBundledContext->frameCount != frameCount) {
813            if (pContext->pBundledContext->workBuffer != NULL) {
814                free(pContext->pBundledContext->workBuffer);
815            }
816            pContext->pBundledContext->workBuffer =
817                    (LVM_INT16 *)calloc(frameCount, sizeof(LVM_INT16) * FCC_2);
818            if (pContext->pBundledContext->workBuffer == NULL) {
819                return -ENOMEM;
820            }
821            pContext->pBundledContext->frameCount = frameCount;
822        }
823        pOutTmp = pContext->pBundledContext->workBuffer;
824    } else {
825        ALOGV("LVM_ERROR : LvmBundle_process invalid access mode");
826        return -EINVAL;
827    }
828
829    #ifdef LVM_PCM
830    fwrite(pIn, frameCount*sizeof(LVM_INT16) * FCC_2, 1, pContext->pBundledContext->PcmInPtr);
831    fflush(pContext->pBundledContext->PcmInPtr);
832    #endif
833
834    /* Converting input data from fixed point to float point */
835    Int16ToFloat(pIn, pInputBuff, frameCount * 2);
836
837    /* Process the samples */
838    LvmStatus = LVM_Process(pContext->pBundledContext->hInstance, /* Instance handle */
839                            pInputBuff,                           /* Input buffer */
840                            pOutputBuff,                          /* Output buffer */
841                            (LVM_UINT16)frameCount,               /* Number of samples to read */
842                            0);                                   /* Audo Time */
843
844    LVM_ERROR_CHECK(LvmStatus, "LVM_Process", "LvmBundle_process")
845    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
846
847    /* Converting output data from float point to fixed point */
848    FloatToInt16_SAT(pOutputBuff, pOutTmp, (LVM_UINT16)frameCount * 2);
849    #ifdef LVM_PCM
850    fwrite(pOutTmp, frameCount*sizeof(LVM_INT16) * FCC_2, 1, pContext->pBundledContext->PcmOutPtr);
851    fflush(pContext->pBundledContext->PcmOutPtr);
852    #endif
853
854    if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE){
855        for (int i = 0; i < frameCount * 2; i++){
856            pOut[i] = clamp16((LVM_INT32)pOut[i] + (LVM_INT32)pOutTmp[i]);
857        }
858    }
859    return 0;
860}    /* end LvmBundle_process */
861#else
862int LvmBundle_process(LVM_INT16        *pIn,
863                      LVM_INT16        *pOut,
864                      int              frameCount,
865                      EffectContext    *pContext){
866
867    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
868    LVM_INT16               *pOutTmp;
869
870    if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_WRITE){
871        pOutTmp = pOut;
872    } else if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE){
873        if (pContext->pBundledContext->frameCount != frameCount) {
874            if (pContext->pBundledContext->workBuffer != NULL) {
875                free(pContext->pBundledContext->workBuffer);
876            }
877            pContext->pBundledContext->workBuffer =
878                    (LVM_INT16 *)calloc(frameCount, sizeof(LVM_INT16) * 2);
879            if (pContext->pBundledContext->workBuffer == NULL) {
880                return -ENOMEM;
881            }
882            pContext->pBundledContext->frameCount = frameCount;
883        }
884        pOutTmp = pContext->pBundledContext->workBuffer;
885    } else {
886        ALOGV("LVM_ERROR : LvmBundle_process invalid access mode");
887        return -EINVAL;
888    }
889
890    #ifdef LVM_PCM
891    fwrite(pIn, frameCount*sizeof(LVM_INT16)*2, 1, pContext->pBundledContext->PcmInPtr);
892    fflush(pContext->pBundledContext->PcmInPtr);
893    #endif
894
895    //ALOGV("Calling LVM_Process");
896
897    /* Process the samples */
898    LvmStatus = LVM_Process(pContext->pBundledContext->hInstance, /* Instance handle */
899                            pIn,                                  /* Input buffer */
900                            pOutTmp,                              /* Output buffer */
901                            (LVM_UINT16)frameCount,               /* Number of samples to read */
902                            0);                                   /* Audo Time */
903
904    LVM_ERROR_CHECK(LvmStatus, "LVM_Process", "LvmBundle_process")
905    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
906
907    #ifdef LVM_PCM
908    fwrite(pOutTmp, frameCount*sizeof(LVM_INT16)*2, 1, pContext->pBundledContext->PcmOutPtr);
909    fflush(pContext->pBundledContext->PcmOutPtr);
910    #endif
911
912    if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE){
913        for (int i=0; i<frameCount*2; i++){
914            pOut[i] = clamp16((LVM_INT32)pOut[i] + (LVM_INT32)pOutTmp[i]);
915        }
916    }
917    return 0;
918}    /* end LvmBundle_process */
919#endif
920
921//----------------------------------------------------------------------------
922// EqualizerUpdateActiveParams()
923//----------------------------------------------------------------------------
924// Purpose: Update ActiveParams for Equalizer
925//
926// Inputs:
927//  pContext:   effect engine context
928//
929// Outputs:
930//
931//----------------------------------------------------------------------------
932void EqualizerUpdateActiveParams(EffectContext *pContext) {
933    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
934    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
935
936    /* Get the current settings */
937    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
938    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "EqualizerUpdateActiveParams")
939    //ALOGV("\tEqualizerUpdateActiveParams Succesfully returned from LVM_GetControlParameters\n");
940    //ALOGV("\tEqualizerUpdateActiveParams just Got -> %d\n",
941    //          ActiveParams.pEQNB_BandDefinition[band].Gain);
942
943
944    for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
945           ActiveParams.pEQNB_BandDefinition[i].Frequency = EQNB_5BandPresetsFrequencies[i];
946           ActiveParams.pEQNB_BandDefinition[i].QFactor   = EQNB_5BandPresetsQFactors[i];
947           ActiveParams.pEQNB_BandDefinition[i].Gain = pContext->pBundledContext->bandGaindB[i];
948       }
949
950    /* Activate the initial settings */
951    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
952    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "EqualizerUpdateActiveParams")
953    //ALOGV("\tEqualizerUpdateActiveParams just Set -> %d\n",
954    //          ActiveParams.pEQNB_BandDefinition[band].Gain);
955
956}
957
958//----------------------------------------------------------------------------
959// LvmEffect_limitLevel()
960//----------------------------------------------------------------------------
961// Purpose: limit the overall level to a value less than 0 dB preserving
962//          the overall EQ band gain and BassBoost relative levels.
963//
964// Inputs:
965//  pContext:   effect engine context
966//
967// Outputs:
968//
969//----------------------------------------------------------------------------
970void LvmEffect_limitLevel(EffectContext *pContext) {
971    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
972    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
973
974    /* Get the current settings */
975    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
976    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "LvmEffect_limitLevel")
977    //ALOGV("\tLvmEffect_limitLevel Succesfully returned from LVM_GetControlParameters\n");
978    //ALOGV("\tLvmEffect_limitLevel just Got -> %d\n",
979    //          ActiveParams.pEQNB_BandDefinition[band].Gain);
980
981    int gainCorrection = 0;
982    //Count the energy contribution per band for EQ and BassBoost only if they are active.
983    float energyContribution = 0;
984    float energyCross = 0;
985    float energyBassBoost = 0;
986    float crossCorrection = 0;
987
988    //EQ contribution
989    if (pContext->pBundledContext->bEqualizerEnabled == LVM_TRUE) {
990        for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
991            float bandFactor = pContext->pBundledContext->bandGaindB[i]/15.0;
992            float bandCoefficient = LimitLevel_bandEnergyCoefficient[i];
993            float bandEnergy = bandFactor * bandCoefficient * bandCoefficient;
994            if (bandEnergy > 0)
995                energyContribution += bandEnergy;
996        }
997
998        //cross EQ coefficients
999        float bandFactorSum = 0;
1000        for (int i = 0; i < FIVEBAND_NUMBANDS-1; i++) {
1001            float bandFactor1 = pContext->pBundledContext->bandGaindB[i]/15.0;
1002            float bandFactor2 = pContext->pBundledContext->bandGaindB[i+1]/15.0;
1003
1004            if (bandFactor1 > 0 && bandFactor2 > 0) {
1005                float crossEnergy = bandFactor1 * bandFactor2 *
1006                        LimitLevel_bandEnergyCrossCoefficient[i];
1007                bandFactorSum += bandFactor1 * bandFactor2;
1008
1009                if (crossEnergy > 0)
1010                    energyCross += crossEnergy;
1011            }
1012        }
1013        bandFactorSum -= 1.0;
1014        if (bandFactorSum > 0)
1015            crossCorrection = bandFactorSum * 0.7;
1016    }
1017
1018    //BassBoost contribution
1019    if (pContext->pBundledContext->bBassEnabled == LVM_TRUE) {
1020        float boostFactor = (pContext->pBundledContext->BassStrengthSaved)/1000.0;
1021        float boostCoefficient = LimitLevel_bassBoostEnergyCoefficient;
1022
1023        energyContribution += boostFactor * boostCoefficient * boostCoefficient;
1024
1025        for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
1026            float bandFactor = pContext->pBundledContext->bandGaindB[i]/15.0;
1027            float bandCrossCoefficient = LimitLevel_bassBoostEnergyCrossCoefficient[i];
1028            float bandEnergy = boostFactor * bandFactor *
1029                    bandCrossCoefficient;
1030            if (bandEnergy > 0)
1031                energyBassBoost += bandEnergy;
1032        }
1033    }
1034
1035    //Virtualizer contribution
1036    if (pContext->pBundledContext->bVirtualizerEnabled == LVM_TRUE) {
1037        energyContribution += LimitLevel_virtualizerContribution *
1038                LimitLevel_virtualizerContribution;
1039    }
1040
1041    double totalEnergyEstimation = sqrt(energyContribution + energyCross + energyBassBoost) -
1042            crossCorrection;
1043    ALOGV(" TOTAL energy estimation: %0.2f", totalEnergyEstimation);
1044
1045    //roundoff
1046    int maxLevelRound = (int)(totalEnergyEstimation + 0.99);
1047    if (maxLevelRound + pContext->pBundledContext->volume > 0) {
1048        gainCorrection = maxLevelRound + pContext->pBundledContext->volume;
1049    }
1050
1051    ActiveParams.VC_EffectLevel  = pContext->pBundledContext->volume - gainCorrection;
1052    if (ActiveParams.VC_EffectLevel < -96) {
1053        ActiveParams.VC_EffectLevel = -96;
1054    }
1055    ALOGV("\tVol:%d, GainCorrection: %d, Actual vol: %d", pContext->pBundledContext->volume,
1056            gainCorrection, ActiveParams.VC_EffectLevel);
1057
1058    /* Activate the initial settings */
1059    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1060    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmEffect_limitLevel")
1061    //ALOGV("\tLvmEffect_limitLevel just Set -> %d\n",
1062    //          ActiveParams.pEQNB_BandDefinition[band].Gain);
1063
1064    //ALOGV("\tLvmEffect_limitLevel just set (-96dB -> 0dB) -> %d\n",ActiveParams.VC_EffectLevel );
1065    if (pContext->pBundledContext->firstVolume == LVM_TRUE){
1066        LvmStatus = LVM_SetVolumeNoSmoothing(pContext->pBundledContext->hInstance, &ActiveParams);
1067        LVM_ERROR_CHECK(LvmStatus, "LVM_SetVolumeNoSmoothing", "LvmBundle_process")
1068        ALOGV("\tLVM_VOLUME: Disabling Smoothing for first volume change to remove spikes/clicks");
1069        pContext->pBundledContext->firstVolume = LVM_FALSE;
1070    }
1071}
1072
1073//----------------------------------------------------------------------------
1074// LvmEffect_enable()
1075//----------------------------------------------------------------------------
1076// Purpose: Enable the effect in the bundle
1077//
1078// Inputs:
1079//  pContext:   effect engine context
1080//
1081// Outputs:
1082//
1083//----------------------------------------------------------------------------
1084
1085int LvmEffect_enable(EffectContext *pContext){
1086    //ALOGV("\tLvmEffect_enable start");
1087
1088    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
1089    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
1090
1091    /* Get the current settings */
1092    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,
1093                                         &ActiveParams);
1094
1095    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "LvmEffect_enable")
1096    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1097    //ALOGV("\tLvmEffect_enable Succesfully called LVM_GetControlParameters\n");
1098
1099    if(pContext->EffectType == LVM_BASS_BOOST) {
1100        ALOGV("\tLvmEffect_enable : Enabling LVM_BASS_BOOST");
1101        ActiveParams.BE_OperatingMode       = LVM_BE_ON;
1102    }
1103    if(pContext->EffectType == LVM_VIRTUALIZER) {
1104        ALOGV("\tLvmEffect_enable : Enabling LVM_VIRTUALIZER");
1105        ActiveParams.VirtualizerOperatingMode   = LVM_MODE_ON;
1106    }
1107    if(pContext->EffectType == LVM_EQUALIZER) {
1108        ALOGV("\tLvmEffect_enable : Enabling LVM_EQUALIZER");
1109        ActiveParams.EQNB_OperatingMode     = LVM_EQNB_ON;
1110    }
1111    if(pContext->EffectType == LVM_VOLUME) {
1112        ALOGV("\tLvmEffect_enable : Enabling LVM_VOLUME");
1113    }
1114
1115    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1116    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmEffect_enable")
1117    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1118
1119    //ALOGV("\tLvmEffect_enable Succesfully called LVM_SetControlParameters\n");
1120    //ALOGV("\tLvmEffect_enable end");
1121    LvmEffect_limitLevel(pContext);
1122    return 0;
1123}
1124
1125//----------------------------------------------------------------------------
1126// LvmEffect_disable()
1127//----------------------------------------------------------------------------
1128// Purpose: Disable the effect in the bundle
1129//
1130// Inputs:
1131//  pContext:   effect engine context
1132//
1133// Outputs:
1134//
1135//----------------------------------------------------------------------------
1136
1137int LvmEffect_disable(EffectContext *pContext){
1138    //ALOGV("\tLvmEffect_disable start");
1139
1140    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
1141    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
1142    /* Get the current settings */
1143    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,
1144                                         &ActiveParams);
1145
1146    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "LvmEffect_disable")
1147    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1148    //ALOGV("\tLvmEffect_disable Succesfully called LVM_GetControlParameters\n");
1149
1150    if(pContext->EffectType == LVM_BASS_BOOST) {
1151        ALOGV("\tLvmEffect_disable : Disabling LVM_BASS_BOOST");
1152        ActiveParams.BE_OperatingMode       = LVM_BE_OFF;
1153    }
1154    if(pContext->EffectType == LVM_VIRTUALIZER) {
1155        ALOGV("\tLvmEffect_disable : Disabling LVM_VIRTUALIZER");
1156        ActiveParams.VirtualizerOperatingMode   = LVM_MODE_OFF;
1157    }
1158    if(pContext->EffectType == LVM_EQUALIZER) {
1159        ALOGV("\tLvmEffect_disable : Disabling LVM_EQUALIZER");
1160        ActiveParams.EQNB_OperatingMode     = LVM_EQNB_OFF;
1161    }
1162    if(pContext->EffectType == LVM_VOLUME) {
1163        ALOGV("\tLvmEffect_disable : Disabling LVM_VOLUME");
1164    }
1165
1166    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1167    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmEffect_disable")
1168    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1169
1170    //ALOGV("\tLvmEffect_disable Succesfully called LVM_SetControlParameters\n");
1171    //ALOGV("\tLvmEffect_disable end");
1172    LvmEffect_limitLevel(pContext);
1173    return 0;
1174}
1175
1176//----------------------------------------------------------------------------
1177// LvmEffect_free()
1178//----------------------------------------------------------------------------
1179// Purpose: Free all memory associated with the Bundle.
1180//
1181// Inputs:
1182//  pContext:   effect engine context
1183//
1184// Outputs:
1185//
1186//----------------------------------------------------------------------------
1187
1188void LvmEffect_free(EffectContext *pContext){
1189    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;         /* Function call status */
1190    LVM_MemTab_t            MemTab;
1191
1192    /* Free the algorithm memory */
1193    LvmStatus = LVM_GetMemoryTable(pContext->pBundledContext->hInstance,
1194                                   &MemTab,
1195                                   LVM_NULL);
1196
1197    LVM_ERROR_CHECK(LvmStatus, "LVM_GetMemoryTable", "LvmEffect_free")
1198
1199    for (int i=0; i<LVM_NR_MEMORY_REGIONS; i++){
1200        if (MemTab.Region[i].Size != 0){
1201            if (MemTab.Region[i].pBaseAddress != NULL){
1202                ALOGV("\tLvmEffect_free - START freeing %" PRIu32 " bytes for region %u at %p\n",
1203                        MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
1204
1205                free(MemTab.Region[i].pBaseAddress);
1206
1207                ALOGV("\tLvmEffect_free - END   freeing %" PRIu32 " bytes for region %u at %p\n",
1208                        MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
1209            }else{
1210                ALOGV("\tLVM_ERROR : LvmEffect_free - trying to free with NULL pointer %" PRIu32
1211                        " bytes for region %u at %p ERROR\n",
1212                        MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
1213            }
1214        }
1215    }
1216}    /* end LvmEffect_free */
1217
1218//----------------------------------------------------------------------------
1219// Effect_setConfig()
1220//----------------------------------------------------------------------------
1221// Purpose: Set input and output audio configuration.
1222//
1223// Inputs:
1224//  pContext:   effect engine context
1225//  pConfig:    pointer to effect_config_t structure holding input and output
1226//      configuration parameters
1227//
1228// Outputs:
1229//
1230//----------------------------------------------------------------------------
1231
1232int Effect_setConfig(EffectContext *pContext, effect_config_t *pConfig){
1233    LVM_Fs_en   SampleRate;
1234    //ALOGV("\tEffect_setConfig start");
1235
1236    CHECK_ARG(pContext != NULL);
1237    CHECK_ARG(pConfig != NULL);
1238
1239    CHECK_ARG(pConfig->inputCfg.samplingRate == pConfig->outputCfg.samplingRate);
1240    CHECK_ARG(pConfig->inputCfg.channels == pConfig->outputCfg.channels);
1241    CHECK_ARG(pConfig->inputCfg.format == pConfig->outputCfg.format);
1242    CHECK_ARG(pConfig->inputCfg.channels == AUDIO_CHANNEL_OUT_STEREO);
1243    CHECK_ARG(pConfig->outputCfg.accessMode == EFFECT_BUFFER_ACCESS_WRITE
1244              || pConfig->outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE);
1245    CHECK_ARG(pConfig->inputCfg.format == AUDIO_FORMAT_PCM_16_BIT);
1246
1247    pContext->config = *pConfig;
1248
1249    switch (pConfig->inputCfg.samplingRate) {
1250    case 8000:
1251        SampleRate = LVM_FS_8000;
1252        pContext->pBundledContext->SamplesPerSecond = 8000*2; // 2 secs Stereo
1253        break;
1254    case 16000:
1255        SampleRate = LVM_FS_16000;
1256        pContext->pBundledContext->SamplesPerSecond = 16000*2; // 2 secs Stereo
1257        break;
1258    case 22050:
1259        SampleRate = LVM_FS_22050;
1260        pContext->pBundledContext->SamplesPerSecond = 22050*2; // 2 secs Stereo
1261        break;
1262    case 32000:
1263        SampleRate = LVM_FS_32000;
1264        pContext->pBundledContext->SamplesPerSecond = 32000*2; // 2 secs Stereo
1265        break;
1266    case 44100:
1267        SampleRate = LVM_FS_44100;
1268        pContext->pBundledContext->SamplesPerSecond = 44100*2; // 2 secs Stereo
1269        break;
1270    case 48000:
1271        SampleRate = LVM_FS_48000;
1272        pContext->pBundledContext->SamplesPerSecond = 48000*2; // 2 secs Stereo
1273        break;
1274#if defined(BUILD_FLOAT) && defined(HIGHER_FS)
1275    case 96000:
1276        SampleRate = LVM_FS_96000;
1277        pContext->pBundledContext->SamplesPerSecond = 96000*2; // 2 secs Stereo
1278        break;
1279    case 192000:
1280        SampleRate = LVM_FS_192000;
1281        pContext->pBundledContext->SamplesPerSecond = 192000*2; // 2 secs Stereo
1282        break;
1283#endif
1284    default:
1285        ALOGV("\tEffect_setConfig invalid sampling rate %d", pConfig->inputCfg.samplingRate);
1286        return -EINVAL;
1287    }
1288
1289    if(pContext->pBundledContext->SampleRate != SampleRate){
1290
1291        LVM_ControlParams_t     ActiveParams;
1292        LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;
1293
1294        ALOGV("\tEffect_setConfig change sampling rate to %d", SampleRate);
1295
1296        /* Get the current settings */
1297        LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,
1298                                         &ActiveParams);
1299
1300        LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "Effect_setConfig")
1301        if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1302
1303        ActiveParams.SampleRate = SampleRate;
1304
1305        LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1306
1307        LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "Effect_setConfig")
1308        ALOGV("\tEffect_setConfig Succesfully called LVM_SetControlParameters\n");
1309        pContext->pBundledContext->SampleRate = SampleRate;
1310
1311    }else{
1312        //ALOGV("\tEffect_setConfig keep sampling rate at %d", SampleRate);
1313    }
1314
1315    //ALOGV("\tEffect_setConfig End....");
1316    return 0;
1317}   /* end Effect_setConfig */
1318
1319//----------------------------------------------------------------------------
1320// Effect_getConfig()
1321//----------------------------------------------------------------------------
1322// Purpose: Get input and output audio configuration.
1323//
1324// Inputs:
1325//  pContext:   effect engine context
1326//  pConfig:    pointer to effect_config_t structure holding input and output
1327//      configuration parameters
1328//
1329// Outputs:
1330//
1331//----------------------------------------------------------------------------
1332
1333void Effect_getConfig(EffectContext *pContext, effect_config_t *pConfig)
1334{
1335    *pConfig = pContext->config;
1336}   /* end Effect_getConfig */
1337
1338//----------------------------------------------------------------------------
1339// BassGetStrength()
1340//----------------------------------------------------------------------------
1341// Purpose:
1342// get the effect strength currently being used, what is actually returned is the strengh that was
1343// previously used in the set, this is because the app uses a strength in the range 0-1000 while
1344// the bassboost uses 1-15, so to avoid a quantisation the original set value is used. However the
1345// actual used value is checked to make sure it corresponds to the one being returned
1346//
1347// Inputs:
1348//  pContext:   effect engine context
1349//
1350//----------------------------------------------------------------------------
1351
1352uint32_t BassGetStrength(EffectContext *pContext){
1353    //ALOGV("\tBassGetStrength() (0-1000) -> %d\n", pContext->pBundledContext->BassStrengthSaved);
1354
1355    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
1356    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
1357    /* Get the current settings */
1358    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,
1359                                         &ActiveParams);
1360
1361    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "BassGetStrength")
1362    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1363
1364    //ALOGV("\tBassGetStrength Succesfully returned from LVM_GetControlParameters\n");
1365
1366    /* Check that the strength returned matches the strength that was set earlier */
1367    if(ActiveParams.BE_EffectLevel !=
1368       (LVM_INT16)((15*pContext->pBundledContext->BassStrengthSaved)/1000)){
1369        ALOGV("\tLVM_ERROR : BassGetStrength module strength does not match savedStrength %d %d\n",
1370                ActiveParams.BE_EffectLevel, pContext->pBundledContext->BassStrengthSaved);
1371        return -EINVAL;
1372    }
1373
1374    //ALOGV("\tBassGetStrength() (0-15)   -> %d\n", ActiveParams.BE_EffectLevel );
1375    //ALOGV("\tBassGetStrength() (saved)  -> %d\n", pContext->pBundledContext->BassStrengthSaved );
1376    return pContext->pBundledContext->BassStrengthSaved;
1377}    /* end BassGetStrength */
1378
1379//----------------------------------------------------------------------------
1380// BassSetStrength()
1381//----------------------------------------------------------------------------
1382// Purpose:
1383// Apply the strength to the BassBosst. Must first be converted from the range 0-1000 to 1-15
1384//
1385// Inputs:
1386//  pContext:   effect engine context
1387//  strength    strength to be applied
1388//
1389//----------------------------------------------------------------------------
1390
1391void BassSetStrength(EffectContext *pContext, uint32_t strength){
1392    //ALOGV("\tBassSetStrength(%d)", strength);
1393
1394    pContext->pBundledContext->BassStrengthSaved = (int)strength;
1395
1396    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
1397    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
1398
1399    /* Get the current settings */
1400    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,
1401                                         &ActiveParams);
1402
1403    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "BassSetStrength")
1404    //ALOGV("\tBassSetStrength Succesfully returned from LVM_GetControlParameters\n");
1405
1406    /* Bass Enhancement parameters */
1407    ActiveParams.BE_EffectLevel    = (LVM_INT16)((15*strength)/1000);
1408    ActiveParams.BE_CentreFreq     = LVM_BE_CENTRE_90Hz;
1409
1410    //ALOGV("\tBassSetStrength() (0-15)   -> %d\n", ActiveParams.BE_EffectLevel );
1411
1412    /* Activate the initial settings */
1413    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1414
1415    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "BassSetStrength")
1416    //ALOGV("\tBassSetStrength Succesfully called LVM_SetControlParameters\n");
1417
1418    LvmEffect_limitLevel(pContext);
1419}    /* end BassSetStrength */
1420
1421//----------------------------------------------------------------------------
1422// VirtualizerGetStrength()
1423//----------------------------------------------------------------------------
1424// Purpose:
1425// get the effect strength currently being used, what is actually returned is the strengh that was
1426// previously used in the set, this is because the app uses a strength in the range 0-1000 while
1427// the Virtualizer uses 1-100, so to avoid a quantisation the original set value is used.However the
1428// actual used value is checked to make sure it corresponds to the one being returned
1429//
1430// Inputs:
1431//  pContext:   effect engine context
1432//
1433//----------------------------------------------------------------------------
1434
1435uint32_t VirtualizerGetStrength(EffectContext *pContext){
1436    //ALOGV("\tVirtualizerGetStrength (0-1000) -> %d\n",pContext->pBundledContext->VirtStrengthSaved);
1437
1438    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
1439    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
1440
1441    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1442
1443    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VirtualizerGetStrength")
1444    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1445
1446    //ALOGV("\tVirtualizerGetStrength Succesfully returned from LVM_GetControlParameters\n");
1447    //ALOGV("\tVirtualizerGetStrength() (0-100)   -> %d\n", ActiveParams.VirtualizerReverbLevel*10);
1448    return pContext->pBundledContext->VirtStrengthSaved;
1449}    /* end getStrength */
1450
1451//----------------------------------------------------------------------------
1452// VirtualizerSetStrength()
1453//----------------------------------------------------------------------------
1454// Purpose:
1455// Apply the strength to the Virtualizer. Must first be converted from the range 0-1000 to 1-15
1456//
1457// Inputs:
1458//  pContext:   effect engine context
1459//  strength    strength to be applied
1460//
1461//----------------------------------------------------------------------------
1462
1463void VirtualizerSetStrength(EffectContext *pContext, uint32_t strength){
1464    //ALOGV("\tVirtualizerSetStrength(%d)", strength);
1465    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
1466    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
1467
1468    pContext->pBundledContext->VirtStrengthSaved = (int)strength;
1469
1470    /* Get the current settings */
1471    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,&ActiveParams);
1472
1473    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VirtualizerSetStrength")
1474    //ALOGV("\tVirtualizerSetStrength Succesfully returned from LVM_GetControlParameters\n");
1475
1476    /* Virtualizer parameters */
1477    ActiveParams.CS_EffectLevel             = (int)((strength*32767)/1000);
1478
1479    ALOGV("\tVirtualizerSetStrength() (0-1000)   -> %d\n", strength );
1480    ALOGV("\tVirtualizerSetStrength() (0- 100)   -> %d\n", ActiveParams.CS_EffectLevel );
1481
1482    /* Activate the initial settings */
1483    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1484    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "VirtualizerSetStrength")
1485    //ALOGV("\tVirtualizerSetStrength Succesfully called LVM_SetControlParameters\n\n");
1486    LvmEffect_limitLevel(pContext);
1487}    /* end setStrength */
1488
1489//----------------------------------------------------------------------------
1490// VirtualizerIsDeviceSupported()
1491//----------------------------------------------------------------------------
1492// Purpose:
1493// Check if an audio device type is supported by this implementation
1494//
1495// Inputs:
1496//  deviceType   the type of device that affects the processing (e.g. for binaural vs transaural)
1497// Output:
1498//  -EINVAL      if the configuration is not supported or it is unknown
1499//  0            if the configuration is supported
1500//----------------------------------------------------------------------------
1501int VirtualizerIsDeviceSupported(audio_devices_t deviceType) {
1502    switch (deviceType) {
1503    case AUDIO_DEVICE_OUT_WIRED_HEADSET:
1504    case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
1505    case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
1506    case AUDIO_DEVICE_OUT_USB_HEADSET:
1507        return 0;
1508    default :
1509        return -EINVAL;
1510    }
1511}
1512
1513//----------------------------------------------------------------------------
1514// VirtualizerIsConfigurationSupported()
1515//----------------------------------------------------------------------------
1516// Purpose:
1517// Check if a channel mask + audio device type is supported by this implementation
1518//
1519// Inputs:
1520//  channelMask  the channel mask of the input to virtualize
1521//  deviceType   the type of device that affects the processing (e.g. for binaural vs transaural)
1522// Output:
1523//  -EINVAL      if the configuration is not supported or it is unknown
1524//  0            if the configuration is supported
1525//----------------------------------------------------------------------------
1526int VirtualizerIsConfigurationSupported(audio_channel_mask_t channelMask,
1527        audio_devices_t deviceType) {
1528    uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
1529    if ((channelCount == 0) || (channelCount > 2)) {
1530        return -EINVAL;
1531    }
1532
1533    return VirtualizerIsDeviceSupported(deviceType);
1534}
1535
1536//----------------------------------------------------------------------------
1537// VirtualizerForceVirtualizationMode()
1538//----------------------------------------------------------------------------
1539// Purpose:
1540// Force the virtualization mode to that of the given audio device
1541//
1542// Inputs:
1543//  pContext     effect engine context
1544//  forcedDevice the type of device whose virtualization mode we'll always use
1545// Output:
1546//  -EINVAL      if the device is not supported or is unknown
1547//  0            if the device is supported and the virtualization mode forced
1548//
1549//----------------------------------------------------------------------------
1550int VirtualizerForceVirtualizationMode(EffectContext *pContext, audio_devices_t forcedDevice) {
1551    ALOGV("VirtualizerForceVirtualizationMode: forcedDev=0x%x enabled=%d tmpDisabled=%d",
1552            forcedDevice, pContext->pBundledContext->bVirtualizerEnabled,
1553            pContext->pBundledContext->bVirtualizerTempDisabled);
1554    int status = 0;
1555    bool useVirtualizer = false;
1556
1557    if (VirtualizerIsDeviceSupported(forcedDevice) != 0) {
1558        if (forcedDevice != AUDIO_DEVICE_NONE) {
1559            //forced device is not supported, make it behave as a reset of forced mode
1560            forcedDevice = AUDIO_DEVICE_NONE;
1561            // but return an error
1562            status = -EINVAL;
1563        }
1564    }
1565
1566    if (forcedDevice == AUDIO_DEVICE_NONE) {
1567        // disabling forced virtualization mode:
1568        // verify whether the virtualization should be enabled or disabled
1569        if (VirtualizerIsDeviceSupported(pContext->pBundledContext->nOutputDevice) == 0) {
1570            useVirtualizer = (pContext->pBundledContext->bVirtualizerEnabled == LVM_TRUE);
1571        }
1572        pContext->pBundledContext->nVirtualizerForcedDevice = AUDIO_DEVICE_NONE;
1573    } else {
1574        // forcing virtualization mode: here we already know the device is supported
1575        pContext->pBundledContext->nVirtualizerForcedDevice = AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
1576        // only enable for a supported mode, when the effect is enabled
1577        useVirtualizer = (pContext->pBundledContext->bVirtualizerEnabled == LVM_TRUE);
1578    }
1579
1580    if (useVirtualizer) {
1581        if (pContext->pBundledContext->bVirtualizerTempDisabled == LVM_TRUE) {
1582            ALOGV("\tVirtualizerForceVirtualizationMode re-enable LVM_VIRTUALIZER");
1583            android::LvmEffect_enable(pContext);
1584            pContext->pBundledContext->bVirtualizerTempDisabled = LVM_FALSE;
1585        } else {
1586            ALOGV("\tVirtualizerForceVirtualizationMode leaving LVM_VIRTUALIZER enabled");
1587        }
1588    } else {
1589        if (pContext->pBundledContext->bVirtualizerTempDisabled == LVM_FALSE) {
1590            ALOGV("\tVirtualizerForceVirtualizationMode disable LVM_VIRTUALIZER");
1591            android::LvmEffect_disable(pContext);
1592            pContext->pBundledContext->bVirtualizerTempDisabled = LVM_TRUE;
1593        } else {
1594            ALOGV("\tVirtualizerForceVirtualizationMode leaving LVM_VIRTUALIZER disabled");
1595        }
1596    }
1597
1598    ALOGV("\tafter VirtualizerForceVirtualizationMode: enabled=%d tmpDisabled=%d",
1599            pContext->pBundledContext->bVirtualizerEnabled,
1600            pContext->pBundledContext->bVirtualizerTempDisabled);
1601
1602    return status;
1603}
1604//----------------------------------------------------------------------------
1605// VirtualizerGetSpeakerAngles()
1606//----------------------------------------------------------------------------
1607// Purpose:
1608// Get the virtual speaker angles for a channel mask + audio device type
1609// configuration which is guaranteed to be supported by this implementation
1610//
1611// Inputs:
1612//  channelMask:   the channel mask of the input to virtualize
1613//  deviceType     the type of device that affects the processing (e.g. for binaural vs transaural)
1614// Input/Output:
1615//  pSpeakerAngles the array of integer where each speaker angle is written as a triplet in the
1616//                 following format:
1617//                    int32_t a bit mask with a single value selected for each speaker, following
1618//                            the convention of the audio_channel_mask_t type
1619//                    int32_t a value in degrees expressing the speaker azimuth, where 0 is in front
1620//                            of the user, 180 behind, -90 to the left, 90 to the right of the user
1621//                    int32_t a value in degrees expressing the speaker elevation, where 0 is the
1622//                            horizontal plane, +90 is directly above the user, -90 below
1623//
1624//----------------------------------------------------------------------------
1625void VirtualizerGetSpeakerAngles(audio_channel_mask_t channelMask,
1626        audio_devices_t deviceType __unused, int32_t *pSpeakerAngles) {
1627    // the channel count is guaranteed to be 1 or 2
1628    // the device is guaranteed to be of type headphone
1629    // this virtualizer is always using 2 virtual speakers at -90 and 90deg of azimuth, 0deg of
1630    // elevation but the return information is sized for nbChannels * 3, so we have to consider
1631    // the (false here) case of a single channel, and return only 3 fields.
1632    if (audio_channel_count_from_out_mask(channelMask) == 1) {
1633        *pSpeakerAngles++ = (int32_t) AUDIO_CHANNEL_OUT_MONO; // same as FRONT_LEFT
1634        *pSpeakerAngles++ = 0; // azimuth
1635        *pSpeakerAngles = 0; // elevation
1636    } else {
1637        *pSpeakerAngles++ = (int32_t) AUDIO_CHANNEL_OUT_FRONT_LEFT;
1638        *pSpeakerAngles++ = -90; // azimuth
1639        *pSpeakerAngles++ = 0;   // elevation
1640        *pSpeakerAngles++ = (int32_t) AUDIO_CHANNEL_OUT_FRONT_RIGHT;
1641        *pSpeakerAngles++ = 90;  // azimuth
1642        *pSpeakerAngles   = 0;   // elevation
1643    }
1644}
1645
1646//----------------------------------------------------------------------------
1647// VirtualizerGetVirtualizationMode()
1648//----------------------------------------------------------------------------
1649// Purpose:
1650// Retrieve the current device whose processing mode is used by this effect
1651//
1652// Output:
1653//   AUDIO_DEVICE_NONE if the effect is not virtualizing
1654//   or the device type if the effect is virtualizing
1655//----------------------------------------------------------------------------
1656audio_devices_t VirtualizerGetVirtualizationMode(EffectContext *pContext) {
1657    audio_devices_t virtDevice = AUDIO_DEVICE_NONE;
1658    if ((pContext->pBundledContext->bVirtualizerEnabled == LVM_TRUE)
1659            && (pContext->pBundledContext->bVirtualizerTempDisabled == LVM_FALSE)) {
1660        if (pContext->pBundledContext->nVirtualizerForcedDevice != AUDIO_DEVICE_NONE) {
1661            // virtualization mode is forced, return that device
1662            virtDevice = pContext->pBundledContext->nVirtualizerForcedDevice;
1663        } else {
1664            // no forced mode, return the current device
1665            virtDevice = pContext->pBundledContext->nOutputDevice;
1666        }
1667    }
1668    ALOGV("VirtualizerGetVirtualizationMode() returning 0x%x", virtDevice);
1669    return virtDevice;
1670}
1671
1672//----------------------------------------------------------------------------
1673// EqualizerGetBandLevel()
1674//----------------------------------------------------------------------------
1675// Purpose: Retrieve the gain currently being used for the band passed in
1676//
1677// Inputs:
1678//  band:       band number
1679//  pContext:   effect engine context
1680//
1681// Outputs:
1682//
1683//----------------------------------------------------------------------------
1684int32_t EqualizerGetBandLevel(EffectContext *pContext, int32_t band){
1685    //ALOGV("\tEqualizerGetBandLevel -> %d\n", pContext->pBundledContext->bandGaindB[band] );
1686    return pContext->pBundledContext->bandGaindB[band] * 100;
1687}
1688
1689//----------------------------------------------------------------------------
1690// EqualizerSetBandLevel()
1691//----------------------------------------------------------------------------
1692// Purpose:
1693//  Sets gain value for the given band.
1694//
1695// Inputs:
1696//  band:       band number
1697//  Gain:       Gain to be applied in millibels
1698//  pContext:   effect engine context
1699//
1700// Outputs:
1701//
1702//---------------------------------------------------------------------------
1703void EqualizerSetBandLevel(EffectContext *pContext, int band, short Gain){
1704    int gainRounded;
1705    if(Gain > 0){
1706        gainRounded = (int)((Gain+50)/100);
1707    }else{
1708        gainRounded = (int)((Gain-50)/100);
1709    }
1710    //ALOGV("\tEqualizerSetBandLevel(%d)->(%d)", Gain, gainRounded);
1711    pContext->pBundledContext->bandGaindB[band] = gainRounded;
1712    pContext->pBundledContext->CurPreset = PRESET_CUSTOM;
1713
1714    EqualizerUpdateActiveParams(pContext);
1715    LvmEffect_limitLevel(pContext);
1716}
1717
1718//----------------------------------------------------------------------------
1719// EqualizerGetCentreFrequency()
1720//----------------------------------------------------------------------------
1721// Purpose: Retrieve the frequency being used for the band passed in
1722//
1723// Inputs:
1724//  band:       band number
1725//  pContext:   effect engine context
1726//
1727// Outputs:
1728//
1729//----------------------------------------------------------------------------
1730int32_t EqualizerGetCentreFrequency(EffectContext *pContext, int32_t band){
1731    int32_t Frequency =0;
1732
1733    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
1734    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
1735    LVM_EQNB_BandDef_t      *BandDef;
1736    /* Get the current settings */
1737    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,
1738                                         &ActiveParams);
1739
1740    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "EqualizerGetCentreFrequency")
1741
1742    BandDef   = ActiveParams.pEQNB_BandDefinition;
1743    Frequency = (int32_t)BandDef[band].Frequency*1000;     // Convert to millibels
1744
1745    //ALOGV("\tEqualizerGetCentreFrequency -> %d\n", Frequency );
1746    //ALOGV("\tEqualizerGetCentreFrequency Succesfully returned from LVM_GetControlParameters\n");
1747    return Frequency;
1748}
1749
1750//----------------------------------------------------------------------------
1751// EqualizerGetBandFreqRange(
1752//----------------------------------------------------------------------------
1753// Purpose:
1754//
1755// Gets lower and upper boundaries of a band.
1756// For the high shelf, the low bound is the band frequency and the high
1757// bound is Nyquist.
1758// For the peaking filters, they are the gain[dB]/2 points.
1759//
1760// Inputs:
1761//  band:       band number
1762//  pContext:   effect engine context
1763//
1764// Outputs:
1765//  pLow:       lower band range
1766//  pLow:       upper band range
1767//----------------------------------------------------------------------------
1768int32_t EqualizerGetBandFreqRange(EffectContext *pContext __unused, int32_t band, uint32_t *pLow,
1769                                  uint32_t *pHi){
1770    *pLow = bandFreqRange[band][0];
1771    *pHi  = bandFreqRange[band][1];
1772    return 0;
1773}
1774
1775//----------------------------------------------------------------------------
1776// EqualizerGetBand(
1777//----------------------------------------------------------------------------
1778// Purpose:
1779//
1780// Returns the band with the maximum influence on a given frequency.
1781// Result is unaffected by whether EQ is enabled or not, or by whether
1782// changes have been committed or not.
1783//
1784// Inputs:
1785//  targetFreq   The target frequency, in millihertz.
1786//  pContext:    effect engine context
1787//
1788// Outputs:
1789//  pLow:       lower band range
1790//  pLow:       upper band range
1791//----------------------------------------------------------------------------
1792int32_t EqualizerGetBand(EffectContext *pContext __unused, uint32_t targetFreq){
1793    int band = 0;
1794
1795    if(targetFreq < bandFreqRange[0][0]){
1796        return -EINVAL;
1797    }else if(targetFreq == bandFreqRange[0][0]){
1798        return 0;
1799    }
1800    for(int i=0; i<FIVEBAND_NUMBANDS;i++){
1801        if((targetFreq > bandFreqRange[i][0])&&(targetFreq <= bandFreqRange[i][1])){
1802            band = i;
1803        }
1804    }
1805    return band;
1806}
1807
1808//----------------------------------------------------------------------------
1809// EqualizerGetPreset(
1810//----------------------------------------------------------------------------
1811// Purpose:
1812//
1813// Gets the currently set preset ID.
1814// Will return PRESET_CUSTOM in case the EQ parameters have been modified
1815// manually since a preset was set.
1816//
1817// Inputs:
1818//  pContext:    effect engine context
1819//
1820//----------------------------------------------------------------------------
1821int32_t EqualizerGetPreset(EffectContext *pContext){
1822    return pContext->pBundledContext->CurPreset;
1823}
1824
1825//----------------------------------------------------------------------------
1826// EqualizerSetPreset(
1827//----------------------------------------------------------------------------
1828// Purpose:
1829//
1830// Sets the current preset by ID.
1831// All the band parameters will be overridden.
1832//
1833// Inputs:
1834//  pContext:    effect engine context
1835//  preset       The preset ID.
1836//
1837//----------------------------------------------------------------------------
1838void EqualizerSetPreset(EffectContext *pContext, int preset){
1839
1840    //ALOGV("\tEqualizerSetPreset(%d)", preset);
1841    pContext->pBundledContext->CurPreset = preset;
1842
1843    //ActiveParams.pEQNB_BandDefinition = &BandDefs[0];
1844    for (int i=0; i<FIVEBAND_NUMBANDS; i++)
1845    {
1846        pContext->pBundledContext->bandGaindB[i] =
1847                EQNB_5BandSoftPresets[i + preset * FIVEBAND_NUMBANDS];
1848    }
1849
1850    EqualizerUpdateActiveParams(pContext);
1851    LvmEffect_limitLevel(pContext);
1852
1853    //ALOGV("\tEqualizerSetPreset Succesfully called LVM_SetControlParameters\n");
1854    return;
1855}
1856
1857int32_t EqualizerGetNumPresets(){
1858    return sizeof(gEqualizerPresets) / sizeof(PresetConfig);
1859}
1860
1861//----------------------------------------------------------------------------
1862// EqualizerGetPresetName(
1863//----------------------------------------------------------------------------
1864// Purpose:
1865// Gets a human-readable name for a preset ID. Will return "Custom" if
1866// PRESET_CUSTOM is passed.
1867//
1868// Inputs:
1869// preset       The preset ID. Must be less than number of presets.
1870//
1871//-------------------------------------------------------------------------
1872const char * EqualizerGetPresetName(int32_t preset){
1873    //ALOGV("\tEqualizerGetPresetName start(%d)", preset);
1874    if (preset == PRESET_CUSTOM) {
1875        return "Custom";
1876    } else {
1877        return gEqualizerPresets[preset].name;
1878    }
1879    //ALOGV("\tEqualizerGetPresetName end(%d)", preset);
1880    return 0;
1881}
1882
1883//----------------------------------------------------------------------------
1884// VolumeSetVolumeLevel()
1885//----------------------------------------------------------------------------
1886// Purpose:
1887//
1888// Inputs:
1889//  pContext:   effect engine context
1890//  level       level to be applied
1891//
1892//----------------------------------------------------------------------------
1893
1894int VolumeSetVolumeLevel(EffectContext *pContext, int16_t level){
1895
1896    if (level > 0 || level < -9600) {
1897        return -EINVAL;
1898    }
1899
1900    if (pContext->pBundledContext->bMuteEnabled == LVM_TRUE) {
1901        pContext->pBundledContext->levelSaved = level / 100;
1902    } else {
1903        pContext->pBundledContext->volume = level / 100;
1904    }
1905
1906    LvmEffect_limitLevel(pContext);
1907
1908    return 0;
1909}    /* end VolumeSetVolumeLevel */
1910
1911//----------------------------------------------------------------------------
1912// VolumeGetVolumeLevel()
1913//----------------------------------------------------------------------------
1914// Purpose:
1915//
1916// Inputs:
1917//  pContext:   effect engine context
1918//
1919//----------------------------------------------------------------------------
1920
1921int VolumeGetVolumeLevel(EffectContext *pContext, int16_t *level){
1922
1923    if (pContext->pBundledContext->bMuteEnabled == LVM_TRUE) {
1924        *level = pContext->pBundledContext->levelSaved * 100;
1925    } else {
1926        *level = pContext->pBundledContext->volume * 100;
1927    }
1928    return 0;
1929}    /* end VolumeGetVolumeLevel */
1930
1931//----------------------------------------------------------------------------
1932// VolumeSetMute()
1933//----------------------------------------------------------------------------
1934// Purpose:
1935//
1936// Inputs:
1937//  pContext:   effect engine context
1938//  mute:       enable/disable flag
1939//
1940//----------------------------------------------------------------------------
1941
1942int32_t VolumeSetMute(EffectContext *pContext, uint32_t mute){
1943    //ALOGV("\tVolumeSetMute start(%d)", mute);
1944
1945    pContext->pBundledContext->bMuteEnabled = mute;
1946
1947    /* Set appropriate volume level */
1948    if(pContext->pBundledContext->bMuteEnabled == LVM_TRUE){
1949        pContext->pBundledContext->levelSaved = pContext->pBundledContext->volume;
1950        pContext->pBundledContext->volume = -96;
1951    }else{
1952        pContext->pBundledContext->volume = pContext->pBundledContext->levelSaved;
1953    }
1954
1955    LvmEffect_limitLevel(pContext);
1956
1957    return 0;
1958}    /* end setMute */
1959
1960//----------------------------------------------------------------------------
1961// VolumeGetMute()
1962//----------------------------------------------------------------------------
1963// Purpose:
1964//
1965// Inputs:
1966//  pContext:   effect engine context
1967//
1968// Ourputs:
1969//  mute:       enable/disable flag
1970//----------------------------------------------------------------------------
1971
1972int32_t VolumeGetMute(EffectContext *pContext, uint32_t *mute){
1973    //ALOGV("\tVolumeGetMute start");
1974    if((pContext->pBundledContext->bMuteEnabled == LVM_FALSE)||
1975       (pContext->pBundledContext->bMuteEnabled == LVM_TRUE)){
1976        *mute = pContext->pBundledContext->bMuteEnabled;
1977        return 0;
1978    }else{
1979        ALOGV("\tLVM_ERROR : VolumeGetMute read an invalid value from context %d",
1980              pContext->pBundledContext->bMuteEnabled);
1981        return -EINVAL;
1982    }
1983    //ALOGV("\tVolumeGetMute end");
1984}    /* end getMute */
1985
1986int16_t VolumeConvertStereoPosition(int16_t position){
1987    int16_t convertedPosition = 0;
1988
1989    convertedPosition = (int16_t)(((float)position/1000)*96);
1990    return convertedPosition;
1991
1992}
1993
1994//----------------------------------------------------------------------------
1995// VolumeSetStereoPosition()
1996//----------------------------------------------------------------------------
1997// Purpose:
1998//
1999// Inputs:
2000//  pContext:       effect engine context
2001//  position:       stereo position
2002//
2003// Outputs:
2004//----------------------------------------------------------------------------
2005
2006int VolumeSetStereoPosition(EffectContext *pContext, int16_t position){
2007
2008    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
2009    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
2010    LVM_INT16               Balance = 0;
2011
2012
2013
2014    pContext->pBundledContext->positionSaved = position;
2015    Balance = VolumeConvertStereoPosition(pContext->pBundledContext->positionSaved);
2016
2017    //ALOGV("\tVolumeSetStereoPosition start pContext->pBundledContext->positionSaved = %d",
2018    //pContext->pBundledContext->positionSaved);
2019
2020    if(pContext->pBundledContext->bStereoPositionEnabled == LVM_TRUE){
2021
2022        //ALOGV("\tVolumeSetStereoPosition Position to be set is %d %d\n", position, Balance);
2023        pContext->pBundledContext->positionSaved = position;
2024        /* Get the current settings */
2025        LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
2026        LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VolumeSetStereoPosition")
2027        if(LvmStatus != LVM_SUCCESS) return -EINVAL;
2028        //ALOGV("\tVolumeSetStereoPosition Succesfully returned from LVM_GetControlParameters got:"
2029        //     " %d\n", ActiveParams.VC_Balance);
2030
2031        /* Volume parameters */
2032        ActiveParams.VC_Balance  = Balance;
2033        //ALOGV("\tVolumeSetStereoPosition() (-96dB -> +96dB)   -> %d\n", ActiveParams.VC_Balance );
2034
2035        /* Activate the initial settings */
2036        LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
2037        LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "VolumeSetStereoPosition")
2038        if(LvmStatus != LVM_SUCCESS) return -EINVAL;
2039
2040        //ALOGV("\tVolumeSetStereoPosition Succesfully called LVM_SetControlParameters\n");
2041
2042        /* Get the current settings */
2043        LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
2044        LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VolumeSetStereoPosition")
2045        if(LvmStatus != LVM_SUCCESS) return -EINVAL;
2046        //ALOGV("\tVolumeSetStereoPosition Succesfully returned from LVM_GetControlParameters got: "
2047        //     "%d\n", ActiveParams.VC_Balance);
2048    }
2049    else{
2050        //ALOGV("\tVolumeSetStereoPosition Position attempting to set, but not enabled %d %d\n",
2051        //position, Balance);
2052    }
2053    //ALOGV("\tVolumeSetStereoPosition end pContext->pBundledContext->positionSaved = %d\n",
2054    //pContext->pBundledContext->positionSaved);
2055    return 0;
2056}    /* end VolumeSetStereoPosition */
2057
2058
2059//----------------------------------------------------------------------------
2060// VolumeGetStereoPosition()
2061//----------------------------------------------------------------------------
2062// Purpose:
2063//
2064// Inputs:
2065//  pContext:       effect engine context
2066//
2067// Outputs:
2068//  position:       stereo position
2069//----------------------------------------------------------------------------
2070
2071int32_t VolumeGetStereoPosition(EffectContext *pContext, int16_t *position){
2072    //ALOGV("\tVolumeGetStereoPosition start");
2073
2074    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
2075    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
2076    LVM_INT16               balance;
2077
2078    //ALOGV("\tVolumeGetStereoPosition start pContext->pBundledContext->positionSaved = %d",
2079    //pContext->pBundledContext->positionSaved);
2080
2081    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
2082    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VolumeGetStereoPosition")
2083    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
2084
2085    //ALOGV("\tVolumeGetStereoPosition -> %d\n", ActiveParams.VC_Balance);
2086    //ALOGV("\tVolumeGetStereoPosition Succesfully returned from LVM_GetControlParameters\n");
2087
2088    balance = VolumeConvertStereoPosition(pContext->pBundledContext->positionSaved);
2089
2090    if(pContext->pBundledContext->bStereoPositionEnabled == LVM_TRUE){
2091        if(balance != ActiveParams.VC_Balance){
2092            return -EINVAL;
2093        }
2094    }
2095    *position = (LVM_INT16)pContext->pBundledContext->positionSaved;     // Convert dB to millibels
2096    //ALOGV("\tVolumeGetStereoPosition end returning pContext->pBundledContext->positionSaved =%d\n",
2097    //pContext->pBundledContext->positionSaved);
2098    return 0;
2099}    /* end VolumeGetStereoPosition */
2100
2101//----------------------------------------------------------------------------
2102// VolumeEnableStereoPosition()
2103//----------------------------------------------------------------------------
2104// Purpose:
2105//
2106// Inputs:
2107//  pContext:   effect engine context
2108//  mute:       enable/disable flag
2109//
2110//----------------------------------------------------------------------------
2111
2112int32_t VolumeEnableStereoPosition(EffectContext *pContext, uint32_t enabled){
2113    //ALOGV("\tVolumeEnableStereoPosition start()");
2114
2115    pContext->pBundledContext->bStereoPositionEnabled = enabled;
2116
2117    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
2118    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
2119
2120    /* Get the current settings */
2121    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
2122    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VolumeEnableStereoPosition")
2123    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
2124
2125    //ALOGV("\tVolumeEnableStereoPosition Succesfully returned from LVM_GetControlParameters\n");
2126    //ALOGV("\tVolumeEnableStereoPosition to %d, position was %d\n",
2127    //     enabled, ActiveParams.VC_Balance );
2128
2129    /* Set appropriate stereo position */
2130    if(pContext->pBundledContext->bStereoPositionEnabled == LVM_FALSE){
2131        ActiveParams.VC_Balance = 0;
2132    }else{
2133        ActiveParams.VC_Balance  =
2134                            VolumeConvertStereoPosition(pContext->pBundledContext->positionSaved);
2135    }
2136
2137    /* Activate the initial settings */
2138    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
2139    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "VolumeEnableStereoPosition")
2140    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
2141
2142    //ALOGV("\tVolumeEnableStereoPosition Succesfully called LVM_SetControlParameters\n");
2143    //ALOGV("\tVolumeEnableStereoPosition end()\n");
2144    return 0;
2145}    /* end VolumeEnableStereoPosition */
2146
2147//----------------------------------------------------------------------------
2148// BassBoost_getParameter()
2149//----------------------------------------------------------------------------
2150// Purpose:
2151// Get a BassBoost parameter
2152//
2153// Inputs:
2154//  pBassBoost       - handle to instance data
2155//  pParam           - pointer to parameter
2156//  pValue           - pointer to variable to hold retrieved value
2157//  pValueSize       - pointer to value size: maximum size as input
2158//
2159// Outputs:
2160//  *pValue updated with parameter value
2161//  *pValueSize updated with actual value size
2162//
2163//
2164// Side Effects:
2165//
2166//----------------------------------------------------------------------------
2167
2168int BassBoost_getParameter(EffectContext     *pContext,
2169                           void              *pParam,
2170                           uint32_t          *pValueSize,
2171                           void              *pValue){
2172    int status = 0;
2173    int32_t *pParamTemp = (int32_t *)pParam;
2174    int32_t param = *pParamTemp++;
2175
2176    //ALOGV("\tBassBoost_getParameter start");
2177
2178    switch (param){
2179        case BASSBOOST_PARAM_STRENGTH_SUPPORTED:
2180            if (*pValueSize != sizeof(uint32_t)){
2181                ALOGV("\tLVM_ERROR : BassBoost_getParameter() invalid pValueSize1 %d", *pValueSize);
2182                return -EINVAL;
2183            }
2184            *pValueSize = sizeof(uint32_t);
2185            break;
2186        case BASSBOOST_PARAM_STRENGTH:
2187            if (*pValueSize != sizeof(int16_t)){
2188                ALOGV("\tLVM_ERROR : BassBoost_getParameter() invalid pValueSize2 %d", *pValueSize);
2189                return -EINVAL;
2190            }
2191            *pValueSize = sizeof(int16_t);
2192            break;
2193
2194        default:
2195            ALOGV("\tLVM_ERROR : BassBoost_getParameter() invalid param %d", param);
2196            return -EINVAL;
2197    }
2198
2199    switch (param){
2200        case BASSBOOST_PARAM_STRENGTH_SUPPORTED:
2201            *(uint32_t *)pValue = 1;
2202
2203            //ALOGV("\tBassBoost_getParameter() BASSBOOST_PARAM_STRENGTH_SUPPORTED Value is %d",
2204            //        *(uint32_t *)pValue);
2205            break;
2206
2207        case BASSBOOST_PARAM_STRENGTH:
2208            *(int16_t *)pValue = BassGetStrength(pContext);
2209
2210            //ALOGV("\tBassBoost_getParameter() BASSBOOST_PARAM_STRENGTH Value is %d",
2211            //        *(int16_t *)pValue);
2212            break;
2213
2214        default:
2215            ALOGV("\tLVM_ERROR : BassBoost_getParameter() invalid param %d", param);
2216            status = -EINVAL;
2217            break;
2218    }
2219
2220    //ALOGV("\tBassBoost_getParameter end");
2221    return status;
2222} /* end BassBoost_getParameter */
2223
2224//----------------------------------------------------------------------------
2225// BassBoost_setParameter()
2226//----------------------------------------------------------------------------
2227// Purpose:
2228// Set a BassBoost parameter
2229//
2230// Inputs:
2231//  pBassBoost       - handle to instance data
2232//  pParam           - pointer to parameter
2233//  pValue           - pointer to value
2234//
2235// Outputs:
2236//
2237//----------------------------------------------------------------------------
2238
2239int BassBoost_setParameter (EffectContext *pContext, void *pParam, void *pValue){
2240    int status = 0;
2241    int16_t strength;
2242    int32_t *pParamTemp = (int32_t *)pParam;
2243
2244    //ALOGV("\tBassBoost_setParameter start");
2245
2246    switch (*pParamTemp){
2247        case BASSBOOST_PARAM_STRENGTH:
2248            strength = *(int16_t *)pValue;
2249            //ALOGV("\tBassBoost_setParameter() BASSBOOST_PARAM_STRENGTH value is %d", strength);
2250            //ALOGV("\tBassBoost_setParameter() Calling pBassBoost->BassSetStrength");
2251            BassSetStrength(pContext, (int32_t)strength);
2252            //ALOGV("\tBassBoost_setParameter() Called pBassBoost->BassSetStrength");
2253           break;
2254        default:
2255            ALOGV("\tLVM_ERROR : BassBoost_setParameter() invalid param %d", *pParamTemp);
2256            break;
2257    }
2258
2259    //ALOGV("\tBassBoost_setParameter end");
2260    return status;
2261} /* end BassBoost_setParameter */
2262
2263//----------------------------------------------------------------------------
2264// Virtualizer_getParameter()
2265//----------------------------------------------------------------------------
2266// Purpose:
2267// Get a Virtualizer parameter
2268//
2269// Inputs:
2270//  pVirtualizer     - handle to instance data
2271//  pParam           - pointer to parameter
2272//  pValue           - pointer to variable to hold retrieved value
2273//  pValueSize       - pointer to value size: maximum size as input
2274//
2275// Outputs:
2276//  *pValue updated with parameter value
2277//  *pValueSize updated with actual value size
2278//
2279//
2280// Side Effects:
2281//
2282//----------------------------------------------------------------------------
2283
2284int Virtualizer_getParameter(EffectContext        *pContext,
2285                             void                 *pParam,
2286                             uint32_t             *pValueSize,
2287                             void                 *pValue){
2288    int status = 0;
2289    int32_t *pParamTemp = (int32_t *)pParam;
2290    int32_t param = *pParamTemp++;
2291
2292    //ALOGV("\tVirtualizer_getParameter start");
2293
2294    switch (param){
2295        case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
2296            if (*pValueSize != sizeof(uint32_t)){
2297                ALOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid pValueSize %d",*pValueSize);
2298                return -EINVAL;
2299            }
2300            *pValueSize = sizeof(uint32_t);
2301            break;
2302        case VIRTUALIZER_PARAM_STRENGTH:
2303            if (*pValueSize != sizeof(int16_t)){
2304                ALOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid pValueSize2 %d",*pValueSize);
2305                return -EINVAL;
2306            }
2307            *pValueSize = sizeof(int16_t);
2308            break;
2309        case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES:
2310            // return value size can only be interpreted as relative to input value,
2311            // deferring validity check to below
2312            break;
2313        case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE:
2314            if (*pValueSize != sizeof(uint32_t)){
2315                ALOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid pValueSize %d",*pValueSize);
2316                return -EINVAL;
2317            }
2318            *pValueSize = sizeof(uint32_t);
2319            break;
2320        default:
2321            ALOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid param %d", param);
2322            return -EINVAL;
2323    }
2324
2325    switch (param){
2326        case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
2327            *(uint32_t *)pValue = 1;
2328
2329            //ALOGV("\tVirtualizer_getParameter() VIRTUALIZER_PARAM_STRENGTH_SUPPORTED Value is %d",
2330            //        *(uint32_t *)pValue);
2331            break;
2332
2333        case VIRTUALIZER_PARAM_STRENGTH:
2334            *(int16_t *)pValue = VirtualizerGetStrength(pContext);
2335
2336            //ALOGV("\tVirtualizer_getParameter() VIRTUALIZER_PARAM_STRENGTH Value is %d",
2337            //        *(int16_t *)pValue);
2338            break;
2339
2340        case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES: {
2341            const audio_channel_mask_t channelMask = (audio_channel_mask_t) *pParamTemp++;
2342            const audio_devices_t deviceType = (audio_devices_t) *pParamTemp;
2343            uint32_t nbChannels = audio_channel_count_from_out_mask(channelMask);
2344            if (*pValueSize < 3 * nbChannels * sizeof(int32_t)){
2345                ALOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid pValueSize %d",*pValueSize);
2346                return -EINVAL;
2347            }
2348            // verify the configuration is supported
2349            status = VirtualizerIsConfigurationSupported(channelMask, deviceType);
2350            if (status == 0) {
2351                ALOGV("VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES supports mask=0x%x device=0x%x",
2352                        channelMask, deviceType);
2353                // configuration is supported, get the angles
2354                VirtualizerGetSpeakerAngles(channelMask, deviceType, (int32_t *)pValue);
2355            }
2356            }
2357            break;
2358
2359        case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE:
2360            *(uint32_t *)pValue  = (uint32_t) VirtualizerGetVirtualizationMode(pContext);
2361            break;
2362
2363        default:
2364            ALOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid param %d", param);
2365            status = -EINVAL;
2366            break;
2367    }
2368
2369    ALOGV("\tVirtualizer_getParameter end returning status=%d", status);
2370    return status;
2371} /* end Virtualizer_getParameter */
2372
2373//----------------------------------------------------------------------------
2374// Virtualizer_setParameter()
2375//----------------------------------------------------------------------------
2376// Purpose:
2377// Set a Virtualizer parameter
2378//
2379// Inputs:
2380//  pVirtualizer     - handle to instance data
2381//  pParam           - pointer to parameter
2382//  pValue           - pointer to value
2383//
2384// Outputs:
2385//
2386//----------------------------------------------------------------------------
2387
2388int Virtualizer_setParameter (EffectContext *pContext, void *pParam, void *pValue){
2389    int status = 0;
2390    int16_t strength;
2391    int32_t *pParamTemp = (int32_t *)pParam;
2392    int32_t param = *pParamTemp++;
2393
2394    //ALOGV("\tVirtualizer_setParameter start");
2395
2396    switch (param){
2397        case VIRTUALIZER_PARAM_STRENGTH:
2398            strength = *(int16_t *)pValue;
2399            //ALOGV("\tVirtualizer_setParameter() VIRTUALIZER_PARAM_STRENGTH value is %d", strength);
2400            //ALOGV("\tVirtualizer_setParameter() Calling pVirtualizer->setStrength");
2401            VirtualizerSetStrength(pContext, (int32_t)strength);
2402            //ALOGV("\tVirtualizer_setParameter() Called pVirtualizer->setStrength");
2403           break;
2404
2405        case VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE: {
2406            const audio_devices_t deviceType = *(audio_devices_t *) pValue;
2407            status = VirtualizerForceVirtualizationMode(pContext, deviceType);
2408            //ALOGV("VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE device=0x%x result=%d",
2409            //        deviceType, status);
2410            }
2411            break;
2412
2413        default:
2414            ALOGV("\tLVM_ERROR : Virtualizer_setParameter() invalid param %d", param);
2415            break;
2416    }
2417
2418    //ALOGV("\tVirtualizer_setParameter end");
2419    return status;
2420} /* end Virtualizer_setParameter */
2421
2422//----------------------------------------------------------------------------
2423// Equalizer_getParameter()
2424//----------------------------------------------------------------------------
2425// Purpose:
2426// Get a Equalizer parameter
2427//
2428// Inputs:
2429//  pEqualizer       - handle to instance data
2430//  pParam           - pointer to parameter
2431//  pValue           - pointer to variable to hold retrieved value
2432//  pValueSize       - pointer to value size: maximum size as input
2433//
2434// Outputs:
2435//  *pValue updated with parameter value
2436//  *pValueSize updated with actual value size
2437//
2438//
2439// Side Effects:
2440//
2441//----------------------------------------------------------------------------
2442int Equalizer_getParameter(EffectContext     *pContext,
2443                           void              *pParam,
2444                           uint32_t          *pValueSize,
2445                           void              *pValue){
2446    int status = 0;
2447    int32_t *pParamTemp = (int32_t *)pParam;
2448    int32_t param = *pParamTemp++;
2449    int32_t param2;
2450    char *name;
2451
2452    //ALOGV("\tEqualizer_getParameter start");
2453
2454    switch (param) {
2455    case EQ_PARAM_NUM_BANDS:
2456    case EQ_PARAM_CUR_PRESET:
2457    case EQ_PARAM_GET_NUM_OF_PRESETS:
2458    case EQ_PARAM_BAND_LEVEL:
2459    case EQ_PARAM_GET_BAND:
2460        if (*pValueSize < sizeof(int16_t)) {
2461            ALOGV("\tLVM_ERROR : Equalizer_getParameter() invalid pValueSize 1  %d", *pValueSize);
2462            return -EINVAL;
2463        }
2464        *pValueSize = sizeof(int16_t);
2465        break;
2466
2467    case EQ_PARAM_LEVEL_RANGE:
2468        if (*pValueSize < 2 * sizeof(int16_t)) {
2469            ALOGV("\tLVM_ERROR : Equalizer_getParameter() invalid pValueSize 2  %d", *pValueSize);
2470            return -EINVAL;
2471        }
2472        *pValueSize = 2 * sizeof(int16_t);
2473        break;
2474    case EQ_PARAM_BAND_FREQ_RANGE:
2475        if (*pValueSize < 2 * sizeof(int32_t)) {
2476            ALOGV("\tLVM_ERROR : Equalizer_getParameter() invalid pValueSize 3  %d", *pValueSize);
2477            return -EINVAL;
2478        }
2479        *pValueSize = 2 * sizeof(int32_t);
2480        break;
2481
2482    case EQ_PARAM_CENTER_FREQ:
2483        if (*pValueSize < sizeof(int32_t)) {
2484            ALOGV("\tLVM_ERROR : Equalizer_getParameter() invalid pValueSize 5  %d", *pValueSize);
2485            return -EINVAL;
2486        }
2487        *pValueSize = sizeof(int32_t);
2488        break;
2489
2490    case EQ_PARAM_GET_PRESET_NAME:
2491        break;
2492
2493    case EQ_PARAM_PROPERTIES:
2494        if (*pValueSize < (2 + FIVEBAND_NUMBANDS) * sizeof(uint16_t)) {
2495            ALOGV("\tLVM_ERROR : Equalizer_getParameter() invalid pValueSize 1  %d", *pValueSize);
2496            return -EINVAL;
2497        }
2498        *pValueSize = (2 + FIVEBAND_NUMBANDS) * sizeof(uint16_t);
2499        break;
2500
2501    default:
2502        ALOGV("\tLVM_ERROR : Equalizer_getParameter unknown param %d", param);
2503        return -EINVAL;
2504    }
2505
2506    switch (param) {
2507    case EQ_PARAM_NUM_BANDS:
2508        *(uint16_t *)pValue = (uint16_t)FIVEBAND_NUMBANDS;
2509        //ALOGV("\tEqualizer_getParameter() EQ_PARAM_NUM_BANDS %d", *(int16_t *)pValue);
2510        break;
2511
2512    case EQ_PARAM_LEVEL_RANGE:
2513        *(int16_t *)pValue = -1500;
2514        *((int16_t *)pValue + 1) = 1500;
2515        //ALOGV("\tEqualizer_getParameter() EQ_PARAM_LEVEL_RANGE min %d, max %d",
2516        //      *(int16_t *)pValue, *((int16_t *)pValue + 1));
2517        break;
2518
2519    case EQ_PARAM_BAND_LEVEL:
2520        param2 = *pParamTemp;
2521        if (param2 < 0 || param2 >= FIVEBAND_NUMBANDS) {
2522            status = -EINVAL;
2523            if (param2 < 0) {
2524                android_errorWriteLog(0x534e4554, "32438598");
2525                ALOGW("\tERROR Equalizer_getParameter() EQ_PARAM_BAND_LEVEL band %d", param2);
2526            }
2527            break;
2528        }
2529        *(int16_t *)pValue = (int16_t)EqualizerGetBandLevel(pContext, param2);
2530        //ALOGV("\tEqualizer_getParameter() EQ_PARAM_BAND_LEVEL band %d, level %d",
2531        //      param2, *(int32_t *)pValue);
2532        break;
2533
2534    case EQ_PARAM_CENTER_FREQ:
2535        param2 = *pParamTemp;
2536        if (param2 < 0 || param2 >= FIVEBAND_NUMBANDS) {
2537            status = -EINVAL;
2538            if (param2 < 0) {
2539                android_errorWriteLog(0x534e4554, "32436341");
2540                ALOGW("\tERROR Equalizer_getParameter() EQ_PARAM_CENTER_FREQ band %d", param2);
2541            }
2542            break;
2543        }
2544        *(int32_t *)pValue = EqualizerGetCentreFrequency(pContext, param2);
2545        //ALOGV("\tEqualizer_getParameter() EQ_PARAM_CENTER_FREQ band %d, frequency %d",
2546        //      param2, *(int32_t *)pValue);
2547        break;
2548
2549    case EQ_PARAM_BAND_FREQ_RANGE:
2550        param2 = *pParamTemp;
2551        if (param2 < 0 || param2 >= FIVEBAND_NUMBANDS) {
2552            status = -EINVAL;
2553            if (param2 < 0) {
2554                android_errorWriteLog(0x534e4554, "32247948");
2555                ALOGW("\tERROR Equalizer_getParameter() EQ_PARAM_BAND_FREQ_RANGE band %d", param2);
2556            }
2557            break;
2558        }
2559        EqualizerGetBandFreqRange(pContext, param2, (uint32_t *)pValue, ((uint32_t *)pValue + 1));
2560        //ALOGV("\tEqualizer_getParameter() EQ_PARAM_BAND_FREQ_RANGE band %d, min %d, max %d",
2561        //      param2, *(int32_t *)pValue, *((int32_t *)pValue + 1));
2562        break;
2563
2564    case EQ_PARAM_GET_BAND:
2565        param2 = *pParamTemp;
2566        *(uint16_t *)pValue = (uint16_t)EqualizerGetBand(pContext, param2);
2567        //ALOGV("\tEqualizer_getParameter() EQ_PARAM_GET_BAND frequency %d, band %d",
2568        //      param2, *(uint16_t *)pValue);
2569        break;
2570
2571    case EQ_PARAM_CUR_PRESET:
2572        *(uint16_t *)pValue = (uint16_t)EqualizerGetPreset(pContext);
2573        //ALOGV("\tEqualizer_getParameter() EQ_PARAM_CUR_PRESET %d", *(int32_t *)pValue);
2574        break;
2575
2576    case EQ_PARAM_GET_NUM_OF_PRESETS:
2577        *(uint16_t *)pValue = (uint16_t)EqualizerGetNumPresets();
2578        //ALOGV("\tEqualizer_getParameter() EQ_PARAM_GET_NUM_OF_PRESETS %d", *(int16_t *)pValue);
2579        break;
2580
2581    case EQ_PARAM_GET_PRESET_NAME:
2582        param2 = *pParamTemp;
2583        if ((param2 < 0 && param2 != PRESET_CUSTOM) ||  param2 >= EqualizerGetNumPresets()) {
2584            status = -EINVAL;
2585            if (param2 < 0) {
2586                android_errorWriteLog(0x534e4554, "32448258");
2587                ALOGE("\tERROR Equalizer_getParameter() EQ_PARAM_GET_PRESET_NAME preset %d",
2588                        param2);
2589            }
2590            break;
2591        }
2592
2593        if (*pValueSize < 1) {
2594            status = -EINVAL;
2595            android_errorWriteLog(0x534e4554, "37536407");
2596            break;
2597        }
2598
2599        name = (char *)pValue;
2600        strncpy(name, EqualizerGetPresetName(param2), *pValueSize - 1);
2601        name[*pValueSize - 1] = 0;
2602        *pValueSize = strlen(name) + 1;
2603        //ALOGV("\tEqualizer_getParameter() EQ_PARAM_GET_PRESET_NAME preset %d, name %s len %d",
2604        //      param2, gEqualizerPresets[param2].name, *pValueSize);
2605        break;
2606
2607    case EQ_PARAM_PROPERTIES: {
2608        int16_t *p = (int16_t *)pValue;
2609        ALOGV("\tEqualizer_getParameter() EQ_PARAM_PROPERTIES");
2610        p[0] = (int16_t)EqualizerGetPreset(pContext);
2611        p[1] = (int16_t)FIVEBAND_NUMBANDS;
2612        for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
2613            p[2 + i] = (int16_t)EqualizerGetBandLevel(pContext, i);
2614        }
2615    } break;
2616
2617    default:
2618        ALOGV("\tLVM_ERROR : Equalizer_getParameter() invalid param %d", param);
2619        status = -EINVAL;
2620        break;
2621    }
2622
2623    //GV("\tEqualizer_getParameter end\n");
2624    return status;
2625} /* end Equalizer_getParameter */
2626
2627//----------------------------------------------------------------------------
2628// Equalizer_setParameter()
2629//----------------------------------------------------------------------------
2630// Purpose:
2631// Set a Equalizer parameter
2632//
2633// Inputs:
2634//  pEqualizer    - handle to instance data
2635//  pParam        - pointer to parameter
2636//  valueSize     - value size
2637//  pValue        - pointer to value
2638
2639//
2640// Outputs:
2641//
2642//----------------------------------------------------------------------------
2643int Equalizer_setParameter (EffectContext *pContext,
2644                            void *pParam,
2645                            uint32_t valueSize,
2646                            void *pValue) {
2647    int status = 0;
2648    int32_t preset;
2649    int32_t band;
2650    int32_t level;
2651    int32_t *pParamTemp = (int32_t *)pParam;
2652    int32_t param = *pParamTemp++;
2653
2654
2655    //ALOGV("\tEqualizer_setParameter start");
2656    switch (param) {
2657    case EQ_PARAM_CUR_PRESET:
2658        if (valueSize < sizeof(int16_t)) {
2659          status = -EINVAL;
2660          break;
2661        }
2662        preset = (int32_t)(*(uint16_t *)pValue);
2663
2664        //ALOGV("\tEqualizer_setParameter() EQ_PARAM_CUR_PRESET %d", preset);
2665        if ((preset >= EqualizerGetNumPresets())||(preset < 0)) {
2666            status = -EINVAL;
2667            break;
2668        }
2669        EqualizerSetPreset(pContext, preset);
2670        break;
2671    case EQ_PARAM_BAND_LEVEL:
2672        if (valueSize < sizeof(int16_t)) {
2673          status = -EINVAL;
2674          break;
2675        }
2676        band =  *pParamTemp;
2677        level = (int32_t)(*(int16_t *)pValue);
2678        //ALOGV("\tEqualizer_setParameter() EQ_PARAM_BAND_LEVEL band %d, level %d", band, level);
2679        if (band < 0 || band >= FIVEBAND_NUMBANDS) {
2680            status = -EINVAL;
2681            if (band < 0) {
2682                android_errorWriteLog(0x534e4554, "32095626");
2683                ALOGE("\tERROR Equalizer_setParameter() EQ_PARAM_BAND_LEVEL band %d", band);
2684            }
2685            break;
2686        }
2687        EqualizerSetBandLevel(pContext, band, level);
2688        break;
2689    case EQ_PARAM_PROPERTIES: {
2690        //ALOGV("\tEqualizer_setParameter() EQ_PARAM_PROPERTIES");
2691        if (valueSize < sizeof(int16_t)) {
2692          status = -EINVAL;
2693          break;
2694        }
2695        int16_t *p = (int16_t *)pValue;
2696        if ((int)p[0] >= EqualizerGetNumPresets()) {
2697            status = -EINVAL;
2698            break;
2699        }
2700        if (p[0] >= 0) {
2701            EqualizerSetPreset(pContext, (int)p[0]);
2702        } else {
2703            if (valueSize < (2 + FIVEBAND_NUMBANDS) * sizeof(int16_t)) {
2704              android_errorWriteLog(0x534e4554, "37563371");
2705              ALOGE("\tERROR Equalizer_setParameter() EQ_PARAM_PROPERTIES valueSize %d < %d",
2706                    (int)valueSize, (int)((2 + FIVEBAND_NUMBANDS) * sizeof(int16_t)));
2707              status = -EINVAL;
2708              break;
2709            }
2710            if ((int)p[1] != FIVEBAND_NUMBANDS) {
2711                status = -EINVAL;
2712                break;
2713            }
2714            for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
2715                EqualizerSetBandLevel(pContext, i, (int)p[2 + i]);
2716            }
2717        }
2718    } break;
2719    default:
2720        ALOGV("\tLVM_ERROR : Equalizer_setParameter() invalid param %d", param);
2721        status = -EINVAL;
2722        break;
2723    }
2724
2725    //ALOGV("\tEqualizer_setParameter end");
2726    return status;
2727} /* end Equalizer_setParameter */
2728
2729//----------------------------------------------------------------------------
2730// Volume_getParameter()
2731//----------------------------------------------------------------------------
2732// Purpose:
2733// Get a Volume parameter
2734//
2735// Inputs:
2736//  pVolume          - handle to instance data
2737//  pParam           - pointer to parameter
2738//  pValue           - pointer to variable to hold retrieved value
2739//  pValueSize       - pointer to value size: maximum size as input
2740//
2741// Outputs:
2742//  *pValue updated with parameter value
2743//  *pValueSize updated with actual value size
2744//
2745//
2746// Side Effects:
2747//
2748//----------------------------------------------------------------------------
2749
2750int Volume_getParameter(EffectContext     *pContext,
2751                        void              *pParam,
2752                        uint32_t          *pValueSize,
2753                        void              *pValue){
2754    int status = 0;
2755    int32_t *pParamTemp = (int32_t *)pParam;
2756    int32_t param = *pParamTemp++;;
2757
2758    //ALOGV("\tVolume_getParameter start");
2759
2760    switch (param){
2761        case VOLUME_PARAM_LEVEL:
2762        case VOLUME_PARAM_MAXLEVEL:
2763        case VOLUME_PARAM_STEREOPOSITION:
2764            if (*pValueSize != sizeof(int16_t)){
2765                ALOGV("\tLVM_ERROR : Volume_getParameter() invalid pValueSize 1  %d", *pValueSize);
2766                return -EINVAL;
2767            }
2768            *pValueSize = sizeof(int16_t);
2769            break;
2770
2771        case VOLUME_PARAM_MUTE:
2772        case VOLUME_PARAM_ENABLESTEREOPOSITION:
2773            if (*pValueSize < sizeof(int32_t)){
2774                ALOGV("\tLVM_ERROR : Volume_getParameter() invalid pValueSize 2  %d", *pValueSize);
2775                return -EINVAL;
2776            }
2777            *pValueSize = sizeof(int32_t);
2778            break;
2779
2780        default:
2781            ALOGV("\tLVM_ERROR : Volume_getParameter unknown param %d", param);
2782            return -EINVAL;
2783    }
2784
2785    switch (param){
2786        case VOLUME_PARAM_LEVEL:
2787            status = VolumeGetVolumeLevel(pContext, (int16_t *)(pValue));
2788            //ALOGV("\tVolume_getParameter() VOLUME_PARAM_LEVEL Value is %d",
2789            //        *(int16_t *)pValue);
2790            break;
2791
2792        case VOLUME_PARAM_MAXLEVEL:
2793            *(int16_t *)pValue = 0;
2794            //ALOGV("\tVolume_getParameter() VOLUME_PARAM_MAXLEVEL Value is %d",
2795            //        *(int16_t *)pValue);
2796            break;
2797
2798        case VOLUME_PARAM_STEREOPOSITION:
2799            VolumeGetStereoPosition(pContext, (int16_t *)pValue);
2800            //ALOGV("\tVolume_getParameter() VOLUME_PARAM_STEREOPOSITION Value is %d",
2801            //        *(int16_t *)pValue);
2802            break;
2803
2804        case VOLUME_PARAM_MUTE:
2805            status = VolumeGetMute(pContext, (uint32_t *)pValue);
2806            ALOGV("\tVolume_getParameter() VOLUME_PARAM_MUTE Value is %d",
2807                    *(uint32_t *)pValue);
2808            break;
2809
2810        case VOLUME_PARAM_ENABLESTEREOPOSITION:
2811            *(int32_t *)pValue = pContext->pBundledContext->bStereoPositionEnabled;
2812            //ALOGV("\tVolume_getParameter() VOLUME_PARAM_ENABLESTEREOPOSITION Value is %d",
2813            //        *(uint32_t *)pValue);
2814            break;
2815
2816        default:
2817            ALOGV("\tLVM_ERROR : Volume_getParameter() invalid param %d", param);
2818            status = -EINVAL;
2819            break;
2820    }
2821
2822    //ALOGV("\tVolume_getParameter end");
2823    return status;
2824} /* end Volume_getParameter */
2825
2826
2827//----------------------------------------------------------------------------
2828// Volume_setParameter()
2829//----------------------------------------------------------------------------
2830// Purpose:
2831// Set a Volume parameter
2832//
2833// Inputs:
2834//  pVolume       - handle to instance data
2835//  pParam        - pointer to parameter
2836//  pValue        - pointer to value
2837//
2838// Outputs:
2839//
2840//----------------------------------------------------------------------------
2841
2842int Volume_setParameter (EffectContext *pContext, void *pParam, void *pValue){
2843    int      status = 0;
2844    int16_t  level;
2845    int16_t  position;
2846    uint32_t mute;
2847    uint32_t positionEnabled;
2848    int32_t *pParamTemp = (int32_t *)pParam;
2849    int32_t param = *pParamTemp++;
2850
2851    //ALOGV("\tVolume_setParameter start");
2852
2853    switch (param){
2854        case VOLUME_PARAM_LEVEL:
2855            level = *(int16_t *)pValue;
2856            //ALOGV("\tVolume_setParameter() VOLUME_PARAM_LEVEL value is %d", level);
2857            //ALOGV("\tVolume_setParameter() Calling pVolume->setVolumeLevel");
2858            status = VolumeSetVolumeLevel(pContext, (int16_t)level);
2859            //ALOGV("\tVolume_setParameter() Called pVolume->setVolumeLevel");
2860            break;
2861
2862        case VOLUME_PARAM_MUTE:
2863            mute = *(uint32_t *)pValue;
2864            //ALOGV("\tVolume_setParameter() Calling pVolume->setMute, mute is %d", mute);
2865            //ALOGV("\tVolume_setParameter() Calling pVolume->setMute");
2866            status = VolumeSetMute(pContext, mute);
2867            //ALOGV("\tVolume_setParameter() Called pVolume->setMute");
2868            break;
2869
2870        case VOLUME_PARAM_ENABLESTEREOPOSITION:
2871            positionEnabled = *(uint32_t *)pValue;
2872            (void) VolumeEnableStereoPosition(pContext, positionEnabled);
2873            (void) VolumeSetStereoPosition(pContext, pContext->pBundledContext->positionSaved);
2874            //ALOGV("\tVolume_setParameter() VOLUME_PARAM_ENABLESTEREOPOSITION called");
2875            break;
2876
2877        case VOLUME_PARAM_STEREOPOSITION:
2878            position = *(int16_t *)pValue;
2879            //ALOGV("\tVolume_setParameter() VOLUME_PARAM_STEREOPOSITION value is %d", position);
2880            //ALOGV("\tVolume_setParameter() Calling pVolume->VolumeSetStereoPosition");
2881            status = VolumeSetStereoPosition(pContext, (int16_t)position);
2882            //ALOGV("\tVolume_setParameter() Called pVolume->VolumeSetStereoPosition");
2883            break;
2884
2885        default:
2886            ALOGV("\tLVM_ERROR : Volume_setParameter() invalid param %d", param);
2887            break;
2888    }
2889
2890    //ALOGV("\tVolume_setParameter end");
2891    return status;
2892} /* end Volume_setParameter */
2893
2894/****************************************************************************************
2895 * Name : LVC_ToDB_s32Tos16()
2896 *  Input       : Signed 32-bit integer
2897 *  Output      : Signed 16-bit integer
2898 *                  MSB (16) = sign bit
2899 *                  (15->05) = integer part
2900 *                  (04->01) = decimal part
2901 *  Returns     : Db value with respect to full scale
2902 *  Description :
2903 *  Remarks     :
2904 ****************************************************************************************/
2905
2906LVM_INT16 LVC_ToDB_s32Tos16(LVM_INT32 Lin_fix)
2907{
2908    LVM_INT16   db_fix;
2909    LVM_INT16   Shift;
2910    LVM_INT16   SmallRemainder;
2911    LVM_UINT32  Remainder = (LVM_UINT32)Lin_fix;
2912
2913    /* Count leading bits, 1 cycle in assembly*/
2914    for (Shift = 0; Shift<32; Shift++)
2915    {
2916        if ((Remainder & 0x80000000U)!=0)
2917        {
2918            break;
2919        }
2920        Remainder = Remainder << 1;
2921    }
2922
2923    /*
2924     * Based on the approximation equation (for Q11.4 format):
2925     *
2926     * dB = -96 * Shift + 16 * (8 * Remainder - 2 * Remainder^2)
2927     */
2928    db_fix    = (LVM_INT16)(-96 * Shift);               /* Six dB steps in Q11.4 format*/
2929    SmallRemainder = (LVM_INT16)((Remainder & 0x7fffffff) >> 24);
2930    db_fix = (LVM_INT16)(db_fix + SmallRemainder );
2931    SmallRemainder = (LVM_INT16)(SmallRemainder * SmallRemainder);
2932    db_fix = (LVM_INT16)(db_fix - (LVM_INT16)((LVM_UINT16)SmallRemainder >> 9));
2933
2934    /* Correct for small offset */
2935    db_fix = (LVM_INT16)(db_fix - 5);
2936
2937    return db_fix;
2938}
2939
2940//----------------------------------------------------------------------------
2941// Effect_setEnabled()
2942//----------------------------------------------------------------------------
2943// Purpose:
2944// Enable or disable effect
2945//
2946// Inputs:
2947//  pContext      - pointer to effect context
2948//  enabled       - true if enabling the effect, false otherwise
2949//
2950// Outputs:
2951//
2952//----------------------------------------------------------------------------
2953
2954int Effect_setEnabled(EffectContext *pContext, bool enabled)
2955{
2956    ALOGV("\tEffect_setEnabled() type %d, enabled %d", pContext->EffectType, enabled);
2957
2958    if (enabled) {
2959        // Bass boost or Virtualizer can be temporarily disabled if playing over device speaker due
2960        // to their nature.
2961        bool tempDisabled = false;
2962        switch (pContext->EffectType) {
2963            case LVM_BASS_BOOST:
2964                if (pContext->pBundledContext->bBassEnabled == LVM_TRUE) {
2965                     ALOGV("\tEffect_setEnabled() LVM_BASS_BOOST is already enabled");
2966                     return -EINVAL;
2967                }
2968                if(pContext->pBundledContext->SamplesToExitCountBb <= 0){
2969                    pContext->pBundledContext->NumberEffectsEnabled++;
2970                }
2971                pContext->pBundledContext->SamplesToExitCountBb =
2972                     (LVM_INT32)(pContext->pBundledContext->SamplesPerSecond*0.1);
2973                pContext->pBundledContext->bBassEnabled = LVM_TRUE;
2974                tempDisabled = pContext->pBundledContext->bBassTempDisabled;
2975                break;
2976            case LVM_EQUALIZER:
2977                if (pContext->pBundledContext->bEqualizerEnabled == LVM_TRUE) {
2978                    ALOGV("\tEffect_setEnabled() LVM_EQUALIZER is already enabled");
2979                    return -EINVAL;
2980                }
2981                if(pContext->pBundledContext->SamplesToExitCountEq <= 0){
2982                    pContext->pBundledContext->NumberEffectsEnabled++;
2983                }
2984                pContext->pBundledContext->SamplesToExitCountEq =
2985                     (LVM_INT32)(pContext->pBundledContext->SamplesPerSecond*0.1);
2986                pContext->pBundledContext->bEqualizerEnabled = LVM_TRUE;
2987                break;
2988            case LVM_VIRTUALIZER:
2989                if (pContext->pBundledContext->bVirtualizerEnabled == LVM_TRUE) {
2990                    ALOGV("\tEffect_setEnabled() LVM_VIRTUALIZER is already enabled");
2991                    return -EINVAL;
2992                }
2993                if(pContext->pBundledContext->SamplesToExitCountVirt <= 0){
2994                    pContext->pBundledContext->NumberEffectsEnabled++;
2995                }
2996                pContext->pBundledContext->SamplesToExitCountVirt =
2997                     (LVM_INT32)(pContext->pBundledContext->SamplesPerSecond*0.1);
2998                pContext->pBundledContext->bVirtualizerEnabled = LVM_TRUE;
2999                tempDisabled = pContext->pBundledContext->bVirtualizerTempDisabled;
3000                break;
3001            case LVM_VOLUME:
3002                if (pContext->pBundledContext->bVolumeEnabled == LVM_TRUE) {
3003                    ALOGV("\tEffect_setEnabled() LVM_VOLUME is already enabled");
3004                    return -EINVAL;
3005                }
3006                pContext->pBundledContext->NumberEffectsEnabled++;
3007                pContext->pBundledContext->bVolumeEnabled = LVM_TRUE;
3008                break;
3009            default:
3010                ALOGV("\tEffect_setEnabled() invalid effect type");
3011                return -EINVAL;
3012        }
3013        if (!tempDisabled) {
3014            LvmEffect_enable(pContext);
3015        }
3016    } else {
3017        switch (pContext->EffectType) {
3018            case LVM_BASS_BOOST:
3019                if (pContext->pBundledContext->bBassEnabled == LVM_FALSE) {
3020                    ALOGV("\tEffect_setEnabled() LVM_BASS_BOOST is already disabled");
3021                    return -EINVAL;
3022                }
3023                pContext->pBundledContext->bBassEnabled = LVM_FALSE;
3024                break;
3025            case LVM_EQUALIZER:
3026                if (pContext->pBundledContext->bEqualizerEnabled == LVM_FALSE) {
3027                    ALOGV("\tEffect_setEnabled() LVM_EQUALIZER is already disabled");
3028                    return -EINVAL;
3029                }
3030                pContext->pBundledContext->bEqualizerEnabled = LVM_FALSE;
3031                break;
3032            case LVM_VIRTUALIZER:
3033                if (pContext->pBundledContext->bVirtualizerEnabled == LVM_FALSE) {
3034                    ALOGV("\tEffect_setEnabled() LVM_VIRTUALIZER is already disabled");
3035                    return -EINVAL;
3036                }
3037                pContext->pBundledContext->bVirtualizerEnabled = LVM_FALSE;
3038                break;
3039            case LVM_VOLUME:
3040                if (pContext->pBundledContext->bVolumeEnabled == LVM_FALSE) {
3041                    ALOGV("\tEffect_setEnabled() LVM_VOLUME is already disabled");
3042                    return -EINVAL;
3043                }
3044                pContext->pBundledContext->bVolumeEnabled = LVM_FALSE;
3045                break;
3046            default:
3047                ALOGV("\tEffect_setEnabled() invalid effect type");
3048                return -EINVAL;
3049        }
3050        LvmEffect_disable(pContext);
3051    }
3052
3053    return 0;
3054}
3055
3056//----------------------------------------------------------------------------
3057// LVC_Convert_VolToDb()
3058//----------------------------------------------------------------------------
3059// Purpose:
3060// Convery volume in Q24 to dB
3061//
3062// Inputs:
3063//  vol:   Q.24 volume dB
3064//
3065//-----------------------------------------------------------------------
3066
3067int16_t LVC_Convert_VolToDb(uint32_t vol){
3068    int16_t  dB;
3069
3070    dB = LVC_ToDB_s32Tos16(vol <<7);
3071    dB = (dB +8)>>4;
3072    dB = (dB <-96) ? -96 : dB ;
3073
3074    return dB;
3075}
3076
3077} // namespace
3078} // namespace
3079
3080extern "C" {
3081/* Effect Control Interface Implementation: Process */
3082int Effect_process(effect_handle_t     self,
3083                              audio_buffer_t         *inBuffer,
3084                              audio_buffer_t         *outBuffer){
3085    EffectContext * pContext = (EffectContext *) self;
3086    int    status = 0;
3087    int    processStatus = 0;
3088
3089//ALOGV("\tEffect_process Start : Enabled = %d     Called = %d (%8d %8d %8d)",
3090//pContext->pBundledContext->NumberEffectsEnabled,pContext->pBundledContext->NumberEffectsCalled,
3091//    pContext->pBundledContext->SamplesToExitCountBb,
3092//    pContext->pBundledContext->SamplesToExitCountVirt,
3093//    pContext->pBundledContext->SamplesToExitCountEq);
3094
3095    if (pContext == NULL){
3096        ALOGV("\tLVM_ERROR : Effect_process() ERROR pContext == NULL");
3097        return -EINVAL;
3098    }
3099
3100    //if(pContext->EffectType == LVM_BASS_BOOST){
3101    //  ALOGV("\tEffect_process: Effect type is BASS_BOOST");
3102    //}else if(pContext->EffectType == LVM_EQUALIZER){
3103    //  ALOGV("\tEffect_process: Effect type is LVM_EQUALIZER");
3104    //}else if(pContext->EffectType == LVM_VIRTUALIZER){
3105    //  ALOGV("\tEffect_process: Effect type is LVM_VIRTUALIZER");
3106    //}
3107
3108    if (inBuffer == NULL  || inBuffer->raw == NULL  ||
3109            outBuffer == NULL || outBuffer->raw == NULL ||
3110            inBuffer->frameCount != outBuffer->frameCount){
3111        ALOGV("\tLVM_ERROR : Effect_process() ERROR NULL INPUT POINTER OR FRAME COUNT IS WRONG");
3112        return -EINVAL;
3113    }
3114    if ((pContext->pBundledContext->bBassEnabled == LVM_FALSE)&&
3115        (pContext->EffectType == LVM_BASS_BOOST)){
3116        //ALOGV("\tEffect_process() LVM_BASS_BOOST Effect is not enabled");
3117        if(pContext->pBundledContext->SamplesToExitCountBb > 0){
3118            pContext->pBundledContext->SamplesToExitCountBb -= outBuffer->frameCount * 2; // STEREO
3119            //ALOGV("\tEffect_process: Waiting to turn off BASS_BOOST, %d samples left",
3120            //    pContext->pBundledContext->SamplesToExitCountBb);
3121        }
3122        if(pContext->pBundledContext->SamplesToExitCountBb <= 0) {
3123            status = -ENODATA;
3124            pContext->pBundledContext->NumberEffectsEnabled--;
3125            ALOGV("\tEffect_process() this is the last frame for LVM_BASS_BOOST");
3126        }
3127    }
3128    if ((pContext->pBundledContext->bVolumeEnabled == LVM_FALSE)&&
3129        (pContext->EffectType == LVM_VOLUME)){
3130        //ALOGV("\tEffect_process() LVM_VOLUME Effect is not enabled");
3131        status = -ENODATA;
3132        pContext->pBundledContext->NumberEffectsEnabled--;
3133    }
3134    if ((pContext->pBundledContext->bEqualizerEnabled == LVM_FALSE)&&
3135        (pContext->EffectType == LVM_EQUALIZER)){
3136        //ALOGV("\tEffect_process() LVM_EQUALIZER Effect is not enabled");
3137        if(pContext->pBundledContext->SamplesToExitCountEq > 0){
3138            pContext->pBundledContext->SamplesToExitCountEq -= outBuffer->frameCount * 2; // STEREO
3139            //ALOGV("\tEffect_process: Waiting to turn off EQUALIZER, %d samples left",
3140            //    pContext->pBundledContext->SamplesToExitCountEq);
3141        }
3142        if(pContext->pBundledContext->SamplesToExitCountEq <= 0) {
3143            status = -ENODATA;
3144            pContext->pBundledContext->NumberEffectsEnabled--;
3145            ALOGV("\tEffect_process() this is the last frame for LVM_EQUALIZER");
3146        }
3147    }
3148    if ((pContext->pBundledContext->bVirtualizerEnabled == LVM_FALSE)&&
3149        (pContext->EffectType == LVM_VIRTUALIZER)){
3150        //ALOGV("\tEffect_process() LVM_VIRTUALIZER Effect is not enabled");
3151        if(pContext->pBundledContext->SamplesToExitCountVirt > 0){
3152            pContext->pBundledContext->SamplesToExitCountVirt -= outBuffer->frameCount * 2;// STEREO
3153            //ALOGV("\tEffect_process: Waiting for to turn off VIRTUALIZER, %d samples left",
3154            //    pContext->pBundledContext->SamplesToExitCountVirt);
3155        }
3156        if(pContext->pBundledContext->SamplesToExitCountVirt <= 0) {
3157            status = -ENODATA;
3158            pContext->pBundledContext->NumberEffectsEnabled--;
3159            ALOGV("\tEffect_process() this is the last frame for LVM_VIRTUALIZER");
3160        }
3161    }
3162
3163    if(status != -ENODATA){
3164        pContext->pBundledContext->NumberEffectsCalled++;
3165    }
3166
3167    if(pContext->pBundledContext->NumberEffectsCalled ==
3168       pContext->pBundledContext->NumberEffectsEnabled){
3169        //ALOGV("\tEffect_process     Calling process with %d effects enabled, %d called: Effect %d",
3170        //pContext->pBundledContext->NumberEffectsEnabled,
3171        //pContext->pBundledContext->NumberEffectsCalled, pContext->EffectType);
3172
3173        if (status == -ENODATA){
3174            ALOGV("\tEffect_process() processing last frame");
3175        }
3176        pContext->pBundledContext->NumberEffectsCalled = 0;
3177        /* Process all the available frames, block processing is
3178           handled internalLY by the LVM bundle */
3179        processStatus = android::LvmBundle_process(    (LVM_INT16 *)inBuffer->raw,
3180                                                (LVM_INT16 *)outBuffer->raw,
3181                                                outBuffer->frameCount,
3182                                                pContext);
3183        if (processStatus != 0){
3184            ALOGV("\tLVM_ERROR : LvmBundle_process returned error %d", processStatus);
3185            if (status == 0) {
3186                status = processStatus;
3187            }
3188            return status;
3189        }
3190    } else {
3191        //ALOGV("\tEffect_process Not Calling process with %d effects enabled, %d called: Effect %d",
3192        //pContext->pBundledContext->NumberEffectsEnabled,
3193        //pContext->pBundledContext->NumberEffectsCalled, pContext->EffectType);
3194        // 2 is for stereo input
3195        if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
3196            for (size_t i=0; i < outBuffer->frameCount*2; i++){
3197                outBuffer->s16[i] =
3198                        clamp16((LVM_INT32)outBuffer->s16[i] + (LVM_INT32)inBuffer->s16[i]);
3199            }
3200        } else if (outBuffer->raw != inBuffer->raw) {
3201            memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount*sizeof(LVM_INT16)*2);
3202        }
3203    }
3204
3205    return status;
3206}   /* end Effect_process */
3207
3208/* Effect Control Interface Implementation: Command */
3209int Effect_command(effect_handle_t  self,
3210                              uint32_t            cmdCode,
3211                              uint32_t            cmdSize,
3212                              void                *pCmdData,
3213                              uint32_t            *replySize,
3214                              void                *pReplyData){
3215    EffectContext * pContext = (EffectContext *) self;
3216
3217    //ALOGV("\t\nEffect_command start");
3218
3219    if(pContext->EffectType == LVM_BASS_BOOST){
3220        //ALOGV("\tEffect_command setting command for LVM_BASS_BOOST");
3221    }
3222    if(pContext->EffectType == LVM_VIRTUALIZER){
3223        //ALOGV("\tEffect_command setting command for LVM_VIRTUALIZER");
3224    }
3225    if(pContext->EffectType == LVM_EQUALIZER){
3226        //ALOGV("\tEffect_command setting command for LVM_EQUALIZER");
3227    }
3228    if(pContext->EffectType == LVM_VOLUME){
3229        //ALOGV("\tEffect_command setting command for LVM_VOLUME");
3230    }
3231
3232    if (pContext == NULL){
3233        ALOGV("\tLVM_ERROR : Effect_command ERROR pContext == NULL");
3234        return -EINVAL;
3235    }
3236
3237    //ALOGV("\tEffect_command INPUTS are: command %d cmdSize %d",cmdCode, cmdSize);
3238
3239    // Incase we disable an effect, next time process is
3240    // called the number of effect called could be greater
3241    // pContext->pBundledContext->NumberEffectsCalled = 0;
3242
3243    //ALOGV("\tEffect_command NumberEffectsCalled = %d, NumberEffectsEnabled = %d",
3244    //        pContext->pBundledContext->NumberEffectsCalled,
3245    //        pContext->pBundledContext->NumberEffectsEnabled);
3246
3247    switch (cmdCode){
3248        case EFFECT_CMD_INIT:
3249            if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)){
3250                ALOGV("\tLVM_ERROR, EFFECT_CMD_INIT: ERROR for effect type %d",
3251                        pContext->EffectType);
3252                return -EINVAL;
3253            }
3254            *(int *) pReplyData = 0;
3255            //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT start");
3256            if(pContext->EffectType == LVM_BASS_BOOST){
3257                //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT for LVM_BASS_BOOST");
3258                android::BassSetStrength(pContext, 0);
3259            }
3260            if(pContext->EffectType == LVM_VIRTUALIZER){
3261                //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT for LVM_VIRTUALIZER");
3262                android::VirtualizerSetStrength(pContext, 0);
3263            }
3264            if(pContext->EffectType == LVM_EQUALIZER){
3265                //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT for LVM_EQUALIZER");
3266                android::EqualizerSetPreset(pContext, 0);
3267            }
3268            if(pContext->EffectType == LVM_VOLUME){
3269                //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT for LVM_VOLUME");
3270                *(int *) pReplyData = android::VolumeSetVolumeLevel(pContext, 0);
3271            }
3272            break;
3273
3274        case EFFECT_CMD_SET_CONFIG:
3275            //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_SET_CONFIG start");
3276            if (pCmdData    == NULL || cmdSize     != sizeof(effect_config_t) ||
3277                    pReplyData  == NULL || replySize == NULL || *replySize  != sizeof(int)) {
3278                ALOGV("\tLVM_ERROR : Effect_command cmdCode Case: "
3279                        "EFFECT_CMD_SET_CONFIG: ERROR");
3280                return -EINVAL;
3281            }
3282            *(int *) pReplyData = android::Effect_setConfig(pContext, (effect_config_t *) pCmdData);
3283            //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_SET_CONFIG end");
3284            break;
3285
3286        case EFFECT_CMD_GET_CONFIG:
3287            if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(effect_config_t)) {
3288                ALOGV("\tLVM_ERROR : Effect_command cmdCode Case: "
3289                        "EFFECT_CMD_GET_CONFIG: ERROR");
3290                return -EINVAL;
3291            }
3292
3293            android::Effect_getConfig(pContext, (effect_config_t *)pReplyData);
3294            break;
3295
3296        case EFFECT_CMD_RESET:
3297            //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_RESET start");
3298            android::Effect_setConfig(pContext, &pContext->config);
3299            //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_RESET end");
3300            break;
3301
3302        case EFFECT_CMD_GET_PARAM:{
3303            //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_GET_PARAM start");
3304
3305            effect_param_t *p = (effect_param_t *)pCmdData;
3306            if (pCmdData == NULL || cmdSize < sizeof(effect_param_t) ||
3307                    cmdSize < (sizeof(effect_param_t) + p->psize) ||
3308                    pReplyData == NULL || replySize == NULL ||
3309                    *replySize < (sizeof(effect_param_t) + p->psize)) {
3310                ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: ERROR");
3311                return -EINVAL;
3312            }
3313            if (EFFECT_PARAM_SIZE_MAX - sizeof(effect_param_t) < (size_t)p->psize) {
3314                android_errorWriteLog(0x534e4554, "26347509");
3315                ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: psize too big");
3316                return -EINVAL;
3317            }
3318            uint32_t paddedParamSize = ((p->psize + sizeof(int32_t) - 1) / sizeof(int32_t)) *
3319                    sizeof(int32_t);
3320            if ((EFFECT_PARAM_SIZE_MAX - sizeof(effect_param_t) < paddedParamSize) ||
3321                (EFFECT_PARAM_SIZE_MAX - sizeof(effect_param_t) - paddedParamSize <
3322                    p->vsize)) {
3323                ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: padded_psize or vsize too big");
3324                return -EINVAL;
3325            }
3326            uint32_t expectedReplySize = sizeof(effect_param_t) + paddedParamSize + p->vsize;
3327            if (*replySize < expectedReplySize) {
3328                ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: min. replySize %u, got %u bytes",
3329                        expectedReplySize, *replySize);
3330                android_errorWriteLog(0x534e4554, "32705438");
3331                return -EINVAL;
3332            }
3333
3334            memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize);
3335
3336            p = (effect_param_t *)pReplyData;
3337
3338            uint32_t voffset = paddedParamSize;
3339            if(pContext->EffectType == LVM_BASS_BOOST){
3340                p->status = android::BassBoost_getParameter(pContext,
3341                                                            p->data,
3342                                                            &p->vsize,
3343                                                            p->data + voffset);
3344                //ALOGV("\tBassBoost_command EFFECT_CMD_GET_PARAM "
3345                //        "*pCmdData %d, *replySize %d, *pReplyData %d ",
3346                //        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
3347                //        *replySize,
3348                //        *(int16_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset));
3349            }
3350
3351            if(pContext->EffectType == LVM_VIRTUALIZER){
3352                p->status = android::Virtualizer_getParameter(pContext,
3353                                                              (void *)p->data,
3354                                                              &p->vsize,
3355                                                              p->data + voffset);
3356
3357                //ALOGV("\tVirtualizer_command EFFECT_CMD_GET_PARAM "
3358                //        "*pCmdData %d, *replySize %d, *pReplyData %d ",
3359                //        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
3360                //        *replySize,
3361                //        *(int16_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset));
3362            }
3363            if(pContext->EffectType == LVM_EQUALIZER){
3364                //ALOGV("\tEqualizer_command cmdCode Case: "
3365                //        "EFFECT_CMD_GET_PARAM start");
3366                p->status = android::Equalizer_getParameter(pContext,
3367                                                            p->data,
3368                                                            &p->vsize,
3369                                                            p->data + voffset);
3370
3371                //ALOGV("\tEqualizer_command EFFECT_CMD_GET_PARAM *pCmdData %d, *replySize %d, "
3372                //       "*pReplyData %08x %08x",
3373                //        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)), *replySize,
3374                //        *(int32_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset),
3375                //        *(int32_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset +
3376                //        sizeof(int32_t)));
3377            }
3378            if(pContext->EffectType == LVM_VOLUME){
3379                //ALOGV("\tVolume_command cmdCode Case: EFFECT_CMD_GET_PARAM start");
3380                p->status = android::Volume_getParameter(pContext,
3381                                                         (void *)p->data,
3382                                                         &p->vsize,
3383                                                         p->data + voffset);
3384
3385                //ALOGV("\tVolume_command EFFECT_CMD_GET_PARAM "
3386                //        "*pCmdData %d, *replySize %d, *pReplyData %d ",
3387                //        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
3388                //        *replySize,
3389                //        *(int16_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset));
3390            }
3391            *replySize = sizeof(effect_param_t) + voffset + p->vsize;
3392
3393            //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_GET_PARAM end");
3394        } break;
3395        case EFFECT_CMD_SET_PARAM:{
3396            //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_SET_PARAM start");
3397            if(pContext->EffectType == LVM_BASS_BOOST){
3398                //ALOGV("\tBassBoost_command EFFECT_CMD_SET_PARAM param %d, *replySize %d, value %d",
3399                //       *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
3400                //       *replySize,
3401                //       *(int16_t *)((char *)pCmdData + sizeof(effect_param_t) + sizeof(int32_t)));
3402
3403                if (pCmdData   == NULL ||
3404                        cmdSize    != (sizeof(effect_param_t) + sizeof(int32_t) +sizeof(int16_t)) ||
3405                        pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
3406                    ALOGV("\tLVM_ERROR : BassBoost_command cmdCode Case: "
3407                            "EFFECT_CMD_SET_PARAM: ERROR");
3408                    return -EINVAL;
3409                }
3410                effect_param_t *p = (effect_param_t *) pCmdData;
3411
3412                if (p->psize != sizeof(int32_t)){
3413                    ALOGV("\tLVM_ERROR : BassBoost_command cmdCode Case: "
3414                            "EFFECT_CMD_SET_PARAM: ERROR, psize is not sizeof(int32_t)");
3415                    return -EINVAL;
3416                }
3417
3418                //ALOGV("\tnBassBoost_command cmdSize is %d\n"
3419                //        "\tsizeof(effect_param_t) is  %d\n"
3420                //        "\tp->psize is %d\n"
3421                //        "\tp->vsize is %d"
3422                //        "\n",
3423                //        cmdSize, sizeof(effect_param_t), p->psize, p->vsize );
3424
3425                *(int *)pReplyData = android::BassBoost_setParameter(pContext,
3426                                                                    (void *)p->data,
3427                                                                    p->data + p->psize);
3428            }
3429            if(pContext->EffectType == LVM_VIRTUALIZER){
3430              // Warning this log will fail to properly read an int32_t value, assumes int16_t
3431              //ALOGV("\tVirtualizer_command EFFECT_CMD_SET_PARAM param %d, *replySize %d, value %d",
3432              //        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
3433              //        *replySize,
3434              //        *(int16_t *)((char *)pCmdData + sizeof(effect_param_t) + sizeof(int32_t)));
3435
3436                if (pCmdData   == NULL ||
3437                        // legal parameters are int16_t or int32_t
3438                        cmdSize    > (sizeof(effect_param_t) + sizeof(int32_t) +sizeof(int32_t)) ||
3439                        cmdSize    < (sizeof(effect_param_t) + sizeof(int32_t) +sizeof(int16_t)) ||
3440                        pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
3441                    ALOGV("\tLVM_ERROR : Virtualizer_command cmdCode Case: "
3442                            "EFFECT_CMD_SET_PARAM: ERROR");
3443                    return -EINVAL;
3444                }
3445                effect_param_t *p = (effect_param_t *) pCmdData;
3446
3447                if (p->psize != sizeof(int32_t)){
3448                    ALOGV("\tLVM_ERROR : Virtualizer_command cmdCode Case: "
3449                            "EFFECT_CMD_SET_PARAM: ERROR, psize is not sizeof(int32_t)");
3450                    return -EINVAL;
3451                }
3452
3453                //ALOGV("\tnVirtualizer_command cmdSize is %d\n"
3454                //        "\tsizeof(effect_param_t) is  %d\n"
3455                //        "\tp->psize is %d\n"
3456                //        "\tp->vsize is %d"
3457                //        "\n",
3458                //        cmdSize, sizeof(effect_param_t), p->psize, p->vsize );
3459
3460                *(int *)pReplyData = android::Virtualizer_setParameter(pContext,
3461                                                                      (void *)p->data,
3462                                                                       p->data + p->psize);
3463            }
3464            if(pContext->EffectType == LVM_EQUALIZER){
3465               //ALOGV("\tEqualizer_command cmdCode Case: "
3466               //        "EFFECT_CMD_SET_PARAM start");
3467               //ALOGV("\tEqualizer_command EFFECT_CMD_SET_PARAM param %d, *replySize %d, value %d ",
3468               //        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
3469               //        *replySize,
3470               //        *(int16_t *)((char *)pCmdData + sizeof(effect_param_t) + sizeof(int32_t)));
3471
3472                if (pCmdData == NULL || cmdSize < (sizeof(effect_param_t) + sizeof(int32_t)) ||
3473                        pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
3474                    ALOGV("\tLVM_ERROR : Equalizer_command cmdCode Case: "
3475                            "EFFECT_CMD_SET_PARAM: ERROR");
3476                    return -EINVAL;
3477                }
3478                effect_param_t *p = (effect_param_t *) pCmdData;
3479
3480                *(int *)pReplyData = android::Equalizer_setParameter(pContext,
3481                                                                    (void *)p->data,
3482                                                                    p->vsize,
3483                                                                    p->data + p->psize);
3484            }
3485            if(pContext->EffectType == LVM_VOLUME){
3486                //ALOGV("\tVolume_command cmdCode Case: EFFECT_CMD_SET_PARAM start");
3487                //ALOGV("\tVolume_command EFFECT_CMD_SET_PARAM param %d, *replySize %d, value %d ",
3488                //        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
3489                //        *replySize,
3490                //        *(int16_t *)((char *)pCmdData + sizeof(effect_param_t) +sizeof(int32_t)));
3491
3492                if (pCmdData   == NULL ||
3493                        cmdSize    < (sizeof(effect_param_t) + sizeof(int32_t)) ||
3494                        pReplyData == NULL || replySize == NULL ||
3495                        *replySize != sizeof(int32_t)) {
3496                    ALOGV("\tLVM_ERROR : Volume_command cmdCode Case: "
3497                            "EFFECT_CMD_SET_PARAM: ERROR");
3498                    return -EINVAL;
3499                }
3500                effect_param_t *p = (effect_param_t *) pCmdData;
3501
3502                *(int *)pReplyData = android::Volume_setParameter(pContext,
3503                                                                 (void *)p->data,
3504                                                                 p->data + p->psize);
3505            }
3506            //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_SET_PARAM end");
3507        } break;
3508
3509        case EFFECT_CMD_ENABLE:
3510            ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_ENABLE start");
3511            if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
3512                ALOGV("\tLVM_ERROR : Effect_command cmdCode Case: EFFECT_CMD_ENABLE: ERROR");
3513                return -EINVAL;
3514            }
3515
3516            *(int *)pReplyData = android::Effect_setEnabled(pContext, LVM_TRUE);
3517            break;
3518
3519        case EFFECT_CMD_DISABLE:
3520            //ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_DISABLE start");
3521            if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
3522                ALOGV("\tLVM_ERROR : Effect_command cmdCode Case: EFFECT_CMD_DISABLE: ERROR");
3523                return -EINVAL;
3524            }
3525            *(int *)pReplyData = android::Effect_setEnabled(pContext, LVM_FALSE);
3526            break;
3527
3528        case EFFECT_CMD_SET_DEVICE:
3529        {
3530            ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_SET_DEVICE start");
3531            if (pCmdData   == NULL){
3532                ALOGV("\tLVM_ERROR : Effect_command cmdCode Case: EFFECT_CMD_SET_DEVICE: ERROR");
3533                return -EINVAL;
3534            }
3535
3536            uint32_t device = *(uint32_t *)pCmdData;
3537            pContext->pBundledContext->nOutputDevice = (audio_devices_t) device;
3538
3539            if (pContext->EffectType == LVM_BASS_BOOST) {
3540                if((device == AUDIO_DEVICE_OUT_SPEAKER) ||
3541                        (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) ||
3542                        (device == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)){
3543                    ALOGV("\tEFFECT_CMD_SET_DEVICE device is invalid for LVM_BASS_BOOST %d",
3544                          *(int32_t *)pCmdData);
3545                    ALOGV("\tEFFECT_CMD_SET_DEVICE temporary disable LVM_BAS_BOOST");
3546
3547                    // If a device doesnt support bassboost the effect must be temporarily disabled
3548                    // the effect must still report its original state as this can only be changed
3549                    // by the ENABLE/DISABLE command
3550
3551                    if (pContext->pBundledContext->bBassEnabled == LVM_TRUE) {
3552                        ALOGV("\tEFFECT_CMD_SET_DEVICE disable LVM_BASS_BOOST %d",
3553                             *(int32_t *)pCmdData);
3554                        android::LvmEffect_disable(pContext);
3555                    }
3556                    pContext->pBundledContext->bBassTempDisabled = LVM_TRUE;
3557                } else {
3558                    ALOGV("\tEFFECT_CMD_SET_DEVICE device is valid for LVM_BASS_BOOST %d",
3559                         *(int32_t *)pCmdData);
3560
3561                    // If a device supports bassboost and the effect has been temporarily disabled
3562                    // previously then re-enable it
3563
3564                    if (pContext->pBundledContext->bBassEnabled == LVM_TRUE) {
3565                        ALOGV("\tEFFECT_CMD_SET_DEVICE re-enable LVM_BASS_BOOST %d",
3566                             *(int32_t *)pCmdData);
3567                        android::LvmEffect_enable(pContext);
3568                    }
3569                    pContext->pBundledContext->bBassTempDisabled = LVM_FALSE;
3570                }
3571            }
3572            if (pContext->EffectType == LVM_VIRTUALIZER) {
3573                if (pContext->pBundledContext->nVirtualizerForcedDevice == AUDIO_DEVICE_NONE) {
3574                    // default case unless configuration is forced
3575                    if (android::VirtualizerIsDeviceSupported(device) != 0) {
3576                        ALOGV("\tEFFECT_CMD_SET_DEVICE device is invalid for LVM_VIRTUALIZER %d",
3577                                *(int32_t *)pCmdData);
3578                        ALOGV("\tEFFECT_CMD_SET_DEVICE temporary disable LVM_VIRTUALIZER");
3579
3580                        //If a device doesnt support virtualizer the effect must be temporarily
3581                        // disabled the effect must still report its original state as this can
3582                        // only be changed by the ENABLE/DISABLE command
3583
3584                        if (pContext->pBundledContext->bVirtualizerEnabled == LVM_TRUE) {
3585                            ALOGV("\tEFFECT_CMD_SET_DEVICE disable LVM_VIRTUALIZER %d",
3586                                    *(int32_t *)pCmdData);
3587                            android::LvmEffect_disable(pContext);
3588                        }
3589                        pContext->pBundledContext->bVirtualizerTempDisabled = LVM_TRUE;
3590                    } else {
3591                        ALOGV("\tEFFECT_CMD_SET_DEVICE device is valid for LVM_VIRTUALIZER %d",
3592                                *(int32_t *)pCmdData);
3593
3594                        // If a device supports virtualizer and the effect has been temporarily
3595                        // disabled previously then re-enable it
3596
3597                        if(pContext->pBundledContext->bVirtualizerEnabled == LVM_TRUE){
3598                            ALOGV("\tEFFECT_CMD_SET_DEVICE re-enable LVM_VIRTUALIZER %d",
3599                                    *(int32_t *)pCmdData);
3600                            android::LvmEffect_enable(pContext);
3601                        }
3602                        pContext->pBundledContext->bVirtualizerTempDisabled = LVM_FALSE;
3603                    }
3604                } // else virtualization mode is forced to a certain device, nothing to do
3605            }
3606            ALOGV("\tEffect_command cmdCode Case: EFFECT_CMD_SET_DEVICE end");
3607            break;
3608        }
3609        case EFFECT_CMD_SET_VOLUME:
3610        {
3611            uint32_t leftVolume, rightVolume;
3612            int16_t  leftdB, rightdB;
3613            int16_t  maxdB, pandB;
3614            int32_t  vol_ret[2] = {1<<24,1<<24}; // Apply no volume
3615            LVM_ControlParams_t     ActiveParams;           /* Current control Parameters */
3616            LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;  /* Function call status */
3617
3618            // if pReplyData is NULL, VOL_CTRL is delegated to another effect
3619            if(pReplyData == LVM_NULL){
3620                break;
3621            }
3622
3623            if (pCmdData == NULL || cmdSize != 2 * sizeof(uint32_t) || pReplyData == NULL ||
3624                    replySize == NULL || *replySize < 2*sizeof(int32_t)) {
3625                ALOGV("\tLVM_ERROR : Effect_command cmdCode Case: "
3626                        "EFFECT_CMD_SET_VOLUME: ERROR");
3627                return -EINVAL;
3628            }
3629
3630            leftVolume  = ((*(uint32_t *)pCmdData));
3631            rightVolume = ((*((uint32_t *)pCmdData + 1)));
3632
3633            if(leftVolume == 0x1000000){
3634                leftVolume -= 1;
3635            }
3636            if(rightVolume == 0x1000000){
3637                rightVolume -= 1;
3638            }
3639
3640            // Convert volume to dB
3641            leftdB  = android::LVC_Convert_VolToDb(leftVolume);
3642            rightdB = android::LVC_Convert_VolToDb(rightVolume);
3643
3644            pandB = rightdB - leftdB;
3645
3646            // Calculate max volume in dB
3647            maxdB = leftdB;
3648            if(rightdB > maxdB){
3649                maxdB = rightdB;
3650            }
3651            //ALOGV("\tEFFECT_CMD_SET_VOLUME Session: %d, SessionID: %d VOLUME is %d dB (%d), "
3652            //      "effect is %d",
3653            //pContext->pBundledContext->SessionNo, pContext->pBundledContext->SessionId,
3654            //(int32_t)maxdB, maxVol<<7, pContext->EffectType);
3655            //ALOGV("\tEFFECT_CMD_SET_VOLUME: Left is %d, Right is %d", leftVolume, rightVolume);
3656            //ALOGV("\tEFFECT_CMD_SET_VOLUME: Left %ddB, Right %ddB, Position %ddB",
3657            //        leftdB, rightdB, pandB);
3658
3659            memcpy(pReplyData, vol_ret, sizeof(int32_t)*2);
3660            android::VolumeSetVolumeLevel(pContext, (int16_t)(maxdB*100));
3661
3662            /* Get the current settings */
3663            LvmStatus =LVM_GetControlParameters(pContext->pBundledContext->hInstance,&ActiveParams);
3664            LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VolumeSetStereoPosition")
3665            if(LvmStatus != LVM_SUCCESS) return -EINVAL;
3666
3667            /* Volume parameters */
3668            ActiveParams.VC_Balance  = pandB;
3669            ALOGV("\t\tVolumeSetStereoPosition() (-96dB -> +96dB)-> %d\n", ActiveParams.VC_Balance );
3670
3671            /* Activate the initial settings */
3672            LvmStatus =LVM_SetControlParameters(pContext->pBundledContext->hInstance,&ActiveParams);
3673            LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "VolumeSetStereoPosition")
3674            if(LvmStatus != LVM_SUCCESS) return -EINVAL;
3675            break;
3676         }
3677        case EFFECT_CMD_SET_AUDIO_MODE:
3678            break;
3679        default:
3680            return -EINVAL;
3681    }
3682
3683    //ALOGV("\tEffect_command end...\n\n");
3684    return 0;
3685}    /* end Effect_command */
3686
3687/* Effect Control Interface Implementation: get_descriptor */
3688int Effect_getDescriptor(effect_handle_t   self,
3689                                    effect_descriptor_t *pDescriptor)
3690{
3691    EffectContext * pContext = (EffectContext *) self;
3692    const effect_descriptor_t *desc;
3693
3694    if (pContext == NULL || pDescriptor == NULL) {
3695        ALOGV("Effect_getDescriptor() invalid param");
3696        return -EINVAL;
3697    }
3698
3699    switch(pContext->EffectType) {
3700        case LVM_BASS_BOOST:
3701            desc = &android::gBassBoostDescriptor;
3702            break;
3703        case LVM_VIRTUALIZER:
3704            desc = &android::gVirtualizerDescriptor;
3705            break;
3706        case LVM_EQUALIZER:
3707            desc = &android::gEqualizerDescriptor;
3708            break;
3709        case LVM_VOLUME:
3710            desc = &android::gVolumeDescriptor;
3711            break;
3712        default:
3713            return -EINVAL;
3714    }
3715
3716    *pDescriptor = *desc;
3717
3718    return 0;
3719}   /* end Effect_getDescriptor */
3720
3721// effect_handle_t interface implementation for effect
3722const struct effect_interface_s gLvmEffectInterface = {
3723    Effect_process,
3724    Effect_command,
3725    Effect_getDescriptor,
3726    NULL,
3727};    /* end gLvmEffectInterface */
3728
3729// This is the only symbol that needs to be exported
3730__attribute__ ((visibility ("default")))
3731audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
3732    .tag = AUDIO_EFFECT_LIBRARY_TAG,
3733    .version = EFFECT_LIBRARY_API_VERSION,
3734    .name = "Effect Bundle Library",
3735    .implementor = "NXP Software Ltd.",
3736    .create_effect = android::EffectCreate,
3737    .release_effect = android::EffectRelease,
3738    .get_descriptor = android::EffectGetDescriptor,
3739};
3740
3741}
3742