2002-03-20 21:45:03 +00:00
|
|
|
/* GStreamer
|
2001-12-22 23:27:31 +00:00
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
2005-11-21 19:50:25 +00:00
|
|
|
* Copyright (C) 2002,2003,2005
|
|
|
|
* Thomas Vander Stichele <thomas at apestaart dot org>
|
2001-12-22 23:27:31 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2001-12-22 23:27:31 +00:00
|
|
|
*/
|
2009-01-28 10:29:42 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-cutter
|
2018-10-22 09:39:24 +00:00
|
|
|
* @title: cutter
|
2009-01-28 10:29:42 +00:00
|
|
|
*
|
|
|
|
* Analyses the audio signal for periods of silence. The start and end of
|
|
|
|
* silence is signalled by bus messages named
|
2018-10-22 09:39:24 +00:00
|
|
|
* `cutter`.
|
|
|
|
*
|
2009-01-28 10:29:42 +00:00
|
|
|
* The message's structure contains two fields:
|
|
|
|
*
|
2018-10-22 09:39:24 +00:00
|
|
|
* * #GstClockTime `timestamp`: the timestamp of the buffer that triggered the message.
|
|
|
|
* * gboolean `above`: %TRUE for begin of silence and %FALSE for end of silence.
|
|
|
|
*
|
|
|
|
* ## Example launch line
|
2009-01-28 10:29:42 +00:00
|
|
|
* |[
|
2012-08-26 21:39:55 +00:00
|
|
|
* gst-launch-1.0 -m filesrc location=foo.ogg ! decodebin ! audioconvert ! cutter ! autoaudiosink
|
2009-01-28 10:29:42 +00:00
|
|
|
* ]| Show cut messages.
|
2018-10-22 09:39:24 +00:00
|
|
|
*
|
2009-01-28 10:29:42 +00:00
|
|
|
*/
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2003-06-29 19:46:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2001-12-22 23:27:31 +00:00
|
|
|
#include <gst/gst.h>
|
2001-12-23 04:38:37 +00:00
|
|
|
#include <gst/audio/audio.h>
|
|
|
|
#include "gstcutter.h"
|
2001-12-22 23:27:31 +00:00
|
|
|
#include "math.h"
|
|
|
|
|
2006-06-22 19:31:04 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (cutter_debug);
|
2005-11-21 19:50:25 +00:00
|
|
|
#define GST_CAT_DEFAULT cutter_debug
|
|
|
|
|
|
|
|
#define CUTTER_DEFAULT_THRESHOLD_LEVEL 0.1
|
|
|
|
#define CUTTER_DEFAULT_THRESHOLD_LENGTH (500 * GST_MSECOND)
|
|
|
|
#define CUTTER_DEFAULT_PRE_LENGTH (200 * GST_MSECOND)
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static GstStaticPadTemplate cutter_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2011-08-19 12:01:45 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw, "
|
2014-02-04 12:43:56 +00:00
|
|
|
"format = (string) { S8," GST_AUDIO_NE (S16) " }, "
|
2012-01-04 10:11:06 +00:00
|
|
|
"rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ], "
|
|
|
|
"layout = (string) interleaved")
|
2004-03-14 22:34:33 +00:00
|
|
|
);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate cutter_sink_factory =
|
2005-11-22 13:13:21 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-01-04 10:11:06 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw, "
|
2014-02-04 12:43:56 +00:00
|
|
|
"format = (string) { S8," GST_AUDIO_NE (S16) " }, "
|
2012-01-04 10:11:06 +00:00
|
|
|
"rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ], "
|
|
|
|
"layout = (string) interleaved")
|
2004-03-14 22:34:33 +00:00
|
|
|
);
|
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_THRESHOLD,
|
|
|
|
PROP_THRESHOLD_DB,
|
|
|
|
PROP_RUN_LENGTH,
|
|
|
|
PROP_PRE_LENGTH,
|
|
|
|
PROP_LEAKY
|
|
|
|
};
|
|
|
|
|
2011-06-30 09:35:21 +00:00
|
|
|
#define gst_cutter_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstCutter, gst_cutter, GST_TYPE_ELEMENT);
|
2021-02-16 16:11:26 +00:00
|
|
|
GST_ELEMENT_REGISTER_DEFINE (cutter, "cutter", GST_RANK_NONE, GST_TYPE_CUTTER);
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2015-10-05 04:10:56 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_cutter_change_state (GstElement * element, GstStateChange transition);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static void gst_cutter_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_cutter_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
2011-11-17 14:02:55 +00:00
|
|
|
static gboolean gst_cutter_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
|
|
|
static GstFlowReturn gst_cutter_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buffer);
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_cutter_class_init (GstCutterClass * klass)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2011-06-30 09:35:21 +00:00
|
|
|
GstElementClass *element_class;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
2011-06-30 09:35:21 +00:00
|
|
|
element_class = (GstElementClass *) klass;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
gobject_class->set_property = gst_cutter_set_property;
|
|
|
|
gobject_class->get_property = gst_cutter_get_property;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_THRESHOLD,
|
2004-03-14 22:34:33 +00:00
|
|
|
g_param_spec_double ("threshold", "Threshold",
|
2004-03-15 19:32:27 +00:00
|
|
|
"Volume threshold before trigger",
|
2010-10-13 14:21:23 +00:00
|
|
|
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2005-11-21 19:50:25 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_THRESHOLD_DB,
|
|
|
|
g_param_spec_double ("threshold-dB", "Threshold (dB)",
|
2004-03-15 19:32:27 +00:00
|
|
|
"Volume threshold before trigger (in dB)",
|
2010-10-13 14:21:23 +00:00
|
|
|
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2005-11-21 19:50:25 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_RUN_LENGTH,
|
|
|
|
g_param_spec_uint64 ("run-length", "Run length",
|
|
|
|
"Length of drop below threshold before cut_stop (in nanoseconds)",
|
2010-10-13 14:21:23 +00:00
|
|
|
0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2005-11-21 19:50:25 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PRE_LENGTH,
|
|
|
|
g_param_spec_uint64 ("pre-length", "Pre-recording buffer length",
|
|
|
|
"Length of pre-recording buffer (in nanoseconds)",
|
2010-10-13 14:21:23 +00:00
|
|
|
0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2005-11-21 19:50:25 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LEAKY,
|
2004-03-14 22:34:33 +00:00
|
|
|
g_param_spec_boolean ("leaky", "Leaky",
|
2004-03-15 19:32:27 +00:00
|
|
|
"do we leak buffers when below threshold ?",
|
2010-10-13 14:21:23 +00:00
|
|
|
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (cutter_debug, "cutter", 0, "Audio cutting");
|
2011-06-30 09:35:21 +00:00
|
|
|
|
2016-03-04 01:30:12 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&cutter_src_factory);
|
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&cutter_sink_factory);
|
2012-04-09 23:51:41 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class, "Audio cutter",
|
2016-03-04 01:30:12 +00:00
|
|
|
"Filter/Editor/Audio", "Audio Cutter to split audio into non-silent bits",
|
2011-06-30 09:35:21 +00:00
|
|
|
"Thomas Vander Stichele <thomas at apestaart dot org>");
|
2015-10-05 04:10:56 +00:00
|
|
|
element_class->change_state = gst_cutter_change_state;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-30 09:35:21 +00:00
|
|
|
gst_cutter_init (GstCutter * filter)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
filter->sinkpad =
|
2006-03-15 16:17:12 +00:00
|
|
|
gst_pad_new_from_static_template (&cutter_sink_factory, "sink");
|
2011-08-20 08:46:18 +00:00
|
|
|
gst_pad_set_chain_function (filter->sinkpad, gst_cutter_chain);
|
|
|
|
gst_pad_set_event_function (filter->sinkpad, gst_cutter_event);
|
|
|
|
gst_pad_use_fixed_caps (filter->sinkpad);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
filter->srcpad =
|
2006-03-15 16:17:12 +00:00
|
|
|
gst_pad_new_from_static_template (&cutter_src_factory, "src");
|
2011-08-20 08:46:18 +00:00
|
|
|
gst_pad_use_fixed_caps (filter->srcpad);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
filter->threshold_level = CUTTER_DEFAULT_THRESHOLD_LEVEL;
|
|
|
|
filter->threshold_length = CUTTER_DEFAULT_THRESHOLD_LENGTH;
|
|
|
|
filter->silent_run_length = 0 * GST_SECOND;
|
2001-12-22 23:27:31 +00:00
|
|
|
filter->silent = TRUE;
|
2007-04-18 12:36:37 +00:00
|
|
|
filter->silent_prev = FALSE; /* previous value of silent */
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
filter->pre_length = CUTTER_DEFAULT_PRE_LENGTH;
|
|
|
|
filter->pre_run_length = 0 * GST_SECOND;
|
2001-12-22 23:27:31 +00:00
|
|
|
filter->pre_buffer = NULL;
|
2003-05-10 16:57:38 +00:00
|
|
|
filter->leaky = FALSE;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
static GstMessage *
|
|
|
|
gst_cutter_message_new (GstCutter * c, gboolean above, GstClockTime timestamp)
|
|
|
|
{
|
|
|
|
GstStructure *s;
|
|
|
|
|
|
|
|
s = gst_structure_new ("cutter",
|
|
|
|
"above", G_TYPE_BOOLEAN, above,
|
|
|
|
"timestamp", GST_TYPE_CLOCK_TIME, timestamp, NULL);
|
|
|
|
|
|
|
|
return gst_message_new_element (GST_OBJECT (c), s);
|
|
|
|
}
|
|
|
|
|
2005-11-22 13:13:21 +00:00
|
|
|
/* Calculate the Normalized Cumulative Square over a buffer of the given type
|
|
|
|
* and over all channels combined */
|
|
|
|
|
|
|
|
#define DEFINE_CUTTER_CALCULATOR(TYPE, RESOLUTION) \
|
|
|
|
static void inline \
|
|
|
|
gst_cutter_calculate_##TYPE (TYPE * in, guint num, \
|
|
|
|
double *NCS) \
|
|
|
|
{ \
|
|
|
|
register int j; \
|
|
|
|
double squaresum = 0.0; /* square sum of the integer samples */ \
|
|
|
|
register double square = 0.0; /* Square */ \
|
|
|
|
gdouble normalizer; /* divisor to get a [-1.0, 1.0] range */ \
|
|
|
|
\
|
|
|
|
*NCS = 0.0; /* Normalized Cumulative Square */ \
|
|
|
|
\
|
|
|
|
normalizer = (double) (1 << (RESOLUTION * 2)); \
|
|
|
|
\
|
|
|
|
for (j = 0; j < num; j++) \
|
|
|
|
{ \
|
|
|
|
square = ((double) in[j]) * in[j]; \
|
|
|
|
squaresum += square; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
\
|
|
|
|
*NCS = squaresum / normalizer; \
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_CUTTER_CALCULATOR (gint16, 15);
|
|
|
|
DEFINE_CUTTER_CALCULATOR (gint8, 7);
|
|
|
|
|
2011-08-20 08:46:18 +00:00
|
|
|
static gboolean
|
|
|
|
gst_cutter_setcaps (GstCutter * filter, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstAudioInfo info;
|
|
|
|
|
|
|
|
if (!gst_audio_info_from_caps (&info, caps))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
filter->info = info;
|
|
|
|
|
|
|
|
return gst_pad_set_caps (filter->srcpad, caps);
|
|
|
|
}
|
|
|
|
|
2015-10-05 04:10:56 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_cutter_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
GstCutter *filter = GST_CUTTER (element);
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
g_list_free_full (filter->pre_buffer, (GDestroyNotify) gst_buffer_unref);
|
|
|
|
filter->pre_buffer = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-20 08:46:18 +00:00
|
|
|
static gboolean
|
2011-11-17 14:02:55 +00:00
|
|
|
gst_cutter_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2011-08-20 08:46:18 +00:00
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
GstCutter *filter;
|
|
|
|
|
2011-11-17 14:02:55 +00:00
|
|
|
filter = GST_CUTTER (parent);
|
2011-08-20 08:46:18 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
gst_event_parse_caps (event, &caps);
|
|
|
|
ret = gst_cutter_setcaps (filter, caps);
|
2011-11-17 14:02:55 +00:00
|
|
|
gst_event_unref (event);
|
2011-08-20 08:46:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2011-11-17 14:02:55 +00:00
|
|
|
ret = gst_pad_event_default (pad, parent, event);
|
2011-08-20 08:46:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2005-11-22 13:13:21 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
static GstFlowReturn
|
2011-11-17 14:02:55 +00:00
|
|
|
gst_cutter_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2011-08-20 08:46:18 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2001-12-22 23:27:31 +00:00
|
|
|
GstCutter *filter;
|
2012-01-23 16:25:37 +00:00
|
|
|
GstMapInfo map;
|
2001-12-22 23:27:31 +00:00
|
|
|
gint16 *in_data;
|
2011-08-20 08:46:18 +00:00
|
|
|
gint bpf, rate;
|
2011-06-30 09:35:21 +00:00
|
|
|
gsize in_size;
|
2005-11-22 13:13:21 +00:00
|
|
|
guint num_samples;
|
|
|
|
gdouble NCS = 0.0; /* Normalized Cumulative Square of buffer */
|
|
|
|
gdouble RMS = 0.0; /* RMS of signal in buffer */
|
|
|
|
gdouble NMS = 0.0; /* Normalized Mean Square of buffer */
|
2004-03-15 19:32:27 +00:00
|
|
|
GstBuffer *prebuf; /* pointer to a prebuffer element */
|
2011-08-20 08:46:18 +00:00
|
|
|
GstClockTime duration;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2011-11-17 14:02:55 +00:00
|
|
|
filter = GST_CUTTER (parent);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2011-08-20 08:46:18 +00:00
|
|
|
if (GST_AUDIO_INFO_FORMAT (&filter->info) == GST_AUDIO_FORMAT_UNKNOWN)
|
|
|
|
goto not_negotiated;
|
|
|
|
|
|
|
|
bpf = GST_AUDIO_INFO_BPF (&filter->info);
|
|
|
|
rate = GST_AUDIO_INFO_RATE (&filter->info);
|
|
|
|
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
|
|
|
in_data = (gint16 *) map.data;
|
|
|
|
in_size = map.size;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
GST_LOG_OBJECT (filter, "length of prerec buffer: %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (filter->pre_run_length));
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* calculate mean square value on buffer */
|
2011-08-20 08:46:18 +00:00
|
|
|
switch (GST_AUDIO_INFO_FORMAT (&filter->info)) {
|
|
|
|
case GST_AUDIO_FORMAT_S16:
|
2011-06-30 09:35:21 +00:00
|
|
|
num_samples = in_size / 2;
|
2005-11-22 13:13:21 +00:00
|
|
|
gst_cutter_calculate_gint16 (in_data, num_samples, &NCS);
|
|
|
|
NMS = NCS / num_samples;
|
2001-12-22 23:27:31 +00:00
|
|
|
break;
|
2011-08-20 08:46:18 +00:00
|
|
|
case GST_AUDIO_FORMAT_S8:
|
2011-06-30 09:35:21 +00:00
|
|
|
num_samples = in_size;
|
2005-11-22 13:13:21 +00:00
|
|
|
gst_cutter_calculate_gint8 ((gint8 *) in_data, num_samples, &NCS);
|
|
|
|
NMS = NCS / num_samples;
|
2001-12-22 23:27:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* this shouldn't happen */
|
2011-08-20 08:46:18 +00:00
|
|
|
g_warning ("no mean square function for format");
|
2001-12-22 23:27:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
2011-06-30 09:35:21 +00:00
|
|
|
|
2007-04-18 12:36:37 +00:00
|
|
|
filter->silent_prev = filter->silent;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2011-08-20 08:46:18 +00:00
|
|
|
duration = gst_util_uint64_scale (in_size / bpf, GST_SECOND, rate);
|
|
|
|
|
2005-11-22 13:13:21 +00:00
|
|
|
RMS = sqrt (NMS);
|
2003-05-10 16:57:38 +00:00
|
|
|
/* if RMS below threshold, add buffer length to silent run length count
|
2001-12-22 23:27:31 +00:00
|
|
|
* if not, reset
|
|
|
|
*/
|
2006-10-05 16:37:33 +00:00
|
|
|
GST_LOG_OBJECT (filter, "buffer stats: NMS %f, RMS %f, audio length %f", NMS,
|
2011-08-20 08:46:18 +00:00
|
|
|
RMS, gst_guint64_to_gdouble (duration));
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
if (RMS < filter->threshold_level)
|
2011-08-20 08:46:18 +00:00
|
|
|
filter->silent_run_length += gst_guint64_to_gdouble (duration);
|
2004-03-14 22:34:33 +00:00
|
|
|
else {
|
2005-11-21 19:50:25 +00:00
|
|
|
filter->silent_run_length = 0 * GST_SECOND;
|
2001-12-22 23:27:31 +00:00
|
|
|
filter->silent = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filter->silent_run_length > filter->threshold_length)
|
|
|
|
/* it has been silent long enough, flag it */
|
|
|
|
filter->silent = TRUE;
|
|
|
|
|
2003-05-10 16:57:38 +00:00
|
|
|
/* has the silent status changed ? if so, send right signal
|
|
|
|
* and, if from silent -> not silent, flush pre_record buffer
|
2001-12-22 23:27:31 +00:00
|
|
|
*/
|
2007-04-18 12:36:37 +00:00
|
|
|
if (filter->silent != filter->silent_prev) {
|
2004-03-14 22:34:33 +00:00
|
|
|
if (filter->silent) {
|
2005-11-21 19:50:25 +00:00
|
|
|
GstMessage *m =
|
|
|
|
gst_cutter_message_new (filter, FALSE, GST_BUFFER_TIMESTAMP (buf));
|
|
|
|
GST_DEBUG_OBJECT (filter, "signaling CUT_STOP");
|
|
|
|
gst_element_post_message (GST_ELEMENT (filter), m);
|
2004-03-14 22:34:33 +00:00
|
|
|
} else {
|
2003-05-10 16:57:38 +00:00
|
|
|
gint count = 0;
|
2005-11-21 19:50:25 +00:00
|
|
|
GstMessage *m =
|
|
|
|
gst_cutter_message_new (filter, TRUE, GST_BUFFER_TIMESTAMP (buf));
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
GST_DEBUG_OBJECT (filter, "signaling CUT_START");
|
|
|
|
gst_element_post_message (GST_ELEMENT (filter), m);
|
2001-12-22 23:27:31 +00:00
|
|
|
/* first of all, flush current buffer */
|
2005-11-21 19:50:25 +00:00
|
|
|
GST_DEBUG_OBJECT (filter, "flushing buffer of length %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (filter->pre_run_length));
|
2011-08-20 08:46:18 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
while (filter->pre_buffer) {
|
2004-03-15 19:32:27 +00:00
|
|
|
prebuf = (g_list_first (filter->pre_buffer))->data;
|
|
|
|
filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf);
|
2005-11-21 19:50:25 +00:00
|
|
|
gst_pad_push (filter->srcpad, prebuf);
|
2004-03-15 19:32:27 +00:00
|
|
|
++count;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
2005-11-21 19:50:25 +00:00
|
|
|
GST_DEBUG_OBJECT (filter, "flushed %d buffers", count);
|
|
|
|
filter->pre_run_length = 0 * GST_SECOND;
|
2003-05-10 16:57:38 +00:00
|
|
|
}
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
2003-05-10 16:57:38 +00:00
|
|
|
/* now check if we have to send the new buffer to the internal buffer cache
|
|
|
|
* or to the srcpad */
|
2004-03-14 22:34:33 +00:00
|
|
|
if (filter->silent) {
|
|
|
|
filter->pre_buffer = g_list_append (filter->pre_buffer, buf);
|
2011-08-20 08:46:18 +00:00
|
|
|
filter->pre_run_length += gst_guint64_to_gdouble (duration);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
while (filter->pre_run_length > filter->pre_length) {
|
2011-08-20 08:46:18 +00:00
|
|
|
GstClockTime pduration;
|
|
|
|
gsize psize;
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
prebuf = (g_list_first (filter->pre_buffer))->data;
|
|
|
|
g_assert (GST_IS_BUFFER (prebuf));
|
2011-08-20 08:46:18 +00:00
|
|
|
|
|
|
|
psize = gst_buffer_get_size (prebuf);
|
|
|
|
pduration = gst_util_uint64_scale (psize / bpf, GST_SECOND, rate);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf);
|
2011-08-20 08:46:18 +00:00
|
|
|
filter->pre_run_length -= gst_guint64_to_gdouble (pduration);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
/* only pass buffers if we don't leak */
|
|
|
|
if (!filter->leaky)
|
2011-08-20 08:46:18 +00:00
|
|
|
ret = gst_pad_push (filter->srcpad, prebuf);
|
2007-04-18 12:36:37 +00:00
|
|
|
else
|
|
|
|
gst_buffer_unref (prebuf);
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
} else
|
2011-08-20 08:46:18 +00:00
|
|
|
ret = gst_pad_push (filter->srcpad, buf);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2011-08-20 08:46:18 +00:00
|
|
|
return ret;
|
2009-01-28 10:29:42 +00:00
|
|
|
|
2011-08-20 08:46:18 +00:00
|
|
|
/* ERRORS */
|
|
|
|
not_negotiated:
|
|
|
|
{
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
2009-01-28 10:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-22 13:13:21 +00:00
|
|
|
static void
|
|
|
|
gst_cutter_set_property (GObject * object, guint prop_id,
|
2004-03-14 22:34:33 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
GstCutter *filter;
|
|
|
|
|
2002-09-09 08:47:32 +00:00
|
|
|
g_return_if_fail (GST_IS_CUTTER (object));
|
2001-12-22 23:27:31 +00:00
|
|
|
filter = GST_CUTTER (object);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
switch (prop_id) {
|
2005-11-21 19:50:25 +00:00
|
|
|
case PROP_THRESHOLD:
|
2001-12-22 23:27:31 +00:00
|
|
|
filter->threshold_level = g_value_get_double (value);
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_DEBUG ("DEBUG: set threshold level to %f", filter->threshold_level);
|
2001-12-22 23:27:31 +00:00
|
|
|
break;
|
2005-11-21 19:50:25 +00:00
|
|
|
case PROP_THRESHOLD_DB:
|
2003-05-10 16:57:38 +00:00
|
|
|
/* set the level given in dB
|
|
|
|
* value in dB = 20 * log (value)
|
2001-12-22 23:27:31 +00:00
|
|
|
* values in dB < 0 result in values between 0 and 1
|
|
|
|
*/
|
|
|
|
filter->threshold_level = pow (10, g_value_get_double (value) / 20);
|
2005-11-22 13:13:21 +00:00
|
|
|
GST_DEBUG_OBJECT (filter, "set threshold level to %f",
|
|
|
|
filter->threshold_level);
|
2001-12-22 23:27:31 +00:00
|
|
|
break;
|
2005-11-21 19:50:25 +00:00
|
|
|
case PROP_RUN_LENGTH:
|
2001-12-22 23:27:31 +00:00
|
|
|
/* set the minimum length of the silent run required */
|
2006-03-30 23:37:16 +00:00
|
|
|
filter->threshold_length =
|
|
|
|
gst_guint64_to_gdouble (g_value_get_uint64 (value));
|
2003-05-10 16:57:38 +00:00
|
|
|
break;
|
2005-11-21 19:50:25 +00:00
|
|
|
case PROP_PRE_LENGTH:
|
2001-12-22 23:27:31 +00:00
|
|
|
/* set the length of the pre-record block */
|
2006-03-30 23:37:16 +00:00
|
|
|
filter->pre_length = gst_guint64_to_gdouble (g_value_get_uint64 (value));
|
2001-12-22 23:27:31 +00:00
|
|
|
break;
|
2005-11-21 19:50:25 +00:00
|
|
|
case PROP_LEAKY:
|
2003-05-10 16:57:38 +00:00
|
|
|
/* set if the pre-record buffer is leaky or not */
|
|
|
|
filter->leaky = g_value_get_boolean (value);
|
|
|
|
break;
|
2001-12-22 23:27:31 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_cutter_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
GstCutter *filter;
|
|
|
|
|
2002-09-09 08:47:32 +00:00
|
|
|
g_return_if_fail (GST_IS_CUTTER (object));
|
2001-12-22 23:27:31 +00:00
|
|
|
filter = GST_CUTTER (object);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
switch (prop_id) {
|
2005-11-21 19:50:25 +00:00
|
|
|
case PROP_RUN_LENGTH:
|
|
|
|
g_value_set_uint64 (value, filter->threshold_length);
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
2005-11-21 19:50:25 +00:00
|
|
|
case PROP_THRESHOLD:
|
2004-03-14 22:34:33 +00:00
|
|
|
g_value_set_double (value, filter->threshold_level);
|
|
|
|
break;
|
2005-11-21 19:50:25 +00:00
|
|
|
case PROP_THRESHOLD_DB:
|
2004-03-14 22:34:33 +00:00
|
|
|
g_value_set_double (value, 20 * log (filter->threshold_level));
|
|
|
|
break;
|
2005-11-21 19:50:25 +00:00
|
|
|
case PROP_PRE_LENGTH:
|
|
|
|
g_value_set_uint64 (value, filter->pre_length);
|
2001-12-22 23:27:31 +00:00
|
|
|
break;
|
2005-11-21 19:50:25 +00:00
|
|
|
case PROP_LEAKY:
|
2003-05-10 16:57:38 +00:00
|
|
|
g_value_set_boolean (value, filter->leaky);
|
|
|
|
break;
|
2001-12-22 23:27:31 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2004-03-14 22:34:33 +00:00
|
|
|
plugin_init (GstPlugin * plugin)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2021-02-16 16:11:26 +00:00
|
|
|
return GST_ELEMENT_REGISTER (cutter, plugin);
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
2012-04-05 15:36:38 +00:00
|
|
|
cutter,
|
2004-03-14 22:34:33 +00:00
|
|
|
"Audio Cutter to split audio into non-silent bits",
|
2005-11-21 19:50:25 +00:00
|
|
|
plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
|