2 * Copyright (C) 2011 Tiago Katcipis <tiagokatcipis@gmail.com>
3 * Copyright (C) 2011 Paulo Pizarro <paulo.pizarro@gmail.com>
4 * Copyright (C) 2012-2016 Nicola Murino <nicola.murino@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-removesilence
24 * @title: removesilence
26 * Removes all silence periods from an audio stream, dropping silence buffers.
28 * ## Example launch line
30 * gst-launch-1.0 -v -m filesrc location="audiofile" ! decodebin ! removesilence remove=true ! wavenc ! filesink location=without_audio.wav
40 #include <gst/base/gstbasetransform.h>
41 #include <gst/audio/audio.h>
43 #include "gstremovesilence.h"
46 GST_DEBUG_CATEGORY_STATIC (gst_remove_silence_debug);
47 #define GST_CAT_DEFAULT gst_remove_silence_debug
48 #define DEFAULT_VAD_HYSTERESIS 480 /* 60 mseg */
50 /* Filter signals and args */
67 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
70 GST_STATIC_CAPS ("audio/x-raw, "
71 "format = (string) " GST_AUDIO_NE (S16) ", "
72 "layout = (string) interleaved, "
73 "rate = (int) [ 1, MAX ], " "channels = (int) 1"));
75 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
78 GST_STATIC_CAPS ("audio/x-raw, "
79 "format = (string) " GST_AUDIO_NE (S16) ", "
80 "layout = (string) interleaved, "
81 "rate = (int) [ 1, MAX ], " "channels = (int) 1"));
84 #define DEBUG_INIT(bla) \
85 GST_DEBUG_CATEGORY_INIT (gst_remove_silence_debug, "removesilence", 0, "removesilence element")
87 #define gst_remove_silence_parent_class parent_class
88 G_DEFINE_TYPE_WITH_CODE (GstRemoveSilence, gst_remove_silence,
89 GST_TYPE_BASE_TRANSFORM, DEBUG_INIT (0));
91 static void gst_remove_silence_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_remove_silence_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
96 static GstFlowReturn gst_remove_silence_transform_ip (GstBaseTransform * base,
98 static void gst_remove_silence_finalize (GObject * obj);
100 /* GObject vmethod implementations */
102 /* initialize the removesilence's class */
104 gst_remove_silence_class_init (GstRemoveSilenceClass * klass)
106 GObjectClass *gobject_class;
107 GstElementClass *gstelement_class;
109 gobject_class = (GObjectClass *) klass;
110 gstelement_class = (GstElementClass *) klass;
112 gobject_class->finalize = gst_remove_silence_finalize;
113 gobject_class->set_property = gst_remove_silence_set_property;
114 gobject_class->get_property = gst_remove_silence_get_property;
116 g_object_class_install_property (gobject_class, PROP_REMOVE,
117 g_param_spec_boolean ("remove", "Remove",
118 "Set to true to remove silence from the stream, false otherwhise",
119 FALSE, G_PARAM_READWRITE));
121 g_object_class_install_property (gobject_class, PROP_HYSTERESIS,
122 g_param_spec_uint64 ("hysteresis",
124 "Set the hysteresis (on samples) used on the internal VAD",
125 1, G_MAXUINT64, DEFAULT_VAD_HYSTERESIS, G_PARAM_READWRITE));
127 g_object_class_install_property (gobject_class, PROP_SQUASH,
128 g_param_spec_boolean ("squash", "Squash",
129 "Set to true to retimestamp buffers when silence is removed and so avoid timestamp gap",
130 FALSE, G_PARAM_READWRITE));
132 g_object_class_install_property (gobject_class, PROP_SILENT,
133 g_param_spec_boolean ("silent", "Silent",
134 "Disable/enable bus message notifications for silent detected/finished",
135 TRUE, G_PARAM_READWRITE));
137 gst_element_class_set_static_metadata (gstelement_class,
139 "Filter/Effect/Audio",
140 "Removes all the silence periods from the audio stream.",
141 "Tiago Katcipis <tiagokatcipis@gmail.com>\n \
142 Paulo Pizarro <paulo.pizarro@gmail.com>\n \
143 Nicola Murino <nicola.murino@gmail.com>");
145 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
146 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
148 GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
149 GST_DEBUG_FUNCPTR (gst_remove_silence_transform_ip);
152 /* initialize the new element
153 * instantiate pads and add them to element
154 * set pad calback functions
155 * initialize instance structure
158 gst_remove_silence_init (GstRemoveSilence * filter)
160 filter->vad = vad_new (DEFAULT_VAD_HYSTERESIS);
161 filter->remove = FALSE;
162 filter->squash = FALSE;
163 filter->ts_offset = 0;
164 filter->silence_detected = FALSE;
165 filter->silent = TRUE;
168 GST_DEBUG ("Error initializing VAD !!");
174 gst_remove_silence_finalize (GObject * obj)
176 GstRemoveSilence *filter = GST_REMOVE_SILENCE (obj);
177 GST_DEBUG ("Destroying VAD");
178 vad_destroy (filter->vad);
180 GST_DEBUG ("VAD Destroyed");
181 G_OBJECT_CLASS (parent_class)->finalize (obj);
185 gst_remove_silence_set_property (GObject * object, guint prop_id,
186 const GValue * value, GParamSpec * pspec)
188 GstRemoveSilence *filter = GST_REMOVE_SILENCE (object);
192 filter->remove = g_value_get_boolean (value);
194 case PROP_HYSTERESIS:
195 vad_set_hysteresis (filter->vad, g_value_get_uint64 (value));
198 filter->squash = g_value_get_boolean (value);
201 filter->silent = g_value_get_boolean (value);
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210 gst_remove_silence_get_property (GObject * object, guint prop_id,
211 GValue * value, GParamSpec * pspec)
213 GstRemoveSilence *filter = GST_REMOVE_SILENCE (object);
217 g_value_set_boolean (value, filter->remove);
219 case PROP_HYSTERESIS:
220 g_value_set_uint64 (value, vad_get_hysteresis (filter->vad));
223 g_value_set_boolean (value, filter->squash);
226 g_value_set_boolean (value, filter->silent);
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 gst_remove_silence_transform_ip (GstBaseTransform * trans, GstBuffer * inbuf)
237 GstRemoveSilence *filter = NULL;
241 filter = GST_REMOVE_SILENCE (trans);
243 gst_buffer_map (inbuf, &map, GST_MAP_READ);
245 vad_update (filter->vad, (gint16 *) map.data, map.size / sizeof (gint16));
246 gst_buffer_unmap (inbuf, &map);
248 if (frame_type == VAD_SILENCE) {
249 GST_DEBUG ("Silence detected");
250 if (!filter->silence_detected) {
251 if (!filter->silent) {
252 if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
255 s = gst_structure_new ("removesilence", "silence_detected",
256 G_TYPE_UINT64, GST_BUFFER_PTS (inbuf) - filter->ts_offset, NULL);
257 m = gst_message_new_element (GST_OBJECT (filter), s);
258 gst_element_post_message (GST_ELEMENT (filter), m);
261 filter->silence_detected = TRUE;
264 if (filter->remove) {
265 GST_DEBUG ("Removing silence");
266 if (filter->squash) {
267 if (GST_BUFFER_DURATION_IS_VALID (inbuf)) {
268 filter->ts_offset += inbuf->duration;
270 GST_WARNING ("Invalid buffer duration: ts_offset not updated");
273 return GST_BASE_TRANSFORM_FLOW_DROPPED;
277 if (filter->silence_detected) {
278 if (!filter->silent) {
279 if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
282 s = gst_structure_new ("removesilence", "silence_finished",
283 G_TYPE_UINT64, GST_BUFFER_PTS (inbuf) - filter->ts_offset, NULL);
284 m = gst_message_new_element (GST_OBJECT (filter), s);
285 gst_element_post_message (GST_ELEMENT (filter), m);
288 filter->silence_detected = FALSE;
292 if (filter->squash && filter->ts_offset > 0) {
293 if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
294 inbuf = gst_buffer_make_writable (inbuf);
295 GST_BUFFER_PTS (inbuf) -= filter->ts_offset;
297 GST_WARNING ("Invalid buffer pts, update not possibile");
304 /*Plugin init functions*/
306 plugin_init (GstPlugin * plugin)
308 return gst_element_register (plugin, "removesilence", GST_RANK_NONE,
309 gst_remove_silence_get_type ());
312 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
315 "Removes silence from an audio stream",
316 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);