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
49void LVC_Mixer_VarSlope_SetTimeConstant( LVMixer3_st *pStream,
50                                        LVM_INT32           Tc_millisec,
51                                        LVM_Fs_en           Fs,
52                                        LVM_INT16           NumChannels)
53{
54    LVM_INT32   DeltaTable[9]={1073741824,
55                               779132389,
56                               715827882,
57                               536870912,
58                               389566194,
59                               357913941,
60                               268435456,
61                               194783097,
62                               178956971};
63    Mix_Private_st  *pInstance=(Mix_Private_st *)pStream->PrivateParams;
64    LVM_INT32   Delta=DeltaTable[Fs];
65
66    LVM_INT32   Current;
67    LVM_INT32   Target;
68
69    Delta=Delta>>(NumChannels-1);
70
71    /*  Get gain values  */
72    Current = LVC_Mixer_GetCurrent( pStream );
73    Target = LVC_Mixer_GetTarget( pStream );
74
75    if (Current != Target)
76    {
77        Tc_millisec = Tc_millisec * 32767 / (Current - Target);
78        if (Tc_millisec<0) Tc_millisec = -Tc_millisec;
79
80        if(Tc_millisec==0)
81            Delta=0x7FFFFFFF;
82        else
83            Delta=Delta/Tc_millisec;
84
85        if(Delta==0)
86            Delta=1;            // If Time Constant is so large that Delta is 0, assign minimum value to Delta
87    }
88    else
89    {
90        Delta =1;               // Minimum value for proper call-backs (setting it to zero has some problems, to be corrected)
91    }
92
93
94    pInstance->Delta=Delta;     // Delta=(2147483647*4*1000)/(NumChannels*SampleRate*Tc_millisec) in Q 0.31 format
95}
96