1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (iir.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <retro_miscellaneous.h>
28 #include <libretro_dspfilter.h>
29 #include <string/stdstring.h>
31 #define sqr(a) ((a) * (a))
36 LPF, /* low pass filter */
37 HPF, /* High pass filter */
38 BPCSGF, /* band pass filter 1 */
39 BPZPGF, /* band pass filter 2 */
40 APF, /* Allpass filter*/
41 NOTCH, /* Notch Filter */
42 RIAA_phono, /* RIAA record/tape deemphasis */
43 PEQ, /* Peaking band EQ filter */
44 BBOOST, /* Bassboost filter */
45 LSH, /* Low shelf filter */
46 HSH, /* High shelf filter */
47 RIAA_CD /* CD de-emphasis */
62 static void iir_free(void *data)
67 static void iir_process(void *data, struct dspfilter_output *output,
68 const struct dspfilter_input *input)
71 struct iir_data *iir = (struct iir_data*)data;
72 float *out = output->samples;
81 float xn1_l = iir->l.xn1;
82 float xn2_l = iir->l.xn2;
83 float yn1_l = iir->l.yn1;
84 float yn2_l = iir->l.yn2;
86 float xn1_r = iir->r.xn1;
87 float xn2_r = iir->r.xn2;
88 float yn1_r = iir->r.yn1;
89 float yn2_r = iir->r.yn2;
91 output->samples = input->samples;
92 output->frames = input->frames;
94 for (i = 0; i < input->frames; i++, out += 2)
99 float l = (b0 * in_l + b1 * xn1_l + b2 * xn2_l - a1 * yn1_l - a2 * yn2_l) / a0;
100 float r = (b0 * in_r + b1 * xn1_r + b2 * xn2_r - a1 * yn1_r - a2 * yn2_r) / a0;
127 #define CHECK(x) if (string_is_equal(str, #x)) return x
128 static enum IIRFilter str_to_type(const char *str)
143 return LPF; /* Fallback. */
146 static void make_poly_from_roots(
147 const double *roots, unsigned num_roots, float *poly)
153 memset(poly + 2, 0, (num_roots + 1 - 2) * sizeof(*poly));
155 for (i = 1; i < num_roots; i++)
156 for (j = num_roots; j > 0; j--)
157 poly[j] -= poly[j - 1] * roots[i];
160 static void iir_filter_init(struct iir_data *iir,
161 float sample_rate, float freq, float qual, float gain, enum IIRFilter filter_type)
163 double omega = 2.0 * M_PI * freq / sample_rate;
164 double cs = cos(omega);
165 double sn = sin(omega);
166 double a1pha = sn / (2.0 * qual);
167 double A = exp(log(10.0) * gain / 40.0);
168 double beta = sqrt(A + A);
170 float b0 = 0.0, b1 = 0.0, b2 = 0.0, a0 = 0.0, a1 = 0.0, a2 = 0.0;
172 /* Set up filter coefficients according to type */
176 b0 = (1.0 - cs) / 2.0;
178 b2 = (1.0 - cs) / 2.0;
184 b0 = (1.0 + cs) / 2.0;
186 b2 = (1.0 + cs) / 2.0;
223 case RIAA_phono: /* http://www.dsprelated.com/showmessage/73300/3.php */
225 double y, b_re, a_re, b_im, a_im, g;
229 if ((int)sample_rate == 44100)
231 static const double zeros[] = {-0.2014898, 0.9233820};
232 static const double poles[] = {0.7083149, 0.9924091};
233 make_poly_from_roots(zeros, 2, b);
234 make_poly_from_roots(poles, 2, a);
236 else if ((int)sample_rate == 48000)
238 static const double zeros[] = {-0.1766069, 0.9321590};
239 static const double poles[] = {0.7396325, 0.9931330};
240 make_poly_from_roots(zeros, 2, b);
241 make_poly_from_roots(poles, 2, a);
243 else if ((int)sample_rate == 88200)
245 static const double zeros[] = {-0.1168735, 0.9648312};
246 static const double poles[] = {0.8590646, 0.9964002};
247 make_poly_from_roots(zeros, 2, b);
248 make_poly_from_roots(poles, 2, a);
250 else if ((int)sample_rate == 96000)
252 static const double zeros[] = {-0.1141486, 0.9676817};
253 static const double poles[] = {0.8699137, 0.9966946};
254 make_poly_from_roots(zeros, 2, b);
255 make_poly_from_roots(poles, 2, a);
265 /* Normalise to 0dB at 1kHz (Thanks to Glenn Davis) */
266 y = 2.0 * M_PI * 1000.0 / sample_rate;
267 b_re = b0 + b1 * cos(-y) + b2 * cos(-2.0 * y);
268 a_re = a0 + a1 * cos(-y) + a2 * cos(-2.0 * y);
269 b_im = b1 * sin(-y) + b2 * sin(-2.0 * y);
270 a_im = a1 * sin(-y) + a2 * sin(-2.0 * y);
271 g = 1.0 / sqrt((sqr(b_re) + sqr(b_im)) / (sqr(a_re) + sqr(a_im)));
272 b0 *= g; b1 *= g; b2 *= g;
276 b0 = 1.0 + a1pha * A;
278 b2 = 1.0 - a1pha * A;
279 a0 = 1.0 + a1pha / A;
281 a2 = 1.0 - a1pha / A;
284 beta = sqrt((A * A + 1) / 1.0 - (pow((A - 1), 2)));
285 b0 = A * ((A + 1) - (A - 1) * cs + beta * sn);
286 b1 = 2 * A * ((A - 1) - (A + 1) * cs);
287 b2 = A * ((A + 1) - (A - 1) * cs - beta * sn);
288 a0 = ((A + 1) + (A - 1) * cs + beta * sn);
289 a1 = -2 * ((A - 1) + (A + 1) * cs);
290 a2 = (A + 1) + (A - 1) * cs - beta * sn;
293 b0 = A * ((A + 1) - (A - 1) * cs + beta * sn);
294 b1 = 2 * A * ((A - 1) - (A + 1) * cs);
295 b2 = A * ((A + 1) - (A - 1) * cs - beta * sn);
296 a0 = (A + 1) + (A - 1) * cs + beta * sn;
297 a1 = -2 * ((A - 1) + (A + 1) * cs);
298 a2 = (A + 1) + (A - 1) * cs - beta * sn;
301 omega = 2.0 * M_PI * 5283.0 / sample_rate;
304 a1pha = sn / (2.0 * 0.4845);
305 A = exp(log(10.0) * -9.477 / 40.0);
309 b0 = A * ((A + 1.0) + (A - 1.0) * cs + beta * sn);
310 b1 = -2.0 * A * ((A - 1.0) + (A + 1.0) * cs);
311 b2 = A * ((A + 1.0) + (A - 1.0) * cs - beta * sn);
312 a0 = (A + 1.0) - (A - 1.0) * cs + beta * sn;
313 a1 = 2.0 * ((A - 1.0) - (A + 1.0) * cs);
314 a2 = (A + 1.0) - (A - 1.0) * cs - beta * sn;
328 static void *iir_init(const struct dspfilter_info *info,
329 const struct dspfilter_config *config, void *userdata)
331 float freq, qual, gain;
332 enum IIRFilter filter = LPF;
334 struct iir_data *iir = (struct iir_data*)calloc(1, sizeof(*iir));
338 config->get_float(userdata, "frequency", &freq, 1024.0f);
339 config->get_float(userdata, "quality", &qual, 0.707f);
340 config->get_float(userdata, "gain", &gain, 0.0f);
342 config->get_string(userdata, "type", &type, "LPF");
344 filter = str_to_type(type);
347 iir_filter_init(iir, info->input_rate, freq, qual, gain, filter);
351 static const struct dspfilter_implementation iir_plug = {
356 DSPFILTER_API_VERSION,
361 #ifdef HAVE_FILTERS_BUILTIN
362 #define dspfilter_get_implementation iir_dspfilter_get_implementation
365 const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
370 #undef dspfilter_get_implementation