2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * gstlevel.c: signals RMS, peak and decaying peak levels
5 * Copyright (C) 2000,2001,2002,2003
6 * Thomas Vander Stichele <thomas at apestaart dot org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
31 /* elementfactory information */
32 static GstElementDetails level_details = {
34 "Filter/Analyzer/Audio",
35 "RMS/Peak/Decaying Peak Level signaller for audio/raw",
36 "Thomas <thomas@apestaart.org>"
41 GST_PAD_TEMPLATE_FACTORY (sink_template_factory,
48 "signed", GST_PROPS_BOOLEAN (TRUE),
49 "width", GST_PROPS_LIST (
53 "depth", GST_PROPS_LIST (
57 "rate", GST_PROPS_INT_RANGE (1, G_MAXINT),
58 "channels", GST_PROPS_INT_RANGE (1, 2)
62 GST_PAD_TEMPLATE_FACTORY (src_template_factory,
69 "signed", GST_PROPS_BOOLEAN (TRUE),
70 "width", GST_PROPS_LIST (
74 "depth", GST_PROPS_LIST (
78 "rate", GST_PROPS_INT_RANGE (1, G_MAXINT),
79 "channels", GST_PROPS_INT_RANGE (1, 2)
83 /* Filter signals and args */
98 static void gst_level_class_init (GstLevelClass *klass);
99 static void gst_level_base_init (GstLevelClass *klass);
100 static void gst_level_init (GstLevel *filter);
102 static GstElementStateReturn gst_level_change_state (GstElement *element);
103 static void gst_level_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
104 static void gst_level_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
106 static void gst_level_chain (GstPad *pad, GstData *_data);
108 static GstElementClass *parent_class = NULL;
109 static guint gst_filter_signals[LAST_SIGNAL] = { 0 };
112 gst_level_get_type (void)
114 static GType level_type = 0;
118 static const GTypeInfo level_info =
120 sizeof (GstLevelClass),
121 (GBaseInitFunc) gst_level_base_init, NULL,
122 (GClassInitFunc) gst_level_class_init, NULL, NULL,
123 sizeof (GstLevel), 0,
124 (GInstanceInitFunc) gst_level_init
126 level_type = g_type_register_static (GST_TYPE_ELEMENT, "GstLevel",
132 static GstPadLinkReturn
133 gst_level_link (GstPad *pad, GstCaps *caps)
137 GstPadLinkReturn res;
140 filter = GST_LEVEL (gst_pad_get_parent (pad));
141 g_return_val_if_fail (filter != NULL, GST_PAD_LINK_REFUSED);
142 g_return_val_if_fail (GST_IS_LEVEL (filter), GST_PAD_LINK_REFUSED);
143 otherpad = (pad == filter->srcpad ? filter->sinkpad : filter->srcpad);
145 if (!GST_CAPS_IS_FIXED (caps)) {
146 return GST_PAD_LINK_DELAYED;
149 res = gst_pad_try_set_caps (otherpad, caps);
150 /* if ok, set filter */
151 if (res != GST_PAD_LINK_OK && res != GST_PAD_LINK_DONE) {
155 filter->num_samples = 0;
157 if (!gst_caps_get_int (caps, "rate", &(filter->rate)))
158 return GST_PAD_LINK_REFUSED;
159 if (!gst_caps_get_int (caps, "width", &(filter->width)))
160 return GST_PAD_LINK_REFUSED;
161 if (!gst_caps_get_int (caps, "channels", &(filter->channels)))
162 return GST_PAD_LINK_REFUSED;
164 /* allocate channel variable arrays */
165 if (filter->CS) g_free (filter->CS);
166 if (filter->peak) g_free (filter->peak);
167 if (filter->last_peak) g_free (filter->last_peak);
168 if (filter->decay_peak) g_free (filter->decay_peak);
169 if (filter->decay_peak_age) g_free (filter->decay_peak_age);
170 if (filter->MS) g_free (filter->MS);
171 if (filter->RMS_dB) g_free (filter->RMS_dB);
172 filter->CS = g_new (double, filter->channels);
173 filter->peak = g_new (double, filter->channels);
174 filter->last_peak = g_new (double, filter->channels);
175 filter->decay_peak = g_new (double, filter->channels);
176 filter->decay_peak_age = g_new (double, filter->channels);
177 filter->MS = g_new (double, filter->channels);
178 filter->RMS_dB = g_new (double, filter->channels);
179 for (i = 0; i < filter->channels; ++i) {
180 filter->CS[i] = filter->peak[i] = filter->last_peak[i] =
181 filter->decay_peak[i] = filter->decay_peak_age[i] =
182 filter->MS[i] = filter->RMS_dB[i] = 0.0;
185 filter->inited = TRUE;
187 return GST_PAD_LINK_OK;
191 gst_level_fast_16bit_chain (gint16* in, guint num, gint channels,
192 gint resolution, double *CS, double *peak)
193 #include "filter.func"
196 gst_level_fast_8bit_chain (gint8* in, guint num, gint channels,
197 gint resolution, double *CS, double *peak)
198 #include "filter.func"
201 gst_level_chain (GstPad *pad, GstData *_data)
203 GstBuffer *buf = GST_BUFFER (_data);
208 gint num_samples = 0;
211 g_return_if_fail (pad != NULL);
212 g_return_if_fail (GST_IS_PAD (pad));
213 g_return_if_fail (buf != NULL);
215 filter = GST_LEVEL (GST_OBJECT_PARENT (pad));
216 g_return_if_fail (filter != NULL);
217 g_return_if_fail (GST_IS_LEVEL (filter));
219 for (i = 0; i < filter->channels; ++i)
220 filter->CS[i] = filter->peak[i] = filter->MS[i] = filter->RMS_dB[i] = 0.0;
222 in_data = (gint16 *) GST_BUFFER_DATA (buf);
224 num_samples = GST_BUFFER_SIZE (buf) / (filter->width / 8);
225 if (num_samples % filter->channels != 0)
226 g_warning ("WARNING: level: programming error, data not properly interleaved");
228 for (i = 0; i < filter->channels; ++i)
230 switch (filter->width)
233 gst_level_fast_16bit_chain (in_data + i, num_samples,
234 filter->channels, filter->width - 1,
235 &CS, &filter->peak[i]);
238 gst_level_fast_8bit_chain (((gint8 *) in_data) + i, num_samples,
239 filter->channels, filter->width - 1,
240 &CS, &filter->peak[i]);
243 /* g_print ("DEBUG: CS %f, peak %f\n", CS, filter->peak[i]); */
247 gst_pad_push (filter->srcpad, GST_DATA (buf));
249 filter->num_samples += num_samples;
251 for (i = 0; i < filter->channels; ++i)
253 filter->decay_peak_age[i] += num_samples;
254 /* g_print ("filter peak info [%d]: peak %f, age %f\n", i,
255 filter->last_peak[i], filter->decay_peak_age[i]); */
256 /* update running peak */
257 if (filter->peak[i] > filter->last_peak[i])
258 filter->last_peak[i] = filter->peak[i];
260 /* update decay peak */
261 if (filter->peak[i] >= filter->decay_peak[i])
263 /* g_print ("new peak, %f\n", filter->peak[i]); */
264 filter->decay_peak[i] = filter->peak[i];
265 filter->decay_peak_age[i] = 0;
269 /* make decay peak fall off if too old */
270 if (filter->decay_peak_age[i] > filter->rate * filter->decay_peak_ttl)
274 double length; /* length of buffer in seconds */
277 length = (double) num_samples / (filter->channels * filter->rate);
278 falloff_dB = filter->decay_peak_falloff * length;
279 falloff = pow (10, falloff_dB / -20.0);
281 /* g_print ("falloff: length %f, dB falloff %f, falloff factor %e\n",
282 length, falloff_dB, falloff); */
283 filter->decay_peak[i] *= falloff;
284 /* g_print ("peak is %f samples old, decayed with factor %e to %f\n",
285 filter->decay_peak_age[i], falloff, filter->decay_peak[i]); */
290 /* do we need to emit ? */
292 if (filter->num_samples >= filter->interval * (gdouble) filter->rate)
296 gdouble RMS, peak, endtime;
297 for (i = 0; i < filter->channels; ++i)
299 RMS = sqrt (filter->CS[i] / (filter->num_samples / filter->channels));
300 peak = filter->last_peak[i];
301 num_samples = GST_BUFFER_SIZE (buf) / (filter->width / 8);
302 endtime = (double) GST_BUFFER_TIMESTAMP (buf) / GST_SECOND
303 + (double) num_samples / (double) filter->rate;
305 g_signal_emit (G_OBJECT (filter), gst_filter_signals[SIGNAL_LEVEL], 0,
307 20 * log10 (RMS), 20 * log10 (filter->last_peak[i]),
308 20 * log10 (filter->decay_peak[i]));
309 /* we emitted, so reset cumulative and normal peak */
311 filter->last_peak[i] = 0.0;
314 filter->num_samples = 0;
318 static GstElementStateReturn gst_level_change_state (GstElement *element)
320 GstLevel *filter = GST_LEVEL (element);
322 switch(GST_STATE_TRANSITION(element)){
323 case GST_STATE_PAUSED_TO_PLAYING:
324 if (!filter->inited) return GST_STATE_FAILURE;
330 return GST_ELEMENT_CLASS(parent_class)->change_state(element);
334 gst_level_set_property (GObject *object, guint prop_id,
335 const GValue *value, GParamSpec *pspec)
339 /* it's not null if we got it, but it might not be ours */
340 g_return_if_fail (GST_IS_LEVEL (object));
341 filter = GST_LEVEL (object);
344 case ARG_SIGNAL_LEVEL:
345 filter->signal = g_value_get_boolean (value);
347 case ARG_SIGNAL_INTERVAL:
348 filter->interval = g_value_get_double (value);
351 filter->decay_peak_ttl = g_value_get_double (value);
353 case ARG_PEAK_FALLOFF:
354 filter->decay_peak_falloff = g_value_get_double (value);
362 gst_level_get_property (GObject *object, guint prop_id,
363 GValue *value, GParamSpec *pspec)
367 /* it's not null if we got it, but it might not be ours */
368 g_return_if_fail (GST_IS_LEVEL (object));
369 filter = GST_LEVEL (object);
372 case ARG_SIGNAL_LEVEL:
373 g_value_set_boolean (value, filter->signal);
375 case ARG_SIGNAL_INTERVAL:
376 g_value_set_double (value, filter->interval);
379 g_value_set_double (value, filter->decay_peak_ttl);
381 case ARG_PEAK_FALLOFF:
382 g_value_set_double (value, filter->decay_peak_falloff);
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
391 gst_level_base_init (GstLevelClass *klass)
393 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
395 gst_element_class_add_pad_template (element_class,
396 GST_PAD_TEMPLATE_GET (sink_template_factory));
397 gst_element_class_add_pad_template (element_class,
398 GST_PAD_TEMPLATE_GET (src_template_factory));
399 gst_element_class_set_details (element_class, &level_details);
401 element_class->change_state = gst_level_change_state;
405 gst_level_class_init (GstLevelClass *klass)
407 GObjectClass *gobject_class;
408 GstElementClass *gstelement_class;
410 gobject_class = (GObjectClass*) klass;
411 gstelement_class = (GstElementClass*) klass;
413 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
414 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIGNAL_LEVEL,
415 g_param_spec_boolean ("signal", "Signal",
416 "Emit level signals for each interval",
417 TRUE, G_PARAM_READWRITE));
418 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIGNAL_INTERVAL,
419 g_param_spec_double ("interval", "Interval",
420 "Interval between emissions (in seconds)",
421 0.01, 100.0, 0.1, G_PARAM_READWRITE));
422 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PEAK_TTL,
423 g_param_spec_double ("peak_ttl", "Peak TTL",
424 "Time To Live of decay peak before it falls back",
425 0, 100.0, 0.3, G_PARAM_READWRITE));
426 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PEAK_FALLOFF,
427 g_param_spec_double ("peak_falloff", "Peak Falloff",
428 "Decay rate of decay peak after TTL (in dB/sec)",
429 0.0, G_MAXDOUBLE, 10.0, G_PARAM_READWRITE));
431 gobject_class->set_property = gst_level_set_property;
432 gobject_class->get_property = gst_level_get_property;
434 gst_filter_signals[SIGNAL_LEVEL] =
435 g_signal_new ("level", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
436 G_STRUCT_OFFSET (GstLevelClass, level), NULL, NULL,
437 gstlevel_cclosure_marshal_VOID__DOUBLE_INT_DOUBLE_DOUBLE_DOUBLE,
439 G_TYPE_DOUBLE, G_TYPE_INT,
440 G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
444 gst_level_init (GstLevel *filter)
446 filter->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_template_factory), "sink");
447 gst_pad_set_link_function (filter->sinkpad, gst_level_link);
448 filter->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (src_template_factory), "src");
449 gst_pad_set_link_function (filter->srcpad, gst_level_link);
451 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
452 gst_pad_set_chain_function (filter->sinkpad, gst_level_chain);
453 filter->srcpad = gst_pad_new ("src", GST_PAD_SRC);
454 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
459 filter->RMS_dB = NULL;
463 filter->channels = 0;
465 filter->interval = 0.1;
466 filter->decay_peak_ttl = 0.4;
467 filter->decay_peak_falloff = 10.0; /* dB falloff (/sec) */
471 plugin_init (GstPlugin *plugin)
473 return gst_element_register (plugin, "level",
474 GST_RANK_NONE, GST_TYPE_LEVEL);
481 "Audio level plugin",