EffectBundle.cpp revision 2c8e5cab3faa6d360e222b7a6c40a80083d021ac
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 LVM_BUNDLE                // Include all the bundle code
21#define LOG_NDEBUG 0
22
23#include <cutils/log.h>
24#include <assert.h>
25#include <stdlib.h>
26#include <string.h>
27#include <new>
28#include <EffectBundle.h>
29
30#ifdef LVM_BUNDLE
31#define LVM_MAX_SESSIONS        32
32#define MAX_NUM_BANDS           5
33#define MAX_CALL_SIZE           256
34#endif  /* end LVM_BUNDLE */
35
36
37int framecountag = 0;
38// effect_interface_t interface implementation for bass boost
39extern "C" const struct effect_interface_s gLvmEffectInterface;
40
41#define LVM_ERROR_CHECK(LvmStatus, callingFunc, calledFunc){\
42        if (LvmStatus == LVM_NULLADDRESS){\
43            LOGV("\tLVM_ERROR : Parameter error - "\
44                    "null pointer returned by %s in %s\n\n\n\n", callingFunc, calledFunc);\
45        }\
46        if (LvmStatus == LVM_ALIGNMENTERROR){\
47            LOGV("\tLVM_ERROR : Parameter error - "\
48                    "bad alignment returned by %s in %s\n\n\n\n", callingFunc, calledFunc);\
49        }\
50        if (LvmStatus == LVM_INVALIDNUMSAMPLES){\
51            LOGV("\tLVM_ERROR : Parameter error - "\
52                    "bad number of samples returned by %s in %s\n\n\n\n", callingFunc, calledFunc);\
53        }\
54        if (LvmStatus == LVM_OUTOFRANGE){\
55            LOGV("\tLVM_ERROR : Parameter error - "\
56                    "out of range returned by %s in %s\n", callingFunc, calledFunc);\
57        }\
58    }
59
60
61
62
63
64// Namespaces
65namespace android {
66namespace {
67
68/* local functions */
69#define CHECK_ARG(cond) {                     \
70    if (!(cond)) {                            \
71        LOGV("\tLVM_ERROR : Invalid argument: "#cond);      \
72        return -EINVAL;                       \
73    }                                         \
74}
75
76int LvmInitFlag = LVM_FALSE;
77SessionContext GlobalSessionMemory[32];
78
79// NXP SW BassBoost UUID
80const effect_descriptor_t gBassBoostDescriptor = {
81        {0x0634f220, 0xddd4, 0x11db, 0xa0fc, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }},
82        {0x8631f300, 0x72e2, 0x11df, 0xb57e, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
83        EFFECT_API_VERSION,
84        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST),
85        0, // TODO
86        1,
87        "Dynamic Bass Boost",
88        "NXP Software Ltd.",
89};
90
91// NXP SW Virtualizer UUID
92const effect_descriptor_t gVirtualizerDescriptor = {
93        {0x37cc2c00, 0xdddd, 0x11db, 0x8577, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // Virtualizer ID
94        {0x1d4033c0, 0x8557, 0x11df, 0x9f2d, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // NXP SW UUID gen july 01 2010
95        EFFECT_API_VERSION,
96        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST),
97        0, // TODO
98        1,
99        "Virtualizer",
100        "NXP Software Ltd.",
101};
102
103// NXP SW Equalizer UUID
104const effect_descriptor_t gEqualizerDescriptor = {
105        {0x0bed4300, 0xddd6, 0x11db, 0x8f34, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
106        {0xce772f20, 0x847d, 0x11df, 0xbb17, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid Eq NXP
107        EFFECT_API_VERSION,
108        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST),
109        0, // TODO
110        1,
111        "Equalizer",
112        "NXP Software Ltd.",
113};
114
115// NXP SW Volume UUID
116const effect_descriptor_t gVolumeDescriptor = {
117        {0x09e8ede0, 0xddde, 0x11db, 0xb4f6, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }},
118        //{0x8631f300, 0x72e2, 0x11df, 0xb57e, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }}, // uuid
119        {0x119341a0, 0x8469, 0x11df, 0x81f9, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }}, //uuid VOL NXP
120        EFFECT_API_VERSION,
121        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST),
122        0, // TODO
123        1,
124        "Volume",
125        "NXP Software Ltd.",
126};
127
128//--- local function prototypes
129void LvmGlobalBundle_init      (void);
130int  LvmBundle_init            (EffectContext *pContext);
131int  LvmEffect_enable          (EffectContext *pContext);
132int  LvmEffect_disable         (EffectContext *pContext);
133void LvmEffect_free            (EffectContext *pContext);
134int  Effect_configure       (EffectContext *pContext, effect_config_t *pConfig);
135int  BassBoost_setParameter    (EffectContext *pContext, int32_t *pParam, void *pValue);
136int  BassBoost_getParameter    (EffectContext *pContext,
137                               int32_t        *pParam,
138                               size_t         *pValueSize,
139                               void           *pValue);
140int  Virtualizer_setParameter  (EffectContext *pContext, int32_t *pParam, void *pValue);
141int  Virtualizer_getParameter  (EffectContext *pContext,
142                               int32_t        *pParam,
143                               size_t         *pValueSize,
144                               void           *pValue);
145int  Equalizer_setParameter    (EffectContext *pContext, int32_t *pParam, void *pValue);
146int  Equalizer_getParameter    (EffectContext *pContext,
147                                int32_t       *pParam,
148                                size_t        *pValueSize,
149                                void          *pValue);
150int  Volume_setParameter       (EffectContext *pContext, int32_t *pParam, void *pValue);
151int  Volume_getParameter       (EffectContext *pContext,
152                                int32_t       *pParam,
153                                size_t        *pValueSize,
154                                void          *pValue);
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171/* Effect Library Interface Implementation */
172extern "C" int EffectQueryNumberEffects(uint32_t *pNumEffects){
173    LOGV("\n\tEffectQueryNumberEffects start");
174    *pNumEffects = 4;
175    LOGV("\tEffectQueryNumberEffects creating %d effects", *pNumEffects);
176    LOGV("\tEffectQueryNumberEffects end\n");
177    return 0;
178}     /* end EffectQueryNumberEffects */
179
180
181extern "C" int EffectQueryEffect(uint32_t index, effect_descriptor_t *pDescriptor){
182    LOGV("\n\tEffectQueryEffect start");
183    LOGV("\tEffectQueryEffect processing index %d", index);
184
185    if (pDescriptor == NULL){
186    	LOGV("\tLVM_ERROR : EffectQueryEffect was passed NULL pointer");
187        return -EINVAL;
188    }
189    if (index > 3){
190    	LOGV("\tLVM_ERROR : EffectQueryEffect index out of range %d", index);
191        return -ENOENT;
192    }
193    if(index == LVM_BASS_BOOST){
194        LOGV("\tEffectQueryEffect processing LVM_BASS_BOOST");
195        memcpy(pDescriptor, &gBassBoostDescriptor,   sizeof(effect_descriptor_t));
196    }else if(index == LVM_VIRTUALIZER){
197        LOGV("\tEffectQueryEffect processing LVM_VIRTUALIZER");
198        memcpy(pDescriptor, &gVirtualizerDescriptor, sizeof(effect_descriptor_t));
199    } else if(index == LVM_EQUALIZER){
200        LOGV("\tEffectQueryEffect processing LVM_EQUALIZER");
201        memcpy(pDescriptor, &gEqualizerDescriptor,   sizeof(effect_descriptor_t));
202    } else if(index == LVM_VOLUME){
203        LOGV("\tEffectQueryEffect processing LVM_VOLUME");
204        memcpy(pDescriptor, &gVolumeDescriptor, sizeof(effect_descriptor_t));
205    }
206    LOGV("\tEffectQueryEffect end\n");
207    return 0;
208}     /* end EffectQueryEffect */
209
210
211extern "C" int EffectCreate(effect_uuid_t       *uuid,
212                            int32_t             sessionId,
213                            int32_t             ioId,
214                            effect_interface_t  *pInterface){
215    int ret;
216    int i;
217    EffectContext *pContext = new EffectContext;
218
219    LOGV("\n\tEffectCreate start session %d", sessionId);
220
221    if (pInterface == NULL || uuid == NULL){
222    	LOGV("\tLVM_ERROR : EffectCreate() called with NULL pointer");
223        return -EINVAL;
224    }
225
226    if((sessionId < 0)||(sessionId >= LVM_MAX_SESSIONS)){
227        LOGV("\tLVM_ERROR : EffectCreate sessionId is less than 0");
228        return -EINVAL;
229    }
230
231    if(LvmInitFlag == LVM_FALSE){
232        LvmInitFlag = LVM_TRUE;
233        LOGV("\tEffectCreate - Initializing all global memory");
234        LvmGlobalBundle_init();
235    }
236
237    // If this is the first create in this session
238    if(GlobalSessionMemory[sessionId].bBundledEffectsEnabled == LVM_FALSE){
239        LOGV("\tEffectCreate - This is the first effect in current session %d", sessionId);
240        LOGV("\tEffectCreate - Setting up Bundled Effects Instance for session %d", sessionId);
241        GlobalSessionMemory[sessionId].bBundledEffectsEnabled = LVM_TRUE;
242        GlobalSessionMemory[sessionId].pBundledContext        = new BundledEffectContext;
243
244        pContext->pBundledContext = GlobalSessionMemory[sessionId].pBundledContext;
245        pContext->pBundledContext->SessionNo            = sessionId;
246        pContext->pBundledContext->hInstance            = NULL;
247        pContext->pBundledContext->bVolumeEnabled       = LVM_FALSE;
248        pContext->pBundledContext->bEqualizerEnabled    = LVM_FALSE;
249        pContext->pBundledContext->bBassEnabled         = LVM_FALSE;
250        pContext->pBundledContext->bVirtualizerEnabled  = LVM_FALSE;
251        pContext->pBundledContext->NumberEffectsEnabled = 0;
252        pContext->pBundledContext->NumberEffectsCalled  = 0;
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
264        LOGV("\tEffectCreate - Calling LvmBundle_init");
265        ret = LvmBundle_init(pContext);
266
267        if (ret < 0){
268            LOGV("\tLVM_ERROR : EffectCreate() Bundle init failed");
269            delete pContext->pBundledContext;
270            delete pContext;
271            return ret;
272        }
273    }
274    else{
275        pContext->pBundledContext = GlobalSessionMemory[sessionId].pBundledContext;
276    }
277
278    LOGV("\tEffectCreate - pBundledContext is %p", pContext->pBundledContext);
279
280    // Create each Effect
281    if (memcmp(uuid, &gBassBoostDescriptor.uuid, sizeof(effect_uuid_t)) == 0){
282        // Create Bass Boost
283        LOGV("\tEffectCreate - Effect to be created is LVM_BASS_BOOST");
284        GlobalSessionMemory[sessionId].bBassInstantiated = LVM_TRUE;
285
286        pContext->itfe       = &gLvmEffectInterface;
287        pContext->EffectType = LVM_BASS_BOOST;
288    } else if (memcmp(uuid, &gVirtualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0){
289        // Create Virtualizer
290        LOGV("\tEffectCreate - Effect to be created is LVM_VIRTUALIZER");
291        GlobalSessionMemory[sessionId].bVirtualizerInstantiated = LVM_TRUE;
292
293        pContext->itfe       = &gLvmEffectInterface;
294        pContext->EffectType = LVM_VIRTUALIZER;
295    } else if (memcmp(uuid, &gEqualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0){
296        // Create Equalizer
297        LOGV("\tEffectCreate - Effect to be created is LVM_EQUALIZER");
298        GlobalSessionMemory[sessionId].bEqualizerInstantiated = LVM_TRUE;
299
300        pContext->itfe       = &gLvmEffectInterface;
301        pContext->EffectType = LVM_EQUALIZER;
302    } else if (memcmp(uuid, &gVolumeDescriptor.uuid, sizeof(effect_uuid_t)) == 0){
303        // Create Volume
304        LOGV("\tEffectCreate - Effect to be created is LVM_VOLUME");
305        GlobalSessionMemory[sessionId].bVolumeInstantiated = LVM_TRUE;
306
307        pContext->itfe       = &gLvmEffectInterface;
308        pContext->EffectType = LVM_VOLUME;
309    }
310    else{
311     	LOGV("\tLVM_ERROR : EffectCreate() invalid UUID");
312        return -EINVAL;
313    }
314
315    *pInterface = (effect_interface_t)pContext;
316    LOGV("\tEffectCreate end..\n\n");
317    return 0;
318} /* end EffectCreate */
319
320
321
322extern "C" int EffectRelease(effect_interface_t interface){
323    LOGV("\n\tEffectRelease start %p", interface);
324    EffectContext * pContext = (EffectContext *)interface;
325
326    if (pContext == NULL){
327    	LOGV("\tLVM_ERROR : EffectRelease called with NULL pointer");
328        return -EINVAL;
329    }
330
331    // Clear the instantiated flag for the effect
332    if(pContext->EffectType == LVM_BASS_BOOST) {
333        LOGV("\tEffectRelease LVM_BASS_BOOST Clearing global intstantiated flag");
334        GlobalSessionMemory[pContext->pBundledContext->SessionNo].bBassInstantiated = LVM_FALSE;
335    } else if(pContext->EffectType == LVM_VIRTUALIZER) {
336        LOGV("\tEffectRelease LVM_VIRTUALIZER Clearing global intstantiated flag");
337        GlobalSessionMemory[pContext->pBundledContext->SessionNo].bVirtualizerInstantiated = LVM_FALSE;
338    } else if(pContext->EffectType == LVM_EQUALIZER) {
339        LOGV("\tEffectRelease LVM_EQUALIZER Clearing global intstantiated flag");
340        GlobalSessionMemory[pContext->pBundledContext->SessionNo].bEqualizerInstantiated = LVM_FALSE;
341    } else if(pContext->EffectType == LVM_VOLUME) {
342        LOGV("\tEffectRelease LVM_VOLUME Clearing global intstantiated flag");
343        GlobalSessionMemory[pContext->pBundledContext->SessionNo].bVolumeInstantiated = LVM_FALSE;
344    } else {
345        LOGV("\tLVM_ERROR : EffectRelease : Unsupported effect\n\n\n\n\n\n\n");
346    }
347
348    // if all effects are no longer instantiaed free the lvm memory and delete BundledEffectContext
349    if((GlobalSessionMemory[pContext->pBundledContext->SessionNo].bBassInstantiated == LVM_FALSE)&&
350       (GlobalSessionMemory[pContext->pBundledContext->SessionNo].bVolumeInstantiated == LVM_FALSE)&&
351       (GlobalSessionMemory[pContext->pBundledContext->SessionNo].bEqualizerInstantiated == LVM_FALSE)&&
352       (GlobalSessionMemory[pContext->pBundledContext->SessionNo].bVirtualizerInstantiated == LVM_FALSE))
353    {
354       LOGV("\tEffectRelease: All effects are no longer instantiated\n");
355       GlobalSessionMemory[pContext->pBundledContext->SessionNo].bBundledEffectsEnabled = LVM_FALSE;
356       GlobalSessionMemory[pContext->pBundledContext->SessionNo].pBundledContext = LVM_NULL;
357       LOGV("\tEffectRelease: Freeing LVM Bundle memory\n");
358       LvmEffect_free(pContext);
359       LOGV("\tEffectRelease: Deleting LVM Bundle context\n");
360       delete pContext->pBundledContext;
361    }
362
363    // free the effect context for current effect
364    delete pContext;
365
366    LOGV("\tEffectRelease end\n");
367    return 0;
368
369} /* end EffectRelease */
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386void LvmGlobalBundle_init(){
387    LOGV("\tLvmGlobalBundle_init start");
388    for(int i=0; i<LVM_MAX_SESSIONS; i++){
389        GlobalSessionMemory[i].bBundledEffectsEnabled   = LVM_FALSE;
390        GlobalSessionMemory[i].bVolumeInstantiated      = LVM_FALSE;
391        GlobalSessionMemory[i].bEqualizerInstantiated   = LVM_FALSE;
392        GlobalSessionMemory[i].bBassInstantiated        = LVM_FALSE;
393        GlobalSessionMemory[i].bVirtualizerInstantiated = LVM_FALSE;
394        GlobalSessionMemory[i].pBundledContext          = LVM_NULL;
395    }
396    return;
397}
398//----------------------------------------------------------------------------
399// LvmBundle_init()
400//----------------------------------------------------------------------------
401// Purpose: Initialize engine with default configuration, creates instance
402// with all effects disabled.
403//
404// Inputs:
405//  pContext:   effect engine context
406//
407// Outputs:
408//
409//----------------------------------------------------------------------------
410
411int LvmBundle_init(EffectContext *pContext){
412    int status;
413
414    LOGV("\tLvmBundle_init start");
415
416    pContext->config.inputCfg.accessMode                           = EFFECT_BUFFER_ACCESS_READ;
417    pContext->config.inputCfg.channels                             = CHANNEL_STEREO;
418    pContext->config.inputCfg.format                               = SAMPLE_FORMAT_PCM_S15;
419    pContext->config.inputCfg.samplingRate                         = 44100;
420    pContext->config.inputCfg.bufferProvider.getBuffer             = NULL;
421    pContext->config.inputCfg.bufferProvider.releaseBuffer         = NULL;
422    pContext->config.inputCfg.bufferProvider.cookie                = NULL;
423    pContext->config.inputCfg.mask                                 = EFFECT_CONFIG_ALL;
424    pContext->config.outputCfg.accessMode                          = EFFECT_BUFFER_ACCESS_ACCUMULATE;
425    pContext->config.outputCfg.channels                            = CHANNEL_STEREO;
426    pContext->config.outputCfg.format                              = SAMPLE_FORMAT_PCM_S15;
427    pContext->config.outputCfg.samplingRate                        = 44100;
428    pContext->config.outputCfg.bufferProvider.getBuffer            = NULL;
429    pContext->config.outputCfg.bufferProvider.releaseBuffer        = NULL;
430    pContext->config.outputCfg.bufferProvider.cookie               = NULL;
431    pContext->config.outputCfg.mask                                = EFFECT_CONFIG_ALL;
432
433    CHECK_ARG(pContext != NULL);
434
435    if (pContext->pBundledContext->hInstance != NULL){
436        LOGV("\tLvmBundle_init pContext->pBassBoost != NULL "
437                "-> Calling pContext->pBassBoost->free()");
438
439        LvmEffect_free(pContext);
440
441        LOGV("\tLvmBundle_init pContext->pBassBoost != NULL "
442                "-> Called pContext->pBassBoost->free()");
443    }
444
445    #ifdef LVM_BUNDLE
446    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;          /* Function call status */
447    LVM_ControlParams_t     params;                         /* Control Parameters */
448    LVM_InstParams_t        InstParams;                     /* Instance parameters */
449    LVM_EQNB_BandDef_t      BandDefs[MAX_NUM_BANDS];        /* Equaliser band definitions */
450    LVM_HeadroomParams_t    HeadroomParams;                 /* Headroom parameters */
451    LVM_HeadroomBandDef_t   HeadroomBandDef[LVM_HEADROOM_MAX_NBANDS];
452    LVM_MemTab_t            MemTab;                         /* Memory allocation table */
453    bool                    bMallocFailure = LVM_FALSE;
454
455    /* Set the capabilities */
456    InstParams.BufferMode       = LVM_MANAGED_BUFFERS;
457    InstParams.MaxBlockSize     = MAX_CALL_SIZE;
458    InstParams.EQNB_NumBands    = MAX_NUM_BANDS;
459    InstParams.PSA_Included     = LVM_PSA_ON;
460
461    /* Allocate memory, forcing alignment */
462    LvmStatus = LVM_GetMemoryTable(LVM_NULL,
463                                  &MemTab,
464                                  &InstParams);
465
466    LVM_ERROR_CHECK(LvmStatus, "LVM_GetMemoryTable", "LvmBundle_init")
467    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
468
469    LOGV("\tCreateInstance Succesfully called LVM_GetMemoryTable\n");
470
471    /* Allocate memory */
472    for (int i=0; i<LVM_NR_MEMORY_REGIONS; i++){
473        if (MemTab.Region[i].Size != 0){
474            MemTab.Region[i].pBaseAddress = malloc(MemTab.Region[i].Size);
475
476            if (MemTab.Region[i].pBaseAddress == LVM_NULL){
477                LOGV("\tLVM_ERROR : CreateInstance Failed to allocate %ld bytes for region %u\n",
478                        MemTab.Region[i].Size, i );
479                bMallocFailure = LVM_TRUE;
480            }else{
481                LOGV("\tCreateInstance allocated %ld bytes for region %u at %p\n",
482                        MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
483            }
484        }
485    }
486
487    /* If one or more of the memory regions failed to allocate, free the regions that were
488     * succesfully allocated and return with an error
489     */
490    if(bMallocFailure == LVM_TRUE){
491        for (int i=0; i<LVM_NR_MEMORY_REGIONS; i++){
492            if (MemTab.Region[i].pBaseAddress == LVM_NULL){
493                LOGV("\tLVM_ERROR : CreateInstance Failed to allocate %ld bytes for region %u - Not freeing\n",
494                        MemTab.Region[i].Size, i );
495            }else{
496                LOGV("\tLVM_ERROR : CreateInstance Failed: but allocated %ld bytes for region %u at %p- free\n",
497                        MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
498                free(MemTab.Region[i].pBaseAddress);
499            }
500        }
501        return -EINVAL;
502    }
503    LOGV("\tCreateInstance Succesfully malloc'd memory\n");
504
505    /* Initialise */
506    pContext->pBundledContext->hInstance = LVM_NULL;                        /* Initialise to NULL */
507
508    LvmStatus = LVM_GetInstanceHandle(&pContext->pBundledContext->hInstance,/* Init sets the instance handle */
509                                      &MemTab,
510                                      &InstParams);
511
512    LVM_ERROR_CHECK(LvmStatus, "LVM_GetInstanceHandle", "LvmBundle_init")
513    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
514
515    LOGV("\tCreateInstance Succesfully called LVM_GetInstanceHandle\n");
516
517    /* Set the initial process parameters */
518    /* General parameters */
519    params.OperatingMode          = LVM_MODE_ON;
520    params.SampleRate             = LVM_FS_44100;
521    params.SourceFormat           = LVM_STEREO;
522    params.SpeakerType            = LVM_HEADPHONES;
523
524    /* Concert Sound parameters */
525    params.VirtualizerOperatingMode   = LVM_MODE_OFF;
526    params.VirtualizerType            = LVM_CONCERTSOUND;
527    params.VirtualizerReverbLevel     = 100;
528    params.CS_EffectLevel             = LVM_CS_EFFECT_HIGH;
529
530    /* N-Band Equaliser parameters */
531    params.EQNB_OperatingMode     = LVM_EQNB_OFF;
532    params.EQNB_NBands            = FIVEBAND_NUMBANDS;
533    params.pEQNB_BandDefinition   = &BandDefs[0];
534    for (int i=0; i<FIVEBAND_NUMBANDS; i++)
535    {
536        BandDefs[i].Frequency = EQNB_5BandPresetsFrequencies[i];
537        BandDefs[i].QFactor   = EQNB_5BandPresetsQFactors[i];
538        BandDefs[i].Gain      = EQNB_5BandNormalPresets[i];
539    }
540
541    /* Volume Control parameters */
542    params.VC_EffectLevel         = 0;
543    params.VC_Balance             = 0;
544
545    /* Treble Enhancement parameters */
546    params.TE_OperatingMode       = LVM_TE_OFF;
547    params.TE_EffectLevel         = 0;
548
549    /* PSA Control parameters */
550    params.PSA_Enable             = LVM_PSA_OFF;
551    params.PSA_PeakDecayRate      = (LVM_PSA_DecaySpeed_en)0;
552
553    /* Bass Enhancement parameters */
554    params.BE_OperatingMode       = LVM_BE_OFF;
555    params.BE_EffectLevel         = 0;
556    params.BE_CentreFreq          = LVM_BE_CENTRE_90Hz;
557    params.BE_HPF                 = LVM_BE_HPF_ON;
558
559    /* PSA Control parameters */
560    params.PSA_Enable             = LVM_PSA_OFF;
561    params.PSA_PeakDecayRate      = LVM_PSA_SPEED_MEDIUM;
562
563    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance,     /* Activate the initial settings */
564                                         &params);
565
566    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmBundle_init")
567    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
568
569    LOGV("\tCreateInstance Succesfully called LVM_SetControlParameters\n");
570
571    /* Set the headroom parameters */
572    HeadroomBandDef[0].Limit_Low          = 20;
573    HeadroomBandDef[0].Limit_High         = 4999;
574    HeadroomBandDef[0].Headroom_Offset    = 3;
575    HeadroomBandDef[1].Limit_Low          = 5000;
576    HeadroomBandDef[1].Limit_High         = 24000;
577    HeadroomBandDef[1].Headroom_Offset    = 4;
578    HeadroomParams.pHeadroomDefinition    = &HeadroomBandDef[0];
579    HeadroomParams.Headroom_OperatingMode = LVM_HEADROOM_ON;
580    HeadroomParams.NHeadroomBands         = 2;
581
582    LvmStatus = LVM_SetHeadroomParams(pContext->pBundledContext->hInstance,
583                                      &HeadroomParams);
584
585    LVM_ERROR_CHECK(LvmStatus, "LVM_SetHeadroomParams", "LvmBundle_init")
586    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
587
588    LOGV("\tCreateInstance Succesfully called LVM_SetHeadroomParams\n");
589    #endif    /* end LVM_BUNDLE */
590
591    LOGV("\tLvmBundle_init End");
592    return 0;
593}   /* end LvmBundle_init */
594
595
596
597//----------------------------------------------------------------------------
598// LvmBundle_process()
599//----------------------------------------------------------------------------
600// Purpose:
601// Apply LVM Bundle effects
602//
603// Inputs:
604//  pIn:        pointer to stereo 16 bit input data
605//  pOut:       pointer to stereo 16 bit output data
606//  frameCount: Frames to process
607//  pContext:   effect engine context
608//  strength    strength to be applied
609//
610//  Outputs:
611//  pOut:       pointer to updated stereo 16 bit output data
612//
613//----------------------------------------------------------------------------
614
615int LvmBundle_process(LVM_INT16        *pIn,
616             LVM_INT16        *pOut,
617             int              frameCount,
618             EffectContext *pContext){
619
620    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
621    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
622
623
624    #ifdef LVM_BUNDLE
625	LVM_INT16				*pOutTmp;
626	if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_WRITE){
627		//LOGV("\tprocess: mBehavior is EFFECT_BUFFER_ACCESS_WRITE %d\n",
628		//		pContext->config.outputCfg.accessMode);
629		pOutTmp = pOut;
630	}else if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE){
631		//LOGV("\tprocess: mBehavior is EFFECT_BUFFER_ACCESS_ACCUMULATE %d\n",
632		//		pContext->config.outputCfg.accessMode);
633		pOutTmp = (LVM_INT16 *)malloc(frameCount * sizeof(LVM_INT16) * 2);
634		if(pOutTmp == NULL){
635			LOGV("\tLVM_ERROR : LvmBundle_process failed to allocate memory for EFFECT_BUFFER_ACCESS_ACCUMULATE mode");
636			return -EINVAL;
637		}
638	}else{
639		LOGV("LVM_ERROR : LvmBundle_process invalid access mode");
640		return -EINVAL;
641	}
642
643	/* Process the samples */
644    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,  /* Get the current settings */
645                                         &ActiveParams);
646
647    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "LvmEffect_enable")
648    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
649    LOGV("\t%d %d %d\n",ActiveParams.BE_OperatingMode, ActiveParams.VirtualizerOperatingMode, ActiveParams.EQNB_OperatingMode);
650
651
652    LvmStatus = LVM_Process(pContext->pBundledContext->hInstance, /* Instance handle */
653                            pIn,                                  /* Input buffer */
654                            pOutTmp,                              /* Output buffer */
655                            (LVM_UINT16)frameCount,               /* Number of samples to read */
656                            0);                                   /* Audo Time */
657    LVM_ERROR_CHECK(LvmStatus, "LVM_Process", "LvmBundle_process")
658    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
659
660	if (pContext->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE){
661		for (int i=0; i<frameCount*2; i++){
662			pOut[i] +=  pOutTmp[i];
663		}
664		free(pOutTmp);
665	}
666    #else
667    memcpy(pOut, pIn, frameCount*sizeof(LVM_INT16)*2); // 2 is for stereo input
668    #endif    /* end LVM_BUNDLE */
669    return 0;
670}    /* end LvmBundle_process */
671
672//----------------------------------------------------------------------------
673// LvmEffect_enable()
674//----------------------------------------------------------------------------
675// Purpose: Enable the effect in the bundle
676//
677// Inputs:
678//  pContext:   effect engine context
679//
680// Outputs:
681//
682//----------------------------------------------------------------------------
683
684int LvmEffect_enable(EffectContext *pContext){
685    LOGV("\tLvmEffect_enable start");
686
687    #ifdef LVM_BUNDLE
688    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
689    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
690
691    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,  /* Get the current settings */
692                                         &ActiveParams);
693
694    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "LvmEffect_enable")
695    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
696    LOGV("\tLvmEffect_enable Succesfully called LVM_GetControlParameters\n");
697
698    if(pContext->EffectType == LVM_BASS_BOOST) {
699        LOGV("\tLvmEffect_enable : Enabling LVM_BASS_BOOST");
700        ActiveParams.BE_OperatingMode       = LVM_BE_ON;
701    }
702    if(pContext->EffectType == LVM_VIRTUALIZER) {
703        LOGV("\tLvmEffect_enable : Enabling LVM_VIRTUALIZER");
704        ActiveParams.VirtualizerOperatingMode   = LVM_MODE_ON;
705    }
706    if(pContext->EffectType == LVM_EQUALIZER) {
707        LOGV("\tLvmEffect_enable : Enabling LVM_EQUALIZER");
708        ActiveParams.EQNB_OperatingMode     = LVM_EQNB_ON;
709    }
710    if(pContext->EffectType == LVM_VOLUME) {
711        LOGV("\tLvmEffect_enable : Enabling LVM_VOLUME");
712    }
713
714    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
715    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmEffect_enable")
716    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
717    LOGV("\tLvmEffect_enable Succesfully called LVM_SetControlParameters\n");
718    #endif    /* end LVM_BUNDLE */
719
720    LOGV("\tLvmEffect_enable end");
721    return 0;
722}
723
724
725//----------------------------------------------------------------------------
726// LvmEffect_disable()
727//----------------------------------------------------------------------------
728// Purpose: Disable the effect in the bundle
729//
730// Inputs:
731//  pContext:   effect engine context
732//
733// Outputs:
734//
735//----------------------------------------------------------------------------
736
737int LvmEffect_disable(EffectContext *pContext){
738    LOGV("\tLvmEffect_disable start");
739
740    #ifdef LVM_BUNDLE
741    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
742    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
743
744    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,  /* Get the current settings */
745                                         &ActiveParams);
746
747    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "LvmEffect_disable")
748    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
749    LOGV("\tLvmEffect_disable Succesfully called LVM_GetControlParameters\n");
750
751    if(pContext->EffectType == LVM_BASS_BOOST) {
752        LOGV("\tLvmEffect_disable : Disabling LVM_BASS_BOOST");
753        ActiveParams.BE_OperatingMode       = LVM_BE_OFF;
754    }
755    if(pContext->EffectType == LVM_VIRTUALIZER) {
756        LOGV("\tLvmEffect_enable : Enabling LVM_VIRTUALIZER");
757        ActiveParams.VirtualizerOperatingMode   = LVM_MODE_OFF;
758    }
759    if(pContext->EffectType == LVM_EQUALIZER) {
760        LOGV("\tLvmEffect_enable : Enabling LVM_EQUALIZER");
761        ActiveParams.EQNB_OperatingMode     = LVM_EQNB_OFF;
762    }
763    if(pContext->EffectType == LVM_VOLUME) {
764        LOGV("\tLvmEffect_enable : Enabling LVM_VOLUME");
765    }
766
767    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
768    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmEffect_disable")
769    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
770    LOGV("\tLvmEffect_enable Succesfully called LVM_SetControlParameters\n");
771    #endif    /* end LVM_BUNDLE */
772
773    LOGV("\tLvmEffect_enable end");
774    return 0;
775}
776
777
778//----------------------------------------------------------------------------
779// LvmEffect_free()
780//----------------------------------------------------------------------------
781// Purpose: Free all memory associated with the Bundle.
782//
783// Inputs:
784//  pContext:   effect engine context
785//
786// Outputs:
787//
788//----------------------------------------------------------------------------
789
790void LvmEffect_free(EffectContext *pContext){
791    #ifdef LVM_BUNDLE
792    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;         /* Function call status */
793    LVM_ControlParams_t     params;                        /* Control Parameters */
794    LVM_MemTab_t            MemTab;
795
796    /* Free the algorithm memory */
797    LvmStatus = LVM_GetMemoryTable(pContext->pBundledContext->hInstance,
798                                   &MemTab,
799                                   LVM_NULL);
800
801    LVM_ERROR_CHECK(LvmStatus, "LVM_GetMemoryTable", "LvmEffect_free")
802
803    for (int i=0; i<LVM_NR_MEMORY_REGIONS; i++){
804        if (MemTab.Region[i].Size != 0){
805            if (MemTab.Region[i].pBaseAddress != NULL){
806                LOGV("\tfree() - START freeing %ld bytes for region %u at %p\n",
807                        MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
808
809                free(MemTab.Region[i].pBaseAddress);
810
811                LOGV("\tfree() - END   freeing %ld bytes for region %u at %p\n",
812                        MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
813            }else{
814                LOGV("\tLVM_ERROR : free() - trying to free with NULL pointer %ld bytes "
815                        "for region %u at %p ERROR\n",
816                        MemTab.Region[i].Size, i, MemTab.Region[i].pBaseAddress);
817            }
818        }
819    }
820    #endif    /* end LVM_BUNDLE */
821}    /* end LvmEffect_free */
822
823//----------------------------------------------------------------------------
824// Effect_configure()
825//----------------------------------------------------------------------------
826// Purpose: Set input and output audio configuration.
827//
828// Inputs:
829//  pContext:   effect engine context
830//  pConfig:    pointer to effect_config_t structure holding input and output
831//      configuration parameters
832//
833// Outputs:
834//
835//----------------------------------------------------------------------------
836
837int Effect_configure(EffectContext *pContext, effect_config_t *pConfig){
838    LOGV("\tEffect_configure start");
839
840    CHECK_ARG(pContext != NULL);
841    CHECK_ARG(pConfig != NULL);
842
843    CHECK_ARG(pConfig->inputCfg.samplingRate == pConfig->outputCfg.samplingRate);
844    CHECK_ARG(pConfig->inputCfg.channels == pConfig->outputCfg.channels);
845    CHECK_ARG(pConfig->inputCfg.format == pConfig->outputCfg.format);
846    CHECK_ARG(pConfig->inputCfg.channels == CHANNEL_STEREO);
847    CHECK_ARG(pConfig->outputCfg.accessMode == EFFECT_BUFFER_ACCESS_WRITE
848              || pConfig->outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE);
849    CHECK_ARG(pConfig->inputCfg.format == SAMPLE_FORMAT_PCM_S15);
850
851    LOGV("\tEffect_configure calling memcpy");
852    memcpy(&pContext->config, pConfig, sizeof(effect_config_t));
853    LOGV("\tEffect_configure End");
854    return 0;
855}   /* end Effect_configure */
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881//----------------------------------------------------------------------------
882// BassGetStrength()
883//----------------------------------------------------------------------------
884// Purpose:
885// get the effect strength currently being used, what is actually returned is the strengh that was
886// previously used in the set, this is because the app uses a strength in the range 0-1000 while
887// the bassboost uses 1-15, so to avoid a quantisation the original set value is used. However the
888// actual used value is checked to make sure it corresponds to the one being returned
889//
890// Inputs:
891//  pContext:   effect engine context
892//
893//----------------------------------------------------------------------------
894
895uint32_t BassGetStrength(EffectContext *pContext){
896    LOGV("\tBassGetStrength() (0-1000) -> %d\n", pContext->pBundledContext->BassStrengthSaved);
897
898    #ifdef LVM_BUNDLE
899    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
900    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
901
902    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,  /* Get the current settings */
903                                         &ActiveParams);
904
905    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "BassGetStrength")
906    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
907
908    LOGV("\tBassGetStrength Succesfully returned from LVM_GetControlParameters\n");
909
910    /* Check that the strength returned matches the strength that was set earlier */
911    if(ActiveParams.BE_EffectLevel != (LVM_INT16)((15*pContext->pBundledContext->BassStrengthSaved)/1000)){
912        LOGV("\tLVM_ERROR : BassGetStrength module strength does not match savedStrength %d %d\n",
913                ActiveParams.BE_EffectLevel, pContext->pBundledContext->BassStrengthSaved);
914        return -EINVAL;
915    }
916
917    LOGV("\tBassGetStrength() (0-15)   -> %d\n", ActiveParams.BE_EffectLevel );
918    #endif    /* end LVM_BUNDLE */
919
920    LOGV("\tBassGetStrength() (saved)  -> %d\n", pContext->pBundledContext->BassStrengthSaved );
921    return pContext->pBundledContext->BassStrengthSaved;
922}    /* end BassGetStrength */
923
924//----------------------------------------------------------------------------
925// BassSetStrength()
926//----------------------------------------------------------------------------
927// Purpose:
928// Apply the strength to the BassBosst. Must first be converted from the range 0-1000 to 1-15
929//
930// Inputs:
931//  pContext:   effect engine context
932//  strength    strength to be applied
933//
934//----------------------------------------------------------------------------
935
936void BassSetStrength(EffectContext *pContext, uint32_t strength){
937    LOGV("\tBassSetStrength(%d)", strength);
938
939    pContext->pBundledContext->BassStrengthSaved = (int)strength;
940
941    #ifdef LVM_BUNDLE
942    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
943    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
944
945    /* Get the current settings */
946    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,
947                                         &ActiveParams);
948
949    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "BassSetStrength")
950    LOGV("\tsetStrength Succesfully returned from LVM_GetControlParameters\n");
951
952    /* Bass Enhancement parameters */
953//    ActiveParams.BE_OperatingMode  = LVM_BE_ON;
954    ActiveParams.BE_EffectLevel    = (LVM_INT16)((15*strength)/1000);
955    ActiveParams.BE_CentreFreq     = LVM_BE_CENTRE_90Hz;
956
957    LOGV("\tBassSetStrength() (0-15)   -> %d\n", ActiveParams.BE_EffectLevel );
958
959    /* Activate the initial settings */
960    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
961
962    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "BassSetStrength")
963    LOGV("\tBassSetStrength Succesfully called LVM_SetControlParameters\n");
964    #endif    /* end LVM_BUNDLE */
965}    /* end BassSetStrength */
966
967//----------------------------------------------------------------------------
968// VirtualizerGetStrength()
969//----------------------------------------------------------------------------
970// Purpose:
971// get the effect strength currently being used, what is actually returned is the strengh that was
972// previously used in the set, this is because the app uses a strength in the range 0-1000 while
973// the Virtualizer uses 1-100, so to avoid a quantisation the original set value is used. However the
974// actual used value is checked to make sure it corresponds to the one being returned
975//
976// Inputs:
977//  pContext:   effect engine context
978//
979//----------------------------------------------------------------------------
980
981uint32_t VirtualizerGetStrength(EffectContext *pContext){
982    LOGV("\tVirtualizerGetStrength() (0-1000) -> %d\n", pContext->pBundledContext->VirtStrengthSaved );
983
984    #ifdef LVM_BUNDLE
985    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
986    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
987
988    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
989
990    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VirtualizerGetStrength")
991    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
992
993    LOGV("\tVirtualizerGetStrength Succesfully returned from LVM_GetControlParameters\n");
994    LOGV("\tVirtualizerGetStrength() (0-100)   -> %d\n", ActiveParams.VirtualizerReverbLevel*10);
995    #endif    /* end LVM_BUNDLE */
996
997    return ActiveParams.VirtualizerReverbLevel*10;
998}    /* end getStrength */
999
1000//----------------------------------------------------------------------------
1001// VirtualizerSetStrength()
1002//----------------------------------------------------------------------------
1003// Purpose:
1004// Apply the strength to the Virtualizer. Must first be converted from the range 0-1000 to 1-15
1005//
1006// Inputs:
1007//  pContext:   effect engine context
1008//  strength    strength to be applied
1009//
1010//----------------------------------------------------------------------------
1011
1012void VirtualizerSetStrength(EffectContext *pContext, uint32_t strength){
1013    LOGV("\tVirtualizerSetStrength(%d)", strength);
1014
1015    pContext->pBundledContext->VirtStrengthSaved = (int)strength;
1016
1017    #ifdef LVM_BUNDLE
1018    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
1019    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
1020
1021    /* Get the current settings */
1022    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,&ActiveParams);
1023
1024    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VirtualizerSetStrength")
1025    LOGV("\tVirtualizerSetStrength Succesfully returned from LVM_GetControlParameters\n");
1026
1027    /* Virtualizer parameters */
1028    ActiveParams.VirtualizerReverbLevel    = (LVM_INT16)(strength/10);
1029
1030    LOGV("\tVirtualizerSetStrength() (0-1000)   -> %d\n", strength );
1031    LOGV("\tVirtualizerSetStrength() (0- 100)   -> %d\n", ActiveParams.VirtualizerReverbLevel );
1032
1033    /* Activate the initial settings */
1034    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1035    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "VirtualizerSetStrength")
1036    LOGV("\tVirtualizerSetStrength Succesfully called LVM_SetControlParameters\n");
1037    #endif    /* end LVM_BUNDLE */
1038}    /* end setStrength */
1039
1040//----------------------------------------------------------------------------
1041// EqualizerGetBandLevel()
1042//----------------------------------------------------------------------------
1043// Purpose: Retrieve the gain currently being used for the band passed in
1044//
1045// Inputs:
1046//  band:       band number
1047//  pContext:   effect engine context
1048//
1049// Outputs:
1050//
1051//----------------------------------------------------------------------------
1052int32_t EqualizerGetBandLevel(EffectContext *pContext, int32_t band){
1053
1054	int32_t Gain =0;
1055
1056	#ifdef LVM_BUNDLE
1057	LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
1058	LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
1059	LVM_EQNB_BandDef_t      *BandDef;
1060
1061	LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,        /* Get the current settings */
1062										 &ActiveParams);
1063
1064	LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "getBandLevel")
1065
1066	BandDef = ActiveParams.pEQNB_BandDefinition;
1067	Gain    = (int32_t)BandDef[band].Gain*100;    // Convert to millibels
1068
1069	LOGV("\tgetBandLevel() -> %d\n", Gain );
1070	LOGV("\tgetBandLevel Succesfully returned from LVM_GetControlParameters\n");
1071	#endif    /* end LVM_BUNDLE */
1072
1073	return Gain;
1074}
1075
1076//----------------------------------------------------------------------------
1077// EqualizerSetBandLevel()
1078//----------------------------------------------------------------------------
1079// Purpose:
1080//  Sets gain value for the given band.
1081//
1082// Inputs:
1083//  band:       band number
1084//  Gain:       Gain to be applied in millibels
1085//  pContext:   effect engine context
1086//
1087// Outputs:
1088//
1089//---------------------------------------------------------------------------
1090void EqualizerSetBandLevel(EffectContext *pContext, int band, int Gain){
1091	int gainRounded;
1092	if(Gain > 0){
1093		gainRounded = (int)((Gain+50)/100);
1094	}else{
1095		gainRounded = (int)((Gain-50)/100);
1096	}
1097    LOGV("\tsetBandLevel(%d)->(%d)", Gain, gainRounded);
1098
1099    #ifdef LVM_BUNDLE
1100    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
1101    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
1102	LVM_EQNB_BandDef_t      *BandDef;
1103
1104    /* Get the current settings */
1105    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1106    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "setBandLevel")
1107    LOGV("\tsetBandLevel Succesfully returned from LVM_GetControlParameters\n");
1108    LOGV("\tsetBandLevel() just Got -> %d\n", ActiveParams.pEQNB_BandDefinition[band].Gain);
1109
1110    /* Set local EQ parameters */
1111	BandDef = ActiveParams.pEQNB_BandDefinition;
1112	ActiveParams.pEQNB_BandDefinition[band].Gain = gainRounded;
1113
1114    /* Activate the initial settings */
1115    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1116    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "setBandLevel")
1117    LOGV("\tsetBandLevel() just Set -> %d\n", ActiveParams.pEQNB_BandDefinition[band].Gain);
1118
1119    /* Get the current settings to check they were applied ok, might not work as process not called*/
1120    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1121    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "setBandLevel")
1122    LOGV("\tLVM_ERROR : remove this test setBandLevel Succesfully returned from LVM_GetControlParameters\n");
1123    LOGV("\tsetBandLevel() just Got -> %d\n", ActiveParams.pEQNB_BandDefinition[band].Gain);
1124    LOGV("\tsetBandLevel Succesfully called LVM_SetControlParameters\n");
1125    #endif    /* end LVM_BUNDLE */
1126
1127    pContext->pBundledContext->CurPreset = PRESET_CUSTOM;
1128    return;
1129}
1130//----------------------------------------------------------------------------
1131// EqualizerGetCentreFrequency()
1132//----------------------------------------------------------------------------
1133// Purpose: Retrieve the frequency being used for the band passed in
1134//
1135// Inputs:
1136//  band:       band number
1137//  pContext:   effect engine context
1138//
1139// Outputs:
1140//
1141//----------------------------------------------------------------------------
1142int32_t EqualizerGetCentreFrequency(EffectContext *pContext, int32_t band){
1143	int32_t Frequency =0;
1144
1145	#ifdef LVM_BUNDLE
1146	LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
1147	LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
1148	LVM_EQNB_BandDef_t      *BandDef;
1149
1150	LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,        /* Get the current settings */
1151										 &ActiveParams);
1152
1153	LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "getCentreFrequency")
1154
1155	BandDef   = ActiveParams.pEQNB_BandDefinition;
1156	Frequency = (int32_t)BandDef[band].Frequency*1000;     // Convert to millibels
1157
1158	LOGV("\tgetCentreFrequency() -> %d\n", Frequency );
1159	LOGV("\tgetCentreFrequency Succesfully returned from LVM_GetControlParameters\n");
1160	#endif    /* end LVM_BUNDLE */
1161
1162	return Frequency;
1163}
1164
1165//----------------------------------------------------------------------------
1166// EqualizerGetBandFreqRange(
1167//----------------------------------------------------------------------------
1168// Purpose:
1169//
1170// Gets lower and upper boundaries of a band.
1171// For the high shelf, the low bound is the band frequency and the high
1172// bound is Nyquist.
1173// For the peaking filters, they are the gain[dB]/2 points.
1174//
1175// Inputs:
1176//  band:       band number
1177//  pContext:   effect engine context
1178//
1179// Outputs:
1180//  pLow:       lower band range
1181//  pLow:       upper band range
1182//----------------------------------------------------------------------------
1183int32_t EqualizerGetBandFreqRange(EffectContext *pContext, int32_t band, uint32_t *pLow, uint32_t *pHi){
1184	*pLow = bandFreqRange[band][0];
1185	*pHi  = bandFreqRange[band][1];
1186	return 0;
1187}
1188
1189//----------------------------------------------------------------------------
1190// EqualizerGetBand(
1191//----------------------------------------------------------------------------
1192// Purpose:
1193//
1194// Returns the band with the maximum influence on a given frequency.
1195// Result is unaffected by whether EQ is enabled or not, or by whether
1196// changes have been committed or not.
1197//
1198// Inputs:
1199//  targetFreq   The target frequency, in millihertz.
1200//  pContext:    effect engine context
1201//
1202// Outputs:
1203//  pLow:       lower band range
1204//  pLow:       upper band range
1205//----------------------------------------------------------------------------
1206int32_t EqualizerGetBand(EffectContext *pContext, uint32_t targetFreq){
1207    int band = 0;
1208
1209	if(targetFreq < bandFreqRange[0][0]){
1210		return -EINVAL;
1211	}else if(targetFreq == bandFreqRange[0][0]){
1212		return 0;
1213	}
1214	for(int i=0; i<FIVEBAND_NUMBANDS;i++){
1215		if((targetFreq > bandFreqRange[i][0])&&(targetFreq <= bandFreqRange[i][1])){
1216			band = i;
1217		}
1218	}
1219    return band;
1220}
1221
1222//----------------------------------------------------------------------------
1223// EqualizerGetPreset(
1224//----------------------------------------------------------------------------
1225// Purpose:
1226//
1227// Gets the currently set preset ID.
1228// Will return PRESET_CUSTOM in case the EQ parameters have been modified
1229// manually since a preset was set.
1230//
1231// Inputs:
1232//  pContext:    effect engine context
1233//
1234//----------------------------------------------------------------------------
1235int32_t EqualizerGetPreset(EffectContext *pContext){
1236	return pContext->pBundledContext->CurPreset;
1237}
1238
1239//----------------------------------------------------------------------------
1240// EqualizerSetPreset(
1241//----------------------------------------------------------------------------
1242// Purpose:
1243//
1244// Sets the current preset by ID.
1245// All the band parameters will be overridden.
1246//
1247// Inputs:
1248//  pContext:    effect engine context
1249//  preset       The preset ID.
1250//
1251//----------------------------------------------------------------------------
1252void EqualizerSetPreset(EffectContext *pContext, int preset){
1253
1254    LOGV("\tsetPreset(%d)", preset);
1255    pContext->pBundledContext->CurPreset = preset;
1256
1257    #ifdef LVM_BUNDLE
1258    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
1259    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
1260
1261    /* Get the current settings */
1262    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1263    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "setPreset")
1264    LOGV("\tsetPreset Succesfully returned from LVM_GetControlParameters\n");
1265
1266    //ActiveParams.pEQNB_BandDefinition = &BandDefs[0];
1267    for (int i=0; i<FIVEBAND_NUMBANDS; i++)
1268    {
1269    	ActiveParams.pEQNB_BandDefinition[i].Frequency = EQNB_5BandPresetsFrequencies[i];
1270    	ActiveParams.pEQNB_BandDefinition[i].QFactor   = EQNB_5BandPresetsQFactors[i];
1271    	ActiveParams.pEQNB_BandDefinition[i].Gain
1272    	= EQNB_5BandNormalPresets[i + preset * FIVEBAND_NUMBANDS];
1273    }
1274    /* Activate the new settings */
1275    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1276    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "setPreset")
1277    LOGV("\tsetPreset Succesfully called LVM_SetControlParameters\n");
1278    #endif    /* end LVM_BUNDLE */
1279
1280    return;
1281}
1282int32_t EqualizerGetNumPresets(){
1283	return 10;
1284}
1285
1286//----------------------------------------------------------------------------
1287// EqualizerGetPresetName(
1288//----------------------------------------------------------------------------
1289// Purpose:
1290// Gets a human-readable name for a preset ID. Will return "Custom" if
1291// PRESET_CUSTOM is passed.
1292//
1293// Inputs:
1294// preset       The preset ID. Must be less than number of presets.
1295//
1296//-------------------------------------------------------------------------
1297const char * EqualizerGetPresetName(int32_t preset){
1298    LOGV("\tEqualizerGetPresetName start(%d)", preset);
1299    if (preset == PRESET_CUSTOM) {
1300        return "Custom";
1301    } else {
1302        return gEqualizerPresets[preset].name;
1303    }
1304    LOGV("\tEqualizerGetPresetName end(%d)", preset);
1305	return 0;
1306}
1307
1308//----------------------------------------------------------------------------
1309// VolumeSetVolumeLevel()
1310//----------------------------------------------------------------------------
1311// Purpose:
1312//
1313// Inputs:
1314//  pContext:   effect engine context
1315//  level       level to be applied
1316//
1317//----------------------------------------------------------------------------
1318
1319int VolumeSetVolumeLevel(EffectContext *pContext, int16_t level){
1320    LOGV("\tVolumeSetVolumeLevel start(%d)", level);
1321
1322    #ifdef LVM_BUNDLE
1323    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
1324    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
1325
1326    /* Get the current settings */
1327    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1328    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VolumeSetVolumeLevel")
1329    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1330    LOGV("\tVolumeSetVolumeLevel Succesfully returned from LVM_GetControlParameters\n");
1331
1332    /* Volume parameters */
1333    ActiveParams.VC_EffectLevel  = (LVM_INT16)(level/100);
1334    LOGV("\tVolumeSetVolumeLevel() (-96dB -> 0dB)   -> %d\n", ActiveParams.VC_EffectLevel );
1335
1336    /* Activate the initial settings */
1337    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1338    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "VolumeSetVolumeLevel")
1339    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1340
1341    LOGV("\tVolumeSetVolumeLevel Succesfully called LVM_SetControlParameters\n");
1342    LOGV("\tVolumeSetVolumeLevel end");
1343    #endif    /* end LVM_BUNDLE */
1344    return 0;
1345}    /* end setVolumeLevel */
1346
1347//----------------------------------------------------------------------------
1348// VolumeGetVolumeLevel()
1349//----------------------------------------------------------------------------
1350// Purpose:
1351//
1352// Inputs:
1353//  pContext:   effect engine context
1354//
1355//----------------------------------------------------------------------------
1356
1357int VolumeGetVolumeLevel(EffectContext *pContext, int16_t *level){
1358
1359    LOGV("\tVolumeGetVolumeLevel start");
1360    #ifdef LVM_BUNDLE
1361    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
1362    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
1363
1364    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1365    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VolumeGetVolumeLevel")
1366    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1367
1368    LOGV("\tVolumeGetVolumeLevel() (-96dB -> 0dB) -> %d\n", ActiveParams.VC_EffectLevel );
1369    LOGV("\tVolumeGetVolumeLevel Succesfully returned from LVM_GetControlParameters\n");
1370    #endif    /* end LVM_BUNDLE */
1371
1372    *level = ActiveParams.VC_EffectLevel*100;     // Convert dB to millibels
1373    LOGV("\tVolumeGetVolumeLevel end");
1374    return 0;
1375}    /* end VolumeGetVolumeLevel */
1376
1377//----------------------------------------------------------------------------
1378// VolumeSetMute()
1379//----------------------------------------------------------------------------
1380// Purpose:
1381//
1382// Inputs:
1383//  pContext:   effect engine context
1384//  mute:       enable/disable flag
1385//
1386//----------------------------------------------------------------------------
1387
1388int32_t VolumeSetMute(EffectContext *pContext, uint32_t mute){
1389    LOGV("\tVolumeSetMute start()");
1390
1391    pContext->pBundledContext->bMuteEnabled = mute;
1392
1393    #ifdef LVM_BUNDLE
1394    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
1395    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
1396
1397    /* Get the current settings */
1398    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1399    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "VolumeSetMute")
1400    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1401
1402    LOGV("\tVolumeSetMute Succesfully returned from LVM_GetControlParameters\n");
1403    LOGV("\tVolumeSetMute() to %d, level was %d\n", mute, ActiveParams.BE_EffectLevel );
1404
1405    /* Set appropriate volume level */
1406    if(pContext->pBundledContext->bMuteEnabled == LVM_TRUE){
1407    	pContext->pBundledContext->levelSaved = ActiveParams.VC_EffectLevel;
1408    	ActiveParams.VC_EffectLevel           = -96;
1409    }else{
1410    	ActiveParams.VC_EffectLevel  = pContext->pBundledContext->levelSaved;
1411    }
1412
1413    /* Activate the initial settings */
1414    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
1415    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "VolumeSetMute")
1416    if(LvmStatus != LVM_SUCCESS) return -EINVAL;
1417
1418    LOGV("\tVolumeSetMute Succesfully called LVM_SetControlParameters\n");
1419    #endif    /* end LVM_BUNDLE */
1420    LOGV("\tVolumeSetMute start()");
1421    return 0;
1422}    /* end setMute */
1423
1424//----------------------------------------------------------------------------
1425// VolumeSetMute()
1426//----------------------------------------------------------------------------
1427// Purpose:
1428//
1429// Inputs:
1430//  pContext:   effect engine context
1431//
1432// Ourputs:
1433//  mute:       enable/disable flag
1434//----------------------------------------------------------------------------
1435
1436int32_t VolumeGetMute(EffectContext *pContext, uint32_t *mute){
1437    LOGV("\tVolumeGetMute start");
1438    if((pContext->pBundledContext->bMuteEnabled == LVM_FALSE)||(pContext->pBundledContext->bMuteEnabled == LVM_TRUE)){
1439    	*mute = pContext->pBundledContext->bMuteEnabled;
1440    	return 0;
1441    }else{
1442    	LOGV("\tLVM_ERROR : VolumeGetMute read an invalid value from context %d", pContext->pBundledContext->bMuteEnabled);
1443    	return -EINVAL;
1444    }
1445    LOGV("\tVolumeGetMute start");
1446}    /* end getMute */
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463//----------------------------------------------------------------------------
1464// BassBoost_getParameter()
1465//----------------------------------------------------------------------------
1466// Purpose:
1467// Get a BassBoost parameter
1468//
1469// Inputs:
1470//  pBassBoost       - handle to instance data
1471//  pParam           - pointer to parameter
1472//  pValue           - pointer to variable to hold retrieved value
1473//  pValueSize       - pointer to value size: maximum size as input
1474//
1475// Outputs:
1476//  *pValue updated with parameter value
1477//  *pValueSize updated with actual value size
1478//
1479//
1480// Side Effects:
1481//
1482//----------------------------------------------------------------------------
1483
1484int BassBoost_getParameter(EffectContext     *pContext,
1485                           int32_t              *pParam,
1486                           size_t               *pValueSize,
1487                           void                 *pValue){
1488    int status = 0;
1489    int32_t param = *pParam++;
1490    int32_t param2;
1491    char *name;
1492
1493    LOGV("\tBassBoost_getParameter start");
1494
1495    switch (param){
1496        case BASSBOOST_PARAM_STRENGTH_SUP:
1497            //if (*pValueSize != sizeof(uint32_t)){
1498            //    LOGV("\tLVM_ERROR : BassBoost_getParameter() invalid pValueSize1 %d", *pValueSize);
1499            //    return -EINVAL;
1500            //}
1501            //*pValueSize = sizeof(uint32_t);
1502            //break;
1503        case BASSBOOST_PARAM_STRENGTH:
1504            if (*pValueSize != sizeof(int16_t)){
1505                LOGV("\tLVM_ERROR : BassBoost_getParameter() invalid pValueSize2 %d", *pValueSize);
1506                return -EINVAL;
1507            }
1508            *pValueSize = sizeof(int16_t);
1509            break;
1510
1511        default:
1512        	LOGV("\tLVM_ERROR : BassBoost_getParameter() invalid param %d", param);
1513            return -EINVAL;
1514    }
1515
1516    switch (param){
1517        case BASSBOOST_PARAM_STRENGTH_SUP:
1518            *(uint32_t *)pValue = 1;
1519
1520            LOGV("\tBassBoost_getParameter() BASSBOOST_PARAM_STRENGTH_SUP Value is %d",
1521                    *(uint32_t *)pValue);
1522            break;
1523
1524        case BASSBOOST_PARAM_STRENGTH:
1525            *(int16_t *)pValue = BassGetStrength(pContext);
1526
1527            LOGV("\tBassBoost_getParameter() BASSBOOST_PARAM_STRENGTH Value is %d",
1528                    *(int16_t *)pValue);
1529            break;
1530
1531        default:
1532            LOGV("\tLVM_ERROR : BassBoost_getParameter() invalid param %d", param);
1533            status = -EINVAL;
1534            break;
1535    }
1536
1537    LOGV("\tBassBoost_getParameter end");
1538    return status;
1539} /* end BassBoost_getParameter */
1540
1541
1542//----------------------------------------------------------------------------
1543// BassBoost_setParameter()
1544//----------------------------------------------------------------------------
1545// Purpose:
1546// Set a BassBoost parameter
1547//
1548// Inputs:
1549//  pBassBoost       - handle to instance data
1550//  pParam           - pointer to parameter
1551//  pValue           - pointer to value
1552//
1553// Outputs:
1554//
1555//----------------------------------------------------------------------------
1556
1557int BassBoost_setParameter (EffectContext *pContext, int32_t *pParam, void *pValue){
1558    int status = 0;
1559    int16_t strength;
1560
1561    LOGV("\tBassBoost_setParameter start");
1562
1563    switch (*pParam){
1564        case BASSBOOST_PARAM_STRENGTH:
1565            strength = *(int16_t *)pValue;
1566            LOGV("\tBassBoost_setParameter() BASSBOOST_PARAM_STRENGTH value is %d", strength);
1567            LOGV("\tBassBoost_setParameter() Calling pBassBoost->BassSetStrength");
1568            BassSetStrength(pContext, (int32_t)strength);
1569            LOGV("\tBassBoost_setParameter() Called pBassBoost->BassSetStrength");
1570           break;
1571        default:
1572            LOGV("\tLVM_ERROR : BassBoost_setParameter() invalid param %d", *pParam);
1573            break;
1574    }
1575
1576    LOGV("\tBassBoost_setParameter end");
1577    return status;
1578} /* end BassBoost_setParameter */
1579
1580//----------------------------------------------------------------------------
1581// Virtualizer_getParameter()
1582//----------------------------------------------------------------------------
1583// Purpose:
1584// Get a Virtualizer parameter
1585//
1586// Inputs:
1587//  pVirtualizer     - handle to instance data
1588//  pParam           - pointer to parameter
1589//  pValue           - pointer to variable to hold retrieved value
1590//  pValueSize       - pointer to value size: maximum size as input
1591//
1592// Outputs:
1593//  *pValue updated with parameter value
1594//  *pValueSize updated with actual value size
1595//
1596//
1597// Side Effects:
1598//
1599//----------------------------------------------------------------------------
1600
1601int Virtualizer_getParameter(EffectContext        *pContext,
1602                             int32_t              *pParam,
1603                             size_t               *pValueSize,
1604                             void                 *pValue){
1605    int status = 0;
1606    int32_t param = *pParam++;
1607    int32_t param2;
1608    char *name;
1609
1610    LOGV("\tVirtualizer_getParameter start");
1611
1612    switch (param){
1613        case VIRTUALIZER_PARAM_STRENGTH_SUP:
1614            //if (*pValueSize != sizeof(uint32_t)){
1615            //    LOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid pValueSize1 %d", *pValueSize);
1616            //    return -EINVAL;
1617            //}
1618            //*pValueSize = sizeof(uint32_t);
1619            //break;
1620        case VIRTUALIZER_PARAM_STRENGTH:
1621            if (*pValueSize != sizeof(int16_t)){
1622                LOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid pValueSize2 %d", *pValueSize);
1623                return -EINVAL;
1624            }
1625            *pValueSize = sizeof(int16_t);
1626            break;
1627
1628        default:
1629            LOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid param %d", param);
1630            return -EINVAL;
1631    }
1632
1633    switch (param){
1634        case VIRTUALIZER_PARAM_STRENGTH_SUP:
1635            *(uint32_t *)pValue = 1;
1636
1637            LOGV("\tVirtualizer_getParameter() VIRTUALIZER_PARAM_STRENGTH_SUP Value is %d",
1638                    *(uint32_t *)pValue);
1639            break;
1640
1641        case VIRTUALIZER_PARAM_STRENGTH:
1642            *(int16_t *)pValue = VirtualizerGetStrength(pContext);
1643
1644            LOGV("\tVirtualizer_getParameter() VIRTUALIZER_PARAM_STRENGTH Value is %d",
1645                    *(int16_t *)pValue);
1646            break;
1647
1648        default:
1649            LOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid param %d", param);
1650            status = -EINVAL;
1651            break;
1652    }
1653
1654    LOGV("\tVirtualizer_getParameter end");
1655    return status;
1656} /* end Virtualizer_getParameter */
1657
1658
1659//----------------------------------------------------------------------------
1660// Virtualizer_setParameter()
1661//----------------------------------------------------------------------------
1662// Purpose:
1663// Set a Virtualizer parameter
1664//
1665// Inputs:
1666//  pVirtualizer     - handle to instance data
1667//  pParam           - pointer to parameter
1668//  pValue           - pointer to value
1669//
1670// Outputs:
1671//
1672//----------------------------------------------------------------------------
1673
1674int Virtualizer_setParameter (EffectContext *pContext, int32_t *pParam, void *pValue){
1675    int status = 0;
1676    int16_t strength;
1677
1678    LOGV("\tVirtualizer_setParameter start");
1679
1680    switch (*pParam){
1681        case VIRTUALIZER_PARAM_STRENGTH:
1682            strength = *(int16_t *)pValue;
1683            LOGV("\tVirtualizer_setParameter() VIRTUALIZER_PARAM_STRENGTH value is %d", strength);
1684            LOGV("\tVirtualizer_setParameter() Calling pVirtualizer->setStrength");
1685            VirtualizerSetStrength(pContext, (int32_t)strength);
1686            LOGV("\tVirtualizer_setParameter() Called pVirtualizer->setStrength");
1687           break;
1688        default:
1689            LOGV("\tLVM_ERROR : Virtualizer_setParameter() invalid param %d", *pParam);
1690            break;
1691    }
1692
1693    LOGV("\tVirtualizer_setParameter end");
1694    return status;
1695} /* end Virtualizer_setParameter */
1696
1697//----------------------------------------------------------------------------
1698// Equalizer_getParameter()
1699//----------------------------------------------------------------------------
1700// Purpose:
1701// Get a Equalizer parameter
1702//
1703// Inputs:
1704//  pEqualizer       - handle to instance data
1705//  pParam           - pointer to parameter
1706//  pValue           - pointer to variable to hold retrieved value
1707//  pValueSize       - pointer to value size: maximum size as input
1708//
1709// Outputs:
1710//  *pValue updated with parameter value
1711//  *pValueSize updated with actual value size
1712//
1713//
1714// Side Effects:
1715//
1716//----------------------------------------------------------------------------
1717int Equalizer_getParameter(EffectContext     *pContext,
1718                           int32_t           *pParam,
1719                           size_t            *pValueSize,
1720                           void              *pValue){
1721    int status = 0;
1722    int bMute = 0;
1723    int32_t param = *pParam++;
1724    int32_t param2;
1725    char *name;
1726
1727    LOGV("\tEqualizer_getParameter start");
1728
1729    switch (param) {
1730    case EQ_PARAM_NUM_BANDS:
1731    case EQ_PARAM_CUR_PRESET:
1732    case EQ_PARAM_GET_NUM_OF_PRESETS:
1733        if (*pValueSize < sizeof(int16_t)) {
1734        	LOGV("\tLVM_ERROR : Equalizer_getParameter() invalid pValueSize 1  %d", *pValueSize);
1735            return -EINVAL;
1736        }
1737        *pValueSize = sizeof(int16_t);
1738        break;
1739
1740    case EQ_PARAM_LEVEL_RANGE:
1741    case EQ_PARAM_BAND_FREQ_RANGE:
1742        if (*pValueSize < 2 * sizeof(int32_t)) {
1743        	LOGV("\tLVM_ERROR : Equalizer_getParameter() invalid pValueSize 2  %d", *pValueSize);
1744            return -EINVAL;
1745        }
1746        *pValueSize = 2 * sizeof(int32_t);
1747        break;
1748    case EQ_PARAM_BAND_LEVEL:
1749    case EQ_PARAM_GET_BAND:
1750    case EQ_PARAM_CENTER_FREQ:
1751        if (*pValueSize < sizeof(int32_t)) {
1752        	LOGV("\tLVM_ERROR : Equalizer_getParameter() invalid pValueSize 1  %d", *pValueSize);
1753            return -EINVAL;
1754        }
1755        *pValueSize = sizeof(int32_t);
1756        break;
1757
1758    case EQ_PARAM_GET_PRESET_NAME:
1759        break;
1760
1761    default:
1762    	LOGV("\tLVM_ERROR : Equalizer_getParameter unknown param %d", param);
1763        return -EINVAL;
1764    }
1765
1766    switch (param) {
1767    case EQ_PARAM_NUM_BANDS:
1768        *(int16_t *)pValue = FIVEBAND_NUMBANDS;
1769        LOGV("\tEqualizer_getParameter() EQ_PARAM_NUM_BANDS %d", *(int16_t *)pValue);
1770        break;
1771
1772    case EQ_PARAM_LEVEL_RANGE:
1773        *(int32_t *)pValue = -1500;
1774        *((int32_t *)pValue + 1) = 1500;
1775        LOGV("\tEqualizer_getParameter() EQ_PARAM_LEVEL_RANGE min %d, max %d", *(int32_t *)pValue, *((int32_t *)pValue + 1));
1776        break;
1777
1778    case EQ_PARAM_BAND_LEVEL:
1779        param2 = *pParam;
1780        if (param2 >= FIVEBAND_NUMBANDS) {
1781            status = -EINVAL;
1782            break;
1783        }
1784        *(int32_t *)pValue = EqualizerGetBandLevel(pContext, param2);
1785        LOGV("\tEqualizer_getParameter() EQ_PARAM_BAND_LEVEL band %d, level %d", param2, *(int32_t *)pValue);
1786        break;
1787
1788    case EQ_PARAM_CENTER_FREQ:
1789        param2 = *pParam;
1790        if (param2 >= FIVEBAND_NUMBANDS) {
1791            status = -EINVAL;
1792            break;
1793        }
1794        *(int32_t *)pValue = EqualizerGetCentreFrequency(pContext, param2);
1795        LOGV("\tEqualizer_getParameter() EQ_PARAM_CENTER_FREQ band %d, frequency %d", param2, *(int32_t *)pValue);
1796        break;
1797
1798    case EQ_PARAM_BAND_FREQ_RANGE:
1799        param2 = *pParam;
1800        if (param2 >= FIVEBAND_NUMBANDS) {
1801            status = -EINVAL;
1802            break;
1803        }
1804        EqualizerGetBandFreqRange(pContext, param2, (uint32_t *)pValue, ((uint32_t *)pValue + 1));
1805        LOGV("\tEqualizer_getParameter() EQ_PARAM_BAND_FREQ_RANGE band %d, min %d, max %d", param2, *(int32_t *)pValue, *((int32_t *)pValue + 1));
1806        break;
1807
1808    case EQ_PARAM_GET_BAND:
1809        param2 = *pParam;
1810        *(int32_t *)pValue = EqualizerGetBand(pContext, param2);
1811        LOGV("\tEqualizer_getParameter() EQ_PARAM_GET_BAND frequency %d, band %d", param2, *(int32_t *)pValue);
1812        break;
1813
1814    case EQ_PARAM_CUR_PRESET:
1815        *(int16_t *)pValue = EqualizerGetPreset(pContext);
1816        LOGV("\tEqualizer_getParameter() EQ_PARAM_CUR_PRESET %d", *(int32_t *)pValue);
1817        break;
1818
1819    case EQ_PARAM_GET_NUM_OF_PRESETS:
1820        *(int16_t *)pValue = EqualizerGetNumPresets();
1821        LOGV("\tEqualizer_getParameter() EQ_PARAM_GET_NUM_OF_PRESETS %d", *(int16_t *)pValue);
1822        break;
1823
1824    case EQ_PARAM_GET_PRESET_NAME:
1825        param2 = *pParam;
1826        if (param2 >= EqualizerGetNumPresets()) {
1827        //if (param2 >= 20) {     // AGO FIX
1828            status = -EINVAL;
1829            break;
1830        }
1831        name = (char *)pValue;
1832        strncpy(name, EqualizerGetPresetName(param2), *pValueSize - 1);
1833        name[*pValueSize - 1] = 0;
1834        *pValueSize = strlen(name) + 1;
1835        LOGV("\tEqualizer_getParameter() EQ_PARAM_GET_PRESET_NAME preset %d, name %s len %d", param2, gEqualizerPresets[param2].name, *pValueSize);
1836        break;
1837
1838    default:
1839        LOGV("\tLVM_ERROR : Equalizer_getParameter() invalid param %d", param);
1840        status = -EINVAL;
1841        break;
1842    }
1843
1844    LOGV("\tEqualizer_getParameter end");
1845    return status;
1846} /* end Equalizer_getParameter */
1847
1848//----------------------------------------------------------------------------
1849// Equalizer_setParameter()
1850//----------------------------------------------------------------------------
1851// Purpose:
1852// Set a Equalizer parameter
1853//
1854// Inputs:
1855//  pEqualizer    - handle to instance data
1856//  pParam        - pointer to parameter
1857//  pValue        - pointer to value
1858//
1859// Outputs:
1860//
1861//----------------------------------------------------------------------------
1862int Equalizer_setParameter (EffectContext *pContext, int32_t *pParam, void *pValue){
1863    int status = 0;
1864    int32_t preset;
1865    int32_t band;
1866    int32_t level;
1867    int32_t param = *pParam++;
1868
1869    LOGV("\tEqualizer_setParameter start");
1870    switch (param) {
1871    case EQ_PARAM_CUR_PRESET:
1872        preset = *(int16_t *)pValue;
1873
1874        LOGV("\tEqualizer_setParameter() EQ_PARAM_CUR_PRESET %d", preset);
1875        if ((preset >= EqualizerGetNumPresets())||(preset < 0)) {
1876            status = -EINVAL;
1877            break;
1878        }
1879        EqualizerSetPreset(pContext, preset);
1880        break;
1881    case EQ_PARAM_BAND_LEVEL:
1882        band =  *pParam;
1883        level = *(int32_t *)pValue;
1884        LOGV("\tEqualizer_setParameter() EQ_PARAM_BAND_LEVEL band %d, level %d", band, level);
1885        if (band >= FIVEBAND_NUMBANDS) {
1886            status = -EINVAL;
1887            break;
1888        }
1889        EqualizerSetBandLevel(pContext, band, level);
1890        break;
1891    default:
1892        LOGV("\tLVM_ERROR : setParameter() invalid param %d", param);
1893        break;
1894    }
1895
1896    LOGV("\tEqualizer_setParameter end");
1897    return status;
1898} /* end Equalizer_setParameter */
1899
1900//----------------------------------------------------------------------------
1901// Volume_getParameter()
1902//----------------------------------------------------------------------------
1903// Purpose:
1904// Get a Volume parameter
1905//
1906// Inputs:
1907//  pVolume          - handle to instance data
1908//  pParam           - pointer to parameter
1909//  pValue           - pointer to variable to hold retrieved value
1910//  pValueSize       - pointer to value size: maximum size as input
1911//
1912// Outputs:
1913//  *pValue updated with parameter value
1914//  *pValueSize updated with actual value size
1915//
1916//
1917// Side Effects:
1918//
1919//----------------------------------------------------------------------------
1920
1921int Volume_getParameter(EffectContext     *pContext,
1922                        int32_t           *pParam,
1923                        size_t            *pValueSize,
1924                        void              *pValue){
1925    int status = 0;
1926    int bMute = 0;
1927    int32_t param = *pParam++;
1928    int32_t param2;
1929    char *name;
1930
1931    LOGV("\tVolume_getParameter start");
1932
1933    switch (param){
1934        case VOLUME_PARAM_LEVEL:
1935        case VOLUME_PARAM_MAXLEVEL:
1936        case VOLUME_PARAM_STEREOPOSITION:
1937            if (*pValueSize < sizeof(int16_t)){
1938                LOGV("\tLVM_ERROR : Volume_getParameter() invalid pValueSize 1  %d", *pValueSize);
1939                return -EINVAL;
1940            }
1941            *pValueSize = sizeof(int16_t);
1942            break;
1943
1944        case VOLUME_PARAM_MUTE:
1945        case VOLUME_PARAM_ENABLESTEREOPOSITION:
1946            if (*pValueSize < sizeof(int32_t)){
1947                LOGV("\tLVM_ERROR : Volume_getParameter() invalid pValueSize 2  %d", *pValueSize);
1948                return -EINVAL;
1949            }
1950            *pValueSize = sizeof(int32_t);
1951            break;
1952
1953        default:
1954        	LOGV("\tLVM_ERROR : EVolume_getParameter unknown param %d", param);
1955            return -EINVAL;
1956    }
1957
1958    switch (param){
1959        case VOLUME_PARAM_LEVEL:
1960            status = VolumeGetVolumeLevel(pContext, (int16_t *)(pValue));
1961			LOGV("\tVolume_getParameter() VOLUME_PARAM_LEVEL Value is %d",
1962					*(int16_t *)pValue);
1963            break;
1964
1965        case VOLUME_PARAM_MAXLEVEL:
1966            *(int16_t *)pValue = 0;
1967            LOGV("\tVolume_getParameter() VOLUME_PARAM_MAXLEVEL Value is %d",
1968                    *(int16_t *)pValue);
1969            break;
1970
1971        case VOLUME_PARAM_STEREOPOSITION:
1972            *(int16_t *)pValue = 0;
1973            LOGV("\tVolume_getParameter() VOLUME_PARAM_STEREOPOSITION Value is %d",
1974                    *(int16_t *)pValue);
1975            break;
1976
1977        case VOLUME_PARAM_MUTE:
1978        	status = VolumeGetMute(pContext, (uint32_t *)pValue);
1979			LOGV("\tVolume_getParameter() VOLUME_PARAM_MUTE Value is %d",
1980					*(uint32_t *)pValue);
1981            break;
1982
1983        case VOLUME_PARAM_ENABLESTEREOPOSITION:
1984            *(int32_t *)pValue = 0;
1985            LOGV("\tVolume_getParameter() VOLUME_PARAM_ENABLESTEREOPOSITION Value is %d",
1986                    *(uint32_t *)pValue);
1987            break;
1988
1989        default:
1990            LOGV("\tLVM_ERROR : Volume_getParameter() invalid param %d", param);
1991            status = -EINVAL;
1992            break;
1993    }
1994
1995    LOGV("\tVolume_getParameter end");
1996    return status;
1997} /* end Volume_getParameter */
1998
1999
2000//----------------------------------------------------------------------------
2001// Volume_setParameter()
2002//----------------------------------------------------------------------------
2003// Purpose:
2004// Set a Volume parameter
2005//
2006// Inputs:
2007//  pVolume       - handle to instance data
2008//  pParam        - pointer to parameter
2009//  pValue        - pointer to value
2010//
2011// Outputs:
2012//
2013//----------------------------------------------------------------------------
2014
2015int Volume_setParameter (EffectContext *pContext, int32_t *pParam, void *pValue){
2016    int      status = 0;
2017    int16_t  level;
2018    uint32_t mute;
2019
2020    LOGV("\tVolume_setParameter start");
2021
2022    switch (*pParam){
2023        case VOLUME_PARAM_LEVEL:
2024            level = *(int16_t *)pValue;
2025            LOGV("\tVolume_setParameter() VOLUME_PARAM_LEVEL value is %d", level);
2026            LOGV("\tVolume_setParameter() Calling pVolume->setVolumeLevel");
2027            status = VolumeSetVolumeLevel(pContext, (int16_t)level);
2028            LOGV("\tVolume_setParameter() Called pVolume->setVolumeLevel");
2029            break;
2030
2031        case VOLUME_PARAM_MUTE:
2032        	mute = *(uint32_t *)pValue;
2033        	LOGV("\tVolume_setParameter() Calling pVolume->setMute, mute is %d", mute);
2034        	LOGV("\tVolume_setParameter() Calling pVolume->setMute");
2035        	status = VolumeSetMute(pContext, mute);
2036        	LOGV("\tVolume_setParameter() Called pVolume->setMute");
2037        	break;
2038
2039        case VOLUME_PARAM_ENABLESTEREOPOSITION:
2040        	LOGV("\tVolume_setParameter() VOLUME_PARAM_ENABLESTEREOPOSITION called");
2041        	break;
2042
2043        case VOLUME_PARAM_STEREOPOSITION:
2044        	LOGV("\tVolume_setParameter() VOLUME_PARAM_STEREOPOSITION called");
2045        	break;
2046
2047        default:
2048            LOGV("\tLVM_ERROR : Volume_setParameter() invalid param %d", *pParam);
2049            break;
2050    }
2051
2052    LOGV("\tVolume_setParameter end");
2053    return status;
2054} /* end Volume_setParameter */
2055} // namespace
2056} // namespace
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078/* Effect Control Interface Implementation: Process */
2079extern "C" int Effect_process(effect_interface_t     self,
2080                                 audio_buffer_t         *inBuffer,
2081                                 audio_buffer_t         *outBuffer){
2082    EffectContext * pContext = (EffectContext *) self;
2083    int    status = 0;
2084
2085    //LOGV("\tIn Effect_process");
2086
2087    if (pContext == NULL){
2088        LOGV("\tLVM_ERROR : Effect_process() ERROR pContext == NULL");
2089        return -EINVAL;
2090    }
2091    if (inBuffer == NULL  || inBuffer->raw == NULL  ||
2092            outBuffer == NULL || outBuffer->raw == NULL ||
2093            inBuffer->frameCount != outBuffer->frameCount){
2094        LOGV("\tLVM_ERROR : Effect_process() ERROR NULL INPUT POINTER OR FRAME COUNT IS WRONG");
2095        return -EINVAL;
2096    }
2097
2098    if ((pContext->pBundledContext->bBassEnabled == LVM_FALSE)&&(pContext->EffectType == LVM_BASS_BOOST)){
2099        LOGV("\tLVM_ERROR : Effect_process() ERROR LVM_BASS_BOOST Effect is not enabled");
2100        return -ENODATA;
2101    }
2102    if ((pContext->pBundledContext->bVolumeEnabled == LVM_FALSE)&&(pContext->EffectType == LVM_VOLUME)){
2103        LOGV("\tLVM_ERROR : Effect_process() ERROR LVM_VOLUME Effect is not enabled");
2104        return -ENODATA;
2105    }
2106    if ((pContext->pBundledContext->bEqualizerEnabled == LVM_FALSE)&&(pContext->EffectType == LVM_EQUALIZER)){
2107        LOGV("\tLVM_ERROR : Effect_process() ERROR LVM_EQUALIZER Effect is not enabled");
2108        return -ENODATA;
2109    }
2110    if ((pContext->pBundledContext->bVirtualizerEnabled == LVM_FALSE)&&(pContext->EffectType == LVM_VIRTUALIZER)){
2111        LOGV("\tLVM_ERROR : Effect_process() ERROR LVM_VIRTUALIZER Effect is not enabled");
2112        return -ENODATA;
2113    }
2114
2115    pContext->pBundledContext->NumberEffectsCalled++;
2116
2117    if(pContext->pBundledContext->NumberEffectsCalled == pContext->pBundledContext->NumberEffectsEnabled){
2118        LOGV("\tEffect_process() Calling process with %d frames, %d effects enabled,    %d called: Effect %d", outBuffer->frameCount, pContext->pBundledContext->NumberEffectsEnabled, pContext->pBundledContext->NumberEffectsCalled, pContext->EffectType);
2119        pContext->pBundledContext->NumberEffectsCalled = 0;
2120        /* Process all the available frames, block processing is handled internalLY by the LVM bundle */
2121        status = android::LvmBundle_process(    (LVM_INT16 *)inBuffer->raw,
2122                                                (LVM_INT16 *)outBuffer->raw,
2123                                                outBuffer->frameCount,
2124                                                pContext);
2125        //LOGV("\tEffect_process() Called  process with %d frames, %d effects enabled,    %d called: Effect %d", outBuffer->frameCount, pContext->pBundledContext->NumberEffectsEnabled, pContext->pBundledContext->NumberEffectsCalled, pContext->EffectType);
2126    }else{
2127        LOGV("\tEffect_process() Not Calling process with %d frames, %d effects enabled, %d called: Effect %d", outBuffer->frameCount, pContext->pBundledContext->NumberEffectsEnabled, pContext->pBundledContext->NumberEffectsCalled, pContext->EffectType);
2128        memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount*sizeof(LVM_INT16)*2); // 2 is for stereo input
2129    }
2130    return status;
2131}   /* end Effect_process */
2132
2133/* Effect Control Interface Implementation: Command */
2134extern "C" int Effect_command(effect_interface_t  self,
2135                                 int                 cmdCode,
2136                                 int                 cmdSize,
2137                                 void                *pCmdData,
2138                                 int                 *replySize,
2139                                 void                *pReplyData){
2140    EffectContext * pContext = (EffectContext *) self;
2141    int retsize;
2142    int status = 0;
2143
2144    LOGV("\t\nEffect_command start");
2145
2146    if(pContext->EffectType == LVM_BASS_BOOST){
2147        LOGV("\tEffect_command setting command for LVM_BASS_BOOST");
2148    }
2149    if(pContext->EffectType == LVM_VIRTUALIZER){
2150        LOGV("\tEffect_command setting command for LVM_VIRTUALIZER");
2151    }
2152    if(pContext->EffectType == LVM_EQUALIZER){
2153        LOGV("\tEffect_command setting command for LVM_EQUALIZER");
2154    }
2155    if(pContext->EffectType == LVM_VOLUME){
2156        LOGV("\tEffect_command setting command for LVM_VOLUME");
2157    }
2158
2159    if (pContext == NULL){
2160        LOGV("\tLVM_ERROR : Effect_command ERROR pContext == NULL");
2161        return -EINVAL;
2162    }
2163
2164    LOGV("\tEffect_command INPUTS are: command %d cmdSize %d",cmdCode, cmdSize);
2165
2166    // Incase we disable an effect, next time process is called the number of effect called could be greater
2167    pContext->pBundledContext->NumberEffectsCalled = 0;
2168
2169    LOGV("\tEffect_command NumberEffectsCalled = %d, NumberEffectsEnabled = %d", pContext->pBundledContext->NumberEffectsCalled, pContext->pBundledContext->NumberEffectsEnabled);
2170
2171    switch (cmdCode){
2172        case EFFECT_CMD_INIT:
2173            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT start");
2174            if(pContext->EffectType == LVM_BASS_BOOST){
2175                LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT for LVM_BASS_BOOST");
2176
2177                if (pReplyData == NULL || *replySize != sizeof(int)){
2178                    LOGV("\tLVM_ERROR : BassBoost_command cmdCode Case: "
2179                            "EFFECT_CMD_INIT: ERROR");
2180                    return -EINVAL;
2181                }
2182
2183                android::BassSetStrength(pContext, 0);
2184            }
2185            if(pContext->EffectType == LVM_VIRTUALIZER){
2186                LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT for LVM_VIRTUALIZER");
2187
2188                if (pReplyData == NULL || *replySize != sizeof(int)){
2189                    LOGV("\tLVM_ERROR : Virtualizer_command cmdCode Case: "
2190                            "EFFECT_CMD_INIT: ERROR");
2191                    return -EINVAL;
2192                }
2193
2194                android::VirtualizerSetStrength(pContext, 0);
2195            }
2196            if(pContext->EffectType == LVM_EQUALIZER){
2197                LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT for LVM_EQUALIZER");
2198
2199                if (pReplyData == NULL || *replySize != sizeof(int)){
2200                    LOGV("\tLVM_ERROR : Equalizer_command cmdCode Case: "
2201                            "EFFECT_CMD_INIT: ERROR");
2202                    return -EINVAL;
2203                }
2204
2205                android::EqualizerSetPreset(pContext, 0);
2206            }
2207            if(pContext->EffectType == LVM_VOLUME){
2208                LOGV("\tEffect_command cmdCode Case: "
2209                        "EFFECT_CMD_INIT start");
2210
2211                if (pReplyData == NULL || *replySize != sizeof(int)){
2212                    LOGV("\tLVM_ERROR : Volume_command cmdCode Case: "
2213                            "EFFECT_CMD_INIT: ERROR");
2214                    return -EINVAL;
2215                }
2216
2217                status = android::VolumeSetVolumeLevel(pContext, 0);
2218                if(status == -EINVAL){
2219                	return -EINVAL;
2220                }
2221            }
2222            break;
2223
2224        case EFFECT_CMD_CONFIGURE:
2225            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_CONFIGURE start");
2226            if (pCmdData    == NULL||
2227                cmdSize     != sizeof(effect_config_t)||
2228                pReplyData  == NULL||
2229                *replySize  != sizeof(int)){
2230                LOGV("\tLVM_ERROR : Effect_command cmdCode Case: "
2231                        "EFFECT_CMD_CONFIGURE: ERROR");
2232                return -EINVAL;
2233            }
2234            *(int *) pReplyData = android::Effect_configure(pContext, (effect_config_t *) pCmdData);
2235            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_CONFIGURE end");
2236            break;
2237
2238        case EFFECT_CMD_RESET:
2239            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_RESET start");
2240            android::Effect_configure(pContext, &pContext->config);
2241            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_RESET end");
2242            break;
2243
2244        case EFFECT_CMD_GET_PARAM:{
2245            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_GET_PARAM start");
2246
2247            if(pContext->EffectType == LVM_BASS_BOOST){
2248                if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
2249                        pReplyData == NULL ||*replySize < (int) (sizeof(effect_param_t) + sizeof(int32_t))){
2250                    LOGV("\tLVM_ERROR : BassBoost_command cmdCode Case: "
2251                            "EFFECT_CMD_GET_PARAM: ERROR");
2252                    return -EINVAL;
2253                }
2254                effect_param_t *p = (effect_param_t *)pCmdData;
2255
2256                memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize);
2257
2258                p = (effect_param_t *)pReplyData;
2259
2260                int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
2261
2262                p->status = android::BassBoost_getParameter(pContext,
2263                                                            (int32_t *)p->data,
2264                                                            (size_t  *)&p->vsize,
2265                                                            p->data + voffset);
2266
2267                *replySize = sizeof(effect_param_t) + voffset + p->vsize;
2268
2269                LOGV("\tBassBoost_command EFFECT_CMD_GET_PARAM "
2270                        "*pCmdData %d, *replySize %d, *pReplyData %d ",
2271                        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
2272                        *replySize,
2273                        *(int16_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset));
2274            }
2275
2276            if(pContext->EffectType == LVM_VIRTUALIZER){
2277                if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
2278                        pReplyData == NULL ||*replySize < (int) (sizeof(effect_param_t) + sizeof(int32_t))){
2279                    LOGV("\tLVM_ERROR : Virtualizer_command cmdCode Case: "
2280                            "EFFECT_CMD_GET_PARAM: ERROR");
2281                    return -EINVAL;
2282                }
2283                effect_param_t *p = (effect_param_t *)pCmdData;
2284
2285                memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize);
2286
2287                p = (effect_param_t *)pReplyData;
2288
2289                int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
2290
2291                p->status = android::Virtualizer_getParameter(pContext,
2292                                                             (int32_t *)p->data,
2293                                                             (size_t  *)&p->vsize,
2294                                                              p->data + voffset);
2295
2296                *replySize = sizeof(effect_param_t) + voffset + p->vsize;
2297
2298                LOGV("\tVirtualizer_command EFFECT_CMD_GET_PARAM "
2299                        "*pCmdData %d, *replySize %d, *pReplyData %d ",
2300                        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
2301                        *replySize,
2302                        *(int16_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset));
2303            }
2304            if(pContext->EffectType == LVM_EQUALIZER){
2305                LOGV("\tEqualizer_command cmdCode Case: "
2306                        "EFFECT_CMD_GET_PARAM start");
2307                if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
2308                    pReplyData == NULL || *replySize < (int) (sizeof(effect_param_t) + sizeof(int32_t))) {
2309                    LOGV("\tLVM_ERROR : Equalizer_command cmdCode Case: "
2310                            "EFFECT_CMD_GET_PARAM");
2311                    return -EINVAL;
2312                }
2313                effect_param_t *p = (effect_param_t *)pCmdData;
2314
2315                memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize);
2316
2317                p = (effect_param_t *)pReplyData;
2318
2319                int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
2320
2321                p->status = android::Equalizer_getParameter(pContext, (int32_t *)p->data, &p->vsize,
2322                        p->data + voffset);
2323
2324                *replySize = sizeof(effect_param_t) + voffset + p->vsize;
2325
2326                LOGV("\tEqualizer_command EFFECT_CMD_GET_PARAM *pCmdData %d, *replySize %d, *pReplyData %08x %08x",
2327                        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)), *replySize,
2328                        *(int32_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset),
2329                        *(int32_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset + sizeof(int32_t)));
2330            }
2331            if(pContext->EffectType == LVM_VOLUME){
2332                LOGV("\tVolume_command cmdCode Case: "
2333                        "EFFECT_CMD_GET_PARAM start");
2334                if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
2335                        pReplyData == NULL ||*replySize < (int) (sizeof(effect_param_t) + sizeof(int32_t))){
2336                    LOGV("\tLVM_ERROR : Volume_command cmdCode Case: "
2337                            "EFFECT_CMD_GET_PARAM: ERROR");
2338                    return -EINVAL;
2339                }
2340                effect_param_t *p = (effect_param_t *)pCmdData;
2341
2342                memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize);
2343
2344                p = (effect_param_t *)pReplyData;
2345
2346                int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
2347
2348                p->status = android::Volume_getParameter(pContext,
2349                                                         (int32_t *)p->data,
2350                                                         (size_t  *)&p->vsize,
2351                                                         p->data + voffset);
2352
2353                *replySize = sizeof(effect_param_t) + voffset + p->vsize;
2354
2355                LOGV("\tVolume_command EFFECT_CMD_GET_PARAM "
2356                        "*pCmdData %d, *replySize %d, *pReplyData %d ",
2357                        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
2358                        *replySize,
2359                        *(int16_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset));
2360            }
2361            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_GET_PARAM end");
2362        } break;
2363        case EFFECT_CMD_SET_PARAM:{
2364            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_SET_PARAM start");
2365            if(pContext->EffectType == LVM_BASS_BOOST){
2366                LOGV("\tBassBoost_command EFFECT_CMD_SET_PARAM param %d, *replySize %d, value %d ",
2367                        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
2368                        *replySize,
2369                        *(int16_t *)((char *)pCmdData + sizeof(effect_param_t) + sizeof(int32_t)));
2370
2371                if (    pCmdData   == NULL||
2372                        cmdSize    != (int)(sizeof(effect_param_t) + sizeof(int32_t) + sizeof(int16_t))||
2373                        pReplyData == NULL||
2374                        *replySize != sizeof(int32_t)){
2375                    LOGV("\tLVM_ERROR : BassBoost_command cmdCode Case: "
2376                            "EFFECT_CMD_SET_PARAM: ERROR");
2377                    return -EINVAL;
2378                }
2379                effect_param_t *p = (effect_param_t *) pCmdData;
2380
2381                if (p->psize != sizeof(int32_t)){
2382                    LOGV("\tLVM_ERROR : BassBoost_command cmdCode Case: "
2383                            "EFFECT_CMD_SET_PARAM: ERROR, psize is not sizeof(int32_t)");
2384                    return -EINVAL;
2385                }
2386
2387                LOGV("\tnBassBoost_command cmdSize is %d\n"
2388                		"\tsizeof(effect_param_t) is  %d\n"
2389                		"\tp->psize is %d\n"
2390                		"\tp->vsize is %d"
2391                		"\n",
2392                		cmdSize, sizeof(effect_param_t), p->psize, p->vsize );
2393
2394                *(int *)pReplyData = android::BassBoost_setParameter(pContext,
2395                                                                    (int32_t *)p->data,
2396                                                                    p->data + p->psize);
2397            }
2398            if(pContext->EffectType == LVM_VIRTUALIZER){
2399                LOGV("\tVirtualizer_command EFFECT_CMD_SET_PARAM param %d, *replySize %d, value %d ",
2400                        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
2401                        *replySize,
2402                        *(int16_t *)((char *)pCmdData + sizeof(effect_param_t) + sizeof(int32_t)));
2403
2404                if (    pCmdData   == NULL||
2405                        cmdSize    != (int)(sizeof(effect_param_t) + sizeof(int32_t) + sizeof(int16_t))||
2406                        pReplyData == NULL||
2407                        *replySize != sizeof(int32_t)){
2408                    LOGV("\tLVM_ERROR : Virtualizer_command cmdCode Case: "
2409                            "EFFECT_CMD_SET_PARAM: ERROR");
2410                    return -EINVAL;
2411                }
2412                effect_param_t *p = (effect_param_t *) pCmdData;
2413
2414                if (p->psize != sizeof(int32_t)){
2415                    LOGV("\tLVM_ERROR : Virtualizer_command cmdCode Case: "
2416                            "EFFECT_CMD_SET_PARAM: ERROR, psize is not sizeof(int32_t)");
2417                    return -EINVAL;
2418                }
2419
2420                LOGV("\tnVirtualizer_command cmdSize is %d\n"
2421                        "\tsizeof(effect_param_t) is  %d\n"
2422                        "\tp->psize is %d\n"
2423                        "\tp->vsize is %d"
2424                        "\n",
2425                        cmdSize, sizeof(effect_param_t), p->psize, p->vsize );
2426
2427                *(int *)pReplyData = android::Virtualizer_setParameter(pContext,
2428                                                                      (int32_t *)p->data,
2429                                                                       p->data + p->psize);
2430            }
2431            if(pContext->EffectType == LVM_EQUALIZER){
2432                LOGV("\tEqualizer_command cmdCode Case: "
2433                        "EFFECT_CMD_SET_PARAM start");
2434                LOGV("\tEqualizer_command EFFECT_CMD_SET_PARAM param %d, *replySize %d, value %d ",
2435                        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
2436                        *replySize,
2437                        *(int16_t *)((char *)pCmdData + sizeof(effect_param_t) + sizeof(int32_t)));
2438
2439                if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
2440                    pReplyData == NULL || *replySize != sizeof(int32_t)) {
2441                    LOGV("\tLVM_ERROR : Equalizer_command cmdCode Case: "
2442                            "EFFECT_CMD_SET_PARAM: ERROR");
2443                    return -EINVAL;
2444                }
2445                effect_param_t *p = (effect_param_t *) pCmdData;
2446
2447                *(int *)pReplyData = android::Equalizer_setParameter(pContext,
2448                                                                    (int32_t *)p->data,
2449                                                                     p->data + p->psize);
2450            }
2451            if(pContext->EffectType == LVM_VOLUME){
2452                LOGV("\tVolume_command cmdCode Case: "
2453                        "EFFECT_CMD_SET_PARAM start");
2454                LOGV("\tVolume_command EFFECT_CMD_SET_PARAM param %d, *replySize %d, value %d ",
2455                        *(int32_t *)((char *)pCmdData + sizeof(effect_param_t)),
2456                        *replySize,
2457                        *(int16_t *)((char *)pCmdData + sizeof(effect_param_t) + sizeof(int32_t)));
2458
2459                if (    pCmdData   == NULL||
2460                        cmdSize    < (int)(sizeof(effect_param_t) + sizeof(int32_t))||
2461                        pReplyData == NULL||
2462                        *replySize != sizeof(int32_t)){
2463                    LOGV("\tLVM_ERROR : Volume_command cmdCode Case: "
2464                            "EFFECT_CMD_SET_PARAM: ERROR");
2465                    return -EINVAL;
2466                }
2467                effect_param_t *p = (effect_param_t *) pCmdData;
2468
2469                *(int *)pReplyData = android::Volume_setParameter(pContext,
2470                                                                 (int32_t *)p->data,
2471                                                                 p->data + p->psize);
2472            }
2473            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_SET_PARAM end");
2474        } break;
2475
2476        case EFFECT_CMD_ENABLE:
2477            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_ENABLE start");
2478            if (pReplyData == NULL || *replySize != sizeof(int)){
2479                LOGV("\tLVM_ERROR : Effect_command cmdCode Case: EFFECT_CMD_ENABLE: ERROR");
2480                return -EINVAL;
2481            }
2482            switch (pContext->EffectType){
2483                case LVM_BASS_BOOST:
2484                    if(pContext->pBundledContext->bBassEnabled == LVM_TRUE){
2485                         LOGV("\tLVM_ERROR : BassBoost_command cmdCode Case: "
2486                                 "EFFECT_CMD_ENABLE: ERROR-Effect is already enabled");
2487                         return -EINVAL;
2488                    }
2489                    pContext->pBundledContext->bBassEnabled = LVM_TRUE;
2490                    LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_ENABLE LVM_BASS_BOOST enabled");
2491                    break;
2492                case LVM_EQUALIZER:
2493                    if(pContext->pBundledContext->bEqualizerEnabled == LVM_TRUE){
2494                         LOGV("\tLVM_ERROR : Equalizer_command cmdCode Case: "
2495                                 "EFFECT_CMD_ENABLE: ERROR-Effect is already enabled");
2496                         return -EINVAL;
2497                    }
2498                    pContext->pBundledContext->bEqualizerEnabled = LVM_TRUE;
2499                    LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_ENABLE LVM_EQUALIZER enabled");
2500                    break;
2501                case LVM_VIRTUALIZER:
2502                    if(pContext->pBundledContext->bVirtualizerEnabled == LVM_TRUE){
2503                         LOGV("\tLVM_ERROR : Virtualizer_command cmdCode Case: "
2504                                 "EFFECT_CMD_ENABLE: ERROR-Effect is already enabled");
2505                         return -EINVAL;
2506                    }
2507                    pContext->pBundledContext->bVirtualizerEnabled = LVM_TRUE;
2508                    LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_ENABLE LVM_VIRTUALIZER enabled");
2509                    break;
2510                case LVM_VOLUME:
2511                    if(pContext->pBundledContext->bVolumeEnabled == LVM_TRUE){
2512                         LOGV("\tLVM_ERROR : Volume_command cmdCode Case: "
2513                                 "EFFECT_CMD_ENABLE: ERROR-Effect is already enabled");
2514                         return -EINVAL;
2515                    }
2516                    pContext->pBundledContext->bVolumeEnabled = LVM_TRUE;
2517                    LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_ENABLE LVM_VOLUME enabled");
2518                    break;
2519                default:
2520                    LOGV("\tLVM_ERROR : Effect_command cmdCode Case: "
2521                        "EFFECT_CMD_ENABLE: ERROR, invalid Effect Type");
2522                    return -EINVAL;
2523            }
2524            *(int *)pReplyData = 0;
2525            pContext->pBundledContext->NumberEffectsEnabled++;
2526            android::LvmEffect_enable(pContext);
2527            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_ENABLE NumberEffectsEnabled = %d", pContext->pBundledContext->NumberEffectsEnabled);
2528            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_ENABLE end");
2529            break;
2530
2531        case EFFECT_CMD_DISABLE:
2532            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_DISABLE start");
2533            if (pReplyData == NULL || *replySize != sizeof(int)){
2534                LOGV("\tLVM_ERROR : Effect_command cmdCode Case: EFFECT_CMD_DISABLE: ERROR");
2535                return -EINVAL;
2536            }
2537            switch (pContext->EffectType){
2538                case LVM_BASS_BOOST:
2539                    if(pContext->pBundledContext->bBassEnabled == LVM_FALSE){
2540                         LOGV("\tLVM_ERROR : BassBoost_command cmdCode Case: "
2541                                 "EFFECT_CMD_DISABLE: ERROR-Effect is not yet enabled");
2542                         return -EINVAL;
2543                    }
2544                    pContext->pBundledContext->bBassEnabled = LVM_FALSE;
2545                    LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_DISABLE LVM_BASS_BOOST disabled");
2546                    break;
2547                case LVM_EQUALIZER:
2548                    if(pContext->pBundledContext->bEqualizerEnabled == LVM_FALSE){
2549                         LOGV("\tLVM_ERROR : Equalizer_command cmdCode Case: "
2550                                 "EFFECT_CMD_DISABLE: ERROR-Effect is not yet enabled");
2551                         return -EINVAL;
2552                    }
2553                    pContext->pBundledContext->bEqualizerEnabled = LVM_FALSE;
2554                    LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_DISABLE LVM_EQUALIZER disabled");
2555                    break;
2556                case LVM_VIRTUALIZER:
2557                    if(pContext->pBundledContext->bVirtualizerEnabled == LVM_FALSE){
2558                         LOGV("\tLVM_ERROR : Virtualizer_command cmdCode Case: "
2559                                 "EFFECT_CMD_DISABLE: ERROR-Effect is not yet enabled");
2560                         return -EINVAL;
2561                    }
2562                    pContext->pBundledContext->bVirtualizerEnabled = LVM_FALSE;
2563                    LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_DISABLE LVM_VIRTUALIZER disabled");
2564                    break;
2565                case LVM_VOLUME:
2566                    if(pContext->pBundledContext->bVolumeEnabled == LVM_FALSE){
2567                         LOGV("\tLVM_ERROR : Volume_command cmdCode Case: "
2568                                 "EFFECT_CMD_DISABLE: ERROR-Effect is not yet enabled");
2569                         return -EINVAL;
2570                    }
2571                    pContext->pBundledContext->bVolumeEnabled = LVM_FALSE;
2572                    LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_DISABLE LVM_VOLUME disabled");
2573                    break;
2574                    break;
2575                default:
2576                    LOGV("\tLVM_ERROR : Effect_command cmdCode Case: "
2577                        "EFFECT_CMD_DISABLE: ERROR, invalid Effect Type");
2578                    return -EINVAL;
2579            }
2580            *(int *)pReplyData = 0;
2581            pContext->pBundledContext->NumberEffectsEnabled--;
2582            android::LvmEffect_disable(pContext);
2583            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_DISABLE NumberEffectsEnabled = %d", pContext->pBundledContext->NumberEffectsEnabled);
2584            LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_DISABLE end");
2585            break;
2586
2587        case EFFECT_CMD_SET_DEVICE:
2588        case EFFECT_CMD_SET_VOLUME:
2589//        case EFFECT_CMD_SET_AUDIO_MODE:// AGO add this
2590            LOGV("\tEffect_command cmdCode Case: "
2591                    "EFFECT_CMD_SET_DEVICE/EFFECT_CMD_SET_VOLUME/EFFECT_CMD_SET_AUDIO_MODE start");
2592            break;
2593
2594        default:
2595            LOGV("\tLVM_ERROR : Effect_command cmdCode Case: DEFAULT start %d ERROR",cmdCode);
2596            return -EINVAL;
2597    }
2598
2599    LOGV("\tEffect_command end...\n\n");
2600    return 0;
2601}    /* end Effect_command */
2602
2603// effect_interface_t interface implementation for effect
2604const struct effect_interface_s gLvmEffectInterface = {
2605    Effect_process,
2606    Effect_command
2607};    /* end gLvmEffectInterface */
2608
2609
2610