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