3 * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
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.
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.
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.
22 * SECTION:element-audiodynamic
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.
30 * <title>Example launch line</title>
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
39 /* TODO: Implement attack and release parameters */
46 #include <gst/base/gstbasetransform.h>
47 #include <gst/audio/audio.h>
48 #include <gst/audio/gstaudiofilter.h>
49 #include <gst/controller/gstcontroller.h>
51 #include "audiodynamic.h"
53 #define GST_CAT_DEFAULT gst_audio_dynamic_debug
54 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
56 /* Filter signals and args */
72 #define ALLOWED_CAPS \
74 " format=(string) {"GST_AUDIO_NE(S16)","GST_AUDIO_NE(F32)"}," \
75 " rate=(int)[1,MAX]," \
76 " channels=(int)[1,MAX]"
78 G_DEFINE_TYPE (GstAudioDynamic, gst_audio_dynamic, GST_TYPE_AUDIO_FILTER);
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);
85 static gboolean gst_audio_dynamic_setup (GstAudioFilter * filter,
87 static GstFlowReturn gst_audio_dynamic_transform_ip (GstBaseTransform * base,
91 gst_audio_dynamic_transform_hard_knee_compressor_int (GstAudioDynamic * filter,
92 gint16 * data, guint num_samples);
94 gst_audio_dynamic_transform_hard_knee_compressor_float (GstAudioDynamic *
95 filter, gfloat * data, guint num_samples);
97 gst_audio_dynamic_transform_soft_knee_compressor_int (GstAudioDynamic * filter,
98 gint16 * data, guint num_samples);
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);
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);
110 gst_audio_dynamic_transform_soft_knee_expander_float (GstAudioDynamic * filter,
111 gfloat * data, guint num_samples);
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
134 CHARACTERISTICS_HARD_KNEE = 0,
135 CHARACTERISTICS_SOFT_KNEE
138 #define GST_TYPE_AUDIO_DYNAMIC_CHARACTERISTICS (gst_audio_dynamic_characteristics_get_type ())
140 gst_audio_dynamic_characteristics_get_type (void)
142 static GType gtype = 0;
145 static const GEnumValue values[] = {
146 {CHARACTERISTICS_HARD_KNEE, "Hard Knee (default)",
148 {CHARACTERISTICS_SOFT_KNEE, "Soft Knee (smooth)",
153 gtype = g_enum_register_static ("GstAudioDynamicCharacteristics", values);
164 #define GST_TYPE_AUDIO_DYNAMIC_MODE (gst_audio_dynamic_mode_get_type ())
166 gst_audio_dynamic_mode_get_type (void)
168 static GType gtype = 0;
171 static const GEnumValue values[] = {
172 {MODE_COMPRESSOR, "Compressor (default)",
174 {MODE_EXPANDER, "Expander", "expander"},
178 gtype = g_enum_register_static ("GstAudioDynamicMode", values);
184 gst_audio_dynamic_set_process_function (GstAudioDynamic * filter)
188 func_index = (filter->mode == MODE_COMPRESSOR) ? 0 : 4;
189 func_index += (filter->characteristics == CHARACTERISTICS_HARD_KNEE) ? 0 : 2;
191 (GST_AUDIO_FILTER_FORMAT (filter) == GST_AUDIO_FORMAT_F32) ? 1 : 0;
193 if (func_index >= 0 && func_index < 8) {
194 filter->process = process_functions[func_index];
201 /* GObject vmethod implementations */
204 gst_audio_dynamic_class_init (GstAudioDynamicClass * klass)
206 GObjectClass *gobject_class;
207 GstElementClass *gstelement_class;
210 GST_DEBUG_CATEGORY_INIT (gst_audio_dynamic_debug, "audiodynamic", 0,
211 "audiodynamic element");
213 gobject_class = (GObjectClass *) klass;
214 gstelement_class = (GstElementClass *) klass;
216 gobject_class->set_property = gst_audio_dynamic_set_property;
217 gobject_class->get_property = gst_audio_dynamic_get_property;
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));
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));
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,
237 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
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,
243 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
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>");
249 caps = gst_caps_from_string (ALLOWED_CAPS);
250 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
252 gst_caps_unref (caps);
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);
261 gst_audio_dynamic_init (GstAudioDynamic * filter)
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);
272 gst_audio_dynamic_set_property (GObject * object, guint prop_id,
273 const GValue * value, GParamSpec * pspec)
275 GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object);
278 case PROP_CHARACTERISTICS:
279 filter->characteristics = g_value_get_enum (value);
280 gst_audio_dynamic_set_process_function (filter);
283 filter->mode = g_value_get_enum (value);
284 gst_audio_dynamic_set_process_function (filter);
287 filter->threshold = g_value_get_float (value);
290 filter->ratio = g_value_get_float (value);
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299 gst_audio_dynamic_get_property (GObject * object, guint prop_id,
300 GValue * value, GParamSpec * pspec)
302 GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object);
305 case PROP_CHARACTERISTICS:
306 g_value_set_enum (value, filter->characteristics);
309 g_value_set_enum (value, filter->mode);
312 g_value_set_float (value, filter->threshold);
315 g_value_set_float (value, filter->ratio);
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323 /* GstAudioFilter vmethod implementations */
326 gst_audio_dynamic_setup (GstAudioFilter * base, GstAudioInfo * info)
328 GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base);
331 ret = gst_audio_dynamic_set_process_function (filter);
337 gst_audio_dynamic_transform_hard_knee_compressor_int (GstAudioDynamic * filter,
338 gint16 * data, guint num_samples)
341 glong thr_p = filter->threshold * G_MAXINT16;
342 glong thr_n = filter->threshold * G_MININT16;
344 /* Nothing to do for us if ratio is 1.0 or if the threshold
346 if (filter->threshold == 1.0 || filter->ratio == 1.0)
349 for (; num_samples; num_samples--) {
353 val = thr_p + (val - thr_p) * filter->ratio;
354 } else if (val < thr_n) {
355 val = thr_n + (val - thr_n) * filter->ratio;
357 *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
362 gst_audio_dynamic_transform_hard_knee_compressor_float (GstAudioDynamic *
363 filter, gfloat * data, guint num_samples)
365 gdouble val, threshold = filter->threshold;
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)
373 for (; num_samples; num_samples--) {
376 if (val > threshold) {
377 val = threshold + (val - threshold) * filter->ratio;
378 } else if (val < -threshold) {
379 val = -threshold + (val + threshold) * filter->ratio;
381 *data++ = (gfloat) val;
386 gst_audio_dynamic_transform_soft_knee_compressor_int (GstAudioDynamic * filter,
387 gint16 * data, guint num_samples)
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;
395 /* Nothing to do for us if ratio is 1.0 or if the threshold
397 if (filter->threshold == 1.0 || filter->ratio == 1.0)
400 /* We build a 2nd degree polynomial here for
401 * values greater than threshold or small than
403 * f(t) = t, f'(t) = 1, f'(m) = r
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
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);
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);
423 for (; num_samples; num_samples--) {
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;
431 *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
436 gst_audio_dynamic_transform_soft_knee_compressor_float (GstAudioDynamic *
437 filter, gfloat * data, guint num_samples)
440 gdouble threshold = filter->threshold;
441 gdouble a_p, b_p, c_p;
442 gdouble a_n, b_n, c_n;
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)
450 /* We build a 2nd degree polynomial here for
451 * values greater than threshold or small than
453 * f(t) = t, f'(t) = 1, f'(m) = r
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
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;
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);
474 for (; num_samples; num_samples--) {
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;
486 *data++ = (gfloat) val;
491 gst_audio_dynamic_transform_hard_knee_expander_int (GstAudioDynamic * filter,
492 gint16 * data, guint num_samples)
495 glong thr_p = filter->threshold * G_MAXINT16;
496 glong thr_n = filter->threshold * G_MININT16;
497 gdouble zero_p, zero_n;
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)
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;
509 zero_p = zero_n = 0.0;
517 for (; num_samples; num_samples--) {
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)) {
524 } else if (val > thr_n && val < zero_n) {
525 val = filter->ratio * val + thr_n * (1 - filter->ratio);
527 *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
532 gst_audio_dynamic_transform_hard_knee_expander_float (GstAudioDynamic * filter,
533 gfloat * data, guint num_samples)
535 gdouble val, threshold = filter->threshold, zero;
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)
542 /* zero crossing of our function */
543 if (filter->ratio != 0.0)
544 zero = threshold - threshold / filter->ratio;
551 for (; num_samples; num_samples--) {
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)) {
558 } else if (val > -threshold && val < -zero) {
559 val = filter->ratio * val - threshold * (1.0 - filter->ratio);
561 *data++ = (gfloat) val;
566 gst_audio_dynamic_transform_soft_knee_expander_int (GstAudioDynamic * filter,
567 gint16 * data, guint num_samples)
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;
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)
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);
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);
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
601 * a = (1 - r^2) / (4 * t)
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);
613 for (; num_samples; num_samples--) {
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)) {
620 } else if (val > thr_n && val < zero_n) {
621 val = a_n * val * val + b_n * val + c_n;
623 *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
628 gst_audio_dynamic_transform_soft_knee_expander_float (GstAudioDynamic * filter,
629 gfloat * data, guint num_samples)
632 gdouble threshold = filter->threshold;
634 gdouble a_p, b_p, c_p;
635 gdouble a_n, b_n, c_n;
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)
643 /* zero crossing of our function */
644 zero = (threshold * (filter->ratio - 1.0)) / (1.0 + filter->ratio);
649 /* shouldn't happen as this only happens with
650 * threshold == 0.0 */
651 g_assert (threshold != 0.0);
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
658 * a = (1 - r^2) / (4 * t)
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);
670 for (; num_samples; num_samples--) {
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)) {
677 } else if (val > -threshold && val < -zero) {
678 val = a_n * val * val + b_n * val + c_n;
680 *data++ = (gfloat) val;
684 /* GstBaseTransform vmethod implementations */
686 gst_audio_dynamic_transform_ip (GstBaseTransform * base, GstBuffer * buf)
688 GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base);
690 GstClockTime timestamp, stream_time;
694 timestamp = GST_BUFFER_TIMESTAMP (buf);
696 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
698 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
699 GST_TIME_ARGS (timestamp));
701 if (GST_CLOCK_TIME_IS_VALID (stream_time))
702 gst_object_sync_values (G_OBJECT (filter), stream_time);
704 if (gst_base_transform_is_passthrough (base) ||
705 G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
708 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
709 num_samples = size / GST_AUDIO_FILTER_BPS (filter);
711 filter->process (filter, data, num_samples);
713 gst_buffer_unmap (buf, data, size);