2 * Copyright (C) <2004> Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
27 #include <gst/audio/audio.h>
28 #include <gst/audio/gstaudiofilter.h>
30 typedef struct _GstIirEqualizer GstIirEqualizer;
31 typedef struct _GstIirEqualizerClass GstIirEqualizerClass;
33 #define GST_TYPE_IIR_EQUALIZER \
34 (gst_iir_equalizer_get_type())
35 #define GST_IIR_EQUALIZER(obj) \
36 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_IIR_EQUALIZER,GstIirEqualizer))
37 #define GST_IIR_EQUALIZER_CLASS(klass) \
38 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_IIR_EQUALIZER,GstIirEqualizerClass))
39 #define GST_IS_IIR_EQUALIZER(obj) \
40 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_IIR_EQUALIZER))
41 #define GST_IS_IIR_EQUALIZER_CLASS(obj) \
42 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_IIR_EQUALIZER))
44 #define LOWEST_FREQ (20.0)
45 #define HIGHEST_FREQ (20000.0)
47 typedef void (*ProcessFunc) (GstIirEqualizer * equ, guint8 * data, guint size,
52 gdouble alpha; /* IIR coefficients for outputs */
53 gdouble beta; /* IIR coefficients for inputs */
54 gdouble gamma; /* IIR coefficients for inputs */
57 struct _GstIirEqualizer
59 GstAudiofilter audiofilter;
68 SecondOrderFilter *filter;
74 struct _GstIirEqualizerClass
76 GstAudiofilterClass audiofilter_class;
88 static void gst_iir_equalizer_base_init (gpointer g_class);
89 static void gst_iir_equalizer_class_init (gpointer g_class,
91 static void gst_iir_equalizer_init (GTypeInstance * instance, gpointer g_class);
92 static void gst_iir_equalizer_finalize (GObject * object);
94 static void gst_iir_equalizer_set_property (GObject * object,
95 guint prop_id, const GValue * value, GParamSpec * pspec);
96 static void gst_iir_equalizer_get_property (GObject * object,
97 guint prop_id, GValue * value, GParamSpec * pspec);
99 static void gst_iir_equalizer_setup (GstAudiofilter * iir_equalizer);
100 static void gst_iir_equalizer_filter_inplace (GstAudiofilter *
101 iir_equalizer, GstBuffer * buf);
103 static GstAudiofilterClass *parent_class;
106 gst_iir_equalizer_get_type (void)
108 static GType iir_equalizer_type = 0;
110 if (!iir_equalizer_type) {
111 static const GTypeInfo iir_equalizer_info = {
112 sizeof (GstIirEqualizerClass),
113 gst_iir_equalizer_base_init,
115 gst_iir_equalizer_class_init,
117 gst_iir_equalizer_init,
118 sizeof (GstIirEqualizer),
123 iir_equalizer_type = g_type_register_static (GST_TYPE_AUDIOFILTER,
124 "GstIirEqualizer", &iir_equalizer_info, 0);
126 return iir_equalizer_type;
130 gst_iir_equalizer_base_init (gpointer g_class)
132 static GstElementDetails iir_equalizer_details = {
134 "Filter/Effect/Audio",
135 "Direct Form IIR equalizer",
136 "Benjamin Otte <otte@gnome.org>"
138 GstIirEqualizerClass *klass = (GstIirEqualizerClass *) g_class;
139 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
142 gst_element_class_set_details (element_class, &iir_equalizer_details);
144 caps = gst_caps_from_string ("audio/x-raw-int, depth=(int)16, width=(int)16, "
145 "endianness=(int)BYTE_ORDER, signed=(bool)TRUE, "
146 "rate=(int)[1000,MAX], channels=(int)[1,6];"
147 "audio/x-raw-float, width=(int)32, endianness=(int)BYTE_ORDER,"
148 "rate=(int)[1000,MAX], channels=(int)[1,6]");
149 gst_audiofilter_class_add_pad_templates (GST_AUDIOFILTER_CLASS (g_class),
151 gst_caps_free (caps);
155 gst_iir_equalizer_class_init (gpointer g_class, gpointer class_data)
157 GObjectClass *gobject_class;
158 GstElementClass *gstelement_class;
159 GstIirEqualizerClass *klass;
160 GstAudiofilterClass *audiofilter_class;
162 klass = (GstIirEqualizerClass *) g_class;
163 gobject_class = (GObjectClass *) klass;
164 gstelement_class = (GstElementClass *) klass;
165 audiofilter_class = (GstAudiofilterClass *) g_class;
167 gobject_class->set_property = gst_iir_equalizer_set_property;
168 gobject_class->get_property = gst_iir_equalizer_get_property;
169 gobject_class->finalize = gst_iir_equalizer_finalize;
171 parent_class = g_type_class_peek_parent (g_class);
173 g_object_class_install_property (gobject_class, ARG_BANDS,
174 g_param_spec_uint ("bands", "bands", "number of different bands to use",
175 2, 64, 15, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
176 g_object_class_install_property (gobject_class, ARG_BANDWIDTH,
177 g_param_spec_double ("bandwidth", "bandwidth",
178 "bandwidth calculated as distance between bands * this value", 0.1,
179 5.0, 1.0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
180 /* FIXME FIXME FIXME */
181 g_object_class_install_property (gobject_class, ARG_VALUES,
182 g_param_spec_pointer ("values", "values",
183 "expects a gdouble* of values to use for the bands",
186 audiofilter_class->setup = gst_iir_equalizer_setup;
187 audiofilter_class->filter_inplace = gst_iir_equalizer_filter_inplace;
191 gst_iir_equalizer_init (GTypeInstance * instance, gpointer g_class)
196 gst_iir_equalizer_finalize (GObject * object)
198 GstIirEqualizer *equ = GST_IIR_EQUALIZER (object);
201 g_free (equ->values);
202 g_free (equ->filter);
203 g_free (equ->history);
205 G_OBJECT_CLASS (parent_class)->finalize (object);
208 /* args are in the range [-1 ... 1] with 0 meaning "no action"
209 * convert to [-0.2 ... 1] with 0 meaning no action via the function
210 * f(x) = 0.25 * 5 ^ x - 0.25
213 arg_to_scale (gdouble arg)
215 return 0.25 * exp (log (5) * arg) - 0.25;
219 setup_filter (GstIirEqualizer * equ, SecondOrderFilter * filter, gdouble gain,
222 gdouble q = pow (HIGHEST_FREQ / LOWEST_FREQ,
223 1.0 / (equ->freq_count - 1)) * equ->bandwidth;
224 gdouble theta = frequency * 2 * M_PI;
226 filter->beta = (q - theta / 2) / (2 * q + theta);
227 filter->gamma = (0.5 + filter->beta) * cos (theta);
228 filter->alpha = (0.5 - filter->beta) / 2;
231 filter->alpha *= 2.0 * gain;
232 filter->gamma *= 2.0;
233 GST_INFO ("gain = %g, frequency = %g, alpha = %g, beta = %g, gamma=%g\n",
234 gain, frequency, filter->alpha, filter->beta, filter->gamma);
238 gst_iir_equalizer_compute_frequencies (GstIirEqualizer * equ, guint band_count)
242 gdouble step = pow (HIGHEST_FREQ / LOWEST_FREQ, 1.0 / (band_count - 1));
243 GstAudiofilter *audio = GST_AUDIOFILTER (equ);
245 old_count = equ->freq_count;
246 equ->freq_count = band_count;
247 old_values = equ->values;
248 if (old_count < band_count) {
249 equ->freqs = g_realloc (equ->freqs, sizeof (gdouble) * band_count);
250 memset (equ->freqs + sizeof (gdouble) * old_count, 0,
251 sizeof (gdouble) * (band_count - old_count));
252 equ->values = g_realloc (equ->values, sizeof (gdouble) * band_count);
253 memset (equ->values + sizeof (gdouble) * old_count, 0,
254 sizeof (gdouble) * (band_count - old_count));
256 g_realloc (equ->filter, sizeof (SecondOrderFilter) * band_count);
257 memset (equ->filter + sizeof (SecondOrderFilter) * old_count, 0,
258 sizeof (SecondOrderFilter) * (band_count - old_count));
261 g_realloc (equ->history,
262 equ->history_size * audio->channels * band_count);
263 memset (equ->history, 0, equ->history_size * audio->channels * band_count);
264 equ->freqs[0] = LOWEST_FREQ;
265 for (i = 1; i < band_count; i++) {
266 equ->freqs[i] = equ->freqs[i - 1] * step;
272 for (i = 0; i < band_count; i++) {
273 setup_filter (equ, &equ->filter[i], arg_to_scale (equ->values[i]),
274 equ->freqs[i] / audio->rate);
280 gst_iir_equalizer_set_property (GObject * object, guint prop_id,
281 const GValue * value, GParamSpec * pspec)
283 GstIirEqualizer *equ = GST_IIR_EQUALIZER (object);
287 gst_iir_equalizer_compute_frequencies (equ, g_value_get_uint (value));
290 if (g_value_get_double (value) != equ->bandwidth) {
291 equ->bandwidth = g_value_get_double (value);
292 if (GST_AUDIOFILTER (equ)->rate) {
295 for (i = 0; i < equ->freq_count; i++) {
296 setup_filter (equ, &equ->filter[i], arg_to_scale (equ->values[i]),
297 equ->freqs[i] / GST_AUDIOFILTER (equ)->rate);
304 gdouble *new = g_value_get_pointer (value);
307 for (i = 0; i < equ->freq_count; i++) {
308 if (new[i] != equ->values[i]) {
309 equ->values[i] = new[i];
310 setup_filter (equ, &equ->filter[i], arg_to_scale (new[i]),
311 equ->freqs[i] / GST_AUDIOFILTER (equ)->rate);
317 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323 gst_iir_equalizer_get_property (GObject * object, guint prop_id,
324 GValue * value, GParamSpec * pspec)
326 GstIirEqualizer *equ = GST_IIR_EQUALIZER (object);
330 g_value_set_uint (value, equ->freq_count);
333 g_value_set_double (value, equ->bandwidth);
336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 /* start of code that is type specific */
343 #define CREATE_OPTIMIZED_FUNCTIONS(TYPE,BIG_TYPE,MIN_VAL,MAX_VAL) \
345 TYPE x1, x2; /* history of input values for a filter */ \
346 TYPE y1, y2; /* history of output values for a filter */ \
347 } SecondOrderHistory ## TYPE; \
350 one_step_ ## TYPE (SecondOrderFilter *filter, \
351 SecondOrderHistory ## TYPE *history, TYPE input) \
353 /* calculate output */ \
354 TYPE output = filter->alpha * (input - history->x2) + \
355 filter->gamma * history->y1 - filter->beta * history->y2; \
356 /* update history */ \
357 history->y2 = history->y1; \
358 history->y1 = output; \
359 history->x2 = history->x1; \
360 history->x1 = input; \
366 history_size_ ## TYPE = sizeof (SecondOrderHistory ## TYPE); \
369 gst_iir_equ_process_ ## TYPE (GstIirEqualizer *equ, guint8 *data, \
370 guint size, guint channels) \
372 guint frames = size / channels / sizeof (TYPE); \
377 for (i = 0; i < frames; i++) { \
378 for (c = 0; c < channels; c++) { \
379 SecondOrderHistory ## TYPE *history = equ->history; \
380 val = *((TYPE *) data); \
382 for (f = 0; f < equ->freq_count; f++) { \
383 SecondOrderFilter *filter = &equ->filter[f]; \
385 cur += one_step_ ## TYPE (filter, history, val); \
389 cur = CLAMP (cur, MIN_VAL, MAX_VAL); \
390 *((TYPE *) data) = (TYPE) cur; \
391 data += sizeof (TYPE); \
396 CREATE_OPTIMIZED_FUNCTIONS (gint16, gint, -32768, 32767);
397 CREATE_OPTIMIZED_FUNCTIONS (gfloat, gfloat, -1.0, 1.0);
400 gst_iir_equalizer_filter_inplace (GstAudiofilter * filter, GstBuffer * buf)
402 GstIirEqualizer *equ = GST_IIR_EQUALIZER (filter);
404 equ->process (equ, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf),
409 gst_iir_equalizer_setup (GstAudiofilter * audio)
411 GstIirEqualizer *equ = GST_IIR_EQUALIZER (audio);
413 if (audio->width == 16) {
414 equ->history_size = history_size_gint16;
415 equ->process = gst_iir_equ_process_gint16;
416 } else if (audio->width == 32) {
417 equ->history_size = history_size_gfloat;
418 equ->process = gst_iir_equ_process_gfloat;
420 g_assert_not_reached ();
422 gst_iir_equalizer_compute_frequencies (equ, equ->freq_count);
426 plugin_init (GstPlugin * plugin)
428 if (!gst_library_load ("gstaudiofilter"))
431 return gst_element_register (plugin, "equalizer", GST_RANK_NONE,
432 GST_TYPE_IIR_EQUALIZER);
435 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
438 "GStreamer equalizers",
439 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)