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