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/************************************************************************/
24/* FUNCTION:                                                            */
25/*   LVMixer3_VarSlope_SetTimeConstant                                  */
26/*                                                                      */
27/* DESCRIPTION:                                                         */
28/*  This function calculates the step change for fractional gain for a  */
29/*  given time constant, sample rate and num channels                   */
30/*  Delta=(2147483647*4*1000)/(NumChannels*SampleRate*Tc_millisec)      */
31/*  in Q 0.31 format                                                    */
32/*                                                                      */
33/* PARAMETERS:                                                          */
34/*  pStream     - ptr to Instance Parameter Structure LVMixer3_st for an*/
35/*                Audio Stream                                          */
36/*  Tc_millisec - TimeConstant i.e time required in milli second to     */
37/*                go from linear fractional gain of 0 to 0.99999999     */
38/*  Fs          - LVM_Fs_en enumerator for Sampling Frequency           */
39/*  NumChannels - Number of channels in Audio Stream 1=Mono, 2=Stereo   */
40/*                                                                      */
41/* UPDATES:                                                             */
42/*  Delta       - the step change for fractional gain per 4 samples     */
43/*                in Q0.31 format for a given Time Constant,            */
44/*                Sample Rate and NumChannels                           */
45/* RETURNS:                                                             */
46/*  void                                                                */
47/************************************************************************/
48#ifdef BUILD_FLOAT
49void LVC_Mixer_VarSlope_SetTimeConstant( LVMixer3_FLOAT_st *pStream,
50                                         LVM_INT32           Tc_millisec,
51                                         LVM_Fs_en           Fs,
52                                         LVM_INT16           NumChannels)
53{
54#ifdef HIGHER_FS
55     LVM_FLOAT   DeltaTable[11] = {0.500000f,/*8000*/
56                                   0.362812f,/*11025*/
57                                   0.333333f,/*12000*/
58                                   0.250000f,/*16000*/
59                                   0.181406f,/*22050*/
60                                   0.166666f,/*24000*/
61                                   0.125000f,/*32000*/
62                                   0.090703f,/*44100*/
63                                   0.083333f,/*48000*/
64                                   0.041666f,/*96000*/
65                                   0.020833f};/*192000*/
66#else
67    LVM_FLOAT   DeltaTable[9] = {0.500000f,/*8000*/
68                                 0.362812f,/*11025*/
69                                 0.333333f,/*12000*/
70                                 0.250000f,/*16000*/
71                                 0.181406f,/*22050*/
72                                 0.166666f,/*24000*/
73                                 0.125000f,/*32000*/
74                                 0.090703f,/*44100*/
75                                 0.083333f};/*48000*/
76#endif
77    LVM_FLOAT Tc_millisec_float;
78    Mix_Private_FLOAT_st *pInstance = (Mix_Private_FLOAT_st *)pStream->PrivateParams;
79    LVM_FLOAT Delta = DeltaTable[Fs];
80
81    LVM_FLOAT   Current;
82    LVM_FLOAT   Target;
83
84    Delta=Delta / (NumChannels);
85
86    /*  Get gain values  */
87    Current = pInstance->Current;
88    Target = pInstance->Target;
89
90    if (Current != Target)
91    {
92        Tc_millisec_float = (LVM_FLOAT)(Tc_millisec) / (Current - Target);
93        if (Tc_millisec_float < 0)
94            Tc_millisec_float = -Tc_millisec_float;
95
96        if(Tc_millisec == 0)
97            Delta = 1.000000f;
98        else
99            Delta = Delta / Tc_millisec_float;
100
101        if(Delta == 0)
102            Delta = 0.0000000005f; /* If Time Constant is so large that Delta is 0, \
103                                      assign minimum value to Delta */
104    }
105    else
106    {
107        Delta = 0.0000000005f;  /* Minimum value for proper call-backs \
108                             (setting it to zero has some problems, to be corrected) */
109    }
110
111    pInstance->Delta = Delta;     // Delta=(2147483647*4*1000)/(NumChannels*SampleRate*Tc_millisec)
112}
113#else
114void LVC_Mixer_VarSlope_SetTimeConstant( LVMixer3_st *pStream,
115                                        LVM_INT32           Tc_millisec,
116                                        LVM_Fs_en           Fs,
117                                        LVM_INT16           NumChannels)
118{
119    LVM_INT32   DeltaTable[9]={1073741824,
120                               779132389,
121                               715827882,
122                               536870912,
123                               389566194,
124                               357913941,
125                               268435456,
126                               194783097,
127                               178956971};
128    Mix_Private_st  *pInstance=(Mix_Private_st *)pStream->PrivateParams;
129    LVM_INT32   Delta=DeltaTable[Fs];
130
131    LVM_INT32   Current;
132    LVM_INT32   Target;
133
134    Delta=Delta>>(NumChannels-1);
135
136    /*  Get gain values  */
137    Current = LVC_Mixer_GetCurrent( pStream );
138    Target = LVC_Mixer_GetTarget( pStream );
139
140    if (Current != Target)
141    {
142        Tc_millisec = Tc_millisec * 32767 / (Current - Target);
143        if (Tc_millisec<0) Tc_millisec = -Tc_millisec;
144
145        if(Tc_millisec==0)
146            Delta=0x7FFFFFFF;
147        else
148            Delta=Delta/Tc_millisec;
149
150        if(Delta==0)
151            Delta=1;            // If Time Constant is so large that Delta is 0, assign minimum value to Delta
152    }
153    else
154    {
155        Delta =1;               // Minimum value for proper call-backs (setting it to zero has some problems, to be corrected)
156    }
157
158
159    pInstance->Delta=Delta;     // Delta=(2147483647*4*1000)/(NumChannels*SampleRate*Tc_millisec) in Q 0.31 format
160}
161#endif
162