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