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