Rename audiochebyshevfreqband -> audiochebband and audiochebyshevfreqlimit -> audioch...
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audiocheblimit.c
1 /* 
2  * GStreamer
3  * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
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  */
30
31 /**
32  * SECTION:element-audiocheblimit
33  * @short_description: Chebyshev low pass and high pass filter
34  *
35  * <refsect2>
36  * <para>
37  * Attenuates all frequencies above the cutoff frequency (low-pass) or all frequencies below the
38  * cutoff frequency (high-pass). The number of poles and the ripple parameter control the rolloff.
39  * </para>
40  * <para>
41  * This element has the advantage over the windowed sinc lowpass and highpass 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  * </para>
45  * <para>
46  * For type 1 the ripple parameter specifies how much ripple in dB is allowed in the passband, i.e.
47  * some frequencies in the passband will be amplified by that value. A higher ripple value will allow
48  * a faster rolloff.
49  * </para>
50  * <para>
51  * For type 2 the ripple parameter specifies the stopband attenuation. In the stopband the gain will
52  * be at most this value. A lower ripple value will allow a faster rolloff.
53  * </para>
54  * <para>
55  * As a special case, a Chebyshev type 1 filter with no ripple is a Butterworth filter.
56  * </para>
57  * <para><note>
58  * Be warned that a too large number of poles can produce noise. The most poles are possible with
59  * a cutoff frequency at a quarter of the sampling rate.
60  * </note></para>
61  * <title>Example launch line</title>
62  * <para>
63  * <programlisting>
64  * gst-launch audiotestsrc freq=1500 ! audioconvert ! audiocheblimit mode=low-pass cutoff=1000 poles=4 ! audioconvert ! alsasink
65  * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiocheblimit mode=high-pass cutoff=400 ripple=0.2 ! audioconvert ! alsasink
66  * gst-launch audiotestsrc wave=white-noise ! audioconvert ! audiocheblimit mode=low-pass cutoff=800 type=2 ! audioconvert ! alsasink
67  * </programlisting>
68  * </para>
69  * </refsect2>
70  */
71
72 #ifdef HAVE_CONFIG_H
73 #include "config.h"
74 #endif
75
76 #include <gst/gst.h>
77 #include <gst/base/gstbasetransform.h>
78 #include <gst/audio/audio.h>
79 #include <gst/audio/gstaudiofilter.h>
80 #include <gst/controller/gstcontroller.h>
81
82 #include <math.h>
83
84 #include "audiocheblimit.h"
85
86 #define GST_CAT_DEFAULT gst_audio_cheb_limit_debug
87 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
88
89 static const GstElementDetails element_details =
90 GST_ELEMENT_DETAILS ("AudioChebLimit",
91     "Filter/Effect/Audio",
92     "Chebyshev low pass and high pass filter",
93     "Sebastian Dröge <slomo@circular-chaos.org>");
94
95 /* Filter signals and args */
96 enum
97 {
98   /* FILL ME */
99   LAST_SIGNAL
100 };
101
102 enum
103 {
104   PROP_0,
105   PROP_MODE,
106   PROP_TYPE,
107   PROP_CUTOFF,
108   PROP_RIPPLE,
109   PROP_POLES
110 };
111
112 #define ALLOWED_CAPS \
113     "audio/x-raw-float,"                                              \
114     " width = (int) { 32, 64 }, "                                     \
115     " endianness = (int) BYTE_ORDER,"                                 \
116     " rate = (int) [ 1, MAX ],"                                       \
117     " channels = (int) [ 1, MAX ]"
118
119 #define DEBUG_INIT(bla) \
120   GST_DEBUG_CATEGORY_INIT (gst_audio_cheb_limit_debug, "audiocheblimit", 0, "audiocheblimit element");
121
122 GST_BOILERPLATE_FULL (GstAudioChebLimit,
123     gst_audio_cheb_limit, GstAudioFilter, GST_TYPE_AUDIO_FILTER, DEBUG_INIT);
124
125 static void gst_audio_cheb_limit_set_property (GObject * object,
126     guint prop_id, const GValue * value, GParamSpec * pspec);
127 static void gst_audio_cheb_limit_get_property (GObject * object,
128     guint prop_id, GValue * value, GParamSpec * pspec);
129
130 static gboolean gst_audio_cheb_limit_setup (GstAudioFilter * filter,
131     GstRingBufferSpec * format);
132 static GstFlowReturn
133 gst_audio_cheb_limit_transform_ip (GstBaseTransform * base, GstBuffer * buf);
134 static gboolean gst_audio_cheb_limit_start (GstBaseTransform * base);
135
136 static void process_64 (GstAudioChebLimit * filter,
137     gdouble * data, guint num_samples);
138 static void process_32 (GstAudioChebLimit * filter,
139     gfloat * data, guint num_samples);
140
141 enum
142 {
143   MODE_LOW_PASS = 0,
144   MODE_HIGH_PASS
145 };
146
147 #define GST_TYPE_AUDIO_CHEBYSHEV_FREQ_LIMIT_MODE (gst_audio_cheb_limit_mode_get_type ())
148 static GType
149 gst_audio_cheb_limit_mode_get_type (void)
150 {
151   static GType gtype = 0;
152
153   if (gtype == 0) {
154     static const GEnumValue values[] = {
155       {MODE_LOW_PASS, "Low pass (default)",
156           "low-pass"},
157       {MODE_HIGH_PASS, "High pass",
158           "high-pass"},
159       {0, NULL, NULL}
160     };
161
162     gtype = g_enum_register_static ("GstAudioChebLimitMode", values);
163   }
164   return gtype;
165 }
166
167 /* GObject vmethod implementations */
168
169 static void
170 gst_audio_cheb_limit_base_init (gpointer klass)
171 {
172   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
173   GstCaps *caps;
174
175   gst_element_class_set_details (element_class, &element_details);
176
177   caps = gst_caps_from_string (ALLOWED_CAPS);
178   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
179       caps);
180   gst_caps_unref (caps);
181 }
182
183 static void
184 gst_audio_cheb_limit_dispose (GObject * object)
185 {
186   GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
187
188   if (filter->a) {
189     g_free (filter->a);
190     filter->a = NULL;
191   }
192
193   if (filter->b) {
194     g_free (filter->b);
195     filter->b = NULL;
196   }
197
198   if (filter->channels) {
199     GstAudioChebLimitChannelCtx *ctx;
200     gint i, channels = GST_AUDIO_FILTER (filter)->format.channels;
201
202     for (i = 0; i < channels; i++) {
203       ctx = &filter->channels[i];
204       g_free (ctx->x);
205       g_free (ctx->y);
206     }
207
208     g_free (filter->channels);
209     filter->channels = NULL;
210   }
211
212   G_OBJECT_CLASS (parent_class)->dispose (object);
213 }
214
215 static void
216 gst_audio_cheb_limit_class_init (GstAudioChebLimitClass * klass)
217 {
218   GObjectClass *gobject_class;
219   GstBaseTransformClass *trans_class;
220   GstAudioFilterClass *filter_class;
221
222   gobject_class = (GObjectClass *) klass;
223   trans_class = (GstBaseTransformClass *) klass;
224   filter_class = (GstAudioFilterClass *) klass;
225
226   gobject_class->set_property = gst_audio_cheb_limit_set_property;
227   gobject_class->get_property = gst_audio_cheb_limit_get_property;
228   gobject_class->dispose = gst_audio_cheb_limit_dispose;
229
230   g_object_class_install_property (gobject_class, PROP_MODE,
231       g_param_spec_enum ("mode", "Mode",
232           "Low pass or high pass mode",
233           GST_TYPE_AUDIO_CHEBYSHEV_FREQ_LIMIT_MODE, MODE_LOW_PASS,
234           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
235   g_object_class_install_property (gobject_class, PROP_TYPE,
236       g_param_spec_int ("type", "Type", "Type of the chebychev filter", 1, 2, 1,
237           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
238
239   /* FIXME: Don't use the complete possible range but restrict the upper boundary
240    * so automatically generated UIs can use a slider without */
241   g_object_class_install_property (gobject_class, PROP_CUTOFF,
242       g_param_spec_float ("cutoff", "Cutoff", "Cut off frequency (Hz)", 0.0,
243           100000.0, 0.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
244   g_object_class_install_property (gobject_class, PROP_RIPPLE,
245       g_param_spec_float ("ripple", "Ripple", "Amount of ripple (dB)", 0.0,
246           200.0, 0.25, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
247
248   /* FIXME: What to do about this upper boundary? With a cutoff frequency of
249    * rate/4 32 poles are completely possible, with a cutoff frequency very low
250    * or very high 16 poles already produces only noise */
251   g_object_class_install_property (gobject_class, PROP_POLES,
252       g_param_spec_int ("poles", "Poles",
253           "Number of poles to use, will be rounded up to the next even number",
254           2, 32, 4, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
255
256   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_cheb_limit_setup);
257   trans_class->transform_ip =
258       GST_DEBUG_FUNCPTR (gst_audio_cheb_limit_transform_ip);
259   trans_class->start = GST_DEBUG_FUNCPTR (gst_audio_cheb_limit_start);
260 }
261
262 static void
263 gst_audio_cheb_limit_init (GstAudioChebLimit * filter,
264     GstAudioChebLimitClass * klass)
265 {
266   filter->cutoff = 0.0;
267   filter->mode = MODE_LOW_PASS;
268   filter->type = 1;
269   filter->poles = 4;
270   filter->ripple = 0.25;
271   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
272
273   filter->have_coeffs = FALSE;
274   filter->num_a = 0;
275   filter->num_b = 0;
276   filter->channels = NULL;
277 }
278
279 static void
280 generate_biquad_coefficients (GstAudioChebLimit * filter,
281     gint p, gdouble * a0, gdouble * a1, gdouble * a2,
282     gdouble * b1, gdouble * b2)
283 {
284   gint np = filter->poles;
285   gdouble ripple = filter->ripple;
286
287   /* pole location in s-plane */
288   gdouble rp, ip;
289
290   /* zero location in s-plane */
291   gdouble rz = 0.0, iz = 0.0;
292
293   /* transfer function coefficients for the z-plane */
294   gdouble x0, x1, x2, y1, y2;
295   gint type = filter->type;
296
297   /* Calculate pole location for lowpass at frequency 1 */
298   {
299     gdouble angle = (M_PI / 2.0) * (2.0 * p - 1) / np;
300
301     rp = -sin (angle);
302     ip = cos (angle);
303   }
304
305   /* If we allow ripple, move the pole from the unit
306    * circle to an ellipse and keep cutoff at frequency 1 */
307   if (ripple > 0 && type == 1) {
308     gdouble es, vx;
309
310     es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
311
312     vx = (1.0 / np) * asinh (1.0 / es);
313     rp = rp * sinh (vx);
314     ip = ip * cosh (vx);
315   } else if (type == 2) {
316     gdouble es, vx;
317
318     es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
319     vx = (1.0 / np) * asinh (es);
320     rp = rp * sinh (vx);
321     ip = ip * cosh (vx);
322   }
323
324   /* Calculate inverse of the pole location to convert from
325    * type I to type II */
326   if (type == 2) {
327     gdouble mag2 = rp * rp + ip * ip;
328
329     rp /= mag2;
330     ip /= mag2;
331   }
332
333   /* Calculate zero location for frequency 1 on the
334    * unit circle for type 2 */
335   if (type == 2) {
336     gdouble angle = M_PI / (np * 2.0) + ((p - 1) * M_PI) / (np);
337     gdouble mag2;
338
339     rz = 0.0;
340     iz = cos (angle);
341     mag2 = rz * rz + iz * iz;
342     rz /= mag2;
343     iz /= mag2;
344   }
345
346   /* Convert from s-domain to z-domain by
347    * using the bilinear Z-transform, i.e.
348    * substitute s by (2/t)*((z-1)/(z+1))
349    * with t = 2 * tan(0.5).
350    */
351   if (type == 1) {
352     gdouble t, m, d;
353
354     t = 2.0 * tan (0.5);
355     m = rp * rp + ip * ip;
356     d = 4.0 - 4.0 * rp * t + m * t * t;
357
358     x0 = (t * t) / d;
359     x1 = 2.0 * x0;
360     x2 = x0;
361     y1 = (8.0 - 2.0 * m * t * t) / d;
362     y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
363   } else {
364     gdouble t, m, d;
365
366     t = 2.0 * tan (0.5);
367     m = rp * rp + ip * ip;
368     d = 4.0 - 4.0 * rp * t + m * t * t;
369
370     x0 = (t * t * iz * iz + 4.0) / d;
371     x1 = (-8.0 + 2.0 * iz * iz * t * t) / d;
372     x2 = x0;
373     y1 = (8.0 - 2.0 * m * t * t) / d;
374     y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
375   }
376
377   /* Convert from lowpass at frequency 1 to either lowpass
378    * or highpass.
379    *
380    * For lowpass substitute z^(-1) with:
381    *  -1
382    * z   - k
383    * ------------
384    *          -1
385    * 1 - k * z
386    *
387    * k = sin((1-w)/2) / sin((1+w)/2)
388    *
389    * For highpass substitute z^(-1) with:
390    *
391    *   -1
392    * -z   - k
393    * ------------
394    *          -1
395    * 1 + k * z
396    *
397    * k = -cos((1+w)/2) / cos((1-w)/2)
398    *
399    */
400   {
401     gdouble k, d;
402     gdouble omega =
403         2.0 * M_PI * (filter->cutoff / GST_AUDIO_FILTER (filter)->format.rate);
404
405     if (filter->mode == MODE_LOW_PASS)
406       k = sin ((1.0 - omega) / 2.0) / sin ((1.0 + omega) / 2.0);
407     else
408       k = -cos ((omega + 1.0) / 2.0) / cos ((omega - 1.0) / 2.0);
409
410     d = 1.0 + y1 * k - y2 * k * k;
411     *a0 = (x0 + k * (-x1 + k * x2)) / d;
412     *a1 = (x1 + k * k * x1 - 2.0 * k * (x0 + x2)) / d;
413     *a2 = (x0 * k * k - x1 * k + x2) / d;
414     *b1 = (2.0 * k + y1 + y1 * k * k - 2.0 * y2 * k) / d;
415     *b2 = (-k * k - y1 * k + y2) / d;
416
417     if (filter->mode == MODE_HIGH_PASS) {
418       *a1 = -*a1;
419       *b1 = -*b1;
420     }
421   }
422 }
423
424 /* Evaluate the transfer function that corresponds to the IIR
425  * coefficients at zr + zi*I and return the magnitude */
426 static gdouble
427 calculate_gain (gdouble * a, gdouble * b, gint num_a, gint num_b, gdouble zr,
428     gdouble zi)
429 {
430   gdouble sum_ar, sum_ai;
431   gdouble sum_br, sum_bi;
432   gdouble gain_r, gain_i;
433
434   gdouble sum_r_old;
435   gdouble sum_i_old;
436
437   gint i;
438
439   sum_ar = 0.0;
440   sum_ai = 0.0;
441   for (i = num_a; i >= 0; i--) {
442     sum_r_old = sum_ar;
443     sum_i_old = sum_ai;
444
445     sum_ar = (sum_r_old * zr - sum_i_old * zi) + a[i];
446     sum_ai = (sum_r_old * zi + sum_i_old * zr) + 0.0;
447   }
448
449   sum_br = 0.0;
450   sum_bi = 0.0;
451   for (i = num_b; i >= 0; i--) {
452     sum_r_old = sum_br;
453     sum_i_old = sum_bi;
454
455     sum_br = (sum_r_old * zr - sum_i_old * zi) - b[i];
456     sum_bi = (sum_r_old * zi + sum_i_old * zr) - 0.0;
457   }
458   sum_br += 1.0;
459   sum_bi += 0.0;
460
461   gain_r =
462       (sum_ar * sum_br + sum_ai * sum_bi) / (sum_br * sum_br + sum_bi * sum_bi);
463   gain_i =
464       (sum_ai * sum_br - sum_ar * sum_bi) / (sum_br * sum_br + sum_bi * sum_bi);
465
466   return (sqrt (gain_r * gain_r + gain_i * gain_i));
467 }
468
469 static void
470 generate_coefficients (GstAudioChebLimit * filter)
471 {
472   gint channels = GST_AUDIO_FILTER (filter)->format.channels;
473
474   if (filter->a) {
475     g_free (filter->a);
476     filter->a = NULL;
477   }
478
479   if (filter->b) {
480     g_free (filter->b);
481     filter->b = NULL;
482   }
483
484   if (filter->channels) {
485     GstAudioChebLimitChannelCtx *ctx;
486     gint i;
487
488     for (i = 0; i < channels; i++) {
489       ctx = &filter->channels[i];
490       g_free (ctx->x);
491       g_free (ctx->y);
492     }
493
494     g_free (filter->channels);
495     filter->channels = NULL;
496   }
497
498   if (GST_AUDIO_FILTER (filter)->format.rate == 0) {
499     filter->num_a = 1;
500     filter->a = g_new0 (gdouble, 1);
501     filter->a[0] = 1.0;
502     filter->num_b = 0;
503     filter->channels = g_new0 (GstAudioChebLimitChannelCtx, channels);
504     GST_LOG_OBJECT (filter, "rate was not set yet");
505     return;
506   }
507
508   filter->have_coeffs = TRUE;
509
510   if (filter->cutoff >= GST_AUDIO_FILTER (filter)->format.rate / 2.0) {
511     filter->num_a = 1;
512     filter->a = g_new0 (gdouble, 1);
513     filter->a[0] = (filter->mode == MODE_LOW_PASS) ? 1.0 : 0.0;
514     filter->num_b = 0;
515     filter->channels = g_new0 (GstAudioChebLimitChannelCtx, channels);
516     GST_LOG_OBJECT (filter, "cutoff was higher than nyquist frequency");
517     return;
518   } else if (filter->cutoff <= 0.0) {
519     filter->num_a = 1;
520     filter->a = g_new0 (gdouble, 1);
521     filter->a[0] = (filter->mode == MODE_LOW_PASS) ? 0.0 : 1.0;
522     filter->num_b = 0;
523     filter->channels = g_new0 (GstAudioChebLimitChannelCtx, channels);
524     GST_LOG_OBJECT (filter, "cutoff is lower than zero");
525     return;
526   }
527
528   /* Calculate coefficients for the chebyshev filter */
529   {
530     gint np = filter->poles;
531     gdouble *a, *b;
532     gint i, p;
533
534     filter->num_a = np + 1;
535     filter->a = a = g_new0 (gdouble, np + 3);
536     filter->num_b = np + 1;
537     filter->b = b = g_new0 (gdouble, np + 3);
538
539     filter->channels = g_new0 (GstAudioChebLimitChannelCtx, channels);
540     for (i = 0; i < channels; i++) {
541       GstAudioChebLimitChannelCtx *ctx = &filter->channels[i];
542
543       ctx->x = g_new0 (gdouble, np + 1);
544       ctx->y = g_new0 (gdouble, np + 1);
545     }
546
547     /* Calculate transfer function coefficients */
548     a[2] = 1.0;
549     b[2] = 1.0;
550
551     for (p = 1; p <= np / 2; p++) {
552       gdouble a0, a1, a2, b1, b2;
553       gdouble *ta = g_new0 (gdouble, np + 3);
554       gdouble *tb = g_new0 (gdouble, np + 3);
555
556       generate_biquad_coefficients (filter, p, &a0, &a1, &a2, &b1, &b2);
557
558       memcpy (ta, a, sizeof (gdouble) * (np + 3));
559       memcpy (tb, b, sizeof (gdouble) * (np + 3));
560
561       /* add the new coefficients for the new two poles
562        * to the cascade by multiplication of the transfer
563        * functions */
564       for (i = 2; i < np + 3; i++) {
565         a[i] = a0 * ta[i] + a1 * ta[i - 1] + a2 * ta[i - 2];
566         b[i] = tb[i] - b1 * tb[i - 1] - b2 * tb[i - 2];
567       }
568       g_free (ta);
569       g_free (tb);
570     }
571
572     /* Move coefficients to the beginning of the array
573      * and multiply the b coefficients with -1 to move from
574      * the transfer function's coefficients to the difference
575      * equation's coefficients */
576     b[2] = 0.0;
577     for (i = 0; i <= np; i++) {
578       a[i] = a[i + 2];
579       b[i] = -b[i + 2];
580     }
581
582     /* Normalize to unity gain at frequency 0 for lowpass
583      * and frequency 0.5 for highpass */
584     {
585       gdouble gain;
586
587       if (filter->mode == MODE_LOW_PASS)
588         gain = calculate_gain (a, b, np, np, 1.0, 0.0);
589       else
590         gain = calculate_gain (a, b, np, np, -1.0, 0.0);
591
592       for (i = 0; i <= np; i++) {
593         a[i] /= gain;
594       }
595     }
596
597     GST_LOG_OBJECT (filter,
598         "Generated IIR coefficients for the Chebyshev filter");
599     GST_LOG_OBJECT (filter,
600         "mode: %s, type: %d, poles: %d, cutoff: %.2f Hz, ripple: %.2f dB",
601         (filter->mode == MODE_LOW_PASS) ? "low-pass" : "high-pass",
602         filter->type, filter->poles, filter->cutoff, filter->ripple);
603     GST_LOG_OBJECT (filter, "%.2f dB gain @ 0 Hz",
604         20.0 * log10 (calculate_gain (a, b, np, np, 1.0, 0.0)));
605     {
606       gdouble wc =
607           2.0 * M_PI * (filter->cutoff /
608           GST_AUDIO_FILTER (filter)->format.rate);
609       gdouble zr = cos (wc), zi = sin (wc);
610
611       GST_LOG_OBJECT (filter, "%.2f dB gain @ %d Hz",
612           20.0 * log10 (calculate_gain (a, b, np, np, zr, zi)),
613           (int) filter->cutoff);
614     }
615     GST_LOG_OBJECT (filter, "%.2f dB gain @ %d Hz",
616         20.0 * log10 (calculate_gain (a, b, np, np, -1.0, 0.0)),
617         GST_AUDIO_FILTER (filter)->format.rate / 2);
618   }
619 }
620
621 static void
622 gst_audio_cheb_limit_set_property (GObject * object, guint prop_id,
623     const GValue * value, GParamSpec * pspec)
624 {
625   GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
626
627   switch (prop_id) {
628     case PROP_MODE:
629       GST_BASE_TRANSFORM_LOCK (filter);
630       filter->mode = g_value_get_enum (value);
631       generate_coefficients (filter);
632       GST_BASE_TRANSFORM_UNLOCK (filter);
633       break;
634     case PROP_TYPE:
635       GST_BASE_TRANSFORM_LOCK (filter);
636       filter->type = g_value_get_int (value);
637       generate_coefficients (filter);
638       GST_BASE_TRANSFORM_UNLOCK (filter);
639       break;
640     case PROP_CUTOFF:
641       GST_BASE_TRANSFORM_LOCK (filter);
642       filter->cutoff = g_value_get_float (value);
643       generate_coefficients (filter);
644       GST_BASE_TRANSFORM_UNLOCK (filter);
645       break;
646     case PROP_RIPPLE:
647       GST_BASE_TRANSFORM_LOCK (filter);
648       filter->ripple = g_value_get_float (value);
649       generate_coefficients (filter);
650       GST_BASE_TRANSFORM_UNLOCK (filter);
651       break;
652     case PROP_POLES:
653       GST_BASE_TRANSFORM_LOCK (filter);
654       filter->poles = GST_ROUND_UP_2 (g_value_get_int (value));
655       generate_coefficients (filter);
656       GST_BASE_TRANSFORM_UNLOCK (filter);
657       break;
658     default:
659       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
660       break;
661   }
662 }
663
664 static void
665 gst_audio_cheb_limit_get_property (GObject * object, guint prop_id,
666     GValue * value, GParamSpec * pspec)
667 {
668   GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
669
670   switch (prop_id) {
671     case PROP_MODE:
672       g_value_set_enum (value, filter->mode);
673       break;
674     case PROP_TYPE:
675       g_value_set_int (value, filter->type);
676       break;
677     case PROP_CUTOFF:
678       g_value_set_float (value, filter->cutoff);
679       break;
680     case PROP_RIPPLE:
681       g_value_set_float (value, filter->ripple);
682       break;
683     case PROP_POLES:
684       g_value_set_int (value, filter->poles);
685       break;
686     default:
687       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
688       break;
689   }
690 }
691
692 /* GstAudioFilter vmethod implementations */
693
694 static gboolean
695 gst_audio_cheb_limit_setup (GstAudioFilter * base, GstRingBufferSpec * format)
696 {
697   GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (base);
698   gboolean ret = TRUE;
699
700   if (format->width == 32)
701     filter->process = (GstAudioChebLimitProcessFunc)
702         process_32;
703   else if (format->width == 64)
704     filter->process = (GstAudioChebLimitProcessFunc)
705         process_64;
706   else
707     ret = FALSE;
708
709   filter->have_coeffs = FALSE;
710
711   return ret;
712 }
713
714 static inline gdouble
715 process (GstAudioChebLimit * filter,
716     GstAudioChebLimitChannelCtx * ctx, gdouble x0)
717 {
718   gdouble val = filter->a[0] * x0;
719   gint i, j;
720
721   for (i = 1, j = ctx->x_pos; i < filter->num_a; i++) {
722     val += filter->a[i] * ctx->x[j];
723     j--;
724     if (j < 0)
725       j = filter->num_a - 1;
726   }
727
728   for (i = 1, j = ctx->y_pos; i < filter->num_b; i++) {
729     val += filter->b[i] * ctx->y[j];
730     j--;
731     if (j < 0)
732       j = filter->num_b - 1;
733   }
734
735   if (ctx->x) {
736     ctx->x_pos++;
737     if (ctx->x_pos > filter->num_a - 1)
738       ctx->x_pos = 0;
739     ctx->x[ctx->x_pos] = x0;
740   }
741
742   if (ctx->y) {
743     ctx->y_pos++;
744     if (ctx->y_pos > filter->num_b - 1)
745       ctx->y_pos = 0;
746
747     ctx->y[ctx->y_pos] = val;
748   }
749
750   return val;
751 }
752
753 #define DEFINE_PROCESS_FUNC(width,ctype) \
754 static void \
755 process_##width (GstAudioChebLimit * filter, \
756     g##ctype * data, guint num_samples) \
757 { \
758   gint i, j, channels = GST_AUDIO_FILTER (filter)->format.channels; \
759   gdouble val; \
760   \
761   for (i = 0; i < num_samples / channels; i++) { \
762     for (j = 0; j < channels; j++) { \
763       val = process (filter, &filter->channels[j], *data); \
764       *data++ = val; \
765     } \
766   } \
767 }
768
769 DEFINE_PROCESS_FUNC (32, float);
770 DEFINE_PROCESS_FUNC (64, double);
771
772 #undef DEFINE_PROCESS_FUNC
773
774 /* GstBaseTransform vmethod implementations */
775 static GstFlowReturn
776 gst_audio_cheb_limit_transform_ip (GstBaseTransform * base, GstBuffer * buf)
777 {
778   GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (base);
779   guint num_samples =
780       GST_BUFFER_SIZE (buf) / (GST_AUDIO_FILTER (filter)->format.width / 8);
781
782   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buf)))
783     gst_object_sync_values (G_OBJECT (filter), GST_BUFFER_TIMESTAMP (buf));
784
785   if (gst_base_transform_is_passthrough (base))
786     return GST_FLOW_OK;
787
788   if (!filter->have_coeffs)
789     generate_coefficients (filter);
790
791   filter->process (filter, GST_BUFFER_DATA (buf), num_samples);
792
793   return GST_FLOW_OK;
794 }
795
796
797 static gboolean
798 gst_audio_cheb_limit_start (GstBaseTransform * base)
799 {
800   GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (base);
801   gint channels = GST_AUDIO_FILTER (filter)->format.channels;
802   GstAudioChebLimitChannelCtx *ctx;
803   gint i;
804
805   /* Reset the history of input and output values if
806    * already existing */
807   if (channels && filter->channels) {
808     for (i = 0; i < channels; i++) {
809       ctx = &filter->channels[i];
810       if (ctx->x)
811         memset (ctx->x, 0, (filter->poles + 1) * sizeof (gdouble));
812       if (ctx->y)
813         memset (ctx->y, 0, (filter->poles + 1) * sizeof (gdouble));
814     }
815   }
816   return TRUE;
817 }