scaletempo: Fix timestamp tracking
[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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, 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 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
49  * with newer GLib versions (>= 2.31.0) */
50 #define GLIB_DISABLE_DEPRECATION_WARNINGS
51
52 #ifdef HAVE_CONFIG_H
53 #include "config.h"
54 #endif
55
56 #include <string.h>
57 #include <math.h>
58 #include <gst/gst.h>
59 #include <gst/audio/gstaudiofilter.h>
60
61 #include "audiofirfilter.h"
62
63 #include "gst/glib-compat-private.h"
64
65 #define GST_CAT_DEFAULT gst_audio_fir_filter_debug
66 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
67
68 enum
69 {
70   SIGNAL_RATE_CHANGED,
71   LAST_SIGNAL
72 };
73
74 enum
75 {
76   PROP_0,
77   PROP_KERNEL,
78   PROP_LATENCY
79 };
80
81 static guint gst_audio_fir_filter_signals[LAST_SIGNAL] = { 0, };
82
83 #define gst_audio_fir_filter_parent_class parent_class
84 G_DEFINE_TYPE (GstAudioFIRFilter, gst_audio_fir_filter,
85     GST_TYPE_AUDIO_FX_BASE_FIR_FILTER);
86
87 static void gst_audio_fir_filter_set_property (GObject * object, guint prop_id,
88     const GValue * value, GParamSpec * pspec);
89 static void gst_audio_fir_filter_get_property (GObject * object, guint prop_id,
90     GValue * value, GParamSpec * pspec);
91 static void gst_audio_fir_filter_finalize (GObject * object);
92
93 static gboolean gst_audio_fir_filter_setup (GstAudioFilter * base,
94     const GstAudioInfo * info);
95
96
97 static void
98 gst_audio_fir_filter_class_init (GstAudioFIRFilterClass * klass)
99 {
100   GObjectClass *gobject_class = (GObjectClass *) klass;
101   GstElementClass *gstelement_class = (GstElementClass *) klass;
102   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
103
104   GST_DEBUG_CATEGORY_INIT (gst_audio_fir_filter_debug, "audiofirfilter", 0,
105       "Generic audio FIR filter plugin");
106
107   gobject_class->set_property = gst_audio_fir_filter_set_property;
108   gobject_class->get_property = gst_audio_fir_filter_get_property;
109   gobject_class->finalize = gst_audio_fir_filter_finalize;
110
111   g_object_class_install_property (gobject_class, PROP_KERNEL,
112       g_param_spec_value_array ("kernel", "Filter Kernel",
113           "Filter kernel for the FIR filter",
114           g_param_spec_double ("Element", "Filter Kernel Element",
115               "Element of the filter kernel", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
116               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
117           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
118   g_object_class_install_property (gobject_class, PROP_LATENCY,
119       g_param_spec_uint64 ("latency", "Latecy",
120           "Filter latency in samples",
121           0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
122
123   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fir_filter_setup);
124
125   /**
126    * GstAudioFIRFilter::rate-changed:
127    * @filter: the filter on which the signal is emitted
128    * @rate: the new sampling rate
129    *
130    * Will be emitted when the sampling rate changes. The callbacks
131    * will be called from the streaming thread and processing will
132    * stop until the event is handled.
133    */
134   gst_audio_fir_filter_signals[SIGNAL_RATE_CHANGED] =
135       g_signal_new ("rate-changed", G_TYPE_FROM_CLASS (klass),
136       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAudioFIRFilterClass, rate_changed),
137       NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_INT);
138
139   gst_element_class_set_static_metadata (gstelement_class,
140       "Audio FIR filter", "Filter/Effect/Audio",
141       "Generic audio FIR filter with custom filter kernel",
142       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
143 }
144
145 static void
146 gst_audio_fir_filter_update_kernel (GstAudioFIRFilter * self, GValueArray * va)
147 {
148   gdouble *kernel;
149   guint i;
150
151   if (va) {
152     if (self->kernel)
153       g_value_array_free (self->kernel);
154
155     self->kernel = va;
156   }
157
158   kernel = g_new (gdouble, self->kernel->n_values);
159
160   for (i = 0; i < self->kernel->n_values; i++) {
161     GValue *v = g_value_array_get_nth (self->kernel, i);
162     kernel[i] = g_value_get_double (v);
163   }
164
165   gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
166       kernel, self->kernel->n_values, self->latency, NULL);
167 }
168
169 static void
170 gst_audio_fir_filter_init (GstAudioFIRFilter * self)
171 {
172   GValue v = { 0, };
173   GValueArray *va;
174
175   self->latency = 0;
176   va = g_value_array_new (1);
177
178   g_value_init (&v, G_TYPE_DOUBLE);
179   g_value_set_double (&v, 1.0);
180   g_value_array_append (va, &v);
181   g_value_unset (&v);
182   gst_audio_fir_filter_update_kernel (self, va);
183
184   g_mutex_init (&self->lock);
185 }
186
187 /* GstAudioFilter vmethod implementations */
188
189 /* get notified of caps and plug in the correct process function */
190 static gboolean
191 gst_audio_fir_filter_setup (GstAudioFilter * base, const GstAudioInfo * info)
192 {
193   GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (base);
194   gint new_rate = GST_AUDIO_INFO_RATE (info);
195
196   if (GST_AUDIO_FILTER_RATE (self) != new_rate) {
197     g_signal_emit (G_OBJECT (self),
198         gst_audio_fir_filter_signals[SIGNAL_RATE_CHANGED], 0, new_rate);
199   }
200
201   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
202 }
203
204 static void
205 gst_audio_fir_filter_finalize (GObject * object)
206 {
207   GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object);
208
209   g_mutex_clear (&self->lock);
210
211   if (self->kernel)
212     g_value_array_free (self->kernel);
213   self->kernel = NULL;
214
215   G_OBJECT_CLASS (parent_class)->finalize (object);
216 }
217
218 static void
219 gst_audio_fir_filter_set_property (GObject * object, guint prop_id,
220     const GValue * value, GParamSpec * pspec)
221 {
222   GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object);
223
224   g_return_if_fail (GST_IS_AUDIO_FIR_FILTER (self));
225
226   switch (prop_id) {
227     case PROP_KERNEL:
228       g_mutex_lock (&self->lock);
229       /* update kernel already pushes residues */
230       gst_audio_fir_filter_update_kernel (self, g_value_dup_boxed (value));
231       g_mutex_unlock (&self->lock);
232       break;
233     case PROP_LATENCY:
234       g_mutex_lock (&self->lock);
235       self->latency = g_value_get_uint64 (value);
236       gst_audio_fir_filter_update_kernel (self, NULL);
237       g_mutex_unlock (&self->lock);
238       break;
239     default:
240       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241       break;
242   }
243 }
244
245 static void
246 gst_audio_fir_filter_get_property (GObject * object, guint prop_id,
247     GValue * value, GParamSpec * pspec)
248 {
249   GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object);
250
251   switch (prop_id) {
252     case PROP_KERNEL:
253       g_value_set_boxed (value, self->kernel);
254       break;
255     case PROP_LATENCY:
256       g_value_set_uint64 (value, self->latency);
257       break;
258     default:
259       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260       break;
261   }
262 }