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 #include "gst/glib-compat-private.h"
56
57 #define GST_CAT_DEFAULT gst_audio_iir_filter_debug
58 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
59
60 enum
61 {
62   SIGNAL_RATE_CHANGED,
63   LAST_SIGNAL
64 };
65
66 enum
67 {
68   PROP_0,
69   PROP_A,
70   PROP_B
71 };
72
73 static guint gst_audio_iir_filter_signals[LAST_SIGNAL] = { 0, };
74
75 #define gst_audio_iir_filter_parent_class parent_class
76 G_DEFINE_TYPE (GstAudioIIRFilter, gst_audio_iir_filter,
77     GST_TYPE_AUDIO_FX_BASE_IIR_FILTER);
78
79 static void gst_audio_iir_filter_set_property (GObject * object, guint prop_id,
80     const GValue * value, GParamSpec * pspec);
81 static void gst_audio_iir_filter_get_property (GObject * object, guint prop_id,
82     GValue * value, GParamSpec * pspec);
83 static void gst_audio_iir_filter_finalize (GObject * object);
84
85 static gboolean gst_audio_iir_filter_setup (GstAudioFilter * base,
86     const GstAudioInfo * info);
87
88 static void
89 gst_audio_iir_filter_class_init (GstAudioIIRFilterClass * klass)
90 {
91   GObjectClass *gobject_class = (GObjectClass *) klass;
92   GstElementClass *gstelement_class = (GstElementClass *) klass;
93   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
94
95   GST_DEBUG_CATEGORY_INIT (gst_audio_iir_filter_debug, "audioiirfilter", 0,
96       "Generic audio IIR filter plugin");
97
98   gobject_class->set_property = gst_audio_iir_filter_set_property;
99   gobject_class->get_property = gst_audio_iir_filter_get_property;
100   gobject_class->finalize = gst_audio_iir_filter_finalize;
101
102   g_object_class_install_property (gobject_class, PROP_A,
103       g_param_spec_value_array ("a", "A",
104           "Filter coefficients (numerator of transfer function)",
105           g_param_spec_double ("Coefficient", "Filter Coefficient",
106               "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
107               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
108           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
109   g_object_class_install_property (gobject_class, PROP_B,
110       g_param_spec_value_array ("b", "B",
111           "Filter coefficients (denominator of transfer function)",
112           g_param_spec_double ("Coefficient", "Filter Coefficient",
113               "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
114               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
115           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116
117   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_iir_filter_setup);
118
119   /**
120    * GstAudioIIRFilter::rate-changed:
121    * @filter: the filter on which the signal is emitted
122    * @rate: the new sampling rate
123    *
124    * Will be emitted when the sampling rate changes. The callbacks
125    * will be called from the streaming thread and processing will
126    * stop until the event is handled.
127    */
128   gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED] =
129       g_signal_new ("rate-changed", G_TYPE_FROM_CLASS (klass),
130       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAudioIIRFilterClass, rate_changed),
131       NULL, NULL, gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
132
133   gst_element_class_set_details_simple (gstelement_class,
134       "Audio IIR filter", "Filter/Effect/Audio",
135       "Generic audio IIR filter with custom filter kernel",
136       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
137 }
138
139 static void
140 gst_audio_iir_filter_update_coefficients (GstAudioIIRFilter * self,
141     GValueArray * va, GValueArray * vb)
142 {
143   gdouble *a = NULL, *b = NULL;
144   guint i;
145
146   if (va) {
147     if (self->a)
148       g_value_array_free (self->a);
149
150     self->a = va;
151   }
152   if (vb) {
153     if (self->b)
154       g_value_array_free (self->b);
155
156     self->b = vb;
157   }
158
159   if (self->a && self->a->n_values > 0) {
160     a = g_new (gdouble, self->a->n_values);
161
162     for (i = 0; i < self->a->n_values; i++) {
163       GValue *v = g_value_array_get_nth (self->a, i);
164       a[i] = g_value_get_double (v);
165     }
166   }
167
168   if (self->b && self->b->n_values > 0) {
169     b = g_new (gdouble, self->b->n_values);
170     for (i = 0; i < self->b->n_values; i++) {
171       GValue *v = g_value_array_get_nth (self->b, i);
172       b[i] = g_value_get_double (v);
173     }
174   }
175
176   gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
177       (self), a, (self->a) ? self->a->n_values : 0, b,
178       (self->b) ? self->b->n_values : 0);
179 }
180
181 static void
182 gst_audio_iir_filter_init (GstAudioIIRFilter * self)
183 {
184   GValue v = { 0, };
185   GValueArray *a, *b;
186
187   a = g_value_array_new (1);
188
189   g_value_init (&v, G_TYPE_DOUBLE);
190   g_value_set_double (&v, 1.0);
191   g_value_array_append (a, &v);
192   g_value_unset (&v);
193
194   b = NULL;
195   gst_audio_iir_filter_update_coefficients (self, a, b);
196
197   self->lock = g_mutex_new ();
198 }
199
200 /* GstAudioFilter vmethod implementations */
201
202 /* get notified of caps and plug in the correct process function */
203 static gboolean
204 gst_audio_iir_filter_setup (GstAudioFilter * base, const GstAudioInfo * info)
205 {
206   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (base);
207   gint new_rate = GST_AUDIO_INFO_RATE (info);
208
209   if (GST_AUDIO_FILTER_RATE (self) != new_rate) {
210     g_signal_emit (G_OBJECT (self),
211         gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED], 0, new_rate);
212   }
213
214   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
215 }
216
217 static void
218 gst_audio_iir_filter_finalize (GObject * object)
219 {
220   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
221
222   g_mutex_free (self->lock);
223   self->lock = NULL;
224
225   if (self->a)
226     g_value_array_free (self->a);
227   self->a = NULL;
228   if (self->b)
229     g_value_array_free (self->b);
230   self->b = NULL;
231
232   G_OBJECT_CLASS (parent_class)->finalize (object);
233 }
234
235 static void
236 gst_audio_iir_filter_set_property (GObject * object, guint prop_id,
237     const GValue * value, GParamSpec * pspec)
238 {
239   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
240
241   g_return_if_fail (GST_IS_AUDIO_IIR_FILTER (self));
242
243   switch (prop_id) {
244     case PROP_A:
245       g_mutex_lock (self->lock);
246       gst_audio_iir_filter_update_coefficients (self, g_value_dup_boxed (value),
247           NULL);
248       g_mutex_unlock (self->lock);
249       break;
250     case PROP_B:
251       g_mutex_lock (self->lock);
252       gst_audio_iir_filter_update_coefficients (self, NULL,
253           g_value_dup_boxed (value));
254       g_mutex_unlock (self->lock);
255       break;
256     default:
257       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258       break;
259   }
260 }
261
262 static void
263 gst_audio_iir_filter_get_property (GObject * object, guint prop_id,
264     GValue * value, GParamSpec * pspec)
265 {
266   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
267
268   switch (prop_id) {
269     case PROP_A:
270       g_value_set_boxed (value, self->a);
271       break;
272     case PROP_B:
273       g_value_set_boxed (value, self->b);
274       break;
275     default:
276       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277       break;
278   }
279 }