• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, 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  * @title: audiowsincband
36  *
37  * Attenuates all frequencies outside (bandpass) or inside (bandreject) of a frequency
38  * band. The length parameter controls the rolloff, the window parameter
39  * controls rolloff and stopband attenuation. The Hamming window provides a faster rolloff but a bit
40  * worse stopband attenuation, the other way around for the Blackman window.
41  *
42  * This element has the advantage over the Chebyshev bandpass and bandreject filter that it has
43  * a much better rolloff when using a larger kernel size and almost linear phase. The only
44  * disadvantage is the much slower execution time with larger kernels.
45  *
46  * ## Example launch line
47  * |[
48  * gst-launch-1.0 audiotestsrc freq=1500 ! audioconvert ! audiowsincband mode=band-pass lower-frequency=3000 upper-frequency=10000 length=501 window=blackman ! audioconvert ! alsasink
49  * gst-launch-1.0 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-1.0 audiotestsrc wave=white-noise ! audioconvert ! audiowsincband mode=band-pass lower-frequency=1000 upper-frequency=2000 length=31 ! audioconvert ! alsasink
51  * ]|
52  *
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 #include "gst/glib-compat-private.h"
67 
68 #define GST_CAT_DEFAULT gst_gst_audio_wsincband_debug
69 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
70 
71 enum
72 {
73   PROP_0,
74   PROP_LENGTH,
75   PROP_LOWER_FREQUENCY,
76   PROP_UPPER_FREQUENCY,
77   PROP_MODE,
78   PROP_WINDOW
79 };
80 
81 enum
82 {
83   MODE_BAND_PASS = 0,
84   MODE_BAND_REJECT
85 };
86 
87 #define GST_TYPE_AUDIO_WSINC_BAND_MODE (gst_gst_audio_wsincband_mode_get_type ())
88 static GType
gst_gst_audio_wsincband_mode_get_type(void)89 gst_gst_audio_wsincband_mode_get_type (void)
90 {
91   static GType gtype = 0;
92 
93   if (gtype == 0) {
94     static const GEnumValue values[] = {
95       {MODE_BAND_PASS, "Band pass (default)",
96           "band-pass"},
97       {MODE_BAND_REJECT, "Band reject",
98           "band-reject"},
99       {0, NULL, NULL}
100     };
101 
102     gtype = g_enum_register_static ("GstAudioWSincBandMode", 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_BAND_WINDOW (gst_gst_audio_wsincband_window_get_type ())
117 static GType
gst_gst_audio_wsincband_window_get_type(void)118 gst_gst_audio_wsincband_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 ("GstAudioWSincBandWindow", values);
138   }
139   return gtype;
140 }
141 
142 #define gst_audio_wsincband_parent_class parent_class
143 G_DEFINE_TYPE (GstAudioWSincBand, gst_audio_wsincband,
144     GST_TYPE_AUDIO_FX_BASE_FIR_FILTER);
145 GST_ELEMENT_REGISTER_DEFINE (audiowsincband, "audiowsincband",
146     GST_RANK_NONE, GST_TYPE_AUDIO_WSINC_BAND);
147 
148 static void gst_audio_wsincband_set_property (GObject * object, guint prop_id,
149     const GValue * value, GParamSpec * pspec);
150 static void gst_audio_wsincband_get_property (GObject * object, guint prop_id,
151     GValue * value, GParamSpec * pspec);
152 static void gst_audio_wsincband_finalize (GObject * object);
153 
154 static gboolean gst_audio_wsincband_setup (GstAudioFilter * base,
155     const GstAudioInfo * info);
156 
157 #define POW2(x)  (x)*(x)
158 
159 static void
gst_audio_wsincband_class_init(GstAudioWSincBandClass * klass)160 gst_audio_wsincband_class_init (GstAudioWSincBandClass * klass)
161 {
162   GObjectClass *gobject_class = (GObjectClass *) klass;
163   GstElementClass *gstelement_class = (GstElementClass *) klass;
164   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
165 
166   GST_DEBUG_CATEGORY_INIT (gst_gst_audio_wsincband_debug, "audiowsincband", 0,
167       "Band-pass and Band-reject Windowed sinc filter plugin");
168 
169   gobject_class->set_property = gst_audio_wsincband_set_property;
170   gobject_class->get_property = gst_audio_wsincband_get_property;
171   gobject_class->finalize = gst_audio_wsincband_finalize;
172 
173   /* FIXME: Don't use the complete possible range but restrict the upper boundary
174    * so automatically generated UIs can use a slider */
175   g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
176       g_param_spec_float ("lower-frequency", "Lower Frequency",
177           "Cut-off lower 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_UPPER_FREQUENCY,
180       g_param_spec_float ("upper-frequency", "Upper Frequency",
181           "Cut-off upper frequency (Hz)", 0.0, 100000.0, 0,
182           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
183   g_object_class_install_property (gobject_class, PROP_LENGTH,
184       g_param_spec_int ("length", "Length",
185           "Filter kernel length, will be rounded to the next odd number", 3,
186           256000, 101,
187           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
188 
189   g_object_class_install_property (gobject_class, PROP_MODE,
190       g_param_spec_enum ("mode", "Mode",
191           "Band pass or band reject mode", GST_TYPE_AUDIO_WSINC_BAND_MODE,
192           MODE_BAND_PASS,
193           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
194 
195   g_object_class_install_property (gobject_class, PROP_WINDOW,
196       g_param_spec_enum ("window", "Window",
197           "Window function to use", GST_TYPE_AUDIO_WSINC_BAND_WINDOW,
198           WINDOW_HAMMING,
199           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
200 
201   gst_element_class_set_static_metadata (gstelement_class,
202       "Band pass & band reject filter", "Filter/Effect/Audio",
203       "Band pass and band reject windowed sinc filter",
204       "Thomas Vander Stichele <thomas at apestaart dot org>, "
205       "Steven W. Smith, "
206       "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
207       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
208 
209   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_wsincband_setup);
210 
211   gst_type_mark_as_plugin_api (GST_TYPE_AUDIO_WSINC_BAND_MODE, 0);
212   gst_type_mark_as_plugin_api (GST_TYPE_AUDIO_WSINC_BAND_WINDOW, 0);
213 }
214 
215 static void
gst_audio_wsincband_init(GstAudioWSincBand * self)216 gst_audio_wsincband_init (GstAudioWSincBand * self)
217 {
218   self->kernel_length = 101;
219   self->lower_frequency = 0.0;
220   self->upper_frequency = 0.0;
221   self->mode = MODE_BAND_PASS;
222   self->window = WINDOW_HAMMING;
223 
224   g_mutex_init (&self->lock);
225 }
226 
227 static void
gst_audio_wsincband_build_kernel(GstAudioWSincBand * self,const GstAudioInfo * info)228 gst_audio_wsincband_build_kernel (GstAudioWSincBand * self,
229     const GstAudioInfo * info)
230 {
231   gint i = 0;
232   gdouble sum = 0.0;
233   gint len = 0;
234   gdouble *kernel_lp, *kernel_hp;
235   gdouble w;
236   gdouble *kernel;
237   gint rate, channels;
238 
239   len = self->kernel_length;
240 
241   if (info) {
242     rate = GST_AUDIO_INFO_RATE (info);
243     channels = GST_AUDIO_INFO_CHANNELS (info);
244   } else {
245     rate = GST_AUDIO_FILTER_RATE (self);
246     channels = GST_AUDIO_FILTER_CHANNELS (self);
247   }
248 
249   if (rate == 0) {
250     GST_DEBUG ("rate not set yet");
251     return;
252   }
253 
254   if (channels == 0) {
255     GST_DEBUG ("channels not set yet");
256     return;
257   }
258 
259   /* Clamp frequencies */
260   self->lower_frequency = CLAMP (self->lower_frequency, 0.0, rate / 2);
261   self->upper_frequency = CLAMP (self->upper_frequency, 0.0, rate / 2);
262 
263   if (self->lower_frequency > self->upper_frequency) {
264     gint tmp = self->lower_frequency;
265 
266     self->lower_frequency = self->upper_frequency;
267     self->upper_frequency = tmp;
268   }
269 
270   GST_DEBUG ("gst_audio_wsincband: initializing filter kernel of length %d "
271       "with lower frequency %.2lf Hz "
272       ", upper frequency %.2lf Hz for mode %s",
273       len, self->lower_frequency, self->upper_frequency,
274       (self->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject");
275 
276   /* fill the lp kernel */
277   w = 2 * G_PI * (self->lower_frequency / rate);
278   kernel_lp = g_new (gdouble, len);
279   for (i = 0; i < len; ++i) {
280     if (i == (len - 1) / 2.0)
281       kernel_lp[i] = w;
282     else
283       kernel_lp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
284 
285     /* windowing */
286     switch (self->window) {
287       case WINDOW_HAMMING:
288         kernel_lp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
289         break;
290       case WINDOW_BLACKMAN:
291         kernel_lp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
292             0.08 * cos (4 * G_PI * i / (len - 1)));
293         break;
294       case WINDOW_GAUSSIAN:
295         kernel_lp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
296         break;
297       case WINDOW_COSINE:
298         kernel_lp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
299         break;
300       case WINDOW_HANN:
301         kernel_lp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
302         break;
303     }
304   }
305 
306   /* normalize for unity gain at DC */
307   sum = 0.0;
308   for (i = 0; i < len; ++i)
309     sum += kernel_lp[i];
310   for (i = 0; i < len; ++i)
311     kernel_lp[i] /= sum;
312 
313   /* fill the hp kernel */
314   w = 2 * G_PI * (self->upper_frequency / rate);
315   kernel_hp = g_new (gdouble, len);
316   for (i = 0; i < len; ++i) {
317     if (i == (len - 1) / 2.0)
318       kernel_hp[i] = w;
319     else
320       kernel_hp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
321 
322     /* Windowing */
323     switch (self->window) {
324       case WINDOW_HAMMING:
325         kernel_hp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
326         break;
327       case WINDOW_BLACKMAN:
328         kernel_hp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
329             0.08 * cos (4 * G_PI * i / (len - 1)));
330         break;
331       case WINDOW_GAUSSIAN:
332         kernel_hp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
333         break;
334       case WINDOW_COSINE:
335         kernel_hp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
336         break;
337       case WINDOW_HANN:
338         kernel_hp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
339         break;
340     }
341   }
342 
343   /* normalize for unity gain at DC */
344   sum = 0.0;
345   for (i = 0; i < len; ++i)
346     sum += kernel_hp[i];
347   for (i = 0; i < len; ++i)
348     kernel_hp[i] /= sum;
349 
350   /* do spectral inversion to go from lowpass to highpass */
351   for (i = 0; i < len; ++i)
352     kernel_hp[i] = -kernel_hp[i];
353   if (len % 2 == 1) {
354     kernel_hp[(len - 1) / 2] += 1.0;
355   } else {
356     kernel_hp[len / 2 - 1] += 0.5;
357     kernel_hp[len / 2] += 0.5;
358   }
359 
360   /* combine the two kernels */
361   kernel = g_new (gdouble, len);
362 
363   for (i = 0; i < len; ++i)
364     kernel[i] = kernel_lp[i] + kernel_hp[i];
365 
366   /* free the helper kernels */
367   g_free (kernel_lp);
368   g_free (kernel_hp);
369 
370   /* do spectral inversion to go from bandreject to bandpass
371    * if specified */
372   if (self->mode == MODE_BAND_PASS) {
373     for (i = 0; i < len; ++i)
374       kernel[i] = -kernel[i];
375     kernel[len / 2] += 1;
376   }
377 
378   gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
379       kernel, self->kernel_length, (len - 1) / 2, info);
380 }
381 
382 /* GstAudioFilter vmethod implementations */
383 
384 /* get notified of caps and plug in the correct process function */
385 static gboolean
gst_audio_wsincband_setup(GstAudioFilter * base,const GstAudioInfo * info)386 gst_audio_wsincband_setup (GstAudioFilter * base, const GstAudioInfo * info)
387 {
388   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
389 
390   gst_audio_wsincband_build_kernel (self, info);
391 
392   return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
393 }
394 
395 static void
gst_audio_wsincband_finalize(GObject * object)396 gst_audio_wsincband_finalize (GObject * object)
397 {
398   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
399 
400   g_mutex_clear (&self->lock);
401 
402   G_OBJECT_CLASS (parent_class)->finalize (object);
403 }
404 
405 static void
gst_audio_wsincband_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)406 gst_audio_wsincband_set_property (GObject * object, guint prop_id,
407     const GValue * value, GParamSpec * pspec)
408 {
409   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
410 
411   g_return_if_fail (GST_IS_AUDIO_WSINC_BAND (self));
412 
413   switch (prop_id) {
414     case PROP_LENGTH:{
415       gint val;
416 
417       g_mutex_lock (&self->lock);
418       val = g_value_get_int (value);
419       if (val % 2 == 0)
420         val++;
421 
422       if (val != self->kernel_length) {
423         gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER
424             (self));
425         self->kernel_length = val;
426         gst_audio_wsincband_build_kernel (self, NULL);
427       }
428       g_mutex_unlock (&self->lock);
429       break;
430     }
431     case PROP_LOWER_FREQUENCY:
432       g_mutex_lock (&self->lock);
433       self->lower_frequency = g_value_get_float (value);
434       gst_audio_wsincband_build_kernel (self, NULL);
435       g_mutex_unlock (&self->lock);
436       break;
437     case PROP_UPPER_FREQUENCY:
438       g_mutex_lock (&self->lock);
439       self->upper_frequency = g_value_get_float (value);
440       gst_audio_wsincband_build_kernel (self, NULL);
441       g_mutex_unlock (&self->lock);
442       break;
443     case PROP_MODE:
444       g_mutex_lock (&self->lock);
445       self->mode = g_value_get_enum (value);
446       gst_audio_wsincband_build_kernel (self, NULL);
447       g_mutex_unlock (&self->lock);
448       break;
449     case PROP_WINDOW:
450       g_mutex_lock (&self->lock);
451       self->window = g_value_get_enum (value);
452       gst_audio_wsincband_build_kernel (self, NULL);
453       g_mutex_unlock (&self->lock);
454       break;
455     default:
456       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
457       break;
458   }
459 }
460 
461 static void
gst_audio_wsincband_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)462 gst_audio_wsincband_get_property (GObject * object, guint prop_id,
463     GValue * value, GParamSpec * pspec)
464 {
465   GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
466 
467   switch (prop_id) {
468     case PROP_LENGTH:
469       g_value_set_int (value, self->kernel_length);
470       break;
471     case PROP_LOWER_FREQUENCY:
472       g_value_set_float (value, self->lower_frequency);
473       break;
474     case PROP_UPPER_FREQUENCY:
475       g_value_set_float (value, self->upper_frequency);
476       break;
477     case PROP_MODE:
478       g_value_set_enum (value, self->mode);
479       break;
480     case PROP_WINDOW:
481       g_value_set_enum (value, self->window);
482       break;
483     default:
484       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
485       break;
486   }
487 }
488