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 \
76 " endianness=(int)BYTE_ORDER," \
77 " signed=(bool)TRUE," \
78 " rate=(int)[1,MAX]," \
79 " channels=(int)[1,MAX]; " \
80 "audio/x-raw-float," \
82 " endianness=(int)BYTE_ORDER," \
83 " rate=(int)[1,MAX]," \
84 " channels=(int)[1,MAX]"
86 G_DEFINE_TYPE (GstAudioDynamic, gst_audio_dynamic, GST_TYPE_AUDIO_FILTER);
88 static void gst_audio_dynamic_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec);
90 static void gst_audio_dynamic_get_property (GObject * object, guint prop_id,
91 GValue * value, GParamSpec * pspec);
93 static gboolean gst_audio_dynamic_setup (GstAudioFilter * filter,
94 GstRingBufferSpec * format);
95 static GstFlowReturn gst_audio_dynamic_transform_ip (GstBaseTransform * base,
99 gst_audio_dynamic_transform_hard_knee_compressor_int (GstAudioDynamic * filter,
100 gint16 * data, guint num_samples);
102 gst_audio_dynamic_transform_hard_knee_compressor_float (GstAudioDynamic *
103 filter, gfloat * data, guint num_samples);
105 gst_audio_dynamic_transform_soft_knee_compressor_int (GstAudioDynamic * filter,
106 gint16 * data, guint num_samples);
108 gst_audio_dynamic_transform_soft_knee_compressor_float (GstAudioDynamic *
109 filter, gfloat * data, guint num_samples);
110 static void gst_audio_dynamic_transform_hard_knee_expander_int (GstAudioDynamic
111 * filter, gint16 * data, guint num_samples);
113 gst_audio_dynamic_transform_hard_knee_expander_float (GstAudioDynamic * filter,
114 gfloat * data, guint num_samples);
115 static void gst_audio_dynamic_transform_soft_knee_expander_int (GstAudioDynamic
116 * filter, gint16 * data, guint num_samples);
118 gst_audio_dynamic_transform_soft_knee_expander_float (GstAudioDynamic * filter,
119 gfloat * data, guint num_samples);
121 static GstAudioDynamicProcessFunc process_functions[] = {
122 (GstAudioDynamicProcessFunc)
123 gst_audio_dynamic_transform_hard_knee_compressor_int,
124 (GstAudioDynamicProcessFunc)
125 gst_audio_dynamic_transform_hard_knee_compressor_float,
126 (GstAudioDynamicProcessFunc)
127 gst_audio_dynamic_transform_soft_knee_compressor_int,
128 (GstAudioDynamicProcessFunc)
129 gst_audio_dynamic_transform_soft_knee_compressor_float,
130 (GstAudioDynamicProcessFunc)
131 gst_audio_dynamic_transform_hard_knee_expander_int,
132 (GstAudioDynamicProcessFunc)
133 gst_audio_dynamic_transform_hard_knee_expander_float,
134 (GstAudioDynamicProcessFunc)
135 gst_audio_dynamic_transform_soft_knee_expander_int,
136 (GstAudioDynamicProcessFunc)
137 gst_audio_dynamic_transform_soft_knee_expander_float
142 CHARACTERISTICS_HARD_KNEE = 0,
143 CHARACTERISTICS_SOFT_KNEE
146 #define GST_TYPE_AUDIO_DYNAMIC_CHARACTERISTICS (gst_audio_dynamic_characteristics_get_type ())
148 gst_audio_dynamic_characteristics_get_type (void)
150 static GType gtype = 0;
153 static const GEnumValue values[] = {
154 {CHARACTERISTICS_HARD_KNEE, "Hard Knee (default)",
156 {CHARACTERISTICS_SOFT_KNEE, "Soft Knee (smooth)",
161 gtype = g_enum_register_static ("GstAudioDynamicCharacteristics", values);
172 #define GST_TYPE_AUDIO_DYNAMIC_MODE (gst_audio_dynamic_mode_get_type ())
174 gst_audio_dynamic_mode_get_type (void)
176 static GType gtype = 0;
179 static const GEnumValue values[] = {
180 {MODE_COMPRESSOR, "Compressor (default)",
182 {MODE_EXPANDER, "Expander", "expander"},
186 gtype = g_enum_register_static ("GstAudioDynamicMode", values);
192 gst_audio_dynamic_set_process_function (GstAudioDynamic * filter)
196 func_index = (filter->mode == MODE_COMPRESSOR) ? 0 : 4;
197 func_index += (filter->characteristics == CHARACTERISTICS_HARD_KNEE) ? 0 : 2;
199 (GST_AUDIO_FILTER (filter)->format.type == GST_BUFTYPE_FLOAT) ? 1 : 0;
201 if (func_index >= 0 && func_index < 8) {
202 filter->process = process_functions[func_index];
209 /* GObject vmethod implementations */
212 gst_audio_dynamic_class_init (GstAudioDynamicClass * klass)
214 GObjectClass *gobject_class;
215 GstElementClass *gstelement_class;
218 GST_DEBUG_CATEGORY_INIT (gst_audio_dynamic_debug, "audiodynamic", 0,
219 "audiodynamic element");
221 gobject_class = (GObjectClass *) klass;
222 gstelement_class = (GstElementClass *) klass;
224 gobject_class->set_property = gst_audio_dynamic_set_property;
225 gobject_class->get_property = gst_audio_dynamic_get_property;
227 g_object_class_install_property (gobject_class, PROP_CHARACTERISTICS,
228 g_param_spec_enum ("characteristics", "Characteristics",
229 "Selects whether the ratio should be applied smooth (soft-knee) "
230 "or hard (hard-knee).",
231 GST_TYPE_AUDIO_DYNAMIC_CHARACTERISTICS, CHARACTERISTICS_HARD_KNEE,
232 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_MODE,
235 g_param_spec_enum ("mode", "Mode",
236 "Selects whether the filter should work on loud samples (compressor) or"
237 "quiet samples (expander).",
238 GST_TYPE_AUDIO_DYNAMIC_MODE, MODE_COMPRESSOR,
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 g_object_class_install_property (gobject_class, PROP_THRESHOLD,
242 g_param_spec_float ("threshold", "Threshold",
243 "Threshold until the filter is activated", 0.0, 1.0,
245 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
247 g_object_class_install_property (gobject_class, PROP_RATIO,
248 g_param_spec_float ("ratio", "Ratio",
249 "Ratio that should be applied", 0.0, G_MAXFLOAT,
251 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
253 gst_element_class_set_details_simple (gstelement_class,
254 "Dynamic range controller", "Filter/Effect/Audio",
255 "Compressor and Expander", "Sebastian Dröge <slomo@circular-chaos.org>");
257 caps = gst_caps_from_string (ALLOWED_CAPS);
258 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
260 gst_caps_unref (caps);
262 GST_AUDIO_FILTER_CLASS (klass)->setup =
263 GST_DEBUG_FUNCPTR (gst_audio_dynamic_setup);
264 GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
265 GST_DEBUG_FUNCPTR (gst_audio_dynamic_transform_ip);
269 gst_audio_dynamic_init (GstAudioDynamic * filter)
272 filter->threshold = 0.0;
273 filter->characteristics = CHARACTERISTICS_HARD_KNEE;
274 filter->mode = MODE_COMPRESSOR;
275 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
276 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
280 gst_audio_dynamic_set_property (GObject * object, guint prop_id,
281 const GValue * value, GParamSpec * pspec)
283 GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object);
286 case PROP_CHARACTERISTICS:
287 filter->characteristics = g_value_get_enum (value);
288 gst_audio_dynamic_set_process_function (filter);
291 filter->mode = g_value_get_enum (value);
292 gst_audio_dynamic_set_process_function (filter);
295 filter->threshold = g_value_get_float (value);
298 filter->ratio = g_value_get_float (value);
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
307 gst_audio_dynamic_get_property (GObject * object, guint prop_id,
308 GValue * value, GParamSpec * pspec)
310 GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object);
313 case PROP_CHARACTERISTICS:
314 g_value_set_enum (value, filter->characteristics);
317 g_value_set_enum (value, filter->mode);
320 g_value_set_float (value, filter->threshold);
323 g_value_set_float (value, filter->ratio);
326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331 /* GstAudioFilter vmethod implementations */
334 gst_audio_dynamic_setup (GstAudioFilter * base, GstRingBufferSpec * format)
336 GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base);
339 ret = gst_audio_dynamic_set_process_function (filter);
345 gst_audio_dynamic_transform_hard_knee_compressor_int (GstAudioDynamic * filter,
346 gint16 * data, guint num_samples)
349 glong thr_p = filter->threshold * G_MAXINT16;
350 glong thr_n = filter->threshold * G_MININT16;
352 /* Nothing to do for us if ratio is 1.0 or if the threshold
354 if (filter->threshold == 1.0 || filter->ratio == 1.0)
357 for (; num_samples; num_samples--) {
361 val = thr_p + (val - thr_p) * filter->ratio;
362 } else if (val < thr_n) {
363 val = thr_n + (val - thr_n) * filter->ratio;
365 *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
370 gst_audio_dynamic_transform_hard_knee_compressor_float (GstAudioDynamic *
371 filter, gfloat * data, guint num_samples)
373 gdouble val, threshold = filter->threshold;
375 /* Nothing to do for us if ratio == 1.0.
376 * As float values can be above 1.0 we have to do something
377 * if threshold is greater than 1.0. */
378 if (filter->ratio == 1.0)
381 for (; num_samples; num_samples--) {
384 if (val > threshold) {
385 val = threshold + (val - threshold) * filter->ratio;
386 } else if (val < -threshold) {
387 val = -threshold + (val + threshold) * filter->ratio;
389 *data++ = (gfloat) val;
394 gst_audio_dynamic_transform_soft_knee_compressor_int (GstAudioDynamic * filter,
395 gint16 * data, guint num_samples)
398 glong thr_p = filter->threshold * G_MAXINT16;
399 glong thr_n = filter->threshold * G_MININT16;
400 gdouble a_p, b_p, c_p;
401 gdouble a_n, b_n, c_n;
403 /* Nothing to do for us if ratio is 1.0 or if the threshold
405 if (filter->threshold == 1.0 || filter->ratio == 1.0)
408 /* We build a 2nd degree polynomial here for
409 * values greater than threshold or small than
411 * f(t) = t, f'(t) = 1, f'(m) = r
413 * a = (1-r)/(2*(t-m))
414 * b = (r*t - m)/(t-m)
415 * c = t * (1 - b - a*t)
416 * f(x) = ax^2 + bx + c
419 /* shouldn't happen because this would only be the case
420 * for threshold == 1.0 which we catch above */
421 g_assert (thr_p - G_MAXINT16 != 0);
422 g_assert (thr_n - G_MININT != 0);
424 a_p = (1 - filter->ratio) / (2 * (thr_p - G_MAXINT16));
425 b_p = (filter->ratio * thr_p - G_MAXINT16) / (thr_p - G_MAXINT16);
426 c_p = thr_p * (1 - b_p - a_p * thr_p);
427 a_n = (1 - filter->ratio) / (2 * (thr_n - G_MININT16));
428 b_n = (filter->ratio * thr_n - G_MININT16) / (thr_n - G_MININT16);
429 c_n = thr_n * (1 - b_n - a_n * thr_n);
431 for (; num_samples; num_samples--) {
435 val = a_p * val * val + b_p * val + c_p;
436 } else if (val < thr_n) {
437 val = a_n * val * val + b_n * val + c_n;
439 *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
444 gst_audio_dynamic_transform_soft_knee_compressor_float (GstAudioDynamic *
445 filter, gfloat * data, guint num_samples)
448 gdouble threshold = filter->threshold;
449 gdouble a_p, b_p, c_p;
450 gdouble a_n, b_n, c_n;
452 /* Nothing to do for us if ratio == 1.0.
453 * As float values can be above 1.0 we have to do something
454 * if threshold is greater than 1.0. */
455 if (filter->ratio == 1.0)
458 /* We build a 2nd degree polynomial here for
459 * values greater than threshold or small than
461 * f(t) = t, f'(t) = 1, f'(m) = r
463 * a = (1-r)/(2*(t-m))
464 * b = (r*t - m)/(t-m)
465 * c = t * (1 - b - a*t)
466 * f(x) = ax^2 + bx + c
469 /* FIXME: If treshold is the same as the maximum
470 * we need to raise it a bit to prevent
471 * division by zero. */
472 if (threshold == 1.0)
473 threshold = 1.0 + 0.00001;
475 a_p = (1.0 - filter->ratio) / (2.0 * (threshold - 1.0));
476 b_p = (filter->ratio * threshold - 1.0) / (threshold - 1.0);
477 c_p = threshold * (1.0 - b_p - a_p * threshold);
478 a_n = (1.0 - filter->ratio) / (2.0 * (-threshold + 1.0));
479 b_n = (-filter->ratio * threshold + 1.0) / (-threshold + 1.0);
480 c_n = -threshold * (1.0 - b_n + a_n * threshold);
482 for (; num_samples; num_samples--) {
486 val = 1.0 + (val - 1.0) * filter->ratio;
487 } else if (val > threshold) {
488 val = a_p * val * val + b_p * val + c_p;
489 } else if (val < -1.0) {
490 val = -1.0 + (val + 1.0) * filter->ratio;
491 } else if (val < -threshold) {
492 val = a_n * val * val + b_n * val + c_n;
494 *data++ = (gfloat) val;
499 gst_audio_dynamic_transform_hard_knee_expander_int (GstAudioDynamic * filter,
500 gint16 * data, guint num_samples)
503 glong thr_p = filter->threshold * G_MAXINT16;
504 glong thr_n = filter->threshold * G_MININT16;
505 gdouble zero_p, zero_n;
507 /* Nothing to do for us here if threshold equals 0.0
508 * or ratio equals 1.0 */
509 if (filter->threshold == 0.0 || filter->ratio == 1.0)
512 /* zero crossing of our function */
513 if (filter->ratio != 0.0) {
514 zero_p = thr_p - thr_p / filter->ratio;
515 zero_n = thr_n - thr_n / filter->ratio;
517 zero_p = zero_n = 0.0;
525 for (; num_samples; num_samples--) {
528 if (val < thr_p && val > zero_p) {
529 val = filter->ratio * val + thr_p * (1 - filter->ratio);
530 } else if ((val <= zero_p && val > 0) || (val >= zero_n && val < 0)) {
532 } else if (val > thr_n && val < zero_n) {
533 val = filter->ratio * val + thr_n * (1 - filter->ratio);
535 *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
540 gst_audio_dynamic_transform_hard_knee_expander_float (GstAudioDynamic * filter,
541 gfloat * data, guint num_samples)
543 gdouble val, threshold = filter->threshold, zero;
545 /* Nothing to do for us here if threshold equals 0.0
546 * or ratio equals 1.0 */
547 if (filter->threshold == 0.0 || filter->ratio == 1.0)
550 /* zero crossing of our function */
551 if (filter->ratio != 0.0)
552 zero = threshold - threshold / filter->ratio;
559 for (; num_samples; num_samples--) {
562 if (val < threshold && val > zero) {
563 val = filter->ratio * val + threshold * (1.0 - filter->ratio);
564 } else if ((val <= zero && val > 0.0) || (val >= -zero && val < 0.0)) {
566 } else if (val > -threshold && val < -zero) {
567 val = filter->ratio * val - threshold * (1.0 - filter->ratio);
569 *data++ = (gfloat) val;
574 gst_audio_dynamic_transform_soft_knee_expander_int (GstAudioDynamic * filter,
575 gint16 * data, guint num_samples)
578 glong thr_p = filter->threshold * G_MAXINT16;
579 glong thr_n = filter->threshold * G_MININT16;
580 gdouble zero_p, zero_n;
581 gdouble a_p, b_p, c_p;
582 gdouble a_n, b_n, c_n;
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)
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);
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);
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
608 * a = (1 - r^2) / (4 * t)
610 * c = t * (1.0 - b - a*t)
611 * f(x) = ax^2 + bx + c */
612 a_p = (1.0 - filter->ratio * filter->ratio) / (4.0 * thr_p);
613 b_p = (1.0 + filter->ratio * filter->ratio) / 2.0;
614 c_p = thr_p * (1.0 - b_p - a_p * thr_p);
615 a_n = (1.0 - filter->ratio * filter->ratio) / (4.0 * thr_n);
616 b_n = (1.0 + filter->ratio * filter->ratio) / 2.0;
617 c_n = thr_n * (1.0 - b_n - a_n * thr_n);
619 for (; num_samples; num_samples--) {
622 if (val < thr_p && val > zero_p) {
623 val = a_p * val * val + b_p * val + c_p;
624 } else if ((val <= zero_p && val > 0) || (val >= zero_n && val < 0)) {
626 } else if (val > thr_n && val < zero_n) {
627 val = a_n * val * val + b_n * val + c_n;
629 *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16);
634 gst_audio_dynamic_transform_soft_knee_expander_float (GstAudioDynamic * filter,
635 gfloat * data, guint num_samples)
638 gdouble threshold = filter->threshold;
640 gdouble a_p, b_p, c_p;
641 gdouble a_n, b_n, c_n;
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)
648 /* zero crossing of our function */
649 zero = (threshold * (filter->ratio - 1.0)) / (1.0 + filter->ratio);
654 /* shouldn't happen as this only happens with
655 * threshold == 0.0 */
656 g_assert (threshold != 0.0);
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
663 * a = (1 - r^2) / (4 * t)
665 * c = t * (1.0 - b - a*t)
666 * f(x) = ax^2 + bx + c */
667 a_p = (1.0 - filter->ratio * filter->ratio) / (4.0 * threshold);
668 b_p = (1.0 + filter->ratio * filter->ratio) / 2.0;
669 c_p = threshold * (1.0 - b_p - a_p * threshold);
670 a_n = (1.0 - filter->ratio * filter->ratio) / (-4.0 * threshold);
671 b_n = (1.0 + filter->ratio * filter->ratio) / 2.0;
672 c_n = -threshold * (1.0 - b_n + a_n * threshold);
674 for (; num_samples; num_samples--) {
677 if (val < threshold && val > zero) {
678 val = a_p * val * val + b_p * val + c_p;
679 } else if ((val <= zero && val > 0.0) || (val >= -zero && val < 0.0)) {
681 } else if (val > -threshold && val < -zero) {
682 val = a_n * val * val + b_n * val + c_n;
684 *data++ = (gfloat) val;
688 /* GstBaseTransform vmethod implementations */
690 gst_audio_dynamic_transform_ip (GstBaseTransform * base, GstBuffer * buf)
692 GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base);
694 GstClockTime timestamp, stream_time;
698 timestamp = GST_BUFFER_TIMESTAMP (buf);
700 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
702 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
703 GST_TIME_ARGS (timestamp));
705 if (GST_CLOCK_TIME_IS_VALID (stream_time))
706 gst_object_sync_values (G_OBJECT (filter), stream_time);
708 if (gst_base_transform_is_passthrough (base) ||
709 G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
712 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
713 num_samples = size / (GST_AUDIO_FILTER (filter)->format.width / 8);
715 filter->process (filter, data, num_samples);
717 gst_buffer_unmap (buf, data, size);