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