Home | History | Annotate | Download | only in src
      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 #ifdef BUILD_FLOAT
     47 void LVC_Mixer_SetTarget(LVMixer3_FLOAT_st *pStream,
     48                          LVM_FLOAT         TargetGain)
     49 {
     50     Mix_Private_FLOAT_st *pInstance = (Mix_Private_FLOAT_st *)pStream->PrivateParams;
     51     pInstance->Target = TargetGain;               // Update gain Target
     52 }
     53 #else
     54 void LVC_Mixer_SetTarget(LVMixer3_st *pStream,
     55                         LVM_INT32           TargetGain)
     56 {
     57     LVM_INT32       Shift=0;
     58     LVM_INT32       CurrentGain;
     59     LVM_INT32       MaxGain=TargetGain;                     // MaxGain is in Q16.15 format
     60     Mix_Private_st  *pInstance=(Mix_Private_st *)pStream->PrivateParams;
     61     CurrentGain=pInstance->Current>>(16-pInstance->Shift);  // CurrentGain in Q16.15 format
     62     if(CurrentGain>MaxGain)
     63         MaxGain=CurrentGain;                                // MaxGain=max(CurrentGain,TargetGain)
     64 
     65     MaxGain=MaxGain>>15;                                    // MaxGain in Q31.0 format i.e Integer part only
     66     while(MaxGain>0){                                       // Update Shift required to provide integer gain
     67         Shift++;
     68         MaxGain=MaxGain>>1;
     69     }
     70     pInstance->Target=TargetGain<<(16-Shift);               // Update fractional gain Target
     71     pInstance->Current=CurrentGain<<(16-Shift);             // Update fractional gain Current
     72     pInstance->Shift=Shift;                                 // Update Shift
     73 }
     74 #endif
     75