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