audioiirfilter.c 8.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* 
 * GStreamer
 * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
 *
 * 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
 *
 * audioiirfilter implements a generic audio <ulink url="http://en.wikipedia.org/wiki/Infinite_impulse_response">IIR filter</ulink>. Before usage the
 * "a" and "b" properties have to be set to the filter coefficients that
 * should be used.
28
 *
29 30
 * The filter coefficients describe the numerator and denominator of the
 * transfer function.
31
 *
32 33 34
 * 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.
35 36
 *
 * <refsect2>
37
 * <title>Example application</title>
38 39 40
 * |[
 * <xi:include xmlns:xi="http://www.w3.org/2003/XInclude" parse="text" href="../../../../tests/examples/audiofx/iirfilter-example.c" />
 * ]|
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
 * </refsect2>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <math.h>
#include <gst/gst.h>
#include <gst/audio/gstaudiofilter.h>
#include <gst/controller/gstcontroller.h>

#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 <sebastian.droege@collabora.co.uk>");
}

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;
  }

164
  if (self->a && self->a->n_values > 0) {
165 166 167 168 169 170 171 172
    a = g_new (gdouble, self->a->n_values);

    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);
    }
  }

173 174
  if (self->b && self->b->n_values > 0) {
    b = g_new (gdouble, self->b->n_values);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    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;
  }
}