Suppress deprecation warnings in selected files, for g_value_array_* mostly
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audioiirfilter.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-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  * |[
39  * <xi:include xmlns:xi="http://www.w3.org/2003/XInclude" parse="text" href="../../../../tests/examples/audiofx/iirfilter-example.c" />
40  * ]|
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 #include <gst/controller/gstcontroller.h>
57
58 #include "audioiirfilter.h"
59
60 #include "gst/glib-compat-private.h"
61
62 #define GST_CAT_DEFAULT gst_audio_iir_filter_debug
63 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
64
65 enum
66 {
67   SIGNAL_RATE_CHANGED,
68   LAST_SIGNAL
69 };
70
71 enum
72 {
73   PROP_0,
74   PROP_A,
75   PROP_B
76 };
77
78 static guint gst_audio_iir_filter_signals[LAST_SIGNAL] = { 0, };
79
80 #define DEBUG_INIT(bla) \
81   GST_DEBUG_CATEGORY_INIT (gst_audio_iir_filter_debug, "audioiirfilter", 0, \
82       "Generic audio IIR filter plugin");
83
84 GST_BOILERPLATE_FULL (GstAudioIIRFilter, gst_audio_iir_filter, GstAudioFilter,
85     GST_TYPE_AUDIO_FX_BASE_IIR_FILTER, DEBUG_INIT);
86
87 static void gst_audio_iir_filter_set_property (GObject * object, guint prop_id,
88     const GValue * value, GParamSpec * pspec);
89 static void gst_audio_iir_filter_get_property (GObject * object, guint prop_id,
90     GValue * value, GParamSpec * pspec);
91 static void gst_audio_iir_filter_finalize (GObject * object);
92
93 static gboolean gst_audio_iir_filter_setup (GstAudioFilter * base,
94     GstRingBufferSpec * format);
95
96 /* Element class */
97 static void
98 gst_audio_iir_filter_base_init (gpointer g_class)
99 {
100   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
101
102   gst_element_class_set_details_simple (element_class,
103       "Audio IIR filter", "Filter/Effect/Audio",
104       "Generic audio IIR filter with custom filter kernel",
105       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
106 }
107
108 static void
109 gst_audio_iir_filter_class_init (GstAudioIIRFilterClass * klass)
110 {
111   GObjectClass *gobject_class = (GObjectClass *) klass;
112   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
113
114   gobject_class->set_property = gst_audio_iir_filter_set_property;
115   gobject_class->get_property = gst_audio_iir_filter_get_property;
116   gobject_class->finalize = gst_audio_iir_filter_finalize;
117
118   g_object_class_install_property (gobject_class, PROP_A,
119       g_param_spec_value_array ("a", "A",
120           "Filter coefficients (numerator of transfer function)",
121           g_param_spec_double ("Coefficient", "Filter Coefficient",
122               "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
123               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
124           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125   g_object_class_install_property (gobject_class, PROP_B,
126       g_param_spec_value_array ("b", "B",
127           "Filter coefficients (denominator of transfer function)",
128           g_param_spec_double ("Coefficient", "Filter Coefficient",
129               "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
130               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
131           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132
133   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_iir_filter_setup);
134
135   /**
136    * GstAudioIIRFilter::rate-changed:
137    * @filter: the filter on which the signal is emitted
138    * @rate: the new sampling rate
139    *
140    * Will be emitted when the sampling rate changes. The callbacks
141    * will be called from the streaming thread and processing will
142    * stop until the event is handled.
143    */
144   gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED] =
145       g_signal_new ("rate-changed", G_TYPE_FROM_CLASS (klass),
146       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAudioIIRFilterClass, rate_changed),
147       NULL, NULL, gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
148 }
149
150 static void
151 gst_audio_iir_filter_update_coefficients (GstAudioIIRFilter * self,
152     GValueArray * va, GValueArray * vb)
153 {
154   gdouble *a = NULL, *b = NULL;
155   guint i;
156
157   if (va) {
158     if (self->a)
159       g_value_array_free (self->a);
160
161     self->a = va;
162   }
163   if (vb) {
164     if (self->b)
165       g_value_array_free (self->b);
166
167     self->b = vb;
168   }
169
170   if (self->a && self->a->n_values > 0) {
171     a = g_new (gdouble, self->a->n_values);
172
173     for (i = 0; i < self->a->n_values; i++) {
174       GValue *v = g_value_array_get_nth (self->a, i);
175       a[i] = g_value_get_double (v);
176     }
177   }
178
179   if (self->b && self->b->n_values > 0) {
180     b = g_new (gdouble, self->b->n_values);
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);
184     }
185   }
186
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);
190 }
191
192 static void
193 gst_audio_iir_filter_init (GstAudioIIRFilter * self,
194     GstAudioIIRFilterClass * g_class)
195 {
196   GValue v = { 0, };
197   GValueArray *a, *b;
198
199   a = g_value_array_new (1);
200
201   g_value_init (&v, G_TYPE_DOUBLE);
202   g_value_set_double (&v, 1.0);
203   g_value_array_append (a, &v);
204   g_value_unset (&v);
205
206   b = NULL;
207   gst_audio_iir_filter_update_coefficients (self, a, b);
208
209   self->lock = g_mutex_new ();
210 }
211
212 /* GstAudioFilter vmethod implementations */
213
214 /* get notified of caps and plug in the correct process function */
215 static gboolean
216 gst_audio_iir_filter_setup (GstAudioFilter * base, GstRingBufferSpec * format)
217 {
218   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (base);
219
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;
224   }
225
226   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, format);
227 }
228
229 static void
230 gst_audio_iir_filter_finalize (GObject * object)
231 {
232   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
233
234   g_mutex_free (self->lock);
235   self->lock = NULL;
236
237   if (self->a)
238     g_value_array_free (self->a);
239   self->a = NULL;
240   if (self->b)
241     g_value_array_free (self->b);
242   self->b = NULL;
243
244   G_OBJECT_CLASS (parent_class)->finalize (object);
245 }
246
247 static void
248 gst_audio_iir_filter_set_property (GObject * object, guint prop_id,
249     const GValue * value, GParamSpec * pspec)
250 {
251   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
252
253   g_return_if_fail (GST_IS_AUDIO_IIR_FILTER (self));
254
255   switch (prop_id) {
256     case PROP_A:
257       g_mutex_lock (self->lock);
258       gst_audio_iir_filter_update_coefficients (self, g_value_dup_boxed (value),
259           NULL);
260       g_mutex_unlock (self->lock);
261       break;
262     case PROP_B:
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);
267       break;
268     default:
269       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270       break;
271   }
272 }
273
274 static void
275 gst_audio_iir_filter_get_property (GObject * object, guint prop_id,
276     GValue * value, GParamSpec * pspec)
277 {
278   GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object);
279
280   switch (prop_id) {
281     case PROP_A:
282       g_value_set_boxed (value, self->a);
283       break;
284     case PROP_B:
285       g_value_set_boxed (value, self->b);
286       break;
287     default:
288       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289       break;
290   }
291 }