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-audiowsinclimit
35 * @title: audiowsinclimit
36 *
37 * Attenuates all frequencies above the cutoff frequency (low-pass) or all frequencies below the
38 * cutoff frequency (high-pass). The length parameter controls the rolloff, the window parameter
39 * controls rolloff and stopband attenuation. The Hamming window provides a faster rolloff but a bit
40 * worse stopband attenuation, the other way around for the Blackman window.
41 *
42 * This element has the advantage over the Chebyshev lowpass and highpass filter that it has
43 * a much better rolloff when using a larger kernel size and almost linear phase. The only
44 * disadvantage is the much slower execution time with larger kernels.
45 *
46 * ## Example launch line
47 * |[
48 * gst-launch-1.0 audiotestsrc freq=1500 ! audioconvert ! audiowsinclimit mode=low-pass cutoff=1000 length=501 ! audioconvert ! alsasink
49 * gst-launch-1.0 filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiowsinclimit mode=high-pass cutoff=15000 length=501 ! audioconvert ! alsasink
50 * gst-launch-1.0 audiotestsrc wave=white-noise ! audioconvert ! audiowsinclimit mode=low-pass cutoff=1000 length=10001 window=blackman ! audioconvert ! alsasink
51 * ]|
52 *
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 "audiowsinclimit.h"
65
66 #include "gst/glib-compat-private.h"
67
68 #define GST_CAT_DEFAULT gst_audio_wsinclimit_debug
69 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
70
71 enum
72 {
73 PROP_0,
74 PROP_LENGTH,
75 PROP_FREQUENCY,
76 PROP_MODE,
77 PROP_WINDOW
78 };
79
80 enum
81 {
82 MODE_LOW_PASS = 0,
83 MODE_HIGH_PASS
84 };
85
86 #define GST_TYPE_AUDIO_WSINC_LIMIT_MODE (gst_audio_wsinclimit_mode_get_type ())
87 static GType
gst_audio_wsinclimit_mode_get_type(void)88 gst_audio_wsinclimit_mode_get_type (void)
89 {
90 static GType gtype = 0;
91
92 if (gtype == 0) {
93 static const GEnumValue values[] = {
94 {MODE_LOW_PASS, "Low pass (default)",
95 "low-pass"},
96 {MODE_HIGH_PASS, "High pass",
97 "high-pass"},
98 {0, NULL, NULL}
99 };
100
101 gtype = g_enum_register_static ("GstAudioWSincLimitMode", values);
102 }
103 return gtype;
104 }
105
106 enum
107 {
108 WINDOW_HAMMING = 0,
109 WINDOW_BLACKMAN,
110 WINDOW_GAUSSIAN,
111 WINDOW_COSINE,
112 WINDOW_HANN
113 };
114
115 #define GST_TYPE_AUDIO_WSINC_LIMIT_WINDOW (gst_audio_wsinclimit_window_get_type ())
116 static GType
gst_audio_wsinclimit_window_get_type(void)117 gst_audio_wsinclimit_window_get_type (void)
118 {
119 static GType gtype = 0;
120
121 if (gtype == 0) {
122 static const GEnumValue values[] = {
123 {WINDOW_HAMMING, "Hamming window (default)",
124 "hamming"},
125 {WINDOW_BLACKMAN, "Blackman window",
126 "blackman"},
127 {WINDOW_GAUSSIAN, "Gaussian window",
128 "gaussian"},
129 {WINDOW_COSINE, "Cosine window",
130 "cosine"},
131 {WINDOW_HANN, "Hann window",
132 "hann"},
133 {0, NULL, NULL}
134 };
135
136 gtype = g_enum_register_static ("GstAudioWSincLimitWindow", values);
137 }
138 return gtype;
139 }
140
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);
144 GST_ELEMENT_REGISTER_DEFINE (audiowsinclimit, "audiowsinclimit",
145 GST_RANK_NONE, GST_TYPE_AUDIO_WSINC_LIMIT);
146
147 static void gst_audio_wsinclimit_set_property (GObject * object, guint prop_id,
148 const GValue * value, GParamSpec * pspec);
149 static void gst_audio_wsinclimit_get_property (GObject * object, guint prop_id,
150 GValue * value, GParamSpec * pspec);
151 static void gst_audio_wsinclimit_finalize (GObject * object);
152
153 static gboolean gst_audio_wsinclimit_setup (GstAudioFilter * base,
154 const GstAudioInfo * info);
155
156
157 #define POW2(x) (x)*(x)
158
159 static void
gst_audio_wsinclimit_class_init(GstAudioWSincLimitClass * klass)160 gst_audio_wsinclimit_class_init (GstAudioWSincLimitClass * klass)
161 {
162 GObjectClass *gobject_class = (GObjectClass *) klass;
163 GstElementClass *gstelement_class = (GstElementClass *) klass;
164 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
165
166 GST_DEBUG_CATEGORY_INIT (gst_audio_wsinclimit_debug, "audiowsinclimit", 0,
167 "Low-pass and High-pass Windowed sinc filter plugin");
168
169 gobject_class->set_property = gst_audio_wsinclimit_set_property;
170 gobject_class->get_property = gst_audio_wsinclimit_get_property;
171 gobject_class->finalize = gst_audio_wsinclimit_finalize;
172
173 /* FIXME: Don't use the complete possible range but restrict the upper boundary
174 * so automatically generated UIs can use a slider */
175 g_object_class_install_property (gobject_class, PROP_FREQUENCY,
176 g_param_spec_float ("cutoff", "Cutoff",
177 "Cut-off Frequency (Hz)", 0.0, 100000.0, 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",
182 3, 256000, 101,
183 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
184
185 g_object_class_install_property (gobject_class, PROP_MODE,
186 g_param_spec_enum ("mode", "Mode",
187 "Low pass or high pass mode", GST_TYPE_AUDIO_WSINC_LIMIT_MODE,
188 MODE_LOW_PASS,
189 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
190
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_LIMIT_WINDOW,
194 WINDOW_HAMMING,
195 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
196
197 gst_element_class_set_static_metadata (gstelement_class,
198 "Low pass & high pass filter", "Filter/Effect/Audio",
199 "Low pass and high pass windowed sinc filter",
200 "Thomas Vander Stichele <thomas at apestaart dot org>, "
201 "Steven W. Smith, "
202 "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
203 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
204
205 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_wsinclimit_setup);
206
207 gst_type_mark_as_plugin_api (GST_TYPE_AUDIO_WSINC_LIMIT_MODE, 0);
208 gst_type_mark_as_plugin_api (GST_TYPE_AUDIO_WSINC_LIMIT_WINDOW, 0);
209 }
210
211 static void
gst_audio_wsinclimit_init(GstAudioWSincLimit * self)212 gst_audio_wsinclimit_init (GstAudioWSincLimit * self)
213 {
214 self->mode = MODE_LOW_PASS;
215 self->window = WINDOW_HAMMING;
216 self->kernel_length = 101;
217 self->cutoff = 0.0;
218
219 g_mutex_init (&self->lock);
220 }
221
222 static void
gst_audio_wsinclimit_build_kernel(GstAudioWSincLimit * self,const GstAudioInfo * info)223 gst_audio_wsinclimit_build_kernel (GstAudioWSincLimit * self,
224 const GstAudioInfo * info)
225 {
226 gint i = 0;
227 gdouble sum = 0.0;
228 gint len = 0;
229 gdouble w;
230 gdouble *kernel = NULL;
231 gint rate, channels;
232
233 len = self->kernel_length;
234
235 if (info) {
236 rate = GST_AUDIO_INFO_RATE (info);
237 channels = GST_AUDIO_INFO_CHANNELS (info);
238 } else {
239 rate = GST_AUDIO_FILTER_RATE (self);
240 channels = GST_AUDIO_FILTER_CHANNELS (self);
241 }
242
243 if (rate == 0) {
244 GST_DEBUG ("rate not set yet");
245 return;
246 }
247
248 if (channels == 0) {
249 GST_DEBUG ("channels not set yet");
250 return;
251 }
252
253 /* Clamp cutoff frequency between 0 and the nyquist frequency */
254 self->cutoff = CLAMP (self->cutoff, 0.0, rate / 2);
255
256 GST_DEBUG ("gst_audio_wsinclimit_: initializing filter kernel of length %d "
257 "with cutoff %.2lf Hz "
258 "for mode %s",
259 len, self->cutoff,
260 (self->mode == MODE_LOW_PASS) ? "low-pass" : "high-pass");
261
262 /* fill the kernel */
263 w = 2 * G_PI * (self->cutoff / rate);
264
265 kernel = g_new (gdouble, len);
266
267 for (i = 0; i < len; ++i) {
268 if (i == (len - 1) / 2.0)
269 kernel[i] = w;
270 else
271 kernel[i] = sin (w * (i - (len - 1) / 2)) / (i - (len - 1) / 2.0);
272
273 /* windowing */
274 switch (self->window) {
275 case WINDOW_HAMMING:
276 kernel[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1)));
277 break;
278 case WINDOW_BLACKMAN:
279 kernel[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) +
280 0.08 * cos (4 * G_PI * i / (len - 1)));
281 break;
282 case WINDOW_GAUSSIAN:
283 kernel[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1))));
284 break;
285 case WINDOW_COSINE:
286 kernel[i] *= cos (G_PI * i / (len - 1) - G_PI / 2);
287 break;
288 case WINDOW_HANN:
289 kernel[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1)));
290 break;
291 }
292 }
293
294 /* normalize for unity gain at DC */
295 for (i = 0; i < len; ++i)
296 sum += kernel[i];
297 for (i = 0; i < len; ++i)
298 kernel[i] /= sum;
299
300 /* convert to highpass if specified */
301 if (self->mode == MODE_HIGH_PASS) {
302 for (i = 0; i < len; ++i)
303 kernel[i] = -kernel[i];
304
305 if (len % 2 == 1) {
306 kernel[(len - 1) / 2] += 1.0;
307 } else {
308 kernel[len / 2 - 1] += 0.5;
309 kernel[len / 2] += 0.5;
310 }
311 }
312
313 gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self),
314 kernel, self->kernel_length, (len - 1) / 2, info);
315 }
316
317 /* GstAudioFilter vmethod implementations */
318
319 /* get notified of caps and plug in the correct process function */
320 static gboolean
gst_audio_wsinclimit_setup(GstAudioFilter * base,const GstAudioInfo * info)321 gst_audio_wsinclimit_setup (GstAudioFilter * base, const GstAudioInfo * info)
322 {
323 GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (base);
324
325 gst_audio_wsinclimit_build_kernel (self, info);
326
327 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
328 }
329
330 static void
gst_audio_wsinclimit_finalize(GObject * object)331 gst_audio_wsinclimit_finalize (GObject * object)
332 {
333 GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (object);
334
335 g_mutex_clear (&self->lock);
336
337 G_OBJECT_CLASS (parent_class)->finalize (object);
338 }
339
340 static void
gst_audio_wsinclimit_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)341 gst_audio_wsinclimit_set_property (GObject * object, guint prop_id,
342 const GValue * value, GParamSpec * pspec)
343 {
344 GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (object);
345
346 g_return_if_fail (GST_IS_AUDIO_WSINC_LIMIT (self));
347
348 switch (prop_id) {
349 case PROP_LENGTH:{
350 gint val;
351
352 g_mutex_lock (&self->lock);
353 val = g_value_get_int (value);
354 if (val % 2 == 0)
355 val++;
356
357 if (val != self->kernel_length) {
358 gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER
359 (self));
360 self->kernel_length = val;
361 gst_audio_wsinclimit_build_kernel (self, NULL);
362 }
363 g_mutex_unlock (&self->lock);
364 break;
365 }
366 case PROP_FREQUENCY:
367 g_mutex_lock (&self->lock);
368 self->cutoff = g_value_get_float (value);
369 gst_audio_wsinclimit_build_kernel (self, NULL);
370 g_mutex_unlock (&self->lock);
371 break;
372 case PROP_MODE:
373 g_mutex_lock (&self->lock);
374 self->mode = g_value_get_enum (value);
375 gst_audio_wsinclimit_build_kernel (self, NULL);
376 g_mutex_unlock (&self->lock);
377 break;
378 case PROP_WINDOW:
379 g_mutex_lock (&self->lock);
380 self->window = g_value_get_enum (value);
381 gst_audio_wsinclimit_build_kernel (self, NULL);
382 g_mutex_unlock (&self->lock);
383 break;
384 default:
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
386 break;
387 }
388 }
389
390 static void
gst_audio_wsinclimit_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)391 gst_audio_wsinclimit_get_property (GObject * object, guint prop_id,
392 GValue * value, GParamSpec * pspec)
393 {
394 GstAudioWSincLimit *self = GST_AUDIO_WSINC_LIMIT (object);
395
396 switch (prop_id) {
397 case PROP_LENGTH:
398 g_value_set_int (value, self->kernel_length);
399 break;
400 case PROP_FREQUENCY:
401 g_value_set_float (value, self->cutoff);
402 break;
403 case PROP_MODE:
404 g_value_set_enum (value, self->mode);
405 break;
406 case PROP_WINDOW:
407 g_value_set_enum (value, self->window);
408 break;
409 default:
410 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
411 break;
412 }
413 }
414