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 static GstElementDetails gst_lpwsinc_details =
42 GST_ELEMENT_DETAILS ("Low-pass Windowed sinc filter",
43 "Filter/Effect/Audio",
44 "Low-pass Windowed sinc filter",
45 "Thomas <thomas@apestaart.org>, " "Steven W. Smith");
60 #define GST_TYPE_LPWSINC \
61 (gst_lpwsinc_get_type())
62 #define GST_LPWSINC(obj) \
63 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_LPWSINC,GstLPWSinc))
64 #define GST_LPWSINC_CLASS(klass) \
65 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstLPWSinc))
66 #define GST_IS_LPWSINC(obj) \
67 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_LPWSINC))
68 #define GST_IS_LPWSINC_CLASS(obj) \
69 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_LPWSINC))
71 typedef struct _GstLPWSinc GstLPWSinc;
72 typedef struct _GstLPWSincClass GstLPWSincClass;
78 GstPad *sinkpad, *srcpad;
81 int wing_size; /* length of a "wing" of the filter;
82 actual length is 2 * wing_size + 1 */
84 gfloat *residue; /* buffer for left-over samples from previous buffer */
88 struct _GstLPWSincClass
90 GstElementClass parent_class;
93 static void gst_lpwsinc_base_init (gpointer g_class);
94 static void gst_lpwsinc_class_init (GstLPWSincClass * klass);
95 static void gst_lpwsinc_init (GstLPWSinc * filter);
97 static void gst_lpwsinc_set_property (GObject * object, guint prop_id,
98 const GValue * value, GParamSpec * pspec);
99 static void gst_lpwsinc_get_property (GObject * object, guint prop_id,
100 GValue * value, GParamSpec * pspec);
102 static void gst_lpwsinc_chain (GstPad * pad, GstData * _data);
103 static GstPadLinkReturn
104 gst_lpwsinc_sink_connect (GstPad * pad, const GstCaps * caps);
106 static GstElementClass *parent_class = NULL;
108 /*static guint gst_lpwsinc_signals[LAST_SIGNAL] = { 0 }; */
111 gst_lpwsinc_get_type (void)
113 static GType lpwsinc_type = 0;
116 static const GTypeInfo lpwsinc_info = {
117 sizeof (GstLPWSincClass),
118 gst_lpwsinc_base_init,
120 (GClassInitFunc) gst_lpwsinc_class_init, NULL, NULL,
121 sizeof (GstLPWSinc), 0,
122 (GInstanceInitFunc) gst_lpwsinc_init,
125 lpwsinc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstLPWSinc",
132 gst_lpwsinc_base_init (gpointer g_class)
134 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
136 /* register src pads */
137 gst_element_class_add_pad_template (element_class,
138 gst_static_pad_template_get (&gst_filter_src_template));
139 gst_element_class_add_pad_template (element_class,
140 gst_static_pad_template_get (&gst_filter_sink_template));
142 gst_element_class_set_details (element_class, &gst_lpwsinc_details);
146 gst_lpwsinc_class_init (GstLPWSincClass * klass)
148 GObjectClass *gobject_class;
149 GstElementClass *gstelement_class;
151 gobject_class = (GObjectClass *) klass;
152 gstelement_class = (GstElementClass *) klass;
154 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
156 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREQUENCY,
157 g_param_spec_double ("frequency", "Frequency",
158 "Cut-off Frequency relative to sample rate)",
159 0.0, 0.5, 0, G_PARAM_READWRITE));
160 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LENGTH,
161 g_param_spec_int ("length", "Length",
162 "N such that the filter length = 2N + 1",
163 1, G_MAXINT, 1, G_PARAM_READWRITE));
165 gobject_class->set_property = gst_lpwsinc_set_property;
166 gobject_class->get_property = gst_lpwsinc_get_property;
170 gst_lpwsinc_init (GstLPWSinc * filter)
173 gst_pad_new_from_template (gst_static_pad_template_get
174 (&gst_filter_sink_template), "sink");
175 gst_pad_set_chain_function (filter->sinkpad, gst_lpwsinc_chain);
176 gst_pad_set_link_function (filter->sinkpad, gst_lpwsinc_sink_connect);
177 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
180 gst_pad_new_from_template (gst_static_pad_template_get
181 (&gst_filter_src_template), "src");
182 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
184 filter->wing_size = 50;
185 filter->frequency = 0.25;
186 filter->kernel = NULL;
189 static GstPadLinkReturn
190 gst_lpwsinc_sink_connect (GstPad * pad, const GstCaps * caps)
195 GstLPWSinc *filter = GST_LPWSINC (gst_pad_get_parent (pad));
196 GstPadLinkReturn set_retval;
198 g_assert (GST_IS_PAD (pad));
199 g_assert (caps != NULL);
201 set_retval = gst_pad_try_set_caps (filter->srcpad, caps);
203 if (set_retval > 0) {
204 /* connection works, so init the filter */
205 /* FIXME: remember to free it */
206 /* fill the kernel */
207 g_print ("DEBUG: initing filter kernel\n");
208 len = filter->wing_size;
209 GST_DEBUG ("lpwsinc: initializing filter kernel of length %d", len * 2 + 1);
210 filter->kernel = (double *) g_malloc (sizeof (double) * (2 * len + 1));
212 for (i = 0; i <= len * 2; ++i) {
214 filter->kernel[i] = 2 * M_PI * filter->frequency;
216 filter->kernel[i] = sin (2 * M_PI * filter->frequency * (i - len))
219 filter->kernel[i] *= (0.54 - 0.46 * cos (M_PI * i / len));
222 /* normalize for unity gain at DC
223 * FIXME: sure this is not supposed to be quadratic ? */
224 for (i = 0; i <= len * 2; ++i)
225 sum += filter->kernel[i];
226 for (i = 0; i <= len * 2; ++i)
227 filter->kernel[i] /= sum;
229 /* set up the residue memory space */
230 filter->residue = (gfloat *) g_malloc (sizeof (gfloat) * (len * 2 + 1));
231 for (i = 0; i <= len * 2; ++i)
232 filter->residue[i] = 0.0;
239 gst_lpwsinc_chain (GstPad * pad, GstData * _data)
241 GstBuffer *buf = GST_BUFFER (_data);
245 gint residue_samples;
250 filter = GST_LPWSINC (gst_pad_get_parent (pad));
252 /* FIXME: out of laziness, we copy the left-over bit from last buffer
253 * together with the incoming buffer to a new buffer to make the loop
254 * easy; this could be a lot more optimized though
255 * to make amends we keep the incoming buffer around and write our
256 * output samples there */
258 /* get a writable buffer */
259 buf = gst_buffer_copy_on_write (buf);
261 src = (gfloat *) GST_BUFFER_DATA (buf);
262 residue_samples = filter->wing_size * 2 + 1;
263 input_samples = GST_BUFFER_SIZE (buf) / sizeof (gfloat);
264 total_samples = residue_samples + input_samples;
266 input = (gfloat *) g_malloc (sizeof (gfloat) * total_samples);
268 /* copy the left-over bit */
269 memcpy (input, filter->residue, sizeof (gfloat) * residue_samples);
271 /* copy the new buffer */
272 memcpy (&input[residue_samples], src, sizeof (gfloat) * input_samples);
273 /* copy the tail of the current input buffer to the residue */
274 memcpy (filter->residue, &src[input_samples - residue_samples],
275 sizeof (gfloat) * residue_samples);
278 /* since we copied the previous set of samples we needed before the actual
279 * input data, we need to add the filter length to our indices for input */
280 for (i = 0; i < input_samples; ++i) {
282 for (j = 0; j < residue_samples; ++j)
283 src[i] += input[i - j + residue_samples] * filter->kernel[j];
287 gst_pad_push (filter->srcpad, GST_DATA (buf));
291 gst_lpwsinc_set_property (GObject * object, guint prop_id, const GValue * value,
296 g_return_if_fail (GST_IS_LPWSINC (object));
298 filter = GST_LPWSINC (object);
302 filter->wing_size = g_value_get_int (value);
305 filter->frequency = g_value_get_double (value);
313 gst_lpwsinc_get_property (GObject * object, guint prop_id, GValue * value,
318 g_return_if_fail (GST_IS_LPWSINC (object));
320 filter = GST_LPWSINC (object);
324 g_value_set_int (value, filter->wing_size);
327 g_value_set_double (value, filter->frequency);
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);