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