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