3 * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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.
23 * SECTION:element-audioiirfilter
24 * @short_description: Generic audio IIR filter
28 * audioiirfilter implements a generic audio <ulink url="http://en.wikipedia.org/wiki/Infinite_impulse_response">IIR filter</ulink>. Before usage the
29 * "a" and "b" properties have to be set to the filter coefficients that
33 * The filter coefficients describe the numerator and denominator of the
37 * To change the filter coefficients whenever the sampling rate changes the
38 * "rate-changed" signal can be used. This should be done for most
39 * IIR filters as they're depending on the sampling rate.
41 * <title>Example application</title>
43 * <include xmlns="http://www.w3.org/2003/XInclude" href="element-iirfilter-example.xml" />
55 #include <gst/audio/gstaudiofilter.h>
56 #include <gst/controller/gstcontroller.h>
58 #include "audioiirfilter.h"
60 #define GST_CAT_DEFAULT gst_audio_iir_filter_debug
61 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
76 static guint gst_audio_iir_filter_signals[LAST_SIGNAL] = { 0, };
78 #define DEBUG_INIT(bla) \
79 GST_DEBUG_CATEGORY_INIT (gst_audio_iir_filter_debug, "audioiirfilter", 0, \
80 "Generic audio IIR filter plugin");
82 GST_BOILERPLATE_FULL (GstAudioIIRFilter, gst_audio_iir_filter, GstAudioFilter,
83 GST_TYPE_AUDIO_FX_BASE_IIR_FILTER, DEBUG_INIT);
85 static void gst_audio_iir_filter_set_property (GObject * object, guint prop_id,
86 const GValue * value, GParamSpec * pspec);
87 static void gst_audio_iir_filter_get_property (GObject * object, guint prop_id,
88 GValue * value, GParamSpec * pspec);
89 static void gst_audio_iir_filter_finalize (GObject * object);
91 static gboolean gst_audio_iir_filter_setup (GstAudioFilter * base,
92 GstRingBufferSpec * format);
96 gst_audio_iir_filter_base_init (gpointer g_class)
98 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
100 gst_element_class_set_details_simple (element_class,
101 "Audio IIR filter", "Filter/Effect/Audio",
102 "Generic audio IIR filter with custom filter kernel",
103 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
107 gst_audio_iir_filter_class_init (GstAudioIIRFilterClass * klass)
109 GObjectClass *gobject_class = (GObjectClass *) klass;
110 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
112 gobject_class->set_property = gst_audio_iir_filter_set_property;
113 gobject_class->get_property = gst_audio_iir_filter_get_property;
114 gobject_class->finalize = gst_audio_iir_filter_finalize;
116 g_object_class_install_property (gobject_class, PROP_A,
117 g_param_spec_value_array ("a", "A",
118 "Filter coefficients (numerator of transfer function)",
119 g_param_spec_double ("Coefficient", "Filter Coefficient",
120 "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
121 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
122 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
123 g_object_class_install_property (gobject_class, PROP_B,
124 g_param_spec_value_array ("b", "B",
125 "Filter coefficients (denominator of transfer function)",
126 g_param_spec_double ("Coefficient", "Filter Coefficient",
127 "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
128 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
129 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
131 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_iir_filter_setup);
134 * GstAudioIIRFilter::rate-changed:
135 * @filter: the filter on which the signal is emitted
136 * @rate: the new sampling rate
138 * Will be emitted when the sampling rate changes. The callbacks
139 * will be called from the streaming thread and processing will
140 * stop until the event is handled.
142 gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED] =
143 g_signal_new ("rate-changed", G_TYPE_FROM_CLASS (klass),
144 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAudioIIRFilterClass, rate_changed),
145 NULL, NULL, gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
149 gst_audio_iir_filter_update_coefficients (GstAudioIIRFilter * self,
150 GValueArray * va, GValueArray * vb)
152 gdouble *a = NULL, *b = NULL;
157 g_value_array_free (self->a);
163 g_value_array_free (self->b);
168 if (self->a && self->a->n_values > 0)
169 a = g_new (gdouble, self->a->n_values);
170 if (self->b && self->b->n_values > 0)
171 b = g_new (gdouble, self->b->n_values);
174 for (i = 0; i < self->a->n_values; i++) {
175 GValue *v = g_value_array_get_nth (self->a, i);
176 a[i] = g_value_get_double (v);
181 for (i = 0; i < self->b->n_values; i++) {
182 GValue *v = g_value_array_get_nth (self->b, i);
183 b[i] = g_value_get_double (v);
187 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
188 (self), a, (self->a) ? self->a->n_values : 0, b,
189 (self->b) ? self->b->n_values : 0);
193 gst_audio_iir_filter_init (GstAudioIIRFilter * self,
194 GstAudioIIRFilterClass * g_class)
199 a = g_value_array_new (1);
201 g_value_init (&v, G_TYPE_DOUBLE);
202 g_value_set_double (&v, 1.0);
203 g_value_array_append (a, &v);
207 gst_audio_iir_filter_update_coefficients (self, a, b);
209 self->lock = g_mutex_new ();
212 /* GstAudioFilter vmethod implementations */
214 /* get notified of caps and plug in the correct process function */
216 gst_audio_iir_filter_setup (GstAudioFilter * base, GstRingBufferSpec * format)
218 GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (base);
220 if (self->rate != format->rate) {
221 g_signal_emit (G_OBJECT (self),
222 gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED], 0, format->rate);
223 self->rate = format->rate;
226 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, format);
230 gst_audio_iir_filter_finalize (GObject * object)
232 GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
234 g_mutex_free (self->lock);
238 g_value_array_free (self->a);
241 g_value_array_free (self->b);
244 G_OBJECT_CLASS (parent_class)->finalize (object);
248 gst_audio_iir_filter_set_property (GObject * object, guint prop_id,
249 const GValue * value, GParamSpec * pspec)
251 GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
253 g_return_if_fail (GST_IS_AUDIO_IIR_FILTER (self));
257 g_mutex_lock (self->lock);
258 gst_audio_iir_filter_update_coefficients (self, g_value_dup_boxed (value),
260 g_mutex_unlock (self->lock);
263 g_mutex_lock (self->lock);
264 gst_audio_iir_filter_update_coefficients (self, NULL,
265 g_value_dup_boxed (value));
266 g_mutex_unlock (self->lock);
269 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275 gst_audio_iir_filter_get_property (GObject * object, guint prop_id,
276 GValue * value, GParamSpec * pspec)
278 GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
282 g_value_set_boxed (value, self->a);
285 g_value_set_boxed (value, self->b);
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);