Merge remote-tracking branch 'origin/master' into 0.11
[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 #define GST_CAT_DEFAULT gst_gst_audio_wsincband_debug
67 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
68
69 enum
70 {
71   PROP_0,
72   PROP_LENGTH,
73   PROP_LOWER_FREQUENCY,
74   PROP_UPPER_FREQUENCY,
75   PROP_MODE,
76   PROP_WINDOW
77 };
78
79 enum
80 {
81   MODE_BAND_PASS = 0,
82   MODE_BAND_REJECT
83 };
84
85 #define GST_TYPE_AUDIO_WSINC_BAND_MODE (gst_gst_audio_wsincband_mode_get_type ())
86 static GType
87 gst_gst_audio_wsincband_mode_get_type (void)
88 {
89   static GType gtype = 0;
90
91   if (gtype == 0) {
92     static const GEnumValue values[] = {
93       {MODE_BAND_PASS, "Band pass (default)",
94           "band-pass"},
95       {MODE_BAND_REJECT, "Band reject",
96           "band-reject"},
97       {0, NULL, NULL}
98     };
99
100     gtype = g_enum_register_static ("GstAudioWSincBandMode", values);
101   }
102   return gtype;
103 }
104
105 enum
106 {
107   WINDOW_HAMMING = 0,
108   WINDOW_BLACKMAN,
109   WINDOW_GAUSSIAN,
110   WINDOW_COSINE,
111   WINDOW_HANN
112 };
113
114 #define GST_TYPE_AUDIO_WSINC_BAND_WINDOW (gst_gst_audio_wsincband_window_get_type ())
115 static GType
116 gst_gst_audio_wsincband_window_get_type (void)
117 {
118   static GType gtype = 0;
119
120   if (gtype == 0) {
121     static const GEnumValue values[] = {
122       {WINDOW_HAMMING, "Hamming window (default)",
123           "hamming"},
124       {WINDOW_BLACKMAN, "Blackman window",
125           "blackman"},
126       {WINDOW_GAUSSIAN, "Gaussian window",
127           "gaussian"},
128       {WINDOW_COSINE, "Cosine window",
129           "cosine"},
130       {WINDOW_HANN, "Hann window",
131           "hann"},
132       {0, NULL, NULL}
133     };
134
135     gtype = g_enum_register_static ("GstAudioWSincBandWindow", values);
136   }
137   return gtype;
138 }
139
140 #define gst_audio_wsincband_parent_class parent_class
141 G_DEFINE_TYPE (GstAudioWSincBand, gst_audio_wsincband,
142     GST_TYPE_AUDIO_FX_BASE_FIR_FILTER);
143
144 static void gst_audio_wsincband_set_property (GObject * object, guint prop_id,
145     const GValue * value, GParamSpec * pspec);
146 static void gst_audio_wsincband_get_property (GObject * object, guint prop_id,
147     GValue * value, GParamSpec * pspec);
148 static void gst_audio_wsincband_finalize (GObject * object);
149
150 static gboolean gst_audio_wsincband_setup (GstAudioFilter * base,
151     const GstAudioInfo * info);
152
153 #define POW2(x)  (x)*(x)
154
155 static void
156 gst_audio_wsincband_class_init (GstAudioWSincBandClass * klass)
157 {
158   GObjectClass *gobject_class = (GObjectClass *) klass;
159   GstElementClass *gstelement_class = (GstElementClass *) klass;
160   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
161
162   GST_DEBUG_CATEGORY_INIT (gst_gst_audio_wsincband_debug, "audiowsincband", 0,
163       "Band-pass and Band-reject Windowed sinc filter plugin");
164
165   gobject_class->set_property = gst_audio_wsincband_set_property;
166   gobject_class->get_property = gst_audio_wsincband_get_property;
167   gobject_class->finalize = gst_audio_wsincband_finalize;
168
169   /* FIXME: Don't use the complete possible range but restrict the upper boundary
170    * so automatically generated UIs can use a slider */
171   g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
172       g_param_spec_float ("lower-frequency", "Lower Frequency",
173           "Cut-off lower frequency (Hz)", 0.0, 100000.0, 0,
174           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
175   g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
176       g_param_spec_float ("upper-frequency", "Upper Frequency",
177           "Cut-off upper frequency (Hz)", 0.0, 100000.0, 0,
178           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
179   g_object_class_install_property (gobject_class, PROP_LENGTH,
180       g_param_spec_int ("length", "Length",
181           "Filter kernel length, will be rounded to the next odd number", 3,
182           256000, 101,
183           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
184
185   g_object_class_install_property (gobject_class, PROP_MODE,
186       g_param_spec_enum ("mode", "Mode",
187           "Band pass or band reject mode", GST_TYPE_AUDIO_WSINC_BAND_MODE,
188           MODE_BAND_PASS,
189           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
190
191   g_object_class_install_property (gobject_class, PROP_WINDOW,
192       g_param_spec_enum ("window", "Window",
193           "Window function to use", GST_TYPE_AUDIO_WSINC_BAND_WINDOW,
194           WINDOW_HAMMING,
195           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
196
197   gst_element_class_set_details_simple (gstelement_class,
198       "Band pass & band reject filter", "Filter/Effect/Audio",
199       "Band pass and band reject windowed sinc filter",
200       "Thomas Vander Stichele <thomas at apestaart dot org>, "
201       "Steven W. Smith, "
202       "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
203       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
204
205   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_wsincband_setup);
206 }
207
208 static void
209 gst_audio_wsincband_init (GstAudioWSincBand * self)
210 {
211   self->kernel_length = 101;
212   self->lower_frequency = 0.0;
213   self->upper_frequency = 0.0;
214   self->mode = MODE_BAND_PASS;
215   self->window = WINDOW_HAMMING;
216
217   self->lock = g_mutex_new ();
218 }
219
220 static void
221 gst_audio_wsincband_build_kernel (GstAudioWSincBand * self)
222 {
223   gint i = 0;
224   gdouble sum = 0.0;
225   gint len = 0;
226   gdouble *kernel_lp, *kernel_hp;
227   gdouble w;
228   gdouble *kernel;
229   gint rate, channels;
230
231   len = self->kernel_length;
232
233   rate = GST_AUDIO_FILTER_RATE (self);
234   channels = GST_AUDIO_FILTER_CHANNELS (self);
235
236   if (rate == 0) {
237     GST_DEBUG ("rate not set yet");
238     return;
239   }
240
241   if (channels == 0) {
242     GST_DEBUG ("channels not set yet");
243     return;
244   }
245
246   /* Clamp frequencies */
247   self->lower_frequency = CLAMP (self->lower_frequency, 0.0, rate / 2);
248   self->upper_frequency = CLAMP (self->upper_frequency, 0.0, rate / 2);
249
250   if (self->lower_frequency > self->upper_frequency) {
251     gint tmp = self->lower_frequency;
252
253     self->lower_frequency = self->upper_frequency;
254     self->upper_frequency = tmp;
255   }
256
257   GST_DEBUG ("gst_audio_wsincband: initializing filter kernel of length %d "
258       "with lower frequency %.2lf Hz "
259       ", upper frequency %.2lf Hz for mode %s",
260       len, self->lower_frequency, self->upper_frequency,
261       (self->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject");
262
263   /* fill the lp kernel */
264   w = 2 * G_PI * (self->lower_frequency / rate);
265   kernel_lp = g_new (gdouble, len);
266   for (i = 0; i < len; ++i) {
267     if (i == (len - 1) / 2.0)
268       kernel_lp[i] = w;
269     else
270       kernel_lp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
271
272     /* windowing */
273     switch (self->window) {
274       case WINDOW_HAMMING:
275         kernel_lp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
276         break;
277       case WINDOW_BLACKMAN:
278         kernel_lp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
279             0.08 * cos (4 * G_PI * i / (len - 1)));
280         break;
281       case WINDOW_GAUSSIAN:
282         kernel_lp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
283         break;
284       case WINDOW_COSINE:
285         kernel_lp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
286         break;
287       case WINDOW_HANN:
288         kernel_lp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
289         break;
290     }
291   }
292
293   /* normalize for unity gain at DC */
294   sum = 0.0;
295   for (i = 0; i < len; ++i)
296     sum += kernel_lp[i];
297   for (i = 0; i < len; ++i)
298     kernel_lp[i] /= sum;
299
300   /* fill the hp kernel */
301   w = 2 * G_PI * (self->upper_frequency / rate);
302   kernel_hp = g_new (gdouble, len);
303   for (i = 0; i < len; ++i) {
304     if (i == (len - 1) / 2.0)
305       kernel_hp[i] = w;
306     else
307       kernel_hp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
308
309     /* Windowing */
310     switch (self->window) {
311       case WINDOW_HAMMING:
312         kernel_hp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
313         break;
314       case WINDOW_BLACKMAN:
315         kernel_hp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
316             0.08 * cos (4 * G_PI * i / (len - 1)));
317         break;
318       case WINDOW_GAUSSIAN:
319         kernel_hp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
320         break;
321       case WINDOW_COSINE:
322         kernel_hp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
323         break;
324       case WINDOW_HANN:
325         kernel_hp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
326         break;
327     }
328   }
329
330   /* normalize for unity gain at DC */
331   sum = 0.0;
332   for (i = 0; i < len; ++i)
333     sum += kernel_hp[i];
334   for (i = 0; i < len; ++i)
335     kernel_hp[i] /= sum;
336
337   /* do spectral inversion to go from lowpass to highpass */
338   for (i = 0; i < len; ++i)
339     kernel_hp[i] = -kernel_hp[i];
340   if (len % 2 == 1) {
341     kernel_hp[(len - 1) / 2] += 1.0;
342   } else {
343     kernel_hp[len / 2 - 1] += 0.5;
344     kernel_hp[len / 2] += 0.5;
345   }
346
347   /* combine the two kernels */
348   kernel = g_new (gdouble, len);
349
350   for (i = 0; i < len; ++i)
351     kernel[i] = kernel_lp[i] + kernel_hp[i];
352
353   /* free the helper kernels */
354   g_free (kernel_lp);
355   g_free (kernel_hp);
356
357   /* do spectral inversion to go from bandreject to bandpass
358    * if specified */
359   if (self->mode == MODE_BAND_PASS) {
360     for (i = 0; i < len; ++i)
361       kernel[i] = -kernel[i];
362     kernel[len / 2] += 1;
363   }
364
365   gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
366       kernel, self->kernel_length, (len - 1) / 2);
367 }
368
369 /* GstAudioFilter vmethod implementations */
370
371 /* get notified of caps and plug in the correct process function */
372 static gboolean
373 gst_audio_wsincband_setup (GstAudioFilter * base, const GstAudioInfo * info)
374 {
375   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
376
377   gst_audio_wsincband_build_kernel (self);
378
379   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
380 }
381
382 static void
383 gst_audio_wsincband_finalize (GObject * object)
384 {
385   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
386
387   g_mutex_free (self->lock);
388   self->lock = NULL;
389
390   G_OBJECT_CLASS (parent_class)->finalize (object);
391 }
392
393 static void
394 gst_audio_wsincband_set_property (GObject * object, guint prop_id,
395     const GValue * value, GParamSpec * pspec)
396 {
397   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
398
399   g_return_if_fail (GST_IS_AUDIO_WSINC_BAND (self));
400
401   switch (prop_id) {
402     case PROP_LENGTH:{
403       gint val;
404
405       g_mutex_lock (self->lock);
406       val = g_value_get_int (value);
407       if (val % 2 == 0)
408         val++;
409
410       if (val != self->kernel_length) {
411         gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER
412             (self));
413         self->kernel_length = val;
414         gst_audio_wsincband_build_kernel (self);
415       }
416       g_mutex_unlock (self->lock);
417       break;
418     }
419     case PROP_LOWER_FREQUENCY:
420       g_mutex_lock (self->lock);
421       self->lower_frequency = g_value_get_float (value);
422       gst_audio_wsincband_build_kernel (self);
423       g_mutex_unlock (self->lock);
424       break;
425     case PROP_UPPER_FREQUENCY:
426       g_mutex_lock (self->lock);
427       self->upper_frequency = g_value_get_float (value);
428       gst_audio_wsincband_build_kernel (self);
429       g_mutex_unlock (self->lock);
430       break;
431     case PROP_MODE:
432       g_mutex_lock (self->lock);
433       self->mode = g_value_get_enum (value);
434       gst_audio_wsincband_build_kernel (self);
435       g_mutex_unlock (self->lock);
436       break;
437     case PROP_WINDOW:
438       g_mutex_lock (self->lock);
439       self->window = g_value_get_enum (value);
440       gst_audio_wsincband_build_kernel (self);
441       g_mutex_unlock (self->lock);
442       break;
443     default:
444       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
445       break;
446   }
447 }
448
449 static void
450 gst_audio_wsincband_get_property (GObject * object, guint prop_id,
451     GValue * value, GParamSpec * pspec)
452 {
453   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
454
455   switch (prop_id) {
456     case PROP_LENGTH:
457       g_value_set_int (value, self->kernel_length);
458       break;
459     case PROP_LOWER_FREQUENCY:
460       g_value_set_float (value, self->lower_frequency);
461       break;
462     case PROP_UPPER_FREQUENCY:
463       g_value_set_float (value, self->upper_frequency);
464       break;
465     case PROP_MODE:
466       g_value_set_enum (value, self->mode);
467       break;
468     case PROP_WINDOW:
469       g_value_set_enum (value, self->window);
470       break;
471     default:
472       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
473       break;
474   }
475 }