From 75c1c9f3789568b69678cd3babb4d29e0ebdd87b Mon Sep 17 00:00:00 2001 From: Sebastian Dröge Date: Tue, 13 Jan 2009 19:23:57 +0000 Subject: Add audioiirfilter and audiofirfilter elements which allow generic IIR/FIR filters to be implemented by providing the... Original commit message from CVS: * configure.ac: * gst/audiofx/Makefile.am: * gst/audiofx/audiofirfilter.c: (gst_audio_fir_filter_base_init), (gst_audio_fir_filter_class_init), (gst_audio_fir_filter_update_kernel), (gst_audio_fir_filter_init), (gst_audio_fir_filter_setup), (gst_audio_fir_filter_finalize), (gst_audio_fir_filter_set_property), (gst_audio_fir_filter_get_property): * gst/audiofx/audiofirfilter.h: * gst/audiofx/audiofx.c: (plugin_init): * gst/audiofx/audioiirfilter.c: (gst_audio_iir_filter_base_init), (gst_audio_iir_filter_class_init), (gst_audio_iir_filter_update_coefficients), (gst_audio_iir_filter_init), (gst_audio_iir_filter_setup), (gst_audio_iir_filter_finalize), (gst_audio_iir_filter_set_property), (gst_audio_iir_filter_get_property): * gst/audiofx/audioiirfilter.h: Add audioiirfilter and audiofirfilter elements which allow generic IIR/FIR filters to be implemented by providing the filter coefficients. Fixes bug #567577. * docs/plugins/Makefile.am: * docs/plugins/gst-plugins-good-plugins-docs.sgml: * docs/plugins/gst-plugins-good-plugins-sections.txt: * docs/plugins/gst-plugins-good-plugins.args: * docs/plugins/gst-plugins-good-plugins.hierarchy: * docs/plugins/gst-plugins-good-plugins.signals: * docs/plugins/inspect/plugin-alaw.xml: * docs/plugins/inspect/plugin-audiofx.xml: * docs/plugins/inspect/plugin-avi.xml: * docs/plugins/inspect/plugin-flac.xml: * docs/plugins/inspect/plugin-mulaw.xml: * docs/plugins/inspect/plugin-video4linux2.xml: * docs/plugins/inspect/plugin-wavparse.xml: Add documentation for the audioiirfilter and audiofirfilter elements. * tests/check/Makefile.am: * tests/check/elements/audiofirfilter.c: (on_message), (on_rate_changed), (on_handoff), (GST_START_TEST), (audiofirfilter_suite): * tests/check/elements/audioiirfilter.c: (on_message), (on_rate_changed), (on_handoff), (GST_START_TEST), (audioiirfilter_suite): * tests/examples/Makefile.am: * tests/examples/audiofx/Makefile.am: * tests/examples/audiofx/firfilter-example.c: (on_message), (on_rate_changed), (main): * tests/examples/audiofx/iirfilter-example.c: (on_message), (on_rate_changed), (main): Add unit tests and example applications for the two filter elements. --- gst/audiofx/Makefile.am | 6 +- gst/audiofx/audiofirfilter.c | 273 ++++++++++++++++++++++++++++++++++++++++ gst/audiofx/audiofirfilter.h | 72 +++++++++++ gst/audiofx/audiofx.c | 8 +- gst/audiofx/audioiirfilter.c | 291 +++++++++++++++++++++++++++++++++++++++++++ gst/audiofx/audioiirfilter.h | 71 +++++++++++ 6 files changed, 719 insertions(+), 2 deletions(-) create mode 100644 gst/audiofx/audiofirfilter.c create mode 100644 gst/audiofx/audiofirfilter.h create mode 100644 gst/audiofx/audioiirfilter.c create mode 100644 gst/audiofx/audioiirfilter.h (limited to 'gst/audiofx') diff --git a/gst/audiofx/Makefile.am b/gst/audiofx/Makefile.am index d93d3e9a..0ba4f1f3 100644 --- a/gst/audiofx/Makefile.am +++ b/gst/audiofx/Makefile.am @@ -12,9 +12,11 @@ libgstaudiofx_la_SOURCES = audiofx.c\ audiofxbaseiirfilter.c \ audiocheblimit.c \ audiochebband.c \ + audioiirfilter.c \ audiofxbasefirfilter.c \ audiowsincband.c \ - audiowsinclimit.c + audiowsinclimit.c \ + audiofirfilter.c # flags used to compile this plugin libgstaudiofx_la_CFLAGS = $(GST_CFLAGS) \ @@ -39,8 +41,10 @@ noinst_HEADERS = audiopanorama.h \ audiofxbaseiirfilter.h \ audiocheblimit.h \ audiochebband.h \ + audioiirfilter.h \ audiofxbasefirfilter.h \ audiowsincband.h \ audiowsinclimit.h \ + audiofirfilter.h \ math_compat.h diff --git a/gst/audiofx/audiofirfilter.c b/gst/audiofx/audiofirfilter.c new file mode 100644 index 00000000..3ee4d832 --- /dev/null +++ b/gst/audiofx/audiofirfilter.c @@ -0,0 +1,273 @@ +/* + * GStreamer + * Copyright (C) 2009 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + */ + +/** + * SECTION:element-audiofirfilter + * @short_description: Generic audio FIR filter + * + * + * + * audiofirfilter implements a generic audio FIR filter. Before usage the + * "kernel" property has to be set to the filter kernel that should be + * used and the "latency" property has to be set to the latency (in samples) + * that is introduced by the filter kernel. Setting a latency of n samples + * will lead to the first n samples being dropped from the output and + * n samples added to the end. + * + * + * The filter kernel describes the impulse response of the filter. To + * calculate the frequency response of the filter you have to calculate + * the Fourier Transform of the impulse response. + * + * + * To change the filter kernel whenever the sampling rate changes the + * "rate-changed" signal can be used. This should be done for most + * FIR filters as they're depending on the sampling rate. + * + * Example application + * + * + * + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include + +#include "audiofirfilter.h" + +#define GST_CAT_DEFAULT gst_audio_fir_filter_debug +GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); + +enum +{ + SIGNAL_RATE_CHANGED, + LAST_SIGNAL +}; + +enum +{ + PROP_0, + PROP_KERNEL, + PROP_LATENCY +}; + +static guint gst_audio_fir_filter_signals[LAST_SIGNAL] = { 0, }; + +#define DEBUG_INIT(bla) \ + GST_DEBUG_CATEGORY_INIT (gst_audio_fir_filter_debug, "audiofirfilter", 0, \ + "Generic audio FIR filter plugin"); + +GST_BOILERPLATE_FULL (GstAudioFIRFilter, gst_audio_fir_filter, GstAudioFilter, + GST_TYPE_AUDIO_FX_BASE_FIR_FILTER, DEBUG_INIT); + +static void gst_audio_fir_filter_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_audio_fir_filter_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); +static void gst_audio_fir_filter_finalize (GObject * object); + +static gboolean gst_audio_fir_filter_setup (GstAudioFilter * base, + GstRingBufferSpec * format); + +/* Element class */ +static void +gst_audio_fir_filter_base_init (gpointer g_class) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + + gst_element_class_set_details_simple (element_class, + "Audio FIR filter", "Filter/Effect/Audio", + "Generic audio FIR filter with custom filter kernel", + "Sebastian Dröge "); +} + +static void +gst_audio_fir_filter_class_init (GstAudioFIRFilterClass * klass) +{ + GObjectClass *gobject_class = (GObjectClass *) klass; + GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass; + + gobject_class->set_property = gst_audio_fir_filter_set_property; + gobject_class->get_property = gst_audio_fir_filter_get_property; + gobject_class->finalize = gst_audio_fir_filter_finalize; + + g_object_class_install_property (gobject_class, PROP_KERNEL, + g_param_spec_value_array ("kernel", "Filter Kernel", + "Filter kernel for the FIR filter", + g_param_spec_double ("Element", "Filter Kernel Element", + "Element of the filter kernel", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS), + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property (gobject_class, PROP_LATENCY, + g_param_spec_uint64 ("latency", "Latecy", + "Filter latency in samples", + 0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fir_filter_setup); + + /** + * GstAudioFIRFilter::rate-changed: + * @filter: the filter on which the signal is emitted + * @rate: the new sampling rate + * + * Will be emitted when the sampling rate changes. The callbacks + * will be called from the streaming thread and processing will + * stop until the event is handled. + */ + gst_audio_fir_filter_signals[SIGNAL_RATE_CHANGED] = + g_signal_new ("rate-changed", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAudioFIRFilterClass, rate_changed), + NULL, NULL, gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT); +} + +static void +gst_audio_fir_filter_update_kernel (GstAudioFIRFilter * self, GValueArray * va) +{ + gdouble *kernel; + guint i; + + gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER + (self)); + + if (va) { + if (self->kernel) + g_value_array_free (self->kernel); + + self->kernel = va; + } + + kernel = g_new (gdouble, self->kernel->n_values); + + for (i = 0; i < self->kernel->n_values; i++) { + GValue *v = g_value_array_get_nth (self->kernel, i); + kernel[i] = g_value_get_double (v); + } + + gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self), + kernel, self->kernel->n_values, self->latency); +} + +static void +gst_audio_fir_filter_init (GstAudioFIRFilter * self, + GstAudioFIRFilterClass * g_class) +{ + GValue v = { 0, }; + GValueArray *va; + + self->latency = 0; + va = g_value_array_new (1); + + g_value_init (&v, G_TYPE_DOUBLE); + g_value_set_double (&v, 1.0); + g_value_array_append (va, &v); + g_value_unset (&v); + gst_audio_fir_filter_update_kernel (self, va); + + self->lock = g_mutex_new (); +} + +/* GstAudioFilter vmethod implementations */ + +/* get notified of caps and plug in the correct process function */ +static gboolean +gst_audio_fir_filter_setup (GstAudioFilter * base, GstRingBufferSpec * format) +{ + GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (base); + + if (self->rate != format->rate) { + g_signal_emit (G_OBJECT (self), + gst_audio_fir_filter_signals[SIGNAL_RATE_CHANGED], 0, format->rate); + self->rate = format->rate; + } + + return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, format); +} + +static void +gst_audio_fir_filter_finalize (GObject * object) +{ + GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object); + + g_mutex_free (self->lock); + self->lock = NULL; + + if (self->kernel) + g_value_array_free (self->kernel); + self->kernel = NULL; + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static void +gst_audio_fir_filter_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object); + + g_return_if_fail (GST_IS_AUDIO_FIR_FILTER (self)); + + switch (prop_id) { + case PROP_KERNEL: + g_mutex_lock (self->lock); + gst_audio_fx_base_fir_filter_push_residue (GST_AUDIO_FX_BASE_FIR_FILTER + (self)); + + gst_audio_fir_filter_update_kernel (self, g_value_dup_boxed (value)); + g_mutex_unlock (self->lock); + break; + case PROP_LATENCY: + g_mutex_lock (self->lock); + self->latency = g_value_get_uint64 (value); + gst_audio_fir_filter_update_kernel (self, NULL); + g_mutex_unlock (self->lock); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_audio_fir_filter_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstAudioFIRFilter *self = GST_AUDIO_FIR_FILTER (object); + + switch (prop_id) { + case PROP_KERNEL: + g_value_set_boxed (value, self->kernel); + break; + case PROP_LATENCY: + g_value_set_uint64 (value, self->latency); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} diff --git a/gst/audiofx/audiofirfilter.h b/gst/audiofx/audiofirfilter.h new file mode 100644 index 00000000..d1d896c0 --- /dev/null +++ b/gst/audiofx/audiofirfilter.h @@ -0,0 +1,72 @@ +/* + * GStreamer + * Copyright (C) 2009 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + */ + +#ifndef __GST_AUDIO_FIR_FILTER_H__ +#define __GST_AUDIO_FIR_FILTER_H__ + +#include +#include + +#include "audiofxbasefirfilter.h" + +G_BEGIN_DECLS + +#define GST_TYPE_AUDIO_FIR_FILTER \ + (gst_audio_fir_filter_get_type()) +#define GST_AUDIO_FIR_FILTER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_FIR_FILTER,GstAudioFIRFilter)) +#define GST_AUDIO_FIR_FILTER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_FIR_FILTER,GstAudioFIRFilterClass)) +#define GST_IS_AUDIO_FIR_FILTER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_FIR_FILTER)) +#define GST_IS_AUDIO_FIR_FILTER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_FIR_FILTER)) + +typedef struct _GstAudioFIRFilter GstAudioFIRFilter; +typedef struct _GstAudioFIRFilterClass GstAudioFIRFilterClass; + +/** + * GstAudioFIRFilter: + * + * Opaque data structure. + */ +struct _GstAudioFIRFilter { + GstAudioFXBaseFIRFilter parent; + + GValueArray *kernel; + guint64 latency; + + /* < private > */ + GMutex *lock; + gint rate; +}; + +struct _GstAudioFIRFilterClass { + GstAudioFXBaseFIRFilterClass parent; + + void (*rate_changed) (GstElement * element, gint rate); +}; + +GType gst_audio_fir_filter_get_type (void); + +G_END_DECLS + +#endif /* __GST_AUDIO_FIR_FILTER_H__ */ diff --git a/gst/audiofx/audiofx.c b/gst/audiofx/audiofx.c index 43d1b0cc..62b70761 100644 --- a/gst/audiofx/audiofx.c +++ b/gst/audiofx/audiofx.c @@ -32,8 +32,10 @@ #include "audiodynamic.h" #include "audiocheblimit.h" #include "audiochebband.h" +#include "audioiirfilter.h" #include "audiowsincband.h" #include "audiowsinclimit.h" +#include "audiofirfilter.h" /* entry point to initialize the plug-in * initialize the plug-in itself @@ -60,10 +62,14 @@ plugin_init (GstPlugin * plugin) GST_TYPE_AUDIO_CHEB_LIMIT) && gst_element_register (plugin, "audiochebband", GST_RANK_NONE, GST_TYPE_AUDIO_CHEB_BAND) && + gst_element_register (plugin, "audioiirfilter", GST_RANK_NONE, + GST_TYPE_AUDIO_IIR_FILTER) && gst_element_register (plugin, "audiowsinclimit", GST_RANK_NONE, GST_TYPE_AUDIO_WSINC_LIMIT) && gst_element_register (plugin, "audiowsincband", GST_RANK_NONE, - GST_TYPE_AUDIO_WSINC_BAND)); + GST_TYPE_AUDIO_WSINC_BAND) && + gst_element_register (plugin, "audiofirfilter", GST_RANK_NONE, + GST_TYPE_AUDIO_FIR_FILTER)); } GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, diff --git a/gst/audiofx/audioiirfilter.c b/gst/audiofx/audioiirfilter.c new file mode 100644 index 00000000..76112c6f --- /dev/null +++ b/gst/audiofx/audioiirfilter.c @@ -0,0 +1,291 @@ +/* + * GStreamer + * Copyright (C) 2009 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + */ + +/** + * SECTION:element-audioiirfilter + * @short_description: Generic audio IIR filter + * + * + * + * audioiirfilter implements a generic audio IIR filter. Before usage the + * "a" and "b" properties have to be set to the filter coefficients that + * should be used. + * + * + * The filter coefficients describe the numerator and denominator of the + * transfer function. + * + * + * To change the filter coefficients whenever the sampling rate changes the + * "rate-changed" signal can be used. This should be done for most + * IIR filters as they're depending on the sampling rate. + * + * Example application + * + * + * + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include + +#include "audioiirfilter.h" + +#define GST_CAT_DEFAULT gst_audio_iir_filter_debug +GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); + +enum +{ + SIGNAL_RATE_CHANGED, + LAST_SIGNAL +}; + +enum +{ + PROP_0, + PROP_A, + PROP_B +}; + +static guint gst_audio_iir_filter_signals[LAST_SIGNAL] = { 0, }; + +#define DEBUG_INIT(bla) \ + GST_DEBUG_CATEGORY_INIT (gst_audio_iir_filter_debug, "audioiirfilter", 0, \ + "Generic audio IIR filter plugin"); + +GST_BOILERPLATE_FULL (GstAudioIIRFilter, gst_audio_iir_filter, GstAudioFilter, + GST_TYPE_AUDIO_FX_BASE_IIR_FILTER, DEBUG_INIT); + +static void gst_audio_iir_filter_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_audio_iir_filter_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); +static void gst_audio_iir_filter_finalize (GObject * object); + +static gboolean gst_audio_iir_filter_setup (GstAudioFilter * base, + GstRingBufferSpec * format); + +/* Element class */ +static void +gst_audio_iir_filter_base_init (gpointer g_class) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + + gst_element_class_set_details_simple (element_class, + "Audio IIR filter", "Filter/Effect/Audio", + "Generic audio IIR filter with custom filter kernel", + "Sebastian Dröge "); +} + +static void +gst_audio_iir_filter_class_init (GstAudioIIRFilterClass * klass) +{ + GObjectClass *gobject_class = (GObjectClass *) klass; + GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass; + + gobject_class->set_property = gst_audio_iir_filter_set_property; + gobject_class->get_property = gst_audio_iir_filter_get_property; + gobject_class->finalize = gst_audio_iir_filter_finalize; + + g_object_class_install_property (gobject_class, PROP_A, + g_param_spec_value_array ("a", "A", + "Filter coefficients (numerator of transfer function)", + g_param_spec_double ("Coefficient", "Filter Coefficient", + "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS), + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property (gobject_class, PROP_B, + g_param_spec_value_array ("b", "B", + "Filter coefficients (denominator of transfer function)", + g_param_spec_double ("Coefficient", "Filter Coefficient", + "Filter coefficient", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS), + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_iir_filter_setup); + + /** + * GstAudioIIRFilter::rate-changed: + * @filter: the filter on which the signal is emitted + * @rate: the new sampling rate + * + * Will be emitted when the sampling rate changes. The callbacks + * will be called from the streaming thread and processing will + * stop until the event is handled. + */ + gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED] = + g_signal_new ("rate-changed", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAudioIIRFilterClass, rate_changed), + NULL, NULL, gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT); +} + +static void +gst_audio_iir_filter_update_coefficients (GstAudioIIRFilter * self, + GValueArray * va, GValueArray * vb) +{ + gdouble *a = NULL, *b = NULL; + guint i; + + if (va) { + if (self->a) + g_value_array_free (self->a); + + self->a = va; + } + if (vb) { + if (self->b) + g_value_array_free (self->b); + + self->b = vb; + } + + if (self->a && self->a->n_values > 0) + a = g_new (gdouble, self->a->n_values); + if (self->b && self->b->n_values > 0) + b = g_new (gdouble, self->b->n_values); + + if (self->a) { + for (i = 0; i < self->a->n_values; i++) { + GValue *v = g_value_array_get_nth (self->a, i); + a[i] = g_value_get_double (v); + } + } + + if (self->b) { + for (i = 0; i < self->b->n_values; i++) { + GValue *v = g_value_array_get_nth (self->b, i); + b[i] = g_value_get_double (v); + } + } + + gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER + (self), a, (self->a) ? self->a->n_values : 0, b, + (self->b) ? self->b->n_values : 0); +} + +static void +gst_audio_iir_filter_init (GstAudioIIRFilter * self, + GstAudioIIRFilterClass * g_class) +{ + GValue v = { 0, }; + GValueArray *a, *b; + + a = g_value_array_new (1); + + g_value_init (&v, G_TYPE_DOUBLE); + g_value_set_double (&v, 1.0); + g_value_array_append (a, &v); + g_value_unset (&v); + + b = NULL; + gst_audio_iir_filter_update_coefficients (self, a, b); + + self->lock = g_mutex_new (); +} + +/* GstAudioFilter vmethod implementations */ + +/* get notified of caps and plug in the correct process function */ +static gboolean +gst_audio_iir_filter_setup (GstAudioFilter * base, GstRingBufferSpec * format) +{ + GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (base); + + if (self->rate != format->rate) { + g_signal_emit (G_OBJECT (self), + gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED], 0, format->rate); + self->rate = format->rate; + } + + return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, format); +} + +static void +gst_audio_iir_filter_finalize (GObject * object) +{ + GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object); + + g_mutex_free (self->lock); + self->lock = NULL; + + if (self->a) + g_value_array_free (self->a); + self->a = NULL; + if (self->b) + g_value_array_free (self->b); + self->b = NULL; + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static void +gst_audio_iir_filter_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object); + + g_return_if_fail (GST_IS_AUDIO_IIR_FILTER (self)); + + switch (prop_id) { + case PROP_A: + g_mutex_lock (self->lock); + gst_audio_iir_filter_update_coefficients (self, g_value_dup_boxed (value), + NULL); + g_mutex_unlock (self->lock); + break; + case PROP_B: + g_mutex_lock (self->lock); + gst_audio_iir_filter_update_coefficients (self, NULL, + g_value_dup_boxed (value)); + g_mutex_unlock (self->lock); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_audio_iir_filter_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (object); + + switch (prop_id) { + case PROP_A: + g_value_set_boxed (value, self->a); + break; + case PROP_B: + g_value_set_boxed (value, self->b); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} diff --git a/gst/audiofx/audioiirfilter.h b/gst/audiofx/audioiirfilter.h new file mode 100644 index 00000000..607edf27 --- /dev/null +++ b/gst/audiofx/audioiirfilter.h @@ -0,0 +1,71 @@ +/* + * GStreamer + * Copyright (C) 2009 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + */ + +#ifndef __GST_AUDIO_IIR_FILTER_H__ +#define __GST_AUDIO_IIR_FILTER_H__ + +#include +#include + +#include "audiofxbaseiirfilter.h" + +G_BEGIN_DECLS + +#define GST_TYPE_AUDIO_IIR_FILTER \ + (gst_audio_iir_filter_get_type()) +#define GST_AUDIO_IIR_FILTER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_IIR_FILTER,GstAudioIIRFilter)) +#define GST_AUDIO_IIR_FILTER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_IIR_FILTER,GstAudioIIRFilterClass)) +#define GST_IS_AUDIO_IIR_FILTER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_IIR_FILTER)) +#define GST_IS_AUDIO_IIR_FILTER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_IIR_FILTER)) + +typedef struct _GstAudioIIRFilter GstAudioIIRFilter; +typedef struct _GstAudioIIRFilterClass GstAudioIIRFilterClass; + +/** + * GstAudioIIRFilter: + * + * Opaque data structure. + */ +struct _GstAudioIIRFilter { + GstAudioFXBaseIIRFilter parent; + + GValueArray *a, *b; + + /* < private > */ + GMutex *lock; + gint rate; +}; + +struct _GstAudioIIRFilterClass { + GstAudioFXBaseIIRFilterClass parent; + + void (*rate_changed) (GstElement * element, gint rate); +}; + +GType gst_audio_iir_filter_get_type (void); + +G_END_DECLS + +#endif /* __GST_AUDIO_IIR_FILTER_H__ */ -- cgit