audiofx: adjust to changed semantics of audiofilter _setup method
[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
50 #include "audiodynamic.h"
51
52 #define GST_CAT_DEFAULT gst_audio_dynamic_debug
53 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
54
55 /* Filter signals and args */
56 enum
57 {
58   /* FILL ME */
59   LAST_SIGNAL
60 };
61
62 enum
63 {
64   PROP_0,
65   PROP_CHARACTERISTICS,
66   PROP_MODE,
67   PROP_THRESHOLD,
68   PROP_RATIO
69 };
70
71 #define ALLOWED_CAPS \
72     "audio/x-raw,"                                                \
73     " format=(string) {"GST_AUDIO_NE(S16)","GST_AUDIO_NE(F32)"}," \
74     " rate=(int)[1,MAX],"                                         \
75     " channels=(int)[1,MAX],"                                     \
76     " layout=(string) {interleaved, non-interleaved}"
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     const GstAudioInfo * info)
186 {
187   gint func_index;
188
189   if (GST_AUDIO_INFO_FORMAT (info) == GST_AUDIO_FORMAT_UNKNOWN)
190     return FALSE;
191
192   func_index = (filter->mode == MODE_COMPRESSOR) ? 0 : 4;
193   func_index += (filter->characteristics == CHARACTERISTICS_HARD_KNEE) ? 0 : 2;
194   func_index += (GST_AUDIO_INFO_FORMAT (info) == 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           GST_AUDIO_FILTER_INFO (filter));
285       break;
286     case PROP_MODE:
287       filter->mode = g_value_get_enum (value);
288       gst_audio_dynamic_set_process_function (filter,
289           GST_AUDIO_FILTER_INFO (filter));
290       break;
291     case PROP_THRESHOLD:
292       filter->threshold = g_value_get_float (value);
293       break;
294     case PROP_RATIO:
295       filter->ratio = g_value_get_float (value);
296       break;
297     default:
298       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299       break;
300   }
301 }
302
303 static void
304 gst_audio_dynamic_get_property (GObject * object, guint prop_id,
305     GValue * value, GParamSpec * pspec)
306 {
307   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object);
308
309   switch (prop_id) {
310     case PROP_CHARACTERISTICS:
311       g_value_set_enum (value, filter->characteristics);
312       break;
313     case PROP_MODE:
314       g_value_set_enum (value, filter->mode);
315       break;
316     case PROP_THRESHOLD:
317       g_value_set_float (value, filter->threshold);
318       break;
319     case PROP_RATIO:
320       g_value_set_float (value, filter->ratio);
321       break;
322     default:
323       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324       break;
325   }
326 }
327
328 /* GstAudioFilter vmethod implementations */
329
330 static gboolean
331 gst_audio_dynamic_setup (GstAudioFilter * base, const GstAudioInfo * info)
332 {
333   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base);
334   gboolean ret = TRUE;
335
336   ret = gst_audio_dynamic_set_process_function (filter, info);
337
338   return ret;
339 }
340
341 static void
342 gst_audio_dynamic_transform_hard_knee_compressor_int (GstAudioDynamic * filter,
343     gint16 * data, guint num_samples)
344 {
345   glong val;
346   glong thr_p = filter->threshold * G_MAXINT16;
347   glong thr_n = filter->threshold * G_MININT16;
348
349   /* Nothing to do for us if ratio is 1.0 or if the threshold
350    * equals 1.0. */
351   if (filter->threshold == 1.0 || filter->ratio == 1.0)
352     return;
353
354   for (; num_samples; num_samples--) {
355     val = *data;
356
357     if (val > thr_p) {
358       val = thr_p + (val - thr_p) * filter->ratio;
359     } else if (val < thr_n) {
360       val = thr_n + (val - thr_n) * filter->ratio;
361     }
362     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
363   }
364 }
365
366 static void
367 gst_audio_dynamic_transform_hard_knee_compressor_float (GstAudioDynamic *
368     filter, gfloat * data, guint num_samples)
369 {
370   gdouble val, threshold = filter->threshold;
371
372   /* Nothing to do for us if ratio == 1.0.
373    * As float values can be above 1.0 we have to do something
374    * if threshold is greater than 1.0. */
375   if (filter->ratio == 1.0)
376     return;
377
378   for (; num_samples; num_samples--) {
379     val = *data;
380
381     if (val > threshold) {
382       val = threshold + (val - threshold) * filter->ratio;
383     } else if (val < -threshold) {
384       val = -threshold + (val + threshold) * filter->ratio;
385     }
386     *data++ = (gfloat) val;
387   }
388 }
389
390 static void
391 gst_audio_dynamic_transform_soft_knee_compressor_int (GstAudioDynamic * filter,
392     gint16 * data, guint num_samples)
393 {
394   glong val;
395   glong thr_p = filter->threshold * G_MAXINT16;
396   glong thr_n = filter->threshold * G_MININT16;
397   gdouble a_p, b_p, c_p;
398   gdouble a_n, b_n, c_n;
399
400   /* Nothing to do for us if ratio is 1.0 or if the threshold
401    * equals 1.0. */
402   if (filter->threshold == 1.0 || filter->ratio == 1.0)
403     return;
404
405   /* We build a 2nd degree polynomial here for
406    * values greater than threshold or small than
407    * -threshold with:
408    * f(t) = t, f'(t) = 1, f'(m) = r
409    * =>
410    * a = (1-r)/(2*(t-m))
411    * b = (r*t - m)/(t-m)
412    * c = t * (1 - b - a*t)
413    * f(x) = ax^2 + bx + c
414    */
415
416   /* shouldn't happen because this would only be the case
417    * for threshold == 1.0 which we catch above */
418   g_assert (thr_p - G_MAXINT16 != 0);
419   g_assert (thr_n - G_MININT != 0);
420
421   a_p = (1 - filter->ratio) / (2 * (thr_p - G_MAXINT16));
422   b_p = (filter->ratio * thr_p - G_MAXINT16) / (thr_p - G_MAXINT16);
423   c_p = thr_p * (1 - b_p - a_p * thr_p);
424   a_n = (1 - filter->ratio) / (2 * (thr_n - G_MININT16));
425   b_n = (filter->ratio * thr_n - G_MININT16) / (thr_n - G_MININT16);
426   c_n = thr_n * (1 - b_n - a_n * thr_n);
427
428   for (; num_samples; num_samples--) {
429     val = *data;
430
431     if (val > thr_p) {
432       val = a_p * val * val + b_p * val + c_p;
433     } else if (val < thr_n) {
434       val = a_n * val * val + b_n * val + c_n;
435     }
436     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
437   }
438 }
439
440 static void
441 gst_audio_dynamic_transform_soft_knee_compressor_float (GstAudioDynamic *
442     filter, gfloat * data, guint num_samples)
443 {
444   gdouble val;
445   gdouble threshold = filter->threshold;
446   gdouble a_p, b_p, c_p;
447   gdouble a_n, b_n, c_n;
448
449   /* Nothing to do for us if ratio == 1.0.
450    * As float values can be above 1.0 we have to do something
451    * if threshold is greater than 1.0. */
452   if (filter->ratio == 1.0)
453     return;
454
455   /* We build a 2nd degree polynomial here for
456    * values greater than threshold or small than
457    * -threshold with:
458    * f(t) = t, f'(t) = 1, f'(m) = r
459    * =>
460    * a = (1-r)/(2*(t-m))
461    * b = (r*t - m)/(t-m)
462    * c = t * (1 - b - a*t)
463    * f(x) = ax^2 + bx + c
464    */
465
466   /* FIXME: If treshold is the same as the maximum
467    * we need to raise it a bit to prevent
468    * division by zero. */
469   if (threshold == 1.0)
470     threshold = 1.0 + 0.00001;
471
472   a_p = (1.0 - filter->ratio) / (2.0 * (threshold - 1.0));
473   b_p = (filter->ratio * threshold - 1.0) / (threshold - 1.0);
474   c_p = threshold * (1.0 - b_p - a_p * threshold);
475   a_n = (1.0 - filter->ratio) / (2.0 * (-threshold + 1.0));
476   b_n = (-filter->ratio * threshold + 1.0) / (-threshold + 1.0);
477   c_n = -threshold * (1.0 - b_n + a_n * threshold);
478
479   for (; num_samples; num_samples--) {
480     val = *data;
481
482     if (val > 1.0) {
483       val = 1.0 + (val - 1.0) * filter->ratio;
484     } else if (val > threshold) {
485       val = a_p * val * val + b_p * val + c_p;
486     } else if (val < -1.0) {
487       val = -1.0 + (val + 1.0) * filter->ratio;
488     } else if (val < -threshold) {
489       val = a_n * val * val + b_n * val + c_n;
490     }
491     *data++ = (gfloat) val;
492   }
493 }
494
495 static void
496 gst_audio_dynamic_transform_hard_knee_expander_int (GstAudioDynamic * filter,
497     gint16 * data, guint num_samples)
498 {
499   glong val;
500   glong thr_p = filter->threshold * G_MAXINT16;
501   glong thr_n = filter->threshold * G_MININT16;
502   gdouble zero_p, zero_n;
503
504   /* Nothing to do for us here if threshold equals 0.0
505    * or ratio equals 1.0 */
506   if (filter->threshold == 0.0 || filter->ratio == 1.0)
507     return;
508
509   /* zero crossing of our function */
510   if (filter->ratio != 0.0) {
511     zero_p = thr_p - thr_p / filter->ratio;
512     zero_n = thr_n - thr_n / filter->ratio;
513   } else {
514     zero_p = zero_n = 0.0;
515   }
516
517   if (zero_p < 0.0)
518     zero_p = 0.0;
519   if (zero_n > 0.0)
520     zero_n = 0.0;
521
522   for (; num_samples; num_samples--) {
523     val = *data;
524
525     if (val < thr_p && val > zero_p) {
526       val = filter->ratio * val + thr_p * (1 - filter->ratio);
527     } else if ((val <= zero_p && val > 0) || (val >= zero_n && val < 0)) {
528       val = 0;
529     } else if (val > thr_n && val < zero_n) {
530       val = filter->ratio * val + thr_n * (1 - filter->ratio);
531     }
532     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
533   }
534 }
535
536 static void
537 gst_audio_dynamic_transform_hard_knee_expander_float (GstAudioDynamic * filter,
538     gfloat * data, guint num_samples)
539 {
540   gdouble val, threshold = filter->threshold, zero;
541
542   /* Nothing to do for us here if threshold equals 0.0
543    * or ratio equals 1.0 */
544   if (filter->threshold == 0.0 || filter->ratio == 1.0)
545     return;
546
547   /* zero crossing of our function */
548   if (filter->ratio != 0.0)
549     zero = threshold - threshold / filter->ratio;
550   else
551     zero = 0.0;
552
553   if (zero < 0.0)
554     zero = 0.0;
555
556   for (; num_samples; num_samples--) {
557     val = *data;
558
559     if (val < threshold && val > zero) {
560       val = filter->ratio * val + threshold * (1.0 - filter->ratio);
561     } else if ((val <= zero && val > 0.0) || (val >= -zero && val < 0.0)) {
562       val = 0.0;
563     } else if (val > -threshold && val < -zero) {
564       val = filter->ratio * val - threshold * (1.0 - filter->ratio);
565     }
566     *data++ = (gfloat) val;
567   }
568 }
569
570 static void
571 gst_audio_dynamic_transform_soft_knee_expander_int (GstAudioDynamic * filter,
572     gint16 * data, guint num_samples)
573 {
574   glong val;
575   glong thr_p = filter->threshold * G_MAXINT16;
576   glong thr_n = filter->threshold * G_MININT16;
577   gdouble zero_p, zero_n;
578   gdouble a_p, b_p, c_p;
579   gdouble a_n, b_n, c_n;
580   gdouble r2;
581
582   /* Nothing to do for us here if threshold equals 0.0
583    * or ratio equals 1.0 */
584   if (filter->threshold == 0.0 || filter->ratio == 1.0)
585     return;
586
587   /* zero crossing of our function */
588   zero_p = (thr_p * (filter->ratio - 1.0)) / (1.0 + filter->ratio);
589   zero_n = (thr_n * (filter->ratio - 1.0)) / (1.0 + filter->ratio);
590
591   if (zero_p < 0.0)
592     zero_p = 0.0;
593   if (zero_n > 0.0)
594     zero_n = 0.0;
595
596   /* shouldn't happen as this would only happen
597    * with threshold == 0.0 */
598   g_assert (thr_p != 0);
599   g_assert (thr_n != 0);
600
601   /* We build a 2n degree polynomial here for values between
602    * 0 and threshold or 0 and -threshold with:
603    * f(t) = t, f'(t) = 1, f(z) = 0, f'(z) = r
604    * z between 0 and t
605    * =>
606    * a = (1 - r^2) / (4 * t)
607    * b = (1 + r^2) / 2
608    * c = t * (1.0 - b - a*t)
609    * f(x) = ax^2 + bx + c */
610   r2 = filter->ratio * filter->ratio;
611   a_p = (1.0 - r2) / (4.0 * thr_p);
612   b_p = (1.0 + r2) / 2.0;
613   c_p = thr_p * (1.0 - b_p - a_p * thr_p);
614   a_n = (1.0 - r2) / (4.0 * thr_n);
615   b_n = (1.0 + r2) / 2.0;
616   c_n = thr_n * (1.0 - b_n - a_n * thr_n);
617
618   for (; num_samples; num_samples--) {
619     val = *data;
620
621     if (val < thr_p && val > zero_p) {
622       val = a_p * val * val + b_p * val + c_p;
623     } else if ((val <= zero_p && val > 0) || (val >= zero_n && val < 0)) {
624       val = 0;
625     } else if (val > thr_n && val < zero_n) {
626       val = a_n * val * val + b_n * val + c_n;
627     }
628     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
629   }
630 }
631
632 static void
633 gst_audio_dynamic_transform_soft_knee_expander_float (GstAudioDynamic * filter,
634     gfloat * data, guint num_samples)
635 {
636   gdouble val;
637   gdouble threshold = filter->threshold;
638   gdouble zero;
639   gdouble a_p, b_p, c_p;
640   gdouble a_n, b_n, c_n;
641   gdouble r2;
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   r2 = filter->ratio * filter->ratio;
668   a_p = (1.0 - r2) / (4.0 * threshold);
669   b_p = (1.0 + r2) / 2.0;
670   c_p = threshold * (1.0 - b_p - a_p * threshold);
671   a_n = (1.0 - r2) / (-4.0 * threshold);
672   b_n = (1.0 + r2) / 2.0;
673   c_n = -threshold * (1.0 - b_n + a_n * threshold);
674
675   for (; num_samples; num_samples--) {
676     val = *data;
677
678     if (val < threshold && val > zero) {
679       val = a_p * val * val + b_p * val + c_p;
680     } else if ((val <= zero && val > 0.0) || (val >= -zero && val < 0.0)) {
681       val = 0.0;
682     } else if (val > -threshold && val < -zero) {
683       val = a_n * val * val + b_n * val + c_n;
684     }
685     *data++ = (gfloat) val;
686   }
687 }
688
689 /* GstBaseTransform vmethod implementations */
690 static GstFlowReturn
691 gst_audio_dynamic_transform_ip (GstBaseTransform * base, GstBuffer * buf)
692 {
693   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base);
694   guint num_samples;
695   GstClockTime timestamp, stream_time;
696   GstMapInfo map;
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 (GST_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   gst_buffer_map (buf, &map, GST_MAP_READWRITE);
713   num_samples = map.size / GST_AUDIO_FILTER_BPS (filter);
714
715   filter->process (filter, map.data, num_samples);
716
717   gst_buffer_unmap (buf, &map);
718
719   return GST_FLOW_OK;
720 }