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-audiofirfilter
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.
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.
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.
41 * <title>Example application</title>
43 * <xi:include xmlns:xi="http://www.w3.org/2003/XInclude" parse="text" href="../../../../tests/examples/audiofx/firfilter-example.c" />
55 #include <gst/audio/gstaudiofilter.h>
57 #include "audiofirfilter.h"
59 #include "gst/glib-compat-private.h"
61 #define GST_CAT_DEFAULT gst_audio_fir_filter_debug
62 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
77 static guint gst_audio_fir_filter_signals[LAST_SIGNAL] = { 0, };
79 #define gst_audio_fir_filter_parent_class parent_class
80 G_DEFINE_TYPE (GstAudioFIRFilter, gst_audio_fir_filter,
81 GST_TYPE_AUDIO_FX_BASE_FIR_FILTER);
83 static void gst_audio_fir_filter_set_property (GObject * object, guint prop_id,
84 const GValue * value, GParamSpec * pspec);
85 static void gst_audio_fir_filter_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
87 static void gst_audio_fir_filter_finalize (GObject * object);
89 static gboolean gst_audio_fir_filter_setup (GstAudioFilter * base,
90 const GstAudioInfo * info);
94 gst_audio_fir_filter_class_init (GstAudioFIRFilterClass * klass)
96 GObjectClass *gobject_class = (GObjectClass *) klass;
97 GstElementClass *gstelement_class = (GstElementClass *) klass;
98 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
100 GST_DEBUG_CATEGORY_INIT (gst_audio_fir_filter_debug, "audiofirfilter", 0,
101 "Generic audio FIR filter plugin");
103 gobject_class->set_property = gst_audio_fir_filter_set_property;
104 gobject_class->get_property = gst_audio_fir_filter_get_property;
105 gobject_class->finalize = gst_audio_fir_filter_finalize;
107 g_object_class_install_property (gobject_class, PROP_KERNEL,
108 g_param_spec_value_array ("kernel", "Filter Kernel",
109 "Filter kernel for the FIR filter",
110 g_param_spec_double ("Element", "Filter Kernel Element",
111 "Element of the filter kernel", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
112 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
113 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
114 g_object_class_install_property (gobject_class, PROP_LATENCY,
115 g_param_spec_uint64 ("latency", "Latecy",
116 "Filter latency in samples",
117 0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
119 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fir_filter_setup);
122 * GstAudioFIRFilter::rate-changed:
123 * @filter: the filter on which the signal is emitted
124 * @rate: the new sampling rate
126 * Will be emitted when the sampling rate changes. The callbacks
127 * will be called from the streaming thread and processing will
128 * stop until the event is handled.
130 gst_audio_fir_filter_signals[SIGNAL_RATE_CHANGED] =
131 g_signal_new ("rate-changed", G_TYPE_FROM_CLASS (klass),
132 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAudioFIRFilterClass, rate_changed),
133 NULL, NULL, gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
135 gst_element_class_set_details_simple (gstelement_class,
136 "Audio FIR filter", "Filter/Effect/Audio",
137 "Generic audio FIR filter with custom filter kernel",
138 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
142 gst_audio_fir_filter_update_kernel (GstAudioFIRFilter * self, GValueArray * va)
149 g_value_array_free (self->kernel);
154 kernel = g_new (gdouble, self->kernel->n_values);
156 for (i = 0; i < self->kernel->n_values; i++) {
157 GValue *v = g_value_array_get_nth (self->kernel, i);
158 kernel[i] = g_value_get_double (v);
161 gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
162 kernel, self->kernel->n_values, self->latency);
166 gst_audio_fir_filter_init (GstAudioFIRFilter * self)
172 va = g_value_array_new (1);
174 g_value_init (&v, G_TYPE_DOUBLE);
175 g_value_set_double (&v, 1.0);
176 g_value_array_append (va, &v);
178 gst_audio_fir_filter_update_kernel (self, va);
180 g_mutex_init (&self->lock);
183 /* GstAudioFilter vmethod implementations */
185 /* get notified of caps and plug in the correct process function */
187 gst_audio_fir_filter_setup (GstAudioFilter * base, const GstAudioInfo * info)
189 GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (base);
190 gint new_rate = GST_AUDIO_INFO_RATE (info);
192 if (GST_AUDIO_FILTER_RATE (self) != new_rate) {
193 g_signal_emit (G_OBJECT (self),
194 gst_audio_fir_filter_signals[SIGNAL_RATE_CHANGED], 0, new_rate);
197 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
201 gst_audio_fir_filter_finalize (GObject * object)
203 GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object);
205 g_mutex_clear (&self->lock);
208 g_value_array_free (self->kernel);
211 G_OBJECT_CLASS (parent_class)->finalize (object);
215 gst_audio_fir_filter_set_property (GObject * object, guint prop_id,
216 const GValue * value, GParamSpec * pspec)
218 GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object);
220 g_return_if_fail (GST_IS_AUDIO_FIR_FILTER (self));
224 g_mutex_lock (&self->lock);
225 /* update kernel already pushes residues */
226 gst_audio_fir_filter_update_kernel (self, g_value_dup_boxed (value));
227 g_mutex_unlock (&self->lock);
230 g_mutex_lock (&self->lock);
231 self->latency = g_value_get_uint64 (value);
232 gst_audio_fir_filter_update_kernel (self, NULL);
233 g_mutex_unlock (&self->lock);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 gst_audio_fir_filter_get_property (GObject * object, guint prop_id,
243 GValue * value, GParamSpec * pspec)
245 GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object);
249 g_value_set_boxed (value, self->kernel);
252 g_value_set_uint64 (value, self->latency);
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);