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 DEBUG_INIT(bla) \
142 GST_DEBUG_CATEGORY_INIT (gst_gst_audio_wsincband_debug, "audiowsincband", 0, \
143 "Band-pass and Band-reject Windowed sinc filter plugin");
145 GST_BOILERPLATE_FULL (GstAudioWSincBand, gst_audio_wsincband, GstAudioFilter,
146 GST_TYPE_AUDIO_FX_BASE_FIR_FILTER, DEBUG_INIT);
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);
154 static gboolean gst_audio_wsincband_setup (GstAudioFilter * base,
155 GstRingBufferSpec * format);
158 #define POW2(x) (x)*(x)
162 gst_audio_wsincband_base_init (gpointer g_class)
164 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
166 gst_element_class_set_details_simple (element_class,
167 "Band pass & band reject filter", "Filter/Effect/Audio",
168 "Band pass and band reject windowed sinc filter",
169 "Thomas Vander Stichele <thomas at apestaart dot org>, "
171 "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
172 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
176 gst_audio_wsincband_class_init (GstAudioWSincBandClass * klass)
178 GObjectClass *gobject_class = (GObjectClass *) klass;
179 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
181 gobject_class->set_property = gst_audio_wsincband_set_property;
182 gobject_class->get_property = gst_audio_wsincband_get_property;
183 gobject_class->finalize = gst_audio_wsincband_finalize;
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_LOWER_FREQUENCY,
188 g_param_spec_float ("lower-frequency", "Lower Frequency",
189 "Cut-off lower frequency (Hz)", 0.0, 100000.0, 0,
190 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
192 g_param_spec_float ("upper-frequency", "Upper Frequency",
193 "Cut-off upper frequency (Hz)", 0.0, 100000.0, 0,
194 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
195 g_object_class_install_property (gobject_class, PROP_LENGTH,
196 g_param_spec_int ("length", "Length",
197 "Filter kernel length, will be rounded to the next odd number", 3,
199 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
201 g_object_class_install_property (gobject_class, PROP_MODE,
202 g_param_spec_enum ("mode", "Mode",
203 "Band pass or band reject mode", GST_TYPE_AUDIO_WSINC_BAND_MODE,
205 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
207 g_object_class_install_property (gobject_class, PROP_WINDOW,
208 g_param_spec_enum ("window", "Window",
209 "Window function to use", GST_TYPE_AUDIO_WSINC_BAND_WINDOW,
211 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
213 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_wsincband_setup);
217 gst_audio_wsincband_init (GstAudioWSincBand * self,
218 GstAudioWSincBandClass * g_class)
220 self->kernel_length = 101;
221 self->lower_frequency = 0.0;
222 self->upper_frequency = 0.0;
223 self->mode = MODE_BAND_PASS;
224 self->window = WINDOW_HAMMING;
226 self->lock = g_mutex_new ();
230 gst_audio_wsincband_build_kernel (GstAudioWSincBand * self)
235 gdouble *kernel_lp, *kernel_hp;
239 len = self->kernel_length;
241 if (GST_AUDIO_FILTER (self)->format.rate == 0) {
242 GST_DEBUG ("rate not set yet");
246 if (GST_AUDIO_FILTER (self)->format.channels == 0) {
247 GST_DEBUG ("channels not set yet");
251 /* Clamp frequencies */
252 self->lower_frequency =
253 CLAMP (self->lower_frequency, 0.0,
254 GST_AUDIO_FILTER (self)->format.rate / 2);
255 self->upper_frequency =
256 CLAMP (self->upper_frequency, 0.0,
257 GST_AUDIO_FILTER (self)->format.rate / 2);
258 if (self->lower_frequency > self->upper_frequency) {
259 gint tmp = self->lower_frequency;
261 self->lower_frequency = self->upper_frequency;
262 self->upper_frequency = tmp;
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");
271 /* fill the lp kernel */
272 w = 2 * G_PI * (self->lower_frequency / GST_AUDIO_FILTER (self)->format.rate);
273 kernel_lp = g_new (gdouble, len);
274 for (i = 0; i < len; ++i) {
275 if (i == (len - 1) / 2.0)
278 kernel_lp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
281 switch (self->window) {
283 kernel_lp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
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)));
289 case WINDOW_GAUSSIAN:
290 kernel_lp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
293 kernel_lp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
296 kernel_lp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
301 /* normalize for unity gain at DC */
303 for (i = 0; i < len; ++i)
305 for (i = 0; i < len; ++i)
308 /* fill the hp kernel */
309 w = 2 * G_PI * (self->upper_frequency / GST_AUDIO_FILTER (self)->format.rate);
310 kernel_hp = g_new (gdouble, len);
311 for (i = 0; i < len; ++i) {
312 if (i == (len - 1) / 2.0)
315 kernel_hp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0);
318 switch (self->window) {
320 kernel_hp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
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)));
326 case WINDOW_GAUSSIAN:
327 kernel_hp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
330 kernel_hp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
333 kernel_hp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
338 /* normalize for unity gain at DC */
340 for (i = 0; i < len; ++i)
342 for (i = 0; i < len; ++i)
345 /* do spectral inversion to go from lowpass to highpass */
346 for (i = 0; i < len; ++i)
347 kernel_hp[i] = -kernel_hp[i];
349 kernel_hp[(len - 1) / 2] += 1.0;
351 kernel_hp[len / 2 - 1] += 0.5;
352 kernel_hp[len / 2] += 0.5;
355 /* combine the two kernels */
356 kernel = g_new (gdouble, len);
358 for (i = 0; i < len; ++i)
359 kernel[i] = kernel_lp[i] + kernel_hp[i];
361 /* free the helper kernels */
365 /* do spectral inversion to go from bandreject to bandpass
367 if (self->mode == MODE_BAND_PASS) {
368 for (i = 0; i < len; ++i)
369 kernel[i] = -kernel[i];
370 kernel[len / 2] += 1;
373 gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
374 kernel, self->kernel_length, (len - 1) / 2);
377 /* GstAudioFilter vmethod implementations */
379 /* get notified of caps and plug in the correct process function */
381 gst_audio_wsincband_setup (GstAudioFilter * base, GstRingBufferSpec * format)
383 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
385 gst_audio_wsincband_build_kernel (self);
387 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, format);
391 gst_audio_wsincband_finalize (GObject * object)
393 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
395 g_mutex_free (self->lock);
398 G_OBJECT_CLASS (parent_class)->finalize (object);
402 gst_audio_wsincband_set_property (GObject * object, guint prop_id,
403 const GValue * value, GParamSpec * pspec)
405 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
407 g_return_if_fail (GST_IS_AUDIO_WSINC_BAND (self));
413 g_mutex_lock (self->lock);
414 val = g_value_get_int (value);
418 if (val != self->kernel_length) {
419 gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER
421 self->kernel_length = val;
422 gst_audio_wsincband_build_kernel (self);
424 g_mutex_unlock (self->lock);
427 case PROP_LOWER_FREQUENCY:
428 g_mutex_lock (self->lock);
429 self->lower_frequency = g_value_get_float (value);
430 gst_audio_wsincband_build_kernel (self);
431 g_mutex_unlock (self->lock);
433 case PROP_UPPER_FREQUENCY:
434 g_mutex_lock (self->lock);
435 self->upper_frequency = g_value_get_float (value);
436 gst_audio_wsincband_build_kernel (self);
437 g_mutex_unlock (self->lock);
440 g_mutex_lock (self->lock);
441 self->mode = g_value_get_enum (value);
442 gst_audio_wsincband_build_kernel (self);
443 g_mutex_unlock (self->lock);
446 g_mutex_lock (self->lock);
447 self->window = g_value_get_enum (value);
448 gst_audio_wsincband_build_kernel (self);
449 g_mutex_unlock (self->lock);
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458 gst_audio_wsincband_get_property (GObject * object, guint prop_id,
459 GValue * value, GParamSpec * pspec)
461 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
465 g_value_set_int (value, self->kernel_length);
467 case PROP_LOWER_FREQUENCY:
468 g_value_set_float (value, self->lower_frequency);
470 case PROP_UPPER_FREQUENCY:
471 g_value_set_float (value, self->upper_frequency);
474 g_value_set_enum (value, self->mode);
477 g_value_set_enum (value, self->window);
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);