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