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_VarSlope_SetTimeConstant                                  */
25 /*                                                                      */
26 /* DESCRIPTION:                                                         */
27 /*  This function calculates the step change for fractional gain for a  */
28 /*  given time constant, sample rate and num channels                   */
29 /*  Delta=(2147483647*4*1000)/(NumChannels*SampleRate*Tc_millisec)      */
30 /*  in Q 0.31 format                                                    */
31 /*                                                                      */
32 /* PARAMETERS:                                                          */
33 /*  pStream     - ptr to Instance Parameter Structure LVMixer3_st for an*/
34 /*                Audio Stream                                          */
35 /*  Tc_millisec - TimeConstant i.e time required in milli second to     */
36 /*                go from linear fractional gain of 0 to 0.99999999     */
37 /*  Fs          - LVM_Fs_en enumerator for Sampling Frequency           */
38 /*  NumChannels - Number of channels in Audio Stream 1=Mono, 2=Stereo   */
39 /*                                                                      */
40 /* UPDATES:                                                             */
41 /*  Delta       - the step change for fractional gain per 4 samples     */
42 /*                in Q0.31 format for a given Time Constant,            */
43 /*                Sample Rate and NumChannels                           */
44 /* RETURNS:                                                             */
45 /*  void                                                                */
46 /************************************************************************/
LVC_Mixer_VarSlope_SetTimeConstant(LVMixer3_FLOAT_st * pStream,LVM_INT32 Tc_millisec,LVM_Fs_en Fs,LVM_INT16 NumChannels)47 void LVC_Mixer_VarSlope_SetTimeConstant(LVMixer3_FLOAT_st* pStream, LVM_INT32 Tc_millisec,
48                                         LVM_Fs_en Fs, LVM_INT16 NumChannels) {
49     LVM_FLOAT DeltaTable[13] = {0.500000f,  /*8000*/
50                                 0.362812f,  /*11025*/
51                                 0.333333f,  /*12000*/
52                                 0.250000f,  /*16000*/
53                                 0.181406f,  /*22050*/
54                                 0.166666f,  /*24000*/
55                                 0.125000f,  /*32000*/
56                                 0.090703f,  /*44100*/
57                                 0.083333f,  /*48000*/
58                                 0.045352f,  /*88200*/
59                                 0.041666f,  /*96000*/
60                                 0.022676f,  /*176400*/
61                                 0.020833f}; /*192000*/
62     LVM_FLOAT Tc_millisec_float;
63     Mix_Private_FLOAT_st* pInstance = (Mix_Private_FLOAT_st*)pStream->PrivateParams;
64     LVM_FLOAT Delta = DeltaTable[Fs];
65 
66     LVM_FLOAT Current;
67     LVM_FLOAT Target;
68 
69     Delta = Delta / (NumChannels);
70 
71     /*  Get gain values  */
72     Current = pInstance->Current;
73     Target = pInstance->Target;
74 
75     if (Current != Target) {
76         Tc_millisec_float = (LVM_FLOAT)(Tc_millisec) / (Current - Target);
77         if (Tc_millisec_float < 0) Tc_millisec_float = -Tc_millisec_float;
78 
79         if (Tc_millisec == 0)
80             Delta = 1.000000f;
81         else
82             Delta = Delta / Tc_millisec_float;
83 
84         if (Delta == 0)
85             Delta = 0.0000000005f; /* If Time Constant is so large that Delta is 0, \
86                                       assign minimum value to Delta */
87     } else {
88         Delta = 0.0000000005f; /* Minimum value for proper call-backs \
89                             (setting it to zero has some problems, to be corrected) */
90     }
91 
92     pInstance->Delta = Delta;  // Delta=(2147483647*4*1000)/(NumChannels*SampleRate*Tc_millisec)
93 }
94