1 /* -*- c-basic-offset: 2 -*-
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>
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.
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.
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.
24 * this windowed sinc filter is taken from the freely downloadable DSP book,
25 * "The Scientist and Engineer's Guide to Digital Signal Processing",
27 * available at http://www.dspguide.com/
29 * For the window functions see
30 * http://en.wikipedia.org/wiki/Window_function
34 * SECTION:element-audiowsincband
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.
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.
46 * <title>Example launch line</title>
48 * gst-launch audiotestsrc freq=1500 ! audioconvert ! audiosincband mode=band-pass lower-frequency=3000 upper-frequency=10000 length=501 window=blackman ! audioconvert ! alsasink
49 * gst-launch 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 audiotestsrc wave=white-noise ! audioconvert ! audiowsincband mode=band-pass lower-frequency=1000 upper-frequency=2000 length=31 ! audioconvert ! alsasink
62 #include <gst/audio/gstaudiofilter.h>
63 #include <gst/controller/gstcontroller.h>
65 #include "audiowsincband.h"
67 #define GST_CAT_DEFAULT gst_gst_audio_wsincband_debug
68 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
86 #define GST_TYPE_AUDIO_WSINC_BAND_MODE (gst_gst_audio_wsincband_mode_get_type ())
88 gst_gst_audio_wsincband_mode_get_type (void)
90 static GType gtype = 0;
93 static const GEnumValue values[] = {
94 {MODE_BAND_PASS, "Band pass (default)",
96 {MODE_BAND_REJECT, "Band reject",
101 gtype = g_enum_register_static ("GstAudioWSincBandMode", values);
115 #define GST_TYPE_AUDIO_WSINC_BAND_WINDOW (gst_gst_audio_wsincband_window_get_type ())
117 gst_gst_audio_wsincband_window_get_type (void)
119 static GType gtype = 0;
122 static const GEnumValue values[] = {
123 {WINDOW_HAMMING, "Hamming window (default)",
125 {WINDOW_BLACKMAN, "Blackman window",
127 {WINDOW_GAUSSIAN, "Gaussian window",
129 {WINDOW_COSINE, "Cosine window",
131 {WINDOW_HANN, "Hann window",
136 gtype = g_enum_register_static ("GstAudioWSincBandWindow", values);
141 #define gst_audio_wsincband_parent_class parent_class
142 G_DEFINE_TYPE (GstAudioWSincBand, gst_audio_wsincband,
143 GST_TYPE_AUDIO_FX_BASE_FIR_FILTER);
145 static void gst_audio_wsincband_set_property (GObject * object, guint prop_id,
146 const GValue * value, GParamSpec * pspec);
147 static void gst_audio_wsincband_get_property (GObject * object, guint prop_id,
148 GValue * value, GParamSpec * pspec);
149 static void gst_audio_wsincband_finalize (GObject * object);
151 static gboolean gst_audio_wsincband_setup (GstAudioFilter * base,
152 const GstAudioInfo * info);
154 #define POW2(x) (x)*(x)
157 gst_audio_wsincband_class_init (GstAudioWSincBandClass * klass)
159 GObjectClass *gobject_class = (GObjectClass *) klass;
160 GstElementClass *gstelement_class = (GstElementClass *) klass;
161 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
163 GST_DEBUG_CATEGORY_INIT (gst_gst_audio_wsincband_debug, "audiowsincband", 0,
164 "Band-pass and Band-reject Windowed sinc filter plugin");
166 gobject_class->set_property = gst_audio_wsincband_set_property;
167 gobject_class->get_property = gst_audio_wsincband_get_property;
168 gobject_class->finalize = gst_audio_wsincband_finalize;
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_LOWER_FREQUENCY,
173 g_param_spec_float ("lower-frequency", "Lower Frequency",
174 "Cut-off lower frequency (Hz)", 0.0, 100000.0, 0,
175 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
176 g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
177 g_param_spec_float ("upper-frequency", "Upper Frequency",
178 "Cut-off upper frequency (Hz)", 0.0, 100000.0, 0,
179 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (gobject_class, PROP_LENGTH,
181 g_param_spec_int ("length", "Length",
182 "Filter kernel length, will be rounded to the next odd number", 3,
184 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
186 g_object_class_install_property (gobject_class, PROP_MODE,
187 g_param_spec_enum ("mode", "Mode",
188 "Band pass or band reject mode", GST_TYPE_AUDIO_WSINC_BAND_MODE,
190 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (gobject_class, PROP_WINDOW,
193 g_param_spec_enum ("window", "Window",
194 "Window function to use", GST_TYPE_AUDIO_WSINC_BAND_WINDOW,
196 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
198 gst_element_class_set_details_simple (gstelement_class,
199 "Band pass & band reject filter", "Filter/Effect/Audio",
200 "Band pass and band reject windowed sinc filter",
201 "Thomas Vander Stichele <thomas at apestaart dot org>, "
203 "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
204 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
206 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_wsincband_setup);
210 gst_audio_wsincband_init (GstAudioWSincBand * self)
212 self->kernel_length = 101;
213 self->lower_frequency = 0.0;
214 self->upper_frequency = 0.0;
215 self->mode = MODE_BAND_PASS;
216 self->window = WINDOW_HAMMING;
218 self->lock = g_mutex_new ();
222 gst_audio_wsincband_build_kernel (GstAudioWSincBand * self)
227 gdouble *kernel_lp, *kernel_hp;
232 len = self->kernel_length;
234 rate = GST_AUDIO_FILTER_RATE (self);
235 channels = GST_AUDIO_FILTER_CHANNELS (self);
238 GST_DEBUG ("rate not set yet");
243 GST_DEBUG ("channels not set yet");
247 /* Clamp frequencies */
248 self->lower_frequency = CLAMP (self->lower_frequency, 0.0, rate / 2);
249 self->upper_frequency = CLAMP (self->upper_frequency, 0.0, rate / 2);
251 if (self->lower_frequency > self->upper_frequency) {
252 gint tmp = self->lower_frequency;
254 self->lower_frequency = self->upper_frequency;
255 self->upper_frequency = tmp;
258 GST_DEBUG ("gst_audio_wsincband: initializing filter kernel of length %d "
259 "with lower frequency %.2lf Hz "
260 ", upper frequency %.2lf Hz for mode %s",
261 len, self->lower_frequency, self->upper_frequency,
262 (self->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject");
264 /* fill the lp kernel */
265 w = 2 * G_PI * (self->lower_frequency / rate);
266 kernel_lp = g_new (gdouble, len);
267 for (i = 0; i < len; ++i) {
268 if (i == (len - 1) / 2.0)
271 kernel_lp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
274 switch (self->window) {
276 kernel_lp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
278 case WINDOW_BLACKMAN:
279 kernel_lp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
280 0.08 * cos (4 * G_PI * i / (len - 1)));
282 case WINDOW_GAUSSIAN:
283 kernel_lp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
286 kernel_lp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
289 kernel_lp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
294 /* normalize for unity gain at DC */
296 for (i = 0; i < len; ++i)
298 for (i = 0; i < len; ++i)
301 /* fill the hp kernel */
302 w = 2 * G_PI * (self->upper_frequency / rate);
303 kernel_hp = g_new (gdouble, len);
304 for (i = 0; i < len; ++i) {
305 if (i == (len - 1) / 2.0)
308 kernel_hp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
311 switch (self->window) {
313 kernel_hp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
315 case WINDOW_BLACKMAN:
316 kernel_hp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
317 0.08 * cos (4 * G_PI * i / (len - 1)));
319 case WINDOW_GAUSSIAN:
320 kernel_hp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
323 kernel_hp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
326 kernel_hp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
331 /* normalize for unity gain at DC */
333 for (i = 0; i < len; ++i)
335 for (i = 0; i < len; ++i)
338 /* do spectral inversion to go from lowpass to highpass */
339 for (i = 0; i < len; ++i)
340 kernel_hp[i] = -kernel_hp[i];
342 kernel_hp[(len - 1) / 2] += 1.0;
344 kernel_hp[len / 2 - 1] += 0.5;
345 kernel_hp[len / 2] += 0.5;
348 /* combine the two kernels */
349 kernel = g_new (gdouble, len);
351 for (i = 0; i < len; ++i)
352 kernel[i] = kernel_lp[i] + kernel_hp[i];
354 /* free the helper kernels */
358 /* do spectral inversion to go from bandreject to bandpass
360 if (self->mode == MODE_BAND_PASS) {
361 for (i = 0; i < len; ++i)
362 kernel[i] = -kernel[i];
363 kernel[len / 2] += 1;
366 gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
367 kernel, self->kernel_length, (len - 1) / 2);
370 /* GstAudioFilter vmethod implementations */
372 /* get notified of caps and plug in the correct process function */
374 gst_audio_wsincband_setup (GstAudioFilter * base, const GstAudioInfo * info)
376 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
378 gst_audio_wsincband_build_kernel (self);
380 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
384 gst_audio_wsincband_finalize (GObject * object)
386 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
388 g_mutex_free (self->lock);
391 G_OBJECT_CLASS (parent_class)->finalize (object);
395 gst_audio_wsincband_set_property (GObject * object, guint prop_id,
396 const GValue * value, GParamSpec * pspec)
398 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
400 g_return_if_fail (GST_IS_AUDIO_WSINC_BAND (self));
406 g_mutex_lock (self->lock);
407 val = g_value_get_int (value);
411 if (val != self->kernel_length) {
412 gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER
414 self->kernel_length = val;
415 gst_audio_wsincband_build_kernel (self);
417 g_mutex_unlock (self->lock);
420 case PROP_LOWER_FREQUENCY:
421 g_mutex_lock (self->lock);
422 self->lower_frequency = g_value_get_float (value);
423 gst_audio_wsincband_build_kernel (self);
424 g_mutex_unlock (self->lock);
426 case PROP_UPPER_FREQUENCY:
427 g_mutex_lock (self->lock);
428 self->upper_frequency = g_value_get_float (value);
429 gst_audio_wsincband_build_kernel (self);
430 g_mutex_unlock (self->lock);
433 g_mutex_lock (self->lock);
434 self->mode = g_value_get_enum (value);
435 gst_audio_wsincband_build_kernel (self);
436 g_mutex_unlock (self->lock);
439 g_mutex_lock (self->lock);
440 self->window = g_value_get_enum (value);
441 gst_audio_wsincband_build_kernel (self);
442 g_mutex_unlock (self->lock);
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
451 gst_audio_wsincband_get_property (GObject * object, guint prop_id,
452 GValue * value, GParamSpec * pspec)
454 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
458 g_value_set_int (value, self->kernel_length);
460 case PROP_LOWER_FREQUENCY:
461 g_value_set_float (value, self->lower_frequency);
463 case PROP_UPPER_FREQUENCY:
464 g_value_set_float (value, self->upper_frequency);
467 g_value_set_enum (value, self->mode);
470 g_value_set_enum (value, self->window);
473 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);