Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audiowsinclimit.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-audiowsinclimit
35  *
36  * Attenuates all frequencies above the cutoff frequency (low-pass) or all frequencies below the
37  * cutoff frequency (high-pass). 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 lowpass and highpass 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 ! audiowsinclimit mode=low-pass frequency=1000 length=501 ! audioconvert ! alsasink
49  * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiowsinclimit mode=high-pass frequency=15000 length=501 ! audioconvert ! alsasink
50  * gst-launch audiotestsrc wave=white-noise ! audioconvert ! audiowsinclimit mode=low-pass frequency=1000 length=10001 window=blackman ! 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 "audiowsinclimit.h"
65
66 #define GST_CAT_DEFAULT gst_audio_wsinclimit_debug
67 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
68
69 enum
70 {
71   PROP_0,
72   PROP_LENGTH,
73   PROP_FREQUENCY,
74   PROP_MODE,
75   PROP_WINDOW
76 };
77
78 enum
79 {
80   MODE_LOW_PASS = 0,
81   MODE_HIGH_PASS
82 };
83
84 #define GST_TYPE_AUDIO_WSINC_LIMIT_MODE (gst_audio_wsinclimit_mode_get_type ())
85 static GType
86 gst_audio_wsinclimit_mode_get_type (void)
87 {
88   static GType gtype = 0;
89
90   if (gtype == 0) {
91     static const GEnumValue values[] = {
92       {MODE_LOW_PASS, "Low pass (default)",
93           "low-pass"},
94       {MODE_HIGH_PASS, "High pass",
95           "high-pass"},
96       {0, NULL, NULL}
97     };
98
99     gtype = g_enum_register_static ("GstAudioWSincLimitMode", values);
100   }
101   return gtype;
102 }
103
104 enum
105 {
106   WINDOW_HAMMING = 0,
107   WINDOW_BLACKMAN,
108   WINDOW_GAUSSIAN,
109   WINDOW_COSINE,
110   WINDOW_HANN
111 };
112
113 #define GST_TYPE_AUDIO_WSINC_LIMIT_WINDOW (gst_audio_wsinclimit_window_get_type ())
114 static GType
115 gst_audio_wsinclimit_window_get_type (void)
116 {
117   static GType gtype = 0;
118
119   if (gtype == 0) {
120     static const GEnumValue values[] = {
121       {WINDOW_HAMMING, "Hamming window (default)",
122           "hamming"},
123       {WINDOW_BLACKMAN, "Blackman window",
124           "blackman"},
125       {WINDOW_GAUSSIAN, "Gaussian window",
126           "gaussian"},
127       {WINDOW_COSINE, "Cosine window",
128           "cosine"},
129       {WINDOW_HANN, "Hann window",
130           "hann"},
131       {0, NULL, NULL}
132     };
133
134     gtype = g_enum_register_static ("GstAudioWSincLimitWindow", values);
135   }
136   return gtype;
137 }
138
139 #define gst_audio_wsinclimit_parent_class parent_class
140 G_DEFINE_TYPE (GstAudioWSincLimit, gst_audio_wsinclimit,
141     GST_TYPE_AUDIO_FX_BASE_FIR_FILTER);
142
143 static void gst_audio_wsinclimit_set_property (GObject * object, guint prop_id,
144     const GValue * value, GParamSpec * pspec);
145 static void gst_audio_wsinclimit_get_property (GObject * object, guint prop_id,
146     GValue * value, GParamSpec * pspec);
147 static void gst_audio_wsinclimit_finalize (GObject * object);
148
149 static gboolean gst_audio_wsinclimit_setup (GstAudioFilter * base,
150     const GstAudioInfo * info);
151
152
153 #define POW2(x)  (x)*(x)
154
155 static void
156 gst_audio_wsinclimit_class_init (GstAudioWSincLimitClass * 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_audio_wsinclimit_debug, "audiowsinclimit", 0,
163       "Low-pass and High-pass Windowed sinc filter plugin");
164
165   gobject_class->set_property = gst_audio_wsinclimit_set_property;
166   gobject_class->get_property = gst_audio_wsinclimit_get_property;
167   gobject_class->finalize = gst_audio_wsinclimit_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_FREQUENCY,
172       g_param_spec_float ("cutoff", "Cutoff",
173           "Cut-off Frequency (Hz)", 0.0, 100000.0, 0.0,
174           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
175   g_object_class_install_property (gobject_class, PROP_LENGTH,
176       g_param_spec_int ("length", "Length",
177           "Filter kernel length, will be rounded to the next odd number",
178           3, 256000, 101,
179           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
180
181   g_object_class_install_property (gobject_class, PROP_MODE,
182       g_param_spec_enum ("mode", "Mode",
183           "Low pass or high pass mode", GST_TYPE_AUDIO_WSINC_LIMIT_MODE,
184           MODE_LOW_PASS,
185           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
186
187   g_object_class_install_property (gobject_class, PROP_WINDOW,
188       g_param_spec_enum ("window", "Window",
189           "Window function to use", GST_TYPE_AUDIO_WSINC_LIMIT_WINDOW,
190           WINDOW_HAMMING,
191           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
192
193   gst_element_class_set_details_simple (gstelement_class,
194       "Low pass & high pass filter", "Filter/Effect/Audio",
195       "Low pass and high pass windowed sinc filter",
196       "Thomas Vander Stichele <thomas at apestaart dot org>, "
197       "Steven W. Smith, "
198       "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
199       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
200
201   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_wsinclimit_setup);
202 }
203
204 static void
205 gst_audio_wsinclimit_init (GstAudioWSincLimit * self)
206 {
207   self->mode = MODE_LOW_PASS;
208   self->window = WINDOW_HAMMING;
209   self->kernel_length = 101;
210   self->cutoff = 0.0;
211
212   self->lock = g_mutex_new ();
213 }
214
215 static void
216 gst_audio_wsinclimit_build_kernel (GstAudioWSincLimit * self)
217 {
218   gint i = 0;
219   gdouble sum = 0.0;
220   gint len = 0;
221   gdouble w;
222   gdouble *kernel = NULL;
223   gint rate, channels;
224
225   len = self->kernel_length;
226
227   rate = GST_AUDIO_FILTER_RATE (self);
228   channels = GST_AUDIO_FILTER_CHANNELS (self);
229
230   if (rate == 0) {
231     GST_DEBUG ("rate not set yet");
232     return;
233   }
234
235   if (channels == 0) {
236     GST_DEBUG ("channels not set yet");
237     return;
238   }
239
240   /* Clamp cutoff frequency between 0 and the nyquist frequency */
241   self->cutoff = CLAMP (self->cutoff, 0.0, rate / 2);
242
243   GST_DEBUG ("gst_audio_wsinclimit_: initializing filter kernel of length %d "
244       "with cutoff %.2lf Hz "
245       "for mode %s",
246       len, self->cutoff,
247       (self->mode == MODE_LOW_PASS) ? "low-pass" : "high-pass");
248
249   /* fill the kernel */
250   w = 2 * G_PI * (self->cutoff / rate);
251
252   kernel = g_new (gdouble, len);
253
254   for (i = 0; i < len; ++i) {
255     if (i == (len - 1) / 2.0)
256       kernel[i] = w;
257     else
258       kernel[i] = sin (w * (i - (len - 1) / 2)) / (i - (len - 1) / 2.0);
259
260     /* windowing */
261     switch (self->window) {
262       case WINDOW_HAMMING:
263         kernel[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
264         break;
265       case WINDOW_BLACKMAN:
266         kernel[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
267             0.08 * cos (4 * G_PI * i / (len - 1)));
268         break;
269       case WINDOW_GAUSSIAN:
270         kernel[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
271         break;
272       case WINDOW_COSINE:
273         kernel[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
274         break;
275       case WINDOW_HANN:
276         kernel[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
277         break;
278     }
279   }
280
281   /* normalize for unity gain at DC */
282   for (i = 0; i < len; ++i)
283     sum += kernel[i];
284   for (i = 0; i < len; ++i)
285     kernel[i] /= sum;
286
287   /* convert to highpass if specified */
288   if (self->mode == MODE_HIGH_PASS) {
289     for (i = 0; i < len; ++i)
290       kernel[i] = -kernel[i];
291
292     if (len % 2 == 1) {
293       kernel[(len - 1) / 2] += 1.0;
294     } else {
295       kernel[len / 2 - 1] += 0.5;
296       kernel[len / 2] += 0.5;
297     }
298   }
299
300   gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
301       kernel, self->kernel_length, (len - 1) / 2);
302 }
303
304 /* GstAudioFilter vmethod implementations */
305
306 /* get notified of caps and plug in the correct process function */
307 static gboolean
308 gst_audio_wsinclimit_setup (GstAudioFilter * base, const GstAudioInfo * info)
309 {
310   GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (base);
311
312   gst_audio_wsinclimit_build_kernel (self);
313
314   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
315 }
316
317 static void
318 gst_audio_wsinclimit_finalize (GObject * object)
319 {
320   GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (object);
321
322   g_mutex_free (self->lock);
323   self->lock = NULL;
324
325   G_OBJECT_CLASS (parent_class)->finalize (object);
326 }
327
328 static void
329 gst_audio_wsinclimit_set_property (GObject * object, guint prop_id,
330     const GValue * value, GParamSpec * pspec)
331 {
332   GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (object);
333
334   g_return_if_fail (GST_IS_AUDIO_WSINC_LIMIT (self));
335
336   switch (prop_id) {
337     case PROP_LENGTH:{
338       gint val;
339
340       g_mutex_lock (self->lock);
341       val = g_value_get_int (value);
342       if (val % 2 == 0)
343         val++;
344
345       if (val != self->kernel_length) {
346         gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER
347             (self));
348         self->kernel_length = val;
349         gst_audio_wsinclimit_build_kernel (self);
350       }
351       g_mutex_unlock (self->lock);
352       break;
353     }
354     case PROP_FREQUENCY:
355       g_mutex_lock (self->lock);
356       self->cutoff = g_value_get_float (value);
357       gst_audio_wsinclimit_build_kernel (self);
358       g_mutex_unlock (self->lock);
359       break;
360     case PROP_MODE:
361       g_mutex_lock (self->lock);
362       self->mode = g_value_get_enum (value);
363       gst_audio_wsinclimit_build_kernel (self);
364       g_mutex_unlock (self->lock);
365       break;
366     case PROP_WINDOW:
367       g_mutex_lock (self->lock);
368       self->window = g_value_get_enum (value);
369       gst_audio_wsinclimit_build_kernel (self);
370       g_mutex_unlock (self->lock);
371       break;
372     default:
373       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374       break;
375   }
376 }
377
378 static void
379 gst_audio_wsinclimit_get_property (GObject * object, guint prop_id,
380     GValue * value, GParamSpec * pspec)
381 {
382   GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (object);
383
384   switch (prop_id) {
385     case PROP_LENGTH:
386       g_value_set_int (value, self->kernel_length);
387       break;
388     case PROP_FREQUENCY:
389       g_value_set_float (value, self->cutoff);
390       break;
391     case PROP_MODE:
392       g_value_set_enum (value, self->mode);
393       break;
394     case PROP_WINDOW:
395       g_value_set_enum (value, self->window);
396       break;
397     default:
398       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
399       break;
400   }
401 }