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-09-01 20:23:22 +00:00
|
|
|
* Copyright (C) 2000,2001,2002,2003,2005
|
2003-09-21 12:21:49 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2005-09-14 20:51:47 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-level
|
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <para>
|
2005-09-23 18:15:51 +00:00
|
|
|
* Level analyses incoming audio buffers and, if the
|
2005-09-23 18:23:04 +00:00
|
|
|
* <link linkend="GstLevel--message">message property</link> is #TRUE,
|
2005-10-19 20:48:24 +00:00
|
|
|
* generates an element message named
|
2005-09-23 18:15:51 +00:00
|
|
|
* <classname>"level"</classname>:
|
|
|
|
* after each interval of time given by the
|
|
|
|
* <link linkend="GstLevel--interval">interval property</link>.
|
2005-09-14 20:51:47 +00:00
|
|
|
* The message's structure contains four fields:
|
|
|
|
* <itemizedlist>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
2005-09-23 18:15:51 +00:00
|
|
|
* #GstClockTime
|
2005-09-14 20:51:47 +00:00
|
|
|
* <classname>"endtime"</classname>:
|
2005-09-23 18:15:51 +00:00
|
|
|
* the end time of the buffer that triggered the message
|
|
|
|
* </para>
|
2005-09-14 20:51:47 +00:00
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
2005-09-23 18:15:51 +00:00
|
|
|
* #GstValueList of #gdouble
|
|
|
|
* <classname>"peak"</classname>:
|
|
|
|
* the peak power level in dB for each channel
|
|
|
|
* </para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
|
|
|
* #GstValueList of #gdouble
|
|
|
|
* <classname>"decay"</classname>:
|
|
|
|
* the decaying peak power level in dB for each channel
|
|
|
|
* the decaying peak level follows the peak level, but starts dropping
|
|
|
|
* if no new peak is reached after the time given by
|
|
|
|
* the <link linkend="GstLevel--peak-ttl">the time to live</link>.
|
|
|
|
* When the decaying peak level drops, it does so at the decay rate
|
|
|
|
* as specified by the
|
2005-09-24 12:10:02 +00:00
|
|
|
* <link linkend="GstLevel--peak-falloff">the peak falloff rate</link>.
|
2005-09-23 18:15:51 +00:00
|
|
|
* </para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
|
|
|
* #GstValueList of #gdouble
|
|
|
|
* <classname>"rms"</classname>:
|
|
|
|
* the Root Mean Square (or average power) level in dB for each channel
|
|
|
|
* </para>
|
2005-09-14 20:51:47 +00:00
|
|
|
* </listitem>
|
|
|
|
* </itemizedlist>
|
|
|
|
* </para>
|
|
|
|
* <title>Example application</title>
|
|
|
|
* <para>
|
|
|
|
* <include xmlns="http://www.w3.org/2003/XInclude" href="element-level-example.xml" />
|
|
|
|
* </para>
|
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
2003-11-06 22:14:17 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2001-12-22 23:27:31 +00:00
|
|
|
#include <gst/gst.h>
|
2005-09-23 18:15:51 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2001-12-22 23:27:31 +00:00
|
|
|
#include "gstlevel.h"
|
|
|
|
#include "math.h"
|
|
|
|
|
2004-07-26 18:34:01 +00:00
|
|
|
GST_DEBUG_CATEGORY (level_debug);
|
|
|
|
#define GST_CAT_DEFAULT level_debug
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
static GstElementDetails level_details = {
|
|
|
|
"Level",
|
2003-11-16 22:02:22 +00:00
|
|
|
"Filter/Analyzer/Audio",
|
2005-09-01 20:23:22 +00:00
|
|
|
"RMS/Peak/Decaying Peak Level messager for audio/raw",
|
2003-11-02 18:17:48 +00:00
|
|
|
"Thomas <thomas@apestaart.org>"
|
2001-12-22 23:27:31 +00:00
|
|
|
};
|
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate sink_template_factory =
|
2004-10-26 15:36:17 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2004-07-27 17:42:47 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw-int, "
|
|
|
|
"rate = (int) [ 1, MAX ], "
|
2005-09-23 14:20:01 +00:00
|
|
|
"channels = (int) [ 1, 8 ], "
|
2004-07-27 17:42:47 +00:00
|
|
|
"endianness = (int) BYTE_ORDER, "
|
|
|
|
"width = (int) { 8, 16 }, "
|
|
|
|
"depth = (int) { 8, 16 }, " "signed = (boolean) true")
|
2004-03-14 22:34:33 +00:00
|
|
|
);
|
2003-09-21 13:08:29 +00:00
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate src_template_factory =
|
2004-10-26 15:36:17 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2004-07-27 17:42:47 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw-int, "
|
|
|
|
"rate = (int) [ 1, MAX ], "
|
2005-09-23 14:20:01 +00:00
|
|
|
"channels = (int) [ 1, 8 ], "
|
2004-07-27 17:42:47 +00:00
|
|
|
"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
|
|
|
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
enum
|
|
|
|
{
|
2005-07-04 14:02:46 +00:00
|
|
|
PROP_0,
|
|
|
|
PROP_SIGNAL_LEVEL,
|
|
|
|
PROP_SIGNAL_INTERVAL,
|
|
|
|
PROP_PEAK_TTL,
|
|
|
|
PROP_PEAK_FALLOFF
|
2001-12-22 23:27:31 +00:00
|
|
|
};
|
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
|
|
|
|
GST_BOILERPLATE (GstLevel, gst_level, GstBaseTransform,
|
|
|
|
GST_TYPE_BASE_TRANSFORM);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static void gst_level_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_level_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
static gboolean gst_level_set_caps (GstBaseTransform * trans, GstCaps * in,
|
|
|
|
GstCaps * out);
|
2005-09-09 17:56:43 +00:00
|
|
|
static GstFlowReturn gst_level_transform_ip (GstBaseTransform * trans,
|
|
|
|
GstBuffer * in);
|
2005-07-04 14:02:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_level_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = g_class;
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&sink_template_factory));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&src_template_factory));
|
|
|
|
gst_element_class_set_details (element_class, &level_details);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_level_class_init (GstLevelClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstBaseTransformClass *trans_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
trans_class = (GstBaseTransformClass *) klass;
|
|
|
|
|
|
|
|
gobject_class->set_property = gst_level_set_property;
|
|
|
|
gobject_class->get_property = gst_level_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SIGNAL_LEVEL,
|
2005-09-01 20:23:22 +00:00
|
|
|
g_param_spec_boolean ("message", "mesage",
|
2005-09-23 18:15:51 +00:00
|
|
|
"Post a level message for each passed interval",
|
|
|
|
TRUE, G_PARAM_READWRITE));
|
2005-07-04 14:02:46 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SIGNAL_INTERVAL,
|
2005-09-23 18:15:51 +00:00
|
|
|
g_param_spec_uint64 ("interval", "Interval",
|
|
|
|
"Interval of time between message posts (in nanoseconds)",
|
|
|
|
1, G_MAXUINT64, GST_SECOND / 10, G_PARAM_READWRITE));
|
2005-07-04 14:02:46 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PEAK_TTL,
|
2005-09-23 18:15:51 +00:00
|
|
|
g_param_spec_uint64 ("peak_ttl", "Peak TTL",
|
|
|
|
"Time To Live of decay peak before it falls back (in nanoseconds)",
|
|
|
|
0, G_MAXUINT64, GST_SECOND / 10 * 3, G_PARAM_READWRITE));
|
2005-07-04 14:02:46 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PEAK_FALLOFF,
|
|
|
|
g_param_spec_double ("peak_falloff", "Peak Falloff",
|
|
|
|
"Decay rate of decay peak after TTL (in dB/sec)",
|
|
|
|
0.0, G_MAXDOUBLE, 10.0, G_PARAM_READWRITE));
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (level_debug, "level", 0, "Level calculation");
|
|
|
|
|
|
|
|
trans_class->set_caps = gst_level_set_caps;
|
2005-09-09 17:56:43 +00:00
|
|
|
trans_class->transform_ip = gst_level_transform_ip;
|
|
|
|
trans_class->passthrough_on_same_caps = TRUE;
|
2005-07-04 14:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-08-28 17:59:20 +00:00
|
|
|
gst_level_init (GstLevel * filter, GstLevelClass * g_class)
|
2005-07-04 14:02:46 +00:00
|
|
|
{
|
|
|
|
filter->CS = NULL;
|
|
|
|
filter->peak = NULL;
|
|
|
|
|
|
|
|
filter->rate = 0;
|
|
|
|
filter->width = 0;
|
|
|
|
filter->channels = 0;
|
|
|
|
|
2005-09-23 18:15:51 +00:00
|
|
|
filter->interval = GST_SECOND / 10;
|
|
|
|
filter->decay_peak_ttl = GST_SECOND / 10 * 3;
|
2005-07-04 14:02:46 +00:00
|
|
|
filter->decay_peak_falloff = 10.0; /* dB falloff (/sec) */
|
2005-09-14 20:51:47 +00:00
|
|
|
|
|
|
|
filter->message = TRUE;
|
2005-07-04 14:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_level_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstLevel *filter = GST_LEVEL (object);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_SIGNAL_LEVEL:
|
2005-09-01 20:23:22 +00:00
|
|
|
filter->message = g_value_get_boolean (value);
|
2005-07-04 14:02:46 +00:00
|
|
|
break;
|
|
|
|
case PROP_SIGNAL_INTERVAL:
|
2005-09-23 18:15:51 +00:00
|
|
|
filter->interval = g_value_get_uint64 (value);
|
2005-07-04 14:02:46 +00:00
|
|
|
break;
|
|
|
|
case PROP_PEAK_TTL:
|
2005-09-23 18:15:51 +00:00
|
|
|
filter->decay_peak_ttl = g_value_get_uint64 (value);
|
2005-07-04 14:02:46 +00:00
|
|
|
break;
|
|
|
|
case PROP_PEAK_FALLOFF:
|
|
|
|
filter->decay_peak_falloff = g_value_get_double (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
static void
|
|
|
|
gst_level_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
2002-08-27 17:45:07 +00:00
|
|
|
{
|
2005-07-04 14:02:46 +00:00
|
|
|
GstLevel *filter = GST_LEVEL (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_SIGNAL_LEVEL:
|
2005-09-01 20:23:22 +00:00
|
|
|
g_value_set_boolean (value, filter->message);
|
2005-07-04 14:02:46 +00:00
|
|
|
break;
|
|
|
|
case PROP_SIGNAL_INTERVAL:
|
2005-09-23 18:15:51 +00:00
|
|
|
g_value_set_uint64 (value, filter->interval);
|
2005-07-04 14:02:46 +00:00
|
|
|
break;
|
|
|
|
case PROP_PEAK_TTL:
|
2005-09-23 18:15:51 +00:00
|
|
|
g_value_set_uint64 (value, filter->decay_peak_ttl);
|
2005-07-04 14:02:46 +00:00
|
|
|
break;
|
|
|
|
case PROP_PEAK_FALLOFF:
|
|
|
|
g_value_set_double (value, filter->decay_peak_falloff);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
static gint
|
|
|
|
structure_get_int (GstStructure * structure, const gchar * field)
|
|
|
|
{
|
|
|
|
gint ret;
|
|
|
|
|
|
|
|
if (!gst_structure_get_int (structure, field, &ret))
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_level_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2002-08-27 17:45:07 +00:00
|
|
|
GstLevel *filter;
|
2003-12-22 01:47:09 +00:00
|
|
|
GstStructure *structure;
|
2003-09-21 12:21:49 +00:00
|
|
|
int i;
|
2002-08-27 17:45:07 +00:00
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
filter = GST_LEVEL (trans);
|
2003-11-19 03:46:21 +00:00
|
|
|
|
2005-09-23 18:15:51 +00:00
|
|
|
filter->num_frames = 0;
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
structure = gst_caps_get_structure (in, 0);
|
|
|
|
filter->rate = structure_get_int (structure, "rate");
|
|
|
|
filter->width = structure_get_int (structure, "width");
|
|
|
|
filter->channels = structure_get_int (structure, "channels");
|
2003-11-19 03:46:21 +00:00
|
|
|
|
|
|
|
/* allocate channel variable arrays */
|
2004-07-26 18:34:01 +00:00
|
|
|
g_free (filter->CS);
|
|
|
|
g_free (filter->peak);
|
|
|
|
g_free (filter->last_peak);
|
|
|
|
g_free (filter->decay_peak);
|
2005-09-24 12:10:02 +00:00
|
|
|
g_free (filter->decay_peak_base);
|
2004-07-26 18:34:01 +00:00
|
|
|
g_free (filter->decay_peak_age);
|
2003-11-19 03:46:21 +00:00
|
|
|
filter->CS = g_new (double, filter->channels);
|
|
|
|
filter->peak = g_new (double, filter->channels);
|
|
|
|
filter->last_peak = g_new (double, filter->channels);
|
|
|
|
filter->decay_peak = g_new (double, filter->channels);
|
2005-09-24 12:10:02 +00:00
|
|
|
filter->decay_peak_base = g_new (double, filter->channels);
|
2005-09-23 18:15:51 +00:00
|
|
|
|
|
|
|
filter->decay_peak_age = g_new (GstClockTime, filter->channels);
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2003-11-19 03:46:21 +00:00
|
|
|
for (i = 0; i < filter->channels; ++i) {
|
|
|
|
filter->CS[i] = filter->peak[i] = filter->last_peak[i] =
|
2005-09-24 12:10:02 +00:00
|
|
|
filter->decay_peak[i] = filter->decay_peak_base[i] = 0.0;
|
2005-09-23 18:15:51 +00:00
|
|
|
filter->decay_peak_age[i] = 0LL;
|
2003-11-19 03:46:21 +00:00
|
|
|
}
|
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* process one (interleaved) channel of incoming samples
|
|
|
|
* calculate square sum of samples
|
2005-08-10 10:54:02 +00:00
|
|
|
* normalize and average over number of samples
|
|
|
|
* returns a normalized average power value as CS, as a double between 0 and 1
|
|
|
|
* also returns the normalized peak power (square of the highest amplitude)
|
|
|
|
*
|
2005-07-04 14:02:46 +00:00
|
|
|
* caller must assure num is a multiple of channels
|
2005-08-10 10:54:02 +00:00
|
|
|
* samples for multiple channels are interleaved
|
|
|
|
* input sample data enters in *in_data as 8 or 16 bit data
|
2005-07-04 14:02:46 +00:00
|
|
|
* this filter only accepts signed audio data, so mid level is always 0
|
2005-09-14 20:51:47 +00:00
|
|
|
*
|
|
|
|
* for 16 bit, this code considers the non-existant 32768 value to be
|
|
|
|
* full-scale; so 32767 will not map to 1.0
|
2005-07-04 14:02:46 +00:00
|
|
|
*/
|
2005-08-10 10:54:02 +00:00
|
|
|
|
|
|
|
#define DEFINE_LEVEL_CALCULATOR(TYPE) \
|
|
|
|
static void inline \
|
|
|
|
gst_level_calculate_##TYPE (TYPE * in, guint num, gint channels, \
|
|
|
|
gint resolution, double *CS, double *peak) \
|
|
|
|
{ \
|
|
|
|
register int j; \
|
|
|
|
double squaresum = 0.0; /* square sum of the integer samples */ \
|
|
|
|
register double square = 0.0; /* Square */ \
|
|
|
|
register double PSS = 0.0; /* Peak Square Sample */ \
|
2005-09-14 20:51:47 +00:00
|
|
|
gdouble normalizer; /* divisor to get a [-1.0, 1.0] range */ \
|
2005-08-10 10:54:02 +00:00
|
|
|
\
|
|
|
|
*CS = 0.0; /* Cumulative Square for this block */ \
|
|
|
|
\
|
|
|
|
normalizer = (double) (1 << resolution); \
|
|
|
|
\
|
|
|
|
for (j = 0; j < num; j += channels) \
|
|
|
|
{ \
|
|
|
|
square = ((double) in[j]) * in[j]; \
|
|
|
|
if (square > PSS) PSS = square; \
|
|
|
|
squaresum += square; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
*CS = squaresum / (normalizer * normalizer); \
|
|
|
|
*peak = PSS / (normalizer * normalizer); \
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
DEFINE_LEVEL_CALCULATOR (gint16);
|
|
|
|
DEFINE_LEVEL_CALCULATOR (gint8);
|
|
|
|
|
|
|
|
static GstMessage *
|
2005-09-23 18:15:51 +00:00
|
|
|
gst_level_message_new (GstLevel * l, GstClockTime endtime)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2005-07-04 14:02:46 +00:00
|
|
|
GstStructure *s;
|
|
|
|
GValue v = { 0, };
|
|
|
|
|
|
|
|
g_value_init (&v, GST_TYPE_LIST);
|
|
|
|
|
2005-09-23 18:15:51 +00:00
|
|
|
s = gst_structure_new ("level", "endtime", GST_TYPE_CLOCK_TIME,
|
|
|
|
endtime, NULL);
|
2005-07-04 14:02:46 +00:00
|
|
|
/* will copy-by-value */
|
|
|
|
gst_structure_set_value (s, "rms", &v);
|
|
|
|
gst_structure_set_value (s, "peak", &v);
|
|
|
|
gst_structure_set_value (s, "decay", &v);
|
|
|
|
|
2005-10-19 20:48:24 +00:00
|
|
|
return gst_message_new_element (GST_OBJECT (l), s);
|
2005-07-04 14:02:46 +00:00
|
|
|
}
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
static void
|
|
|
|
gst_level_message_append_channel (GstMessage * m, gdouble rms, gdouble peak,
|
|
|
|
gdouble decay)
|
|
|
|
{
|
|
|
|
GstStructure *s;
|
|
|
|
GValue v = { 0, };
|
|
|
|
GValue *l;
|
|
|
|
|
|
|
|
g_value_init (&v, G_TYPE_DOUBLE);
|
|
|
|
|
|
|
|
s = (GstStructure *) gst_message_get_structure (m);
|
|
|
|
|
|
|
|
l = (GValue *) gst_structure_get_value (s, "rms");
|
|
|
|
g_value_set_double (&v, rms);
|
|
|
|
gst_value_list_append_value (l, &v); /* copies by value */
|
|
|
|
|
|
|
|
l = (GValue *) gst_structure_get_value (s, "peak");
|
|
|
|
g_value_set_double (&v, peak);
|
|
|
|
gst_value_list_append_value (l, &v); /* copies by value */
|
|
|
|
|
|
|
|
l = (GValue *) gst_structure_get_value (s, "decay");
|
|
|
|
g_value_set_double (&v, decay);
|
|
|
|
gst_value_list_append_value (l, &v); /* copies by value */
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2005-09-09 17:56:43 +00:00
|
|
|
gst_level_transform_ip (GstBaseTransform * trans, GstBuffer * in)
|
2005-07-04 14:02:46 +00:00
|
|
|
{
|
|
|
|
GstLevel *filter;
|
|
|
|
gpointer in_data;
|
2003-09-21 12:21:49 +00:00
|
|
|
double CS = 0.0;
|
2005-09-23 18:15:51 +00:00
|
|
|
gint num_frames = 0;
|
|
|
|
gint num_int_samples = 0; /* number of interleaved samples
|
|
|
|
* ie. total count for all channels combined */
|
2003-09-21 12:21:49 +00:00
|
|
|
gint i;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
filter = GST_LEVEL (trans);
|
2004-05-08 13:03:59 +00:00
|
|
|
|
2003-09-21 12:21:49 +00:00
|
|
|
for (i = 0; i < filter->channels; ++i)
|
2005-09-24 12:10:02 +00:00
|
|
|
filter->peak[i] = 0.0;
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
in_data = GST_BUFFER_DATA (in);
|
2005-08-10 10:54:02 +00:00
|
|
|
num_int_samples = GST_BUFFER_SIZE (in) / (filter->width / 8);
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2005-08-10 10:54:02 +00:00
|
|
|
g_return_val_if_fail (num_int_samples % filter->channels == 0,
|
|
|
|
GST_FLOW_ERROR);
|
2003-09-21 12:21:49 +00:00
|
|
|
|
2005-09-23 18:15:51 +00:00
|
|
|
num_frames = num_int_samples / filter->channels;
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
for (i = 0; i < filter->channels; ++i) {
|
2005-08-10 10:54:02 +00:00
|
|
|
CS = 0.0;
|
2004-03-14 22:34:33 +00:00
|
|
|
switch (filter->width) {
|
2003-09-21 12:21:49 +00:00
|
|
|
case 16:
|
2005-09-01 20:23:22 +00:00
|
|
|
gst_level_calculate_gint16 (((gint16 *) in_data) + i, num_int_samples,
|
2004-03-15 19:32:27 +00:00
|
|
|
filter->channels, filter->width - 1, &CS, &filter->peak[i]);
|
|
|
|
break;
|
2003-09-21 12:21:49 +00:00
|
|
|
case 8:
|
2005-08-10 10:54:02 +00:00
|
|
|
gst_level_calculate_gint8 (((gint8 *) in_data) + i, num_int_samples,
|
2004-03-15 19:32:27 +00:00
|
|
|
filter->channels, filter->width - 1, &CS, &filter->peak[i]);
|
|
|
|
break;
|
2003-09-21 12:21:49 +00:00
|
|
|
}
|
2005-08-10 10:54:02 +00:00
|
|
|
GST_LOG_OBJECT (filter,
|
2005-09-01 20:23:22 +00:00
|
|
|
"channel %d, cumulative sum %f, peak %f, over %d samples/%d channels",
|
2005-08-10 10:54:02 +00:00
|
|
|
i, CS, filter->peak[i], num_int_samples, filter->channels);
|
2003-09-21 12:21:49 +00:00
|
|
|
filter->CS[i] += CS;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
2005-09-23 18:15:51 +00:00
|
|
|
filter->num_frames += num_frames;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
for (i = 0; i < filter->channels; ++i) {
|
2005-09-23 18:15:51 +00:00
|
|
|
filter->decay_peak_age[i] +=
|
|
|
|
GST_FRAMES_TO_CLOCK_TIME (num_frames, filter->rate);
|
2005-09-24 12:10:02 +00:00
|
|
|
GST_LOG_OBJECT (filter, "filter peak info [%d]: decay peak %f, age %"
|
2005-09-23 18:15:51 +00:00
|
|
|
GST_TIME_FORMAT, i,
|
2005-09-24 12:10:02 +00:00
|
|
|
filter->decay_peak[i], GST_TIME_ARGS (filter->decay_peak_age[i]));
|
2005-08-10 10:54:02 +00:00
|
|
|
|
2003-09-21 12:21:49 +00:00
|
|
|
/* update running peak */
|
|
|
|
if (filter->peak[i] > filter->last_peak[i])
|
2004-03-14 22:34:33 +00:00
|
|
|
filter->last_peak[i] = filter->peak[i];
|
2003-09-21 12:21:49 +00:00
|
|
|
|
2005-09-24 12:10:02 +00:00
|
|
|
/* make decay peak fall off if too old */
|
|
|
|
if (filter->decay_peak_age[i] > filter->decay_peak_ttl) {
|
|
|
|
double falloff_dB;
|
|
|
|
double falloff;
|
|
|
|
GstClockTimeDiff falloff_time;
|
|
|
|
double length; /* length of falloff time in seconds */
|
|
|
|
|
|
|
|
falloff_time = GST_CLOCK_DIFF (filter->decay_peak_ttl,
|
|
|
|
filter->decay_peak_age[i]);
|
|
|
|
length = (gdouble) falloff_time / GST_SECOND;
|
|
|
|
falloff_dB = filter->decay_peak_falloff * length;
|
|
|
|
falloff = pow (10, falloff_dB / -20.0);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (filter,
|
|
|
|
"falloff: current %f, base %f, interval %" GST_TIME_FORMAT
|
|
|
|
", dB falloff %f, factor %e",
|
|
|
|
filter->decay_peak[i], filter->decay_peak_base[i],
|
|
|
|
GST_TIME_ARGS (falloff_time), falloff_dB, falloff);
|
|
|
|
filter->decay_peak[i] = filter->decay_peak_base[i] * falloff;
|
|
|
|
GST_LOG_OBJECT (filter,
|
|
|
|
"peak is %" GST_TIME_FORMAT " old, decayed with factor %e to %f",
|
|
|
|
GST_TIME_ARGS (filter->decay_peak_age[i]), falloff,
|
|
|
|
filter->decay_peak[i]);
|
|
|
|
} else {
|
|
|
|
GST_LOG_OBJECT (filter, "peak not old enough, not decaying");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the peak of this run is higher, the decay peak gets reset */
|
2004-03-14 22:34:33 +00:00
|
|
|
if (filter->peak[i] >= filter->decay_peak[i]) {
|
2005-09-23 18:15:51 +00:00
|
|
|
GST_LOG_OBJECT (filter, "new peak, %f", filter->peak[i]);
|
2004-03-14 22:34:33 +00:00
|
|
|
filter->decay_peak[i] = filter->peak[i];
|
2005-09-24 12:10:02 +00:00
|
|
|
filter->decay_peak_base[i] = filter->peak[i];
|
2005-09-23 18:15:51 +00:00
|
|
|
filter->decay_peak_age[i] = 0LL;
|
2003-09-21 12:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2005-09-24 12:10:02 +00:00
|
|
|
/* do we need to message ? */
|
2005-09-23 18:15:51 +00:00
|
|
|
if (filter->num_frames >=
|
2005-09-24 12:10:02 +00:00
|
|
|
GST_CLOCK_TIME_TO_FRAMES (filter->interval, filter->rate)) {
|
2005-09-01 20:23:22 +00:00
|
|
|
if (filter->message) {
|
2005-07-04 14:02:46 +00:00
|
|
|
GstMessage *m;
|
2005-09-23 18:15:51 +00:00
|
|
|
GstClockTime endtime;
|
2005-07-04 14:02:46 +00:00
|
|
|
|
2005-09-23 18:15:51 +00:00
|
|
|
endtime = GST_BUFFER_TIMESTAMP (in)
|
|
|
|
+ GST_FRAMES_TO_CLOCK_TIME (num_frames, filter->rate);
|
2005-07-04 14:02:46 +00:00
|
|
|
|
|
|
|
m = gst_level_message_new (filter, endtime);
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2005-09-24 12:10:02 +00:00
|
|
|
GST_LOG_OBJECT (filter,
|
|
|
|
"message: end time %" GST_TIME_FORMAT ", num_frames %d",
|
|
|
|
GST_TIME_ARGS (endtime), filter->num_frames);
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
for (i = 0; i < filter->channels; ++i) {
|
2005-09-24 12:10:02 +00:00
|
|
|
double RMS;
|
|
|
|
double RMSdB, lastdB, decaydB;
|
|
|
|
|
2005-09-23 18:15:51 +00:00
|
|
|
RMS = sqrt (filter->CS[i] / filter->num_frames);
|
2005-08-10 10:54:02 +00:00
|
|
|
GST_LOG_OBJECT (filter,
|
2005-09-24 12:10:02 +00:00
|
|
|
"message: channel %d, CS %f, num_frames %d, RMS %f",
|
|
|
|
i, filter->CS[i], filter->num_frames, RMS);
|
|
|
|
GST_LOG_OBJECT (filter,
|
|
|
|
"message: last_peak: %f, decay_peak: %f",
|
|
|
|
filter->last_peak[i], filter->decay_peak[i]);
|
2005-08-10 10:54:02 +00:00
|
|
|
/* RMS values are calculated in amplitude, so 20 * log 10 */
|
|
|
|
RMSdB = 20 * log10 (RMS);
|
|
|
|
/* peak values are square sums, ie. power, so 10 * log 10 */
|
|
|
|
lastdB = 10 * log10 (filter->last_peak[i]);
|
|
|
|
decaydB = 10 * log10 (filter->decay_peak[i]);
|
|
|
|
|
2005-09-24 12:10:02 +00:00
|
|
|
if (filter->decay_peak[i] < filter->last_peak[i]) {
|
|
|
|
GST_ERROR_OBJECT (filter,
|
|
|
|
"message: decay peak dB %f smaller than last peak dB %f",
|
|
|
|
decaydB, lastdB);
|
|
|
|
}
|
2005-08-10 10:54:02 +00:00
|
|
|
GST_LOG_OBJECT (filter,
|
2005-09-24 12:10:02 +00:00
|
|
|
"message: RMS %f dB, peak %f dB, decay %f dB",
|
|
|
|
RMSdB, lastdB, decaydB);
|
2005-08-10 10:54:02 +00:00
|
|
|
|
|
|
|
gst_level_message_append_channel (m, RMSdB, lastdB, decaydB);
|
2005-07-04 14:02:46 +00:00
|
|
|
|
|
|
|
/* reset cumulative and normal peak */
|
2004-03-15 19:32:27 +00:00
|
|
|
filter->CS[i] = 0.0;
|
|
|
|
filter->last_peak[i] = 0.0;
|
2003-09-21 12:21:49 +00:00
|
|
|
}
|
2005-07-04 14:02:46 +00:00
|
|
|
|
|
|
|
gst_element_post_message (GST_ELEMENT (filter), m);
|
2003-09-21 12:21:49 +00:00
|
|
|
}
|
2005-09-23 18:15:51 +00:00
|
|
|
filter->num_frames = 0;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
2005-07-04 14:02:46 +00:00
|
|
|
return GST_FLOW_OK;
|
2002-08-27 17:45:07 +00:00
|
|
|
}
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
static gboolean
|
2004-03-14 22:34:33 +00:00
|
|
|
plugin_init (GstPlugin * plugin)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
return gst_element_register (plugin, "level", GST_RANK_NONE, GST_TYPE_LEVEL);
|
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,
|
|
|
|
"level",
|
|
|
|
"Audio level plugin",
|
2005-11-14 02:13:35 +00:00
|
|
|
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
|