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