Unify the long descriptions in the plugin details (#337263).
[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 =
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");
46
47 enum
48 {
49   /* FILL ME */
50   LAST_SIGNAL
51 };
52
53 enum
54 {
55   ARG_0,
56   ARG_LENGTH,
57   ARG_FREQUENCY
58 };
59
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))
70
71 typedef struct _GstLPWSinc GstLPWSinc;
72 typedef struct _GstLPWSincClass GstLPWSincClass;
73
74 struct _GstLPWSinc
75 {
76   GstElement element;
77
78   GstPad *sinkpad, *srcpad;
79
80   double frequency;
81   int wing_size;                /* length of a "wing" of the filter; 
82                                    actual length is 2 * wing_size + 1 */
83
84   gfloat *residue;              /* buffer for left-over samples from previous buffer */
85   double *kernel;
86 };
87
88 struct _GstLPWSincClass
89 {
90   GstElementClass parent_class;
91 };
92
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);
96
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);
101
102 static void gst_lpwsinc_chain (GstPad * pad, GstData * _data);
103 static GstPadLinkReturn
104 gst_lpwsinc_sink_connect (GstPad * pad, const GstCaps * caps);
105
106 static GstElementClass *parent_class = NULL;
107
108 /*static guint gst_lpwsinc_signals[LAST_SIGNAL] = { 0 }; */
109
110 GType
111 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),
118       gst_lpwsinc_base_init,
119       NULL,
120       (GClassInitFunc) gst_lpwsinc_class_init, NULL, NULL,
121       sizeof (GstLPWSinc), 0,
122       (GInstanceInitFunc) gst_lpwsinc_init,
123     };
124
125     lpwsinc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstLPWSinc",
126         &lpwsinc_info, 0);
127   }
128   return lpwsinc_type;
129 }
130
131 static void
132 gst_lpwsinc_base_init (gpointer g_class)
133 {
134   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
135
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));
141
142   gst_element_class_set_details (element_class, &gst_lpwsinc_details);
143 }
144
145 static void
146 gst_lpwsinc_class_init (GstLPWSincClass * klass)
147 {
148   GObjectClass *gobject_class;
149   GstElementClass *gstelement_class;
150
151   gobject_class = (GObjectClass *) klass;
152   gstelement_class = (GstElementClass *) klass;
153
154   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
155
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));
164
165   gobject_class->set_property = gst_lpwsinc_set_property;
166   gobject_class->get_property = gst_lpwsinc_get_property;
167 }
168
169 static void
170 gst_lpwsinc_init (GstLPWSinc * filter)
171 {
172   filter->sinkpad =
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);
178
179   filter->srcpad =
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);
183
184   filter->wing_size = 50;
185   filter->frequency = 0.25;
186   filter->kernel = NULL;
187 }
188
189 static GstPadLinkReturn
190 gst_lpwsinc_sink_connect (GstPad * pad, const GstCaps * caps)
191 {
192   int i = 0;
193   double sum = 0.0;
194   int len = 0;
195   GstLPWSinc *filter = GST_LPWSINC (gst_pad_get_parent (pad));
196   GstPadLinkReturn set_retval;
197
198   g_assert (GST_IS_PAD (pad));
199   g_assert (caps != NULL);
200
201   set_retval = gst_pad_try_set_caps (filter->srcpad, caps);
202
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));
211
212     for (i = 0; i <= len * 2; ++i) {
213       if (i == len)
214         filter->kernel[i] = 2 * M_PI * filter->frequency;
215       else
216         filter->kernel[i] = sin (2 * M_PI * filter->frequency * (i - len))
217             / (i - len);
218       /* windowing */
219       filter->kernel[i] *= (0.54 - 0.46 * cos (M_PI * i / len));
220     }
221
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;
228
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;
233   }
234
235   return set_retval;
236 }
237
238 static void
239 gst_lpwsinc_chain (GstPad * pad, GstData * _data)
240 {
241   GstBuffer *buf = GST_BUFFER (_data);
242   GstLPWSinc *filter;
243   gfloat *src;
244   gfloat *input;
245   gint residue_samples;
246   gint input_samples;
247   gint total_samples;
248   int i, j;
249
250   filter = GST_LPWSINC (gst_pad_get_parent (pad));
251
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 */
257
258   /* get a writable buffer */
259   buf = gst_buffer_copy_on_write (buf);
260
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;
265
266   input = (gfloat *) g_malloc (sizeof (gfloat) * total_samples);
267
268   /* copy the left-over bit */
269   memcpy (input, filter->residue, sizeof (gfloat) * residue_samples);
270
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);
276
277   /* convolution */
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) {
281     src[i] = 0.0;
282     for (j = 0; j < residue_samples; ++j)
283       src[i] += input[i - j + residue_samples] * filter->kernel[j];
284   }
285
286   g_free (input);
287   gst_pad_push (filter->srcpad, GST_DATA (buf));
288 }
289
290 static void
291 gst_lpwsinc_set_property (GObject * object, guint prop_id, const GValue * value,
292     GParamSpec * pspec)
293 {
294   GstLPWSinc *filter;
295
296   g_return_if_fail (GST_IS_LPWSINC (object));
297
298   filter = GST_LPWSINC (object);
299
300   switch (prop_id) {
301     case ARG_LENGTH:
302       filter->wing_size = g_value_get_int (value);
303       break;
304     case ARG_FREQUENCY:
305       filter->frequency = g_value_get_double (value);
306       break;
307     default:
308       break;
309   }
310 }
311
312 static void
313 gst_lpwsinc_get_property (GObject * object, guint prop_id, GValue * value,
314     GParamSpec * pspec)
315 {
316   GstLPWSinc *filter;
317
318   g_return_if_fail (GST_IS_LPWSINC (object));
319
320   filter = GST_LPWSINC (object);
321
322   switch (prop_id) {
323     case ARG_LENGTH:
324       g_value_set_int (value, filter->wing_size);
325       break;
326     case ARG_FREQUENCY:
327       g_value_set_double (value, filter->frequency);
328       break;
329     default:
330       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331       break;
332   }
333 }