1 /*
2  * Copyright (C) 2004-2010 NXP Software
3  * Copyright (C) 2010 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 #include "LVM_Types.h"
19 #include "LVM_Macros.h"
20 #include "LVC_Mixer_Private.h"
21 
22 /************************************************************************/
23 /* FUNCTION:                                                            */
24 /*   LVMixer3_SetTarget                                                 */
25 /*                                                                      */
26 /* DESCRIPTION:                                                         */
27 /*  This function updates the private instance parameters: Shift,Target,*/
28 /*  Current for a given Audio Stream based on new value of TargetGain   */
29 /*                                                                      */
30 /*  This function caclulates the "Shift" required to provide the        */
31 /*  integer part of TargetGain and fractional gain values "Target" and  */
32 /*  "Current" based on maximum(TargetGain,CurrentGain)                  */
33 /*  E.g. CurrentGain=1.9 and TargetGain=2.5 then based on               */
34 /*  MaxGain of 2.5, Shift = 2, Current=1.9/4=0.475, Target=2.5/4=0.625  */
35 /*  Therefore integer gain of 4 is provided by Left Shift of 2 and      */
36 /*  fraction gain is provided through Current=0.475 and Target=0.625    */
37 /* PARAMETERS:                                                          */
38 /*  pStream         - ptr to Instance Parameter Structure LVMixer3_st   */
39 /*                    for an Audio Stream                               */
40 /*  TargetGain      - TargetGain value in Q 16.15 format                */
41 /*                                                                      */
42 /* RETURNS:                                                             */
43 /*  void                                                                */
44 /*                                                                      */
45 /************************************************************************/
46 
LVC_Mixer_SetTarget(LVMixer3_st * pStream,LVM_INT32 TargetGain)47 void LVC_Mixer_SetTarget(LVMixer3_st *pStream,
48                         LVM_INT32           TargetGain)
49 {
50     LVM_INT32       Shift=0;
51     LVM_INT32       CurrentGain;
52     LVM_INT32       MaxGain=TargetGain;                     // MaxGain is in Q16.15 format
53     Mix_Private_st  *pInstance=(Mix_Private_st *)pStream->PrivateParams;
54     CurrentGain=pInstance->Current>>(16-pInstance->Shift);  // CurrentGain in Q16.15 format
55     if(CurrentGain>MaxGain)
56         MaxGain=CurrentGain;                                // MaxGain=max(CurrentGain,TargetGain)
57 
58     MaxGain=MaxGain>>15;                                    // MaxGain in Q31.0 format i.e Integer part only
59     while(MaxGain>0){                                       // Update Shift required to provide integer gain
60         Shift++;
61         MaxGain=MaxGain>>1;
62     }
63     pInstance->Target=TargetGain<<(16-Shift);               // Update fractional gain Target
64     pInstance->Current=CurrentGain<<(16-Shift);             // Update fractional gain Current
65     pInstance->Shift=Shift;                                 // Update Shift
66 }
67