scaletempo: replace gst_element_class_set_details_simple with gst_element_class_set_m...
[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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, 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-1.0 audiotestsrc wave=saw ! audiodynamic characteristics=soft-knee mode=compressor threshold=0.5 rate=0.5 ! alsasink
33  * gst-launch-1.0 filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiodynamic characteristics=hard-knee mode=expander threshold=0.2 rate=4.0 ! alsasink
34  * gst-launch-1.0 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_static_metadata (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
260   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
261       GST_DEBUG_FUNCPTR (gst_audio_dynamic_transform_ip);
262   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip_on_passthrough = FALSE;
263 }
264
265 static void
266 gst_audio_dynamic_init (GstAudioDynamic * filter)
267 {
268   filter->ratio = 1.0;
269   filter->threshold = 0.0;
270   filter->characteristics = CHARACTERISTICS_HARD_KNEE;
271   filter->mode = MODE_COMPRESSOR;
272   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
273   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
274 }
275
276 static void
277 gst_audio_dynamic_set_property (GObject * object, guint prop_id,
278     const GValue * value, GParamSpec * pspec)
279 {
280   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object);
281
282   switch (prop_id) {
283     case PROP_CHARACTERISTICS:
284       filter->characteristics = g_value_get_enum (value);
285       gst_audio_dynamic_set_process_function (filter,
286           GST_AUDIO_FILTER_INFO (filter));
287       break;
288     case PROP_MODE:
289       filter->mode = g_value_get_enum (value);
290       gst_audio_dynamic_set_process_function (filter,
291           GST_AUDIO_FILTER_INFO (filter));
292       break;
293     case PROP_THRESHOLD:
294       filter->threshold = g_value_get_float (value);
295       break;
296     case PROP_RATIO:
297       filter->ratio = g_value_get_float (value);
298       break;
299     default:
300       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301       break;
302   }
303 }
304
305 static void
306 gst_audio_dynamic_get_property (GObject * object, guint prop_id,
307     GValue * value, GParamSpec * pspec)
308 {
309   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object);
310
311   switch (prop_id) {
312     case PROP_CHARACTERISTICS:
313       g_value_set_enum (value, filter->characteristics);
314       break;
315     case PROP_MODE:
316       g_value_set_enum (value, filter->mode);
317       break;
318     case PROP_THRESHOLD:
319       g_value_set_float (value, filter->threshold);
320       break;
321     case PROP_RATIO:
322       g_value_set_float (value, filter->ratio);
323       break;
324     default:
325       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
326       break;
327   }
328 }
329
330 /* GstAudioFilter vmethod implementations */
331
332 static gboolean
333 gst_audio_dynamic_setup (GstAudioFilter * base, const GstAudioInfo * info)
334 {
335   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base);
336   gboolean ret = TRUE;
337
338   ret = gst_audio_dynamic_set_process_function (filter, info);
339
340   return ret;
341 }
342
343 static void
344 gst_audio_dynamic_transform_hard_knee_compressor_int (GstAudioDynamic * filter,
345     gint16 * data, guint num_samples)
346 {
347   glong val;
348   glong thr_p = filter->threshold * G_MAXINT16;
349   glong thr_n = filter->threshold * G_MININT16;
350
351   /* Nothing to do for us if ratio is 1.0 or if the threshold
352    * equals 1.0. */
353   if (filter->threshold == 1.0 || filter->ratio == 1.0)
354     return;
355
356   for (; num_samples; num_samples--) {
357     val = *data;
358
359     if (val > thr_p) {
360       val = thr_p + (val - thr_p) * filter->ratio;
361     } else if (val < thr_n) {
362       val = thr_n + (val - thr_n) * filter->ratio;
363     }
364     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
365   }
366 }
367
368 static void
369 gst_audio_dynamic_transform_hard_knee_compressor_float (GstAudioDynamic *
370     filter, gfloat * data, guint num_samples)
371 {
372   gdouble val, threshold = filter->threshold;
373
374   /* Nothing to do for us if ratio == 1.0.
375    * As float values can be above 1.0 we have to do something
376    * if threshold is greater than 1.0. */
377   if (filter->ratio == 1.0)
378     return;
379
380   for (; num_samples; num_samples--) {
381     val = *data;
382
383     if (val > threshold) {
384       val = threshold + (val - threshold) * filter->ratio;
385     } else if (val < -threshold) {
386       val = -threshold + (val + threshold) * filter->ratio;
387     }
388     *data++ = (gfloat) val;
389   }
390 }
391
392 static void
393 gst_audio_dynamic_transform_soft_knee_compressor_int (GstAudioDynamic * filter,
394     gint16 * data, guint num_samples)
395 {
396   glong val;
397   glong thr_p = filter->threshold * G_MAXINT16;
398   glong thr_n = filter->threshold * G_MININT16;
399   gdouble a_p, b_p, c_p;
400   gdouble a_n, b_n, c_n;
401
402   /* Nothing to do for us if ratio is 1.0 or if the threshold
403    * equals 1.0. */
404   if (filter->threshold == 1.0 || filter->ratio == 1.0)
405     return;
406
407   /* We build a 2nd degree polynomial here for
408    * values greater than threshold or small than
409    * -threshold with:
410    * f(t) = t, f'(t) = 1, f'(m) = r
411    * =>
412    * a = (1-r)/(2*(t-m))
413    * b = (r*t - m)/(t-m)
414    * c = t * (1 - b - a*t)
415    * f(x) = ax^2 + bx + c
416    */
417
418   /* shouldn't happen because this would only be the case
419    * for threshold == 1.0 which we catch above */
420   g_assert (thr_p - G_MAXINT16 != 0);
421   g_assert (thr_n - G_MININT != 0);
422
423   a_p = (1 - filter->ratio) / (2 * (thr_p - G_MAXINT16));
424   b_p = (filter->ratio * thr_p - G_MAXINT16) / (thr_p - G_MAXINT16);
425   c_p = thr_p * (1 - b_p - a_p * thr_p);
426   a_n = (1 - filter->ratio) / (2 * (thr_n - G_MININT16));
427   b_n = (filter->ratio * thr_n - G_MININT16) / (thr_n - G_MININT16);
428   c_n = thr_n * (1 - b_n - a_n * thr_n);
429
430   for (; num_samples; num_samples--) {
431     val = *data;
432
433     if (val > thr_p) {
434       val = a_p * val * val + b_p * val + c_p;
435     } else if (val < thr_n) {
436       val = a_n * val * val + b_n * val + c_n;
437     }
438     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
439   }
440 }
441
442 static void
443 gst_audio_dynamic_transform_soft_knee_compressor_float (GstAudioDynamic *
444     filter, gfloat * data, guint num_samples)
445 {
446   gdouble val;
447   gdouble threshold = filter->threshold;
448   gdouble a_p, b_p, c_p;
449   gdouble a_n, b_n, c_n;
450
451   /* Nothing to do for us if ratio == 1.0.
452    * As float values can be above 1.0 we have to do something
453    * if threshold is greater than 1.0. */
454   if (filter->ratio == 1.0)
455     return;
456
457   /* We build a 2nd degree polynomial here for
458    * values greater than threshold or small than
459    * -threshold with:
460    * f(t) = t, f'(t) = 1, f'(m) = r
461    * =>
462    * a = (1-r)/(2*(t-m))
463    * b = (r*t - m)/(t-m)
464    * c = t * (1 - b - a*t)
465    * f(x) = ax^2 + bx + c
466    */
467
468   /* FIXME: If treshold is the same as the maximum
469    * we need to raise it a bit to prevent
470    * division by zero. */
471   if (threshold == 1.0)
472     threshold = 1.0 + 0.00001;
473
474   a_p = (1.0 - filter->ratio) / (2.0 * (threshold - 1.0));
475   b_p = (filter->ratio * threshold - 1.0) / (threshold - 1.0);
476   c_p = threshold * (1.0 - b_p - a_p * threshold);
477   a_n = (1.0 - filter->ratio) / (2.0 * (-threshold + 1.0));
478   b_n = (-filter->ratio * threshold + 1.0) / (-threshold + 1.0);
479   c_n = -threshold * (1.0 - b_n + a_n * threshold);
480
481   for (; num_samples; num_samples--) {
482     val = *data;
483
484     if (val > 1.0) {
485       val = 1.0 + (val - 1.0) * filter->ratio;
486     } else if (val > threshold) {
487       val = a_p * val * val + b_p * val + c_p;
488     } else if (val < -1.0) {
489       val = -1.0 + (val + 1.0) * filter->ratio;
490     } else if (val < -threshold) {
491       val = a_n * val * val + b_n * val + c_n;
492     }
493     *data++ = (gfloat) val;
494   }
495 }
496
497 static void
498 gst_audio_dynamic_transform_hard_knee_expander_int (GstAudioDynamic * filter,
499     gint16 * data, guint num_samples)
500 {
501   glong val;
502   glong thr_p = filter->threshold * G_MAXINT16;
503   glong thr_n = filter->threshold * G_MININT16;
504   gdouble zero_p, zero_n;
505
506   /* Nothing to do for us here if threshold equals 0.0
507    * or ratio equals 1.0 */
508   if (filter->threshold == 0.0 || filter->ratio == 1.0)
509     return;
510
511   /* zero crossing of our function */
512   if (filter->ratio != 0.0) {
513     zero_p = thr_p - thr_p / filter->ratio;
514     zero_n = thr_n - thr_n / filter->ratio;
515   } else {
516     zero_p = zero_n = 0.0;
517   }
518
519   if (zero_p < 0.0)
520     zero_p = 0.0;
521   if (zero_n > 0.0)
522     zero_n = 0.0;
523
524   for (; num_samples; num_samples--) {
525     val = *data;
526
527     if (val < thr_p && val > zero_p) {
528       val = filter->ratio * val + thr_p * (1 - filter->ratio);
529     } else if ((val <= zero_p && val > 0) || (val >= zero_n && val < 0)) {
530       val = 0;
531     } else if (val > thr_n && val < zero_n) {
532       val = filter->ratio * val + thr_n * (1 - filter->ratio);
533     }
534     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
535   }
536 }
537
538 static void
539 gst_audio_dynamic_transform_hard_knee_expander_float (GstAudioDynamic * filter,
540     gfloat * data, guint num_samples)
541 {
542   gdouble val, threshold = filter->threshold, zero;
543
544   /* Nothing to do for us here if threshold equals 0.0
545    * or ratio equals 1.0 */
546   if (filter->threshold == 0.0 || filter->ratio == 1.0)
547     return;
548
549   /* zero crossing of our function */
550   if (filter->ratio != 0.0)
551     zero = threshold - threshold / filter->ratio;
552   else
553     zero = 0.0;
554
555   if (zero < 0.0)
556     zero = 0.0;
557
558   for (; num_samples; num_samples--) {
559     val = *data;
560
561     if (val < threshold && val > zero) {
562       val = filter->ratio * val + threshold * (1.0 - filter->ratio);
563     } else if ((val <= zero && val > 0.0) || (val >= -zero && val < 0.0)) {
564       val = 0.0;
565     } else if (val > -threshold && val < -zero) {
566       val = filter->ratio * val - threshold * (1.0 - filter->ratio);
567     }
568     *data++ = (gfloat) val;
569   }
570 }
571
572 static void
573 gst_audio_dynamic_transform_soft_knee_expander_int (GstAudioDynamic * filter,
574     gint16 * data, guint num_samples)
575 {
576   glong val;
577   glong thr_p = filter->threshold * G_MAXINT16;
578   glong thr_n = filter->threshold * G_MININT16;
579   gdouble zero_p, zero_n;
580   gdouble a_p, b_p, c_p;
581   gdouble a_n, b_n, c_n;
582   gdouble r2;
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   r2 = filter->ratio * filter->ratio;
613   a_p = (1.0 - r2) / (4.0 * thr_p);
614   b_p = (1.0 + r2) / 2.0;
615   c_p = thr_p * (1.0 - b_p - a_p * thr_p);
616   a_n = (1.0 - r2) / (4.0 * thr_n);
617   b_n = (1.0 + r2) / 2.0;
618   c_n = thr_n * (1.0 - b_n - a_n * thr_n);
619
620   for (; num_samples; num_samples--) {
621     val = *data;
622
623     if (val < thr_p && val > zero_p) {
624       val = a_p * val * val + b_p * val + c_p;
625     } else if ((val <= zero_p && val > 0) || (val >= zero_n && val < 0)) {
626       val = 0;
627     } else if (val > thr_n && val < zero_n) {
628       val = a_n * val * val + b_n * val + c_n;
629     }
630     *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
631   }
632 }
633
634 static void
635 gst_audio_dynamic_transform_soft_knee_expander_float (GstAudioDynamic * filter,
636     gfloat * data, guint num_samples)
637 {
638   gdouble val;
639   gdouble threshold = filter->threshold;
640   gdouble zero;
641   gdouble a_p, b_p, c_p;
642   gdouble a_n, b_n, c_n;
643   gdouble r2;
644
645   /* Nothing to do for us here if threshold equals 0.0
646    * or ratio equals 1.0 */
647   if (filter->threshold == 0.0 || filter->ratio == 1.0)
648     return;
649
650   /* zero crossing of our function */
651   zero = (threshold * (filter->ratio - 1.0)) / (1.0 + filter->ratio);
652
653   if (zero < 0.0)
654     zero = 0.0;
655
656   /* shouldn't happen as this only happens with
657    * threshold == 0.0 */
658   g_assert (threshold != 0.0);
659
660   /* We build a 2n degree polynomial here for values between
661    * 0 and threshold or 0 and -threshold with:
662    * f(t) = t, f'(t) = 1, f(z) = 0, f'(z) = r
663    * z between 0 and t
664    * =>
665    * a = (1 - r^2) / (4 * t)
666    * b = (1 + r^2) / 2
667    * c = t * (1.0 - b - a*t)
668    * f(x) = ax^2 + bx + c */
669   r2 = filter->ratio * filter->ratio;
670   a_p = (1.0 - r2) / (4.0 * threshold);
671   b_p = (1.0 + r2) / 2.0;
672   c_p = threshold * (1.0 - b_p - a_p * threshold);
673   a_n = (1.0 - r2) / (-4.0 * threshold);
674   b_n = (1.0 + r2) / 2.0;
675   c_n = -threshold * (1.0 - b_n + a_n * threshold);
676
677   for (; num_samples; num_samples--) {
678     val = *data;
679
680     if (val < threshold && val > zero) {
681       val = a_p * val * val + b_p * val + c_p;
682     } else if ((val <= zero && val > 0.0) || (val >= -zero && val < 0.0)) {
683       val = 0.0;
684     } else if (val > -threshold && val < -zero) {
685       val = a_n * val * val + b_n * val + c_n;
686     }
687     *data++ = (gfloat) val;
688   }
689 }
690
691 /* GstBaseTransform vmethod implementations */
692 static GstFlowReturn
693 gst_audio_dynamic_transform_ip (GstBaseTransform * base, GstBuffer * buf)
694 {
695   GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base);
696   guint num_samples;
697   GstClockTime timestamp, stream_time;
698   GstMapInfo map;
699
700   timestamp = GST_BUFFER_TIMESTAMP (buf);
701   stream_time =
702       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
703
704   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
705       GST_TIME_ARGS (timestamp));
706
707   if (GST_CLOCK_TIME_IS_VALID (stream_time))
708     gst_object_sync_values (GST_OBJECT (filter), stream_time);
709
710   if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
711     return GST_FLOW_OK;
712
713   gst_buffer_map (buf, &map, GST_MAP_READWRITE);
714   num_samples = map.size / GST_AUDIO_FILTER_BPS (filter);
715
716   filter->process (filter, map.data, num_samples);
717
718   gst_buffer_unmap (buf, &map);
719
720   return GST_FLOW_OK;
721 }