Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audioiirfilter.c
1 /* 
2  * GStreamer
3  * Copyright (C) 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 /**
23  * SECTION:element-audioiirfilter
24  *
25  * audioiirfilter implements a generic audio <ulink url="http://en.wikipedia.org/wiki/Infinite_impulse_response">IIR filter</ulink>. Before usage the
26  * "a" and "b" properties have to be set to the filter coefficients that
27  * should be used.
28  *
29  * The filter coefficients describe the numerator and denominator of the
30  * transfer function.
31  *
32  * To change the filter coefficients whenever the sampling rate changes the
33  * "rate-changed" signal can be used. This should be done for most
34  * IIR filters as they're depending on the sampling rate.
35  *
36  * <refsect2>
37  * <title>Example application</title>
38  * |[
39  * <xi:include xmlns:xi="http://www.w3.org/2003/XInclude" parse="text" href="../../../../tests/examples/audiofx/iirfilter-example.c" />
40  * ]|
41  * </refsect2>
42  */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <string.h>
49 #include <math.h>
50 #include <gst/gst.h>
51 #include <gst/audio/gstaudiofilter.h>
52
53 #include "audioiirfilter.h"
54
55 #define GST_CAT_DEFAULT gst_audio_iir_filter_debug
56 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
57
58 enum
59 {
60   SIGNAL_RATE_CHANGED,
61   LAST_SIGNAL
62 };
63
64 enum
65 {
66   PROP_0,
67   PROP_A,
68   PROP_B
69 };
70
71 static guint gst_audio_iir_filter_signals[LAST_SIGNAL] = { 0, };
72
73 #define gst_audio_iir_filter_parent_class parent_class
74 G_DEFINE_TYPE (GstAudioIIRFilter, gst_audio_iir_filter,
75     GST_TYPE_AUDIO_FX_BASE_IIR_FILTER);
76
77 static void gst_audio_iir_filter_set_property (GObject * object, guint prop_id,
78     const GValue * value, GParamSpec * pspec);
79 static void gst_audio_iir_filter_get_property (GObject * object, guint prop_id,
80     GValue * value, GParamSpec * pspec);
81 static void gst_audio_iir_filter_finalize (GObject * object);
82
83 static gboolean gst_audio_iir_filter_setup (GstAudioFilter * base,
84     const GstAudioInfo * info);
85
86 static void
87 gst_audio_iir_filter_class_init (GstAudioIIRFilterClass * klass)
88 {
89   GObjectClass *gobject_class = (GObjectClass *) klass;
90   GstElementClass *gstelement_class = (GstElementClass *) klass;
91   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
92
93   GST_DEBUG_CATEGORY_INIT (gst_audio_iir_filter_debug, "audioiirfilter", 0,
94       "Generic audio IIR filter plugin");
95
96   gobject_class->set_property = gst_audio_iir_filter_set_property;
97   gobject_class->get_property = gst_audio_iir_filter_get_property;
98   gobject_class->finalize = gst_audio_iir_filter_finalize;
99
100   g_object_class_install_property (gobject_class, PROP_A,
101       g_param_spec_value_array ("a", "A",
102           "Filter coefficients (numerator of transfer function)",
103           g_param_spec_double ("Coefficient", "Filter Coefficient",
104               "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
105               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
106           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107   g_object_class_install_property (gobject_class, PROP_B,
108       g_param_spec_value_array ("b", "B",
109           "Filter coefficients (denominator of transfer function)",
110           g_param_spec_double ("Coefficient", "Filter Coefficient",
111               "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
112               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
113           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
114
115   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_iir_filter_setup);
116
117   /**
118    * GstAudioIIRFilter::rate-changed:
119    * @filter: the filter on which the signal is emitted
120    * @rate: the new sampling rate
121    *
122    * Will be emitted when the sampling rate changes. The callbacks
123    * will be called from the streaming thread and processing will
124    * stop until the event is handled.
125    */
126   gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED] =
127       g_signal_new ("rate-changed", G_TYPE_FROM_CLASS (klass),
128       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAudioIIRFilterClass, rate_changed),
129       NULL, NULL, gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
130
131   gst_element_class_set_details_simple (gstelement_class,
132       "Audio IIR filter", "Filter/Effect/Audio",
133       "Generic audio IIR filter with custom filter kernel",
134       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
135 }
136
137 static void
138 gst_audio_iir_filter_update_coefficients (GstAudioIIRFilter * self,
139     GValueArray * va, GValueArray * vb)
140 {
141   gdouble *a = NULL, *b = NULL;
142   guint i;
143
144   if (va) {
145     if (self->a)
146       g_value_array_free (self->a);
147
148     self->a = va;
149   }
150   if (vb) {
151     if (self->b)
152       g_value_array_free (self->b);
153
154     self->b = vb;
155   }
156
157   if (self->a && self->a->n_values > 0) {
158     a = g_new (gdouble, self->a->n_values);
159
160     for (i = 0; i < self->a->n_values; i++) {
161       GValue *v = g_value_array_get_nth (self->a, i);
162       a[i] = g_value_get_double (v);
163     }
164   }
165
166   if (self->b && self->b->n_values > 0) {
167     b = g_new (gdouble, self->b->n_values);
168     for (i = 0; i < self->b->n_values; i++) {
169       GValue *v = g_value_array_get_nth (self->b, i);
170       b[i] = g_value_get_double (v);
171     }
172   }
173
174   gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
175       (self), a, (self->a) ? self->a->n_values : 0, b,
176       (self->b) ? self->b->n_values : 0);
177 }
178
179 static void
180 gst_audio_iir_filter_init (GstAudioIIRFilter * self)
181 {
182   GValue v = { 0, };
183   GValueArray *a, *b;
184
185   a = g_value_array_new (1);
186
187   g_value_init (&v, G_TYPE_DOUBLE);
188   g_value_set_double (&v, 1.0);
189   g_value_array_append (a, &v);
190   g_value_unset (&v);
191
192   b = NULL;
193   gst_audio_iir_filter_update_coefficients (self, a, b);
194
195   self->lock = g_mutex_new ();
196 }
197
198 /* GstAudioFilter vmethod implementations */
199
200 /* get notified of caps and plug in the correct process function */
201 static gboolean
202 gst_audio_iir_filter_setup (GstAudioFilter * base, const GstAudioInfo * info)
203 {
204   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (base);
205   gint new_rate = GST_AUDIO_INFO_RATE (info);
206
207   if (GST_AUDIO_FILTER_RATE (self) != new_rate) {
208     g_signal_emit (G_OBJECT (self),
209         gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED], 0, new_rate);
210   }
211
212   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
213 }
214
215 static void
216 gst_audio_iir_filter_finalize (GObject * object)
217 {
218   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
219
220   g_mutex_free (self->lock);
221   self->lock = NULL;
222
223   if (self->a)
224     g_value_array_free (self->a);
225   self->a = NULL;
226   if (self->b)
227     g_value_array_free (self->b);
228   self->b = NULL;
229
230   G_OBJECT_CLASS (parent_class)->finalize (object);
231 }
232
233 static void
234 gst_audio_iir_filter_set_property (GObject * object, guint prop_id,
235     const GValue * value, GParamSpec * pspec)
236 {
237   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
238
239   g_return_if_fail (GST_IS_AUDIO_IIR_FILTER (self));
240
241   switch (prop_id) {
242     case PROP_A:
243       g_mutex_lock (self->lock);
244       gst_audio_iir_filter_update_coefficients (self, g_value_dup_boxed (value),
245           NULL);
246       g_mutex_unlock (self->lock);
247       break;
248     case PROP_B:
249       g_mutex_lock (self->lock);
250       gst_audio_iir_filter_update_coefficients (self, NULL,
251           g_value_dup_boxed (value));
252       g_mutex_unlock (self->lock);
253       break;
254     default:
255       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256       break;
257   }
258 }
259
260 static void
261 gst_audio_iir_filter_get_property (GObject * object, guint prop_id,
262     GValue * value, GParamSpec * pspec)
263 {
264   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
265
266   switch (prop_id) {
267     case PROP_A:
268       g_value_set_boxed (value, self->a);
269       break;
270     case PROP_B:
271       g_value_set_boxed (value, self->b);
272       break;
273     default:
274       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275       break;
276   }
277 }