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