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
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
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)
|
|
|
|
|
Define GstElementDetails as const and also static (when defined as global)
Original commit message from CVS:
* ext/aalib/gstaasink.c:
* ext/annodex/gstcmmldec.c:
* ext/annodex/gstcmmlenc.c:
* ext/cairo/gsttextoverlay.c:
* ext/cairo/gsttimeoverlay.c:
* ext/cdio/gstcdiocddasrc.c:
* ext/dv/gstdvdec.c:
* ext/dv/gstdvdemux.c:
* ext/esd/esdmon.c:
* ext/esd/esdsink.c:
* ext/flac/gstflacenc.c:
* ext/flac/gstflactag.c:
* ext/gconf/gstgconfaudiosink.c: (gst_gconf_audio_sink_base_init):
* ext/gconf/gstgconfaudiosrc.c: (gst_gconf_audio_src_base_init):
* ext/gconf/gstgconfvideosink.c: (gst_gconf_video_sink_base_init):
* ext/gconf/gstgconfvideosrc.c: (gst_gconf_video_src_base_init):
* ext/gdk_pixbuf/pixbufscale.c:
* ext/hal/gsthalaudiosink.c: (gst_hal_audio_sink_base_init):
* ext/hal/gsthalaudiosrc.c: (gst_hal_audio_src_base_init):
* ext/jpeg/gstjpegdec.c:
* ext/jpeg/gstjpegenc.c:
* ext/jpeg/gstsmokedec.c:
* ext/jpeg/gstsmokeenc.c:
* ext/libcaca/gstcacasink.c:
* ext/libmng/gstmngdec.c:
* ext/libmng/gstmngenc.c:
* ext/libpng/gstpngdec.c:
* ext/libpng/gstpngenc.c:
* ext/mikmod/gstmikmod.c:
* ext/raw1394/gstdv1394src.c:
* ext/shout2/gstshout2.c: (gst_shout2send_init):
* ext/shout2/gstshout2.h:
* ext/speex/gstspeexdec.c:
* ext/speex/gstspeexenc.c:
* gst/alpha/gstalpha.c:
* gst/alpha/gstalphacolor.c:
* gst/apetag/gstapedemux.c:
* gst/auparse/gstauparse.c:
* gst/autodetect/gstautoaudiosink.c:
(gst_auto_audio_sink_base_init):
* gst/autodetect/gstautovideosink.c:
(gst_auto_video_sink_base_init):
* gst/avi/gstavidemux.c: (gst_avi_demux_base_init):
* gst/avi/gstavimux.c: (gst_avimux_base_init):
* gst/cutter/gstcutter.c:
* gst/debug/breakmydata.c:
* gst/debug/efence.c:
* gst/debug/gstnavigationtest.c:
* gst/debug/gstnavseek.c:
* gst/debug/negotiation.c:
* gst/debug/progressreport.c:
* gst/debug/testplugin.c:
* gst/effectv/gstaging.c:
* gst/effectv/gstdice.c:
* gst/effectv/gstedge.c:
* gst/effectv/gstquark.c:
* gst/effectv/gstrev.c:
* gst/effectv/gstshagadelic.c:
* gst/effectv/gstvertigo.c:
* gst/effectv/gstwarp.c:
* gst/flx/gstflxdec.c:
* gst/goom/gstgoom.c:
* gst/icydemux/gsticydemux.c:
* gst/id3demux/gstid3demux.c:
* gst/interleave/deinterleave.c:
* gst/interleave/interleave.c:
* gst/law/alaw-decode.c: (gst_alawdec_base_init):
* gst/law/alaw-encode.c: (gst_alawenc_base_init):
* gst/law/mulaw-decode.c: (gst_mulawdec_base_init):
* gst/law/mulaw-encode.c: (gst_mulawenc_base_init):
* gst/level/gstlevel.c:
* gst/matroska/matroska-demux.c: (gst_matroska_demux_base_init):
* gst/matroska/matroska-mux.c: (gst_matroska_mux_base_init):
* gst/median/gstmedian.c:
* gst/monoscope/gstmonoscope.c:
* gst/multipart/multipartdemux.c:
* gst/multipart/multipartmux.c:
* gst/oldcore/gstaggregator.c:
* gst/oldcore/gstfdsink.c:
* gst/oldcore/gstmd5sink.c:
* gst/oldcore/gstmultifilesrc.c:
* gst/oldcore/gstpipefilter.c:
* gst/oldcore/gstshaper.c:
* gst/oldcore/gststatistics.c:
* gst/rtp/gstasteriskh263.c:
* gst/rtp/gstrtpL16depay.c:
* gst/rtp/gstrtpL16pay.c:
* gst/rtp/gstrtpamrdepay.c:
* gst/rtp/gstrtpamrpay.c:
* gst/rtp/gstrtpdepay.c:
* gst/rtp/gstrtpgsmpay.c:
* gst/rtp/gstrtph263pay.c:
* gst/rtp/gstrtph263pdepay.c:
* gst/rtp/gstrtph263ppay.c:
* gst/rtp/gstrtpilbcdepay.c:
* gst/rtp/gstrtpmp4gpay.c:
* gst/rtp/gstrtpmp4vdepay.c:
* gst/rtp/gstrtpmp4vpay.c:
* gst/rtp/gstrtpmpadepay.c:
* gst/rtp/gstrtpmpapay.c:
* gst/rtp/gstrtppcmadepay.c:
* gst/rtp/gstrtppcmapay.c:
* gst/rtp/gstrtppcmudepay.c:
* gst/rtp/gstrtppcmupay.c:
* gst/rtp/gstrtpspeexdepay.c:
* gst/rtp/gstrtpspeexpay.c:
* gst/rtsp/gstrtpdec.c:
* gst/rtsp/gstrtspsrc.c:
* gst/smpte/gstsmpte.c:
* gst/udp/gstdynudpsink.c:
* gst/udp/gstmultiudpsink.c:
* gst/udp/gstudpsink.c:
* gst/udp/gstudpsrc.c:
* gst/videobox/gstvideobox.c:
* gst/videofilter/gstgamma.c: (gst_gamma_base_init):
* gst/videofilter/gstvideobalance.c:
* gst/videofilter/gstvideoflip.c:
* gst/videofilter/gstvideotemplate.c:
(gst_videotemplate_base_init):
* gst/videomixer/videomixer.c:
* gst/wavparse/gstwavparse.c: (gst_wavparse_base_init),
(gst_wavparse_class_init), (gst_wavparse_dispose),
(gst_wavparse_reset), (gst_wavparse_init),
(gst_wavparse_perform_seek), (gst_wavparse_peek_chunk_info),
(gst_wavparse_peek_chunk), (gst_wavparse_stream_headers),
(gst_wavparse_parse_stream_init), (gst_wavparse_send_event),
(gst_wavparse_add_src_pad), (gst_wavparse_stream_data),
(gst_wavparse_chain), (gst_wavparse_srcpad_event),
(gst_wavparse_sink_activate), (gst_wavparse_sink_activate_pull),
(gst_wavparse_change_state):
* gst/wavparse/gstwavparse.h:
* sys/oss/gstossmixerelement.c:
* sys/oss/gstosssink.c:
* sys/oss/gstosssrc.c:
* sys/osxaudio/gstosxaudioelement.c:
* sys/osxaudio/gstosxaudiosink.c:
* sys/osxaudio/gstosxaudiosrc.c:
* sys/sunaudio/gstsunaudiomixer.c:
* sys/sunaudio/gstsunaudiosink.c:
Define GstElementDetails as const and also static (when defined as
global)
2006-04-25 21:39:46 +00:00
|
|
|
static const GstElementDetails cutter_details =
|
|
|
|
GST_ELEMENT_DETAILS ("Audio cutter",
|
2006-03-30 15:37:05 +00:00
|
|
|
"Filter/Editor/Audio",
|
|
|
|
"Audio Cutter to split audio into non-silent bits",
|
|
|
|
"Thomas <thomas@apestaart.org>");
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static GstStaticPadTemplate cutter_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2005-11-22 13:13:21 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw-int, "
|
|
|
|
"rate = (int) [ 1, MAX ], "
|
|
|
|
"channels = (int) [ 1, MAX ], "
|
|
|
|
"endianness = (int) BYTE_ORDER, "
|
|
|
|
"width = (int) { 8, 16 }, "
|
|
|
|
"depth = (int) { 8, 16 }, " "signed = (boolean) true")
|
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,
|
2005-11-22 13:13:21 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw-int, "
|
|
|
|
"rate = (int) [ 1, MAX ], "
|
|
|
|
"channels = (int) [ 1, MAX ], "
|
|
|
|
"endianness = (int) BYTE_ORDER, "
|
|
|
|
"width = (int) { 8, 16 }, "
|
|
|
|
"depth = (int) { 8, 16 }, " "signed = (boolean) true")
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
GST_BOILERPLATE (GstCutter, gst_cutter, GstElement, GST_TYPE_ELEMENT);
|
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);
|
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
static GstFlowReturn gst_cutter_chain (GstPad * pad, GstBuffer * buffer);
|
2004-03-14 22:34:33 +00:00
|
|
|
|
|
|
|
void gst_cutter_get_caps (GstPad * pad, GstCutter * filter);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2003-11-01 12:24:33 +00:00
|
|
|
static void
|
|
|
|
gst_cutter_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&cutter_src_factory));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&cutter_sink_factory));
|
2003-11-01 12:24:33 +00:00
|
|
|
gst_element_class_set_details (element_class, &cutter_details);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_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",
|
|
|
|
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
|
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)",
|
|
|
|
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
|
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)",
|
|
|
|
0, G_MAXUINT64, 0, G_PARAM_READWRITE));
|
|
|
|
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)",
|
|
|
|
0, G_MAXUINT64, 0, G_PARAM_READWRITE));
|
|
|
|
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 ?",
|
|
|
|
FALSE, G_PARAM_READWRITE));
|
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");
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-11-21 19:50:25 +00:00
|
|
|
gst_cutter_init (GstCutter * filter, GstCutterClass * g_class)
|
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");
|
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");
|
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;
|
|
|
|
|
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
|
|
|
|
|
|
|
gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
|
|
|
|
gst_pad_set_chain_function (filter->sinkpad, gst_cutter_chain);
|
2005-11-21 19:50:25 +00:00
|
|
|
gst_pad_use_fixed_caps (filter->sinkpad);
|
2004-01-02 15:32:40 +00:00
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
|
2005-11-21 19:50:25 +00:00
|
|
|
gst_pad_use_fixed_caps (filter->srcpad);
|
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;
|
|
|
|
GValue v = { 0, };
|
|
|
|
|
|
|
|
g_value_init (&v, GST_TYPE_LIST);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_cutter_chain (GstPad * pad, GstBuffer * buf)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
GstCutter *filter;
|
|
|
|
gint16 *in_data;
|
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
|
|
|
static gboolean silent_prev = FALSE; /* previous value of silent */
|
|
|
|
GstBuffer *prebuf; /* pointer to a prebuffer element */
|
2003-05-10 16:57:38 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
g_return_val_if_fail (pad != NULL, GST_FLOW_ERROR);
|
|
|
|
g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
|
|
|
|
g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
filter = GST_CUTTER (GST_OBJECT_PARENT (pad));
|
2005-11-21 19:50:25 +00:00
|
|
|
g_return_val_if_fail (filter != NULL, GST_FLOW_ERROR);
|
|
|
|
g_return_val_if_fail (GST_IS_CUTTER (filter), GST_FLOW_ERROR);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
if (gst_audio_is_buffer_framed (pad, buf) == FALSE) {
|
2003-05-10 16:57:38 +00:00
|
|
|
g_warning ("audio buffer is not framed !\n");
|
2005-11-21 19:50:25 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2003-05-10 16:57:38 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
if (!filter->have_caps)
|
|
|
|
gst_cutter_get_caps (pad, filter);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
in_data = (gint16 *) GST_BUFFER_DATA (buf);
|
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 */
|
2004-03-14 22:34:33 +00:00
|
|
|
switch (filter->width) {
|
2001-12-22 23:27:31 +00:00
|
|
|
case 16:
|
2005-11-22 13:13:21 +00:00
|
|
|
num_samples = GST_BUFFER_SIZE (buf) / 2;
|
|
|
|
gst_cutter_calculate_gint16 (in_data, num_samples, &NCS);
|
|
|
|
NMS = NCS / num_samples;
|
2001-12-22 23:27:31 +00:00
|
|
|
break;
|
|
|
|
case 8:
|
2005-11-22 13:13:21 +00:00
|
|
|
num_samples = GST_BUFFER_SIZE (buf);
|
|
|
|
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 */
|
2005-11-21 19:50:25 +00:00
|
|
|
g_warning ("no mean square function for width %d\n", filter->width);
|
2001-12-22 23:27:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
silent_prev = filter->silent;
|
|
|
|
|
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,
|
|
|
|
RMS, (gdouble) gst_audio_duration_from_pad_buffer (filter->sinkpad, buf));
|
2001-12-22 23:27:31 +00:00
|
|
|
if (RMS < filter->threshold_level)
|
2005-11-21 19:50:25 +00:00
|
|
|
filter->silent_run_length +=
|
2006-03-30 23:37:16 +00:00
|
|
|
gst_guint64_to_gdouble (gst_audio_duration_from_pad_buffer (filter->
|
|
|
|
sinkpad, buf));
|
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
|
|
|
*/
|
2004-03-14 22:34:33 +00:00
|
|
|
if (filter->silent != silent_prev) {
|
|
|
|
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));
|
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);
|
2005-11-21 19:50:25 +00:00
|
|
|
filter->pre_run_length +=
|
2006-03-30 23:37:16 +00:00
|
|
|
gst_guint64_to_gdouble (gst_audio_duration_from_pad_buffer (filter->
|
|
|
|
sinkpad, buf));
|
2004-03-14 22:34:33 +00:00
|
|
|
while (filter->pre_run_length > filter->pre_length) {
|
|
|
|
prebuf = (g_list_first (filter->pre_buffer))->data;
|
|
|
|
g_assert (GST_IS_BUFFER (prebuf));
|
|
|
|
filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf);
|
2005-11-21 19:50:25 +00:00
|
|
|
filter->pre_run_length -=
|
2006-03-30 23:37:16 +00:00
|
|
|
gst_guint64_to_gdouble (gst_audio_duration_from_pad_buffer (filter->
|
|
|
|
sinkpad, prebuf));
|
2004-03-14 22:34:33 +00:00
|
|
|
/* only pass buffers if we don't leak */
|
|
|
|
if (!filter->leaky)
|
2005-11-21 19:50:25 +00:00
|
|
|
gst_pad_push (filter->srcpad, prebuf);
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
} else
|
2005-11-21 19:50:25 +00:00
|
|
|
gst_pad_push (filter->srcpad, buf);
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
2001-12-22 23:27:31 +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
|
|
|
{
|
2003-11-01 12:24:33 +00:00
|
|
|
if (!gst_element_register (plugin, "cutter", GST_RANK_NONE, GST_TYPE_CUTTER))
|
|
|
|
return FALSE;
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
|
|
|
"cutter",
|
|
|
|
"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);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2005-11-21 19:50:25 +00:00
|
|
|
void
|
|
|
|
gst_cutter_get_caps (GstPad * pad, GstCutter * filter)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2003-12-27 19:37:16 +00:00
|
|
|
const GstCaps *caps = NULL;
|
2003-12-22 01:47:09 +00:00
|
|
|
GstStructure *structure;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
caps = GST_PAD_CAPS (pad);
|
2004-03-14 22:34:33 +00:00
|
|
|
/* FIXME : Please change this to a better warning method ! */
|
2002-07-12 21:15:31 +00:00
|
|
|
g_assert (caps != NULL);
|
2003-12-22 01:47:09 +00:00
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
gst_structure_get_int (structure, "width", &filter->width);
|
2005-11-22 13:13:21 +00:00
|
|
|
filter->max_sample = 1 << (filter->width - 1); /* signed */
|
2001-12-22 23:27:31 +00:00
|
|
|
filter->have_caps = TRUE;
|
|
|
|
}
|