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-audiowsincband
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 ! audiosincband 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 ! audiowsincband mode=band-reject lower-frequency=59 upper-frequency=61 length=10001 window=hamming ! audioconvert ! alsasink
59 * gst-launch audiotestsrc wave=white-noise ! audioconvert ! audiowsincband 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 "audiowsincband.h"
77 #define GST_CAT_DEFAULT gst_audio_wsincband_debug
78 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
80 static const GstElementDetails audio_wsincband_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_AUDIO_WSINC_BAND_MODE (gst_audio_wsincband_mode_get_type ())
114 gst_audio_wsincband_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 ("GstAudioWSincBandMode", values);
138 #define GST_TYPE_AUDIO_WSINC_BAND_WINDOW (gst_audio_wsincband_window_get_type ())
140 gst_audio_wsincband_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 ("GstAudioWSincBandWindow", 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_audio_wsincband_debug, "audiowsincband", 0, \
167 "Band-pass and Band-reject Windowed sinc filter plugin");
169 GST_BOILERPLATE_FULL (GstAudioWSincBand, audio_wsincband, GstAudioFilter,
170 GST_TYPE_AUDIO_FILTER, DEBUG_INIT);
172 static void audio_wsincband_set_property (GObject * object, guint prop_id,
173 const GValue * value, GParamSpec * pspec);
174 static void audio_wsincband_get_property (GObject * object, guint prop_id,
175 GValue * value, GParamSpec * pspec);
177 static GstFlowReturn audio_wsincband_transform (GstBaseTransform * base,
178 GstBuffer * inbuf, GstBuffer * outbuf);
179 static gboolean audio_wsincband_start (GstBaseTransform * base);
180 static gboolean audio_wsincband_event (GstBaseTransform * base,
183 static gboolean audio_wsincband_setup (GstAudioFilter * base,
184 GstRingBufferSpec * format);
186 static gboolean audio_wsincband_query (GstPad * pad, GstQuery * query);
187 static const GstQueryType *audio_wsincband_query_type (GstPad * pad);
192 audio_wsincband_dispose (GObject * object)
194 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
197 g_free (self->residue);
198 self->residue = NULL;
202 g_free (self->kernel);
206 G_OBJECT_CLASS (parent_class)->dispose (object);
210 audio_wsincband_base_init (gpointer g_class)
212 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
215 gst_element_class_set_details (element_class, &audio_wsincband_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 audio_wsincband_class_init (GstAudioWSincBandClass * 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 = audio_wsincband_set_property;
235 gobject_class->get_property = audio_wsincband_get_property;
236 gobject_class->dispose = audio_wsincband_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_AUDIO_WSINC_BAND_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_AUDIO_WSINC_BAND_WINDOW,
259 WINDOW_HAMMING, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
261 trans_class->transform = GST_DEBUG_FUNCPTR (audio_wsincband_transform);
262 trans_class->start = GST_DEBUG_FUNCPTR (audio_wsincband_start);
263 trans_class->event = GST_DEBUG_FUNCPTR (audio_wsincband_event);
264 filter_class->setup = GST_DEBUG_FUNCPTR (audio_wsincband_setup);
268 audio_wsincband_init (GstAudioWSincBand * self,
269 GstAudioWSincBandClass * 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,
286 audio_wsincband_query);
287 gst_pad_set_query_type_function (GST_BASE_TRANSFORM (self)->srcpad,
288 audio_wsincband_query_type);
291 #define DEFINE_PROCESS_FUNC(width,ctype) \
293 process_##width (GstAudioWSincBand * self, g##ctype * src, g##ctype * dst, guint input_samples) \
295 gint kernel_length = self->kernel_length; \
297 gint channels = GST_AUDIO_FILTER (self)->format.channels; \
301 for (i = 0; i < input_samples; i++) { \
305 for (j = 0; j < kernel_length; j++) \
308 self->residue[(kernel_length + l - j) * channels + \
309 k] * self->kernel[j]; \
311 dst[i] += src[(l - j) * channels + k] * self->kernel[j]; \
314 /* copy the tail of the current input buffer to the residue, while \
315 * keeping parts of the residue if the input buffer is smaller than \
316 * the kernel length */ \
317 if (input_samples < kernel_length * channels) \
318 res_start = kernel_length * channels - input_samples; \
322 for (i = 0; i < res_start; i++) \
323 self->residue[i] = self->residue[i + input_samples]; \
324 for (i = res_start; i < kernel_length * channels; i++) \
325 self->residue[i] = src[input_samples - kernel_length * channels + i]; \
327 self->residue_length += kernel_length * channels - res_start; \
328 if (self->residue_length > kernel_length * channels) \
329 self->residue_length = kernel_length * channels; \
332 DEFINE_PROCESS_FUNC (32, float);
333 DEFINE_PROCESS_FUNC (64, double);
335 #undef DEFINE_PROCESS_FUNC
338 audio_wsincband_build_kernel (GstAudioWSincBand * self)
343 gdouble *kernel_lp, *kernel_hp;
346 len = self->kernel_length;
348 if (GST_AUDIO_FILTER (self)->format.rate == 0) {
349 GST_DEBUG ("rate not set yet");
353 if (GST_AUDIO_FILTER (self)->format.channels == 0) {
354 GST_DEBUG ("channels not set yet");
358 /* Clamp frequencies */
359 self->lower_frequency =
360 CLAMP (self->lower_frequency, 0.0,
361 GST_AUDIO_FILTER (self)->format.rate / 2);
362 self->upper_frequency =
363 CLAMP (self->upper_frequency, 0.0,
364 GST_AUDIO_FILTER (self)->format.rate / 2);
365 if (self->lower_frequency > self->upper_frequency) {
366 gint tmp = self->lower_frequency;
368 self->lower_frequency = self->upper_frequency;
369 self->upper_frequency = tmp;
372 GST_DEBUG ("audio_wsincband: initializing filter kernel of length %d "
373 "with lower frequency %.2lf Hz "
374 ", upper frequency %.2lf Hz for mode %s",
375 len, self->lower_frequency, self->upper_frequency,
376 (self->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject");
378 /* fill the lp kernel */
379 w = 2 * M_PI * (self->lower_frequency / GST_AUDIO_FILTER (self)->format.rate);
380 kernel_lp = g_new (gdouble, len);
381 for (i = 0; i < len; ++i) {
385 kernel_lp[i] = sin (w * (i - len / 2))
388 if (self->window == WINDOW_HAMMING)
389 kernel_lp[i] *= (0.54 - 0.46 * cos (2 * M_PI * i / len));
392 (0.42 - 0.5 * cos (2 * M_PI * i / len) +
393 0.08 * cos (4 * M_PI * i / len));
396 /* normalize for unity gain at DC */
398 for (i = 0; i < len; ++i)
400 for (i = 0; i < len; ++i)
403 /* fill the hp kernel */
404 w = 2 * M_PI * (self->upper_frequency / GST_AUDIO_FILTER (self)->format.rate);
405 kernel_hp = g_new (gdouble, len);
406 for (i = 0; i < len; ++i) {
410 kernel_hp[i] = sin (w * (i - len / 2))
413 if (self->window == WINDOW_HAMMING)
414 kernel_hp[i] *= (0.54 - 0.46 * cos (2 * M_PI * i / len));
417 (0.42 - 0.5 * cos (2 * M_PI * i / len) +
418 0.08 * cos (4 * M_PI * i / len));
421 /* normalize for unity gain at DC */
423 for (i = 0; i < len; ++i)
425 for (i = 0; i < len; ++i)
428 /* do spectral inversion to go from lowpass to highpass */
429 for (i = 0; i < len; ++i)
430 kernel_hp[i] = -kernel_hp[i];
431 kernel_hp[len / 2] += 1;
433 /* combine the two kernels */
435 g_free (self->kernel);
436 self->kernel = g_new (gdouble, len);
438 for (i = 0; i < len; ++i)
439 self->kernel[i] = kernel_lp[i] + kernel_hp[i];
441 /* free the helper kernels */
445 /* do spectral inversion to go from bandreject to bandpass
447 if (self->mode == MODE_BAND_PASS) {
448 for (i = 0; i < len; ++i)
449 self->kernel[i] = -self->kernel[i];
450 self->kernel[len / 2] += 1;
453 /* set up the residue memory space */
454 if (!self->residue) {
456 g_new0 (gdouble, len * GST_AUDIO_FILTER (self)->format.channels);
457 self->residue_length = 0;
460 self->have_kernel = TRUE;
464 audio_wsincband_push_residue (GstAudioWSincBand * self)
468 gint rate = GST_AUDIO_FILTER (self)->format.rate;
469 gint channels = GST_AUDIO_FILTER (self)->format.channels;
470 gint outsize, outsamples;
471 gint diffsize, diffsamples;
474 /* Calculate the number of samples and their memory size that
475 * should be pushed from the residue */
476 outsamples = MIN (self->latency, self->residue_length / channels);
477 outsize = outsamples * channels * (GST_AUDIO_FILTER (self)->format.width / 8);
481 /* Process the difference between latency and residue_length samples
482 * to start at the actual data instead of starting at the zeros before
483 * when we only got one buffer smaller than latency */
484 diffsamples = self->latency - self->residue_length / channels;
486 diffsamples * channels * (GST_AUDIO_FILTER (self)->format.width / 8);
488 in = g_new0 (guint8, diffsize);
489 out = g_new0 (guint8, diffsize);
490 self->process (self, in, out, diffsamples * channels);
495 res = gst_pad_alloc_buffer (GST_BASE_TRANSFORM (self)->srcpad,
496 GST_BUFFER_OFFSET_NONE, outsize,
497 GST_PAD_CAPS (GST_BASE_TRANSFORM (self)->srcpad), &outbuf);
499 if (G_UNLIKELY (res != GST_FLOW_OK)) {
500 GST_WARNING_OBJECT (self, "failed allocating buffer of %d bytes", outsize);
504 /* Convolve the residue with zeros to get the actual remaining data */
505 in = g_new0 (guint8, outsize);
506 self->process (self, in, GST_BUFFER_DATA (outbuf), outsamples * channels);
509 /* Set timestamp, offset, etc from the values we
510 * saved when processing the regular buffers */
511 if (GST_CLOCK_TIME_IS_VALID (self->next_ts))
512 GST_BUFFER_TIMESTAMP (outbuf) = self->next_ts;
514 GST_BUFFER_TIMESTAMP (outbuf) = 0;
515 GST_BUFFER_DURATION (outbuf) =
516 gst_util_uint64_scale (outsamples, GST_SECOND, rate);
517 self->next_ts += gst_util_uint64_scale (outsamples, GST_SECOND, rate);
519 if (self->next_off != GST_BUFFER_OFFSET_NONE) {
520 GST_BUFFER_OFFSET (outbuf) = self->next_off;
521 GST_BUFFER_OFFSET_END (outbuf) = self->next_off + outsamples;
524 GST_DEBUG_OBJECT (self, "Pushing residue buffer of size %d with timestamp: %"
525 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %lld,"
526 " offset_end: %lld, nsamples: %d", GST_BUFFER_SIZE (outbuf),
527 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
528 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
529 GST_BUFFER_OFFSET_END (outbuf), outsamples);
531 res = gst_pad_push (GST_BASE_TRANSFORM (self)->srcpad, outbuf);
533 if (G_UNLIKELY (res != GST_FLOW_OK)) {
534 GST_WARNING_OBJECT (self, "failed to push residue");
539 /* GstAudioFilter vmethod implementations */
541 /* get notified of caps and plug in the correct process function */
543 audio_wsincband_setup (GstAudioFilter * base, GstRingBufferSpec * format)
545 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
549 if (format->width == 32)
550 self->process = (GstAudioWSincBandProcessFunc) process_32;
551 else if (format->width == 64)
552 self->process = (GstAudioWSincBandProcessFunc) process_64;
556 self->have_kernel = FALSE;
561 /* GstBaseTransform vmethod implementations */
564 audio_wsincband_transform (GstBaseTransform * base, GstBuffer * inbuf,
567 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
568 GstClockTime timestamp;
569 gint channels = GST_AUDIO_FILTER (self)->format.channels;
570 gint rate = GST_AUDIO_FILTER (self)->format.rate;
572 GST_BUFFER_SIZE (outbuf) / (GST_AUDIO_FILTER (self)->format.width / 8);
573 gint output_samples = input_samples;
576 /* FIXME: subdivide GST_BUFFER_SIZE into small chunks for smooth fades */
577 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
578 if (GST_CLOCK_TIME_IS_VALID (timestamp))
579 gst_object_sync_values (G_OBJECT (self), timestamp);
581 if (!self->have_kernel)
582 audio_wsincband_build_kernel (self);
584 /* Reset the residue if already existing on discont buffers */
585 if (GST_BUFFER_IS_DISCONT (inbuf)) {
586 if (channels && self->residue)
587 memset (self->residue, 0, channels *
588 self->kernel_length * sizeof (gdouble));
589 self->residue_length = 0;
590 self->next_ts = GST_CLOCK_TIME_NONE;
591 self->next_off = GST_BUFFER_OFFSET_NONE;
594 /* Calculate the number of samples we can push out now without outputting
595 * kernel_length/2 zeros in the beginning */
596 diff = (self->kernel_length / 2) * channels - self->residue_length;
598 output_samples -= diff;
600 self->process (self, GST_BUFFER_DATA (inbuf), GST_BUFFER_DATA (outbuf),
603 if (output_samples <= 0) {
604 /* Drop buffer and save original timestamp/offset for later use */
605 if (!GST_CLOCK_TIME_IS_VALID (self->next_ts)
606 && GST_BUFFER_TIMESTAMP_IS_VALID (outbuf))
607 self->next_ts = GST_BUFFER_TIMESTAMP (outbuf);
608 if (self->next_off == GST_BUFFER_OFFSET_NONE
609 && GST_BUFFER_OFFSET_IS_VALID (outbuf))
610 self->next_off = GST_BUFFER_OFFSET (outbuf);
611 return GST_BASE_TRANSFORM_FLOW_DROPPED;
612 } else if (output_samples < input_samples) {
613 /* First (probably partial) buffer after starting from
614 * a clean residue. Use stored timestamp/offset here */
615 if (GST_CLOCK_TIME_IS_VALID (self->next_ts))
616 GST_BUFFER_TIMESTAMP (outbuf) = self->next_ts;
618 if (self->next_off != GST_BUFFER_OFFSET_NONE) {
619 GST_BUFFER_OFFSET (outbuf) = self->next_off;
620 if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf))
621 GST_BUFFER_OFFSET_END (outbuf) =
622 self->next_off + output_samples / channels;
624 /* We dropped no buffer, offset is valid, offset_end must be adjusted by diff */
625 if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf))
626 GST_BUFFER_OFFSET_END (outbuf) -= diff / channels;
629 if (GST_BUFFER_DURATION_IS_VALID (outbuf))
630 GST_BUFFER_DURATION (outbuf) -=
631 gst_util_uint64_scale (diff, GST_SECOND, channels * rate);
633 GST_BUFFER_DATA (outbuf) +=
634 diff * (GST_AUDIO_FILTER (self)->format.width / 8);
635 GST_BUFFER_SIZE (outbuf) -=
636 diff * (GST_AUDIO_FILTER (self)->format.width / 8);
638 GstClockTime ts_latency =
639 gst_util_uint64_scale (self->latency, GST_SECOND, rate);
641 /* Normal buffer, adjust timestamp/offset/etc by latency */
642 if (GST_BUFFER_TIMESTAMP (outbuf) < ts_latency) {
643 GST_WARNING_OBJECT (self, "GST_BUFFER_TIMESTAMP (outbuf) < latency");
644 GST_BUFFER_TIMESTAMP (outbuf) = 0;
646 GST_BUFFER_TIMESTAMP (outbuf) -= ts_latency;
649 if (GST_BUFFER_OFFSET_IS_VALID (outbuf)) {
650 if (GST_BUFFER_OFFSET (outbuf) > self->latency) {
651 GST_BUFFER_OFFSET (outbuf) -= self->latency;
653 GST_WARNING_OBJECT (self, "GST_BUFFER_OFFSET (outbuf) < latency");
654 GST_BUFFER_OFFSET (outbuf) = 0;
658 if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf)) {
659 if (GST_BUFFER_OFFSET_END (outbuf) > self->latency) {
660 GST_BUFFER_OFFSET_END (outbuf) -= self->latency;
662 GST_WARNING_OBJECT (self, "GST_BUFFER_OFFSET_END (outbuf) < latency");
663 GST_BUFFER_OFFSET_END (outbuf) = 0;
668 GST_DEBUG_OBJECT (self, "Pushing buffer of size %d with timestamp: %"
669 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %lld,"
670 " offset_end: %lld, nsamples: %d", GST_BUFFER_SIZE (outbuf),
671 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
672 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
673 GST_BUFFER_OFFSET_END (outbuf), output_samples / channels);
675 self->next_ts = GST_BUFFER_TIMESTAMP (outbuf) + GST_BUFFER_DURATION (outbuf);
676 self->next_off = GST_BUFFER_OFFSET_END (outbuf);
682 audio_wsincband_start (GstBaseTransform * base)
684 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
685 gint channels = GST_AUDIO_FILTER (self)->format.channels;
687 /* Reset the residue if already existing */
688 if (channels && self->residue)
689 memset (self->residue, 0, channels *
690 self->kernel_length * sizeof (gdouble));
692 self->residue_length = 0;
693 self->next_ts = GST_CLOCK_TIME_NONE;
694 self->next_off = GST_BUFFER_OFFSET_NONE;
700 audio_wsincband_query (GstPad * pad, GstQuery * query)
702 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (gst_pad_get_parent (pad));
705 switch (GST_QUERY_TYPE (query)) {
706 case GST_QUERY_LATENCY:
708 GstClockTime min, max;
712 gint rate = GST_AUDIO_FILTER (self)->format.rate;
714 if ((peer = gst_pad_get_peer (GST_BASE_TRANSFORM (self)->sinkpad))) {
715 if ((res = gst_pad_query (peer, query))) {
716 gst_query_parse_latency (query, &live, &min, &max);
718 GST_DEBUG_OBJECT (self, "Peer latency: min %"
719 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
720 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
722 /* add our own latency */
724 (rate != 0) ? gst_util_uint64_scale (self->latency, GST_SECOND,
727 GST_DEBUG_OBJECT (self, "Our latency: %"
728 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
731 if (max != GST_CLOCK_TIME_NONE)
734 GST_DEBUG_OBJECT (self, "Calculated total latency : min %"
735 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
736 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
738 gst_query_set_latency (query, live, min, max);
740 gst_object_unref (peer);
745 res = gst_pad_query_default (pad, query);
748 gst_object_unref (self);
752 static const GstQueryType *
753 audio_wsincband_query_type (GstPad * pad)
755 static const GstQueryType types[] = {
764 audio_wsincband_event (GstBaseTransform * base, GstEvent * event)
766 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (base);
768 switch (GST_EVENT_TYPE (event)) {
770 audio_wsincband_push_residue (self);
776 return GST_BASE_TRANSFORM_CLASS (parent_class)->event (base, event);
780 audio_wsincband_set_property (GObject * object, guint prop_id,
781 const GValue * value, GParamSpec * pspec)
783 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
785 g_return_if_fail (GST_IS_AUDIO_WSINC_BAND (self));
791 GST_BASE_TRANSFORM_LOCK (self);
792 val = g_value_get_int (value);
796 if (val != self->kernel_length) {
798 audio_wsincband_push_residue (self);
799 g_free (self->residue);
800 self->residue = NULL;
802 self->kernel_length = val;
803 self->latency = val / 2;
804 audio_wsincband_build_kernel (self);
805 gst_element_post_message (GST_ELEMENT (self),
806 gst_message_new_latency (GST_OBJECT (self)));
808 GST_BASE_TRANSFORM_UNLOCK (self);
811 case PROP_LOWER_FREQUENCY:
812 GST_BASE_TRANSFORM_LOCK (self);
813 self->lower_frequency = g_value_get_float (value);
814 audio_wsincband_build_kernel (self);
815 GST_BASE_TRANSFORM_UNLOCK (self);
817 case PROP_UPPER_FREQUENCY:
818 GST_BASE_TRANSFORM_LOCK (self);
819 self->upper_frequency = g_value_get_float (value);
820 audio_wsincband_build_kernel (self);
821 GST_BASE_TRANSFORM_UNLOCK (self);
824 GST_BASE_TRANSFORM_LOCK (self);
825 self->mode = g_value_get_enum (value);
826 audio_wsincband_build_kernel (self);
827 GST_BASE_TRANSFORM_UNLOCK (self);
830 GST_BASE_TRANSFORM_LOCK (self);
831 self->window = g_value_get_enum (value);
832 audio_wsincband_build_kernel (self);
833 GST_BASE_TRANSFORM_UNLOCK (self);
836 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
842 audio_wsincband_get_property (GObject * object, guint prop_id, GValue * value,
845 GstAudioWSincBand *self = GST_AUDIO_WSINC_BAND (object);
849 g_value_set_int (value, self->kernel_length);
851 case PROP_LOWER_FREQUENCY:
852 g_value_set_float (value, self->lower_frequency);
854 case PROP_UPPER_FREQUENCY:
855 g_value_set_float (value, self->upper_frequency);
858 g_value_set_enum (value, self->mode);
861 g_value_set_enum (value, self->window);
864 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);