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 
19 /************************************************************************************/
20 /*                                                                                  */
21 /*    Includes                                                                      */
22 /*                                                                                  */
23 /************************************************************************************/
24 
25 #include "LVDBE.h"
26 #include "LVDBE_Coeffs.h"               /* Filter coefficients */
27 #include "BIQUAD.h"
28 
29 
30 /************************************************************************************/
31 /*                                                                                  */
32 /*    Coefficients constant table                                                   */
33 /*                                                                                  */
34 /************************************************************************************/
35 
36 /*
37  * High Pass Filter Coefficient table
38  */
39 const BQ_C32_Coefs_t LVDBE_HPF_Table[] = {
40     /* Coefficients for 55Hz centre frequency */
41     {HPF_Fs8000_Fc55_A2,                /* 8kS/s coefficients */
42      HPF_Fs8000_Fc55_A1,
43      HPF_Fs8000_Fc55_A0,
44      -HPF_Fs8000_Fc55_B2,
45      -HPF_Fs8000_Fc55_B1},
46     {HPF_Fs11025_Fc55_A2,                /* 11kS/s coefficients */
47      HPF_Fs11025_Fc55_A1,
48      HPF_Fs11025_Fc55_A0,
49      -HPF_Fs11025_Fc55_B2,
50      -HPF_Fs11025_Fc55_B1},
51     {HPF_Fs12000_Fc55_A2,                /* 12kS/s coefficients */
52      HPF_Fs12000_Fc55_A1,
53      HPF_Fs12000_Fc55_A0,
54      -HPF_Fs12000_Fc55_B2,
55      -HPF_Fs12000_Fc55_B1},
56     {HPF_Fs16000_Fc55_A2,                /* 16kS/s coefficients */
57      HPF_Fs16000_Fc55_A1,
58      HPF_Fs16000_Fc55_A0,
59      -HPF_Fs16000_Fc55_B2,
60      -HPF_Fs16000_Fc55_B1},
61     {HPF_Fs22050_Fc55_A2,                /* 22kS/s coefficients */
62      HPF_Fs22050_Fc55_A1,
63      HPF_Fs22050_Fc55_A0,
64      -HPF_Fs22050_Fc55_B2,
65      -HPF_Fs22050_Fc55_B1},
66     {HPF_Fs24000_Fc55_A2,                /* 24kS/s coefficients */
67      HPF_Fs24000_Fc55_A1,
68      HPF_Fs24000_Fc55_A0,
69      -HPF_Fs24000_Fc55_B2,
70      -HPF_Fs24000_Fc55_B1},
71     {HPF_Fs32000_Fc55_A2,                /* 32kS/s coefficients */
72      HPF_Fs32000_Fc55_A1,
73      HPF_Fs32000_Fc55_A0,
74      -HPF_Fs32000_Fc55_B2,
75      -HPF_Fs32000_Fc55_B1},
76     {HPF_Fs44100_Fc55_A2,                /* 44kS/s coefficients */
77      HPF_Fs44100_Fc55_A1,
78      HPF_Fs44100_Fc55_A0,
79      -HPF_Fs44100_Fc55_B2,
80      -HPF_Fs44100_Fc55_B1},
81     {HPF_Fs48000_Fc55_A2,                /* 48kS/s coefficients */
82      HPF_Fs48000_Fc55_A1,
83      HPF_Fs48000_Fc55_A0,
84      -HPF_Fs48000_Fc55_B2,
85      -HPF_Fs48000_Fc55_B1},
86 
87     /* Coefficients for 66Hz centre frequency */
88     {HPF_Fs8000_Fc66_A2,                /* 8kS/s coefficients */
89      HPF_Fs8000_Fc66_A1,
90      HPF_Fs8000_Fc66_A0,
91      -HPF_Fs8000_Fc66_B2,
92      -HPF_Fs8000_Fc66_B1},
93     {HPF_Fs11025_Fc66_A2,                /* 11kS/s coefficients */
94      HPF_Fs11025_Fc66_A1,
95      HPF_Fs11025_Fc66_A0,
96      -HPF_Fs11025_Fc66_B2,
97      -HPF_Fs11025_Fc66_B1},
98     {HPF_Fs12000_Fc66_A2,                /* 12kS/s coefficients */
99      HPF_Fs12000_Fc66_A1,
100      HPF_Fs12000_Fc66_A0,
101      -HPF_Fs12000_Fc66_B2,
102      -HPF_Fs12000_Fc66_B1},
103     {HPF_Fs16000_Fc66_A2,                /* 16kS/s coefficients */
104      HPF_Fs16000_Fc66_A1,
105      HPF_Fs16000_Fc66_A0,
106      -HPF_Fs16000_Fc66_B2,
107      -HPF_Fs16000_Fc66_B1},
108     {HPF_Fs22050_Fc66_A2,                /* 22kS/s coefficients */
109      HPF_Fs22050_Fc66_A1,
110      HPF_Fs22050_Fc66_A0,
111      -HPF_Fs22050_Fc66_B2,
112      -HPF_Fs22050_Fc66_B1},
113     {HPF_Fs24000_Fc66_A2,                /* 24kS/s coefficients */
114      HPF_Fs24000_Fc66_A1,
115      HPF_Fs24000_Fc66_A0,
116      -HPF_Fs24000_Fc66_B2,
117      -HPF_Fs24000_Fc66_B1},
118     {HPF_Fs32000_Fc66_A2,                /* 32kS/s coefficients */
119      HPF_Fs32000_Fc66_A1,
120      HPF_Fs32000_Fc66_A0,
121      -HPF_Fs32000_Fc66_B2,
122      -HPF_Fs32000_Fc66_B1},
123     {HPF_Fs44100_Fc66_A2,                /* 44kS/s coefficients */
124      HPF_Fs44100_Fc66_A1,
125      HPF_Fs44100_Fc66_A0,
126      -HPF_Fs44100_Fc66_B2,
127      -HPF_Fs44100_Fc66_B1},
128     {HPF_Fs48000_Fc66_A2,                /* 48kS/s coefficients */
129      HPF_Fs48000_Fc66_A1,
130      HPF_Fs48000_Fc66_A0,
131      -HPF_Fs48000_Fc66_B2,
132      -HPF_Fs48000_Fc66_B1},
133 
134     /* Coefficients for 78Hz centre frequency */
135     {HPF_Fs8000_Fc78_A2,                /* 8kS/s coefficients */
136      HPF_Fs8000_Fc78_A1,
137      HPF_Fs8000_Fc78_A0,
138      -HPF_Fs8000_Fc78_B2,
139      -HPF_Fs8000_Fc78_B1},
140     {HPF_Fs11025_Fc78_A2,                /* 11kS/s coefficients */
141      HPF_Fs11025_Fc78_A1,
142      HPF_Fs11025_Fc78_A0,
143      -HPF_Fs11025_Fc78_B2,
144      -HPF_Fs11025_Fc78_B1},
145     {HPF_Fs12000_Fc78_A2,                /* 12kS/s coefficients */
146      HPF_Fs12000_Fc78_A1,
147      HPF_Fs12000_Fc78_A0,
148      -HPF_Fs12000_Fc78_B2,
149      -HPF_Fs12000_Fc78_B1},
150     {HPF_Fs16000_Fc78_A2,                /* 16kS/s coefficients */
151      HPF_Fs16000_Fc78_A1,
152      HPF_Fs16000_Fc78_A0,
153      -HPF_Fs16000_Fc78_B2,
154      -HPF_Fs16000_Fc78_B1},
155     {HPF_Fs22050_Fc78_A2,                /* 22kS/s coefficients */
156      HPF_Fs22050_Fc78_A1,
157      HPF_Fs22050_Fc78_A0,
158      -HPF_Fs22050_Fc78_B2,
159      -HPF_Fs22050_Fc78_B1},
160     {HPF_Fs24000_Fc78_A2,                /* 24kS/s coefficients */
161      HPF_Fs24000_Fc78_A1,
162      HPF_Fs24000_Fc78_A0,
163      -HPF_Fs24000_Fc78_B2,
164      -HPF_Fs24000_Fc78_B1},
165     {HPF_Fs32000_Fc78_A2,                /* 32kS/s coefficients */
166      HPF_Fs32000_Fc78_A1,
167      HPF_Fs32000_Fc78_A0,
168      -HPF_Fs32000_Fc78_B2,
169      -HPF_Fs32000_Fc78_B1},
170     {HPF_Fs44100_Fc78_A2,                /* 44kS/s coefficients */
171      HPF_Fs44100_Fc78_A1,
172      HPF_Fs44100_Fc78_A0,
173      -HPF_Fs44100_Fc78_B2,
174      -HPF_Fs44100_Fc78_B1},
175     {HPF_Fs48000_Fc78_A2,                /* 48kS/s coefficients */
176      HPF_Fs48000_Fc78_A1,
177      HPF_Fs48000_Fc78_A0,
178      -HPF_Fs48000_Fc78_B2,
179      -HPF_Fs48000_Fc78_B1},
180 
181     /* Coefficients for 90Hz centre frequency */
182     {HPF_Fs8000_Fc90_A2,                /* 8kS/s coefficients */
183      HPF_Fs8000_Fc90_A1,
184      HPF_Fs8000_Fc90_A0,
185      -HPF_Fs8000_Fc90_B2,
186      -HPF_Fs8000_Fc90_B1},
187     {HPF_Fs11025_Fc90_A2,                /* 11kS/s coefficients */
188      HPF_Fs11025_Fc90_A1,
189      HPF_Fs11025_Fc90_A0,
190      -HPF_Fs11025_Fc90_B2,
191      -HPF_Fs11025_Fc90_B1},
192     {HPF_Fs12000_Fc90_A2,                /* 12kS/s coefficients */
193      HPF_Fs12000_Fc90_A1,
194      HPF_Fs12000_Fc90_A0,
195      -HPF_Fs12000_Fc90_B2,
196      -HPF_Fs12000_Fc90_B1},
197     {HPF_Fs16000_Fc90_A2,                /* 16kS/s coefficients */
198      HPF_Fs16000_Fc90_A1,
199      HPF_Fs16000_Fc90_A0,
200      -HPF_Fs16000_Fc90_B2,
201      -HPF_Fs16000_Fc90_B1},
202     {HPF_Fs22050_Fc90_A2,                /* 22kS/s coefficients */
203      HPF_Fs22050_Fc90_A1,
204      HPF_Fs22050_Fc90_A0,
205      -HPF_Fs22050_Fc90_B2,
206      -HPF_Fs22050_Fc90_B1},
207     {HPF_Fs24000_Fc90_A2,                /* 24kS/s coefficients */
208      HPF_Fs24000_Fc90_A1,
209      HPF_Fs24000_Fc90_A0,
210      -HPF_Fs24000_Fc90_B2,
211      -HPF_Fs24000_Fc90_B1},
212     {HPF_Fs32000_Fc90_A2,                /* 32kS/s coefficients */
213      HPF_Fs32000_Fc90_A1,
214      HPF_Fs32000_Fc90_A0,
215      -HPF_Fs32000_Fc90_B2,
216      -HPF_Fs32000_Fc90_B1},
217     {HPF_Fs44100_Fc90_A2,                /* 44kS/s coefficients */
218      HPF_Fs44100_Fc90_A1,
219      HPF_Fs44100_Fc90_A0,
220      -HPF_Fs44100_Fc90_B2,
221      -HPF_Fs44100_Fc90_B1},
222     {HPF_Fs48000_Fc90_A2,                /* 48kS/s coefficients */
223      HPF_Fs48000_Fc90_A1,
224      HPF_Fs48000_Fc90_A0,
225      -HPF_Fs48000_Fc90_B2,
226      -HPF_Fs48000_Fc90_B1}};
227 
228 /*
229  * Band Pass Filter coefficient table
230  */
231 const BP_C32_Coefs_t LVDBE_BPF_Table[] = {
232     /* Coefficients for 55Hz centre frequency */
233     {BPF_Fs8000_Fc55_A0,                /* 8kS/s coefficients */
234      -BPF_Fs8000_Fc55_B2,
235      -BPF_Fs8000_Fc55_B1},
236     {BPF_Fs11025_Fc55_A0,                /* 11kS/s coefficients */
237      -BPF_Fs11025_Fc55_B2,
238      -BPF_Fs11025_Fc55_B1},
239     {BPF_Fs12000_Fc55_A0,                /* 12kS/s coefficients */
240      -BPF_Fs12000_Fc55_B2,
241      -BPF_Fs12000_Fc55_B1},
242     {BPF_Fs16000_Fc55_A0,                /* 16kS/s coefficients */
243      -BPF_Fs16000_Fc55_B2,
244      -BPF_Fs16000_Fc55_B1},
245     {BPF_Fs22050_Fc55_A0,                /* 22kS/s coefficients */
246      -BPF_Fs22050_Fc55_B2,
247      -BPF_Fs22050_Fc55_B1},
248     {BPF_Fs24000_Fc55_A0,                /* 24kS/s coefficients */
249      -BPF_Fs24000_Fc55_B2,
250      -BPF_Fs24000_Fc55_B1},
251     {BPF_Fs32000_Fc55_A0,                /* 32kS/s coefficients */
252      -BPF_Fs32000_Fc55_B2,
253      -BPF_Fs32000_Fc55_B1},
254     {BPF_Fs44100_Fc55_A0,                /* 44kS/s coefficients */
255      -BPF_Fs44100_Fc55_B2,
256      -BPF_Fs44100_Fc55_B1},
257     {BPF_Fs48000_Fc55_A0,                /* 48kS/s coefficients */
258      -BPF_Fs48000_Fc55_B2,
259      -BPF_Fs48000_Fc55_B1},
260 
261     /* Coefficients for 66Hz centre frequency */
262     {BPF_Fs8000_Fc66_A0,                /* 8kS/s coefficients */
263      -BPF_Fs8000_Fc66_B2,
264      -BPF_Fs8000_Fc66_B1},
265     {BPF_Fs11025_Fc66_A0,                /* 11kS/s coefficients */
266      -BPF_Fs11025_Fc66_B2,
267      -BPF_Fs11025_Fc66_B1},
268     {BPF_Fs12000_Fc66_A0,                /* 12kS/s coefficients */
269      -BPF_Fs12000_Fc66_B2,
270      -BPF_Fs12000_Fc66_B1},
271     {BPF_Fs16000_Fc66_A0,                /* 16kS/s coefficients */
272      -BPF_Fs16000_Fc66_B2,
273      -BPF_Fs16000_Fc66_B1},
274     {BPF_Fs22050_Fc66_A0,                /* 22kS/s coefficients */
275      -BPF_Fs22050_Fc66_B2,
276      -BPF_Fs22050_Fc66_B1},
277     {BPF_Fs24000_Fc66_A0,                /* 24kS/s coefficients */
278      -BPF_Fs24000_Fc66_B2,
279      -BPF_Fs24000_Fc66_B1},
280     {BPF_Fs32000_Fc66_A0,                /* 32kS/s coefficients */
281      -BPF_Fs32000_Fc66_B2,
282      -BPF_Fs32000_Fc66_B1},
283     {BPF_Fs44100_Fc66_A0,                /* 44kS/s coefficients */
284      -BPF_Fs44100_Fc66_B2,
285      -BPF_Fs44100_Fc66_B1},
286     {BPF_Fs48000_Fc66_A0,                /* 48kS/s coefficients */
287      -BPF_Fs48000_Fc66_B2,
288      -BPF_Fs48000_Fc66_B1},
289 
290     /* Coefficients for 78Hz centre frequency */
291     {BPF_Fs8000_Fc78_A0,                /* 8kS/s coefficients */
292      -BPF_Fs8000_Fc78_B2,
293      -BPF_Fs8000_Fc78_B1},
294     {BPF_Fs11025_Fc78_A0,                /* 11kS/s coefficients */
295      -BPF_Fs11025_Fc78_B2,
296      -BPF_Fs11025_Fc78_B1},
297     {BPF_Fs12000_Fc78_A0,                /* 12kS/s coefficients */
298      -BPF_Fs12000_Fc78_B2,
299      -BPF_Fs12000_Fc78_B1},
300     {BPF_Fs16000_Fc78_A0,                /* 16kS/s coefficients */
301      -BPF_Fs16000_Fc78_B2,
302      -BPF_Fs16000_Fc78_B1},
303     {BPF_Fs22050_Fc78_A0,                /* 22kS/s coefficients */
304      -BPF_Fs22050_Fc78_B2,
305      -BPF_Fs22050_Fc78_B1},
306     {BPF_Fs24000_Fc78_A0,                /* 24kS/s coefficients */
307      -BPF_Fs24000_Fc78_B2,
308      -BPF_Fs24000_Fc78_B1},
309     {BPF_Fs32000_Fc78_A0,                /* 32kS/s coefficients */
310      -BPF_Fs32000_Fc78_B2,
311      -BPF_Fs32000_Fc78_B1},
312     {BPF_Fs44100_Fc78_A0,                /* 44kS/s coefficients */
313      -BPF_Fs44100_Fc78_B2,
314      -BPF_Fs44100_Fc78_B1},
315     {BPF_Fs48000_Fc78_A0,                /* 48kS/s coefficients */
316      -BPF_Fs48000_Fc78_B2,
317      -BPF_Fs48000_Fc78_B1},
318 
319     /* Coefficients for 90Hz centre frequency */
320     {BPF_Fs8000_Fc90_A0,                /* 8kS/s coefficients */
321      -BPF_Fs8000_Fc90_B2,
322      -BPF_Fs8000_Fc90_B1},
323     {BPF_Fs11025_Fc90_A0,                /* 11kS/s coefficients */
324      -BPF_Fs11025_Fc90_B2,
325      -BPF_Fs11025_Fc90_B1},
326     {BPF_Fs12000_Fc90_A0,                /* 12kS/s coefficients */
327      -BPF_Fs12000_Fc90_B2,
328      -BPF_Fs12000_Fc90_B1},
329     {BPF_Fs16000_Fc90_A0,                /* 16kS/s coefficients */
330      -BPF_Fs16000_Fc90_B2,
331      -BPF_Fs16000_Fc90_B1},
332     {BPF_Fs22050_Fc90_A0,                /* 22kS/s coefficients */
333      -BPF_Fs22050_Fc90_B2,
334      -BPF_Fs22050_Fc90_B1},
335     {BPF_Fs24000_Fc90_A0,                /* 24kS/s coefficients */
336      -BPF_Fs24000_Fc90_B2,
337      -BPF_Fs24000_Fc90_B1},
338     {BPF_Fs32000_Fc90_A0,                /* 32kS/s coefficients */
339      -BPF_Fs32000_Fc90_B2,
340      -BPF_Fs32000_Fc90_B1},
341     {BPF_Fs44100_Fc90_A0,                /* 44kS/s coefficients */
342      -BPF_Fs44100_Fc90_B2,
343      -BPF_Fs44100_Fc90_B1},
344     {BPF_Fs48000_Fc90_A0,                /* 48kS/s coefficients */
345      -BPF_Fs48000_Fc90_B2,
346      -BPF_Fs48000_Fc90_B1}};
347 
348 
349 /************************************************************************************/
350 /*                                                                                  */
351 /*    AGC constant tables                                                           */
352 /*                                                                                  */
353 /************************************************************************************/
354 
355 /* Attack time (signal too large) */
356 const LVM_INT16 LVDBE_AGC_ATTACK_Table[] = {
357     AGC_ATTACK_Fs8000,
358     AGC_ATTACK_Fs11025,
359     AGC_ATTACK_Fs12000,
360     AGC_ATTACK_Fs16000,
361     AGC_ATTACK_Fs22050,
362     AGC_ATTACK_Fs24000,
363     AGC_ATTACK_Fs32000,
364     AGC_ATTACK_Fs44100,
365     AGC_ATTACK_Fs48000};
366 
367 /* Decay time (signal too small) */
368 const LVM_INT16 LVDBE_AGC_DECAY_Table[] = {
369     AGC_DECAY_Fs8000,
370     AGC_DECAY_Fs11025,
371     AGC_DECAY_Fs12000,
372     AGC_DECAY_Fs16000,
373     AGC_DECAY_Fs22050,
374     AGC_DECAY_Fs24000,
375     AGC_DECAY_Fs32000,
376     AGC_DECAY_Fs44100,
377     AGC_DECAY_Fs48000};
378 
379 /* Gain for use without the high pass filter */
380 const LVM_INT32 LVDBE_AGC_GAIN_Table[] = {
381     AGC_GAIN_0dB,
382     AGC_GAIN_1dB,
383     AGC_GAIN_2dB,
384     AGC_GAIN_3dB,
385     AGC_GAIN_4dB,
386     AGC_GAIN_5dB,
387     AGC_GAIN_6dB,
388     AGC_GAIN_7dB,
389     AGC_GAIN_8dB,
390     AGC_GAIN_9dB,
391     AGC_GAIN_10dB,
392     AGC_GAIN_11dB,
393     AGC_GAIN_12dB,
394     AGC_GAIN_13dB,
395     AGC_GAIN_14dB,
396     AGC_GAIN_15dB};
397 
398 /* Gain for use with the high pass filter */
399 const LVM_INT32 LVDBE_AGC_HPFGAIN_Table[] = {
400     AGC_HPFGAIN_0dB,
401     AGC_HPFGAIN_1dB,
402     AGC_HPFGAIN_2dB,
403     AGC_HPFGAIN_3dB,
404     AGC_HPFGAIN_4dB,
405     AGC_HPFGAIN_5dB,
406     AGC_HPFGAIN_6dB,
407     AGC_HPFGAIN_7dB,
408     AGC_HPFGAIN_8dB,
409     AGC_HPFGAIN_9dB,
410     AGC_HPFGAIN_10dB,
411     AGC_HPFGAIN_11dB,
412     AGC_HPFGAIN_12dB,
413     AGC_HPFGAIN_13dB,
414     AGC_HPFGAIN_14dB,
415     AGC_HPFGAIN_15dB};
416 
417 
418 /************************************************************************************/
419 /*                                                                                  */
420 /*    Volume control gain and time constant tables                                  */
421 /*                                                                                  */
422 /************************************************************************************/
423 
424 /* dB to linear conversion table */
425 const LVM_INT16 LVDBE_VolumeTable[] = {
426     0x4000,             /* -6dB */
427     0x47FB,             /* -5dB */
428     0x50C3,             /* -4dB */
429     0x5A9E,             /* -3dB */
430     0x65AD,             /* -2dB */
431     0x7215,             /* -1dB */
432     0x7FFF};            /*  0dB */
433 
434 const LVM_INT16 LVDBE_VolumeTCTable[] = {
435     VOL_TC_Fs8000,
436     VOL_TC_Fs11025,
437     VOL_TC_Fs12000,
438     VOL_TC_Fs16000,
439     VOL_TC_Fs22050,
440     VOL_TC_Fs24000,
441     VOL_TC_Fs32000,
442     VOL_TC_Fs44100,
443     VOL_TC_Fs48000};
444 
445 const LVM_INT16 LVDBE_MixerTCTable[] = {
446     MIX_TC_Fs8000,
447     MIX_TC_Fs11025,
448     MIX_TC_Fs12000,
449     MIX_TC_Fs16000,
450     MIX_TC_Fs22050,
451     MIX_TC_Fs24000,
452     MIX_TC_Fs32000,
453     MIX_TC_Fs44100,
454     MIX_TC_Fs48000};
455 
456 
457