1 /* GStreamer ReplayGain limiter
3 * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
5 * gstrglimiter.c: Element to apply signal compression to raw audio data
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * SECTION:element-rglimiter
25 * @see_also: <link linkend="GstRgVolume">rgvolume</link>
29 * This element applies signal compression/limiting to raw audio data. It
30 * performs strict hard limiting with soft-knee characteristics, using a
31 * threshold of -6 dB. This type of filter is mentioned in the proposed <ulink
32 * url="http://replaygain.org">ReplayGain standard</ulink>.
34 * <title>Example launch line</title>
35 * <para>Playback of a file:</para>
37 * gst-launch filesrc location="Filename.ext" ! decodebin ! audioconvert \
38 * ! rgvolume pre-amp=6.0 headroom=10.0 ! rglimiter \
39 * ! audioconvert ! audioresample ! alsasink
51 #include "gstrglimiter.h"
53 GST_DEBUG_CATEGORY_STATIC (gst_rg_limiter_debug);
54 #define GST_CAT_DEFAULT gst_rg_limiter_debug
62 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, "
64 "width = (int) 32, channels = (int) [1, MAX], "
65 "rate = (int) [1, MAX], endianness = (int) BYTE_ORDER"));
67 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
68 GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, "
69 "width = (int) 32, channels = (int) [1, MAX], "
70 "rate = (int) [1, MAX], endianness = (int) BYTE_ORDER"));
72 GST_BOILERPLATE (GstRgLimiter, gst_rg_limiter, GstBaseTransform,
73 GST_TYPE_BASE_TRANSFORM);
75 static void gst_rg_limiter_class_init (GstRgLimiterClass * klass);
76 static void gst_rg_limiter_init (GstRgLimiter * filter,
77 GstRgLimiterClass * gclass);
79 static void gst_rg_limiter_set_property (GObject * object, guint prop_id,
80 const GValue * value, GParamSpec * pspec);
81 static void gst_rg_limiter_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec);
84 static GstFlowReturn gst_rg_limiter_transform_ip (GstBaseTransform * base,
87 static const GstElementDetails element_details = {
89 "Filter/Effect/Audio",
90 "Apply signal compression to raw audio data",
91 "Ren\xc3\xa9 Stadler <mail@renestadler.de>"
95 gst_rg_limiter_base_init (gpointer g_class)
97 GstElementClass *element_class = g_class;
99 gst_element_class_add_pad_template (element_class,
100 gst_static_pad_template_get (&src_factory));
101 gst_element_class_add_pad_template (element_class,
102 gst_static_pad_template_get (&sink_factory));
103 gst_element_class_set_details (element_class, &element_details);
105 GST_DEBUG_CATEGORY_INIT (gst_rg_limiter_debug, "rglimiter", 0,
106 "ReplayGain limiter element");
110 gst_rg_limiter_class_init (GstRgLimiterClass * klass)
112 GObjectClass *gobject_class;
113 GstBaseTransformClass *trans_class;
115 gobject_class = (GObjectClass *) klass;
117 gobject_class->set_property = gst_rg_limiter_set_property;
118 gobject_class->get_property = gst_rg_limiter_get_property;
120 g_object_class_install_property (gobject_class, PROP_ENABLED,
121 g_param_spec_boolean ("enabled", "Enabled", "Enable processing", TRUE,
124 trans_class = GST_BASE_TRANSFORM_CLASS (klass);
125 trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_rg_limiter_transform_ip);
126 trans_class->passthrough_on_same_caps = FALSE;
130 gst_rg_limiter_init (GstRgLimiter * filter, GstRgLimiterClass * gclass)
132 filter->enabled = TRUE;
133 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), FALSE);
137 gst_rg_limiter_set_property (GObject * object, guint prop_id,
138 const GValue * value, GParamSpec * pspec)
140 GstRgLimiter *filter = GST_RG_LIMITER (object);
144 filter->enabled = g_value_get_boolean (value);
145 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter),
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
155 gst_rg_limiter_get_property (GObject * object, guint prop_id,
156 GValue * value, GParamSpec * pspec)
158 GstRgLimiter *filter = GST_RG_LIMITER (object);
162 g_value_set_boolean (value, filter->enabled);
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171 #define THRES 0.5 /* ca. -6 dB */
172 #define COMPL 0.5 /* LIMIT - THRESH */
175 gst_rg_limiter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
177 GstRgLimiter *filter = GST_RG_LIMITER (base);
182 if (!filter->enabled)
185 input = (gfloat *) GST_BUFFER_DATA (buf);
186 count = GST_BUFFER_SIZE (buf) / sizeof (gfloat);
188 for (i = count; i--;) {
190 *input = tanhf ((*input - THRES) / COMPL) * COMPL + THRES;
191 else if (*input < -THRES)
192 *input = tanhf ((*input + THRES) / COMPL) * COMPL - THRES;