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