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: #GstRgVolume
27 * This element applies signal compression/limiting to raw audio data. It
28 * performs strict hard limiting with soft-knee characteristics, using a
29 * threshold of -6 dB. This type of filter is mentioned in the proposed <ulink
30 * url="http://replaygain.org">ReplayGain standard</ulink>.
33 * <title>Example launch line</title>
35 * gst-launch-1.0 filesrc location=filename.ext ! decodebin ! audioconvert \
36 * ! rgvolume pre-amp=6.0 headroom=10.0 ! rglimiter \
37 * ! audioconvert ! audioresample ! alsasink
38 * ]|Playback of a file
48 #include <gst/audio/audio.h>
50 #include "gstrglimiter.h"
52 GST_DEBUG_CATEGORY_STATIC (gst_rg_limiter_debug);
53 #define GST_CAT_DEFAULT gst_rg_limiter_debug
61 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("audio/x-raw, "
65 "format = (string) " GST_AUDIO_NE (F32) ", "
66 "layout = (string) { interleaved, non-interleaved }, "
67 "channels = (int) [1, MAX], " "rate = (int) [1, MAX]"));
69 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
72 GST_STATIC_CAPS ("audio/x-raw, "
73 "format = (string) " GST_AUDIO_NE (F32) ", "
74 "layout = (string) { interleaved, non-interleaved}, "
75 "channels = (int) [1, MAX], " "rate = (int) [1, MAX]"));
77 #define gst_rg_limiter_parent_class parent_class
78 G_DEFINE_TYPE (GstRgLimiter, gst_rg_limiter, GST_TYPE_BASE_TRANSFORM);
80 static void gst_rg_limiter_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_rg_limiter_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
85 static GstFlowReturn gst_rg_limiter_transform_ip (GstBaseTransform * base,
89 gst_rg_limiter_class_init (GstRgLimiterClass * klass)
91 GObjectClass *gobject_class;
92 GstElementClass *element_class;
93 GstBaseTransformClass *trans_class;
95 gobject_class = (GObjectClass *) klass;
96 element_class = (GstElementClass *) klass;
98 gobject_class->set_property = gst_rg_limiter_set_property;
99 gobject_class->get_property = gst_rg_limiter_get_property;
101 g_object_class_install_property (gobject_class, PROP_ENABLED,
102 g_param_spec_boolean ("enabled", "Enabled", "Enable processing", TRUE,
103 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
105 trans_class = GST_BASE_TRANSFORM_CLASS (klass);
106 trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_rg_limiter_transform_ip);
107 trans_class->passthrough_on_same_caps = FALSE;
109 gst_element_class_add_static_pad_template (element_class, &src_factory);
110 gst_element_class_add_static_pad_template (element_class, &sink_factory);
111 gst_element_class_set_static_metadata (element_class, "ReplayGain limiter",
112 "Filter/Effect/Audio",
113 "Apply signal compression to raw audio data",
114 "Ren\xc3\xa9 Stadler <mail@renestadler.de>");
116 GST_DEBUG_CATEGORY_INIT (gst_rg_limiter_debug, "rglimiter", 0,
117 "ReplayGain limiter element");
121 gst_rg_limiter_init (GstRgLimiter * filter)
123 GstBaseTransform *base = GST_BASE_TRANSFORM (filter);
125 gst_base_transform_set_passthrough (base, FALSE);
126 gst_base_transform_set_gap_aware (base, TRUE);
128 filter->enabled = TRUE;
132 gst_rg_limiter_set_property (GObject * object, guint prop_id,
133 const GValue * value, GParamSpec * pspec)
135 GstRgLimiter *filter = GST_RG_LIMITER (object);
139 filter->enabled = g_value_get_boolean (value);
140 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter),
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 gst_rg_limiter_get_property (GObject * object, guint prop_id,
151 GValue * value, GParamSpec * pspec)
153 GstRgLimiter *filter = GST_RG_LIMITER (object);
157 g_value_set_boolean (value, filter->enabled);
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 #define THRES 0.5 /* ca. -6 dB */
167 #define COMPL 0.5 /* LIMIT - THRESH */
170 gst_rg_limiter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
172 GstRgLimiter *filter = GST_RG_LIMITER (base);
178 if (!filter->enabled)
181 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP))
184 gst_buffer_map (buf, &map, GST_MAP_READ);
185 input = (gfloat *) map.data;
186 count = gst_buffer_get_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;
196 gst_buffer_unmap (buf, &map);