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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, 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-audiowsinclimit
36 * Attenuates all frequencies above the cutoff frequency (low-pass) or all frequencies below the
37 * cutoff frequency (high-pass). 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 lowpass and highpass 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-1.0 audiotestsrc freq=1500 ! audioconvert ! audiowsinclimit mode=low-pass frequency=1000 length=501 ! audioconvert ! alsasink
49 * gst-launch-1.0 filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiowsinclimit mode=high-pass frequency=15000 length=501 ! audioconvert ! alsasink
50 * gst-launch-1.0 audiotestsrc wave=white-noise ! audioconvert ! audiowsinclimit mode=low-pass frequency=1000 length=10001 window=blackman ! audioconvert ! alsasink
62 #include <gst/audio/gstaudiofilter.h>
64 #include "audiowsinclimit.h"
66 #include "gst/glib-compat-private.h"
68 #define GST_CAT_DEFAULT gst_audio_wsinclimit_debug
69 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
86 #define GST_TYPE_AUDIO_WSINC_LIMIT_MODE (gst_audio_wsinclimit_mode_get_type ())
88 gst_audio_wsinclimit_mode_get_type (void)
90 static GType gtype = 0;
93 static const GEnumValue values[] = {
94 {MODE_LOW_PASS, "Low pass (default)",
96 {MODE_HIGH_PASS, "High pass",
101 gtype = g_enum_register_static ("GstAudioWSincLimitMode", values);
115 #define GST_TYPE_AUDIO_WSINC_LIMIT_WINDOW (gst_audio_wsinclimit_window_get_type ())
117 gst_audio_wsinclimit_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 ("GstAudioWSincLimitWindow", values);
141 #define gst_audio_wsinclimit_parent_class parent_class
142 G_DEFINE_TYPE (GstAudioWSincLimit, gst_audio_wsinclimit,
143 GST_TYPE_AUDIO_FX_BASE_FIR_FILTER);
145 static void gst_audio_wsinclimit_set_property (GObject * object, guint prop_id,
146 const GValue * value, GParamSpec * pspec);
147 static void gst_audio_wsinclimit_get_property (GObject * object, guint prop_id,
148 GValue * value, GParamSpec * pspec);
149 static void gst_audio_wsinclimit_finalize (GObject * object);
151 static gboolean gst_audio_wsinclimit_setup (GstAudioFilter * base,
152 const GstAudioInfo * info);
155 #define POW2(x) (x)*(x)
158 gst_audio_wsinclimit_class_init (GstAudioWSincLimitClass * klass)
160 GObjectClass *gobject_class = (GObjectClass *) klass;
161 GstElementClass *gstelement_class = (GstElementClass *) klass;
162 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
164 GST_DEBUG_CATEGORY_INIT (gst_audio_wsinclimit_debug, "audiowsinclimit", 0,
165 "Low-pass and High-pass Windowed sinc filter plugin");
167 gobject_class->set_property = gst_audio_wsinclimit_set_property;
168 gobject_class->get_property = gst_audio_wsinclimit_get_property;
169 gobject_class->finalize = gst_audio_wsinclimit_finalize;
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_FREQUENCY,
174 g_param_spec_float ("cutoff", "Cutoff",
175 "Cut-off Frequency (Hz)", 0.0, 100000.0, 0.0,
176 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (gobject_class, PROP_LENGTH,
178 g_param_spec_int ("length", "Length",
179 "Filter kernel length, will be rounded to the next odd number",
181 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (gobject_class, PROP_MODE,
184 g_param_spec_enum ("mode", "Mode",
185 "Low pass or high pass mode", GST_TYPE_AUDIO_WSINC_LIMIT_MODE,
187 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (gobject_class, PROP_WINDOW,
190 g_param_spec_enum ("window", "Window",
191 "Window function to use", GST_TYPE_AUDIO_WSINC_LIMIT_WINDOW,
193 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
195 gst_element_class_set_static_metadata (gstelement_class,
196 "Low pass & high pass filter", "Filter/Effect/Audio",
197 "Low pass and high pass windowed sinc filter",
198 "Thomas Vander Stichele <thomas at apestaart dot org>, "
200 "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
201 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
203 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_wsinclimit_setup);
207 gst_audio_wsinclimit_init (GstAudioWSincLimit * self)
209 self->mode = MODE_LOW_PASS;
210 self->window = WINDOW_HAMMING;
211 self->kernel_length = 101;
214 g_mutex_init (&self->lock);
218 gst_audio_wsinclimit_build_kernel (GstAudioWSincLimit * self,
219 const GstAudioInfo * info)
225 gdouble *kernel = NULL;
228 len = self->kernel_length;
231 rate = GST_AUDIO_INFO_RATE (info);
232 channels = GST_AUDIO_INFO_CHANNELS (info);
234 rate = GST_AUDIO_FILTER_RATE (self);
235 channels = GST_AUDIO_FILTER_CHANNELS (self);
239 GST_DEBUG ("rate not set yet");
244 GST_DEBUG ("channels not set yet");
248 /* Clamp cutoff frequency between 0 and the nyquist frequency */
249 self->cutoff = CLAMP (self->cutoff, 0.0, rate / 2);
251 GST_DEBUG ("gst_audio_wsinclimit_: initializing filter kernel of length %d "
252 "with cutoff %.2lf Hz "
255 (self->mode == MODE_LOW_PASS) ? "low-pass" : "high-pass");
257 /* fill the kernel */
258 w = 2 * G_PI * (self->cutoff / rate);
260 kernel = g_new (gdouble, len);
262 for (i = 0; i < len; ++i) {
263 if (i == (len - 1) / 2.0)
266 kernel[i] = sin (w * (i - (len - 1) / 2)) / (i - (len - 1) / 2.0);
269 switch (self->window) {
271 kernel[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
273 case WINDOW_BLACKMAN:
274 kernel[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
275 0.08 * cos (4 * G_PI * i / (len - 1)));
277 case WINDOW_GAUSSIAN:
278 kernel[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
281 kernel[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
284 kernel[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
289 /* normalize for unity gain at DC */
290 for (i = 0; i < len; ++i)
292 for (i = 0; i < len; ++i)
295 /* convert to highpass if specified */
296 if (self->mode == MODE_HIGH_PASS) {
297 for (i = 0; i < len; ++i)
298 kernel[i] = -kernel[i];
301 kernel[(len - 1) / 2] += 1.0;
303 kernel[len / 2 - 1] += 0.5;
304 kernel[len / 2] += 0.5;
308 gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
309 kernel, self->kernel_length, (len - 1) / 2, info);
312 /* GstAudioFilter vmethod implementations */
314 /* get notified of caps and plug in the correct process function */
316 gst_audio_wsinclimit_setup (GstAudioFilter * base, const GstAudioInfo * info)
318 GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (base);
320 gst_audio_wsinclimit_build_kernel (self, info);
322 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
326 gst_audio_wsinclimit_finalize (GObject * object)
328 GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (object);
330 g_mutex_clear (&self->lock);
332 G_OBJECT_CLASS (parent_class)->finalize (object);
336 gst_audio_wsinclimit_set_property (GObject * object, guint prop_id,
337 const GValue * value, GParamSpec * pspec)
339 GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (object);
341 g_return_if_fail (GST_IS_AUDIO_WSINC_LIMIT (self));
347 g_mutex_lock (&self->lock);
348 val = g_value_get_int (value);
352 if (val != self->kernel_length) {
353 gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER
355 self->kernel_length = val;
356 gst_audio_wsinclimit_build_kernel (self, NULL);
358 g_mutex_unlock (&self->lock);
362 g_mutex_lock (&self->lock);
363 self->cutoff = g_value_get_float (value);
364 gst_audio_wsinclimit_build_kernel (self, NULL);
365 g_mutex_unlock (&self->lock);
368 g_mutex_lock (&self->lock);
369 self->mode = g_value_get_enum (value);
370 gst_audio_wsinclimit_build_kernel (self, NULL);
371 g_mutex_unlock (&self->lock);
374 g_mutex_lock (&self->lock);
375 self->window = g_value_get_enum (value);
376 gst_audio_wsinclimit_build_kernel (self, NULL);
377 g_mutex_unlock (&self->lock);
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
386 gst_audio_wsinclimit_get_property (GObject * object, guint prop_id,
387 GValue * value, GParamSpec * pspec)
389 GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (object);
393 g_value_set_int (value, self->kernel_length);
396 g_value_set_float (value, self->cutoff);
399 g_value_set_enum (value, self->mode);
402 g_value_set_enum (value, self->window);
405 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);