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>
6 * 2007 Sebastian Dröge <slomo@circular-chaos.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 * this windowed sinc filter is taken from the freely downloadable DSP book,
25 * "The Scientist and Engineer's Guide to Digital Signal Processing",
27 * available at http://www.dspguide.com/
29 * TODO: - Implement the convolution in place, probably only makes sense
30 * when using FFT convolution as currently the convolution itself
31 * is probably the bottleneck
32 * - Maybe allow cascading the filter to get a better stopband attenuation.
33 * Can be done by convolving a filter kernel with itself
34 * - Drop the first kernel_length/2 samples and append the same number of
35 * samples on EOS as the first few samples are essentialy zero.
39 * SECTION:element-bpwsinc
40 * @short_description: Windowed Sinc band pass and band reject filter
44 * Attenuates all frequencies outside (bandpass) or inside (bandreject) of a frequency
45 * band. The length parameter controls the rolloff, the window parameter
46 * controls rolloff and stopband attenuation. The Hamming window provides a faster rolloff but a bit
47 * worse stopband attenuation, the other way around for the Blackman window.
50 * This element has the advantage over the Chebyshev bandpass and bandreject filter that it has
51 * a much better rolloff when using a larger kernel size and almost linear phase. The only
52 * disadvantage is the much slower execution time with larger kernels.
54 * <title>Example launch line</title>
57 * gst-launch audiotestsrc freq=1500 ! audioconvert ! bpwsinc mode=band-pass lower-frequency=3000 upper-frequency=10000 length=501 window=blackman ! audioconvert ! alsasink
58 * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! bpwsinc mode=band-reject lower-frequency=59 upper-frequency=61 length=10001 window=hamming ! audioconvert ! alsasink
59 * gst-launch audiotestsrc wave=white-noise ! audioconvert ! bpwsinc mode=band-pass lower-frequency=1000 upper-frequency=2000 length=31 ! audioconvert ! alsasink
72 #include <gst/audio/gstaudiofilter.h>
73 #include <gst/controller/gstcontroller.h>
75 #include "gstbpwsinc.h"
77 #define GST_CAT_DEFAULT gst_bpwsinc_debug
78 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
80 static const GstElementDetails bpwsinc_details =
81 GST_ELEMENT_DETAILS ("Band-pass and Band-reject Windowed sinc filter",
82 "Filter/Effect/Audio",
83 "Band-pass Windowed sinc filter",
84 "Thomas <thomas@apestaart.org>, "
86 "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
87 "Sebastian Dröge <slomo@circular-chaos.org>");
89 /* Filter signals and args */
100 PROP_LOWER_FREQUENCY,
101 PROP_UPPER_FREQUENCY,
112 #define GST_TYPE_BPWSINC_MODE (gst_bpwsinc_mode_get_type ())
114 gst_bpwsinc_mode_get_type (void)
116 static GType gtype = 0;
119 static const GEnumValue values[] = {
120 {MODE_BAND_PASS, "Band pass (default)",
122 {MODE_BAND_REJECT, "Band reject",
127 gtype = g_enum_register_static ("GstBPWSincMode", values);
138 #define GST_TYPE_BPWSINC_WINDOW (gst_bpwsinc_window_get_type ())
140 gst_bpwsinc_window_get_type (void)
142 static GType gtype = 0;
145 static const GEnumValue values[] = {
146 {WINDOW_HAMMING, "Hamming window (default)",
148 {WINDOW_BLACKMAN, "Blackman window",
153 gtype = g_enum_register_static ("GstBPWSincWindow", values);
158 #define ALLOWED_CAPS \
159 "audio/x-raw-float, " \
160 " width = (int) { 32, 64 }, " \
161 " endianness = (int) BYTE_ORDER, " \
162 " rate = (int) [ 1, MAX ], " \
163 " channels = (int) [ 1, MAX ] "
165 #define DEBUG_INIT(bla) \
166 GST_DEBUG_CATEGORY_INIT (gst_bpwsinc_debug, "bpwsinc", 0, "Band-pass and Band-reject Windowed sinc filter plugin");
168 GST_BOILERPLATE_FULL (GstBPWSinc, gst_bpwsinc, GstAudioFilter,
169 GST_TYPE_AUDIO_FILTER, DEBUG_INIT);
171 static void bpwsinc_set_property (GObject * object, guint prop_id,
172 const GValue * value, GParamSpec * pspec);
173 static void bpwsinc_get_property (GObject * object, guint prop_id,
174 GValue * value, GParamSpec * pspec);
176 static GstFlowReturn bpwsinc_transform (GstBaseTransform * base,
177 GstBuffer * inbuf, GstBuffer * outbuf);
178 static gboolean bpwsinc_get_unit_size (GstBaseTransform * base, GstCaps * caps,
180 static gboolean bpwsinc_start (GstBaseTransform * base);
181 static gboolean bpwsinc_event (GstBaseTransform * base, GstEvent * event);
183 static gboolean bpwsinc_setup (GstAudioFilter * base,
184 GstRingBufferSpec * format);
186 static gboolean bpwsinc_query (GstPad * pad, GstQuery * query);
187 static const GstQueryType *bpwsinc_query_type (GstPad * pad);
192 gst_bpwsinc_dispose (GObject * object)
194 GstBPWSinc *self = GST_BPWSINC (object);
197 g_free (self->residue);
198 self->residue = NULL;
202 g_free (self->kernel);
206 G_OBJECT_CLASS (parent_class)->dispose (object);
210 gst_bpwsinc_base_init (gpointer g_class)
212 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
215 gst_element_class_set_details (element_class, &bpwsinc_details);
217 caps = gst_caps_from_string (ALLOWED_CAPS);
218 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (g_class),
220 gst_caps_unref (caps);
224 gst_bpwsinc_class_init (GstBPWSincClass * klass)
226 GObjectClass *gobject_class;
227 GstBaseTransformClass *trans_class;
228 GstAudioFilterClass *filter_class;
230 gobject_class = (GObjectClass *) klass;
231 trans_class = (GstBaseTransformClass *) klass;
232 filter_class = (GstAudioFilterClass *) klass;
234 gobject_class->set_property = bpwsinc_set_property;
235 gobject_class->get_property = bpwsinc_get_property;
236 gobject_class->dispose = gst_bpwsinc_dispose;
238 /* FIXME: Don't use the complete possible range but restrict the upper boundary
239 * so automatically generated UIs can use a slider */
240 g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
241 g_param_spec_float ("lower-frequency", "Lower Frequency",
242 "Cut-off lower frequency (Hz)", 0.0, 100000.0, 0, G_PARAM_READWRITE));
243 g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
244 g_param_spec_float ("upper-frequency", "Upper Frequency",
245 "Cut-off upper frequency (Hz)", 0.0, 100000.0, 0, G_PARAM_READWRITE));
246 g_object_class_install_property (gobject_class, PROP_LENGTH,
247 g_param_spec_int ("length", "Length",
248 "Filter kernel length, will be rounded to the next odd number",
249 3, 50000, 101, G_PARAM_READWRITE));
251 g_object_class_install_property (gobject_class, PROP_MODE,
252 g_param_spec_enum ("mode", "Mode",
253 "Band pass or band reject mode", GST_TYPE_BPWSINC_MODE,
254 MODE_BAND_PASS, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
256 g_object_class_install_property (gobject_class, PROP_WINDOW,
257 g_param_spec_enum ("window", "Window",
258 "Window function to use", GST_TYPE_BPWSINC_WINDOW,
259 WINDOW_HAMMING, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
261 trans_class->transform = GST_DEBUG_FUNCPTR (bpwsinc_transform);
262 trans_class->get_unit_size = GST_DEBUG_FUNCPTR (bpwsinc_get_unit_size);
263 trans_class->start = GST_DEBUG_FUNCPTR (bpwsinc_start);
264 trans_class->event = GST_DEBUG_FUNCPTR (bpwsinc_event);
265 filter_class->setup = GST_DEBUG_FUNCPTR (bpwsinc_setup);
269 gst_bpwsinc_init (GstBPWSinc * self, GstBPWSincClass * g_class)
271 self->kernel_length = 101;
273 self->lower_frequency = 0.0;
274 self->upper_frequency = 0.0;
275 self->mode = MODE_BAND_PASS;
276 self->window = WINDOW_HAMMING;
278 self->have_kernel = FALSE;
279 self->residue = NULL;
281 self->residue_length = 0;
282 self->next_ts = GST_CLOCK_TIME_NONE;
283 self->next_off = GST_BUFFER_OFFSET_NONE;
285 gst_pad_set_query_function (GST_BASE_TRANSFORM (self)->srcpad, bpwsinc_query);
286 gst_pad_set_query_type_function (GST_BASE_TRANSFORM (self)->srcpad,
290 #define DEFINE_PROCESS_FUNC(width,ctype) \
292 process_##width (GstBPWSinc * self, g##ctype * src, g##ctype * dst, guint input_samples) \
294 gint kernel_length = self->kernel_length; \
296 gint channels = GST_AUDIO_FILTER (self)->format.channels; \
300 for (i = 0; i < input_samples; i++) { \
304 for (j = 0; j < kernel_length; j++) \
307 self->residue[(kernel_length + l - j) * channels + \
308 k] * self->kernel[j]; \
310 dst[i] += src[(l - j) * channels + k] * self->kernel[j]; \
313 /* copy the tail of the current input buffer to the residue, while \
314 * keeping parts of the residue if the input buffer is smaller than \
315 * the kernel length */ \
316 if (input_samples < kernel_length * channels) \
317 res_start = kernel_length * channels - input_samples; \
321 for (i = 0; i < res_start; i++) \
322 self->residue[i] = self->residue[i + input_samples]; \
323 for (i = res_start; i < kernel_length * channels; i++) \
324 self->residue[i] = src[input_samples - kernel_length * channels + i]; \
326 self->residue_length += kernel_length * channels - res_start; \
327 if (self->residue_length > kernel_length * channels) \
328 self->residue_length = kernel_length * channels; \
331 DEFINE_PROCESS_FUNC (32, float);
332 DEFINE_PROCESS_FUNC (64, double);
334 #undef DEFINE_PROCESS_FUNC
337 bpwsinc_build_kernel (GstBPWSinc * self)
342 gdouble *kernel_lp, *kernel_hp;
345 len = self->kernel_length;
347 if (GST_AUDIO_FILTER (self)->format.rate == 0) {
348 GST_DEBUG ("rate not set yet");
352 if (GST_AUDIO_FILTER (self)->format.channels == 0) {
353 GST_DEBUG ("channels not set yet");
357 /* Clamp frequencies */
358 self->lower_frequency =
359 CLAMP (self->lower_frequency, 0.0,
360 GST_AUDIO_FILTER (self)->format.rate / 2);
361 self->upper_frequency =
362 CLAMP (self->upper_frequency, 0.0,
363 GST_AUDIO_FILTER (self)->format.rate / 2);
364 if (self->lower_frequency > self->upper_frequency) {
365 gint tmp = self->lower_frequency;
367 self->lower_frequency = self->upper_frequency;
368 self->upper_frequency = tmp;
371 GST_DEBUG ("bpwsinc: initializing filter kernel of length %d "
372 "with lower frequency %.2lf Hz "
373 ", upper frequency %.2lf Hz for mode %s",
374 len, self->lower_frequency, self->upper_frequency,
375 (self->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject");
377 /* fill the lp kernel */
378 w = 2 * M_PI * (self->lower_frequency / GST_AUDIO_FILTER (self)->format.rate);
379 kernel_lp = g_new (gdouble, len);
380 for (i = 0; i < len; ++i) {
384 kernel_lp[i] = sin (w * (i - len / 2))
387 if (self->window == WINDOW_HAMMING)
388 kernel_lp[i] *= (0.54 - 0.46 * cos (2 * M_PI * i / len));
391 (0.42 - 0.5 * cos (2 * M_PI * i / len) +
392 0.08 * cos (4 * M_PI * i / len));
395 /* normalize for unity gain at DC */
397 for (i = 0; i < len; ++i)
399 for (i = 0; i < len; ++i)
402 /* fill the hp kernel */
403 w = 2 * M_PI * (self->upper_frequency / GST_AUDIO_FILTER (self)->format.rate);
404 kernel_hp = g_new (gdouble, len);
405 for (i = 0; i < len; ++i) {
409 kernel_hp[i] = sin (w * (i - len / 2))
412 if (self->window == WINDOW_HAMMING)
413 kernel_hp[i] *= (0.54 - 0.46 * cos (2 * M_PI * i / len));
416 (0.42 - 0.5 * cos (2 * M_PI * i / len) +
417 0.08 * cos (4 * M_PI * i / len));
420 /* normalize for unity gain at DC */
422 for (i = 0; i < len; ++i)
424 for (i = 0; i < len; ++i)
427 /* do spectral inversion to go from lowpass to highpass */
428 for (i = 0; i < len; ++i)
429 kernel_hp[i] = -kernel_hp[i];
430 kernel_hp[len / 2] += 1;
432 /* combine the two kernels */
434 g_free (self->kernel);
435 self->kernel = g_new (gdouble, len);
437 for (i = 0; i < len; ++i)
438 self->kernel[i] = kernel_lp[i] + kernel_hp[i];
440 /* free the helper kernels */
444 /* do spectral inversion to go from bandreject to bandpass
446 if (self->mode == MODE_BAND_PASS) {
447 for (i = 0; i < len; ++i)
448 self->kernel[i] = -self->kernel[i];
449 self->kernel[len / 2] += 1;
452 /* set up the residue memory space */
453 if (!self->residue) {
455 g_new0 (gdouble, len * GST_AUDIO_FILTER (self)->format.channels);
456 self->residue_length = 0;
459 self->have_kernel = TRUE;
463 bpwsinc_push_residue (GstBPWSinc * self)
467 gint rate = GST_AUDIO_FILTER (self)->format.rate;
468 gint channels = GST_AUDIO_FILTER (self)->format.channels;
469 gint outsize, outsamples;
470 gint diffsize, diffsamples;
473 /* Calculate the number of samples and their memory size that
474 * should be pushed from the residue */
475 outsamples = MIN (self->latency, self->residue_length / channels);
476 outsize = outsamples * channels * (GST_AUDIO_FILTER (self)->format.width / 8);
480 /* Process the difference between latency and residue_length samples
481 * to start at the actual data instead of starting at the zeros before
482 * when we only got one buffer smaller than latency */
483 diffsamples = self->latency - self->residue_length / channels;
485 diffsamples * channels * (GST_AUDIO_FILTER (self)->format.width / 8);
487 in = g_new0 (guint8, diffsize);
488 out = g_new0 (guint8, diffsize);
489 self->process (self, in, out, diffsamples * channels);
494 res = gst_pad_alloc_buffer (GST_BASE_TRANSFORM (self)->srcpad,
495 GST_BUFFER_OFFSET_NONE, outsize,
496 GST_PAD_CAPS (GST_BASE_TRANSFORM (self)->srcpad), &outbuf);
498 if (G_UNLIKELY (res != GST_FLOW_OK)) {
499 GST_WARNING_OBJECT (self, "failed allocating buffer of %d bytes", outsize);
503 /* Convolve the residue with zeros to get the actual remaining data */
504 in = g_new0 (guint8, outsize);
505 self->process (self, in, GST_BUFFER_DATA (outbuf), outsamples * channels);
508 /* Set timestamp, offset, etc from the values we
509 * saved when processing the regular buffers */
510 if (GST_CLOCK_TIME_IS_VALID (self->next_ts))
511 GST_BUFFER_TIMESTAMP (outbuf) = self->next_ts;
513 GST_BUFFER_TIMESTAMP (outbuf) = 0;
514 GST_BUFFER_DURATION (outbuf) =
515 gst_util_uint64_scale (outsamples, GST_SECOND, rate);
516 self->next_ts += gst_util_uint64_scale (outsamples, GST_SECOND, rate);
518 if (self->next_off != GST_BUFFER_OFFSET_NONE) {
519 GST_BUFFER_OFFSET (outbuf) = self->next_off;
520 GST_BUFFER_OFFSET_END (outbuf) = self->next_off + outsamples;
523 GST_DEBUG_OBJECT (self, "Pushing residue buffer of size %d with timestamp: %"
524 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %lld,"
525 " offset_end: %lld, nsamples: %d", GST_BUFFER_SIZE (outbuf),
526 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
527 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
528 GST_BUFFER_OFFSET_END (outbuf), outsamples);
530 res = gst_pad_push (GST_BASE_TRANSFORM (self)->srcpad, outbuf);
532 if (G_UNLIKELY (res != GST_FLOW_OK)) {
533 GST_WARNING_OBJECT (self, "failed to push residue");
538 /* GstAudioFilter vmethod implementations */
540 /* get notified of caps and plug in the correct process function */
542 bpwsinc_setup (GstAudioFilter * base, GstRingBufferSpec * format)
544 GstBPWSinc *self = GST_BPWSINC (base);
548 if (format->width == 32)
549 self->process = (GstBPWSincProcessFunc) process_32;
550 else if (format->width == 64)
551 self->process = (GstBPWSincProcessFunc) process_64;
555 self->have_kernel = FALSE;
560 /* GstBaseTransform vmethod implementations */
563 bpwsinc_get_unit_size (GstBaseTransform * base, GstCaps * caps, guint * size)
565 gint width, channels;
566 GstStructure *structure;
571 structure = gst_caps_get_structure (caps, 0);
572 ret = gst_structure_get_int (structure, "width", &width);
573 ret &= gst_structure_get_int (structure, "channels", &channels);
575 *size = width * channels / 8;
581 bpwsinc_transform (GstBaseTransform * base, GstBuffer * inbuf,
584 GstBPWSinc *self = GST_BPWSINC (base);
585 GstClockTime timestamp;
586 gint channels = GST_AUDIO_FILTER (self)->format.channels;
587 gint rate = GST_AUDIO_FILTER (self)->format.rate;
589 GST_BUFFER_SIZE (outbuf) / (GST_AUDIO_FILTER (self)->format.width / 8);
590 gint output_samples = input_samples;
593 /* don't process data in passthrough-mode */
594 if (gst_base_transform_is_passthrough (base))
597 /* FIXME: subdivide GST_BUFFER_SIZE into small chunks for smooth fades */
598 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
600 if (GST_CLOCK_TIME_IS_VALID (timestamp))
601 gst_object_sync_values (G_OBJECT (self), timestamp);
603 if (!self->have_kernel)
604 bpwsinc_build_kernel (self);
606 /* Reset the residue if already existing on discont buffers */
607 if (GST_BUFFER_IS_DISCONT (inbuf)) {
608 if (channels && self->residue)
609 memset (self->residue, 0, channels *
610 self->kernel_length * sizeof (gdouble));
611 self->residue_length = 0;
612 self->next_ts = GST_CLOCK_TIME_NONE;
613 self->next_off = GST_BUFFER_OFFSET_NONE;
616 /* Calculate the number of samples we can push out now without outputting
617 * kernel_length/2 zeros in the beginning */
618 diff = (self->kernel_length / 2) * channels - self->residue_length;
620 output_samples -= diff;
622 self->process (self, GST_BUFFER_DATA (inbuf), GST_BUFFER_DATA (outbuf),
625 if (output_samples <= 0) {
626 /* Drop buffer and save original timestamp/offset for later use */
627 if (!GST_CLOCK_TIME_IS_VALID (self->next_ts)
628 && GST_BUFFER_TIMESTAMP_IS_VALID (outbuf))
629 self->next_ts = GST_BUFFER_TIMESTAMP (outbuf);
630 if (self->next_off == GST_BUFFER_OFFSET_NONE
631 && GST_BUFFER_OFFSET_IS_VALID (outbuf))
632 self->next_off = GST_BUFFER_OFFSET (outbuf);
633 return GST_BASE_TRANSFORM_FLOW_DROPPED;
634 } else if (output_samples < input_samples) {
635 /* First (probably partial) buffer after starting from
636 * a clean residue. Use stored timestamp/offset here */
637 if (GST_CLOCK_TIME_IS_VALID (self->next_ts))
638 GST_BUFFER_TIMESTAMP (outbuf) = self->next_ts;
640 if (self->next_off != GST_BUFFER_OFFSET_NONE) {
641 GST_BUFFER_OFFSET (outbuf) = self->next_off;
642 if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf))
643 GST_BUFFER_OFFSET_END (outbuf) =
644 self->next_off + output_samples / channels;
646 /* We dropped no buffer, offset is valid, offset_end must be adjusted by diff */
647 if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf))
648 GST_BUFFER_OFFSET_END (outbuf) -= diff / channels;
651 if (GST_BUFFER_DURATION_IS_VALID (outbuf))
652 GST_BUFFER_DURATION (outbuf) -=
653 gst_util_uint64_scale (diff, GST_SECOND, channels * rate);
655 GST_BUFFER_DATA (outbuf) +=
656 diff * (GST_AUDIO_FILTER (self)->format.width / 8);
657 GST_BUFFER_SIZE (outbuf) -=
658 diff * (GST_AUDIO_FILTER (self)->format.width / 8);
660 GstClockTime ts_latency =
661 gst_util_uint64_scale (self->latency, GST_SECOND, rate);
663 /* Normal buffer, adjust timestamp/offset/etc by latency */
664 if (GST_BUFFER_TIMESTAMP (outbuf) < ts_latency) {
665 GST_WARNING_OBJECT (self, "GST_BUFFER_TIMESTAMP (outbuf) < latency");
666 GST_BUFFER_TIMESTAMP (outbuf) = 0;
668 GST_BUFFER_TIMESTAMP (outbuf) -= ts_latency;
671 if (GST_BUFFER_OFFSET_IS_VALID (outbuf)) {
672 if (GST_BUFFER_OFFSET (outbuf) > self->latency) {
673 GST_BUFFER_OFFSET (outbuf) -= self->latency;
675 GST_WARNING_OBJECT (self, "GST_BUFFER_OFFSET (outbuf) < latency");
676 GST_BUFFER_OFFSET (outbuf) = 0;
680 if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf)) {
681 if (GST_BUFFER_OFFSET_END (outbuf) > self->latency) {
682 GST_BUFFER_OFFSET_END (outbuf) -= self->latency;
684 GST_WARNING_OBJECT (self, "GST_BUFFER_OFFSET_END (outbuf) < latency");
685 GST_BUFFER_OFFSET_END (outbuf) = 0;
690 GST_DEBUG_OBJECT (self, "Pushing buffer of size %d with timestamp: %"
691 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %lld,"
692 " offset_end: %lld, nsamples: %d", GST_BUFFER_SIZE (outbuf),
693 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
694 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
695 GST_BUFFER_OFFSET_END (outbuf), output_samples / channels);
697 self->next_ts = GST_BUFFER_TIMESTAMP (outbuf) + GST_BUFFER_DURATION (outbuf);
698 self->next_off = GST_BUFFER_OFFSET_END (outbuf);
704 bpwsinc_start (GstBaseTransform * base)
706 GstBPWSinc *self = GST_BPWSINC (base);
707 gint channels = GST_AUDIO_FILTER (self)->format.channels;
709 /* Reset the residue if already existing */
710 if (channels && self->residue)
711 memset (self->residue, 0, channels *
712 self->kernel_length * sizeof (gdouble));
714 self->residue_length = 0;
715 self->next_ts = GST_CLOCK_TIME_NONE;
716 self->next_off = GST_BUFFER_OFFSET_NONE;
722 bpwsinc_query (GstPad * pad, GstQuery * query)
724 GstBPWSinc *self = GST_BPWSINC (gst_pad_get_parent (pad));
727 switch (GST_QUERY_TYPE (query)) {
728 case GST_QUERY_LATENCY:
730 GstClockTime min, max;
734 gint rate = GST_AUDIO_FILTER (self)->format.rate;
736 if ((peer = gst_pad_get_peer (GST_BASE_TRANSFORM (self)->sinkpad))) {
737 if ((res = gst_pad_query (peer, query))) {
738 gst_query_parse_latency (query, &live, &min, &max);
740 GST_DEBUG_OBJECT (self, "Peer latency: min %"
741 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
742 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
744 /* add our own latency */
746 (rate != 0) ? gst_util_uint64_scale (self->latency, GST_SECOND,
749 GST_DEBUG_OBJECT (self, "Our latency: %"
750 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
753 if (max != GST_CLOCK_TIME_NONE)
756 GST_DEBUG_OBJECT (self, "Calculated total latency : min %"
757 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
758 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
760 gst_query_set_latency (query, live, min, max);
762 gst_object_unref (peer);
767 res = gst_pad_query_default (pad, query);
770 gst_object_unref (self);
774 static const GstQueryType *
775 bpwsinc_query_type (GstPad * pad)
777 static const GstQueryType types[] = {
786 bpwsinc_event (GstBaseTransform * base, GstEvent * event)
788 GstBPWSinc *self = GST_BPWSINC (base);
790 switch (GST_EVENT_TYPE (event)) {
792 bpwsinc_push_residue (self);
798 return GST_BASE_TRANSFORM_CLASS (parent_class)->event (base, event);
802 bpwsinc_set_property (GObject * object, guint prop_id, const GValue * value,
805 GstBPWSinc *self = GST_BPWSINC (object);
807 g_return_if_fail (GST_IS_BPWSINC (self));
813 GST_BASE_TRANSFORM_LOCK (self);
814 val = g_value_get_int (value);
818 if (val != self->kernel_length) {
820 bpwsinc_push_residue (self);
821 g_free (self->residue);
822 self->residue = NULL;
824 self->kernel_length = val;
825 self->latency = val / 2;
826 bpwsinc_build_kernel (self);
828 GST_BASE_TRANSFORM_UNLOCK (self);
831 case PROP_LOWER_FREQUENCY:
832 GST_BASE_TRANSFORM_LOCK (self);
833 self->lower_frequency = g_value_get_float (value);
834 bpwsinc_build_kernel (self);
835 GST_BASE_TRANSFORM_UNLOCK (self);
837 case PROP_UPPER_FREQUENCY:
838 GST_BASE_TRANSFORM_LOCK (self);
839 self->upper_frequency = g_value_get_float (value);
840 bpwsinc_build_kernel (self);
841 GST_BASE_TRANSFORM_UNLOCK (self);
844 GST_BASE_TRANSFORM_LOCK (self);
845 self->mode = g_value_get_enum (value);
846 bpwsinc_build_kernel (self);
847 GST_BASE_TRANSFORM_UNLOCK (self);
850 GST_BASE_TRANSFORM_LOCK (self);
851 self->window = g_value_get_enum (value);
852 bpwsinc_build_kernel (self);
853 GST_BASE_TRANSFORM_UNLOCK (self);
856 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
862 bpwsinc_get_property (GObject * object, guint prop_id, GValue * value,
865 GstBPWSinc *self = GST_BPWSINC (object);
869 g_value_set_int (value, self->kernel_length);
871 case PROP_LOWER_FREQUENCY:
872 g_value_set_float (value, self->lower_frequency);
874 case PROP_UPPER_FREQUENCY:
875 g_value_set_float (value, self->upper_frequency);
878 g_value_set_enum (value, self->mode);
881 g_value_set_enum (value, self->window);
884 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);