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>
64 #include "audiowsincband.h"
66 #define GST_CAT_DEFAULT gst_gst_audio_wsincband_debug
67 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
85 #define GST_TYPE_AUDIO_WSINC_BAND_MODE (gst_gst_audio_wsincband_mode_get_type ())
87 gst_gst_audio_wsincband_mode_get_type (void)
89 static GType gtype = 0;
92 static const GEnumValue values[] = {
93 {MODE_BAND_PASS, "Band pass (default)",
95 {MODE_BAND_REJECT, "Band reject",
100 gtype = g_enum_register_static ("GstAudioWSincBandMode", values);
114 #define GST_TYPE_AUDIO_WSINC_BAND_WINDOW (gst_gst_audio_wsincband_window_get_type ())
116 gst_gst_audio_wsincband_window_get_type (void)
118 static GType gtype = 0;
121 static const GEnumValue values[] = {
122 {WINDOW_HAMMING, "Hamming window (default)",
124 {WINDOW_BLACKMAN, "Blackman window",
126 {WINDOW_GAUSSIAN, "Gaussian window",
128 {WINDOW_COSINE, "Cosine window",
130 {WINDOW_HANN, "Hann window",
135 gtype = g_enum_register_static ("GstAudioWSincBandWindow", values);
140 #define gst_audio_wsincband_parent_class parent_class
141 G_DEFINE_TYPE (GstAudioWSincBand, gst_audio_wsincband,
142 GST_TYPE_AUDIO_FX_BASE_FIR_FILTER);
144 static void gst_audio_wsincband_set_property (GObject * object, guint prop_id,
145 const GValue * value, GParamSpec * pspec);
146 static void gst_audio_wsincband_get_property (GObject * object, guint prop_id,
147 GValue * value, GParamSpec * pspec);
148 static void gst_audio_wsincband_finalize (GObject * object);
150 static gboolean gst_audio_wsincband_setup (GstAudioFilter * base,
151 const GstAudioInfo * info);
153 #define POW2(x) (x)*(x)
156 gst_audio_wsincband_class_init (GstAudioWSincBandClass * klass)
158 GObjectClass *gobject_class = (GObjectClass *) klass;
159 GstElementClass *gstelement_class = (GstElementClass *) klass;
160 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
162 GST_DEBUG_CATEGORY_INIT (gst_gst_audio_wsincband_debug, "audiowsincband", 0,
163 "Band-pass and Band-reject Windowed sinc filter plugin");
165 gobject_class->set_property = gst_audio_wsincband_set_property;
166 gobject_class->get_property = gst_audio_wsincband_get_property;
167 gobject_class->finalize = gst_audio_wsincband_finalize;
169 /* FIXME: Don't use the complete possible range but restrict the upper boundary
170 * so automatically generated UIs can use a slider */
171 g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
172 g_param_spec_float ("lower-frequency", "Lower Frequency",
173 "Cut-off lower frequency (Hz)", 0.0, 100000.0, 0,
174 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
175 g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
176 g_param_spec_float ("upper-frequency", "Upper Frequency",
177 "Cut-off upper 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_LENGTH,
180 g_param_spec_int ("length", "Length",
181 "Filter kernel length, will be rounded to the next odd number", 3,
183 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
185 g_object_class_install_property (gobject_class, PROP_MODE,
186 g_param_spec_enum ("mode", "Mode",
187 "Band pass or band reject mode", GST_TYPE_AUDIO_WSINC_BAND_MODE,
189 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (gobject_class, PROP_WINDOW,
192 g_param_spec_enum ("window", "Window",
193 "Window function to use", GST_TYPE_AUDIO_WSINC_BAND_WINDOW,
195 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
197 gst_element_class_set_details_simple (gstelement_class,
198 "Band pass & band reject filter", "Filter/Effect/Audio",
199 "Band pass and band reject windowed sinc filter",
200 "Thomas Vander Stichele <thomas at apestaart dot org>, "
202 "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
203 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
205 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_wsincband_setup);
209 gst_audio_wsincband_init (GstAudioWSincBand * self)
211 self->kernel_length = 101;
212 self->lower_frequency = 0.0;
213 self->upper_frequency = 0.0;
214 self->mode = MODE_BAND_PASS;
215 self->window = WINDOW_HAMMING;
217 self->lock = g_mutex_new ();
221 gst_audio_wsincband_build_kernel (GstAudioWSincBand * self)
226 gdouble *kernel_lp, *kernel_hp;
231 len = self->kernel_length;
233 rate = GST_AUDIO_FILTER_RATE (self);
234 channels = GST_AUDIO_FILTER_CHANNELS (self);
237 GST_DEBUG ("rate not set yet");
242 GST_DEBUG ("channels not set yet");
246 /* Clamp frequencies */
247 self->lower_frequency = CLAMP (self->lower_frequency, 0.0, rate / 2);
248 self->upper_frequency = CLAMP (self->upper_frequency, 0.0, rate / 2);
250 if (self->lower_frequency > self->upper_frequency) {
251 gint tmp = self->lower_frequency;
253 self->lower_frequency = self->upper_frequency;
254 self->upper_frequency = tmp;
257 GST_DEBUG ("gst_audio_wsincband: initializing filter kernel of length %d "
258 "with lower frequency %.2lf Hz "
259 ", upper frequency %.2lf Hz for mode %s",
260 len, self->lower_frequency, self->upper_frequency,
261 (self->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject");
263 /* fill the lp kernel */
264 w = 2 * G_PI * (self->lower_frequency / rate);
265 kernel_lp = g_new (gdouble, len);
266 for (i = 0; i < len; ++i) {
267 if (i == (len - 1) / 2.0)
270 kernel_lp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
273 switch (self->window) {
275 kernel_lp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
277 case WINDOW_BLACKMAN:
278 kernel_lp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
279 0.08 * cos (4 * G_PI * i / (len - 1)));
281 case WINDOW_GAUSSIAN:
282 kernel_lp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
285 kernel_lp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
288 kernel_lp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
293 /* normalize for unity gain at DC */
295 for (i = 0; i < len; ++i)
297 for (i = 0; i < len; ++i)
300 /* fill the hp kernel */
301 w = 2 * G_PI * (self->upper_frequency / rate);
302 kernel_hp = g_new (gdouble, len);
303 for (i = 0; i < len; ++i) {
304 if (i == (len - 1) / 2.0)
307 kernel_hp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
310 switch (self->window) {
312 kernel_hp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
314 case WINDOW_BLACKMAN:
315 kernel_hp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
316 0.08 * cos (4 * G_PI * i / (len - 1)));
318 case WINDOW_GAUSSIAN:
319 kernel_hp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
322 kernel_hp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
325 kernel_hp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
330 /* normalize for unity gain at DC */
332 for (i = 0; i < len; ++i)
334 for (i = 0; i < len; ++i)
337 /* do spectral inversion to go from lowpass to highpass */
338 for (i = 0; i < len; ++i)
339 kernel_hp[i] = -kernel_hp[i];
341 kernel_hp[(len - 1) / 2] += 1.0;
343 kernel_hp[len / 2 - 1] += 0.5;
344 kernel_hp[len / 2] += 0.5;
347 /* combine the two kernels */
348 kernel = g_new (gdouble, len);
350 for (i = 0; i < len; ++i)
351 kernel[i] = kernel_lp[i] + kernel_hp[i];
353 /* free the helper kernels */
357 /* do spectral inversion to go from bandreject to bandpass
359 if (self->mode == MODE_BAND_PASS) {
360 for (i = 0; i < len; ++i)
361 kernel[i] = -kernel[i];
362 kernel[len / 2] += 1;
365 gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
366 kernel, self->kernel_length, (len - 1) / 2);
369 /* GstAudioFilter vmethod implementations */
371 /* get notified of caps and plug in the correct process function */
373 gst_audio_wsincband_setup (GstAudioFilter * base, const GstAudioInfo * info)
375 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
377 gst_audio_wsincband_build_kernel (self);
379 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
383 gst_audio_wsincband_finalize (GObject * object)
385 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
387 g_mutex_free (self->lock);
390 G_OBJECT_CLASS (parent_class)->finalize (object);
394 gst_audio_wsincband_set_property (GObject * object, guint prop_id,
395 const GValue * value, GParamSpec * pspec)
397 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
399 g_return_if_fail (GST_IS_AUDIO_WSINC_BAND (self));
405 g_mutex_lock (self->lock);
406 val = g_value_get_int (value);
410 if (val != self->kernel_length) {
411 gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER
413 self->kernel_length = val;
414 gst_audio_wsincband_build_kernel (self);
416 g_mutex_unlock (self->lock);
419 case PROP_LOWER_FREQUENCY:
420 g_mutex_lock (self->lock);
421 self->lower_frequency = g_value_get_float (value);
422 gst_audio_wsincband_build_kernel (self);
423 g_mutex_unlock (self->lock);
425 case PROP_UPPER_FREQUENCY:
426 g_mutex_lock (self->lock);
427 self->upper_frequency = g_value_get_float (value);
428 gst_audio_wsincband_build_kernel (self);
429 g_mutex_unlock (self->lock);
432 g_mutex_lock (self->lock);
433 self->mode = g_value_get_enum (value);
434 gst_audio_wsincband_build_kernel (self);
435 g_mutex_unlock (self->lock);
438 g_mutex_lock (self->lock);
439 self->window = g_value_get_enum (value);
440 gst_audio_wsincband_build_kernel (self);
441 g_mutex_unlock (self->lock);
444 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
450 gst_audio_wsincband_get_property (GObject * object, guint prop_id,
451 GValue * value, GParamSpec * pspec)
453 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
457 g_value_set_int (value, self->kernel_length);
459 case PROP_LOWER_FREQUENCY:
460 g_value_set_float (value, self->lower_frequency);
462 case PROP_UPPER_FREQUENCY:
463 g_value_set_float (value, self->upper_frequency);
466 g_value_set_enum (value, self->mode);
469 g_value_set_enum (value, self->window);
472 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);