Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audiodynamic.c
1 /* 
2  * GStreamer
3  * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:element-audiodynamic
23  *
24  * This element can act as a compressor or expander. A compressor changes the
25  * amplitude of all samples above a specific threshold with a specific ratio,
26  * a expander does the same for all samples below a specific threshold. If
27  * soft-knee mode is selected the ratio is applied smoothly.
28  *
29  * <refsect2>
30  * <title>Example launch line</title>
31  * |[
32  * gst-launch audiotestsrc wave=saw ! audiodynamic characteristics=soft-knee mode=compressor threshold=0.5 rate=0.5 ! alsasink
33  * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiodynamic characteristics=hard-knee mode=expander threshold=0.2 rate=4.0 ! alsasink
34  * gst-launch audiotestsrc wave=saw ! audioconvert ! audiodynamic ! audioconvert ! alsasink
35  * ]|
36  * </refsect2>
37  */
38
39 /* TODO: Implement attack and release parameters */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <gst/gst.h>
46 #include <gst/base/gstbasetransform.h>
47 #include <gst/audio/audio.h>
48 #include <gst/audio/gstaudiofilter.h>
49 #include <gst/controller/gstcontroller.h>
50
51 #include "audiodynamic.h"
52
53 #define GST_CAT_DEFAULT gst_audio_dynamic_debug
54 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
55
56 /* Filter signals and args */
57 enum
58 {
59   /* FILL ME */
60   LAST_SIGNAL
61 };
62
63 enum
64 {
65   PROP_0,
66   PROP_CHARACTERISTICS,
67   PROP_MODE,
68   PROP_THRESHOLD,
69   PROP_RATIO
70 };
71
72 #define ALLOWED_CAPS \
73     "audio/x-raw-int,"                                                \
74     " depth=(int)16,"                                                 \
75     " width=(int)16,"                                                 \
76     " endianness=(int)BYTE_ORDER,"                                    \
77     " signed=(bool)TRUE,"                                             \
78     " rate=(int)[1,MAX],"                                             \
79     " channels=(int)[1,MAX]; "                                        \
80     "audio/x-raw-float,"                                              \
81     " width=(int)32,"                                                 \
82     " endianness=(int)BYTE_ORDER,"                                    \
83     " rate=(int)[1,MAX],"                                             \
84     " channels=(int)[1,MAX]"
85
86 G_DEFINE_TYPE (GstAudioDynamic, gst_audio_dynamic, GST_TYPE_AUDIO_FILTER);
87
88 static void gst_audio_dynamic_set_property (GObject * object, guint prop_id,
89     const GValue * value, GParamSpec * pspec);
90 static void gst_audio_dynamic_get_property (GObject * object, guint prop_id,
91     GValue * value, GParamSpec * pspec);
92
93 static gboolean gst_audio_dynamic_setup (GstAudioFilter * filter,
94     GstRingBufferSpec * format);
95 static GstFlowReturn gst_audio_dynamic_transform_ip (GstBaseTransform * base,
96     GstBuffer * buf);
97
98 static void
99 gst_audio_dynamic_transform_hard_knee_compressor_int (GstAudioDynamic * filter,
100     gint16 * data, guint num_samples);
101 static void
102 gst_audio_dynamic_transform_hard_knee_compressor_float (GstAudioDynamic *
103     filter, gfloat * data, guint num_samples);
104 static void
105 gst_audio_dynamic_transform_soft_knee_compressor_int (GstAudioDynamic * filter,
106     gint16 * data, guint num_samples);
107 static void
108 gst_audio_dynamic_transform_soft_knee_compressor_float (GstAudioDynamic *
109     filter, gfloat * data, guint num_samples);
110 static void gst_audio_dynamic_transform_hard_knee_expander_int (GstAudioDynamic
111     * filter, gint16 * data, guint num_samples);
112 static void
113 gst_audio_dynamic_transform_hard_knee_expander_float (GstAudioDynamic * filter,
114     gfloat * data, guint num_samples);
115 static void gst_audio_dynamic_transform_soft_knee_expander_int (GstAudioDynamic
116     * filter, gint16 * data, guint num_samples);
117 static void
118 gst_audio_dynamic_transform_soft_knee_expander_float (GstAudioDynamic * filter,
119     gfloat * data, guint num_samples);
120
121 static GstAudioDynamicProcessFunc process_functions[] = {
122   (GstAudioDynamicProcessFunc)
123       gst_audio_dynamic_transform_hard_knee_compressor_int,
124   (GstAudioDynamicProcessFunc)
125       gst_audio_dynamic_transform_hard_knee_compressor_float,
126   (GstAudioDynamicProcessFunc)
127       gst_audio_dynamic_transform_soft_knee_compressor_int,
128   (GstAudioDynamicProcessFunc)
129       gst_audio_dynamic_transform_soft_knee_compressor_float,
130   (GstAudioDynamicProcessFunc)
131       gst_audio_dynamic_transform_hard_knee_expander_int,
132   (GstAudioDynamicProcessFunc)
133       gst_audio_dynamic_transform_hard_knee_expander_float,
134   (GstAudioDynamicProcessFunc)
135       gst_audio_dynamic_transform_soft_knee_expander_int,
136   (GstAudioDynamicProcessFunc)
137   gst_audio_dynamic_transform_soft_knee_expander_float
138 };
139
140 enum
141 {
142   CHARACTERISTICS_HARD_KNEE = 0,
143   CHARACTERISTICS_SOFT_KNEE
144 };
145
146 #define GST_TYPE_AUDIO_DYNAMIC_CHARACTERISTICS (gst_audio_dynamic_characteristics_get_type ())
147 static GType
148 gst_audio_dynamic_characteristics_get_type (void)
149 {
150   static GType gtype = 0;
151
152   if (gtype == 0) {
153     static const GEnumValue values[] = {
154       {CHARACTERISTICS_HARD_KNEE, "Hard Knee (default)",
155           "hard-knee"},
156       {CHARACTERISTICS_SOFT_KNEE, "Soft Knee (smooth)",
157           "soft-knee"},
158       {0, NULL, NULL}
159     };
160
161     gtype = g_enum_register_static ("GstAudioDynamicCharacteristics", values);
162   }
163   return gtype;
164 }
165
166 enum
167 {
168   MODE_COMPRESSOR = 0,
169   MODE_EXPANDER
170 };
171
172 #define GST_TYPE_AUDIO_DYNAMIC_MODE (gst_audio_dynamic_mode_get_type ())
173 static GType
174 gst_audio_dynamic_mode_get_type (void)
175 {
176   static GType gtype = 0;
177
178   if (gtype == 0) {
179     static const GEnumValue values[] = {
180       {MODE_COMPRESSOR, "Compressor (default)",
181           "compressor"},
182       {MODE_EXPANDER, "Expander", "expander"},
183       {0, NULL, NULL}
184     };
185
186     gtype = g_enum_register_static ("GstAudioDynamicMode", values);
187   }
188   return gtype;
189 }
190
191 static gboolean
192 gst_audio_dynamic_set_process_function (GstAudioDynamic * filter)
193 {
194   gint func_index;
195
196   func_index = (filter->mode == MODE_COMPRESSOR) ? 0 : 4;
197   func_index += (filter->characteristics == CHARACTERISTICS_HARD_KNEE) ? 0 : 2;
198   func_index +=
199       (GST_AUDIO_FILTER (filter)->format.type == GST_BUFTYPE_FLOAT) ? 1 : 0;
200
201   if (func_index >= 0 && func_index < 8) {
202     filter->process = process_functions[func_index];
203     return TRUE;
204   }
205
206   return FALSE;
207 }
208
209 /* GObject vmethod implementations */
210
211 static void
212 gst_audio_dynamic_class_init (GstAudioDynamicClass * klass)
213 {
214   GObjectClass *gobject_class;
215   GstElementClass *gstelement_class;
216   GstCaps *caps;
217
218   GST_DEBUG_CATEGORY_INIT (gst_audio_dynamic_debug, "audiodynamic", 0,
219       "audiodynamic element");
220
221   gobject_class = (GObjectClass *) klass;
222   gstelement_class = (GstElementClass *) klass;
223
224   gobject_class->set_property = gst_audio_dynamic_set_property;
225   gobject_class->get_property = gst_audio_dynamic_get_property;
226
227   g_object_class_install_property (gobject_class, PROP_CHARACTERISTICS,
228       g_param_spec_enum ("characteristics", "Characteristics",
229           "Selects whether the ratio should be applied smooth (soft-knee) "
230           "or hard (hard-knee).",
231           GST_TYPE_AUDIO_DYNAMIC_CHARACTERISTICS, CHARACTERISTICS_HARD_KNEE,
232           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233
234   g_object_class_install_property (gobject_class, PROP_MODE,
235       g_param_spec_enum ("mode", "Mode",
236           "Selects whether the filter should work on loud samples (compressor) or"
237           "quiet samples (expander).",
238           GST_TYPE_AUDIO_DYNAMIC_MODE, MODE_COMPRESSOR,
239           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240
241   g_object_class_install_property (gobject_class, PROP_THRESHOLD,
242       g_param_spec_float ("threshold", "Threshold",
243           "Threshold until the filter is activated", 0.0, 1.0,
244           0.0,
245           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
246
247   g_object_class_install_property (gobject_class, PROP_RATIO,
248       g_param_spec_float ("ratio", "Ratio",
249           "Ratio that should be applied", 0.0, G_MAXFLOAT,
250           1.0,
251           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
252
253   gst_element_class_set_details_simple (gstelement_class,
254       "Dynamic range controller", "Filter/Effect/Audio",
255       "Compressor and Expander", "Sebastian Dröge <slomo@circular-chaos.org>");
256
257   caps = gst_caps_from_string (ALLOWED_CAPS);
258   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
259       caps);
260   gst_caps_unref (caps);
261
262   GST_AUDIO_FILTER_CLASS (klass)->setup =
263       GST_DEBUG_FUNCPTR (gst_audio_dynamic_setup);
264   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
265       GST_DEBUG_FUNCPTR (gst_audio_dynamic_transform_ip);
266 }
267
268 static void
269 gst_audio_dynamic_init (GstAudioDynamic * filter)
270 {
271   filter->ratio = 1.0;
272   filter->threshold = 0.0;
273   filter->characteristics = CHARACTERISTICS_HARD_KNEE;
274   filter->mode = MODE_COMPRESSOR;
275   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
276   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
277 }
278
279 static void
280 gst_audio_dynamic_set_property (GObject * object, guint prop_id,
281     const GValue * value, GParamSpec * pspec)
282 {
283   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object);
284
285   switch (prop_id) {
286     case PROP_CHARACTERISTICS:
287       filter->characteristics = g_value_get_enum (value);
288       gst_audio_dynamic_set_process_function (filter);
289       break;
290     case PROP_MODE:
291       filter->mode = g_value_get_enum (value);
292       gst_audio_dynamic_set_process_function (filter);
293       break;
294     case PROP_THRESHOLD:
295       filter->threshold = g_value_get_float (value);
296       break;
297     case PROP_RATIO:
298       filter->ratio = g_value_get_float (value);
299       break;
300     default:
301       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
302       break;
303   }
304 }
305
306 static void
307 gst_audio_dynamic_get_property (GObject * object, guint prop_id,
308     GValue * value, GParamSpec * pspec)
309 {
310   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object);
311
312   switch (prop_id) {
313     case PROP_CHARACTERISTICS:
314       g_value_set_enum (value, filter->characteristics);
315       break;
316     case PROP_MODE:
317       g_value_set_enum (value, filter->mode);
318       break;
319     case PROP_THRESHOLD:
320       g_value_set_float (value, filter->threshold);
321       break;
322     case PROP_RATIO:
323       g_value_set_float (value, filter->ratio);
324       break;
325     default:
326       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327       break;
328   }
329 }
330
331 /* GstAudioFilter vmethod implementations */
332
333 static gboolean
334 gst_audio_dynamic_setup (GstAudioFilter * base, GstRingBufferSpec * format)
335 {
336   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base);
337   gboolean ret = TRUE;
338
339   ret = gst_audio_dynamic_set_process_function (filter);
340
341   return ret;
342 }
343
344 static void
345 gst_audio_dynamic_transform_hard_knee_compressor_int (GstAudioDynamic * filter,
346     gint16 * data, guint num_samples)
347 {
348   glong val;
349   glong thr_p = filter->threshold * G_MAXINT16;
350   glong thr_n = filter->threshold * G_MININT16;
351
352   /* Nothing to do for us if ratio is 1.0 or if the threshold
353    * equals 1.0. */
354   if (filter->threshold == 1.0 || filter->ratio == 1.0)
355     return;
356
357   for (; num_samples; num_samples--) {
358     val = *data;
359
360     if (val > thr_p) {
361       val = thr_p + (val - thr_p) * filter->ratio;
362     } else if (val < thr_n) {
363       val = thr_n + (val - thr_n) * filter->ratio;
364     }
365     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
366   }
367 }
368
369 static void
370 gst_audio_dynamic_transform_hard_knee_compressor_float (GstAudioDynamic *
371     filter, gfloat * data, guint num_samples)
372 {
373   gdouble val, threshold = filter->threshold;
374
375   /* Nothing to do for us if ratio == 1.0.
376    * As float values can be above 1.0 we have to do something
377    * if threshold is greater than 1.0. */
378   if (filter->ratio == 1.0)
379     return;
380
381   for (; num_samples; num_samples--) {
382     val = *data;
383
384     if (val > threshold) {
385       val = threshold + (val - threshold) * filter->ratio;
386     } else if (val < -threshold) {
387       val = -threshold + (val + threshold) * filter->ratio;
388     }
389     *data++ = (gfloat) val;
390   }
391 }
392
393 static void
394 gst_audio_dynamic_transform_soft_knee_compressor_int (GstAudioDynamic * filter,
395     gint16 * data, guint num_samples)
396 {
397   glong val;
398   glong thr_p = filter->threshold * G_MAXINT16;
399   glong thr_n = filter->threshold * G_MININT16;
400   gdouble a_p, b_p, c_p;
401   gdouble a_n, b_n, c_n;
402
403   /* Nothing to do for us if ratio is 1.0 or if the threshold
404    * equals 1.0. */
405   if (filter->threshold == 1.0 || filter->ratio == 1.0)
406     return;
407
408   /* We build a 2nd degree polynomial here for
409    * values greater than threshold or small than
410    * -threshold with:
411    * f(t) = t, f'(t) = 1, f'(m) = r
412    * =>
413    * a = (1-r)/(2*(t-m))
414    * b = (r*t - m)/(t-m)
415    * c = t * (1 - b - a*t)
416    * f(x) = ax^2 + bx + c
417    */
418
419   /* shouldn't happen because this would only be the case
420    * for threshold == 1.0 which we catch above */
421   g_assert (thr_p - G_MAXINT16 != 0);
422   g_assert (thr_n - G_MININT != 0);
423
424   a_p = (1 - filter->ratio) / (2 * (thr_p - G_MAXINT16));
425   b_p = (filter->ratio * thr_p - G_MAXINT16) / (thr_p - G_MAXINT16);
426   c_p = thr_p * (1 - b_p - a_p * thr_p);
427   a_n = (1 - filter->ratio) / (2 * (thr_n - G_MININT16));
428   b_n = (filter->ratio * thr_n - G_MININT16) / (thr_n - G_MININT16);
429   c_n = thr_n * (1 - b_n - a_n * thr_n);
430
431   for (; num_samples; num_samples--) {
432     val = *data;
433
434     if (val > thr_p) {
435       val = a_p * val * val + b_p * val + c_p;
436     } else if (val < thr_n) {
437       val = a_n * val * val + b_n * val + c_n;
438     }
439     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
440   }
441 }
442
443 static void
444 gst_audio_dynamic_transform_soft_knee_compressor_float (GstAudioDynamic *
445     filter, gfloat * data, guint num_samples)
446 {
447   gdouble val;
448   gdouble threshold = filter->threshold;
449   gdouble a_p, b_p, c_p;
450   gdouble a_n, b_n, c_n;
451
452   /* Nothing to do for us if ratio == 1.0.
453    * As float values can be above 1.0 we have to do something
454    * if threshold is greater than 1.0. */
455   if (filter->ratio == 1.0)
456     return;
457
458   /* We build a 2nd degree polynomial here for
459    * values greater than threshold or small than
460    * -threshold with:
461    * f(t) = t, f'(t) = 1, f'(m) = r
462    * =>
463    * a = (1-r)/(2*(t-m))
464    * b = (r*t - m)/(t-m)
465    * c = t * (1 - b - a*t)
466    * f(x) = ax^2 + bx + c
467    */
468
469   /* FIXME: If treshold is the same as the maximum
470    * we need to raise it a bit to prevent
471    * division by zero. */
472   if (threshold == 1.0)
473     threshold = 1.0 + 0.00001;
474
475   a_p = (1.0 - filter->ratio) / (2.0 * (threshold - 1.0));
476   b_p = (filter->ratio * threshold - 1.0) / (threshold - 1.0);
477   c_p = threshold * (1.0 - b_p - a_p * threshold);
478   a_n = (1.0 - filter->ratio) / (2.0 * (-threshold + 1.0));
479   b_n = (-filter->ratio * threshold + 1.0) / (-threshold + 1.0);
480   c_n = -threshold * (1.0 - b_n + a_n * threshold);
481
482   for (; num_samples; num_samples--) {
483     val = *data;
484
485     if (val > 1.0) {
486       val = 1.0 + (val - 1.0) * filter->ratio;
487     } else if (val > threshold) {
488       val = a_p * val * val + b_p * val + c_p;
489     } else if (val < -1.0) {
490       val = -1.0 + (val + 1.0) * filter->ratio;
491     } else if (val < -threshold) {
492       val = a_n * val * val + b_n * val + c_n;
493     }
494     *data++ = (gfloat) val;
495   }
496 }
497
498 static void
499 gst_audio_dynamic_transform_hard_knee_expander_int (GstAudioDynamic * filter,
500     gint16 * data, guint num_samples)
501 {
502   glong val;
503   glong thr_p = filter->threshold * G_MAXINT16;
504   glong thr_n = filter->threshold * G_MININT16;
505   gdouble zero_p, zero_n;
506
507   /* Nothing to do for us here if threshold equals 0.0
508    * or ratio equals 1.0 */
509   if (filter->threshold == 0.0 || filter->ratio == 1.0)
510     return;
511
512   /* zero crossing of our function */
513   if (filter->ratio != 0.0) {
514     zero_p = thr_p - thr_p / filter->ratio;
515     zero_n = thr_n - thr_n / filter->ratio;
516   } else {
517     zero_p = zero_n = 0.0;
518   }
519
520   if (zero_p < 0.0)
521     zero_p = 0.0;
522   if (zero_n > 0.0)
523     zero_n = 0.0;
524
525   for (; num_samples; num_samples--) {
526     val = *data;
527
528     if (val < thr_p && val > zero_p) {
529       val = filter->ratio * val + thr_p * (1 - filter->ratio);
530     } else if ((val <= zero_p && val > 0) || (val >= zero_n && val < 0)) {
531       val = 0;
532     } else if (val > thr_n && val < zero_n) {
533       val = filter->ratio * val + thr_n * (1 - filter->ratio);
534     }
535     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
536   }
537 }
538
539 static void
540 gst_audio_dynamic_transform_hard_knee_expander_float (GstAudioDynamic * filter,
541     gfloat * data, guint num_samples)
542 {
543   gdouble val, threshold = filter->threshold, zero;
544
545   /* Nothing to do for us here if threshold equals 0.0
546    * or ratio equals 1.0 */
547   if (filter->threshold == 0.0 || filter->ratio == 1.0)
548     return;
549
550   /* zero crossing of our function */
551   if (filter->ratio != 0.0)
552     zero = threshold - threshold / filter->ratio;
553   else
554     zero = 0.0;
555
556   if (zero < 0.0)
557     zero = 0.0;
558
559   for (; num_samples; num_samples--) {
560     val = *data;
561
562     if (val < threshold && val > zero) {
563       val = filter->ratio * val + threshold * (1.0 - filter->ratio);
564     } else if ((val <= zero && val > 0.0) || (val >= -zero && val < 0.0)) {
565       val = 0.0;
566     } else if (val > -threshold && val < -zero) {
567       val = filter->ratio * val - threshold * (1.0 - filter->ratio);
568     }
569     *data++ = (gfloat) val;
570   }
571 }
572
573 static void
574 gst_audio_dynamic_transform_soft_knee_expander_int (GstAudioDynamic * filter,
575     gint16 * data, guint num_samples)
576 {
577   glong val;
578   glong thr_p = filter->threshold * G_MAXINT16;
579   glong thr_n = filter->threshold * G_MININT16;
580   gdouble zero_p, zero_n;
581   gdouble a_p, b_p, c_p;
582   gdouble a_n, b_n, c_n;
583
584   /* Nothing to do for us here if threshold equals 0.0
585    * or ratio equals 1.0 */
586   if (filter->threshold == 0.0 || filter->ratio == 1.0)
587     return;
588
589   /* zero crossing of our function */
590   zero_p = (thr_p * (filter->ratio - 1.0)) / (1.0 + filter->ratio);
591   zero_n = (thr_n * (filter->ratio - 1.0)) / (1.0 + filter->ratio);
592
593   if (zero_p < 0.0)
594     zero_p = 0.0;
595   if (zero_n > 0.0)
596     zero_n = 0.0;
597
598   /* shouldn't happen as this would only happen
599    * with threshold == 0.0 */
600   g_assert (thr_p != 0);
601   g_assert (thr_n != 0);
602
603   /* We build a 2n degree polynomial here for values between
604    * 0 and threshold or 0 and -threshold with:
605    * f(t) = t, f'(t) = 1, f(z) = 0, f'(z) = r
606    * z between 0 and t
607    * =>
608    * a = (1 - r^2) / (4 * t)
609    * b = (1 + r^2) / 2
610    * c = t * (1.0 - b - a*t)
611    * f(x) = ax^2 + bx + c */
612   a_p = (1.0 - filter->ratio * filter->ratio) / (4.0 * thr_p);
613   b_p = (1.0 + filter->ratio * filter->ratio) / 2.0;
614   c_p = thr_p * (1.0 - b_p - a_p * thr_p);
615   a_n = (1.0 - filter->ratio * filter->ratio) / (4.0 * thr_n);
616   b_n = (1.0 + filter->ratio * filter->ratio) / 2.0;
617   c_n = thr_n * (1.0 - b_n - a_n * thr_n);
618
619   for (; num_samples; num_samples--) {
620     val = *data;
621
622     if (val < thr_p && val > zero_p) {
623       val = a_p * val * val + b_p * val + c_p;
624     } else if ((val <= zero_p && val > 0) || (val >= zero_n && val < 0)) {
625       val = 0;
626     } else if (val > thr_n && val < zero_n) {
627       val = a_n * val * val + b_n * val + c_n;
628     }
629     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
630   }
631 }
632
633 static void
634 gst_audio_dynamic_transform_soft_knee_expander_float (GstAudioDynamic * filter,
635     gfloat * data, guint num_samples)
636 {
637   gdouble val;
638   gdouble threshold = filter->threshold;
639   gdouble zero;
640   gdouble a_p, b_p, c_p;
641   gdouble a_n, b_n, c_n;
642
643   /* Nothing to do for us here if threshold equals 0.0
644    * or ratio equals 1.0 */
645   if (filter->threshold == 0.0 || filter->ratio == 1.0)
646     return;
647
648   /* zero crossing of our function */
649   zero = (threshold * (filter->ratio - 1.0)) / (1.0 + filter->ratio);
650
651   if (zero < 0.0)
652     zero = 0.0;
653
654   /* shouldn't happen as this only happens with
655    * threshold == 0.0 */
656   g_assert (threshold != 0.0);
657
658   /* We build a 2n degree polynomial here for values between
659    * 0 and threshold or 0 and -threshold with:
660    * f(t) = t, f'(t) = 1, f(z) = 0, f'(z) = r
661    * z between 0 and t
662    * =>
663    * a = (1 - r^2) / (4 * t)
664    * b = (1 + r^2) / 2
665    * c = t * (1.0 - b - a*t)
666    * f(x) = ax^2 + bx + c */
667   a_p = (1.0 - filter->ratio * filter->ratio) / (4.0 * threshold);
668   b_p = (1.0 + filter->ratio * filter->ratio) / 2.0;
669   c_p = threshold * (1.0 - b_p - a_p * threshold);
670   a_n = (1.0 - filter->ratio * filter->ratio) / (-4.0 * threshold);
671   b_n = (1.0 + filter->ratio * filter->ratio) / 2.0;
672   c_n = -threshold * (1.0 - b_n + a_n * threshold);
673
674   for (; num_samples; num_samples--) {
675     val = *data;
676
677     if (val < threshold && val > zero) {
678       val = a_p * val * val + b_p * val + c_p;
679     } else if ((val <= zero && val > 0.0) || (val >= -zero && val < 0.0)) {
680       val = 0.0;
681     } else if (val > -threshold && val < -zero) {
682       val = a_n * val * val + b_n * val + c_n;
683     }
684     *data++ = (gfloat) val;
685   }
686 }
687
688 /* GstBaseTransform vmethod implementations */
689 static GstFlowReturn
690 gst_audio_dynamic_transform_ip (GstBaseTransform * base, GstBuffer * buf)
691 {
692   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base);
693   guint num_samples;
694   GstClockTime timestamp, stream_time;
695   guint8 *data;
696   gsize size;
697
698   timestamp = GST_BUFFER_TIMESTAMP (buf);
699   stream_time =
700       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
701
702   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
703       GST_TIME_ARGS (timestamp));
704
705   if (GST_CLOCK_TIME_IS_VALID (stream_time))
706     gst_object_sync_values (G_OBJECT (filter), stream_time);
707
708   if (gst_base_transform_is_passthrough (base) ||
709       G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
710     return GST_FLOW_OK;
711
712   data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
713   num_samples = size / (GST_AUDIO_FILTER (filter)->format.width / 8);
714
715   filter->process (filter, data, num_samples);
716
717   gst_buffer_unmap (buf, data, size);
718
719   return GST_FLOW_OK;
720 }