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 /************************************************************************/ 25 /* FUNCTION: */ 26 /* LVM_Mix_GetTimeConstant */ 27 /* */ 28 /* DESCRIPTION: */ 29 /* This function calculates the filter coefficient using the following */ 30 /* equation: */ 31 /* Alpha = exp(ln(0.1)/ (tc * Update + 1.0)) */ 32 /* */ 33 /* This is to be used with the follow first order filter, called at a */ 34 /* rate of Update times a second. tc is the required time constant in */ 35 /* units of 100us. */ 36 /* */ 37 /* Output(n) = Alpha * Output(n-1) + (1 - Alpha) * Target(n) */ 38 /* */ 39 /* The function assumes the block size is large, i.e. the update rate */ 40 /* is approximately a fixed, and correct factor of the value of Fs */ 41 /* given in the call. This is especially not true when the block size */ 42 /* is very small, see the mixer documentation for further details. */ 43 /* */ 44 /* The function does not support all possible combinations of input */ 45 /* values: */ 46 /* */ 47 /* 1. NumChannels is limited to the values 1 (Mono) and 2 (Stereo) */ 48 /* 2. The product tc * Fs is limited approximately to the range */ 49 /* 8 < (tc * Fs) < 2^35 */ 50 /* */ 51 /* PARAMETERS: */ 52 /* tc - the time constant in 100us steps, i.e. 10 = 1ms */ 53 /* Fs - the filter update rate in Hz */ 54 /* NumChannels - Number of channels 1=Mono, 2=Stereo */ 55 /* */ 56 /* RETURNS: */ 57 /* Alpha - the filter coefficient Q31 format */ 58 /* */ 59 /************************************************************************/ 60 61 LVM_UINT32 LVM_Mixer_TimeConstant(LVM_UINT32 tc, 62 LVM_UINT16 Fs, 63 LVM_UINT16 NumChannels) 64 { 65 66 LVM_UINT32 Product; 67 LVM_INT16 Interpolate; 68 LVM_UINT16 Shift; 69 LVM_INT32 Diff; 70 LVM_UINT32 Table[] = {ALPHA_0, /* Log spaced look-up table */ 71 ALPHA_1, 72 ALPHA_2, 73 ALPHA_3, 74 ALPHA_4, 75 ALPHA_5, 76 ALPHA_6, 77 ALPHA_7, 78 ALPHA_8, 79 ALPHA_9, 80 ALPHA_10, 81 ALPHA_11, 82 ALPHA_12, 83 ALPHA_13, 84 ALPHA_14, 85 ALPHA_15, 86 ALPHA_16, 87 ALPHA_17, 88 ALPHA_18, 89 ALPHA_19, 90 ALPHA_20, 91 ALPHA_21, 92 ALPHA_22, 93 ALPHA_23, 94 ALPHA_24, 95 ALPHA_25, 96 ALPHA_26, 97 ALPHA_27, 98 ALPHA_28, 99 ALPHA_29, 100 ALPHA_30, 101 ALPHA_31, 102 ALPHA_32, 103 ALPHA_33, 104 ALPHA_34, 105 ALPHA_35, 106 ALPHA_36, 107 ALPHA_37, 108 ALPHA_38, 109 ALPHA_39, 110 ALPHA_40, 111 ALPHA_41, 112 ALPHA_42, 113 ALPHA_43, 114 ALPHA_44, 115 ALPHA_45, 116 ALPHA_46, 117 ALPHA_47, 118 ALPHA_48, 119 ALPHA_49, 120 ALPHA_50}; 121 122 123 /* Calculate the product of the time constant and the sample rate */ 124 Product = ((tc >> 16) * (LVM_UINT32)Fs) << 13; /* Stereo value */ 125 Product = Product + (((tc & 0x0000FFFF) * (LVM_UINT32)Fs) >> 3); 126 127 if (NumChannels == 1) 128 { 129 Product = Product >> 1; /* Mono value */ 130 } 131 132 /* Normalize to get the table index and interpolation factor */ 133 for (Shift=0; Shift<((Alpha_TableSize-1)/2); Shift++) 134 { 135 if ((Product & 0x80000000)!=0) 136 { 137 break; 138 } 139 140 Product = Product << 1; 141 } 142 Shift = (LVM_UINT16)((Shift << 1)); 143 144 if ((Product & 0x40000000)==0) 145 { 146 Shift++; 147 } 148 149 Interpolate = (LVM_INT16)((Product >> 15) & 0x00007FFF); 150 151 Diff = (LVM_INT32)(Table[Shift] - Table[Shift+1]); 152 MUL32x16INTO32(Diff,Interpolate,Diff,15) 153 Product = Table[Shift+1] + (LVM_UINT32)Diff; 154 155 return Product; 156 } 157