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