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 "gstiirequalizer.h"
29 #define GST_EQUALIZER_TRANSFORM_LOCK(eq) \
30 g_mutex_lock (GST_BASE_TRANSFORM(eq)->transform_lock)
32 #define GST_EQUALIZER_TRANSFORM_UNLOCK(eq) \
33 g_mutex_unlock (GST_BASE_TRANSFORM(eq)->transform_lock)
43 static void gst_iir_equalizer_finalize (GObject * object);
44 static void gst_iir_equalizer_set_property (GObject * object,
45 guint prop_id, const GValue * value, GParamSpec * pspec);
46 static void gst_iir_equalizer_get_property (GObject * object,
47 guint prop_id, GValue * value, GParamSpec * pspec);
49 static gboolean gst_iir_equalizer_setup (GstAudioFilter * filter,
50 GstRingBufferSpec * fmt);
51 static GstFlowReturn gst_iir_equalizer_transform_ip (GstBaseTransform * btrans,
54 GST_DEBUG_CATEGORY_STATIC (equalizer_debug);
55 #define GST_CAT_DEFAULT equalizer_debug
57 #define ALLOWED_CAPS \
61 " endianness=(int)BYTE_ORDER," \
62 " signed=(bool)TRUE," \
63 " rate=(int)[1000,MAX]," \
64 " channels=(int)[1,MAX]; " \
65 "audio/x-raw-float," \
67 " endianness=(int)BYTE_ORDER," \
68 " rate=(int)[1000,MAX]," \
69 " channels=(int)[1,MAX]"
71 GST_BOILERPLATE (GstIirEqualizer, gst_iir_equalizer, GstAudioFilter,
72 GST_TYPE_AUDIO_FILTER);
75 gst_iir_equalizer_base_init (gpointer g_class)
77 GstAudioFilterClass *audiofilter_class = GST_AUDIO_FILTER_CLASS (g_class);
78 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
79 const GstElementDetails iir_equalizer_details =
80 GST_ELEMENT_DETAILS ("Equalizer",
81 "Filter/Effect/Audio",
82 "Direct Form IIR equalizer",
83 "Benjamin Otte <otte@gnome.org>");
86 gst_element_class_set_details (element_class, &iir_equalizer_details);
88 caps = gst_caps_from_string (ALLOWED_CAPS);
89 gst_audio_filter_class_add_pad_templates (audiofilter_class, caps);
90 gst_caps_unref (caps);
94 gst_iir_equalizer_class_init (GstIirEqualizerClass * klass)
96 GstAudioFilterClass *audio_filter_class = (GstAudioFilterClass *) klass;
97 GstBaseTransformClass *btrans_class = (GstBaseTransformClass *) klass;
98 GObjectClass *gobject_class = (GObjectClass *) klass;
100 gobject_class->set_property = gst_iir_equalizer_set_property;
101 gobject_class->get_property = gst_iir_equalizer_get_property;
102 gobject_class->finalize = gst_iir_equalizer_finalize;
104 g_object_class_install_property (gobject_class, ARG_NUM_BANDS,
105 g_param_spec_uint ("num-bands", "num-bands",
106 "number of different bands to use", 2, 64, 15,
107 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
108 g_object_class_install_property (gobject_class, ARG_BAND_WIDTH,
109 g_param_spec_double ("band-width", "band-width",
110 "band width calculated as distance between bands * this value", 0.1,
111 5.0, 1.0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
112 g_object_class_install_property (gobject_class, ARG_BAND_VALUES,
113 g_param_spec_value_array ("band-values", "band values",
114 "GValueArray holding gdouble values, one for each band with values "
115 "ranging from -1.0 to +1.0",
116 g_param_spec_double ("band-value", "band-value",
117 "Equaliser Band Value", -1.0, 1.0, 0.0, G_PARAM_WRITABLE),
120 audio_filter_class->setup = gst_iir_equalizer_setup;
121 btrans_class->transform_ip = gst_iir_equalizer_transform_ip;
125 gst_iir_equalizer_init (GstIirEqualizer * eq, GstIirEqualizerClass * g_class)
127 /* nothing to do here */
131 gst_iir_equalizer_finalize (GObject * object)
133 GstIirEqualizer *equ = GST_IIR_EQUALIZER (object);
136 g_free (equ->values);
137 g_free (equ->filter);
138 g_free (equ->history);
140 G_OBJECT_CLASS (parent_class)->finalize (object);
143 /* args are in the range [-1 ... 1] with 0 meaning "no action"
144 * convert to [-0.2 ... 1] with 0 meaning no action via the function
145 * f(x) = 0.25 * 5 ^ x - 0.25
148 arg_to_scale (gdouble arg)
150 return 0.25 * exp (log (5) * arg) - 0.25;
154 setup_filter (GstIirEqualizer * equ, SecondOrderFilter * filter, gdouble gain,
157 gdouble q = pow (HIGHEST_FREQ / LOWEST_FREQ,
158 1.0 / (equ->freq_count - 1)) * equ->band_width;
159 gdouble theta = frequency * 2 * M_PI;
161 filter->beta = (q - theta / 2) / (2 * q + theta);
162 filter->gamma = (0.5 + filter->beta) * cos (theta);
163 filter->alpha = (0.5 - filter->beta) / 2;
166 filter->alpha *= 2.0 * gain;
167 filter->gamma *= 2.0;
168 GST_INFO ("gain = %g, frequency = %g, alpha = %g, beta = %g, gamma=%g\n",
169 gain, frequency, filter->alpha, filter->beta, filter->gamma);
173 gst_iir_equalizer_compute_frequencies (GstIirEqualizer * equ, guint band_count)
177 gdouble step = pow (HIGHEST_FREQ / LOWEST_FREQ, 1.0 / (band_count - 1));
178 GstAudioFilter *audio = GST_AUDIO_FILTER (equ);
180 old_count = equ->freq_count;
181 equ->freq_count = band_count;
182 old_values = equ->values;
183 if (old_count < band_count) {
184 equ->freqs = g_realloc (equ->freqs, sizeof (gdouble) * band_count);
185 memset (equ->freqs + sizeof (gdouble) * old_count, 0,
186 sizeof (gdouble) * (band_count - old_count));
187 equ->values = g_realloc (equ->values, sizeof (gdouble) * band_count);
188 memset (equ->values + sizeof (gdouble) * old_count, 0,
189 sizeof (gdouble) * (band_count - old_count));
191 g_realloc (equ->filter, sizeof (SecondOrderFilter) * band_count);
192 memset (equ->filter + sizeof (SecondOrderFilter) * old_count, 0,
193 sizeof (SecondOrderFilter) * (band_count - old_count));
196 /* free + alloc = no memcpy */
197 g_free (equ->history);
199 g_malloc0 (equ->history_size * audio->format.channels * band_count);
200 equ->freqs[0] = LOWEST_FREQ;
201 for (i = 1; i < band_count; i++) {
202 equ->freqs[i] = equ->freqs[i - 1] * step;
205 if (audio->format.rate > 0) {
208 for (i = 0; i < band_count; i++) {
209 setup_filter (equ, &equ->filter[i], arg_to_scale (equ->values[i]),
210 equ->freqs[i] / audio->format.rate);
216 gst_iir_equalizer_set_property (GObject * object, guint prop_id,
217 const GValue * value, GParamSpec * pspec)
219 GstIirEqualizer *equ = GST_IIR_EQUALIZER (object);
221 GST_EQUALIZER_TRANSFORM_LOCK (equ);
222 GST_OBJECT_LOCK (equ);
225 gst_iir_equalizer_compute_frequencies (equ, g_value_get_uint (value));
228 if (g_value_get_double (value) != equ->band_width) {
229 equ->band_width = g_value_get_double (value);
230 if (GST_AUDIO_FILTER (equ)->format.rate) {
233 for (i = 0; i < equ->freq_count; i++) {
234 setup_filter (equ, &equ->filter[i], arg_to_scale (equ->values[i]),
235 equ->freqs[i] / GST_AUDIO_FILTER (equ)->format.rate);
240 case ARG_BAND_VALUES:{
243 arr = (GValueArray *) g_value_get_boxed (value);
245 g_warning ("Application tried to set empty band value array");
246 } else if (arr->n_values != equ->freq_count) {
247 g_warning ("Application tried to set %u band values, but there are "
248 "%u bands", arr->n_values, equ->freq_count);
252 for (i = 0; i < arr->n_values; ++i) {
255 new_val = g_value_get_double (g_value_array_get_nth (arr, i));
256 if (new_val != equ->values[i]) {
257 equ->values[i] = new_val;
258 setup_filter (equ, &equ->filter[i], arg_to_scale (new_val),
259 equ->freqs[i] / GST_AUDIO_FILTER (equ)->format.rate);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 GST_OBJECT_UNLOCK (equ);
270 GST_EQUALIZER_TRANSFORM_UNLOCK (equ);
274 gst_iir_equalizer_get_property (GObject * object, guint prop_id,
275 GValue * value, GParamSpec * pspec)
277 GstIirEqualizer *equ = GST_IIR_EQUALIZER (object);
279 GST_EQUALIZER_TRANSFORM_LOCK (equ);
280 GST_OBJECT_LOCK (equ);
283 g_value_set_uint (value, equ->freq_count);
286 g_value_set_double (value, equ->band_width);
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 GST_OBJECT_UNLOCK (equ);
293 GST_EQUALIZER_TRANSFORM_UNLOCK (equ);
296 /* start of code that is type specific */
298 #define CREATE_OPTIMIZED_FUNCTIONS(TYPE,BIG_TYPE,MIN_VAL,MAX_VAL) \
300 TYPE x1, x2; /* history of input values for a filter */ \
301 TYPE y1, y2; /* history of output values for a filter */ \
302 } SecondOrderHistory ## TYPE; \
305 one_step_ ## TYPE (SecondOrderFilter *filter, \
306 SecondOrderHistory ## TYPE *history, TYPE input) \
308 /* calculate output */ \
309 TYPE output = filter->alpha * (input - history->x2) + \
310 filter->gamma * history->y1 - filter->beta * history->y2; \
311 /* update history */ \
312 history->y2 = history->y1; \
313 history->y1 = output; \
314 history->x2 = history->x1; \
315 history->x1 = input; \
321 history_size_ ## TYPE = sizeof (SecondOrderHistory ## TYPE); \
324 gst_iir_equ_process_ ## TYPE (GstIirEqualizer *equ, guint8 *data, \
325 guint size, guint channels) \
327 guint frames = size / channels / sizeof (TYPE); \
332 for (i = 0; i < frames; i++) { \
333 for (c = 0; c < channels; c++) { \
334 SecondOrderHistory ## TYPE *history = equ->history; \
335 val = *((TYPE *) data); \
337 for (f = 0; f < equ->freq_count; f++) { \
338 SecondOrderFilter *filter = &equ->filter[f]; \
340 cur += one_step_ ## TYPE (filter, history, val); \
344 cur = CLAMP (cur, MIN_VAL, MAX_VAL); \
345 *((TYPE *) data) = (TYPE) cur; \
346 data += sizeof (TYPE); \
351 CREATE_OPTIMIZED_FUNCTIONS (gint16, gint, -32768, 32767);
352 CREATE_OPTIMIZED_FUNCTIONS (gfloat, gfloat, -1.0, 1.0);
355 gst_iir_equalizer_transform_ip (GstBaseTransform * btrans, GstBuffer * buf)
357 GstAudioFilter *filter = GST_AUDIO_FILTER (btrans);
358 GstIirEqualizer *equ = GST_IIR_EQUALIZER (btrans);
360 if (G_UNLIKELY (filter->format.channels < 1 || equ->process == NULL))
361 return GST_FLOW_NOT_NEGOTIATED;
363 equ->process (equ, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf),
364 filter->format.channels);
370 gst_iir_equalizer_setup (GstAudioFilter * audio, GstRingBufferSpec * fmt)
372 GstIirEqualizer *equ = GST_IIR_EQUALIZER (audio);
374 switch (fmt->width) {
376 equ->history_size = history_size_gint16;
377 equ->process = gst_iir_equ_process_gint16;
380 equ->history_size = history_size_gfloat;
381 equ->process = gst_iir_equ_process_gfloat;
386 gst_iir_equalizer_compute_frequencies (equ, equ->freq_count);
391 plugin_init (GstPlugin * plugin)
393 GST_DEBUG_CATEGORY_INIT (equalizer_debug, "equalizer", 0, "equalizer");
395 return gst_element_register (plugin, "equalizer", GST_RANK_NONE,
396 GST_TYPE_IIR_EQUALIZER);
399 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
402 "GStreamer equalizers",
403 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)