Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audiofirfilter.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-audiofirfilter
24  *
25  * audiofirfilter implements a generic audio <ulink url="http://en.wikipedia.org/wiki/Finite_impulse_response">FIR filter</ulink>. Before usage the
26  * "kernel" property has to be set to the filter kernel that should be
27  * used and the "latency" property has to be set to the latency (in samples)
28  * that is introduced by the filter kernel. Setting a latency of n samples
29  * will lead to the first n samples being dropped from the output and
30  * n samples added to the end.
31  *
32  * The filter kernel describes the impulse response of the filter. To
33  * calculate the frequency response of the filter you have to calculate
34  * the Fourier Transform of the impulse response.
35  *
36  * To change the filter kernel whenever the sampling rate changes the
37  * "rate-changed" signal can be used. This should be done for most
38  * FIR filters as they're depending on the sampling rate.
39  *
40  * <refsect2>
41  * <title>Example application</title>
42  * |[
43  * <xi:include xmlns:xi="http://www.w3.org/2003/XInclude" parse="text" href="../../../../tests/examples/audiofx/firfilter-example.c" />
44  * ]|
45  * </refsect2>
46  */
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include <string.h>
53 #include <math.h>
54 #include <gst/gst.h>
55 #include <gst/audio/gstaudiofilter.h>
56
57 #include "audiofirfilter.h"
58
59 #define GST_CAT_DEFAULT gst_audio_fir_filter_debug
60 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
61
62 enum
63 {
64   SIGNAL_RATE_CHANGED,
65   LAST_SIGNAL
66 };
67
68 enum
69 {
70   PROP_0,
71   PROP_KERNEL,
72   PROP_LATENCY
73 };
74
75 static guint gst_audio_fir_filter_signals[LAST_SIGNAL] = { 0, };
76
77 #define gst_audio_fir_filter_parent_class parent_class
78 G_DEFINE_TYPE (GstAudioFIRFilter, gst_audio_fir_filter,
79     GST_TYPE_AUDIO_FX_BASE_FIR_FILTER);
80
81 static void gst_audio_fir_filter_set_property (GObject * object, guint prop_id,
82     const GValue * value, GParamSpec * pspec);
83 static void gst_audio_fir_filter_get_property (GObject * object, guint prop_id,
84     GValue * value, GParamSpec * pspec);
85 static void gst_audio_fir_filter_finalize (GObject * object);
86
87 static gboolean gst_audio_fir_filter_setup (GstAudioFilter * base,
88     const GstAudioInfo * info);
89
90
91 static void
92 gst_audio_fir_filter_class_init (GstAudioFIRFilterClass * klass)
93 {
94   GObjectClass *gobject_class = (GObjectClass *) klass;
95   GstElementClass *gstelement_class = (GstElementClass *) klass;
96   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
97
98   GST_DEBUG_CATEGORY_INIT (gst_audio_fir_filter_debug, "audiofirfilter", 0,
99       "Generic audio FIR filter plugin");
100
101   gobject_class->set_property = gst_audio_fir_filter_set_property;
102   gobject_class->get_property = gst_audio_fir_filter_get_property;
103   gobject_class->finalize = gst_audio_fir_filter_finalize;
104
105   g_object_class_install_property (gobject_class, PROP_KERNEL,
106       g_param_spec_value_array ("kernel", "Filter Kernel",
107           "Filter kernel for the FIR filter",
108           g_param_spec_double ("Element", "Filter Kernel Element",
109               "Element of the filter kernel", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
110               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
111           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
112   g_object_class_install_property (gobject_class, PROP_LATENCY,
113       g_param_spec_uint64 ("latency", "Latecy",
114           "Filter latency in samples",
115           0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116
117   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fir_filter_setup);
118
119   /**
120    * GstAudioFIRFilter::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_fir_filter_signals[SIGNAL_RATE_CHANGED] =
129       g_signal_new ("rate-changed", G_TYPE_FROM_CLASS (klass),
130       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAudioFIRFilterClass, 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 FIR filter", "Filter/Effect/Audio",
135       "Generic audio FIR filter with custom filter kernel",
136       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
137 }
138
139 static void
140 gst_audio_fir_filter_update_kernel (GstAudioFIRFilter * self, GValueArray * va)
141 {
142   gdouble *kernel;
143   guint i;
144
145   if (va) {
146     if (self->kernel)
147       g_value_array_free (self->kernel);
148
149     self->kernel = va;
150   }
151
152   kernel = g_new (gdouble, self->kernel->n_values);
153
154   for (i = 0; i < self->kernel->n_values; i++) {
155     GValue *v = g_value_array_get_nth (self->kernel, i);
156     kernel[i] = g_value_get_double (v);
157   }
158
159   gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
160       kernel, self->kernel->n_values, self->latency);
161 }
162
163 static void
164 gst_audio_fir_filter_init (GstAudioFIRFilter * self)
165 {
166   GValue v = { 0, };
167   GValueArray *va;
168
169   self->latency = 0;
170   va = g_value_array_new (1);
171
172   g_value_init (&v, G_TYPE_DOUBLE);
173   g_value_set_double (&v, 1.0);
174   g_value_array_append (va, &v);
175   g_value_unset (&v);
176   gst_audio_fir_filter_update_kernel (self, va);
177
178   self->lock = g_mutex_new ();
179 }
180
181 /* GstAudioFilter vmethod implementations */
182
183 /* get notified of caps and plug in the correct process function */
184 static gboolean
185 gst_audio_fir_filter_setup (GstAudioFilter * base, const GstAudioInfo * info)
186 {
187   GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (base);
188   gint new_rate = GST_AUDIO_INFO_RATE (info);
189
190   if (GST_AUDIO_FILTER_RATE (self) != new_rate) {
191     g_signal_emit (G_OBJECT (self),
192         gst_audio_fir_filter_signals[SIGNAL_RATE_CHANGED], 0, new_rate);
193   }
194
195   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
196 }
197
198 static void
199 gst_audio_fir_filter_finalize (GObject * object)
200 {
201   GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object);
202
203   g_mutex_free (self->lock);
204   self->lock = NULL;
205
206   if (self->kernel)
207     g_value_array_free (self->kernel);
208   self->kernel = NULL;
209
210   G_OBJECT_CLASS (parent_class)->finalize (object);
211 }
212
213 static void
214 gst_audio_fir_filter_set_property (GObject * object, guint prop_id,
215     const GValue * value, GParamSpec * pspec)
216 {
217   GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object);
218
219   g_return_if_fail (GST_IS_AUDIO_FIR_FILTER (self));
220
221   switch (prop_id) {
222     case PROP_KERNEL:
223       g_mutex_lock (self->lock);
224       /* update kernel already pushes residues */
225       gst_audio_fir_filter_update_kernel (self, g_value_dup_boxed (value));
226       g_mutex_unlock (self->lock);
227       break;
228     case PROP_LATENCY:
229       g_mutex_lock (self->lock);
230       self->latency = g_value_get_uint64 (value);
231       gst_audio_fir_filter_update_kernel (self, NULL);
232       g_mutex_unlock (self->lock);
233       break;
234     default:
235       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236       break;
237   }
238 }
239
240 static void
241 gst_audio_fir_filter_get_property (GObject * object, guint prop_id,
242     GValue * value, GParamSpec * pspec)
243 {
244   GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object);
245
246   switch (prop_id) {
247     case PROP_KERNEL:
248       g_value_set_boxed (value, self->kernel);
249       break;
250     case PROP_LATENCY:
251       g_value_set_uint64 (value, self->latency);
252       break;
253     default:
254       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255       break;
256   }
257 }