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