2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2002,2003,2005
4 * Thomas Vander Stichele <thomas at apestaart dot org>
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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 * SECTION:element-cutter
24 * Analyses the audio signal for periods of silence. The start and end of
25 * silence is signalled by bus messages named
26 * <classname>"cutter"</classname>.
27 * The message's structure contains two fields:
32 * <classname>"timestamp"</classname>:
33 * the timestamp of the buffer that triggered the message.
39 * <classname>"above"</classname>:
40 * %TRUE for begin of silence and %FALSE for end of silence.
46 * <title>Example launch line</title>
48 * gst-launch -m filesrc location=foo.ogg ! decodebin ! audioconvert ! cutter ! autoaudiosink
49 * ]| Show cut messages.
57 #include <gst/audio/audio.h>
58 #include "gstcutter.h"
61 GST_DEBUG_CATEGORY_STATIC (cutter_debug);
62 #define GST_CAT_DEFAULT cutter_debug
64 #define CUTTER_DEFAULT_THRESHOLD_LEVEL 0.1
65 #define CUTTER_DEFAULT_THRESHOLD_LENGTH (500 * GST_MSECOND)
66 #define CUTTER_DEFAULT_PRE_LENGTH (200 * GST_MSECOND)
68 static const GstElementDetails cutter_details =
69 GST_ELEMENT_DETAILS ("Audio cutter",
70 "Filter/Editor/Audio",
71 "Audio Cutter to split audio into non-silent bits",
72 "Thomas Vander Stichele <thomas at apestaart dot org>");
74 static GstStaticPadTemplate cutter_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
77 GST_STATIC_CAPS ("audio/x-raw-int, "
78 "rate = (int) [ 1, MAX ], "
79 "channels = (int) [ 1, MAX ], "
80 "endianness = (int) BYTE_ORDER, "
81 "width = (int) { 8, 16 }, "
82 "depth = (int) { 8, 16 }, " "signed = (boolean) true")
85 static GstStaticPadTemplate cutter_sink_factory =
86 GST_STATIC_PAD_TEMPLATE ("sink",
89 GST_STATIC_CAPS ("audio/x-raw-int, "
90 "rate = (int) [ 1, MAX ], "
91 "channels = (int) [ 1, MAX ], "
92 "endianness = (int) BYTE_ORDER, "
93 "width = (int) { 8, 16 }, "
94 "depth = (int) { 8, 16 }, " "signed = (boolean) true")
107 GST_BOILERPLATE (GstCutter, gst_cutter, GstElement, GST_TYPE_ELEMENT);
109 static void gst_cutter_set_property (GObject * object, guint prop_id,
110 const GValue * value, GParamSpec * pspec);
111 static void gst_cutter_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec);
114 static GstFlowReturn gst_cutter_chain (GstPad * pad, GstBuffer * buffer);
116 static gboolean gst_cutter_get_caps (GstPad * pad, GstCutter * filter);
119 gst_cutter_base_init (gpointer g_class)
121 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
123 gst_element_class_add_pad_template (element_class,
124 gst_static_pad_template_get (&cutter_src_factory));
125 gst_element_class_add_pad_template (element_class,
126 gst_static_pad_template_get (&cutter_sink_factory));
127 gst_element_class_set_details (element_class, &cutter_details);
131 gst_cutter_class_init (GstCutterClass * klass)
133 GObjectClass *gobject_class;
135 gobject_class = (GObjectClass *) klass;
137 gobject_class->set_property = gst_cutter_set_property;
138 gobject_class->get_property = gst_cutter_get_property;
140 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_THRESHOLD,
141 g_param_spec_double ("threshold", "Threshold",
142 "Volume threshold before trigger",
143 -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
144 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_THRESHOLD_DB,
145 g_param_spec_double ("threshold-dB", "Threshold (dB)",
146 "Volume threshold before trigger (in dB)",
147 -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
148 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_RUN_LENGTH,
149 g_param_spec_uint64 ("run-length", "Run length",
150 "Length of drop below threshold before cut_stop (in nanoseconds)",
151 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
152 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PRE_LENGTH,
153 g_param_spec_uint64 ("pre-length", "Pre-recording buffer length",
154 "Length of pre-recording buffer (in nanoseconds)",
155 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
156 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LEAKY,
157 g_param_spec_boolean ("leaky", "Leaky",
158 "do we leak buffers when below threshold ?",
159 FALSE, G_PARAM_READWRITE));
161 GST_DEBUG_CATEGORY_INIT (cutter_debug, "cutter", 0, "Audio cutting");
165 gst_cutter_init (GstCutter * filter, GstCutterClass * g_class)
168 gst_pad_new_from_static_template (&cutter_sink_factory, "sink");
170 gst_pad_new_from_static_template (&cutter_src_factory, "src");
172 filter->threshold_level = CUTTER_DEFAULT_THRESHOLD_LEVEL;
173 filter->threshold_length = CUTTER_DEFAULT_THRESHOLD_LENGTH;
174 filter->silent_run_length = 0 * GST_SECOND;
175 filter->silent = TRUE;
176 filter->silent_prev = FALSE; /* previous value of silent */
178 filter->pre_length = CUTTER_DEFAULT_PRE_LENGTH;
179 filter->pre_run_length = 0 * GST_SECOND;
180 filter->pre_buffer = NULL;
181 filter->leaky = FALSE;
183 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
184 gst_pad_set_chain_function (filter->sinkpad, gst_cutter_chain);
185 gst_pad_use_fixed_caps (filter->sinkpad);
187 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
188 gst_pad_use_fixed_caps (filter->srcpad);
192 gst_cutter_message_new (GstCutter * c, gboolean above, GstClockTime timestamp)
197 g_value_init (&v, GST_TYPE_LIST);
199 s = gst_structure_new ("cutter",
200 "above", G_TYPE_BOOLEAN, above,
201 "timestamp", GST_TYPE_CLOCK_TIME, timestamp, NULL);
203 return gst_message_new_element (GST_OBJECT (c), s);
206 /* Calculate the Normalized Cumulative Square over a buffer of the given type
207 * and over all channels combined */
209 #define DEFINE_CUTTER_CALCULATOR(TYPE, RESOLUTION) \
211 gst_cutter_calculate_##TYPE (TYPE * in, guint num, \
215 double squaresum = 0.0; /* square sum of the integer samples */ \
216 register double square = 0.0; /* Square */ \
217 gdouble normalizer; /* divisor to get a [-1.0, 1.0] range */ \
219 *NCS = 0.0; /* Normalized Cumulative Square */ \
221 normalizer = (double) (1 << (RESOLUTION * 2)); \
223 for (j = 0; j < num; j++) \
225 square = ((double) in[j]) * in[j]; \
226 squaresum += square; \
230 *NCS = squaresum / normalizer; \
233 DEFINE_CUTTER_CALCULATOR (gint16, 15);
234 DEFINE_CUTTER_CALCULATOR (gint8, 7);
238 gst_cutter_chain (GstPad * pad, GstBuffer * buf)
243 gdouble NCS = 0.0; /* Normalized Cumulative Square of buffer */
244 gdouble RMS = 0.0; /* RMS of signal in buffer */
245 gdouble NMS = 0.0; /* Normalized Mean Square of buffer */
246 GstBuffer *prebuf; /* pointer to a prebuffer element */
248 g_return_val_if_fail (pad != NULL, GST_FLOW_ERROR);
249 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
250 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
252 filter = GST_CUTTER (GST_OBJECT_PARENT (pad));
253 g_return_val_if_fail (filter != NULL, GST_FLOW_ERROR);
254 g_return_val_if_fail (GST_IS_CUTTER (filter), GST_FLOW_ERROR);
256 if (!filter->have_caps) {
257 if (!(gst_cutter_get_caps (pad, filter)))
258 return GST_FLOW_NOT_NEGOTIATED;
261 in_data = (gint16 *) GST_BUFFER_DATA (buf);
262 GST_LOG_OBJECT (filter, "length of prerec buffer: %" GST_TIME_FORMAT,
263 GST_TIME_ARGS (filter->pre_run_length));
265 /* calculate mean square value on buffer */
266 switch (filter->width) {
268 num_samples = GST_BUFFER_SIZE (buf) / 2;
269 gst_cutter_calculate_gint16 (in_data, num_samples, &NCS);
270 NMS = NCS / num_samples;
273 num_samples = GST_BUFFER_SIZE (buf);
274 gst_cutter_calculate_gint8 ((gint8 *) in_data, num_samples, &NCS);
275 NMS = NCS / num_samples;
278 /* this shouldn't happen */
279 g_warning ("no mean square function for width %d\n", filter->width);
283 filter->silent_prev = filter->silent;
286 /* if RMS below threshold, add buffer length to silent run length count
289 GST_LOG_OBJECT (filter, "buffer stats: NMS %f, RMS %f, audio length %f", NMS,
291 gst_guint64_to_gdouble (gst_audio_duration_from_pad_buffer
292 (filter->sinkpad, buf)));
293 if (RMS < filter->threshold_level)
294 filter->silent_run_length +=
295 gst_guint64_to_gdouble (gst_audio_duration_from_pad_buffer
296 (filter->sinkpad, buf));
298 filter->silent_run_length = 0 * GST_SECOND;
299 filter->silent = FALSE;
302 if (filter->silent_run_length > filter->threshold_length)
303 /* it has been silent long enough, flag it */
304 filter->silent = TRUE;
306 /* has the silent status changed ? if so, send right signal
307 * and, if from silent -> not silent, flush pre_record buffer
309 if (filter->silent != filter->silent_prev) {
310 if (filter->silent) {
312 gst_cutter_message_new (filter, FALSE, GST_BUFFER_TIMESTAMP (buf));
313 GST_DEBUG_OBJECT (filter, "signaling CUT_STOP");
314 gst_element_post_message (GST_ELEMENT (filter), m);
318 gst_cutter_message_new (filter, TRUE, GST_BUFFER_TIMESTAMP (buf));
320 GST_DEBUG_OBJECT (filter, "signaling CUT_START");
321 gst_element_post_message (GST_ELEMENT (filter), m);
322 /* first of all, flush current buffer */
323 GST_DEBUG_OBJECT (filter, "flushing buffer of length %" GST_TIME_FORMAT,
324 GST_TIME_ARGS (filter->pre_run_length));
325 while (filter->pre_buffer) {
326 prebuf = (g_list_first (filter->pre_buffer))->data;
327 filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf);
328 gst_pad_push (filter->srcpad, prebuf);
331 GST_DEBUG_OBJECT (filter, "flushed %d buffers", count);
332 filter->pre_run_length = 0 * GST_SECOND;
335 /* now check if we have to send the new buffer to the internal buffer cache
336 * or to the srcpad */
337 if (filter->silent) {
338 filter->pre_buffer = g_list_append (filter->pre_buffer, buf);
339 filter->pre_run_length +=
340 gst_guint64_to_gdouble (gst_audio_duration_from_pad_buffer
341 (filter->sinkpad, buf));
342 while (filter->pre_run_length > filter->pre_length) {
343 prebuf = (g_list_first (filter->pre_buffer))->data;
344 g_assert (GST_IS_BUFFER (prebuf));
345 filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf);
346 filter->pre_run_length -=
347 gst_guint64_to_gdouble (gst_audio_duration_from_pad_buffer
348 (filter->sinkpad, prebuf));
349 /* only pass buffers if we don't leak */
351 gst_pad_push (filter->srcpad, prebuf);
353 gst_buffer_unref (prebuf);
356 gst_pad_push (filter->srcpad, buf);
363 gst_cutter_get_caps (GstPad * pad, GstCutter * filter)
366 GstStructure *structure;
368 caps = gst_pad_get_caps (pad);
370 GST_INFO ("no caps on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
373 structure = gst_caps_get_structure (caps, 0);
374 gst_structure_get_int (structure, "width", &filter->width);
375 filter->max_sample = 1 << (filter->width - 1); /* signed */
376 filter->have_caps = TRUE;
378 gst_caps_unref (caps);
384 gst_cutter_set_property (GObject * object, guint prop_id,
385 const GValue * value, GParamSpec * pspec)
389 g_return_if_fail (GST_IS_CUTTER (object));
390 filter = GST_CUTTER (object);
394 filter->threshold_level = g_value_get_double (value);
395 GST_DEBUG ("DEBUG: set threshold level to %f", filter->threshold_level);
397 case PROP_THRESHOLD_DB:
398 /* set the level given in dB
399 * value in dB = 20 * log (value)
400 * values in dB < 0 result in values between 0 and 1
402 filter->threshold_level = pow (10, g_value_get_double (value) / 20);
403 GST_DEBUG_OBJECT (filter, "set threshold level to %f",
404 filter->threshold_level);
406 case PROP_RUN_LENGTH:
407 /* set the minimum length of the silent run required */
408 filter->threshold_length =
409 gst_guint64_to_gdouble (g_value_get_uint64 (value));
411 case PROP_PRE_LENGTH:
412 /* set the length of the pre-record block */
413 filter->pre_length = gst_guint64_to_gdouble (g_value_get_uint64 (value));
416 /* set if the pre-record buffer is leaky or not */
417 filter->leaky = g_value_get_boolean (value);
420 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
426 gst_cutter_get_property (GObject * object, guint prop_id,
427 GValue * value, GParamSpec * pspec)
431 g_return_if_fail (GST_IS_CUTTER (object));
432 filter = GST_CUTTER (object);
435 case PROP_RUN_LENGTH:
436 g_value_set_uint64 (value, filter->threshold_length);
439 g_value_set_double (value, filter->threshold_level);
441 case PROP_THRESHOLD_DB:
442 g_value_set_double (value, 20 * log (filter->threshold_level));
444 case PROP_PRE_LENGTH:
445 g_value_set_uint64 (value, filter->pre_length);
448 g_value_set_boolean (value, filter->leaky);
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
457 plugin_init (GstPlugin * plugin)
459 if (!gst_element_register (plugin, "cutter", GST_RANK_NONE, GST_TYPE_CUTTER))
465 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
468 "Audio Cutter to split audio into non-silent bits",
469 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);