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