Fix test_boost boost handling. Also show the number of loops per second.
[kugel-rb.git] / apps / eq.c
blob6437fed90625d7f4d351522ced09e9a0b69a1cb0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006-2007 Thom Johansen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <inttypes.h>
23 #include "config.h"
24 #include "fixedpoint.h"
25 #include "fracmul.h"
26 #include "eq.h"
27 #include "replaygain.h"
29 /**
30 * Calculate first order shelving filter. Filter is not directly usable by the
31 * eq_filter() function.
32 * @param cutoff shelf midpoint frequency. See eq_pk_coefs for format.
33 * @param A decibel value multiplied by ten, describing gain/attenuation of
34 * shelf. Max value is 24 dB.
35 * @param low true for low-shelf filter, false for high-shelf filter.
36 * @param c pointer to coefficient storage. Coefficients are s4.27 format.
38 void filter_shelf_coefs(unsigned long cutoff, long A, bool low, int32_t *c)
40 long sin, cos;
41 int32_t b0, b1, a0, a1; /* s3.28 */
42 const long g = get_replaygain_int(A*5) << 4; /* 10^(db/40), s3.28 */
44 sin = fp_sincos(cutoff/2, &cos);
45 if (low) {
46 const int32_t sin_div_g = fp_div(sin, g, 25);
47 cos >>= 3;
48 b0 = FRACMUL(sin, g) + cos; /* 0.25 .. 4.10 */
49 b1 = FRACMUL(sin, g) - cos; /* -1 .. 3.98 */
50 a0 = sin_div_g + cos; /* 0.25 .. 4.10 */
51 a1 = sin_div_g - cos; /* -1 .. 3.98 */
52 } else {
53 const int32_t cos_div_g = fp_div(cos, g, 25);
54 sin >>= 3;
55 b0 = sin + FRACMUL(cos, g); /* 0.25 .. 4.10 */
56 b1 = sin - FRACMUL(cos, g); /* -3.98 .. 1 */
57 a0 = sin + cos_div_g; /* 0.25 .. 4.10 */
58 a1 = sin - cos_div_g; /* -3.98 .. 1 */
61 const int32_t rcp_a0 = fp_div(1, a0, 57); /* 0.24 .. 3.98, s2.29 */
62 *c++ = FRACMUL_SHL(b0, rcp_a0, 1); /* 0.063 .. 15.85 */
63 *c++ = FRACMUL_SHL(b1, rcp_a0, 1); /* -15.85 .. 15.85 */
64 *c++ = -FRACMUL_SHL(a1, rcp_a0, 1); /* -1 .. 1 */
67 #ifdef HAVE_SW_TONE_CONTROLS
68 /**
69 * Calculate second order section filter consisting of one low-shelf and one
70 * high-shelf section.
71 * @param cutoff_low low-shelf midpoint frequency. See eq_pk_coefs for format.
72 * @param cutoff_high high-shelf midpoint frequency.
73 * @param A_low decibel value multiplied by ten, describing gain/attenuation of
74 * low-shelf part. Max value is 24 dB.
75 * @param A_high decibel value multiplied by ten, describing gain/attenuation of
76 * high-shelf part. Max value is 24 dB.
77 * @param A decibel value multiplied by ten, describing additional overall gain.
78 * @param c pointer to coefficient storage. Coefficients are s4.27 format.
80 void filter_bishelf_coefs(unsigned long cutoff_low, unsigned long cutoff_high,
81 long A_low, long A_high, long A, int32_t *c)
83 const long g = get_replaygain_int(A*10) << 7; /* 10^(db/20), s0.31 */
84 int32_t c_ls[3], c_hs[3];
86 filter_shelf_coefs(cutoff_low, A_low, true, c_ls);
87 filter_shelf_coefs(cutoff_high, A_high, false, c_hs);
88 c_ls[0] = FRACMUL(g, c_ls[0]);
89 c_ls[1] = FRACMUL(g, c_ls[1]);
91 /* now we cascade the two first order filters to one second order filter
92 * which can be used by eq_filter(). these resulting coefficients have a
93 * really wide numerical range, so we use a fixed point format which will
94 * work for the selected cutoff frequencies (in dsp.c) only.
96 const int32_t b0 = c_ls[0], b1 = c_ls[1], b2 = c_hs[0], b3 = c_hs[1];
97 const int32_t a0 = c_ls[2], a1 = c_hs[2];
98 *c++ = FRACMUL_SHL(b0, b2, 4);
99 *c++ = FRACMUL_SHL(b0, b3, 4) + FRACMUL_SHL(b1, b2, 4);
100 *c++ = FRACMUL_SHL(b1, b3, 4);
101 *c++ = a0 + a1;
102 *c++ = -FRACMUL_SHL(a0, a1, 4);
104 #endif
106 /* Coef calculation taken from Audio-EQ-Cookbook.txt by Robert Bristow-Johnson.
107 * Slightly faster calculation can be done by deriving forms which use tan()
108 * instead of cos() and sin(), but the latter are far easier to use when doing
109 * fixed point math, and performance is not a big point in the calculation part.
110 * All the 'a' filter coefficients are negated so we can use only additions
111 * in the filtering equation.
114 /**
115 * Calculate second order section peaking filter coefficients.
116 * @param cutoff a value from 0 to 0x80000000, where 0 represents 0 Hz and
117 * 0x80000000 represents the Nyquist frequency (samplerate/2).
118 * @param Q Q factor value multiplied by ten. Lower bound is artificially set
119 * at 0.5.
120 * @param db decibel value multiplied by ten, describing gain/attenuation at
121 * peak freq. Max value is 24 dB.
122 * @param c pointer to coefficient storage. Coefficients are s3.28 format.
124 void eq_pk_coefs(unsigned long cutoff, unsigned long Q, long db, int32_t *c)
126 long cs;
127 const long one = 1 << 28; /* s3.28 */
128 const long A = get_replaygain_int(db*5) << 5; /* 10^(db/40), s2.29 */
129 const long alpha = fp_sincos(cutoff, &cs)/(2*Q)*10 >> 1; /* s1.30 */
130 int32_t a0, a1, a2; /* these are all s3.28 format */
131 int32_t b0, b1, b2;
132 const long alphadivA = fp_div(alpha, A, 27);
134 /* possible numerical ranges are in comments by each coef */
135 b0 = one + FRACMUL(alpha, A); /* [1 .. 5] */
136 b1 = a1 = -2*(cs >> 3); /* [-2 .. 2] */
137 b2 = one - FRACMUL(alpha, A); /* [-3 .. 1] */
138 a0 = one + alphadivA; /* [1 .. 5] */
139 a2 = one - alphadivA; /* [-3 .. 1] */
141 /* range of this is roughly [0.2 .. 1], but we'll never hit 1 completely */
142 const long rcp_a0 = fp_div(1, a0, 59); /* s0.31 */
143 *c++ = FRACMUL(b0, rcp_a0); /* [0.25 .. 4] */
144 *c++ = FRACMUL(b1, rcp_a0); /* [-2 .. 2] */
145 *c++ = FRACMUL(b2, rcp_a0); /* [-2.4 .. 1] */
146 *c++ = FRACMUL(-a1, rcp_a0); /* [-2 .. 2] */
147 *c++ = FRACMUL(-a2, rcp_a0); /* [-0.6 .. 1] */
151 * Calculate coefficients for lowshelf filter. Parameters are as for
152 * eq_pk_coefs, but the coefficient format is s5.26 fixed point.
154 void eq_ls_coefs(unsigned long cutoff, unsigned long Q, long db, int32_t *c)
156 long cs;
157 const long one = 1 << 25; /* s6.25 */
158 const long sqrtA = get_replaygain_int(db*5/2) << 2; /* 10^(db/80), s5.26 */
159 const long A = FRACMUL_SHL(sqrtA, sqrtA, 8); /* s2.29 */
160 const long alpha = fp_sincos(cutoff, &cs)/(2*Q)*10 >> 1; /* s1.30 */
161 const long ap1 = (A >> 4) + one;
162 const long am1 = (A >> 4) - one;
163 const long twosqrtalpha = 2*FRACMUL(sqrtA, alpha);
164 int32_t a0, a1, a2; /* these are all s6.25 format */
165 int32_t b0, b1, b2;
167 /* [0.1 .. 40] */
168 b0 = FRACMUL_SHL(A, ap1 - FRACMUL(am1, cs) + twosqrtalpha, 2);
169 /* [-16 .. 63.4] */
170 b1 = FRACMUL_SHL(A, am1 - FRACMUL(ap1, cs), 3);
171 /* [0 .. 31.7] */
172 b2 = FRACMUL_SHL(A, ap1 - FRACMUL(am1, cs) - twosqrtalpha, 2);
173 /* [0.5 .. 10] */
174 a0 = ap1 + FRACMUL(am1, cs) + twosqrtalpha;
175 /* [-16 .. 4] */
176 a1 = -2*((am1 + FRACMUL(ap1, cs)));
177 /* [0 .. 8] */
178 a2 = ap1 + FRACMUL(am1, cs) - twosqrtalpha;
180 /* [0.1 .. 1.99] */
181 const long rcp_a0 = fp_div(1, a0, 55); /* s1.30 */
182 *c++ = FRACMUL_SHL(b0, rcp_a0, 2); /* [0.06 .. 15.9] */
183 *c++ = FRACMUL_SHL(b1, rcp_a0, 2); /* [-2 .. 31.7] */
184 *c++ = FRACMUL_SHL(b2, rcp_a0, 2); /* [0 .. 15.9] */
185 *c++ = FRACMUL_SHL(-a1, rcp_a0, 2); /* [-2 .. 2] */
186 *c++ = FRACMUL_SHL(-a2, rcp_a0, 2); /* [0 .. 1] */
190 * Calculate coefficients for highshelf filter. Parameters are as for
191 * eq_pk_coefs, but the coefficient format is s5.26 fixed point.
193 void eq_hs_coefs(unsigned long cutoff, unsigned long Q, long db, int32_t *c)
195 long cs;
196 const long one = 1 << 25; /* s6.25 */
197 const long sqrtA = get_replaygain_int(db*5/2) << 2; /* 10^(db/80), s5.26 */
198 const long A = FRACMUL_SHL(sqrtA, sqrtA, 8); /* s2.29 */
199 const long alpha = fp_sincos(cutoff, &cs)/(2*Q)*10 >> 1; /* s1.30 */
200 const long ap1 = (A >> 4) + one;
201 const long am1 = (A >> 4) - one;
202 const long twosqrtalpha = 2*FRACMUL(sqrtA, alpha);
203 int32_t a0, a1, a2; /* these are all s6.25 format */
204 int32_t b0, b1, b2;
206 /* [0.1 .. 40] */
207 b0 = FRACMUL_SHL(A, ap1 + FRACMUL(am1, cs) + twosqrtalpha, 2);
208 /* [-63.5 .. 16] */
209 b1 = -FRACMUL_SHL(A, am1 + FRACMUL(ap1, cs), 3);
210 /* [0 .. 32] */
211 b2 = FRACMUL_SHL(A, ap1 + FRACMUL(am1, cs) - twosqrtalpha, 2);
212 /* [0.5 .. 10] */
213 a0 = ap1 - FRACMUL(am1, cs) + twosqrtalpha;
214 /* [-4 .. 16] */
215 a1 = 2*((am1 - FRACMUL(ap1, cs)));
216 /* [0 .. 8] */
217 a2 = ap1 - FRACMUL(am1, cs) - twosqrtalpha;
219 /* [0.1 .. 1.99] */
220 const long rcp_a0 = fp_div(1, a0, 55); /* s1.30 */
221 *c++ = FRACMUL_SHL(b0, rcp_a0, 2); /* [0 .. 16] */
222 *c++ = FRACMUL_SHL(b1, rcp_a0, 2); /* [-31.7 .. 2] */
223 *c++ = FRACMUL_SHL(b2, rcp_a0, 2); /* [0 .. 16] */
224 *c++ = FRACMUL_SHL(-a1, rcp_a0, 2); /* [-2 .. 2] */
225 *c++ = FRACMUL_SHL(-a2, rcp_a0, 2); /* [0 .. 1] */
228 /* We realise the filters as a second order direct form 1 structure. Direct
229 * form 1 was chosen because of better numerical properties for fixed point
230 * implementations.
233 #if (!defined(CPU_COLDFIRE) && !defined(CPU_ARM))
234 void eq_filter(int32_t **x, struct eqfilter *f, unsigned num,
235 unsigned channels, unsigned shift)
237 unsigned c, i;
238 long long acc;
240 /* Direct form 1 filtering code.
241 y[n] = b0*x[i] + b1*x[i - 1] + b2*x[i - 2] + a1*y[i - 1] + a2*y[i - 2],
242 where y[] is output and x[] is input.
245 for (c = 0; c < channels; c++) {
246 for (i = 0; i < num; i++) {
247 acc = (long long) x[c][i] * f->coefs[0];
248 acc += (long long) f->history[c][0] * f->coefs[1];
249 acc += (long long) f->history[c][1] * f->coefs[2];
250 acc += (long long) f->history[c][2] * f->coefs[3];
251 acc += (long long) f->history[c][3] * f->coefs[4];
252 f->history[c][1] = f->history[c][0];
253 f->history[c][0] = x[c][i];
254 f->history[c][3] = f->history[c][2];
255 x[c][i] = (acc << shift) >> 32;
256 f->history[c][2] = x[c][i];
260 #endif