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