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 *
36 * Attenuates all frequencies outside (bandpass) or inside (bandreject) of a frequency
37 * band. 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 bandpass and bandreject 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-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 * </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 "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
146 static void gst_audio_wsincband_set_property (GObject * object, guint prop_id,
147 const GValue * value, GParamSpec * pspec);
148 static void gst_audio_wsincband_get_property (GObject * object, guint prop_id,
149 GValue * value, GParamSpec * pspec);
150 static void gst_audio_wsincband_finalize (GObject * object);
151
152 static gboolean gst_audio_wsincband_setup (GstAudioFilter * base,
153 const GstAudioInfo * info);
154
155 #define POW2(x) (x)*(x)
156
157 static void
gst_audio_wsincband_class_init(GstAudioWSincBandClass * klass)158 gst_audio_wsincband_class_init (GstAudioWSincBandClass * klass)
159 {
160 GObjectClass *gobject_class = (GObjectClass *) klass;
161 GstElementClass *gstelement_class = (GstElementClass *) klass;
162 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
163
164 GST_DEBUG_CATEGORY_INIT (gst_gst_audio_wsincband_debug, "audiowsincband", 0,
165 "Band-pass and Band-reject Windowed sinc filter plugin");
166
167 gobject_class->set_property = gst_audio_wsincband_set_property;
168 gobject_class->get_property = gst_audio_wsincband_get_property;
169 gobject_class->finalize = gst_audio_wsincband_finalize;
170
171 /* FIXME: Don't use the complete possible range but restrict the upper boundary
172 * so automatically generated UIs can use a slider */
173 g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
174 g_param_spec_float ("lower-frequency", "Lower Frequency",
175 "Cut-off lower frequency (Hz)", 0.0, 100000.0, 0,
176 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
178 g_param_spec_float ("upper-frequency", "Upper Frequency",
179 "Cut-off upper frequency (Hz)", 0.0, 100000.0, 0,
180 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
181 g_object_class_install_property (gobject_class, PROP_LENGTH,
182 g_param_spec_int ("length", "Length",
183 "Filter kernel length, will be rounded to the next odd number", 3,
184 256000, 101,
185 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
186
187 g_object_class_install_property (gobject_class, PROP_MODE,
188 g_param_spec_enum ("mode", "Mode",
189 "Band pass or band reject mode", GST_TYPE_AUDIO_WSINC_BAND_MODE,
190 MODE_BAND_PASS,
191 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
192
193 g_object_class_install_property (gobject_class, PROP_WINDOW,
194 g_param_spec_enum ("window", "Window",
195 "Window function to use", GST_TYPE_AUDIO_WSINC_BAND_WINDOW,
196 WINDOW_HAMMING,
197 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
198
199 gst_element_class_set_static_metadata (gstelement_class,
200 "Band pass & band reject filter", "Filter/Effect/Audio",
201 "Band pass and band reject windowed sinc filter",
202 "Thomas Vander Stichele <thomas at apestaart dot org>, "
203 "Steven W. Smith, "
204 "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
205 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
206
207 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_wsincband_setup);
208 }
209
210 static void
gst_audio_wsincband_init(GstAudioWSincBand * self)211 gst_audio_wsincband_init (GstAudioWSincBand * self)
212 {
213 self->kernel_length = 101;
214 self->lower_frequency = 0.0;
215 self->upper_frequency = 0.0;
216 self->mode = MODE_BAND_PASS;
217 self->window = WINDOW_HAMMING;
218
219 g_mutex_init (&self->lock);
220 }
221
222 static void
gst_audio_wsincband_build_kernel(GstAudioWSincBand * self,const GstAudioInfo * info)223 gst_audio_wsincband_build_kernel (GstAudioWSincBand * self,
224 const GstAudioInfo * info)
225 {
226 gint i = 0;
227 gdouble sum = 0.0;
228 gint len = 0;
229 gdouble *kernel_lp, *kernel_hp;
230 gdouble w;
231 gdouble *kernel;
232 gint rate, channels;
233
234 len = self->kernel_length;
235
236 if (info) {
237 rate = GST_AUDIO_INFO_RATE (info);
238 channels = GST_AUDIO_INFO_CHANNELS (info);
239 } else {
240 rate = GST_AUDIO_FILTER_RATE (self);
241 channels = GST_AUDIO_FILTER_CHANNELS (self);
242 }
243
244 if (rate == 0) {
245 GST_DEBUG ("rate not set yet");
246 return;
247 }
248
249 if (channels == 0) {
250 GST_DEBUG ("channels not set yet");
251 return;
252 }
253
254 /* Clamp frequencies */
255 self->lower_frequency = CLAMP (self->lower_frequency, 0.0, rate / 2);
256 self->upper_frequency = CLAMP (self->upper_frequency, 0.0, rate / 2);
257
258 if (self->lower_frequency > self->upper_frequency) {
259 gint tmp = self->lower_frequency;
260
261 self->lower_frequency = self->upper_frequency;
262 self->upper_frequency = tmp;
263 }
264
265 GST_DEBUG ("gst_audio_wsincband: initializing filter kernel of length %d "
266 "with lower frequency %.2lf Hz "
267 ", upper frequency %.2lf Hz for mode %s",
268 len, self->lower_frequency, self->upper_frequency,
269 (self->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject");
270
271 /* fill the lp kernel */
272 w = 2 * G_PI * (self->lower_frequency / rate);
273 kernel_lp = g_new (gdouble, len);
274 for (i = 0; i < len; ++i) {
275 if (i == (len - 1) / 2.0)
276 kernel_lp[i] = w;
277 else
278 kernel_lp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
279
280 /* windowing */
281 switch (self->window) {
282 case WINDOW_HAMMING:
283 kernel_lp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
284 break;
285 case WINDOW_BLACKMAN:
286 kernel_lp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
287 0.08 * cos (4 * G_PI * i / (len - 1)));
288 break;
289 case WINDOW_GAUSSIAN:
290 kernel_lp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
291 break;
292 case WINDOW_COSINE:
293 kernel_lp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
294 break;
295 case WINDOW_HANN:
296 kernel_lp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
297 break;
298 }
299 }
300
301 /* normalize for unity gain at DC */
302 sum = 0.0;
303 for (i = 0; i < len; ++i)
304 sum += kernel_lp[i];
305 for (i = 0; i < len; ++i)
306 kernel_lp[i] /= sum;
307
308 /* fill the hp kernel */
309 w = 2 * G_PI * (self->upper_frequency / rate);
310 kernel_hp = g_new (gdouble, len);
311 for (i = 0; i < len; ++i) {
312 if (i == (len - 1) / 2.0)
313 kernel_hp[i] = w;
314 else
315 kernel_hp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
316
317 /* Windowing */
318 switch (self->window) {
319 case WINDOW_HAMMING:
320 kernel_hp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
321 break;
322 case WINDOW_BLACKMAN:
323 kernel_hp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
324 0.08 * cos (4 * G_PI * i / (len - 1)));
325 break;
326 case WINDOW_GAUSSIAN:
327 kernel_hp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
328 break;
329 case WINDOW_COSINE:
330 kernel_hp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
331 break;
332 case WINDOW_HANN:
333 kernel_hp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
334 break;
335 }
336 }
337
338 /* normalize for unity gain at DC */
339 sum = 0.0;
340 for (i = 0; i < len; ++i)
341 sum += kernel_hp[i];
342 for (i = 0; i < len; ++i)
343 kernel_hp[i] /= sum;
344
345 /* do spectral inversion to go from lowpass to highpass */
346 for (i = 0; i < len; ++i)
347 kernel_hp[i] = -kernel_hp[i];
348 if (len % 2 == 1) {
349 kernel_hp[(len - 1) / 2] += 1.0;
350 } else {
351 kernel_hp[len / 2 - 1] += 0.5;
352 kernel_hp[len / 2] += 0.5;
353 }
354
355 /* combine the two kernels */
356 kernel = g_new (gdouble, len);
357
358 for (i = 0; i < len; ++i)
359 kernel[i] = kernel_lp[i] + kernel_hp[i];
360
361 /* free the helper kernels */
362 g_free (kernel_lp);
363 g_free (kernel_hp);
364
365 /* do spectral inversion to go from bandreject to bandpass
366 * if specified */
367 if (self->mode == MODE_BAND_PASS) {
368 for (i = 0; i < len; ++i)
369 kernel[i] = -kernel[i];
370 kernel[len / 2] += 1;
371 }
372
373 gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
374 kernel, self->kernel_length, (len - 1) / 2, info);
375 }
376
377 /* GstAudioFilter vmethod implementations */
378
379 /* get notified of caps and plug in the correct process function */
380 static gboolean
gst_audio_wsincband_setup(GstAudioFilter * base,const GstAudioInfo * info)381 gst_audio_wsincband_setup (GstAudioFilter * base, const GstAudioInfo * info)
382 {
383 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
384
385 gst_audio_wsincband_build_kernel (self, info);
386
387 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
388 }
389
390 static void
gst_audio_wsincband_finalize(GObject * object)391 gst_audio_wsincband_finalize (GObject * object)
392 {
393 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
394
395 g_mutex_clear (&self->lock);
396
397 G_OBJECT_CLASS (parent_class)->finalize (object);
398 }
399
400 static void
gst_audio_wsincband_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)401 gst_audio_wsincband_set_property (GObject * object, guint prop_id,
402 const GValue * value, GParamSpec * pspec)
403 {
404 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
405
406 g_return_if_fail (GST_IS_AUDIO_WSINC_BAND (self));
407
408 switch (prop_id) {
409 case PROP_LENGTH:{
410 gint val;
411
412 g_mutex_lock (&self->lock);
413 val = g_value_get_int (value);
414 if (val % 2 == 0)
415 val++;
416
417 if (val != self->kernel_length) {
418 gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER
419 (self));
420 self->kernel_length = val;
421 gst_audio_wsincband_build_kernel (self, NULL);
422 }
423 g_mutex_unlock (&self->lock);
424 break;
425 }
426 case PROP_LOWER_FREQUENCY:
427 g_mutex_lock (&self->lock);
428 self->lower_frequency = g_value_get_float (value);
429 gst_audio_wsincband_build_kernel (self, NULL);
430 g_mutex_unlock (&self->lock);
431 break;
432 case PROP_UPPER_FREQUENCY:
433 g_mutex_lock (&self->lock);
434 self->upper_frequency = g_value_get_float (value);
435 gst_audio_wsincband_build_kernel (self, NULL);
436 g_mutex_unlock (&self->lock);
437 break;
438 case PROP_MODE:
439 g_mutex_lock (&self->lock);
440 self->mode = g_value_get_enum (value);
441 gst_audio_wsincband_build_kernel (self, NULL);
442 g_mutex_unlock (&self->lock);
443 break;
444 case PROP_WINDOW:
445 g_mutex_lock (&self->lock);
446 self->window = g_value_get_enum (value);
447 gst_audio_wsincband_build_kernel (self, NULL);
448 g_mutex_unlock (&self->lock);
449 break;
450 default:
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452 break;
453 }
454 }
455
456 static void
gst_audio_wsincband_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)457 gst_audio_wsincband_get_property (GObject * object, guint prop_id,
458 GValue * value, GParamSpec * pspec)
459 {
460 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
461
462 switch (prop_id) {
463 case PROP_LENGTH:
464 g_value_set_int (value, self->kernel_length);
465 break;
466 case PROP_LOWER_FREQUENCY:
467 g_value_set_float (value, self->lower_frequency);
468 break;
469 case PROP_UPPER_FREQUENCY:
470 g_value_set_float (value, self->upper_frequency);
471 break;
472 case PROP_MODE:
473 g_value_set_enum (value, self->mode);
474 break;
475 case PROP_WINDOW:
476 g_value_set_enum (value, self->window);
477 break;
478 default:
479 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
480 break;
481 }
482 }
483