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 = GST_ELEMENT_DETAILS ("LPWSinc",
42 "Filter/Effect/Audio",
43 "Low-pass Windowed sinc filter",
44 "Thomas <thomas@apestaart.org>, " "Steven W. Smith");
59 #define GST_TYPE_LPWSINC \
60 (gst_lpwsinc_get_type())
61 #define GST_LPWSINC(obj) \
62 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_LPWSINC,GstLPWSinc))
63 #define GST_LPWSINC_CLASS(klass) \
64 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstLPWSinc))
65 #define GST_IS_LPWSINC(obj) \
66 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_LPWSINC))
67 #define GST_IS_LPWSINC_CLASS(obj) \
68 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_LPWSINC))
70 typedef struct _GstLPWSinc GstLPWSinc;
71 typedef struct _GstLPWSincClass GstLPWSincClass;
77 GstPad *sinkpad, *srcpad;
80 int wing_size; /* length of a "wing" of the filter;
81 actual length is 2 * wing_size + 1 */
83 gfloat *residue; /* buffer for left-over samples from previous buffer */
87 struct _GstLPWSincClass
89 GstElementClass parent_class;
92 static void gst_lpwsinc_base_init (gpointer g_class);
93 static void gst_lpwsinc_class_init (GstLPWSincClass * klass);
94 static void gst_lpwsinc_init (GstLPWSinc * filter);
96 static void gst_lpwsinc_set_property (GObject * object, guint prop_id,
97 const GValue * value, GParamSpec * pspec);
98 static void gst_lpwsinc_get_property (GObject * object, guint prop_id,
99 GValue * value, GParamSpec * pspec);
101 static void gst_lpwsinc_chain (GstPad * pad, GstData * _data);
102 static GstPadLinkReturn
103 gst_lpwsinc_sink_connect (GstPad * pad, const GstCaps * caps);
105 static GstElementClass *parent_class = NULL;
107 /*static guint gst_lpwsinc_signals[LAST_SIGNAL] = { 0 }; */
110 gst_lpwsinc_get_type (void)
112 static GType lpwsinc_type = 0;
115 static const GTypeInfo lpwsinc_info = {
116 sizeof (GstLPWSincClass),
117 gst_lpwsinc_base_init,
119 (GClassInitFunc) gst_lpwsinc_class_init, NULL, NULL,
120 sizeof (GstLPWSinc), 0,
121 (GInstanceInitFunc) gst_lpwsinc_init,
124 lpwsinc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstLPWSinc",
131 gst_lpwsinc_base_init (gpointer g_class)
133 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
135 /* register src pads */
136 gst_element_class_add_pad_template (element_class,
137 gst_static_pad_template_get (&gst_filter_src_template));
138 gst_element_class_add_pad_template (element_class,
139 gst_static_pad_template_get (&gst_filter_sink_template));
141 gst_element_class_set_details (element_class, &gst_lpwsinc_details);
145 gst_lpwsinc_class_init (GstLPWSincClass * klass)
147 GObjectClass *gobject_class;
148 GstElementClass *gstelement_class;
150 gobject_class = (GObjectClass *) klass;
151 gstelement_class = (GstElementClass *) klass;
153 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
155 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREQUENCY,
156 g_param_spec_double ("frequency", "Frequency",
157 "Cut-off Frequency relative to sample rate)",
158 0.0, 0.5, 0, G_PARAM_READWRITE));
159 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LENGTH,
160 g_param_spec_int ("length", "Length",
161 "N such that the filter length = 2N + 1",
162 1, G_MAXINT, 1, G_PARAM_READWRITE));
164 gobject_class->set_property = gst_lpwsinc_set_property;
165 gobject_class->get_property = gst_lpwsinc_get_property;
169 gst_lpwsinc_init (GstLPWSinc * filter)
172 gst_pad_new_from_template (gst_static_pad_template_get
173 (&gst_filter_sink_template), "sink");
174 gst_pad_set_chain_function (filter->sinkpad, gst_lpwsinc_chain);
175 gst_pad_set_link_function (filter->sinkpad, gst_lpwsinc_sink_connect);
176 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
179 gst_pad_new_from_template (gst_static_pad_template_get
180 (&gst_filter_src_template), "src");
181 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
183 filter->wing_size = 50;
184 filter->frequency = 0.25;
185 filter->kernel = NULL;
188 static GstPadLinkReturn
189 gst_lpwsinc_sink_connect (GstPad * pad, const GstCaps * caps)
194 GstLPWSinc *filter = GST_LPWSINC (gst_pad_get_parent (pad));
195 GstPadLinkReturn set_retval;
197 g_assert (GST_IS_PAD (pad));
198 g_assert (caps != NULL);
200 set_retval = gst_pad_try_set_caps (filter->srcpad, caps);
202 if (set_retval > 0) {
203 /* connection works, so init the filter */
204 /* FIXME: remember to free it */
205 /* fill the kernel */
206 g_print ("DEBUG: initing filter kernel\n");
207 len = filter->wing_size;
208 GST_DEBUG ("lpwsinc: initializing filter kernel of length %d", len * 2 + 1);
209 filter->kernel = (double *) g_malloc (sizeof (double) * (2 * len + 1));
211 for (i = 0; i <= len * 2; ++i) {
213 filter->kernel[i] = 2 * M_PI * filter->frequency;
215 filter->kernel[i] = sin (2 * M_PI * filter->frequency * (i - len))
218 filter->kernel[i] *= (0.54 - 0.46 * cos (M_PI * i / len));
221 /* normalize for unity gain at DC
222 * FIXME: sure this is not supposed to be quadratic ? */
223 for (i = 0; i <= len * 2; ++i)
224 sum += filter->kernel[i];
225 for (i = 0; i <= len * 2; ++i)
226 filter->kernel[i] /= sum;
228 /* set up the residue memory space */
229 filter->residue = (gfloat *) g_malloc (sizeof (gfloat) * (len * 2 + 1));
230 for (i = 0; i <= len * 2; ++i)
231 filter->residue[i] = 0.0;
238 gst_lpwsinc_chain (GstPad * pad, GstData * _data)
240 GstBuffer *buf = GST_BUFFER (_data);
244 gint residue_samples;
249 filter = GST_LPWSINC (gst_pad_get_parent (pad));
251 /* FIXME: out of laziness, we copy the left-over bit from last buffer
252 * together with the incoming buffer to a new buffer to make the loop
253 * easy; this could be a lot more optimized though
254 * to make amends we keep the incoming buffer around and write our
255 * output samples there */
257 /* get a writable buffer */
258 buf = gst_buffer_copy_on_write (buf);
260 src = (gfloat *) GST_BUFFER_DATA (buf);
261 residue_samples = filter->wing_size * 2 + 1;
262 input_samples = GST_BUFFER_SIZE (buf) / sizeof (gfloat);
263 total_samples = residue_samples + input_samples;
265 input = (gfloat *) g_malloc (sizeof (gfloat) * total_samples);
267 /* copy the left-over bit */
268 memcpy (input, filter->residue, sizeof (gfloat) * residue_samples);
270 /* copy the new buffer */
271 memcpy (&input[residue_samples], src, sizeof (gfloat) * input_samples);
272 /* copy the tail of the current input buffer to the residue */
273 memcpy (filter->residue, &src[input_samples - residue_samples],
274 sizeof (gfloat) * residue_samples);
277 /* since we copied the previous set of samples we needed before the actual
278 * input data, we need to add the filter length to our indices for input */
279 for (i = 0; i < input_samples; ++i) {
281 for (j = 0; j < residue_samples; ++j)
282 src[i] += input[i - j + residue_samples] * filter->kernel[j];
286 gst_pad_push (filter->srcpad, GST_DATA (buf));
290 gst_lpwsinc_set_property (GObject * object, guint prop_id, const GValue * value,
295 /* it's not null if we got it, but it might not be ours */
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 /* it's not null if we got it, but it might not be ours */
319 g_return_if_fail (GST_IS_LPWSINC (object));
321 filter = GST_LPWSINC (object);
325 g_value_set_int (value, filter->wing_size);
328 g_value_set_double (value, filter->frequency);
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);