74cea70ef8ef53e2cf3fc1b62d532fdac02e2ded
[platform/upstream/gstreamer.git] / gst / audiofx / audiowsinclimit.c
1 /* -*- c-basic-offset: 2 -*-
2  * GStreamer
3  * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /* this windowed sinc filter is taken from the freely downloadable DSP book,
22  * "The Scientist and Engineer's Guide to Digital Signal Processing",
23  * chapter 16
24  * available at http://www.dspguide.com/
25  */
26
27 /* FIXME:
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
31  */
32
33 #include <gst/gst.h>
34 #include "gstfilter.h"
35 #include <math.h>               /* M_PI */
36 #include <string.h>             /* memmove */
37
38 GstElementDetails gst_lpwsinc_details = {
39   "LPWSinc",
40   "Filter/Audio/Effect",
41   "LGPL",
42   "Low-pass Windowed sinc filter",
43   VERSION,
44   "Thomas <thomas@apestaart.org>",
45   "(C) 2002 Steven W. Smith",
46 };
47
48 enum {
49   /* FILL ME */
50   LAST_SIGNAL
51 };
52
53 enum {
54   ARG_0,
55   ARG_LENGTH,
56   ARG_FREQUENCY,
57 };
58
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))
69
70 typedef struct _GstLPWSinc GstLPWSinc;
71 typedef struct _GstLPWSincClass GstLPWSincClass;
72
73 struct _GstLPWSinc
74 {
75   GstElement element;
76
77   GstPad *sinkpad, *srcpad;
78
79   double frequency;
80   int wing_size;        /* length of a "wing" of the filter; 
81                            actual length is 2 * wing_size + 1 */
82
83   gfloat *residue;      /* buffer for left-over samples from previous buffer */
84   double *kernel;
85 };
86
87 struct _GstLPWSincClass
88 {
89     GstElementClass parent_class;
90 };
91
92 static void gst_lpwsinc_class_init              (GstLPWSincClass * klass);
93 static void gst_lpwsinc_init                    (GstLPWSinc * filter);
94
95 static void gst_lpwsinc_set_property    (GObject * object, guint prop_id,
96                                          const GValue * value, 
97                                          GParamSpec * pspec);
98 static void gst_lpwsinc_get_property    (GObject * object, guint prop_id,
99                                          GValue * value, GParamSpec * pspec);
100
101 static void gst_lpwsinc_chain           (GstPad * pad, GstBuffer * buf);
102 static GstPadLinkReturn
103        gst_lpwsinc_sink_connect                 (GstPad * pad, GstCaps * caps);
104
105 static GstElementClass *parent_class = NULL;
106 /*static guint gst_lpwsinc_signals[LAST_SIGNAL] = { 0 }; */
107
108 GType gst_lpwsinc_get_type (void)
109 {
110   static GType lpwsinc_type = 0;
111
112   if (!lpwsinc_type) {
113     static const GTypeInfo lpwsinc_info = {
114       sizeof (GstLPWSincClass), NULL, NULL,
115       (GClassInitFunc) gst_lpwsinc_class_init, NULL, NULL,
116       sizeof (GstLPWSinc), 0,
117       (GInstanceInitFunc) gst_lpwsinc_init,
118     };
119
120     lpwsinc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstLPWSinc", 
121                                            &lpwsinc_info, 0);
122   }
123   return lpwsinc_type;
124 }
125
126 static void
127 gst_lpwsinc_class_init (GstLPWSincClass * klass)
128 {
129   GObjectClass *gobject_class;
130   GstElementClass *gstelement_class;
131
132   gobject_class = (GObjectClass *) klass;
133   gstelement_class = (GstElementClass *) klass;
134
135   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
136
137   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREQUENCY,
138       g_param_spec_double ("frequency", "Frequency", 
139                            "Cut-off Frequency relative to sample rate)", 
140                            0.0, 0.5,
141                            0, G_PARAM_READWRITE));
142   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LENGTH,
143       g_param_spec_int ("length", "Length", 
144                         "N such that the filter length = 2N + 1",
145                            1, G_MAXINT, 
146                            1, G_PARAM_READWRITE));
147
148   gobject_class->set_property = gst_lpwsinc_set_property;
149   gobject_class->get_property = gst_lpwsinc_get_property;
150 }
151
152 static void
153 gst_lpwsinc_init (GstLPWSinc * filter)
154 {
155   filter->sinkpad = gst_pad_new_from_template (gst_filter_sink_factory (), "sink");
156   gst_pad_set_chain_function (filter->sinkpad, gst_lpwsinc_chain);
157   gst_pad_set_link_function (filter->sinkpad, gst_lpwsinc_sink_connect);
158   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
159
160   filter->srcpad = gst_pad_new_from_template (gst_filter_src_factory (), "src");
161   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
162
163   filter->wing_size = 50;
164   filter->frequency = 0.25;
165   filter->kernel = NULL;
166 }
167
168 static GstPadLinkReturn
169 gst_lpwsinc_sink_connect (GstPad * pad, GstCaps * caps)
170 {
171   int i = 0;
172   double sum = 0.0;
173   int len = 0;
174   GstLPWSinc *filter = GST_LPWSINC (gst_pad_get_parent (pad));
175   GstPadLinkReturn set_retval;
176
177   g_assert (GST_IS_PAD (pad));
178   g_assert (caps != NULL);
179
180   if (!GST_CAPS_IS_FIXED (caps))
181     return GST_PAD_LINK_DELAYED;
182
183   set_retval = gst_pad_try_set_caps(filter->srcpad, caps);
184   
185   if (set_retval > 0) 
186   {
187     /* connection works, so init the filter */
188     /* FIXME: remember to free it */
189     /* fill the kernel */
190     g_print ("DEBUG: initing filter kernel\n");
191     len = filter->wing_size;
192     GST_DEBUG (GST_CAT_PLUGIN_INFO, 
193                "lpwsinc: initializing filter kernel of length %d", len * 2 + 1);
194     filter->kernel = (double *) g_malloc (sizeof (double) * (2 * len + 1));
195
196     for (i = 0; i <= len * 2; ++i)
197     {
198       if (i == len)
199         filter->kernel[i] = 2 * M_PI * filter->frequency;
200       else
201         filter->kernel[i] = sin (2 * M_PI * filter->frequency * (i - len)) 
202                           / (i - len);
203       /* windowing */
204       filter->kernel[i] *= (0.54 - 0.46 * cos (M_PI * i / len));
205     }
206
207     /* normalize for unity gain at DC
208      * FIXME: sure this is not supposed to be quadratic ? */
209     for (i = 0; i <= len * 2; ++i) sum += filter->kernel[i];
210     for (i = 0; i <= len * 2; ++i) filter->kernel[i] /= sum;
211
212     /* set up the residue memory space */
213     filter->residue = (gfloat *) g_malloc (sizeof (gfloat) * (len * 2 + 1));
214     for (i = 0; i <= len * 2; ++i) filter->residue[i] = 0.0;
215   }
216
217   return set_retval;
218 }
219
220 static void
221 gst_lpwsinc_chain (GstPad * pad, GstBuffer * buf)
222 {
223   GstLPWSinc *filter;
224   gfloat *src;
225   gfloat *input;
226   gint residue_samples;
227   gint input_samples;
228   gint total_samples;
229   int i, j;
230
231   filter = GST_LPWSINC (gst_pad_get_parent (pad));
232
233   /* FIXME: out of laziness, we copy the left-over bit from last buffer
234    * together with the incoming buffer to a new buffer to make the loop
235    * easy; this could be a lot more optimized though
236    * to make amends we keep the incoming buffer around and write our
237    * output samples there */
238
239   /* get a writable buffer */
240   buf = gst_buffer_copy_on_write (buf);
241
242   src = (gfloat *) GST_BUFFER_DATA (buf);
243   residue_samples = filter->wing_size * 2 + 1;
244   input_samples = GST_BUFFER_SIZE (buf) / sizeof (gfloat);
245   total_samples = residue_samples + input_samples;
246
247   input = (gfloat *) g_malloc (sizeof (gfloat) * total_samples);
248
249   /* copy the left-over bit */
250   memcpy (input, filter->residue, sizeof (gfloat) * residue_samples);
251
252   /* copy the new buffer */
253   memcpy (&input[residue_samples], src, sizeof (gfloat) * input_samples);
254   /* copy the tail of the current input buffer to the residue */
255   memcpy (filter->residue, &src[input_samples - residue_samples],
256           sizeof (gfloat) * residue_samples);
257
258   /* convolution */
259   /* since we copied the previous set of samples we needed before the actual
260    * input data, we need to add the filter length to our indices for input */
261   for (i = 0; i < input_samples; ++i)
262   {
263     src[i] = 0.0;
264     for (j = 0; j < residue_samples; ++j)
265       src[i] += input[i - j + residue_samples] * filter->kernel[j];
266   }
267   
268   g_free (input);
269   gst_pad_push (filter->srcpad, buf);
270 }
271
272 static void
273 gst_lpwsinc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
274 {
275   GstLPWSinc *filter;
276
277   /* it's not null if we got it, but it might not be ours */
278   g_return_if_fail (GST_IS_LPWSINC (object));
279
280   filter = GST_LPWSINC (object);
281
282   switch (prop_id) {
283     case ARG_LENGTH:
284      filter->wing_size = g_value_get_int (value);
285      break; 
286     case ARG_FREQUENCY:
287      filter->frequency = g_value_get_double (value);
288      break; 
289     default:
290       break;
291   }
292 }
293
294 static void
295 gst_lpwsinc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
296 {
297   GstLPWSinc *filter;
298
299   /* it's not null if we got it, but it might not be ours */
300   g_return_if_fail (GST_IS_LPWSINC (object));
301   
302   filter = GST_LPWSINC (object);
303
304   switch (prop_id) {
305     case ARG_LENGTH:
306       g_value_set_int (value, filter->wing_size);
307       break;
308     case ARG_FREQUENCY:
309       g_value_set_double (value, filter->frequency);
310       break;
311     default:
312       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313       break;
314   }
315
316