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 "Mixer.h"
21 #include "LVM_Mixer_FilterCoeffs.h"
22
23 /************************************************************************/
24 /* FUNCTION: */
25 /* LVM_Mix_GetTimeConstant */
26 /* */
27 /* DESCRIPTION: */
28 /* This function calculates the filter coefficient using the following */
29 /* equation: */
30 /* Alpha = exp(ln(0.1)/ (tc * Update + 1.0)) */
31 /* */
32 /* This is to be used with the follow first order filter, called at a */
33 /* rate of Update times a second. tc is the required time constant in */
34 /* units of 100us. */
35 /* */
36 /* Output(n) = Alpha * Output(n-1) + (1 - Alpha) * Target(n) */
37 /* */
38 /* The function assumes the block size is large, i.e. the update rate */
39 /* is approximately a fixed, and correct factor of the value of Fs */
40 /* given in the call. This is especially not true when the block size */
41 /* is very small, see the mixer documentation for further details. */
42 /* */
43 /* The function does not support all possible combinations of input */
44 /* values: */
45 /* */
46 /* 1. NumChannels is limited to the values 1 (Mono) and 2 (Stereo) */
47 /* 2. The product tc * Fs is limited approximately to the range */
48 /* 8 < (tc * Fs) < 2^35 */
49 /* */
50 /* PARAMETERS: */
51 /* tc - the time constant in 100us steps, i.e. 10 = 1ms */
52 /* Fs - the filter update rate in Hz */
53 /* NumChannels - Number of channels 1=Mono, 2=Stereo */
54 /* */
55 /* RETURNS: */
56 /* Alpha - the filter coefficient Q31 format */
57 /* */
58 /************************************************************************/
LVM_Mixer_TimeConstant(LVM_UINT32 tc,LVM_UINT32 Fs,LVM_UINT16 NumChannels)59 LVM_FLOAT LVM_Mixer_TimeConstant(LVM_UINT32 tc,
60 LVM_UINT32 Fs,
61 LVM_UINT16 NumChannels)
62 {
63
64 LVM_UINT32 Product;
65 LVM_FLOAT ProductFloat;
66 LVM_INT16 InterpolateShort;
67 LVM_FLOAT Interpolate;
68 LVM_UINT16 Shift;
69 LVM_FLOAT Diff;
70 LVM_FLOAT Table[] = {ALPHA_Float_0, /* Log spaced look-up table */
71 ALPHA_Float_1,
72 ALPHA_Float_2,
73 ALPHA_Float_3,
74 ALPHA_Float_4,
75 ALPHA_Float_5,
76 ALPHA_Float_6,
77 ALPHA_Float_7,
78 ALPHA_Float_8,
79 ALPHA_Float_9,
80 ALPHA_Float_10,
81 ALPHA_Float_11,
82 ALPHA_Float_12,
83 ALPHA_Float_13,
84 ALPHA_Float_14,
85 ALPHA_Float_15,
86 ALPHA_Float_16,
87 ALPHA_Float_17,
88 ALPHA_Float_18,
89 ALPHA_Float_19,
90 ALPHA_Float_20,
91 ALPHA_Float_21,
92 ALPHA_Float_22,
93 ALPHA_Float_23,
94 ALPHA_Float_24,
95 ALPHA_Float_25,
96 ALPHA_Float_26,
97 ALPHA_Float_27,
98 ALPHA_Float_28,
99 ALPHA_Float_29,
100 ALPHA_Float_30,
101 ALPHA_Float_31,
102 ALPHA_Float_32,
103 ALPHA_Float_33,
104 ALPHA_Float_34,
105 ALPHA_Float_35,
106 ALPHA_Float_36,
107 ALPHA_Float_37,
108 ALPHA_Float_38,
109 ALPHA_Float_39,
110 ALPHA_Float_40,
111 ALPHA_Float_41,
112 ALPHA_Float_42,
113 ALPHA_Float_43,
114 ALPHA_Float_44,
115 ALPHA_Float_45,
116 ALPHA_Float_46,
117 ALPHA_Float_47,
118 ALPHA_Float_48,
119 ALPHA_Float_49,
120 ALPHA_Float_50};
121
122 /* Calculate the product of the time constant and the sample rate */
123 Product = ((tc >> 16) * (LVM_UINT32)Fs) << 13; /* Stereo value */
124 Product = Product + (((tc & 0x0000FFFF) * (LVM_UINT32)Fs) >> 3);
125
126 if (NumChannels == 1)
127 {
128 Product = Product >> 1; /* Mono value */
129 }
130
131 /* Normalize to get the table index and interpolation factor */
132 for (Shift = 0; Shift < ((Alpha_TableSize - 1) / 2); Shift++)
133 {
134 if ((Product & 0x80000000) != 0)
135 {
136 break;
137 }
138
139 Product = Product << 1;
140 }
141 Shift = (LVM_UINT16)((Shift << 1));
142
143 if ((Product & 0x40000000)==0)
144 {
145 Shift++;
146 }
147
148 InterpolateShort = (LVM_INT16)((Product >> 15) & 0x00007FFF);
149 Interpolate = (LVM_FLOAT)InterpolateShort / 32768.0f;
150
151 Diff = (Table[Shift] - Table[Shift + 1]);
152 Diff = Diff * Interpolate;
153 ProductFloat = Table[Shift + 1] + Diff;
154
155 return ProductFloat;
156 }
157