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 */
66 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
69 GST_STATIC_CAPS ("audio/x-raw, "
70 "format = (string) " GST_AUDIO_NE (S16) ", "
71 "layout = (string) interleaved, "
72 "rate = (int) [ 1, MAX ], " "channels = (int) 1"));
74 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
77 GST_STATIC_CAPS ("audio/x-raw, "
78 "format = (string) " GST_AUDIO_NE (S16) ", "
79 "layout = (string) interleaved, "
80 "rate = (int) [ 1, MAX ], " "channels = (int) 1"));
83 #define DEBUG_INIT(bla) \
84 GST_DEBUG_CATEGORY_INIT (gst_remove_silence_debug, "removesilence", 0, "removesilence element")
86 #define gst_remove_silence_parent_class parent_class
87 G_DEFINE_TYPE_WITH_CODE (GstRemoveSilence, gst_remove_silence,
88 GST_TYPE_BASE_TRANSFORM, DEBUG_INIT (0));
90 static void gst_remove_silence_set_property (GObject * object, guint prop_id,
91 const GValue * value, GParamSpec * pspec);
92 static void gst_remove_silence_get_property (GObject * object, guint prop_id,
93 GValue * value, GParamSpec * pspec);
95 static GstFlowReturn gst_remove_silence_transform_ip (GstBaseTransform * base,
97 static void gst_remove_silence_finalize (GObject * obj);
99 /* GObject vmethod implementations */
101 /* initialize the removesilence's class */
103 gst_remove_silence_class_init (GstRemoveSilenceClass * klass)
105 GObjectClass *gobject_class;
106 GstElementClass *gstelement_class;
108 gobject_class = (GObjectClass *) klass;
109 gstelement_class = (GstElementClass *) klass;
111 gobject_class->finalize = gst_remove_silence_finalize;
112 gobject_class->set_property = gst_remove_silence_set_property;
113 gobject_class->get_property = gst_remove_silence_get_property;
115 g_object_class_install_property (gobject_class, PROP_REMOVE,
116 g_param_spec_boolean ("remove", "Remove",
117 "Set to true to remove silence from the stream, false otherwhise",
118 FALSE, G_PARAM_READWRITE));
120 g_object_class_install_property (gobject_class, PROP_HYSTERESIS,
121 g_param_spec_uint64 ("hysteresis",
123 "Set the hysteresis (on samples) used on the internal VAD",
124 1, G_MAXUINT64, DEFAULT_VAD_HYSTERESIS, G_PARAM_READWRITE));
126 g_object_class_install_property (gobject_class, PROP_SQUASH,
127 g_param_spec_boolean ("squash", "Squash",
128 "Set to true to retimestamp buffers when silence is removed and so avoid timestamp gap",
129 FALSE, G_PARAM_READWRITE));
131 gst_element_class_set_static_metadata (gstelement_class,
133 "Filter/Effect/Audio",
134 "Removes all the silence periods from the audio stream.",
135 "Tiago Katcipis <tiagokatcipis@gmail.com>\n \
136 Paulo Pizarro <paulo.pizarro@gmail.com>\n \
137 Nicola Murino <nicola.murino@gmail.com>");
139 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
140 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
142 GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
143 GST_DEBUG_FUNCPTR (gst_remove_silence_transform_ip);
146 /* initialize the new element
147 * instantiate pads and add them to element
148 * set pad calback functions
149 * initialize instance structure
152 gst_remove_silence_init (GstRemoveSilence * filter)
154 filter->vad = vad_new (DEFAULT_VAD_HYSTERESIS);
155 filter->remove = FALSE;
156 filter->squash = FALSE;
157 filter->ts_offset = 0;
158 filter->silence_detected = FALSE;
161 GST_DEBUG ("Error initializing VAD !!");
167 gst_remove_silence_finalize (GObject * obj)
169 GstRemoveSilence *filter = GST_REMOVE_SILENCE (obj);
170 GST_DEBUG ("Destroying VAD");
171 vad_destroy (filter->vad);
173 GST_DEBUG ("VAD Destroyed");
174 G_OBJECT_CLASS (parent_class)->finalize (obj);
178 gst_remove_silence_set_property (GObject * object, guint prop_id,
179 const GValue * value, GParamSpec * pspec)
181 GstRemoveSilence *filter = GST_REMOVE_SILENCE (object);
185 filter->remove = g_value_get_boolean (value);
187 case PROP_HYSTERESIS:
188 vad_set_hysteresis (filter->vad, g_value_get_uint64 (value));
191 filter->squash = g_value_get_boolean (value);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 gst_remove_silence_get_property (GObject * object, guint prop_id,
201 GValue * value, GParamSpec * pspec)
203 GstRemoveSilence *filter = GST_REMOVE_SILENCE (object);
207 g_value_set_boolean (value, filter->remove);
209 case PROP_HYSTERESIS:
210 g_value_set_uint64 (value, vad_get_hysteresis (filter->vad));
213 g_value_set_boolean (value, filter->squash);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 gst_remove_silence_transform_ip (GstBaseTransform * trans, GstBuffer * inbuf)
224 GstRemoveSilence *filter = NULL;
228 filter = GST_REMOVE_SILENCE (trans);
230 gst_buffer_map (inbuf, &map, GST_MAP_READ);
232 vad_update (filter->vad, (gint16 *) map.data, map.size / sizeof (gint16));
233 gst_buffer_unmap (inbuf, &map);
235 if (frame_type == VAD_SILENCE) {
236 GST_DEBUG ("Silence detected");
237 if (!filter->silence_detected) {
238 if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
241 s = gst_structure_new ("removesilence", "silence_detected",
242 G_TYPE_UINT64, GST_BUFFER_PTS (inbuf) - filter->ts_offset, NULL);
243 m = gst_message_new_element (GST_OBJECT (filter), s);
244 gst_element_post_message (GST_ELEMENT (filter), m);
246 filter->silence_detected = TRUE;
249 if (filter->remove) {
250 GST_DEBUG ("Removing silence");
251 if (filter->squash) {
252 if (GST_BUFFER_DURATION_IS_VALID (inbuf)) {
253 filter->ts_offset += inbuf->duration;
255 GST_WARNING ("Invalid buffer duration: ts_offset not updated");
258 return GST_BASE_TRANSFORM_FLOW_DROPPED;
262 if (filter->silence_detected) {
263 if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
266 s = gst_structure_new ("removesilence", "silence_finished",
267 G_TYPE_UINT64, GST_BUFFER_PTS (inbuf) - filter->ts_offset, NULL);
268 m = gst_message_new_element (GST_OBJECT (filter), s);
269 gst_element_post_message (GST_ELEMENT (filter), m);
271 filter->silence_detected = FALSE;
275 if (filter->squash && filter->ts_offset > 0) {
276 if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
277 inbuf = gst_buffer_make_writable (inbuf);
278 GST_BUFFER_PTS (inbuf) -= filter->ts_offset;
280 GST_WARNING ("Invalid buffer pts, update not possibile");
287 /*Plugin init functions*/
289 plugin_init (GstPlugin * plugin)
291 return gst_element_register (plugin, "removesilence", GST_RANK_NONE,
292 gst_remove_silence_get_type ());
295 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
298 "Removes silence from an audio stream",
299 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);