bf2773e6f358bdc783ae9b36102986b3c85020f4
[platform/upstream/gst-plugins-good.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 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");
45
46 enum
47 {
48   /* FILL ME */
49   LAST_SIGNAL
50 };
51
52 enum
53 {
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_base_init (gpointer g_class);
93 static void gst_lpwsinc_class_init (GstLPWSincClass * klass);
94 static void gst_lpwsinc_init (GstLPWSinc * filter);
95
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);
100
101 static void gst_lpwsinc_chain (GstPad * pad, GstData * _data);
102 static GstPadLinkReturn
103 gst_lpwsinc_sink_connect (GstPad * pad, const GstCaps * caps);
104
105 static GstElementClass *parent_class = NULL;
106
107 /*static guint gst_lpwsinc_signals[LAST_SIGNAL] = { 0 }; */
108
109 GType
110 gst_lpwsinc_get_type (void)
111 {
112   static GType lpwsinc_type = 0;
113
114   if (!lpwsinc_type) {
115     static const GTypeInfo lpwsinc_info = {
116       sizeof (GstLPWSincClass),
117       gst_lpwsinc_base_init,
118       NULL,
119       (GClassInitFunc) gst_lpwsinc_class_init, NULL, NULL,
120       sizeof (GstLPWSinc), 0,
121       (GInstanceInitFunc) gst_lpwsinc_init,
122     };
123
124     lpwsinc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstLPWSinc",
125         &lpwsinc_info, 0);
126   }
127   return lpwsinc_type;
128 }
129
130 static void
131 gst_lpwsinc_base_init (gpointer g_class)
132 {
133   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
134
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));
140
141   gst_element_class_set_details (element_class, &gst_lpwsinc_details);
142 }
143
144 static void
145 gst_lpwsinc_class_init (GstLPWSincClass * klass)
146 {
147   GObjectClass *gobject_class;
148   GstElementClass *gstelement_class;
149
150   gobject_class = (GObjectClass *) klass;
151   gstelement_class = (GstElementClass *) klass;
152
153   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
154
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));
163
164   gobject_class->set_property = gst_lpwsinc_set_property;
165   gobject_class->get_property = gst_lpwsinc_get_property;
166 }
167
168 static void
169 gst_lpwsinc_init (GstLPWSinc * filter)
170 {
171   filter->sinkpad =
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);
177
178   filter->srcpad =
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);
182
183   filter->wing_size = 50;
184   filter->frequency = 0.25;
185   filter->kernel = NULL;
186 }
187
188 static GstPadLinkReturn
189 gst_lpwsinc_sink_connect (GstPad * pad, const GstCaps * caps)
190 {
191   int i = 0;
192   double sum = 0.0;
193   int len = 0;
194   GstLPWSinc *filter = GST_LPWSINC (gst_pad_get_parent (pad));
195   GstPadLinkReturn set_retval;
196
197   g_assert (GST_IS_PAD (pad));
198   g_assert (caps != NULL);
199
200   set_retval = gst_pad_try_set_caps (filter->srcpad, caps);
201
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));
210
211     for (i = 0; i <= len * 2; ++i) {
212       if (i == len)
213         filter->kernel[i] = 2 * M_PI * filter->frequency;
214       else
215         filter->kernel[i] = sin (2 * M_PI * filter->frequency * (i - len))
216             / (i - len);
217       /* windowing */
218       filter->kernel[i] *= (0.54 - 0.46 * cos (M_PI * i / len));
219     }
220
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;
227
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;
232   }
233
234   return set_retval;
235 }
236
237 static void
238 gst_lpwsinc_chain (GstPad * pad, GstData * _data)
239 {
240   GstBuffer *buf = GST_BUFFER (_data);
241   GstLPWSinc *filter;
242   gfloat *src;
243   gfloat *input;
244   gint residue_samples;
245   gint input_samples;
246   gint total_samples;
247   int i, j;
248
249   filter = GST_LPWSINC (gst_pad_get_parent (pad));
250
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 */
256
257   /* get a writable buffer */
258   buf = gst_buffer_copy_on_write (buf);
259
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;
264
265   input = (gfloat *) g_malloc (sizeof (gfloat) * total_samples);
266
267   /* copy the left-over bit */
268   memcpy (input, filter->residue, sizeof (gfloat) * residue_samples);
269
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);
275
276   /* convolution */
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) {
280     src[i] = 0.0;
281     for (j = 0; j < residue_samples; ++j)
282       src[i] += input[i - j + residue_samples] * filter->kernel[j];
283   }
284
285   g_free (input);
286   gst_pad_push (filter->srcpad, GST_DATA (buf));
287 }
288
289 static void
290 gst_lpwsinc_set_property (GObject * object, guint prop_id, const GValue * value,
291     GParamSpec * pspec)
292 {
293   GstLPWSinc *filter;
294
295   g_return_if_fail (GST_IS_LPWSINC (object));
296
297   filter = GST_LPWSINC (object);
298
299   switch (prop_id) {
300     case ARG_LENGTH:
301       filter->wing_size = g_value_get_int (value);
302       break;
303     case ARG_FREQUENCY:
304       filter->frequency = g_value_get_double (value);
305       break;
306     default:
307       break;
308   }
309 }
310
311 static void
312 gst_lpwsinc_get_property (GObject * object, guint prop_id, GValue * value,
313     GParamSpec * pspec)
314 {
315   GstLPWSinc *filter;
316
317   g_return_if_fail (GST_IS_LPWSINC (object));
318
319   filter = GST_LPWSINC (object);
320
321   switch (prop_id) {
322     case ARG_LENGTH:
323       g_value_set_int (value, filter->wing_size);
324       break;
325     case ARG_FREQUENCY:
326       g_value_set_double (value, filter->frequency);
327       break;
328     default:
329       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330       break;
331   }
332 }