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