775290530b335c5b8c8887d4b18f3c837e758fed
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audiowsincband.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 /* elementfactory information */
42 static GstElementDetails gst_bpwsinc_details = GST_ELEMENT_DETAILS (
43   "BPWSinc",
44   "Filter/Effect/Audio",
45   "Band-Pass Windowed sinc filter",
46   "Thomas <thomas@apestaart.org>, "
47   "Steven W. Smith"
48 );
49
50 enum {
51   /* FILL ME */
52   LAST_SIGNAL
53 };
54
55 enum {
56   ARG_0,
57   ARG_LENGTH,
58   ARG_LOWER_FREQUENCY,
59   ARG_UPPER_FREQUENCY,
60 };
61
62 #define GST_TYPE_BPWSINC \
63   (gst_bpwsinc_get_type())
64 #define GST_BPWSINC(obj) \
65       (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BPWSINC,GstBPWSinc))
66 #define GST_BPWSINC_CLASS(klass) \
67       (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstBPWSinc))
68 #define GST_IS_BPWSINC(obj) \
69       (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BPWSINC))
70 #define GST_IS_BPWSINC_CLASS(obj) \
71       (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BPWSINC))
72
73 typedef struct _GstBPWSinc GstBPWSinc;
74 typedef struct _GstBPWSincClass GstBPWSincClass;
75
76 struct _GstBPWSinc
77 {
78   GstElement element;
79
80   GstPad *sinkpad, *srcpad;
81
82   double frequency;
83   double lower_frequency, upper_frequency;
84   int wing_size;        /* length of a "wing" of the filter; 
85                            actual length is 2 * wing_size + 1 */
86
87   gfloat *residue;      /* buffer for left-over samples from previous buffer */
88   double *kernel;
89 };
90
91 struct _GstBPWSincClass
92 {
93     GstElementClass parent_class;
94 };
95
96 static void gst_bpwsinc_base_init               (gpointer g_class);
97 static void gst_bpwsinc_class_init              (GstBPWSincClass * klass);
98 static void gst_bpwsinc_init                    (GstBPWSinc * filter);
99
100 static void gst_bpwsinc_set_property    (GObject * object, guint prop_id,
101                                          const GValue * value, 
102                                          GParamSpec * pspec);
103 static void gst_bpwsinc_get_property    (GObject * object, guint prop_id,
104                                          GValue * value, GParamSpec * pspec);
105
106 static void gst_bpwsinc_chain           (GstPad * pad, GstData *_data);
107 static GstPadLinkReturn
108        gst_bpwsinc_sink_connect                 (GstPad * pad, const GstCaps * caps);
109
110 static GstElementClass *parent_class = NULL;
111 /*static guint gst_bpwsinc_signals[LAST_SIGNAL] = { 0 }; */
112
113 GType gst_bpwsinc_get_type (void)
114 {
115   static GType bpwsinc_type = 0;
116
117   if (!bpwsinc_type) {
118     static const GTypeInfo bpwsinc_info = {
119       sizeof (GstBPWSincClass), 
120       gst_bpwsinc_base_init, 
121       NULL,
122       (GClassInitFunc) gst_bpwsinc_class_init, NULL, NULL,
123       sizeof (GstBPWSinc), 0,
124       (GInstanceInitFunc) gst_bpwsinc_init,
125     };
126
127     bpwsinc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstBPWSinc", 
128                                            &bpwsinc_info, 0);
129   }
130   return bpwsinc_type;
131 }
132
133 static void
134 gst_bpwsinc_base_init (gpointer g_class)
135 {
136   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
137
138   /* register src pads */
139   gst_element_class_add_pad_template (element_class,
140       gst_static_pad_template_get (&gst_filter_src_template));
141   gst_element_class_add_pad_template (element_class,
142     gst_static_pad_template_get (&gst_filter_sink_template));
143
144   gst_element_class_set_details (element_class, &gst_bpwsinc_details);  
145 }
146
147 static void
148 gst_bpwsinc_class_init (GstBPWSincClass * klass)
149 {
150   GObjectClass *gobject_class;
151   GstElementClass *gstelement_class;
152
153   gobject_class = (GObjectClass *) klass;
154   gstelement_class = (GstElementClass *) klass;
155
156   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
157
158   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOWER_FREQUENCY,
159       g_param_spec_double ("lower-frequency", "Lower Frequency", 
160                            "Cut-off lower frequency (relative to sample rate)", 
161                            0.0, 0.5,
162                            0, G_PARAM_READWRITE));
163   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_UPPER_FREQUENCY,
164       g_param_spec_double ("upper-frequency", "Upper Frequency", 
165                            "Cut-off upper frequency (relative to sample rate)", 
166                            0.0, 0.5,
167                            0, G_PARAM_READWRITE));
168   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LENGTH,
169       g_param_spec_int ("length", "Length", 
170                         "N such that the filter length = 2N + 1",
171                            1, G_MAXINT, 
172                            1, G_PARAM_READWRITE));
173
174   gobject_class->set_property = gst_bpwsinc_set_property;
175   gobject_class->get_property = gst_bpwsinc_get_property;
176 }
177
178 static void
179 gst_bpwsinc_init (GstBPWSinc * filter)
180 {
181   filter->sinkpad = gst_pad_new_from_template (
182       gst_static_pad_template_get (&gst_filter_sink_template), "sink");
183   gst_pad_set_chain_function (filter->sinkpad, gst_bpwsinc_chain);
184   gst_pad_set_link_function (filter->sinkpad, gst_bpwsinc_sink_connect);
185   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
186
187   filter->srcpad = gst_pad_new_from_template (
188       gst_static_pad_template_get (&gst_filter_src_template), "src");
189   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
190
191   filter->wing_size = 50;
192   filter->lower_frequency = 0.25;
193   filter->upper_frequency = 0.3;
194   filter->kernel = NULL;
195 }
196
197 static GstPadLinkReturn
198 gst_bpwsinc_sink_connect (GstPad * pad, const GstCaps * caps)
199 {
200   int i = 0;
201   double sum = 0.0;
202   int len = 0;
203   double *kernel_lp, *kernel_hp;
204   GstPadLinkReturn set_retval;
205   
206   GstBPWSinc *filter = GST_BPWSINC (gst_pad_get_parent (pad));
207
208   g_assert (GST_IS_PAD (pad));
209   g_assert (caps != NULL);
210
211   set_retval = gst_pad_try_set_caps (filter->srcpad, caps);
212   
213   if (set_retval > 0)
214   {
215     len = filter->wing_size;
216     /* fill the lp kernel */
217     GST_DEBUG (
218                "bpwsinc: initializing LP kernel of length %d with cut-off %f", 
219                len * 2 + 1, filter->lower_frequency);
220     kernel_lp = (double *) g_malloc (sizeof (double) * (2 * len + 1));
221     for (i = 0; i <= len * 2; ++i)
222     {
223       if (i == len)
224         kernel_lp[i] = 2 * M_PI * filter->lower_frequency;
225       else
226         kernel_lp[i] = sin (2 * M_PI * filter->lower_frequency * (i - len)) 
227                      / (i - len);
228       /* Blackman windowing */
229       kernel_lp[i] *= (0.42 - 0.5 * cos (M_PI * i / len) 
230                             + 0.08 * cos (2 * M_PI * i / len));
231     }
232
233     /* normalize for unity gain at DC
234      * FIXME: sure this is not supposed to be quadratic ? */
235     sum = 0.0;
236     for (i = 0; i <= len * 2; ++i) sum += kernel_lp[i];
237     for (i = 0; i <= len * 2; ++i) kernel_lp[i] /= sum;
238
239     /* fill the hp kernel */
240     GST_DEBUG (
241                "bpwsinc: initializing HP kernel of length %d with cut-off %f", 
242                len * 2 + 1, filter->upper_frequency);
243     kernel_hp = (double *) g_malloc (sizeof (double) * (2 * len + 1));
244     for (i = 0; i <= len * 2; ++i)
245     {
246       if (i == len)
247         kernel_hp[i] = 2 * M_PI * filter->upper_frequency;
248       else
249         kernel_hp[i] = sin (2 * M_PI * filter->upper_frequency * (i - len)) 
250                      / (i - len);
251       /* Blackman windowing */
252       kernel_hp[i] *= (0.42 - 0.5 * cos (M_PI * i / len) 
253                             + 0.08 * cos (2 * M_PI * i / len));
254     }
255
256     /* normalize for unity gain at DC
257      * FIXME: sure this is not supposed to be quadratic ? */
258     sum = 0.0;
259     for (i = 0; i <= len * 2; ++i) sum += kernel_hp[i];
260     for (i = 0; i <= len * 2; ++i) kernel_hp[i] /= sum;
261
262     /* do spectral inversion to get a HP filter */
263     for (i = 0; i <= len * 2; ++i) kernel_hp[i] = -kernel_hp[i];
264     kernel_hp[len] += 1;
265
266     /* combine the two filters */
267
268     filter->kernel = (double *) g_malloc (sizeof (double) * (2 * len + 1));
269
270     for (i = 0; i <= len * 2; ++i)
271       filter->kernel[i] = kernel_lp[i] + kernel_hp[i];
272
273     /* do spectral inversion to go from band reject to bandpass */
274     for (i = 0; i <= len * 2; ++i) filter->kernel[i] = -filter->kernel[i];
275     filter->kernel[len] += 1;
276
277     /* free the helper kernels */
278     g_free (kernel_lp);
279     g_free (kernel_hp);
280
281     /* set up the residue memory space */
282     filter->residue = (gfloat *) g_malloc (sizeof (gfloat) * (len * 2 + 1));
283     for (i = 0; i <= len * 2; ++i) filter->residue[i] = 0.0;
284   }
285
286   return set_retval;
287 }
288
289 static void
290 gst_bpwsinc_chain (GstPad *pad, GstData *_data)
291 {
292   GstBuffer *buf = GST_BUFFER (_data);
293   GstBPWSinc *filter;
294   gfloat *src;
295   gfloat *input;
296   gint residue_samples;
297   gint input_samples;
298   gint total_samples;
299   int i, j;
300
301   filter = GST_BPWSINC (gst_pad_get_parent (pad));
302
303   /* FIXME: out of laziness, we copy the left-over bit from last buffer
304    * together with the incoming buffer to a new buffer to make the loop
305    * easy; this could be a lot more optimized though
306    * to make amends we keep the incoming buffer around and write our
307    * output samples there */
308
309   /* get a writable buffer */
310   buf = gst_buffer_copy_on_write (buf);
311
312   src = (gfloat *) GST_BUFFER_DATA (buf);
313   residue_samples = filter->wing_size * 2 + 1;
314   input_samples = GST_BUFFER_SIZE (buf) / sizeof (gfloat);
315   total_samples = residue_samples + input_samples;
316
317   input = (gfloat *) g_malloc (sizeof (gfloat) * total_samples);
318
319   /* copy the left-over bit */
320   memcpy (input, filter->residue, sizeof (gfloat) * residue_samples);
321
322   /* copy the new buffer */
323   memcpy (&input[residue_samples], src, sizeof (gfloat) * input_samples);
324   /* copy the tail of the current input buffer to the residue */
325   memcpy (filter->residue, &src[input_samples - residue_samples],
326           sizeof (gfloat) * residue_samples);
327
328   /* convolution */
329   /* since we copied the previous set of samples we needed before the actual
330    * input data, we need to add the filter length to our indices for input */
331   for (i = 0; i < input_samples; ++i)
332   {
333     src[i] = 0.0;
334     for (j = 0; j < residue_samples; ++j)
335       src[i] += input[i - j + residue_samples] * filter->kernel[j];
336   }
337   
338   g_free (input);
339   gst_pad_push (filter->srcpad, GST_DATA (buf));
340 }
341
342 static void
343 gst_bpwsinc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
344 {
345   GstBPWSinc *filter;
346
347   /* it's not null if we got it, but it might not be ours */
348   g_return_if_fail (GST_IS_BPWSINC (object));
349
350   filter = GST_BPWSINC (object);
351
352   switch (prop_id) {
353     case ARG_LENGTH:
354      filter->wing_size = g_value_get_int (value);
355      break; 
356     case ARG_LOWER_FREQUENCY:
357      filter->lower_frequency = g_value_get_double (value);
358      break; 
359     case ARG_UPPER_FREQUENCY:
360      filter->upper_frequency = g_value_get_double (value);
361      break; 
362     default:
363       break;
364   }
365 }
366
367 static void
368 gst_bpwsinc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
369 {
370   GstBPWSinc *filter;
371
372   /* it's not null if we got it, but it might not be ours */
373   g_return_if_fail (GST_IS_BPWSINC (object));
374   
375   filter = GST_BPWSINC (object);
376
377   switch (prop_id) {
378     case ARG_LENGTH:
379       g_value_set_int (value, filter->wing_size);
380       break;
381     case ARG_LOWER_FREQUENCY:
382       g_value_set_double (value, filter->lower_frequency);
383       break;
384     case ARG_UPPER_FREQUENCY:
385       g_value_set_double (value, filter->upper_frequency);
386       break;
387     default:
388       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
389       break;
390   }
391
392