1 /* -*- c-basic-offset: 2 -*-
3 * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 /* this windowed sinc filter is taken from the freely downloadable DSP book,
22 * "The Scientist and Engineer's Guide to Digital Signal Processing",
24 * available at http://www.dspguide.com/
28 * - this filter is totally unoptimized !
29 * - we do not destroy the allocated memory for filters and residue
30 * - this might be improved upon with bytestream
37 #include "gstfilter.h"
38 #include <math.h> /* M_PI */
39 #include <string.h> /* memmove */
41 /* elementfactory information */
42 static const GstElementDetails gst_bpwsinc_details =
43 GST_ELEMENT_DETAILS ("BPWSinc",
44 "Filter/Effect/Audio",
45 "Band-Pass Windowed sinc filter",
46 "Thomas <thomas@apestaart.org>, " "Steven W. Smith");
62 #define GST_TYPE_BPWSINC \
63 (gst_bpwsinc_get_type())
64 #define GST_BPWSINC(obj) \
65 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BPWSINC,GstBPWSinc))
66 #define GST_BPWSINC_CLASS(klass) \
67 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstBPWSinc))
68 #define GST_IS_BPWSINC(obj) \
69 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BPWSINC))
70 #define GST_IS_BPWSINC_CLASS(obj) \
71 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BPWSINC))
73 typedef struct _GstBPWSinc GstBPWSinc;
74 typedef struct _GstBPWSincClass GstBPWSincClass;
80 GstPad *sinkpad, *srcpad;
83 double lower_frequency, upper_frequency;
84 int wing_size; /* length of a "wing" of the filter;
85 actual length is 2 * wing_size + 1 */
87 gfloat *residue; /* buffer for left-over samples from previous buffer */
91 struct _GstBPWSincClass
93 GstElementClass parent_class;
96 static void gst_bpwsinc_base_init (gpointer g_class);
97 static void gst_bpwsinc_class_init (GstBPWSincClass * klass);
98 static void gst_bpwsinc_init (GstBPWSinc * filter);
100 static void gst_bpwsinc_set_property (GObject * object, guint prop_id,
101 const GValue * value, GParamSpec * pspec);
102 static void gst_bpwsinc_get_property (GObject * object, guint prop_id,
103 GValue * value, GParamSpec * pspec);
105 static void gst_bpwsinc_chain (GstPad * pad, GstData * _data);
106 static GstPadLinkReturn
107 gst_bpwsinc_sink_connect (GstPad * pad, const GstCaps * caps);
109 static GstElementClass *parent_class = NULL;
111 /*static guint gst_bpwsinc_signals[LAST_SIGNAL] = { 0 }; */
114 gst_bpwsinc_get_type (void)
116 static GType bpwsinc_type = 0;
119 static const GTypeInfo bpwsinc_info = {
120 sizeof (GstBPWSincClass),
121 gst_bpwsinc_base_init,
123 (GClassInitFunc) gst_bpwsinc_class_init, NULL, NULL,
124 sizeof (GstBPWSinc), 0,
125 (GInstanceInitFunc) gst_bpwsinc_init,
128 bpwsinc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstBPWSinc",
135 gst_bpwsinc_base_init (gpointer g_class)
137 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
139 /* register src pads */
140 gst_element_class_add_pad_template (element_class,
141 gst_static_pad_template_get (&gst_filter_src_template));
142 gst_element_class_add_pad_template (element_class,
143 gst_static_pad_template_get (&gst_filter_sink_template));
145 gst_element_class_set_details (element_class, &gst_bpwsinc_details);
149 gst_bpwsinc_class_init (GstBPWSincClass * klass)
151 GObjectClass *gobject_class;
152 GstElementClass *gstelement_class;
154 gobject_class = (GObjectClass *) klass;
155 gstelement_class = (GstElementClass *) klass;
157 parent_class = g_type_class_peek_parent (klass);
159 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOWER_FREQUENCY,
160 g_param_spec_double ("lower-frequency", "Lower Frequency",
161 "Cut-off lower frequency (relative to sample rate)",
162 0.0, 0.5, 0, G_PARAM_READWRITE));
163 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_UPPER_FREQUENCY,
164 g_param_spec_double ("upper-frequency", "Upper Frequency",
165 "Cut-off upper frequency (relative to sample rate)",
166 0.0, 0.5, 0, G_PARAM_READWRITE));
167 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LENGTH,
168 g_param_spec_int ("length", "Length",
169 "N such that the filter length = 2N + 1",
170 1, G_MAXINT, 1, G_PARAM_READWRITE));
172 gobject_class->set_property = gst_bpwsinc_set_property;
173 gobject_class->get_property = gst_bpwsinc_get_property;
177 gst_bpwsinc_init (GstBPWSinc * filter)
180 gst_pad_new_from_template (gst_static_pad_template_get
181 (&gst_filter_sink_template), "sink");
182 gst_pad_set_chain_function (filter->sinkpad, gst_bpwsinc_chain);
183 gst_pad_set_link_function (filter->sinkpad, gst_bpwsinc_sink_connect);
184 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
187 gst_pad_new_from_template (gst_static_pad_template_get
188 (&gst_filter_src_template), "src");
189 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
191 filter->wing_size = 50;
192 filter->lower_frequency = 0.25;
193 filter->upper_frequency = 0.3;
194 filter->kernel = NULL;
197 static GstPadLinkReturn
198 gst_bpwsinc_sink_connect (GstPad * pad, const GstCaps * caps)
203 double *kernel_lp, *kernel_hp;
204 GstPadLinkReturn set_retval;
206 GstBPWSinc *filter = GST_BPWSINC (gst_pad_get_parent (pad));
208 g_assert (GST_IS_PAD (pad));
209 g_assert (caps != NULL);
211 set_retval = gst_pad_try_set_caps (filter->srcpad, caps);
213 if (set_retval > 0) {
214 len = filter->wing_size;
215 /* fill the lp kernel */
216 GST_DEBUG ("bpwsinc: initializing LP kernel of length %d with cut-off %f",
217 len * 2 + 1, filter->lower_frequency);
218 kernel_lp = (double *) g_malloc (sizeof (double) * (2 * len + 1));
219 for (i = 0; i <= len * 2; ++i) {
221 kernel_lp[i] = 2 * M_PI * filter->lower_frequency;
223 kernel_lp[i] = sin (2 * M_PI * filter->lower_frequency * (i - len))
225 /* Blackman windowing */
226 kernel_lp[i] *= (0.42 - 0.5 * cos (M_PI * i / len)
227 + 0.08 * cos (2 * M_PI * i / len));
230 /* normalize for unity gain at DC
231 * FIXME: sure this is not supposed to be quadratic ? */
233 for (i = 0; i <= len * 2; ++i)
235 for (i = 0; i <= len * 2; ++i)
238 /* fill the hp kernel */
239 GST_DEBUG ("bpwsinc: initializing HP kernel of length %d with cut-off %f",
240 len * 2 + 1, filter->upper_frequency);
241 kernel_hp = (double *) g_malloc (sizeof (double) * (2 * len + 1));
242 for (i = 0; i <= len * 2; ++i) {
244 kernel_hp[i] = 2 * M_PI * filter->upper_frequency;
246 kernel_hp[i] = sin (2 * M_PI * filter->upper_frequency * (i - len))
248 /* Blackman windowing */
249 kernel_hp[i] *= (0.42 - 0.5 * cos (M_PI * i / len)
250 + 0.08 * cos (2 * M_PI * i / len));
253 /* normalize for unity gain at DC
254 * FIXME: sure this is not supposed to be quadratic ? */
256 for (i = 0; i <= len * 2; ++i)
258 for (i = 0; i <= len * 2; ++i)
261 /* do spectral inversion to get a HP filter */
262 for (i = 0; i <= len * 2; ++i)
263 kernel_hp[i] = -kernel_hp[i];
266 /* combine the two filters */
268 filter->kernel = (double *) g_malloc (sizeof (double) * (2 * len + 1));
270 for (i = 0; i <= len * 2; ++i)
271 filter->kernel[i] = kernel_lp[i] + kernel_hp[i];
273 /* do spectral inversion to go from band reject to bandpass */
274 for (i = 0; i <= len * 2; ++i)
275 filter->kernel[i] = -filter->kernel[i];
276 filter->kernel[len] += 1;
278 /* free the helper kernels */
282 /* set up the residue memory space */
283 filter->residue = (gfloat *) g_malloc (sizeof (gfloat) * (len * 2 + 1));
284 for (i = 0; i <= len * 2; ++i)
285 filter->residue[i] = 0.0;
292 gst_bpwsinc_chain (GstPad * pad, GstData * _data)
294 GstBuffer *buf = GST_BUFFER (_data);
298 gint residue_samples;
303 filter = GST_BPWSINC (gst_pad_get_parent (pad));
305 /* FIXME: out of laziness, we copy the left-over bit from last buffer
306 * together with the incoming buffer to a new buffer to make the loop
307 * easy; this could be a lot more optimized though
308 * to make amends we keep the incoming buffer around and write our
309 * output samples there */
311 /* get a writable buffer */
312 buf = gst_buffer_copy_on_write (buf);
314 src = (gfloat *) GST_BUFFER_DATA (buf);
315 residue_samples = filter->wing_size * 2 + 1;
316 input_samples = GST_BUFFER_SIZE (buf) / sizeof (gfloat);
317 total_samples = residue_samples + input_samples;
319 input = (gfloat *) g_malloc (sizeof (gfloat) * total_samples);
321 /* copy the left-over bit */
322 memcpy (input, filter->residue, sizeof (gfloat) * residue_samples);
324 /* copy the new buffer */
325 memcpy (&input[residue_samples], src, sizeof (gfloat) * input_samples);
326 /* copy the tail of the current input buffer to the residue */
327 memcpy (filter->residue, &src[input_samples - residue_samples],
328 sizeof (gfloat) * residue_samples);
331 /* since we copied the previous set of samples we needed before the actual
332 * input data, we need to add the filter length to our indices for input */
333 for (i = 0; i < input_samples; ++i) {
335 for (j = 0; j < residue_samples; ++j)
336 src[i] += input[i - j + residue_samples] * filter->kernel[j];
340 gst_pad_push (filter->srcpad, GST_DATA (buf));
344 gst_bpwsinc_set_property (GObject * object, guint prop_id, const GValue * value,
349 g_return_if_fail (GST_IS_BPWSINC (object));
351 filter = GST_BPWSINC (object);
355 filter->wing_size = g_value_get_int (value);
357 case ARG_LOWER_FREQUENCY:
358 filter->lower_frequency = g_value_get_double (value);
360 case ARG_UPPER_FREQUENCY:
361 filter->upper_frequency = g_value_get_double (value);
369 gst_bpwsinc_get_property (GObject * object, guint prop_id, GValue * value,
374 g_return_if_fail (GST_IS_BPWSINC (object));
376 filter = GST_BPWSINC (object);
380 g_value_set_int (value, filter->wing_size);
382 case ARG_LOWER_FREQUENCY:
383 g_value_set_double (value, filter->lower_frequency);
385 case ARG_UPPER_FREQUENCY:
386 g_value_set_double (value, filter->upper_frequency);
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);