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