1 /* -*- c-basic-offset: 2 -*-
4 * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
5 * 2006 Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 * this windowed sinc filter is taken from the freely downloadable DSP book,
24 * "The Scientist and Engineer's Guide to Digital Signal Processing",
26 * available at http://www.dspguide.com/
28 * TODO: - Implement the convolution in place, probably only makes sense
29 * when using FFT convolution as currently the convolution itself
30 * is probably the bottleneck
31 * - Implement a band reject mode (spectral inversion)
32 * - Allow choosing between different windows (blackman, hanning, ...)
33 * - Specify filter length instead of 2*N+1
34 * FIXME: - Doesn't work at all with >1 channels
35 * - Is bandreject, not bandpass
45 #include <gst/audio/gstaudiofilter.h>
46 #include <gst/controller/gstcontroller.h>
48 #include "gstbpwsinc.h"
50 #define GST_CAT_DEFAULT gst_bpwsinc_debug
51 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
53 static const GstElementDetails bpwsinc_details =
54 GST_ELEMENT_DETAILS ("Band-pass Windowed sinc filter",
55 "Filter/Effect/Audio",
56 "Band-pass Windowed sinc filter",
57 "Thomas <thomas@apestaart.org>, "
59 "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>");
61 /* Filter signals and args */
76 #define ALLOWED_CAPS \
77 "audio/x-raw-float," \
78 " width = (int) 32, " \
79 " endianness = (int) BYTE_ORDER," \
80 " rate = (int) [ 1, MAX ]," \
81 " channels = (int) [ 1, MAX ]"
83 #define DEBUG_INIT(bla) \
84 GST_DEBUG_CATEGORY_INIT (gst_bpwsinc_debug, "bpwsinc", 0, "Band-pass Windowed sinc filter plugin");
86 GST_BOILERPLATE_FULL (GstBPWSinc, gst_bpwsinc, GstAudioFilter,
87 GST_TYPE_AUDIO_FILTER, DEBUG_INIT);
89 static void bpwsinc_set_property (GObject * object, guint prop_id,
90 const GValue * value, GParamSpec * pspec);
91 static void bpwsinc_get_property (GObject * object, guint prop_id,
92 GValue * value, GParamSpec * pspec);
94 static GstFlowReturn bpwsinc_transform_ip (GstBaseTransform * base,
96 static gboolean bpwsinc_setup (GstAudioFilter * base,
97 GstRingBufferSpec * format);
102 gst_bpwsinc_dispose (GObject * object)
104 GstBPWSinc *self = GST_BPWSINC (object);
107 g_free (self->residue);
108 self->residue = NULL;
112 g_free (self->kernel);
116 G_OBJECT_CLASS (parent_class)->dispose (object);
120 gst_bpwsinc_base_init (gpointer g_class)
122 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
125 gst_element_class_set_details (element_class, &bpwsinc_details);
127 caps = gst_caps_from_string (ALLOWED_CAPS);
128 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (g_class),
130 gst_caps_unref (caps);
134 gst_bpwsinc_class_init (GstBPWSincClass * klass)
136 GObjectClass *gobject_class;
137 GstBaseTransformClass *trans_class;
139 gobject_class = (GObjectClass *) klass;
140 trans_class = (GstBaseTransformClass *) klass;
142 gobject_class->set_property = bpwsinc_set_property;
143 gobject_class->get_property = bpwsinc_get_property;
144 gobject_class->dispose = gst_bpwsinc_dispose;
146 g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
147 g_param_spec_double ("lower-frequency", "Lower Frequency",
148 "Cut-off lower frequency (relative to sample rate)",
149 0.0, 0.5, 0, G_PARAM_READWRITE));
150 g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
151 g_param_spec_double ("upper-frequency", "Upper Frequency",
152 "Cut-off upper frequency (relative to sample rate)",
153 0.0, 0.5, 0, G_PARAM_READWRITE));
154 g_object_class_install_property (gobject_class, PROP_LENGTH,
155 g_param_spec_int ("length", "Length",
156 "N such that the filter length = 2N + 1",
157 1, G_MAXINT, 1, G_PARAM_READWRITE));
159 trans_class->transform_ip = GST_DEBUG_FUNCPTR (bpwsinc_transform_ip);
160 GST_AUDIO_FILTER_CLASS (klass)->setup = GST_DEBUG_FUNCPTR (bpwsinc_setup);
164 gst_bpwsinc_init (GstBPWSinc * self, GstBPWSincClass * g_class)
166 self->wing_size = 50;
167 self->lower_frequency = 0.25;
168 self->upper_frequency = 0.3;
170 self->residue = NULL;
174 /* GstAudioFilter vmethod implementations */
176 /* get notified of caps and plug in the correct process function */
178 bpwsinc_setup (GstAudioFilter * base, GstRingBufferSpec * format)
183 double *kernel_lp, *kernel_hp;
184 GstBPWSinc *self = GST_BPWSINC (base);
186 len = self->wing_size;
187 /* fill the lp kernel
188 * FIXME: refactor to own function, this is not caps related
190 GST_DEBUG ("bpwsinc: initializing LP kernel of length %d with cut-off %f",
191 len * 2 + 1, self->lower_frequency);
192 kernel_lp = (double *) g_malloc (sizeof (double) * (2 * len + 1));
193 for (i = 0; i <= len * 2; ++i) {
195 kernel_lp[i] = 2 * M_PI * self->lower_frequency;
197 kernel_lp[i] = sin (2 * M_PI * self->lower_frequency * (i - len))
199 /* Blackman windowing */
200 kernel_lp[i] *= (0.42 - 0.5 * cos (M_PI * i / len)
201 + 0.08 * cos (2 * M_PI * i / len));
204 /* normalize for unity gain at DC */
206 for (i = 0; i <= len * 2; ++i)
208 for (i = 0; i <= len * 2; ++i)
211 /* fill the hp kernel */
212 GST_DEBUG ("bpwsinc: initializing HP kernel of length %d with cut-off %f",
213 len * 2 + 1, self->upper_frequency);
214 kernel_hp = (double *) g_malloc (sizeof (double) * (2 * len + 1));
215 for (i = 0; i <= len * 2; ++i) {
217 kernel_hp[i] = 2 * M_PI * self->upper_frequency;
219 kernel_hp[i] = sin (2 * M_PI * self->upper_frequency * (i - len))
221 /* Blackman windowing */
222 kernel_hp[i] *= (0.42 - 0.5 * cos (M_PI * i / len)
223 + 0.08 * cos (2 * M_PI * i / len));
226 /* normalize for unity gain at DC */
228 for (i = 0; i <= len * 2; ++i)
230 for (i = 0; i <= len * 2; ++i)
233 /* combine the two kernels */
235 g_free (self->kernel);
236 self->kernel = (double *) g_malloc (sizeof (double) * (2 * len + 1));
238 for (i = 0; i <= len * 2; ++i)
239 self->kernel[i] = kernel_lp[i] + kernel_hp[i];
241 /* do spectral inversion to go from band reject to bandpass */
242 for (i = 0; i <= len * 2; ++i)
243 self->kernel[i] = -self->kernel[i];
244 self->kernel[len] += 1;
246 /* free the helper kernels */
250 /* set up the residue memory space */
252 g_free (self->residue);
254 self->residue = (gfloat *) g_malloc (sizeof (gfloat) * (len * 2 + 1));
255 for (i = 0; i <= len * 2; ++i)
256 self->residue[i] = 0.0;
261 /* GstBaseTransform vmethod implementations */
264 bpwsinc_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
266 GstBPWSinc *self = GST_BPWSINC (base);
267 GstClockTime timestamp;
276 /* don't process data in passthrough-mode */
277 if (gst_base_transform_is_passthrough (base))
280 /* FIXME: subdivide GST_BUFFER_SIZE into small chunks for smooth fades */
281 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
283 if (GST_CLOCK_TIME_IS_VALID (timestamp))
284 gst_object_sync_values (G_OBJECT (self), timestamp);
286 /* FIXME: out of laziness, we copy the left-over bit from last buffer
287 * together with the incoming buffer to a new buffer to make the loop
288 * easy; self could be a lot more optimized though
289 * to make amends we keep the incoming buffer around and write our
290 * output samples there */
292 src = (gfloat *) GST_BUFFER_DATA (outbuf);
293 residue_samples = self->wing_size * 2 + 1;
294 input_samples = GST_BUFFER_SIZE (outbuf) / sizeof (gfloat);
295 total_samples = residue_samples + input_samples;
297 input = (gfloat *) g_malloc (sizeof (gfloat) * total_samples);
299 /* copy the left-over bit */
300 memcpy (input, self->residue, sizeof (gfloat) * residue_samples);
302 /* copy the new buffer */
303 memcpy (&input[residue_samples], src, sizeof (gfloat) * input_samples);
304 /* copy the tail of the current input buffer to the residue */
305 memcpy (self->residue, &src[input_samples - residue_samples],
306 sizeof (gfloat) * residue_samples);
309 /* since we copied the previous set of samples we needed before the actual
310 * input data, we need to add the filter length to our indices for input */
311 for (i = 0; i < input_samples; ++i) {
313 for (j = 0; j < residue_samples; ++j)
314 src[i] += input[i - j + residue_samples] * self->kernel[j];
323 bpwsinc_set_property (GObject * object, guint prop_id, const GValue * value,
326 GstBPWSinc *self = GST_BPWSINC (object);
328 g_return_if_fail (GST_IS_BPWSINC (self));
332 self->wing_size = g_value_get_int (value);
334 case PROP_LOWER_FREQUENCY:
335 self->lower_frequency = g_value_get_double (value);
337 case PROP_UPPER_FREQUENCY:
338 self->upper_frequency = g_value_get_double (value);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347 bpwsinc_get_property (GObject * object, guint prop_id, GValue * value,
350 GstBPWSinc *self = GST_BPWSINC (object);
354 g_value_set_int (value, self->wing_size);
356 case PROP_LOWER_FREQUENCY:
357 g_value_set_double (value, self->lower_frequency);
359 case PROP_UPPER_FREQUENCY:
360 g_value_set_double (value, self->upper_frequency);
363 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);