Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / audiofx / audiochebband.c
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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, 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 audiotestsrc freq=1500 ! audioconvert ! audiochebband mode=band-pass lower-frequency=1000 upper-frequenc=6000 poles=4 ! audioconvert ! alsasink
63  * gst-launch 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 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 <gst/gst.h>
74 #include <gst/base/gstbasetransform.h>
75 #include <gst/audio/audio.h>
76 #include <gst/audio/gstaudiofilter.h>
77 #include <gst/controller/gstcontroller.h>
78
79 #include <math.h>
80
81 #include "math_compat.h"
82
83 #include "audiochebband.h"
84
85 #include "gst/glib-compat-private.h"
86
87 #define GST_CAT_DEFAULT gst_audio_cheb_band_debug
88 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
89
90 enum
91 {
92   PROP_0,
93   PROP_MODE,
94   PROP_TYPE,
95   PROP_LOWER_FREQUENCY,
96   PROP_UPPER_FREQUENCY,
97   PROP_RIPPLE,
98   PROP_POLES
99 };
100
101 #define DEBUG_INIT(bla) \
102   GST_DEBUG_CATEGORY_INIT (gst_audio_cheb_band_debug, "audiochebband", 0, "audiochebband element");
103
104 GST_BOILERPLATE_FULL (GstAudioChebBand, gst_audio_cheb_band,
105     GstAudioFXBaseIIRFilter, GST_TYPE_AUDIO_FX_BASE_IIR_FILTER, DEBUG_INIT);
106
107 static void gst_audio_cheb_band_set_property (GObject * object,
108     guint prop_id, const GValue * value, GParamSpec * pspec);
109 static void gst_audio_cheb_band_get_property (GObject * object,
110     guint prop_id, GValue * value, GParamSpec * pspec);
111 static void gst_audio_cheb_band_finalize (GObject * object);
112
113 static gboolean gst_audio_cheb_band_setup (GstAudioFilter * filter,
114     GstRingBufferSpec * format);
115
116 enum
117 {
118   MODE_BAND_PASS = 0,
119   MODE_BAND_REJECT
120 };
121
122 #define GST_TYPE_AUDIO_CHEBYSHEV_FREQ_BAND_MODE (gst_audio_cheb_band_mode_get_type ())
123 static GType
124 gst_audio_cheb_band_mode_get_type (void)
125 {
126   static GType gtype = 0;
127
128   if (gtype == 0) {
129     static const GEnumValue values[] = {
130       {MODE_BAND_PASS, "Band pass (default)",
131           "band-pass"},
132       {MODE_BAND_REJECT, "Band reject",
133           "band-reject"},
134       {0, NULL, NULL}
135     };
136
137     gtype = g_enum_register_static ("GstAudioChebBandMode", values);
138   }
139   return gtype;
140 }
141
142 /* GObject vmethod implementations */
143
144 static void
145 gst_audio_cheb_band_base_init (gpointer klass)
146 {
147   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
148
149   gst_element_class_set_details_simple (element_class,
150       "Band pass & band reject filter", "Filter/Effect/Audio",
151       "Chebyshev band pass and band reject filter",
152       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
153 }
154
155 static void
156 gst_audio_cheb_band_class_init (GstAudioChebBandClass * klass)
157 {
158   GObjectClass *gobject_class = (GObjectClass *) klass;
159   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
160
161   gobject_class->set_property = gst_audio_cheb_band_set_property;
162   gobject_class->get_property = gst_audio_cheb_band_get_property;
163   gobject_class->finalize = gst_audio_cheb_band_finalize;
164
165   g_object_class_install_property (gobject_class, PROP_MODE,
166       g_param_spec_enum ("mode", "Mode",
167           "Low pass or high pass mode", GST_TYPE_AUDIO_CHEBYSHEV_FREQ_BAND_MODE,
168           MODE_BAND_PASS,
169           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
170   g_object_class_install_property (gobject_class, PROP_TYPE,
171       g_param_spec_int ("type", "Type", "Type of the chebychev filter", 1, 2, 1,
172           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
173
174   /* FIXME: Don't use the complete possible range but restrict the upper boundary
175    * so automatically generated UIs can use a slider without */
176   g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
177       g_param_spec_float ("lower-frequency", "Lower frequency",
178           "Start frequency of the band (Hz)", 0.0, 100000.0,
179           0.0,
180           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
181   g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
182       g_param_spec_float ("upper-frequency", "Upper frequency",
183           "Stop frequency of the band (Hz)", 0.0, 100000.0, 0.0,
184           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
185   g_object_class_install_property (gobject_class, PROP_RIPPLE,
186       g_param_spec_float ("ripple", "Ripple", "Amount of ripple (dB)", 0.0,
187           200.0, 0.25,
188           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
189   /* FIXME: What to do about this upper boundary? With a frequencies near
190    * rate/4 32 poles are completely possible, with frequencies very low
191    * or very high 16 poles already produces only noise */
192   g_object_class_install_property (gobject_class, PROP_POLES,
193       g_param_spec_int ("poles", "Poles",
194           "Number of poles to use, will be rounded up to the next multiply of four",
195           4, 32, 4,
196           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
197
198   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_cheb_band_setup);
199 }
200
201 static void
202 gst_audio_cheb_band_init (GstAudioChebBand * filter,
203     GstAudioChebBandClass * klass)
204 {
205   filter->lower_frequency = filter->upper_frequency = 0.0;
206   filter->mode = MODE_BAND_PASS;
207   filter->type = 1;
208   filter->poles = 4;
209   filter->ripple = 0.25;
210
211   filter->lock = g_mutex_new ();
212 }
213
214 static void
215 generate_biquad_coefficients (GstAudioChebBand * filter,
216     gint p, gdouble * a0, gdouble * a1, gdouble * a2, gdouble * a3,
217     gdouble * a4, gdouble * b1, gdouble * b2, gdouble * b3, gdouble * b4)
218 {
219   gint np = filter->poles / 2;
220   gdouble ripple = filter->ripple;
221
222   /* pole location in s-plane */
223   gdouble rp, ip;
224
225   /* zero location in s-plane */
226   gdouble iz = 0.0;
227
228   /* transfer function coefficients for the z-plane */
229   gdouble x0, x1, x2, y1, y2;
230   gint type = filter->type;
231
232   /* Calculate pole location for lowpass at frequency 1 */
233   {
234     gdouble angle = (G_PI / 2.0) * (2.0 * p - 1) / np;
235
236     rp = -sin (angle);
237     ip = cos (angle);
238   }
239
240   /* If we allow ripple, move the pole from the unit
241    * circle to an ellipse and keep cutoff at frequency 1 */
242   if (ripple > 0 && type == 1) {
243     gdouble es, vx;
244
245     es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
246
247     vx = (1.0 / np) * asinh (1.0 / es);
248     rp = rp * sinh (vx);
249     ip = ip * cosh (vx);
250   } else if (type == 2) {
251     gdouble es, vx;
252
253     es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
254     vx = (1.0 / np) * asinh (es);
255     rp = rp * sinh (vx);
256     ip = ip * cosh (vx);
257   }
258
259   /* Calculate inverse of the pole location to move from
260    * type I to type II */
261   if (type == 2) {
262     gdouble mag2 = rp * rp + ip * ip;
263
264     rp /= mag2;
265     ip /= mag2;
266   }
267
268   /* Calculate zero location for frequency 1 on the
269    * unit circle for type 2 */
270   if (type == 2) {
271     gdouble angle = G_PI / (np * 2.0) + ((p - 1) * G_PI) / (np);
272     gdouble mag2;
273
274     iz = cos (angle);
275     mag2 = iz * iz;
276     iz /= mag2;
277   }
278
279   /* Convert from s-domain to z-domain by
280    * using the bilinear Z-transform, i.e.
281    * substitute s by (2/t)*((z-1)/(z+1))
282    * with t = 2 * tan(0.5).
283    */
284   if (type == 1) {
285     gdouble t, m, d;
286
287     t = 2.0 * tan (0.5);
288     m = rp * rp + ip * ip;
289     d = 4.0 - 4.0 * rp * t + m * t * t;
290
291     x0 = (t * t) / d;
292     x1 = 2.0 * x0;
293     x2 = x0;
294     y1 = (8.0 - 2.0 * m * t * t) / d;
295     y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
296   } else {
297     gdouble t, m, d;
298
299     t = 2.0 * tan (0.5);
300     m = rp * rp + ip * ip;
301     d = 4.0 - 4.0 * rp * t + m * t * t;
302
303     x0 = (t * t * iz * iz + 4.0) / d;
304     x1 = (-8.0 + 2.0 * iz * iz * t * t) / d;
305     x2 = x0;
306     y1 = (8.0 - 2.0 * m * t * t) / d;
307     y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
308   }
309
310   /* Convert from lowpass at frequency 1 to either bandpass
311    * or band reject.
312    *
313    * For bandpass substitute z^(-1) with:
314    *
315    *   -2            -1
316    * -z   + alpha * z   - beta
317    * ----------------------------
318    *         -2            -1
319    * beta * z   - alpha * z   + 1
320    *
321    * alpha = (2*a*b)/(1+b)
322    * beta = (b-1)/(b+1)
323    * a = cos((w1 + w0)/2) / cos((w1 - w0)/2)
324    * b = tan(1/2) * cot((w1 - w0)/2)
325    *
326    * For bandreject substitute z^(-1) with:
327    * 
328    *  -2            -1
329    * z   - alpha * z   + beta
330    * ----------------------------
331    *         -2            -1
332    * beta * z   - alpha * z   + 1
333    *
334    * alpha = (2*a)/(1+b)
335    * beta = (1-b)/(1+b)
336    * a = cos((w1 + w0)/2) / cos((w1 - w0)/2)
337    * b = tan(1/2) * tan((w1 - w0)/2)
338    *
339    */
340   {
341     gdouble a, b, d;
342     gdouble alpha, beta;
343     gdouble w0 =
344         2.0 * G_PI * (filter->lower_frequency /
345         GST_AUDIO_FILTER (filter)->format.rate);
346     gdouble w1 =
347         2.0 * G_PI * (filter->upper_frequency /
348         GST_AUDIO_FILTER (filter)->format.rate);
349
350     if (filter->mode == MODE_BAND_PASS) {
351       a = cos ((w1 + w0) / 2.0) / cos ((w1 - w0) / 2.0);
352       b = tan (1.0 / 2.0) / tan ((w1 - w0) / 2.0);
353
354       alpha = (2.0 * a * b) / (1.0 + b);
355       beta = (b - 1.0) / (b + 1.0);
356
357       d = 1.0 + beta * (y1 - beta * y2);
358
359       *a0 = (x0 + beta * (-x1 + beta * x2)) / d;
360       *a1 = (alpha * (-2.0 * x0 + x1 + beta * x1 - 2.0 * beta * x2)) / d;
361       *a2 =
362           (-x1 - beta * beta * x1 + 2.0 * beta * (x0 + x2) +
363           alpha * alpha * (x0 - x1 + x2)) / d;
364       *a3 = (alpha * (x1 + beta * (-2.0 * x0 + x1) - 2.0 * x2)) / d;
365       *a4 = (beta * (beta * x0 - x1) + x2) / d;
366       *b1 = (alpha * (2.0 + y1 + beta * y1 - 2.0 * beta * y2)) / d;
367       *b2 =
368           (-y1 - beta * beta * y1 - alpha * alpha * (1.0 + y1 - y2) +
369           2.0 * beta * (-1.0 + y2)) / d;
370       *b3 = (alpha * (y1 + beta * (2.0 + y1) - 2.0 * y2)) / d;
371       *b4 = (-beta * beta - beta * y1 + y2) / d;
372     } else {
373       a = cos ((w1 + w0) / 2.0) / cos ((w1 - w0) / 2.0);
374       b = tan (1.0 / 2.0) * tan ((w1 - w0) / 2.0);
375
376       alpha = (2.0 * a) / (1.0 + b);
377       beta = (1.0 - b) / (1.0 + b);
378
379       d = -1.0 + beta * (beta * y2 + y1);
380
381       *a0 = (-x0 - beta * x1 - beta * beta * x2) / d;
382       *a1 = (alpha * (2.0 * x0 + x1 + beta * x1 + 2.0 * beta * x2)) / d;
383       *a2 =
384           (-x1 - beta * beta * x1 - 2.0 * beta * (x0 + x2) -
385           alpha * alpha * (x0 + x1 + x2)) / d;
386       *a3 = (alpha * (x1 + beta * (2.0 * x0 + x1) + 2.0 * x2)) / d;
387       *a4 = (-beta * beta * x0 - beta * x1 - x2) / d;
388       *b1 = (alpha * (-2.0 + y1 + beta * y1 + 2.0 * beta * y2)) / d;
389       *b2 =
390           -(y1 + beta * beta * y1 + 2.0 * beta * (-1.0 + y2) +
391           alpha * alpha * (-1.0 + y1 + y2)) / d;
392       *b3 = (alpha * (beta * (-2.0 + y1) + y1 + 2.0 * y2)) / d;
393       *b4 = -(-beta * beta + beta * y1 + y2) / d;
394     }
395   }
396 }
397
398 static void
399 generate_coefficients (GstAudioChebBand * filter)
400 {
401   if (GST_AUDIO_FILTER (filter)->format.rate == 0) {
402     gdouble *a = g_new0 (gdouble, 1);
403
404     a[0] = 1.0;
405     gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
406         (filter), a, 1, NULL, 0);
407     GST_LOG_OBJECT (filter, "rate was not set yet");
408     return;
409   }
410
411   if (filter->upper_frequency <= filter->lower_frequency) {
412     gdouble *a = g_new0 (gdouble, 1);
413
414     a[0] = (filter->mode == MODE_BAND_PASS) ? 0.0 : 1.0;
415     gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
416         (filter), a, 1, NULL, 0);
417
418     GST_LOG_OBJECT (filter, "frequency band had no or negative dimension");
419     return;
420   }
421
422   if (filter->upper_frequency > GST_AUDIO_FILTER (filter)->format.rate / 2) {
423     filter->upper_frequency = GST_AUDIO_FILTER (filter)->format.rate / 2;
424     GST_LOG_OBJECT (filter, "clipped upper frequency to nyquist frequency");
425   }
426
427   if (filter->lower_frequency < 0.0) {
428     filter->lower_frequency = 0.0;
429     GST_LOG_OBJECT (filter, "clipped lower frequency to 0.0");
430   }
431
432   /* Calculate coefficients for the chebyshev filter */
433   {
434     gint np = filter->poles;
435     gdouble *a, *b;
436     gint i, p;
437
438     a = g_new0 (gdouble, np + 5);
439     b = g_new0 (gdouble, np + 5);
440
441     /* Calculate transfer function coefficients */
442     a[4] = 1.0;
443     b[4] = 1.0;
444
445     for (p = 1; p <= np / 4; p++) {
446       gdouble a0, a1, a2, a3, a4, b1, b2, b3, b4;
447       gdouble *ta = g_new0 (gdouble, np + 5);
448       gdouble *tb = g_new0 (gdouble, np + 5);
449
450       generate_biquad_coefficients (filter, p, &a0, &a1, &a2, &a3, &a4, &b1,
451           &b2, &b3, &b4);
452
453       memcpy (ta, a, sizeof (gdouble) * (np + 5));
454       memcpy (tb, b, sizeof (gdouble) * (np + 5));
455
456       /* add the new coefficients for the new two poles
457        * to the cascade by multiplication of the transfer
458        * functions */
459       for (i = 4; i < np + 5; i++) {
460         a[i] =
461             a0 * ta[i] + a1 * ta[i - 1] + a2 * ta[i - 2] + a3 * ta[i - 3] +
462             a4 * ta[i - 4];
463         b[i] =
464             tb[i] - b1 * tb[i - 1] - b2 * tb[i - 2] - b3 * tb[i - 3] -
465             b4 * tb[i - 4];
466       }
467       g_free (ta);
468       g_free (tb);
469     }
470
471     /* Move coefficients to the beginning of the array
472      * and multiply the b coefficients with -1 to move from
473      * the transfer function's coefficients to the difference
474      * equation's coefficients */
475     b[4] = 0.0;
476     for (i = 0; i <= np; i++) {
477       a[i] = a[i + 4];
478       b[i] = -b[i + 4];
479     }
480
481     /* Normalize to unity gain at frequency 0 and frequency
482      * 0.5 for bandreject and unity gain at band center frequency
483      * for bandpass */
484     if (filter->mode == MODE_BAND_REJECT) {
485       /* gain is sqrt(H(0)*H(0.5)) */
486
487       gdouble gain1 =
488           gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1,
489           1.0, 0.0);
490       gdouble gain2 =
491           gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1,
492           -1.0, 0.0);
493
494       gain1 = sqrt (gain1 * gain2);
495
496       for (i = 0; i <= np; i++) {
497         a[i] /= gain1;
498       }
499     } else {
500       /* gain is H(wc), wc = center frequency */
501
502       gdouble w1 =
503           2.0 * G_PI * (filter->lower_frequency /
504           GST_AUDIO_FILTER (filter)->format.rate);
505       gdouble w2 =
506           2.0 * G_PI * (filter->upper_frequency /
507           GST_AUDIO_FILTER (filter)->format.rate);
508       gdouble w0 = (w2 + w1) / 2.0;
509       gdouble zr = cos (w0), zi = sin (w0);
510       gdouble gain =
511           gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1, zr,
512           zi);
513
514       for (i = 0; i <= np; i++) {
515         a[i] /= gain;
516       }
517     }
518
519     gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
520         (filter), a, np + 1, b, np + 1);
521
522     GST_LOG_OBJECT (filter,
523         "Generated IIR coefficients for the Chebyshev filter");
524     GST_LOG_OBJECT (filter,
525         "mode: %s, type: %d, poles: %d, lower-frequency: %.2f Hz, upper-frequency: %.2f Hz, ripple: %.2f dB",
526         (filter->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject",
527         filter->type, filter->poles, filter->lower_frequency,
528         filter->upper_frequency, filter->ripple);
529
530     GST_LOG_OBJECT (filter, "%.2f dB gain @ 0Hz",
531         20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b,
532                 np + 1, 1.0, 0.0)));
533     {
534       gdouble w1 =
535           2.0 * G_PI * (filter->lower_frequency /
536           GST_AUDIO_FILTER (filter)->format.rate);
537       gdouble w2 =
538           2.0 * G_PI * (filter->upper_frequency /
539           GST_AUDIO_FILTER (filter)->format.rate);
540       gdouble w0 = (w2 + w1) / 2.0;
541       gdouble zr, zi;
542
543       zr = cos (w1);
544       zi = sin (w1);
545       GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
546           20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1,
547                   b, np + 1, zr, zi)), (int) filter->lower_frequency);
548       zr = cos (w0);
549       zi = sin (w0);
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)),
553           (int) ((filter->lower_frequency + filter->upper_frequency) / 2.0));
554       zr = cos (w2);
555       zi = sin (w2);
556       GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
557           20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1,
558                   b, np + 1, zr, zi)), (int) filter->upper_frequency);
559     }
560     GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
561         20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b,
562                 np + 1, -1.0, 0.0)),
563         GST_AUDIO_FILTER (filter)->format.rate / 2);
564   }
565 }
566
567 static void
568 gst_audio_cheb_band_finalize (GObject * object)
569 {
570   GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (object);
571
572   g_mutex_free (filter->lock);
573   filter->lock = NULL;
574
575   G_OBJECT_CLASS (parent_class)->finalize (object);
576 }
577
578 static void
579 gst_audio_cheb_band_set_property (GObject * object, guint prop_id,
580     const GValue * value, GParamSpec * pspec)
581 {
582   GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (object);
583
584   switch (prop_id) {
585     case PROP_MODE:
586       g_mutex_lock (filter->lock);
587       filter->mode = g_value_get_enum (value);
588       generate_coefficients (filter);
589       g_mutex_unlock (filter->lock);
590       break;
591     case PROP_TYPE:
592       g_mutex_lock (filter->lock);
593       filter->type = g_value_get_int (value);
594       generate_coefficients (filter);
595       g_mutex_unlock (filter->lock);
596       break;
597     case PROP_LOWER_FREQUENCY:
598       g_mutex_lock (filter->lock);
599       filter->lower_frequency = g_value_get_float (value);
600       generate_coefficients (filter);
601       g_mutex_unlock (filter->lock);
602       break;
603     case PROP_UPPER_FREQUENCY:
604       g_mutex_lock (filter->lock);
605       filter->upper_frequency = g_value_get_float (value);
606       generate_coefficients (filter);
607       g_mutex_unlock (filter->lock);
608       break;
609     case PROP_RIPPLE:
610       g_mutex_lock (filter->lock);
611       filter->ripple = g_value_get_float (value);
612       generate_coefficients (filter);
613       g_mutex_unlock (filter->lock);
614       break;
615     case PROP_POLES:
616       g_mutex_lock (filter->lock);
617       filter->poles = GST_ROUND_UP_4 (g_value_get_int (value));
618       generate_coefficients (filter);
619       g_mutex_unlock (filter->lock);
620       break;
621     default:
622       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
623       break;
624   }
625 }
626
627 static void
628 gst_audio_cheb_band_get_property (GObject * object, guint prop_id,
629     GValue * value, GParamSpec * pspec)
630 {
631   GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (object);
632
633   switch (prop_id) {
634     case PROP_MODE:
635       g_value_set_enum (value, filter->mode);
636       break;
637     case PROP_TYPE:
638       g_value_set_int (value, filter->type);
639       break;
640     case PROP_LOWER_FREQUENCY:
641       g_value_set_float (value, filter->lower_frequency);
642       break;
643     case PROP_UPPER_FREQUENCY:
644       g_value_set_float (value, filter->upper_frequency);
645       break;
646     case PROP_RIPPLE:
647       g_value_set_float (value, filter->ripple);
648       break;
649     case PROP_POLES:
650       g_value_set_int (value, filter->poles);
651       break;
652     default:
653       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
654       break;
655   }
656 }
657
658 /* GstAudioFilter vmethod implementations */
659
660 static gboolean
661 gst_audio_cheb_band_setup (GstAudioFilter * base, GstRingBufferSpec * format)
662 {
663   GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (base);
664
665   generate_coefficients (filter);
666
667   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, format);
668 }