summaryrefslogtreecommitdiffstats
path: root/media/libeffects/lvm/lib/Bundle/src/LVM_Control.c
blob: 922f77d2bae12f006382025eb9617549b2a95817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
/*
 * Copyright (C) 2004-2010 NXP Software
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/****************************************************************************************

     $Author: nxp007753 $
     $Revision: 1316 $
     $Date: 2010-07-23 11:53:24 +0200 (Fri, 23 Jul 2010) $

*****************************************************************************************/


/****************************************************************************************/
/*                                                                                      */
/* Includes                                                                             */
/*                                                                                      */
/****************************************************************************************/

#include "VectorArithmetic.h"
#include "ScalarArithmetic.h"
#include "LVM_Coeffs.h"
#include "LVM_Tables.h"
#include "LVM_Private.h"

/****************************************************************************************/
/*                                                                                      */
/* FUNCTION:           LVM_SetControlParameters                                         */
/*                                                                                      */
/* DESCRIPTION:                                                                         */
/*  Sets or changes the LifeVibes module parameters.                                    */
/*                                                                                      */
/* PARAMETERS:                                                                          */
/*  hInstance          Instance handle                                                  */
/*  pParams            Pointer to a parameter structure                                 */
/*                                                                                      */
/* RETURNS:                                                                             */
/*  LVM_SUCCESS        Succeeded                                                        */
/*  LVM_NULLADDRESS    When hInstance, pParams or any control pointers are NULL         */
/*  LVM_OUTOFRANGE     When any of the control parameters are out of range              */
/*                                                                                      */
/* NOTES:                                                                               */
/*  1. This function may be interrupted by the LVM_Process function                     */
/*                                                                                      */
/****************************************************************************************/

LVM_ReturnStatus_en LVM_SetControlParameters(LVM_Handle_t           hInstance,
                                             LVM_ControlParams_t    *pParams)
{
    LVM_Instance_t    *pInstance =(LVM_Instance_t  *)hInstance;


    if ((pParams == LVM_NULL) || (hInstance == LVM_NULL))
    {
        return (LVM_NULLADDRESS);
    }

    pInstance->NewParams = *pParams;

    if(
        /* General parameters */
        ((pParams->OperatingMode != LVM_MODE_OFF) && (pParams->OperatingMode != LVM_MODE_ON))                                         ||
        ((pParams->SampleRate != LVM_FS_8000) && (pParams->SampleRate != LVM_FS_11025) && (pParams->SampleRate != LVM_FS_12000)       &&
        (pParams->SampleRate != LVM_FS_16000) && (pParams->SampleRate != LVM_FS_22050) && (pParams->SampleRate != LVM_FS_24000)       &&
        (pParams->SampleRate != LVM_FS_32000) && (pParams->SampleRate != LVM_FS_44100) && (pParams->SampleRate != LVM_FS_48000))      ||
        ((pParams->SourceFormat != LVM_STEREO) && (pParams->SourceFormat != LVM_MONOINSTEREO) && (pParams->SourceFormat != LVM_MONO)) ||
        (pParams->SpeakerType > LVM_EX_HEADPHONES))
    {
        return (LVM_OUTOFRANGE);
    }

    /*
     * Cinema Sound parameters
     */
    if((pParams->VirtualizerOperatingMode != LVM_MODE_OFF) && (pParams->VirtualizerOperatingMode != LVM_MODE_ON))
    {
        return (LVM_OUTOFRANGE);
    }

    if(pParams->VirtualizerType != LVM_CONCERTSOUND)
    {
        return (LVM_OUTOFRANGE);
    }

    if(pParams->VirtualizerReverbLevel > LVM_VIRTUALIZER_MAX_REVERB_LEVEL)
    {
        return (LVM_OUTOFRANGE);
    }

    if(pParams->CS_EffectLevel < LVM_CS_MIN_EFFECT_LEVEL)
    {
        return (LVM_OUTOFRANGE);
    }

    /*
     * N-Band Equalizer
     */
    if(pParams->EQNB_NBands > pInstance->InstParams.EQNB_NumBands)
    {
        return (LVM_OUTOFRANGE);
    }

    /* Definition pointer */
    if ((pParams->pEQNB_BandDefinition == LVM_NULL) &&
        (pParams->EQNB_NBands != 0))
    {
        return (LVM_NULLADDRESS);
    }

    /*
     * Copy the filter definitions for the Equaliser
     */
    {
        LVM_INT16           i;

        if (pParams->EQNB_NBands != 0)
        {
            for (i=0; i<pParams->EQNB_NBands; i++)
            {
                pInstance->pEQNB_BandDefs[i] = pParams->pEQNB_BandDefinition[i];
            }
            pInstance->NewParams.pEQNB_BandDefinition = pInstance->pEQNB_BandDefs;
        }
    }
    if( /* N-Band Equaliser parameters */
        ((pParams->EQNB_OperatingMode != LVM_EQNB_OFF) && (pParams->EQNB_OperatingMode != LVM_EQNB_ON)) ||
        (pParams->EQNB_NBands > pInstance->InstParams.EQNB_NumBands))
    {
        return (LVM_OUTOFRANGE);
    }
    /* Band parameters*/
    {
        LVM_INT16 i;
        for(i = 0; i < pParams->EQNB_NBands; i++)
        {
            if(((pParams->pEQNB_BandDefinition[i].Frequency < LVM_EQNB_MIN_BAND_FREQ)  ||
                (pParams->pEQNB_BandDefinition[i].Frequency > LVM_EQNB_MAX_BAND_FREQ)) ||
                ((pParams->pEQNB_BandDefinition[i].Gain     < LVM_EQNB_MIN_BAND_GAIN)  ||
                (pParams->pEQNB_BandDefinition[i].Gain      > LVM_EQNB_MAX_BAND_GAIN)) ||
                ((pParams->pEQNB_BandDefinition[i].QFactor  < LVM_EQNB_MIN_QFACTOR)     ||
                (pParams->pEQNB_BandDefinition[i].QFactor   > LVM_EQNB_MAX_QFACTOR)))
            {
                return (LVM_OUTOFRANGE);
            }
        }
    }

    /*
     * Bass Enhancement parameters
     */
    if(((pParams->BE_OperatingMode != LVM_BE_OFF) && (pParams->BE_OperatingMode != LVM_BE_ON))                      ||
        ((pParams->BE_EffectLevel < LVM_BE_MIN_EFFECTLEVEL ) || (pParams->BE_EffectLevel > LVM_BE_MAX_EFFECTLEVEL ))||
        ((pParams->BE_CentreFreq != LVM_BE_CENTRE_55Hz) && (pParams->BE_CentreFreq != LVM_BE_CENTRE_66Hz)           &&
        (pParams->BE_CentreFreq != LVM_BE_CENTRE_78Hz) && (pParams->BE_CentreFreq != LVM_BE_CENTRE_90Hz))           ||
        ((pParams->BE_HPF != LVM_BE_HPF_OFF) && (pParams->BE_HPF != LVM_BE_HPF_ON)))
    {
        return (LVM_OUTOFRANGE);
    }

    /*
     * Volume Control parameters
     */
    if((pParams->VC_EffectLevel < LVM_VC_MIN_EFFECTLEVEL ) || (pParams->VC_EffectLevel > LVM_VC_MAX_EFFECTLEVEL ))
    {
        return (LVM_OUTOFRANGE);
    }
    if((pParams->VC_Balance < LVM_VC_BALANCE_MIN ) || (pParams->VC_Balance > LVM_VC_BALANCE_MAX ))
    {
        return (LVM_OUTOFRANGE);
    }

    /*
     * PSA parameters
     */
    if( (pParams->PSA_PeakDecayRate > LVPSA_SPEED_HIGH) ||
        (pParams->PSA_Enable > LVM_PSA_ON))
    {
        return (LVM_OUTOFRANGE);
    }


    /*
    * Set the flag to indicate there are new parameters to use
    *
    * Protect the copy of the new parameters from interrupts to avoid possible problems
    * with loss control parameters. This problem can occur if this control function is called more
    * than once before a call to the process function. If the process function interrupts
    * the copy to NewParams then one frame may have mixed parameters, some old and some new.
    */
    pInstance->ControlPending = LVM_TRUE;
    pInstance->NoSmoothVolume = LVM_FALSE;

    return(LVM_SUCCESS);
}


/****************************************************************************************/
/*                                                                                      */
/* FUNCTION:             LVM_GetControlParameters                                       */
/*                                                                                      */
/* DESCRIPTION:                                                                         */
/*  Request the LifeVibes module parameters. The current parameter set is returned      */
/*  via the parameter pointer.                                                          */
/*                                                                                      */
/* PARAMETERS:                                                                          */
/*  hInstance            Instance handle                                                */
/*  pParams              Pointer to an empty parameter structure                        */
/*                                                                                      */
/* RETURNS:                                                                             */
/*  LVM_SUCCESS          Succeeded                                                      */
/*  LVM_NULLADDRESS      when any of hInstance or pParams is NULL                       */
/*                                                                                      */
/* NOTES:                                                                               */
/*  1. This function may be interrupted by the LVM_Process function                     */
/*                                                                                      */
/****************************************************************************************/

LVM_ReturnStatus_en LVM_GetControlParameters(LVM_Handle_t           hInstance,
                                             LVM_ControlParams_t    *pParams)
{
    LVM_Instance_t    *pInstance =(LVM_Instance_t  *)hInstance;


    /*
     * Check pointer
     */
    if ((pParams == LVM_NULL) || (hInstance == LVM_NULL))
    {
        return (LVM_NULLADDRESS);
    }
    *pParams = pInstance->NewParams;

    /*
     * Copy the filter definitions for the Equaliser
     */
    {
        LVM_INT16           i;

        if (pInstance->NewParams.EQNB_NBands != 0)
        for (i=0; i<pInstance->NewParams.EQNB_NBands; i++)
        {
            pInstance->pEQNB_UserDefs[i] = pInstance->pEQNB_BandDefs[i];
        }
        pParams->pEQNB_BandDefinition = pInstance->pEQNB_UserDefs;
    }

    return(LVM_SUCCESS);
}


/****************************************************************************************/
/*                                                                                      */
/* FUNCTION:                LVM_SetTrebleBoost                                          */
/*                                                                                      */
/* DESCRIPTION:                                                                         */
/*  Enable the treble boost when the settings are appropriate, i.e. non-zero gain       */
/*  and the sample rate is high enough for the effect to be heard.                      */
/*                                                                                      */
/* PARAMETERS:                                                                          */
/*  pInstance               Pointer to the instance structure                           */
/*  pParams                 Pointer to the parameters to use                            */
/*                                                                                      */
/****************************************************************************************/
void LVM_SetTrebleBoost(LVM_Instance_t         *pInstance,
                        LVM_ControlParams_t    *pParams)
{
    extern FO_C16_LShx_Coefs_t  LVM_TrebleBoostCoefs[];
    LVM_INT16               Offset;
    LVM_INT16               EffectLevel = 0;

    /*
     * Load the coefficients
     */
    if ((pParams->TE_OperatingMode == LVM_TE_ON) &&
        (pParams->SampleRate >= TrebleBoostMinRate) &&
        (pParams->OperatingMode == LVM_MODE_ON) &&
        (pParams->TE_EffectLevel > 0))
    {
        if((pParams->TE_EffectLevel == LVM_TE_LOW_MIPS) &&
            ((pParams->SpeakerType == LVM_HEADPHONES)||
            (pParams->SpeakerType == LVM_EX_HEADPHONES)))
        {
            pInstance->TE_Active = LVM_FALSE;
        }
        else
        {
            EffectLevel = pParams->TE_EffectLevel;
            pInstance->TE_Active = LVM_TRUE;
        }

        if(pInstance->TE_Active == LVM_TRUE)
        {
            /*
             * Load the coefficients and enabled the treble boost
             */
            Offset = (LVM_INT16)(EffectLevel - 1 + TrebleBoostSteps * (pParams->SampleRate - TrebleBoostMinRate));
            FO_2I_D16F32Css_LShx_TRC_WRA_01_Init(&pInstance->pTE_State->TrebleBoost_State,
                                            &pInstance->pTE_Taps->TrebleBoost_Taps,
                                            &LVM_TrebleBoostCoefs[Offset]);

            /*
             * Clear the taps
             */
            LoadConst_16((LVM_INT16)0,                                     /* Value */
                         (void *)&pInstance->pTE_Taps->TrebleBoost_Taps,  /* Destination.\
                                                     Cast to void: no dereferencing in function */
                         (LVM_UINT16)(sizeof(pInstance->pTE_Taps->TrebleBoost_Taps)/sizeof(LVM_INT16))); /* Number of words */
        }
    }
    else
    {
        /*
         * Disable the treble boost
         */
        pInstance->TE_Active = LVM_FALSE;
    }

    return;
}


/************************************************************************************/
/*                                                                                  */
/* FUNCTION:            LVM_SetVolume                                               */
/*                                                                                  */
/* DESCRIPTION:                                                                     */
/*  Converts the input volume demand from dBs to linear.                            */
/*                                                                                  */
/* PARAMETERS:                                                                      */
/*  pInstance           Pointer to the instance                                     */
/*  pParams             Initialisation parameters                                   */
/*                                                                                  */
/************************************************************************************/
void    LVM_SetVolume(LVM_Instance_t         *pInstance,
                      LVM_ControlParams_t    *pParams)
{

    LVM_UINT16      dBShifts;                                   /* 6dB shifts */
    LVM_UINT16      dBOffset;                                   /* Table offset */
    LVM_INT16       Volume = 0;                                 /* Required volume in dBs */

    /*
     * Limit the gain to the maximum allowed
     */
     if  (pParams->VC_EffectLevel > 0)
     {
         Volume = 0;
     }
     else
     {
         Volume = pParams->VC_EffectLevel;
     }

     /* Compensate this volume in PSA plot */
     if(Volume > -60)  /* Limit volume loss to PSA Limits*/
         pInstance->PSA_GainOffset=(LVM_INT16)(-Volume);/* Loss is compensated by Gain*/
     else
         pInstance->PSA_GainOffset=(LVM_INT16)60;/* Loss is compensated by Gain*/

    pInstance->VC_AVLFixedVolume = 0;

    /*
     * Set volume control and AVL volumes according to headroom and volume user setting
     */
    if(pParams->OperatingMode == LVM_MODE_ON)
    {
        /* Default Situation with no AVL and no RS */
        if(pParams->EQNB_OperatingMode == LVM_EQNB_ON)
        {
            if(Volume > -pInstance->Headroom)
                Volume = (LVM_INT16)-pInstance->Headroom;
        }
    }

    /*
     * Activate volume control if necessary
     */
    pInstance->VC_Active   = LVM_TRUE;
    if (Volume != 0)
    {
        pInstance->VC_VolumedB = Volume;
    }
    else
    {
        pInstance->VC_VolumedB = 0;
    }

    /*
     * Calculate the required gain and shifts
     */
    dBOffset = (LVM_UINT16)((-Volume) % 6);             /* Get the dBs 0-5 */
    dBShifts = (LVM_UINT16)(Volume / -6);               /* Get the 6dB shifts */


    /*
     * Set the parameters
     */
    if(dBShifts == 0)
    {
        LVC_Mixer_SetTarget(&pInstance->VC_Volume.MixerStream[0],
                                (LVM_INT32)LVM_VolumeTable[dBOffset]);
    }
    else
    {
        LVC_Mixer_SetTarget(&pInstance->VC_Volume.MixerStream[0],
                                (((LVM_INT32)LVM_VolumeTable[dBOffset])>>dBShifts));
    }
    pInstance->VC_Volume.MixerStream[0].CallbackSet = 1;
    if(pInstance->NoSmoothVolume == LVM_TRUE)
    {
        LVC_Mixer_SetTimeConstant(&pInstance->VC_Volume.MixerStream[0],0,pInstance->Params.SampleRate,2);
    }
    else
    {
        LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_Volume.MixerStream[0],LVM_VC_MIXER_TIME,pInstance->Params.SampleRate,2);
    }
}


/************************************************************************************/
/*                                                                                  */
/* FUNCTION:            LVM_SetHeadroom                                             */
/*                                                                                  */
/* DESCRIPTION:                                                                     */
/*  Find suitable headroom based on EQ settings.                                    */
/*                                                                                  */
/* PARAMETERS:                                                                      */
/*  pInstance           Pointer to the instance                                     */
/*  pParams             Initialisation parameters                                   */
/*                                                                                  */
/* RETURNS:                                                                         */
/*  void                Nothing                                                     */
/*                                                                                  */
/* NOTES:                                                                           */
/*                                                                                  */
/************************************************************************************/
void    LVM_SetHeadroom(LVM_Instance_t         *pInstance,
                        LVM_ControlParams_t    *pParams)
{
    LVM_INT16   ii, jj;
    LVM_INT16   Headroom = 0;
    LVM_INT16   MaxGain = 0;


    if ((pParams->EQNB_OperatingMode == LVEQNB_ON) && (pInstance->HeadroomParams.Headroom_OperatingMode == LVM_HEADROOM_ON))
    {
        /* Find typical headroom value */
        for(jj = 0; jj < pInstance->HeadroomParams.NHeadroomBands; jj++)
        {
            MaxGain = 0;
            for( ii = 0; ii < pParams->EQNB_NBands; ii++)
            {
                if((pParams->pEQNB_BandDefinition[ii].Frequency >= pInstance->HeadroomParams.pHeadroomDefinition[jj].Limit_Low) &&
                   (pParams->pEQNB_BandDefinition[ii].Frequency <= pInstance->HeadroomParams.pHeadroomDefinition[jj].Limit_High))
                {
                    if(pParams->pEQNB_BandDefinition[ii].Gain > MaxGain)
                    {
                        MaxGain = pParams->pEQNB_BandDefinition[ii].Gain;
                    }
                }
            }

            if((MaxGain - pInstance->HeadroomParams.pHeadroomDefinition[jj].Headroom_Offset) > Headroom){
                Headroom = (LVM_INT16)(MaxGain - pInstance->HeadroomParams.pHeadroomDefinition[jj].Headroom_Offset);
            }
        }

        /* Saturate */
        if(Headroom < 0)
            Headroom = 0;
    }
    pInstance->Headroom = (LVM_UINT16)Headroom ;

}


/****************************************************************************************/
/*                                                                                      */
/* FUNCTION:                LVM_ApplyNewSettings                                        */
/*                                                                                      */
/* DESCRIPTION:                                                                         */
/*  Applies changes to parametres. This function makes no assumptions about what        */
/*  each module needs for initialisation and hence passes all parameters to all the     */
/*  the modules in turn.                                                                */
/*                                                                                      */
/*                                                                                      */
/* PARAMETERS:                                                                          */
/*  hInstance               Instance handle                                             */
/*                                                                                      */
/* RETURNS:                                                                             */
/*  LVM_Success             Succeeded                                                   */
/*                                                                                      */
/****************************************************************************************/

LVM_ReturnStatus_en LVM_ApplyNewSettings(LVM_Handle_t   hInstance)
{
    LVM_Instance_t         *pInstance =(LVM_Instance_t *)hInstance;
    LVM_ControlParams_t    LocalParams;
    LVM_INT16              Count = 5;


    /*
     * Copy the new parameters but make sure they didn't change while copying
     */
    do
    {
        pInstance->ControlPending = LVM_FALSE;
        LocalParams = pInstance->NewParams;
        pInstance->HeadroomParams = pInstance->NewHeadroomParams;
        Count--;
    } while ((pInstance->ControlPending != LVM_FALSE) &&
             (Count > 0));

    /* Clear all internal data if format change*/
    if(LocalParams.SourceFormat != pInstance->Params.SourceFormat)
    {
        LVM_ClearAudioBuffers(pInstance);
        pInstance->ControlPending = LVM_FALSE;
    }

    /*
     * Update the treble boost if required
     */
    if ((pInstance->Params.SampleRate != LocalParams.SampleRate) ||
        (pInstance->Params.TE_EffectLevel != LocalParams.TE_EffectLevel) ||
        (pInstance->Params.TE_OperatingMode != LocalParams.TE_OperatingMode) ||
        (pInstance->Params.OperatingMode != LocalParams.OperatingMode) ||
        (pInstance->Params.SpeakerType != LocalParams.SpeakerType))
    {
        LVM_SetTrebleBoost(pInstance,
                           &LocalParams);
    }

    /*
     * Update the headroom if required
     */
        LVM_SetHeadroom(pInstance,                      /* Instance pointer */
                        &LocalParams);                  /* New parameters */

    /*
     * Update the volume if required
     */
    {
        LVM_SetVolume(pInstance,                      /* Instance pointer */
                      &LocalParams);                  /* New parameters */
    }
    /* Apply balance changes*/
    if(pInstance->Params.VC_Balance != LocalParams.VC_Balance)
    {
        /* Configure Mixer module for gradual changes to volume*/
        if(LocalParams.VC_Balance < 0)
        {
            LVM_INT32 Target;
            /* Drop in right channel volume*/
            Target = LVM_MAXINT_16;
            LVC_Mixer_SetTarget(&pInstance->VC_BalanceMix.MixerStream[0],Target);
            LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_BalanceMix.MixerStream[0],LVM_VC_MIXER_TIME,LocalParams.SampleRate,1);

            Target = dB_to_Lin32((LVM_INT16)(LocalParams.VC_Balance<<4));
            LVC_Mixer_SetTarget(&pInstance->VC_BalanceMix.MixerStream[1],Target);
            LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_BalanceMix.MixerStream[1],LVM_VC_MIXER_TIME,LocalParams.SampleRate,1);
        }
        else if(LocalParams.VC_Balance >0)
        {
            LVM_INT32 Target;
            /* Drop in left channel volume*/
            Target = dB_to_Lin32((LVM_INT16)((-LocalParams.VC_Balance)<<4));
            LVC_Mixer_SetTarget(&pInstance->VC_BalanceMix.MixerStream[0],Target);
            LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_BalanceMix.MixerStream[0],LVM_VC_MIXER_TIME,LocalParams.SampleRate,1);

            Target = LVM_MAXINT_16;
            LVC_Mixer_SetTarget(&pInstance->VC_BalanceMix.MixerStream[1],Target);
            LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_BalanceMix.MixerStream[1],LVM_VC_MIXER_TIME,LocalParams.SampleRate,1);
        }
        else
        {
            LVM_INT32 Target;
            /* No drop*/
            Target = LVM_MAXINT_16;
            LVC_Mixer_SetTarget(&pInstance->VC_BalanceMix.MixerStream[0],Target);
            LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_BalanceMix.MixerStream[0],LVM_VC_MIXER_TIME,LocalParams.SampleRate,1);

            LVC_Mixer_SetTarget(&pInstance->VC_BalanceMix.MixerStream[1],Target);
            LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_BalanceMix.MixerStream[1],LVM_VC_MIXER_TIME,LocalParams.SampleRate,1);
        }
    }
    /*
     * Update the bass enhancement
     */
    {
        LVDBE_ReturnStatus_en       DBE_Status;
        LVDBE_Params_t              DBE_Params;
        LVDBE_Handle_t              *hDBEInstance = pInstance->hDBEInstance;


        /*
         * Set the new parameters
         */
        if(LocalParams.OperatingMode == LVM_MODE_OFF)
        {
            DBE_Params.OperatingMode = LVDBE_OFF;
        }
        else
        {
            DBE_Params.OperatingMode    = (LVDBE_Mode_en)LocalParams.BE_OperatingMode;
        }
        DBE_Params.SampleRate       = (LVDBE_Fs_en)LocalParams.SampleRate;
        DBE_Params.EffectLevel      = LocalParams.BE_EffectLevel;
        DBE_Params.CentreFrequency  = (LVDBE_CentreFreq_en)LocalParams.BE_CentreFreq;
        DBE_Params.HPFSelect        = (LVDBE_FilterSelect_en)LocalParams.BE_HPF;
        DBE_Params.HeadroomdB       = 0;
        DBE_Params.VolumeControl    = LVDBE_VOLUME_OFF;
        DBE_Params.VolumedB         = 0;

        /*
         * Make the changes
         */
        DBE_Status = LVDBE_Control(hDBEInstance,
                                   &DBE_Params);


        /*
         * Quit if the changes were not accepted
         */
        if (DBE_Status != LVDBE_SUCCESS)
        {
            return((LVM_ReturnStatus_en)DBE_Status);
        }


        /*
         * Set the control flag
         */
        pInstance->DBE_Active = LVM_TRUE;
    }

    /*
     * Update the N-Band Equaliser
     */
    {
        LVEQNB_ReturnStatus_en      EQNB_Status;
        LVEQNB_Params_t             EQNB_Params;
        LVEQNB_Handle_t             *hEQNBInstance = pInstance->hEQNBInstance;


        /*
         * Set the new parameters
         */

        if(LocalParams.OperatingMode == LVM_MODE_OFF)
        {
            EQNB_Params.OperatingMode    = LVEQNB_BYPASS;
        }
        else
        {
            EQNB_Params.OperatingMode    = (LVEQNB_Mode_en)LocalParams.EQNB_OperatingMode;
        }

        EQNB_Params.SampleRate       = (LVEQNB_Fs_en)LocalParams.SampleRate;
        EQNB_Params.NBands           = LocalParams.EQNB_NBands;
        EQNB_Params.pBandDefinition  = (LVEQNB_BandDef_t *)LocalParams.pEQNB_BandDefinition;
        if (LocalParams.SourceFormat == LVM_STEREO)    /* Mono format not supported */
        {
            EQNB_Params.SourceFormat = LVEQNB_STEREO;
        }
        else
        {
            EQNB_Params.SourceFormat = LVEQNB_MONOINSTEREO;     /* Force to Mono-in-Stereo mode */
        }


        /*
         * Set the control flag
         */
        if ((LocalParams.OperatingMode == LVM_MODE_ON) &&
            (LocalParams.EQNB_OperatingMode == LVM_EQNB_ON))
        {
            pInstance->EQNB_Active = LVM_TRUE;
        }
        else
        {
            EQNB_Params.OperatingMode = LVEQNB_BYPASS;
        }

        /*
         * Make the changes
         */
        EQNB_Status = LVEQNB_Control(hEQNBInstance,
                                     &EQNB_Params);


        /*
         * Quit if the changes were not accepted
         */
        if (EQNB_Status != LVEQNB_SUCCESS)
        {
            return((LVM_ReturnStatus_en)EQNB_Status);
        }

    }


    /*
     * Update concert sound
     */
    {
        LVCS_ReturnStatus_en        CS_Status;
        LVCS_Params_t               CS_Params;
        LVCS_Handle_t               *hCSInstance = pInstance->hCSInstance;
        LVM_Mode_en                 CompressorMode=LVM_MODE_ON;

        /*
         * Set the new parameters
         */
        if(LocalParams.VirtualizerOperatingMode == LVM_MODE_ON)
        {
            CS_Params.OperatingMode    = LVCS_ON;
        }
        else
        {
            CS_Params.OperatingMode    = LVCS_OFF;
        }

        if((LocalParams.TE_OperatingMode == LVM_TE_ON) && (LocalParams.TE_EffectLevel == LVM_TE_LOW_MIPS))
        {
            CS_Params.SpeakerType  = LVCS_EX_HEADPHONES;
        }
        else
        {
            CS_Params.SpeakerType  = LVCS_HEADPHONES;
        }

        if (LocalParams.SourceFormat == LVM_STEREO)    /* Mono format not supported */
        {
            CS_Params.SourceFormat = LVCS_STEREO;
        }
        else
        {
            CS_Params.SourceFormat = LVCS_MONOINSTEREO;          /* Force to Mono-in-Stereo mode */
        }
        CS_Params.SampleRate  = LocalParams.SampleRate;
        CS_Params.ReverbLevel = LocalParams.VirtualizerReverbLevel;
        CS_Params.EffectLevel = LocalParams.CS_EffectLevel;


        /*
         * Set the control flag
         */
        if ((LocalParams.OperatingMode == LVM_MODE_ON) &&
            (LocalParams.VirtualizerOperatingMode != LVCS_OFF))
        {
            pInstance->CS_Active = LVM_TRUE;
        }
        else
        {
            CS_Params.OperatingMode = LVCS_OFF;
        }

        CS_Params.CompressorMode=CompressorMode;

        /*
         * Make the changes
         */
        CS_Status = LVCS_Control(hCSInstance,
                                 &CS_Params);


        /*
         * Quit if the changes were not accepted
         */
        if (CS_Status != LVCS_SUCCESS)
        {
            return((LVM_ReturnStatus_en)CS_Status);
        }

    }

    /*
     * Update the Power Spectrum Analyser
     */
    {
        LVPSA_RETURN                PSA_Status;
        LVPSA_ControlParams_t       PSA_Params;
        pLVPSA_Handle_t             *hPSAInstance = pInstance->hPSAInstance;


        /*
         * Set the new parameters
         */
        PSA_Params.Fs = LocalParams.SampleRate;
        PSA_Params.LevelDetectionSpeed = (LVPSA_LevelDetectSpeed_en)LocalParams.PSA_PeakDecayRate;

        /*
         * Make the changes
         */
        if(pInstance->InstParams.PSA_Included==LVM_PSA_ON)
        {
            PSA_Status = LVPSA_Control(hPSAInstance,
                &PSA_Params);

            if (PSA_Status != LVPSA_OK)
            {
                return((LVM_ReturnStatus_en)PSA_Status);
            }

            /*
             * Apply new settings
             */
            PSA_Status = LVPSA_ApplyNewSettings ((LVPSA_InstancePr_t*)hPSAInstance);
            if(PSA_Status != LVPSA_OK)
            {
                return((LVM_ReturnStatus_en)PSA_Status);
            }
        }
    }

    /*
     * Update the parameters and clear the flag
     */
    pInstance->Params =  LocalParams;


    return(LVM_SUCCESS);
}


/****************************************************************************************/
/*                                                                                      */
/* FUNCTION:                LVM_SetHeadroomParams                                       */
/*                                                                                      */
/* DESCRIPTION:                                                                         */
/*  This function is used to set the automatiuc headroom management parameters.         */
/*                                                                                      */
/* PARAMETERS:                                                                          */
/*  hInstance               Instance Handle                                             */
/*  pHeadroomParams         Pointer to headroom parameter structure                     */
/*                                                                                      */
/* RETURNS:                                                                             */
/*  LVM_Success             Succeeded                                                   */
/*                                                                                      */
/* NOTES:                                                                               */
/*  1.  This function may be interrupted by the LVM_Process function                    */
/*                                                                                      */
/****************************************************************************************/

LVM_ReturnStatus_en LVM_SetHeadroomParams(LVM_Handle_t              hInstance,
                                          LVM_HeadroomParams_t      *pHeadroomParams)
{
    LVM_Instance_t      *pInstance =(LVM_Instance_t  *)hInstance;
    LVM_UINT16          ii, NBands;

    /* Check for NULL pointers */
    if ((hInstance == LVM_NULL) || (pHeadroomParams == LVM_NULL))
    {
        return (LVM_NULLADDRESS);
    }
    if ((pHeadroomParams->NHeadroomBands != 0) && (pHeadroomParams->pHeadroomDefinition == LVM_NULL))
    {
        return (LVM_NULLADDRESS);
    }

    /* Consider only the LVM_HEADROOM_MAX_NBANDS first bands*/
    if (pHeadroomParams->NHeadroomBands > LVM_HEADROOM_MAX_NBANDS)
    {
        NBands = LVM_HEADROOM_MAX_NBANDS;
    }
    else
    {
        NBands = pHeadroomParams->NHeadroomBands;
    }
    pInstance->NewHeadroomParams.NHeadroomBands = NBands;

    /* Copy settings in memory */
    for(ii = 0; ii < NBands; ii++)
    {
        pInstance->pHeadroom_BandDefs[ii] = pHeadroomParams->pHeadroomDefinition[ii];
    }

    pInstance->NewHeadroomParams.pHeadroomDefinition = pInstance->pHeadroom_BandDefs;
    pInstance->NewHeadroomParams.Headroom_OperatingMode = pHeadroomParams->Headroom_OperatingMode;
    pInstance->ControlPending = LVM_TRUE;

    return(LVM_SUCCESS);
}

/****************************************************************************************/
/*                                                                                      */
/* FUNCTION:                LVM_GetHeadroomParams                                       */
/*                                                                                      */
/* DESCRIPTION:                                                                         */
/*  This function is used to get the automatic headroom management parameters.          */
/*                                                                                      */
/* PARAMETERS:                                                                          */
/*  hInstance               Instance Handle                                             */
/*  pHeadroomParams         Pointer to headroom parameter structure (output)            */
/*                                                                                      */
/* RETURNS:                                                                             */
/*  LVM_SUCCESS             Succeeded                                                   */
/*  LVM_NULLADDRESS         When hInstance or pHeadroomParams are NULL                  */
/*                                                                                      */
/* NOTES:                                                                               */
/*  1.  This function may be interrupted by the LVM_Process function                    */
/*                                                                                      */
/****************************************************************************************/

LVM_ReturnStatus_en LVM_GetHeadroomParams(LVM_Handle_t          hInstance,
                                          LVM_HeadroomParams_t  *pHeadroomParams)
{
    LVM_Instance_t      *pInstance =(LVM_Instance_t  *)hInstance;
    LVM_UINT16          ii;

    /* Check for NULL pointers */
    if ((hInstance == LVM_NULL) || (pHeadroomParams == LVM_NULL))
    {
        return (LVM_NULLADDRESS);
    }

    pHeadroomParams->NHeadroomBands = pInstance->NewHeadroomParams.NHeadroomBands;


    /* Copy settings in memory */
    for(ii = 0; ii < pInstance->NewHeadroomParams.NHeadroomBands; ii++)
    {
        pInstance->pHeadroom_UserDefs[ii] = pInstance->pHeadroom_BandDefs[ii];
    }


    pHeadroomParams->pHeadroomDefinition = pInstance->pHeadroom_UserDefs;
    pHeadroomParams->Headroom_OperatingMode = pInstance->NewHeadroomParams.Headroom_OperatingMode;
    return(LVM_SUCCESS);
}

/****************************************************************************************/
/*                                                                                      */
/* FUNCTION:                LVM_AlgoCallBack                                            */
/*                                                                                      */
/* DESCRIPTION:                                                                         */
/*  This is the callback function of the algorithm.                                     */
/*                                                                                      */
/* PARAMETERS:                                                                          */
/*  pBundleHandle           Pointer to the Instance Handle                              */
/*  pData                   Pointer to the data                                         */
/*  callbackId              ID of the callback                                          */
/*                                                                                      */
/* NOTES:                                                                               */
/*  1.  This function may be interrupted by the LVM_Process function                    */
/*                                                                                      */
/****************************************************************************************/
LVM_INT32 LVM_AlgoCallBack( void          *pBundleHandle,
                            void          *pData,
                            LVM_INT16     callbackId)
{
    LVM_Instance_t      *pInstance =(LVM_Instance_t  *)pBundleHandle;

    (void) pData;

    switch(callbackId & 0xFF00){
        case ALGORITHM_CS_ID:
            switch(callbackId & 0x00FF)
            {
                case LVCS_EVENT_ALGOFF:
                    pInstance->CS_Active = LVM_FALSE;
                    break;
                default:
                    break;
            }
            break;
        case ALGORITHM_EQNB_ID:
            switch(callbackId & 0x00FF)
            {
                case LVEQNB_EVENT_ALGOFF:
                    pInstance->EQNB_Active = LVM_FALSE;
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }

    return 0;
}

/****************************************************************************************/
/*                                                                                      */
/* FUNCTION:                LVM_VCCallBack                                              */
/*                                                                                      */
/* DESCRIPTION:                                                                         */
/*  This is the callback function of the Volume control.                                */
/*                                                                                      */
/* PARAMETERS:                                                                          */
/*  pBundleHandle           Pointer to the Instance Handle                              */
/*  pGeneralPurpose         Pointer to the data                                         */
/*  CallBackParam           ID of the callback                                          */
/*                                                                                      */
/* NOTES:                                                                               */
/*  1.  This function may be interrupted by the LVM_Process function                    */
/*                                                                                      */
/****************************************************************************************/
LVM_INT32    LVM_VCCallBack(void*   pBundleHandle,
                            void*   pGeneralPurpose,
                            short   CallBackParam)
{
    LVM_Instance_t *pInstance =(LVM_Instance_t  *)pBundleHandle;
    LVM_INT32    Target;

    (void) pGeneralPurpose;
    (void) CallBackParam;

    /* When volume mixer has reached 0 dB target then stop it to avoid
       unnecessary processing. */
    Target = LVC_Mixer_GetTarget(&pInstance->VC_Volume.MixerStream[0]);

    if(Target == 0x7FFF)
    {
        pInstance->VC_Active = LVM_FALSE;
    }
    return 1;
}