1 /*
2 * GStreamer
3 * Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /*
22 * Chebyshev type 1 filter design based on
23 * "The Scientist and Engineer's Guide to DSP", Chapter 20.
24 * http://www.dspguide.com/
25 *
26 * For type 2 and Chebyshev filters in general read
27 * http://en.wikipedia.org/wiki/Chebyshev_filter
28 *
29 * Transformation from lowpass to bandpass/bandreject:
30 * http://docs.dewresearch.com/DspHelp/html/IDH_LinearSystems_LowpassToBandPassZ.htm
31 * http://docs.dewresearch.com/DspHelp/html/IDH_LinearSystems_LowpassToBandStopZ.htm
32 *
33 */
34
35 /**
36 * SECTION:element-audiochebband
37 *
38 * Attenuates all frequencies outside (bandpass) or inside (bandreject) of a frequency
39 * band. The number of poles and the ripple parameter control the rolloff.
40 *
41 * This element has the advantage over the windowed sinc bandpass and bandreject filter that it is
42 * much faster and produces almost as good results. It's only disadvantages are the highly
43 * non-linear phase and the slower rolloff compared to a windowed sinc filter with a large kernel.
44 *
45 * For type 1 the ripple parameter specifies how much ripple in dB is allowed in the passband, i.e.
46 * some frequencies in the passband will be amplified by that value. A higher ripple value will allow
47 * a faster rolloff.
48 *
49 * For type 2 the ripple parameter specifies the stopband attenuation. In the stopband the gain will
50 * be at most this value. A lower ripple value will allow a faster rolloff.
51 *
52 * As a special case, a Chebyshev type 1 filter with no ripple is a Butterworth filter.
53 *
54 * <note>
55 * Be warned that a too large number of poles can produce noise. The most poles are possible with
56 * a cutoff frequency at a quarter of the sampling rate.
57 * </note>
58 *
59 * <refsect2>
60 * <title>Example launch line</title>
61 * |[
62 * gst-launch-1.0 audiotestsrc freq=1500 ! audioconvert ! audiochebband mode=band-pass lower-frequency=1000 upper-frequency=6000 poles=4 ! audioconvert ! alsasink
63 * gst-launch-1.0 filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiochebband mode=band-reject lower-frequency=1000 upper-frequency=4000 ripple=0.2 ! audioconvert ! alsasink
64 * gst-launch-1.0 audiotestsrc wave=white-noise ! audioconvert ! audiochebband mode=band-pass lower-frequency=1000 upper-frequency=4000 type=2 ! audioconvert ! alsasink
65 * ]|
66 * </refsect2>
67 */
68
69 #ifdef HAVE_CONFIG_H
70 #include "config.h"
71 #endif
72
73 #include <string.h>
74
75 #include <gst/gst.h>
76 #include <gst/base/gstbasetransform.h>
77 #include <gst/audio/audio.h>
78 #include <gst/audio/gstaudiofilter.h>
79
80 #include <math.h>
81
82 #include "math_compat.h"
83
84 #include "audiochebband.h"
85
86 #include "gst/glib-compat-private.h"
87
88 #define GST_CAT_DEFAULT gst_audio_cheb_band_debug
89 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
90
91 enum
92 {
93 PROP_0,
94 PROP_MODE,
95 PROP_TYPE,
96 PROP_LOWER_FREQUENCY,
97 PROP_UPPER_FREQUENCY,
98 PROP_RIPPLE,
99 PROP_POLES
100 };
101
102 #define gst_audio_cheb_band_parent_class parent_class
103 G_DEFINE_TYPE (GstAudioChebBand, gst_audio_cheb_band,
104 GST_TYPE_AUDIO_FX_BASE_IIR_FILTER);
105
106 static void gst_audio_cheb_band_set_property (GObject * object,
107 guint prop_id, const GValue * value, GParamSpec * pspec);
108 static void gst_audio_cheb_band_get_property (GObject * object,
109 guint prop_id, GValue * value, GParamSpec * pspec);
110 static void gst_audio_cheb_band_finalize (GObject * object);
111
112 static gboolean gst_audio_cheb_band_setup (GstAudioFilter * filter,
113 const GstAudioInfo * info);
114
115 enum
116 {
117 MODE_BAND_PASS = 0,
118 MODE_BAND_REJECT
119 };
120
121 #define GST_TYPE_AUDIO_CHEBYSHEV_FREQ_BAND_MODE (gst_audio_cheb_band_mode_get_type ())
122 static GType
gst_audio_cheb_band_mode_get_type(void)123 gst_audio_cheb_band_mode_get_type (void)
124 {
125 static GType gtype = 0;
126
127 if (gtype == 0) {
128 static const GEnumValue values[] = {
129 {MODE_BAND_PASS, "Band pass (default)",
130 "band-pass"},
131 {MODE_BAND_REJECT, "Band reject",
132 "band-reject"},
133 {0, NULL, NULL}
134 };
135
136 gtype = g_enum_register_static ("GstAudioChebBandMode", values);
137 }
138 return gtype;
139 }
140
141 /* GObject vmethod implementations */
142
143 static void
gst_audio_cheb_band_class_init(GstAudioChebBandClass * klass)144 gst_audio_cheb_band_class_init (GstAudioChebBandClass * klass)
145 {
146 GObjectClass *gobject_class = (GObjectClass *) klass;
147 GstElementClass *gstelement_class = (GstElementClass *) klass;
148 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
149
150 GST_DEBUG_CATEGORY_INIT (gst_audio_cheb_band_debug, "audiochebband", 0,
151 "audiochebband element");
152
153 gobject_class->set_property = gst_audio_cheb_band_set_property;
154 gobject_class->get_property = gst_audio_cheb_band_get_property;
155 gobject_class->finalize = gst_audio_cheb_band_finalize;
156
157 g_object_class_install_property (gobject_class, PROP_MODE,
158 g_param_spec_enum ("mode", "Mode",
159 "Low pass or high pass mode", GST_TYPE_AUDIO_CHEBYSHEV_FREQ_BAND_MODE,
160 MODE_BAND_PASS,
161 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
162 g_object_class_install_property (gobject_class, PROP_TYPE,
163 g_param_spec_int ("type", "Type", "Type of the chebychev filter", 1, 2, 1,
164 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
165
166 /* FIXME: Don't use the complete possible range but restrict the upper boundary
167 * so automatically generated UIs can use a slider without */
168 g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
169 g_param_spec_float ("lower-frequency", "Lower frequency",
170 "Start frequency of the band (Hz)", 0.0, 100000.0,
171 0.0,
172 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
173 g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
174 g_param_spec_float ("upper-frequency", "Upper frequency",
175 "Stop frequency of the band (Hz)", 0.0, 100000.0, 0.0,
176 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (gobject_class, PROP_RIPPLE,
178 g_param_spec_float ("ripple", "Ripple", "Amount of ripple (dB)", 0.0,
179 200.0, 0.25,
180 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
181 /* FIXME: What to do about this upper boundary? With a frequencies near
182 * rate/4 32 poles are completely possible, with frequencies very low
183 * or very high 16 poles already produces only noise */
184 g_object_class_install_property (gobject_class, PROP_POLES,
185 g_param_spec_int ("poles", "Poles",
186 "Number of poles to use, will be rounded up to the next multiply of four",
187 4, 32, 4,
188 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
189
190 gst_element_class_set_static_metadata (gstelement_class,
191 "Band pass & band reject filter", "Filter/Effect/Audio",
192 "Chebyshev band pass and band reject filter",
193 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
194
195 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_cheb_band_setup);
196 }
197
198 static void
gst_audio_cheb_band_init(GstAudioChebBand * filter)199 gst_audio_cheb_band_init (GstAudioChebBand * filter)
200 {
201 filter->lower_frequency = filter->upper_frequency = 0.0;
202 filter->mode = MODE_BAND_PASS;
203 filter->type = 1;
204 filter->poles = 4;
205 filter->ripple = 0.25;
206
207 g_mutex_init (&filter->lock);
208 }
209
210 static void
generate_biquad_coefficients(GstAudioChebBand * filter,gint p,gint rate,gdouble * b0,gdouble * b1,gdouble * b2,gdouble * b3,gdouble * b4,gdouble * a1,gdouble * a2,gdouble * a3,gdouble * a4)211 generate_biquad_coefficients (GstAudioChebBand * filter,
212 gint p, gint rate, gdouble * b0, gdouble * b1, gdouble * b2, gdouble * b3,
213 gdouble * b4, gdouble * a1, gdouble * a2, gdouble * a3, gdouble * a4)
214 {
215 gint np = filter->poles / 2;
216 gdouble ripple = filter->ripple;
217
218 /* pole location in s-plane */
219 gdouble rp, ip;
220
221 /* zero location in s-plane */
222 gdouble iz = 0.0;
223
224 /* transfer function coefficients for the z-plane */
225 gdouble x0, x1, x2, y1, y2;
226 gint type = filter->type;
227
228 /* Calculate pole location for lowpass at frequency 1 */
229 {
230 gdouble angle = (G_PI / 2.0) * (2.0 * p - 1) / np;
231
232 rp = -sin (angle);
233 ip = cos (angle);
234 }
235
236 /* If we allow ripple, move the pole from the unit
237 * circle to an ellipse and keep cutoff at frequency 1 */
238 if (ripple > 0 && type == 1) {
239 gdouble es, vx;
240
241 es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
242
243 vx = (1.0 / np) * asinh (1.0 / es);
244 rp = rp * sinh (vx);
245 ip = ip * cosh (vx);
246 } else if (type == 2) {
247 gdouble es, vx;
248
249 es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
250 vx = (1.0 / np) * asinh (es);
251 rp = rp * sinh (vx);
252 ip = ip * cosh (vx);
253 }
254
255 /* Calculate inverse of the pole location to move from
256 * type I to type II */
257 if (type == 2) {
258 gdouble mag2 = rp * rp + ip * ip;
259
260 rp /= mag2;
261 ip /= mag2;
262 }
263
264 /* Calculate zero location for frequency 1 on the
265 * unit circle for type 2 */
266 if (type == 2) {
267 gdouble angle = G_PI / (np * 2.0) + ((p - 1) * G_PI) / (np);
268 gdouble mag2;
269
270 iz = cos (angle);
271 mag2 = iz * iz;
272 iz /= mag2;
273 }
274
275 /* Convert from s-domain to z-domain by
276 * using the bilinear Z-transform, i.e.
277 * substitute s by (2/t)*((z-1)/(z+1))
278 * with t = 2 * tan(0.5).
279 */
280 if (type == 1) {
281 gdouble t, m, d;
282
283 t = 2.0 * tan (0.5);
284 m = rp * rp + ip * ip;
285 d = 4.0 - 4.0 * rp * t + m * t * t;
286
287 x0 = (t * t) / d;
288 x1 = 2.0 * x0;
289 x2 = x0;
290 y1 = (8.0 - 2.0 * m * t * t) / d;
291 y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
292 } else {
293 gdouble t, m, d;
294
295 t = 2.0 * tan (0.5);
296 m = rp * rp + ip * ip;
297 d = 4.0 - 4.0 * rp * t + m * t * t;
298
299 x0 = (t * t * iz * iz + 4.0) / d;
300 x1 = (-8.0 + 2.0 * iz * iz * t * t) / d;
301 x2 = x0;
302 y1 = (8.0 - 2.0 * m * t * t) / d;
303 y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
304 }
305
306 /* Convert from lowpass at frequency 1 to either bandpass
307 * or band reject.
308 *
309 * For bandpass substitute z^(-1) with:
310 *
311 * -2 -1
312 * -z + alpha * z - beta
313 * ----------------------------
314 * -2 -1
315 * beta * z - alpha * z + 1
316 *
317 * alpha = (2*a*b)/(1+b)
318 * beta = (b-1)/(b+1)
319 * a = cos((w1 + w0)/2) / cos((w1 - w0)/2)
320 * b = tan(1/2) * cot((w1 - w0)/2)
321 *
322 * For bandreject substitute z^(-1) with:
323 *
324 * -2 -1
325 * z - alpha * z + beta
326 * ----------------------------
327 * -2 -1
328 * beta * z - alpha * z + 1
329 *
330 * alpha = (2*a)/(1+b)
331 * beta = (1-b)/(1+b)
332 * a = cos((w1 + w0)/2) / cos((w1 - w0)/2)
333 * b = tan(1/2) * tan((w1 - w0)/2)
334 *
335 */
336 {
337 gdouble a, b, d;
338 gdouble alpha, beta;
339 gdouble w0 = 2.0 * G_PI * (filter->lower_frequency / rate);
340 gdouble w1 = 2.0 * G_PI * (filter->upper_frequency / rate);
341
342 if (filter->mode == MODE_BAND_PASS) {
343 a = cos ((w1 + w0) / 2.0) / cos ((w1 - w0) / 2.0);
344 b = tan (1.0 / 2.0) / tan ((w1 - w0) / 2.0);
345
346 alpha = (2.0 * a * b) / (1.0 + b);
347 beta = (b - 1.0) / (b + 1.0);
348
349 d = 1.0 + beta * (y1 - beta * y2);
350
351 *b0 = (x0 + beta * (-x1 + beta * x2)) / d;
352 *b1 = (alpha * (-2.0 * x0 + x1 + beta * x1 - 2.0 * beta * x2)) / d;
353 *b2 =
354 (-x1 - beta * beta * x1 + 2.0 * beta * (x0 + x2) +
355 alpha * alpha * (x0 - x1 + x2)) / d;
356 *b3 = (alpha * (x1 + beta * (-2.0 * x0 + x1) - 2.0 * x2)) / d;
357 *b4 = (beta * (beta * x0 - x1) + x2) / d;
358 *a1 = (alpha * (2.0 + y1 + beta * y1 - 2.0 * beta * y2)) / d;
359 *a2 =
360 (-y1 - beta * beta * y1 - alpha * alpha * (1.0 + y1 - y2) +
361 2.0 * beta * (-1.0 + y2)) / d;
362 *a3 = (alpha * (y1 + beta * (2.0 + y1) - 2.0 * y2)) / d;
363 *a4 = (-beta * beta - beta * y1 + y2) / d;
364 } else {
365 a = cos ((w1 + w0) / 2.0) / cos ((w1 - w0) / 2.0);
366 b = tan (1.0 / 2.0) * tan ((w1 - w0) / 2.0);
367
368 alpha = (2.0 * a) / (1.0 + b);
369 beta = (1.0 - b) / (1.0 + b);
370
371 d = -1.0 + beta * (beta * y2 + y1);
372
373 *b0 = (-x0 - beta * x1 - beta * beta * x2) / d;
374 *b1 = (alpha * (2.0 * x0 + x1 + beta * x1 + 2.0 * beta * x2)) / d;
375 *b2 =
376 (-x1 - beta * beta * x1 - 2.0 * beta * (x0 + x2) -
377 alpha * alpha * (x0 + x1 + x2)) / d;
378 *b3 = (alpha * (x1 + beta * (2.0 * x0 + x1) + 2.0 * x2)) / d;
379 *b4 = (-beta * beta * x0 - beta * x1 - x2) / d;
380 *a1 = (alpha * (-2.0 + y1 + beta * y1 + 2.0 * beta * y2)) / d;
381 *a2 =
382 -(y1 + beta * beta * y1 + 2.0 * beta * (-1.0 + y2) +
383 alpha * alpha * (-1.0 + y1 + y2)) / d;
384 *a3 = (alpha * (beta * (-2.0 + y1) + y1 + 2.0 * y2)) / d;
385 *a4 = -(-beta * beta + beta * y1 + y2) / d;
386 }
387 }
388 }
389
390 static void
generate_coefficients(GstAudioChebBand * filter,const GstAudioInfo * info)391 generate_coefficients (GstAudioChebBand * filter, const GstAudioInfo * info)
392 {
393 gint rate;
394
395 if (info) {
396 rate = GST_AUDIO_INFO_RATE (info);
397 } else {
398 rate = GST_AUDIO_FILTER_RATE (filter);
399 }
400
401 if (rate == 0) {
402 gdouble *a = g_new0 (gdouble, 1);
403 gdouble *b = g_new0 (gdouble, 1);
404
405 a[0] = 1.0;
406 b[0] = 1.0;
407 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
408 (filter), a, 1, b, 1);
409 GST_LOG_OBJECT (filter, "rate was not set yet");
410 return;
411 }
412
413 if (filter->upper_frequency <= filter->lower_frequency) {
414 gdouble *a = g_new0 (gdouble, 1);
415 gdouble *b = g_new0 (gdouble, 1);
416
417 a[0] = 1.0;
418 b[0] = (filter->mode == MODE_BAND_PASS) ? 0.0 : 1.0;
419 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
420 (filter), a, 1, b, 1);
421
422 GST_LOG_OBJECT (filter, "frequency band had no or negative dimension");
423 return;
424 }
425
426 if (filter->upper_frequency > rate / 2) {
427 filter->upper_frequency = rate / 2;
428 GST_LOG_OBJECT (filter, "clipped upper frequency to nyquist frequency");
429 }
430
431 if (filter->lower_frequency < 0.0) {
432 filter->lower_frequency = 0.0;
433 GST_LOG_OBJECT (filter, "clipped lower frequency to 0.0");
434 }
435
436 /* Calculate coefficients for the chebyshev filter */
437 {
438 gint np = filter->poles;
439 gdouble *a, *b;
440 gint i, p;
441
442 a = g_new0 (gdouble, np + 5);
443 b = g_new0 (gdouble, np + 5);
444
445 /* Calculate transfer function coefficients */
446 a[4] = 1.0;
447 b[4] = 1.0;
448
449 for (p = 1; p <= np / 4; p++) {
450 gdouble b0, b1, b2, b3, b4, a1, a2, a3, a4;
451 gdouble *ta = g_new0 (gdouble, np + 5);
452 gdouble *tb = g_new0 (gdouble, np + 5);
453
454 generate_biquad_coefficients (filter, p, rate,
455 &b0, &b1, &b2, &b3, &b4, &a1, &a2, &a3, &a4);
456
457 memcpy (ta, a, sizeof (gdouble) * (np + 5));
458 memcpy (tb, b, sizeof (gdouble) * (np + 5));
459
460 /* add the new coefficients for the new two poles
461 * to the cascade by multiplication of the transfer
462 * functions */
463 for (i = 4; i < np + 5; i++) {
464 b[i] =
465 b0 * tb[i] + b1 * tb[i - 1] + b2 * tb[i - 2] + b3 * tb[i - 3] +
466 b4 * tb[i - 4];
467 a[i] =
468 ta[i] - a1 * ta[i - 1] - a2 * ta[i - 2] - a3 * ta[i - 3] -
469 a4 * ta[i - 4];
470 }
471 g_free (ta);
472 g_free (tb);
473 }
474
475 /* Move coefficients to the beginning of the array to move from
476 * the transfer function's coefficients to the difference
477 * equation's coefficients */
478 for (i = 0; i <= np; i++) {
479 a[i] = a[i + 4];
480 b[i] = b[i + 4];
481 }
482
483 /* Normalize to unity gain at frequency 0 and frequency
484 * 0.5 for bandreject and unity gain at band center frequency
485 * for bandpass */
486 if (filter->mode == MODE_BAND_REJECT) {
487 /* gain is sqrt(H(0)*H(0.5)) */
488
489 gdouble gain1 =
490 gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1,
491 1.0, 0.0);
492 gdouble gain2 =
493 gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1,
494 -1.0, 0.0);
495
496 gain1 = sqrt (gain1 * gain2);
497
498 for (i = 0; i <= np; i++) {
499 b[i] /= gain1;
500 }
501 } else {
502 /* gain is H(wc), wc = center frequency */
503
504 gdouble w1 = 2.0 * G_PI * (filter->lower_frequency / rate);
505 gdouble w2 = 2.0 * G_PI * (filter->upper_frequency / rate);
506 gdouble w0 = (w2 + w1) / 2.0;
507 gdouble zr = cos (w0), zi = sin (w0);
508 gdouble gain =
509 gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1, zr,
510 zi);
511
512 for (i = 0; i <= np; i++) {
513 b[i] /= gain;
514 }
515 }
516
517 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
518 (filter), a, np + 1, b, np + 1);
519
520 GST_LOG_OBJECT (filter,
521 "Generated IIR coefficients for the Chebyshev filter");
522 GST_LOG_OBJECT (filter,
523 "mode: %s, type: %d, poles: %d, lower-frequency: %.2f Hz, upper-frequency: %.2f Hz, ripple: %.2f dB",
524 (filter->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject",
525 filter->type, filter->poles, filter->lower_frequency,
526 filter->upper_frequency, filter->ripple);
527
528 GST_LOG_OBJECT (filter, "%.2f dB gain @ 0Hz",
529 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b,
530 np + 1, 1.0, 0.0)));
531 {
532 gdouble w1 = 2.0 * G_PI * (filter->lower_frequency / rate);
533 gdouble w2 = 2.0 * G_PI * (filter->upper_frequency / rate);
534 gdouble w0 = (w2 + w1) / 2.0;
535 gdouble zr, zi;
536
537 zr = cos (w1);
538 zi = sin (w1);
539 GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
540 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1,
541 b, np + 1, zr, zi)), (int) filter->lower_frequency);
542 zr = cos (w0);
543 zi = sin (w0);
544 GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
545 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1,
546 b, np + 1, zr, zi)),
547 (int) ((filter->lower_frequency + filter->upper_frequency) / 2.0));
548 zr = cos (w2);
549 zi = sin (w2);
550 GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
551 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1,
552 b, np + 1, zr, zi)), (int) filter->upper_frequency);
553 }
554 GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
555 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b,
556 np + 1, -1.0, 0.0)), rate / 2);
557 }
558 }
559
560 static void
gst_audio_cheb_band_finalize(GObject * object)561 gst_audio_cheb_band_finalize (GObject * object)
562 {
563 GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (object);
564
565 g_mutex_clear (&filter->lock);
566
567 G_OBJECT_CLASS (parent_class)->finalize (object);
568 }
569
570 static void
gst_audio_cheb_band_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)571 gst_audio_cheb_band_set_property (GObject * object, guint prop_id,
572 const GValue * value, GParamSpec * pspec)
573 {
574 GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (object);
575
576 switch (prop_id) {
577 case PROP_MODE:
578 g_mutex_lock (&filter->lock);
579 filter->mode = g_value_get_enum (value);
580 generate_coefficients (filter, NULL);
581 g_mutex_unlock (&filter->lock);
582 break;
583 case PROP_TYPE:
584 g_mutex_lock (&filter->lock);
585 filter->type = g_value_get_int (value);
586 generate_coefficients (filter, NULL);
587 g_mutex_unlock (&filter->lock);
588 break;
589 case PROP_LOWER_FREQUENCY:
590 g_mutex_lock (&filter->lock);
591 filter->lower_frequency = g_value_get_float (value);
592 generate_coefficients (filter, NULL);
593 g_mutex_unlock (&filter->lock);
594 break;
595 case PROP_UPPER_FREQUENCY:
596 g_mutex_lock (&filter->lock);
597 filter->upper_frequency = g_value_get_float (value);
598 generate_coefficients (filter, NULL);
599 g_mutex_unlock (&filter->lock);
600 break;
601 case PROP_RIPPLE:
602 g_mutex_lock (&filter->lock);
603 filter->ripple = g_value_get_float (value);
604 generate_coefficients (filter, NULL);
605 g_mutex_unlock (&filter->lock);
606 break;
607 case PROP_POLES:
608 g_mutex_lock (&filter->lock);
609 filter->poles = GST_ROUND_UP_4 (g_value_get_int (value));
610 generate_coefficients (filter, NULL);
611 g_mutex_unlock (&filter->lock);
612 break;
613 default:
614 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
615 break;
616 }
617 }
618
619 static void
gst_audio_cheb_band_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)620 gst_audio_cheb_band_get_property (GObject * object, guint prop_id,
621 GValue * value, GParamSpec * pspec)
622 {
623 GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (object);
624
625 switch (prop_id) {
626 case PROP_MODE:
627 g_value_set_enum (value, filter->mode);
628 break;
629 case PROP_TYPE:
630 g_value_set_int (value, filter->type);
631 break;
632 case PROP_LOWER_FREQUENCY:
633 g_value_set_float (value, filter->lower_frequency);
634 break;
635 case PROP_UPPER_FREQUENCY:
636 g_value_set_float (value, filter->upper_frequency);
637 break;
638 case PROP_RIPPLE:
639 g_value_set_float (value, filter->ripple);
640 break;
641 case PROP_POLES:
642 g_value_set_int (value, filter->poles);
643 break;
644 default:
645 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
646 break;
647 }
648 }
649
650 /* GstAudioFilter vmethod implementations */
651
652 static gboolean
gst_audio_cheb_band_setup(GstAudioFilter * base,const GstAudioInfo * info)653 gst_audio_cheb_band_setup (GstAudioFilter * base, const GstAudioInfo * info)
654 {
655 GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (base);
656
657 generate_coefficients (filter, info);
658
659 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
660 }
661