build: ignore GValueArray deprecation warnings for the time being
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audiowsincband.c
1 /* -*- c-basic-offset: 2 -*-
2  * 
3  * GStreamer
4  * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
5  *               2006 Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>
6  *               2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  * 
23  * 
24  * this windowed sinc filter is taken from the freely downloadable DSP book,
25  * "The Scientist and Engineer's Guide to Digital Signal Processing",
26  * chapter 16
27  * available at http://www.dspguide.com/
28  *
29  * For the window functions see
30  * http://en.wikipedia.org/wiki/Window_function
31  */
32
33 /**
34  * SECTION:element-audiowsincband
35  *
36  * Attenuates all frequencies outside (bandpass) or inside (bandreject) of a frequency
37  * band. The length parameter controls the rolloff, the window parameter
38  * controls rolloff and stopband attenuation. The Hamming window provides a faster rolloff but a bit
39  * worse stopband attenuation, the other way around for the Blackman window.
40  *
41  * This element has the advantage over the Chebyshev bandpass and bandreject filter that it has
42  * a much better rolloff when using a larger kernel size and almost linear phase. The only
43  * disadvantage is the much slower execution time with larger kernels.
44  *
45  * <refsect2>
46  * <title>Example launch line</title>
47  * |[
48  * gst-launch audiotestsrc freq=1500 ! audioconvert ! audiosincband mode=band-pass lower-frequency=3000 upper-frequency=10000 length=501 window=blackman ! audioconvert ! alsasink
49  * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiowsincband mode=band-reject lower-frequency=59 upper-frequency=61 length=10001 window=hamming ! audioconvert ! alsasink
50  * gst-launch audiotestsrc wave=white-noise ! audioconvert ! audiowsincband mode=band-pass lower-frequency=1000 upper-frequency=2000 length=31 ! audioconvert ! alsasink
51  * ]|
52  * </refsect2>
53  */
54
55 #ifdef HAVE_CONFIG_H
56 #include "config.h"
57 #endif
58
59 #include <string.h>
60 #include <math.h>
61 #include <gst/gst.h>
62 #include <gst/audio/gstaudiofilter.h>
63
64 #include "audiowsincband.h"
65
66 #include "gst/glib-compat-private.h"
67
68 #define GST_CAT_DEFAULT gst_gst_audio_wsincband_debug
69 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
70
71 enum
72 {
73   PROP_0,
74   PROP_LENGTH,
75   PROP_LOWER_FREQUENCY,
76   PROP_UPPER_FREQUENCY,
77   PROP_MODE,
78   PROP_WINDOW
79 };
80
81 enum
82 {
83   MODE_BAND_PASS = 0,
84   MODE_BAND_REJECT
85 };
86
87 #define GST_TYPE_AUDIO_WSINC_BAND_MODE (gst_gst_audio_wsincband_mode_get_type ())
88 static GType
89 gst_gst_audio_wsincband_mode_get_type (void)
90 {
91   static GType gtype = 0;
92
93   if (gtype == 0) {
94     static const GEnumValue values[] = {
95       {MODE_BAND_PASS, "Band pass (default)",
96           "band-pass"},
97       {MODE_BAND_REJECT, "Band reject",
98           "band-reject"},
99       {0, NULL, NULL}
100     };
101
102     gtype = g_enum_register_static ("GstAudioWSincBandMode", values);
103   }
104   return gtype;
105 }
106
107 enum
108 {
109   WINDOW_HAMMING = 0,
110   WINDOW_BLACKMAN,
111   WINDOW_GAUSSIAN,
112   WINDOW_COSINE,
113   WINDOW_HANN
114 };
115
116 #define GST_TYPE_AUDIO_WSINC_BAND_WINDOW (gst_gst_audio_wsincband_window_get_type ())
117 static GType
118 gst_gst_audio_wsincband_window_get_type (void)
119 {
120   static GType gtype = 0;
121
122   if (gtype == 0) {
123     static const GEnumValue values[] = {
124       {WINDOW_HAMMING, "Hamming window (default)",
125           "hamming"},
126       {WINDOW_BLACKMAN, "Blackman window",
127           "blackman"},
128       {WINDOW_GAUSSIAN, "Gaussian window",
129           "gaussian"},
130       {WINDOW_COSINE, "Cosine window",
131           "cosine"},
132       {WINDOW_HANN, "Hann window",
133           "hann"},
134       {0, NULL, NULL}
135     };
136
137     gtype = g_enum_register_static ("GstAudioWSincBandWindow", values);
138   }
139   return gtype;
140 }
141
142 #define gst_audio_wsincband_parent_class parent_class
143 G_DEFINE_TYPE (GstAudioWSincBand, gst_audio_wsincband,
144     GST_TYPE_AUDIO_FX_BASE_FIR_FILTER);
145
146 static void gst_audio_wsincband_set_property (GObject * object, guint prop_id,
147     const GValue * value, GParamSpec * pspec);
148 static void gst_audio_wsincband_get_property (GObject * object, guint prop_id,
149     GValue * value, GParamSpec * pspec);
150 static void gst_audio_wsincband_finalize (GObject * object);
151
152 static gboolean gst_audio_wsincband_setup (GstAudioFilter * base,
153     const GstAudioInfo * info);
154
155 #define POW2(x)  (x)*(x)
156
157 static void
158 gst_audio_wsincband_class_init (GstAudioWSincBandClass * klass)
159 {
160   GObjectClass *gobject_class = (GObjectClass *) klass;
161   GstElementClass *gstelement_class = (GstElementClass *) klass;
162   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
163
164   GST_DEBUG_CATEGORY_INIT (gst_gst_audio_wsincband_debug, "audiowsincband", 0,
165       "Band-pass and Band-reject Windowed sinc filter plugin");
166
167   gobject_class->set_property = gst_audio_wsincband_set_property;
168   gobject_class->get_property = gst_audio_wsincband_get_property;
169   gobject_class->finalize = gst_audio_wsincband_finalize;
170
171   /* FIXME: Don't use the complete possible range but restrict the upper boundary
172    * so automatically generated UIs can use a slider */
173   g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
174       g_param_spec_float ("lower-frequency", "Lower Frequency",
175           "Cut-off lower frequency (Hz)", 0.0, 100000.0, 0,
176           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
177   g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
178       g_param_spec_float ("upper-frequency", "Upper Frequency",
179           "Cut-off upper frequency (Hz)", 0.0, 100000.0, 0,
180           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
181   g_object_class_install_property (gobject_class, PROP_LENGTH,
182       g_param_spec_int ("length", "Length",
183           "Filter kernel length, will be rounded to the next odd number", 3,
184           256000, 101,
185           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
186
187   g_object_class_install_property (gobject_class, PROP_MODE,
188       g_param_spec_enum ("mode", "Mode",
189           "Band pass or band reject mode", GST_TYPE_AUDIO_WSINC_BAND_MODE,
190           MODE_BAND_PASS,
191           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
192
193   g_object_class_install_property (gobject_class, PROP_WINDOW,
194       g_param_spec_enum ("window", "Window",
195           "Window function to use", GST_TYPE_AUDIO_WSINC_BAND_WINDOW,
196           WINDOW_HAMMING,
197           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
198
199   gst_element_class_set_details_simple (gstelement_class,
200       "Band pass & band reject filter", "Filter/Effect/Audio",
201       "Band pass and band reject windowed sinc filter",
202       "Thomas Vander Stichele <thomas at apestaart dot org>, "
203       "Steven W. Smith, "
204       "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
205       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
206
207   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_wsincband_setup);
208 }
209
210 static void
211 gst_audio_wsincband_init (GstAudioWSincBand * self)
212 {
213   self->kernel_length = 101;
214   self->lower_frequency = 0.0;
215   self->upper_frequency = 0.0;
216   self->mode = MODE_BAND_PASS;
217   self->window = WINDOW_HAMMING;
218
219   g_mutex_init (&self->lock);
220 }
221
222 static void
223 gst_audio_wsincband_build_kernel (GstAudioWSincBand * self)
224 {
225   gint i = 0;
226   gdouble sum = 0.0;
227   gint len = 0;
228   gdouble *kernel_lp, *kernel_hp;
229   gdouble w;
230   gdouble *kernel;
231   gint rate, channels;
232
233   len = self->kernel_length;
234
235   rate = GST_AUDIO_FILTER_RATE (self);
236   channels = GST_AUDIO_FILTER_CHANNELS (self);
237
238   if (rate == 0) {
239     GST_DEBUG ("rate not set yet");
240     return;
241   }
242
243   if (channels == 0) {
244     GST_DEBUG ("channels not set yet");
245     return;
246   }
247
248   /* Clamp frequencies */
249   self->lower_frequency = CLAMP (self->lower_frequency, 0.0, rate / 2);
250   self->upper_frequency = CLAMP (self->upper_frequency, 0.0, rate / 2);
251
252   if (self->lower_frequency > self->upper_frequency) {
253     gint tmp = self->lower_frequency;
254
255     self->lower_frequency = self->upper_frequency;
256     self->upper_frequency = tmp;
257   }
258
259   GST_DEBUG ("gst_audio_wsincband: initializing filter kernel of length %d "
260       "with lower frequency %.2lf Hz "
261       ", upper frequency %.2lf Hz for mode %s",
262       len, self->lower_frequency, self->upper_frequency,
263       (self->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject");
264
265   /* fill the lp kernel */
266   w = 2 * G_PI * (self->lower_frequency / rate);
267   kernel_lp = g_new (gdouble, len);
268   for (i = 0; i < len; ++i) {
269     if (i == (len - 1) / 2.0)
270       kernel_lp[i] = w;
271     else
272       kernel_lp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
273
274     /* windowing */
275     switch (self->window) {
276       case WINDOW_HAMMING:
277         kernel_lp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
278         break;
279       case WINDOW_BLACKMAN:
280         kernel_lp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
281             0.08 * cos (4 * G_PI * i / (len - 1)));
282         break;
283       case WINDOW_GAUSSIAN:
284         kernel_lp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
285         break;
286       case WINDOW_COSINE:
287         kernel_lp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
288         break;
289       case WINDOW_HANN:
290         kernel_lp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
291         break;
292     }
293   }
294
295   /* normalize for unity gain at DC */
296   sum = 0.0;
297   for (i = 0; i < len; ++i)
298     sum += kernel_lp[i];
299   for (i = 0; i < len; ++i)
300     kernel_lp[i] /= sum;
301
302   /* fill the hp kernel */
303   w = 2 * G_PI * (self->upper_frequency / rate);
304   kernel_hp = g_new (gdouble, len);
305   for (i = 0; i < len; ++i) {
306     if (i == (len - 1) / 2.0)
307       kernel_hp[i] = w;
308     else
309       kernel_hp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
310
311     /* Windowing */
312     switch (self->window) {
313       case WINDOW_HAMMING:
314         kernel_hp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
315         break;
316       case WINDOW_BLACKMAN:
317         kernel_hp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
318             0.08 * cos (4 * G_PI * i / (len - 1)));
319         break;
320       case WINDOW_GAUSSIAN:
321         kernel_hp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
322         break;
323       case WINDOW_COSINE:
324         kernel_hp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
325         break;
326       case WINDOW_HANN:
327         kernel_hp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
328         break;
329     }
330   }
331
332   /* normalize for unity gain at DC */
333   sum = 0.0;
334   for (i = 0; i < len; ++i)
335     sum += kernel_hp[i];
336   for (i = 0; i < len; ++i)
337     kernel_hp[i] /= sum;
338
339   /* do spectral inversion to go from lowpass to highpass */
340   for (i = 0; i < len; ++i)
341     kernel_hp[i] = -kernel_hp[i];
342   if (len % 2 == 1) {
343     kernel_hp[(len - 1) / 2] += 1.0;
344   } else {
345     kernel_hp[len / 2 - 1] += 0.5;
346     kernel_hp[len / 2] += 0.5;
347   }
348
349   /* combine the two kernels */
350   kernel = g_new (gdouble, len);
351
352   for (i = 0; i < len; ++i)
353     kernel[i] = kernel_lp[i] + kernel_hp[i];
354
355   /* free the helper kernels */
356   g_free (kernel_lp);
357   g_free (kernel_hp);
358
359   /* do spectral inversion to go from bandreject to bandpass
360    * if specified */
361   if (self->mode == MODE_BAND_PASS) {
362     for (i = 0; i < len; ++i)
363       kernel[i] = -kernel[i];
364     kernel[len / 2] += 1;
365   }
366
367   gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
368       kernel, self->kernel_length, (len - 1) / 2);
369 }
370
371 /* GstAudioFilter vmethod implementations */
372
373 /* get notified of caps and plug in the correct process function */
374 static gboolean
375 gst_audio_wsincband_setup (GstAudioFilter * base, const GstAudioInfo * info)
376 {
377   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
378
379   gst_audio_wsincband_build_kernel (self);
380
381   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
382 }
383
384 static void
385 gst_audio_wsincband_finalize (GObject * object)
386 {
387   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
388
389   g_mutex_clear (&self->lock);
390
391   G_OBJECT_CLASS (parent_class)->finalize (object);
392 }
393
394 static void
395 gst_audio_wsincband_set_property (GObject * object, guint prop_id,
396     const GValue * value, GParamSpec * pspec)
397 {
398   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
399
400   g_return_if_fail (GST_IS_AUDIO_WSINC_BAND (self));
401
402   switch (prop_id) {
403     case PROP_LENGTH:{
404       gint val;
405
406       g_mutex_lock (&self->lock);
407       val = g_value_get_int (value);
408       if (val % 2 == 0)
409         val++;
410
411       if (val != self->kernel_length) {
412         gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER
413             (self));
414         self->kernel_length = val;
415         gst_audio_wsincband_build_kernel (self);
416       }
417       g_mutex_unlock (&self->lock);
418       break;
419     }
420     case PROP_LOWER_FREQUENCY:
421       g_mutex_lock (&self->lock);
422       self->lower_frequency = g_value_get_float (value);
423       gst_audio_wsincband_build_kernel (self);
424       g_mutex_unlock (&self->lock);
425       break;
426     case PROP_UPPER_FREQUENCY:
427       g_mutex_lock (&self->lock);
428       self->upper_frequency = g_value_get_float (value);
429       gst_audio_wsincband_build_kernel (self);
430       g_mutex_unlock (&self->lock);
431       break;
432     case PROP_MODE:
433       g_mutex_lock (&self->lock);
434       self->mode = g_value_get_enum (value);
435       gst_audio_wsincband_build_kernel (self);
436       g_mutex_unlock (&self->lock);
437       break;
438     case PROP_WINDOW:
439       g_mutex_lock (&self->lock);
440       self->window = g_value_get_enum (value);
441       gst_audio_wsincband_build_kernel (self);
442       g_mutex_unlock (&self->lock);
443       break;
444     default:
445       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
446       break;
447   }
448 }
449
450 static void
451 gst_audio_wsincband_get_property (GObject * object, guint prop_id,
452     GValue * value, GParamSpec * pspec)
453 {
454   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
455
456   switch (prop_id) {
457     case PROP_LENGTH:
458       g_value_set_int (value, self->kernel_length);
459       break;
460     case PROP_LOWER_FREQUENCY:
461       g_value_set_float (value, self->lower_frequency);
462       break;
463     case PROP_UPPER_FREQUENCY:
464       g_value_set_float (value, self->upper_frequency);
465       break;
466     case PROP_MODE:
467       g_value_set_enum (value, self->mode);
468       break;
469     case PROP_WINDOW:
470       g_value_set_enum (value, self->window);
471       break;
472     default:
473       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
474       break;
475   }
476 }