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_Init                                                      */
25 /*                                                                      */
26 /* DESCRIPTION:                                                         */
27 /*  This intialization function intializes the private instance         */
28 /*  paramters for a given Audio Stream based on TargetGain and          */
29 /*  CurrentGain                                                         */
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 for an*/
39 /*                Audio Stream                                          */
40 /*  TargetGain  - TargetGain value in Q 16.15 format                    */
41 /*  CurrentGain - CurrentGain value in Q 16.15 format                   */
42 /*                                                                      */
43 /* RETURNS:                                                             */
44 /*  void                                                                */
45 /*                                                                      */
46 /************************************************************************/
47 
LVC_Mixer_Init(LVMixer3_st * pStream,LVM_INT32 TargetGain,LVM_INT32 CurrentGain)48 void LVC_Mixer_Init( LVMixer3_st *pStream,
49                     LVM_INT32           TargetGain,
50                     LVM_INT32           CurrentGain)
51 {
52     LVM_INT16       Shift=0;
53     LVM_INT32       MaxGain=TargetGain;         // MaxGain is in Q16.15 format
54     Mix_Private_st  *pInstance=(Mix_Private_st *)pStream->PrivateParams;
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 
68