fix Tab navigation in rbutil, and make it also look at other places for rbutil.ini.
[kugel-rb.git] / apps / eq.c
blobe71af7cefc4bb82a9b8c6769d79787855a731130
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006-2007 Thom Johansen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <inttypes.h>
21 #include "config.h"
22 #include "dsp.h"
23 #include "eq.h"
24 #include "replaygain.h"
26 /* Inverse gain of circular cordic rotation in s0.31 format. */
27 static const long cordic_circular_gain = 0xb2458939; /* 0.607252929 */
29 /* Table of values of atan(2^-i) in 0.32 format fractions of pi where pi = 0xffffffff / 2 */
30 static const unsigned long atan_table[] = {
31 0x1fffffff, /* +0.785398163 (or pi/4) */
32 0x12e4051d, /* +0.463647609 */
33 0x09fb385b, /* +0.244978663 */
34 0x051111d4, /* +0.124354995 */
35 0x028b0d43, /* +0.062418810 */
36 0x0145d7e1, /* +0.031239833 */
37 0x00a2f61e, /* +0.015623729 */
38 0x00517c55, /* +0.007812341 */
39 0x0028be53, /* +0.003906230 */
40 0x00145f2e, /* +0.001953123 */
41 0x000a2f98, /* +0.000976562 */
42 0x000517cc, /* +0.000488281 */
43 0x00028be6, /* +0.000244141 */
44 0x000145f3, /* +0.000122070 */
45 0x0000a2f9, /* +0.000061035 */
46 0x0000517c, /* +0.000030518 */
47 0x000028be, /* +0.000015259 */
48 0x0000145f, /* +0.000007629 */
49 0x00000a2f, /* +0.000003815 */
50 0x00000517, /* +0.000001907 */
51 0x0000028b, /* +0.000000954 */
52 0x00000145, /* +0.000000477 */
53 0x000000a2, /* +0.000000238 */
54 0x00000051, /* +0.000000119 */
55 0x00000028, /* +0.000000060 */
56 0x00000014, /* +0.000000030 */
57 0x0000000a, /* +0.000000015 */
58 0x00000005, /* +0.000000007 */
59 0x00000002, /* +0.000000004 */
60 0x00000001, /* +0.000000002 */
61 0x00000000, /* +0.000000001 */
62 0x00000000, /* +0.000000000 */
65 /**
66 * Implements sin and cos using CORDIC rotation.
68 * @param phase has range from 0 to 0xffffffff, representing 0 and
69 * 2*pi respectively.
70 * @param cos return address for cos
71 * @return sin of phase, value is a signed value from LONG_MIN to LONG_MAX,
72 * representing -1 and 1 respectively.
74 static long fsincos(unsigned long phase, long *cos) {
75 int32_t x, x1, y, y1;
76 unsigned long z, z1;
77 int i;
79 /* Setup initial vector */
80 x = cordic_circular_gain;
81 y = 0;
82 z = phase;
84 /* The phase has to be somewhere between 0..pi for this to work right */
85 if (z < 0xffffffff / 4) {
86 /* z in first quadrant, z += pi/2 to correct */
87 x = -x;
88 z += 0xffffffff / 4;
89 } else if (z < 3 * (0xffffffff / 4)) {
90 /* z in third quadrant, z -= pi/2 to correct */
91 z -= 0xffffffff / 4;
92 } else {
93 /* z in fourth quadrant, z -= 3pi/2 to correct */
94 x = -x;
95 z -= 3 * (0xffffffff / 4);
98 /* Each iteration adds roughly 1-bit of extra precision */
99 for (i = 0; i < 31; i++) {
100 x1 = x >> i;
101 y1 = y >> i;
102 z1 = atan_table[i];
104 /* Decided which direction to rotate vector. Pivot point is pi/2 */
105 if (z >= 0xffffffff / 4) {
106 x -= y1;
107 y += x1;
108 z -= z1;
109 } else {
110 x += y1;
111 y -= x1;
112 z += z1;
116 *cos = x;
118 return y;
121 /**
122 * Calculate first order shelving filter. Filter is not directly usable by the
123 * eq_filter() function.
124 * @param cutoff shelf midpoint frequency. See eq_pk_coefs for format.
125 * @param A decibel value multiplied by ten, describing gain/attenuation of
126 * shelf. Max value is 24 dB.
127 * @param low true for low-shelf filter, false for high-shelf filter.
128 * @param c pointer to coefficient storage. Coefficients are s4.27 format.
130 void filter_shelf_coefs(unsigned long cutoff, long A, bool low, int32_t *c)
132 long sin, cos;
133 int32_t b0, b1, a0, a1; /* s3.28 */
134 const long g = get_replaygain_int(A*5) << 4; /* 10^(db/40), s3.28 */
136 sin = fsincos(cutoff/2, &cos);
137 if (low) {
138 const int32_t sin_div_g = DIV64(sin, g, 25);
139 cos >>= 3;
140 b0 = FRACMUL(sin, g) + cos; /* 0.25 .. 4.10 */
141 b1 = FRACMUL(sin, g) - cos; /* -1 .. 3.98 */
142 a0 = sin_div_g + cos; /* 0.25 .. 4.10 */
143 a1 = sin_div_g - cos; /* -1 .. 3.98 */
144 } else {
145 const int32_t cos_div_g = DIV64(cos, g, 25);
146 sin >>= 3;
147 b0 = sin + FRACMUL(cos, g); /* 0.25 .. 4.10 */
148 b1 = sin - FRACMUL(cos, g); /* -3.98 .. 1 */
149 a0 = sin + cos_div_g; /* 0.25 .. 4.10 */
150 a1 = sin - cos_div_g; /* -3.98 .. 1 */
153 const int32_t rcp_a0 = DIV64(1, a0, 57); /* 0.24 .. 3.98, s2.29 */
154 *c++ = FRACMUL_SHL(b0, rcp_a0, 1); /* 0.063 .. 15.85 */
155 *c++ = FRACMUL_SHL(b1, rcp_a0, 1); /* -15.85 .. 15.85 */
156 *c++ = -FRACMUL_SHL(a1, rcp_a0, 1); /* -1 .. 1 */
159 #ifdef HAVE_SW_TONE_CONTROLS
160 /**
161 * Calculate second order section filter consisting of one low-shelf and one
162 * high-shelf section.
163 * @param cutoff_low low-shelf midpoint frequency. See eq_pk_coefs for format.
164 * @param cutoff_high high-shelf midpoint frequency.
165 * @param A_low decibel value multiplied by ten, describing gain/attenuation of
166 * low-shelf part. Max value is 24 dB.
167 * @param A_high decibel value multiplied by ten, describing gain/attenuation of
168 * high-shelf part. Max value is 24 dB.
169 * @param A decibel value multiplied by ten, describing additional overall gain.
170 * @param c pointer to coefficient storage. Coefficients are s4.27 format.
172 void filter_bishelf_coefs(unsigned long cutoff_low, unsigned long cutoff_high,
173 long A_low, long A_high, long A, int32_t *c)
175 const long g = get_replaygain_int(A*10) << 7; /* 10^(db/20), s0.31 */
176 int32_t c_ls[3], c_hs[3];
178 filter_shelf_coefs(cutoff_low, A_low, true, c_ls);
179 filter_shelf_coefs(cutoff_high, A_high, false, c_hs);
180 c_ls[0] = FRACMUL(g, c_ls[0]);
181 c_ls[1] = FRACMUL(g, c_ls[1]);
183 /* now we cascade the two first order filters to one second order filter
184 * which can be used by eq_filter(). these resulting coefficients have a
185 * really wide numerical range, so we use a fixed point format which will
186 * work for the selected cutoff frequencies (in dsp.c) only.
188 const int32_t b0 = c_ls[0], b1 = c_ls[1], b2 = c_hs[0], b3 = c_hs[1];
189 const int32_t a0 = c_ls[2], a1 = c_hs[2];
190 *c++ = FRACMUL_SHL(b0, b2, 4);
191 *c++ = FRACMUL_SHL(b0, b3, 4) + FRACMUL_SHL(b1, b2, 4);
192 *c++ = FRACMUL_SHL(b1, b3, 4);
193 *c++ = a0 + a1;
194 *c++ = -FRACMUL_SHL(a0, a1, 4);
196 #endif
198 /* Coef calculation taken from Audio-EQ-Cookbook.txt by Robert Bristow-Johnson.
199 * Slightly faster calculation can be done by deriving forms which use tan()
200 * instead of cos() and sin(), but the latter are far easier to use when doing
201 * fixed point math, and performance is not a big point in the calculation part.
202 * All the 'a' filter coefficients are negated so we can use only additions
203 * in the filtering equation.
206 /**
207 * Calculate second order section peaking filter coefficients.
208 * @param cutoff a value from 0 to 0x80000000, where 0 represents 0 Hz and
209 * 0x80000000 represents the Nyquist frequency (samplerate/2).
210 * @param Q Q factor value multiplied by ten. Lower bound is artificially set
211 * at 0.5.
212 * @param db decibel value multiplied by ten, describing gain/attenuation at
213 * peak freq. Max value is 24 dB.
214 * @param c pointer to coefficient storage. Coefficients are s3.28 format.
216 void eq_pk_coefs(unsigned long cutoff, unsigned long Q, long db, int32_t *c)
218 long cs;
219 const long one = 1 << 28; /* s3.28 */
220 const long A = get_replaygain_int(db*5) << 5; /* 10^(db/40), s2.29 */
221 const long alpha = fsincos(cutoff, &cs)/(2*Q)*10 >> 1; /* s1.30 */
222 int32_t a0, a1, a2; /* these are all s3.28 format */
223 int32_t b0, b1, b2;
224 const long alphadivA = DIV64(alpha, A, 27);
226 /* possible numerical ranges are in comments by each coef */
227 b0 = one + FRACMUL(alpha, A); /* [1 .. 5] */
228 b1 = a1 = -2*(cs >> 3); /* [-2 .. 2] */
229 b2 = one - FRACMUL(alpha, A); /* [-3 .. 1] */
230 a0 = one + alphadivA; /* [1 .. 5] */
231 a2 = one - alphadivA; /* [-3 .. 1] */
233 /* range of this is roughly [0.2 .. 1], but we'll never hit 1 completely */
234 const long rcp_a0 = DIV64(1, a0, 59); /* s0.31 */
235 *c++ = FRACMUL(b0, rcp_a0); /* [0.25 .. 4] */
236 *c++ = FRACMUL(b1, rcp_a0); /* [-2 .. 2] */
237 *c++ = FRACMUL(b2, rcp_a0); /* [-2.4 .. 1] */
238 *c++ = FRACMUL(-a1, rcp_a0); /* [-2 .. 2] */
239 *c++ = FRACMUL(-a2, rcp_a0); /* [-0.6 .. 1] */
243 * Calculate coefficients for lowshelf filter. Parameters are as for
244 * eq_pk_coefs, but the coefficient format is s5.26 fixed point.
246 void eq_ls_coefs(unsigned long cutoff, unsigned long Q, long db, int32_t *c)
248 long cs;
249 const long one = 1 << 25; /* s6.25 */
250 const long sqrtA = get_replaygain_int(db*5/2) << 2; /* 10^(db/80), s5.26 */
251 const long A = FRACMUL_SHL(sqrtA, sqrtA, 8); /* s2.29 */
252 const long alpha = fsincos(cutoff, &cs)/(2*Q)*10 >> 1; /* s1.30 */
253 const long ap1 = (A >> 4) + one;
254 const long am1 = (A >> 4) - one;
255 const long twosqrtalpha = 2*FRACMUL(sqrtA, alpha);
256 int32_t a0, a1, a2; /* these are all s6.25 format */
257 int32_t b0, b1, b2;
259 /* [0.1 .. 40] */
260 b0 = FRACMUL_SHL(A, ap1 - FRACMUL(am1, cs) + twosqrtalpha, 2);
261 /* [-16 .. 63.4] */
262 b1 = FRACMUL_SHL(A, am1 - FRACMUL(ap1, cs), 3);
263 /* [0 .. 31.7] */
264 b2 = FRACMUL_SHL(A, ap1 - FRACMUL(am1, cs) - twosqrtalpha, 2);
265 /* [0.5 .. 10] */
266 a0 = ap1 + FRACMUL(am1, cs) + twosqrtalpha;
267 /* [-16 .. 4] */
268 a1 = -2*((am1 + FRACMUL(ap1, cs)));
269 /* [0 .. 8] */
270 a2 = ap1 + FRACMUL(am1, cs) - twosqrtalpha;
272 /* [0.1 .. 1.99] */
273 const long rcp_a0 = DIV64(1, a0, 55); /* s1.30 */
274 *c++ = FRACMUL_SHL(b0, rcp_a0, 2); /* [0.06 .. 15.9] */
275 *c++ = FRACMUL_SHL(b1, rcp_a0, 2); /* [-2 .. 31.7] */
276 *c++ = FRACMUL_SHL(b2, rcp_a0, 2); /* [0 .. 15.9] */
277 *c++ = FRACMUL_SHL(-a1, rcp_a0, 2); /* [-2 .. 2] */
278 *c++ = FRACMUL_SHL(-a2, rcp_a0, 2); /* [0 .. 1] */
282 * Calculate coefficients for highshelf filter. Parameters are as for
283 * eq_pk_coefs, but the coefficient format is s5.26 fixed point.
285 void eq_hs_coefs(unsigned long cutoff, unsigned long Q, long db, int32_t *c)
287 long cs;
288 const long one = 1 << 25; /* s6.25 */
289 const long sqrtA = get_replaygain_int(db*5/2) << 2; /* 10^(db/80), s5.26 */
290 const long A = FRACMUL_SHL(sqrtA, sqrtA, 8); /* s2.29 */
291 const long alpha = fsincos(cutoff, &cs)/(2*Q)*10 >> 1; /* s1.30 */
292 const long ap1 = (A >> 4) + one;
293 const long am1 = (A >> 4) - one;
294 const long twosqrtalpha = 2*FRACMUL(sqrtA, alpha);
295 int32_t a0, a1, a2; /* these are all s6.25 format */
296 int32_t b0, b1, b2;
298 /* [0.1 .. 40] */
299 b0 = FRACMUL_SHL(A, ap1 + FRACMUL(am1, cs) + twosqrtalpha, 2);
300 /* [-63.5 .. 16] */
301 b1 = -FRACMUL_SHL(A, am1 + FRACMUL(ap1, cs), 3);
302 /* [0 .. 32] */
303 b2 = FRACMUL_SHL(A, ap1 + FRACMUL(am1, cs) - twosqrtalpha, 2);
304 /* [0.5 .. 10] */
305 a0 = ap1 - FRACMUL(am1, cs) + twosqrtalpha;
306 /* [-4 .. 16] */
307 a1 = 2*((am1 - FRACMUL(ap1, cs)));
308 /* [0 .. 8] */
309 a2 = ap1 - FRACMUL(am1, cs) - twosqrtalpha;
311 /* [0.1 .. 1.99] */
312 const long rcp_a0 = DIV64(1, a0, 55); /* s1.30 */
313 *c++ = FRACMUL_SHL(b0, rcp_a0, 2); /* [0 .. 16] */
314 *c++ = FRACMUL_SHL(b1, rcp_a0, 2); /* [-31.7 .. 2] */
315 *c++ = FRACMUL_SHL(b2, rcp_a0, 2); /* [0 .. 16] */
316 *c++ = FRACMUL_SHL(-a1, rcp_a0, 2); /* [-2 .. 2] */
317 *c++ = FRACMUL_SHL(-a2, rcp_a0, 2); /* [0 .. 1] */
320 /* We realise the filters as a second order direct form 1 structure. Direct
321 * form 1 was chosen because of better numerical properties for fixed point
322 * implementations.
325 #if (!defined(CPU_COLDFIRE) && !defined(CPU_ARM))
326 void eq_filter(int32_t **x, struct eqfilter *f, unsigned num,
327 unsigned channels, unsigned shift)
329 unsigned c, i;
330 long long acc;
332 /* Direct form 1 filtering code.
333 y[n] = b0*x[i] + b1*x[i - 1] + b2*x[i - 2] + a1*y[i - 1] + a2*y[i - 2],
334 where y[] is output and x[] is input.
337 for (c = 0; c < channels; c++) {
338 for (i = 0; i < num; i++) {
339 acc = (long long) x[c][i] * f->coefs[0];
340 acc += (long long) f->history[c][0] * f->coefs[1];
341 acc += (long long) f->history[c][1] * f->coefs[2];
342 acc += (long long) f->history[c][2] * f->coefs[3];
343 acc += (long long) f->history[c][3] * f->coefs[4];
344 f->history[c][1] = f->history[c][0];
345 f->history[c][0] = x[c][i];
346 f->history[c][3] = f->history[c][2];
347 x[c][i] = (acc << shift) >> 32;
348 f->history[c][2] = x[c][i];
352 #endif