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