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 GstElementDetails gst_bpwsinc_details = {
44 "Filter/Audio/Effect",
46 "Band-Pass Windowed sinc filter",
48 "Thomas <thomas@apestaart.org>",
49 "(C) 2002 Steven W. Smith",
64 #define GST_TYPE_BPWSINC \
65 (gst_bpwsinc_get_type())
66 #define GST_BPWSINC(obj) \
67 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BPWSINC,GstBPWSinc))
68 #define GST_BPWSINC_CLASS(klass) \
69 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstBPWSinc))
70 #define GST_IS_BPWSINC(obj) \
71 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BPWSINC))
72 #define GST_IS_BPWSINC_CLASS(obj) \
73 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BPWSINC))
75 typedef struct _GstBPWSinc GstBPWSinc;
76 typedef struct _GstBPWSincClass GstBPWSincClass;
82 GstPad *sinkpad, *srcpad;
85 double lower_frequency, upper_frequency;
86 int wing_size; /* length of a "wing" of the filter;
87 actual length is 2 * wing_size + 1 */
89 gfloat *residue; /* buffer for left-over samples from previous buffer */
93 struct _GstBPWSincClass
95 GstElementClass parent_class;
98 static void gst_bpwsinc_class_init (GstBPWSincClass * klass);
99 static void gst_bpwsinc_init (GstBPWSinc * filter);
101 static void gst_bpwsinc_set_property (GObject * object, guint prop_id,
102 const GValue * value,
104 static void gst_bpwsinc_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static void gst_bpwsinc_chain (GstPad * pad, GstBuffer * buf);
108 static GstPadLinkReturn
109 gst_bpwsinc_sink_connect (GstPad * pad, GstCaps * caps);
111 static GstElementClass *parent_class = NULL;
112 /*static guint gst_bpwsinc_signals[LAST_SIGNAL] = { 0 }; */
114 GType gst_bpwsinc_get_type (void)
116 static GType bpwsinc_type = 0;
119 static const GTypeInfo bpwsinc_info = {
120 sizeof (GstBPWSincClass), NULL, NULL,
121 (GClassInitFunc) gst_bpwsinc_class_init, NULL, NULL,
122 sizeof (GstBPWSinc), 0,
123 (GInstanceInitFunc) gst_bpwsinc_init,
126 bpwsinc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstBPWSinc",
133 gst_bpwsinc_class_init (GstBPWSincClass * klass)
135 GObjectClass *gobject_class;
136 GstElementClass *gstelement_class;
138 gobject_class = (GObjectClass *) klass;
139 gstelement_class = (GstElementClass *) klass;
141 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
143 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOWER_FREQUENCY,
144 g_param_spec_double ("lower-frequency", "Lower Frequency",
145 "Cut-off lower frequency (relative to sample rate)",
147 0, G_PARAM_READWRITE));
148 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_UPPER_FREQUENCY,
149 g_param_spec_double ("upper-frequency", "Upper Frequency",
150 "Cut-off upper frequency (relative to sample rate)",
152 0, G_PARAM_READWRITE));
153 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LENGTH,
154 g_param_spec_int ("length", "Length",
155 "N such that the filter length = 2N + 1",
157 1, G_PARAM_READWRITE));
159 gobject_class->set_property = gst_bpwsinc_set_property;
160 gobject_class->get_property = gst_bpwsinc_get_property;
164 gst_bpwsinc_init (GstBPWSinc * filter)
166 filter->sinkpad = gst_pad_new_from_template (gst_filter_sink_factory (), "sink");
167 gst_pad_set_chain_function (filter->sinkpad, gst_bpwsinc_chain);
168 gst_pad_set_link_function (filter->sinkpad, gst_bpwsinc_sink_connect);
169 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
171 filter->srcpad = gst_pad_new_from_template (gst_filter_src_factory (), "src");
172 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
174 filter->wing_size = 50;
175 filter->lower_frequency = 0.25;
176 filter->upper_frequency = 0.3;
177 filter->kernel = NULL;
180 static GstPadLinkReturn
181 gst_bpwsinc_sink_connect (GstPad * pad, GstCaps * caps)
186 double *kernel_lp, *kernel_hp;
187 GstPadLinkReturn set_retval;
189 GstBPWSinc *filter = GST_BPWSINC (gst_pad_get_parent (pad));
191 g_assert (GST_IS_PAD (pad));
192 g_assert (caps != NULL);
194 if (!GST_CAPS_IS_FIXED (caps))
195 return GST_PAD_LINK_DELAYED;
197 set_retval = gst_pad_try_set_caps (filter->srcpad, caps);
201 len = filter->wing_size;
202 /* fill the lp kernel */
204 "bpwsinc: initializing LP kernel of length %d with cut-off %f",
205 len * 2 + 1, filter->lower_frequency);
206 kernel_lp = (double *) g_malloc (sizeof (double) * (2 * len + 1));
207 for (i = 0; i <= len * 2; ++i)
210 kernel_lp[i] = 2 * M_PI * filter->lower_frequency;
212 kernel_lp[i] = sin (2 * M_PI * filter->lower_frequency * (i - len))
214 /* Blackman windowing */
215 kernel_lp[i] *= (0.42 - 0.5 * cos (M_PI * i / len)
216 + 0.08 * cos (2 * M_PI * i / len));
219 /* normalize for unity gain at DC
220 * FIXME: sure this is not supposed to be quadratic ? */
222 for (i = 0; i <= len * 2; ++i) sum += kernel_lp[i];
223 for (i = 0; i <= len * 2; ++i) kernel_lp[i] /= sum;
225 /* fill the hp kernel */
227 "bpwsinc: initializing HP kernel of length %d with cut-off %f",
228 len * 2 + 1, filter->upper_frequency);
229 kernel_hp = (double *) g_malloc (sizeof (double) * (2 * len + 1));
230 for (i = 0; i <= len * 2; ++i)
233 kernel_hp[i] = 2 * M_PI * filter->upper_frequency;
235 kernel_hp[i] = sin (2 * M_PI * filter->upper_frequency * (i - len))
237 /* Blackman windowing */
238 kernel_hp[i] *= (0.42 - 0.5 * cos (M_PI * i / len)
239 + 0.08 * cos (2 * M_PI * i / len));
242 /* normalize for unity gain at DC
243 * FIXME: sure this is not supposed to be quadratic ? */
245 for (i = 0; i <= len * 2; ++i) sum += kernel_hp[i];
246 for (i = 0; i <= len * 2; ++i) kernel_hp[i] /= sum;
248 /* do spectral inversion to get a HP filter */
249 for (i = 0; i <= len * 2; ++i) kernel_hp[i] = -kernel_hp[i];
252 /* combine the two filters */
254 filter->kernel = (double *) g_malloc (sizeof (double) * (2 * len + 1));
256 for (i = 0; i <= len * 2; ++i)
257 filter->kernel[i] = kernel_lp[i] + kernel_hp[i];
259 /* do spectral inversion to go from band reject to bandpass */
260 for (i = 0; i <= len * 2; ++i) filter->kernel[i] = -filter->kernel[i];
261 filter->kernel[len] += 1;
263 /* free the helper kernels */
267 /* set up the residue memory space */
268 filter->residue = (gfloat *) g_malloc (sizeof (gfloat) * (len * 2 + 1));
269 for (i = 0; i <= len * 2; ++i) filter->residue[i] = 0.0;
276 gst_bpwsinc_chain (GstPad *pad, GstBuffer *buf)
281 gint residue_samples;
286 filter = GST_BPWSINC (gst_pad_get_parent (pad));
288 /* FIXME: out of laziness, we copy the left-over bit from last buffer
289 * together with the incoming buffer to a new buffer to make the loop
290 * easy; this could be a lot more optimized though
291 * to make amends we keep the incoming buffer around and write our
292 * output samples there */
294 /* get a writable buffer */
295 buf = gst_buffer_copy_on_write (buf);
297 src = (gfloat *) GST_BUFFER_DATA (buf);
298 residue_samples = filter->wing_size * 2 + 1;
299 input_samples = GST_BUFFER_SIZE (buf) / sizeof (gfloat);
300 total_samples = residue_samples + input_samples;
302 input = (gfloat *) g_malloc (sizeof (gfloat) * total_samples);
304 /* copy the left-over bit */
305 memcpy (input, filter->residue, sizeof (gfloat) * residue_samples);
307 /* copy the new buffer */
308 memcpy (&input[residue_samples], src, sizeof (gfloat) * input_samples);
309 /* copy the tail of the current input buffer to the residue */
310 memcpy (filter->residue, &src[input_samples - residue_samples],
311 sizeof (gfloat) * residue_samples);
314 /* since we copied the previous set of samples we needed before the actual
315 * input data, we need to add the filter length to our indices for input */
316 for (i = 0; i < input_samples; ++i)
319 for (j = 0; j < residue_samples; ++j)
320 src[i] += input[i - j + residue_samples] * filter->kernel[j];
324 gst_pad_push (filter->srcpad, buf);
328 gst_bpwsinc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
332 /* it's not null if we got it, but it might not be ours */
333 g_return_if_fail (GST_IS_BPWSINC (object));
335 filter = GST_BPWSINC (object);
339 filter->wing_size = g_value_get_int (value);
341 case ARG_LOWER_FREQUENCY:
342 filter->lower_frequency = g_value_get_double (value);
344 case ARG_UPPER_FREQUENCY:
345 filter->upper_frequency = g_value_get_double (value);
353 gst_bpwsinc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
357 /* it's not null if we got it, but it might not be ours */
358 g_return_if_fail (GST_IS_BPWSINC (object));
360 filter = GST_BPWSINC (object);
364 g_value_set_int (value, filter->wing_size);
366 case ARG_LOWER_FREQUENCY:
367 g_value_set_double (value, filter->lower_frequency);
369 case ARG_UPPER_FREQUENCY:
370 g_value_set_double (value, filter->upper_frequency);
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);