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.
28 #include <gst/audio/audio.h>
32 /* elementfactory information */
33 static GstElementDetails level_details = {
35 "Filter/Analyzer/Audio",
36 "RMS/Peak/Decaying Peak Level signaller for audio/raw",
37 "Thomas <thomas@apestaart.org>"
42 static GstStaticPadTemplate sink_template_factory =
43 GST_STATIC_PAD_TEMPLATE (
47 GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS)
50 static GstStaticPadTemplate src_template_factory =
51 GST_STATIC_PAD_TEMPLATE (
55 GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS)
58 /* Filter signals and args */
73 static void gst_level_class_init (GstLevelClass *klass);
74 static void gst_level_base_init (GstLevelClass *klass);
75 static void gst_level_init (GstLevel *filter);
77 static GstElementStateReturn gst_level_change_state (GstElement *element);
78 static void gst_level_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
79 static void gst_level_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
81 static void gst_level_chain (GstPad *pad, GstData *_data);
83 static GstElementClass *parent_class = NULL;
84 static guint gst_filter_signals[LAST_SIGNAL] = { 0 };
87 gst_level_get_type (void)
89 static GType level_type = 0;
93 static const GTypeInfo level_info =
95 sizeof (GstLevelClass),
96 (GBaseInitFunc) gst_level_base_init, NULL,
97 (GClassInitFunc) gst_level_class_init, NULL, NULL,
99 (GInstanceInitFunc) gst_level_init
101 level_type = g_type_register_static (GST_TYPE_ELEMENT, "GstLevel",
107 static GstPadLinkReturn
108 gst_level_link (GstPad *pad, const GstCaps *caps)
112 GstPadLinkReturn res;
113 GstStructure *structure;
117 filter = GST_LEVEL (gst_pad_get_parent (pad));
118 g_return_val_if_fail (filter != NULL, GST_PAD_LINK_REFUSED);
119 g_return_val_if_fail (GST_IS_LEVEL (filter), GST_PAD_LINK_REFUSED);
120 otherpad = (pad == filter->srcpad ? filter->sinkpad : filter->srcpad);
122 res = gst_pad_try_set_caps (otherpad, caps);
123 /* if ok, set filter */
124 if (res != GST_PAD_LINK_OK && res != GST_PAD_LINK_DONE) {
128 filter->num_samples = 0;
130 structure = gst_caps_get_structure (caps, 0);
131 ret = gst_structure_get_int (structure, "rate", &filter->rate);
132 ret &= gst_structure_get_int (structure, "width", &filter->width);
133 ret &= gst_structure_get_int (structure, "channels", &filter->channels);
135 if (!ret) return GST_PAD_LINK_REFUSED;
137 /* allocate channel variable arrays */
138 if (filter->CS) g_free (filter->CS);
139 if (filter->peak) g_free (filter->peak);
140 if (filter->last_peak) g_free (filter->last_peak);
141 if (filter->decay_peak) g_free (filter->decay_peak);
142 if (filter->decay_peak_age) g_free (filter->decay_peak_age);
143 if (filter->MS) g_free (filter->MS);
144 if (filter->RMS_dB) g_free (filter->RMS_dB);
145 filter->CS = g_new (double, filter->channels);
146 filter->peak = g_new (double, filter->channels);
147 filter->last_peak = g_new (double, filter->channels);
148 filter->decay_peak = g_new (double, filter->channels);
149 filter->decay_peak_age = g_new (double, filter->channels);
150 filter->MS = g_new (double, filter->channels);
151 filter->RMS_dB = g_new (double, filter->channels);
152 for (i = 0; i < filter->channels; ++i) {
153 filter->CS[i] = filter->peak[i] = filter->last_peak[i] =
154 filter->decay_peak[i] = filter->decay_peak_age[i] =
155 filter->MS[i] = filter->RMS_dB[i] = 0.0;
158 filter->inited = TRUE;
160 return GST_PAD_LINK_OK;
164 gst_level_fast_16bit_chain (gint16* in, guint num, gint channels,
165 gint resolution, double *CS, double *peak)
166 #include "filter.func"
169 gst_level_fast_8bit_chain (gint8* in, guint num, gint channels,
170 gint resolution, double *CS, double *peak)
171 #include "filter.func"
174 gst_level_chain (GstPad *pad, GstData *_data)
176 GstBuffer *buf = GST_BUFFER (_data);
181 gint num_samples = 0;
184 g_return_if_fail (pad != NULL);
185 g_return_if_fail (GST_IS_PAD (pad));
186 g_return_if_fail (buf != NULL);
188 filter = GST_LEVEL (GST_OBJECT_PARENT (pad));
189 g_return_if_fail (filter != NULL);
190 g_return_if_fail (GST_IS_LEVEL (filter));
192 for (i = 0; i < filter->channels; ++i)
193 filter->CS[i] = filter->peak[i] = filter->MS[i] = filter->RMS_dB[i] = 0.0;
195 in_data = (gint16 *) GST_BUFFER_DATA (buf);
197 num_samples = GST_BUFFER_SIZE (buf) / (filter->width / 8);
198 if (num_samples % filter->channels != 0)
199 g_warning ("WARNING: level: programming error, data not properly interleaved");
201 for (i = 0; i < filter->channels; ++i)
203 switch (filter->width)
206 gst_level_fast_16bit_chain (in_data + i, num_samples,
207 filter->channels, filter->width - 1,
208 &CS, &filter->peak[i]);
211 gst_level_fast_8bit_chain (((gint8 *) in_data) + i, num_samples,
212 filter->channels, filter->width - 1,
213 &CS, &filter->peak[i]);
216 /* g_print ("DEBUG: CS %f, peak %f\n", CS, filter->peak[i]); */
220 gst_pad_push (filter->srcpad, GST_DATA (buf));
222 filter->num_samples += num_samples;
224 for (i = 0; i < filter->channels; ++i)
226 filter->decay_peak_age[i] += num_samples;
227 /* g_print ("filter peak info [%d]: peak %f, age %f\n", i,
228 filter->last_peak[i], filter->decay_peak_age[i]); */
229 /* update running peak */
230 if (filter->peak[i] > filter->last_peak[i])
231 filter->last_peak[i] = filter->peak[i];
233 /* update decay peak */
234 if (filter->peak[i] >= filter->decay_peak[i])
236 /* g_print ("new peak, %f\n", filter->peak[i]); */
237 filter->decay_peak[i] = filter->peak[i];
238 filter->decay_peak_age[i] = 0;
242 /* make decay peak fall off if too old */
243 if (filter->decay_peak_age[i] > filter->rate * filter->decay_peak_ttl)
247 double length; /* length of buffer in seconds */
250 length = (double) num_samples / (filter->channels * filter->rate);
251 falloff_dB = filter->decay_peak_falloff * length;
252 falloff = pow (10, falloff_dB / -20.0);
254 /* g_print ("falloff: length %f, dB falloff %f, falloff factor %e\n",
255 length, falloff_dB, falloff); */
256 filter->decay_peak[i] *= falloff;
257 /* g_print ("peak is %f samples old, decayed with factor %e to %f\n",
258 filter->decay_peak_age[i], falloff, filter->decay_peak[i]); */
263 /* do we need to emit ? */
265 if (filter->num_samples >= filter->interval * (gdouble) filter->rate)
269 gdouble RMS, peak, endtime;
270 for (i = 0; i < filter->channels; ++i)
272 RMS = sqrt (filter->CS[i] / (filter->num_samples / filter->channels));
273 peak = filter->last_peak[i];
274 num_samples = GST_BUFFER_SIZE (buf) / (filter->width / 8);
275 endtime = (double) GST_BUFFER_TIMESTAMP (buf) / GST_SECOND
276 + (double) num_samples / (double) filter->rate;
278 g_signal_emit (G_OBJECT (filter), gst_filter_signals[SIGNAL_LEVEL], 0,
280 20 * log10 (RMS), 20 * log10 (filter->last_peak[i]),
281 20 * log10 (filter->decay_peak[i]));
282 /* we emitted, so reset cumulative and normal peak */
284 filter->last_peak[i] = 0.0;
287 filter->num_samples = 0;
291 static GstElementStateReturn gst_level_change_state (GstElement *element)
293 GstLevel *filter = GST_LEVEL (element);
295 switch(GST_STATE_TRANSITION(element)){
296 case GST_STATE_PAUSED_TO_PLAYING:
297 if (!filter->inited) return GST_STATE_FAILURE;
303 return GST_ELEMENT_CLASS(parent_class)->change_state(element);
307 gst_level_set_property (GObject *object, guint prop_id,
308 const GValue *value, GParamSpec *pspec)
312 /* it's not null if we got it, but it might not be ours */
313 g_return_if_fail (GST_IS_LEVEL (object));
314 filter = GST_LEVEL (object);
317 case ARG_SIGNAL_LEVEL:
318 filter->signal = g_value_get_boolean (value);
320 case ARG_SIGNAL_INTERVAL:
321 filter->interval = g_value_get_double (value);
324 filter->decay_peak_ttl = g_value_get_double (value);
326 case ARG_PEAK_FALLOFF:
327 filter->decay_peak_falloff = g_value_get_double (value);
335 gst_level_get_property (GObject *object, guint prop_id,
336 GValue *value, GParamSpec *pspec)
340 /* it's not null if we got it, but it might not be ours */
341 g_return_if_fail (GST_IS_LEVEL (object));
342 filter = GST_LEVEL (object);
345 case ARG_SIGNAL_LEVEL:
346 g_value_set_boolean (value, filter->signal);
348 case ARG_SIGNAL_INTERVAL:
349 g_value_set_double (value, filter->interval);
352 g_value_set_double (value, filter->decay_peak_ttl);
354 case ARG_PEAK_FALLOFF:
355 g_value_set_double (value, filter->decay_peak_falloff);
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
364 gst_level_base_init (GstLevelClass *klass)
366 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
368 gst_element_class_add_pad_template (element_class,
369 gst_static_pad_template_get (&sink_template_factory));
370 gst_element_class_add_pad_template (element_class,
371 gst_static_pad_template_get (&src_template_factory));
372 gst_element_class_set_details (element_class, &level_details);
374 element_class->change_state = gst_level_change_state;
378 gst_level_class_init (GstLevelClass *klass)
380 GObjectClass *gobject_class;
381 GstElementClass *gstelement_class;
383 gobject_class = (GObjectClass*) klass;
384 gstelement_class = (GstElementClass*) klass;
386 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
387 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIGNAL_LEVEL,
388 g_param_spec_boolean ("signal", "Signal",
389 "Emit level signals for each interval",
390 TRUE, G_PARAM_READWRITE));
391 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIGNAL_INTERVAL,
392 g_param_spec_double ("interval", "Interval",
393 "Interval between emissions (in seconds)",
394 0.01, 100.0, 0.1, G_PARAM_READWRITE));
395 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PEAK_TTL,
396 g_param_spec_double ("peak_ttl", "Peak TTL",
397 "Time To Live of decay peak before it falls back",
398 0, 100.0, 0.3, G_PARAM_READWRITE));
399 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PEAK_FALLOFF,
400 g_param_spec_double ("peak_falloff", "Peak Falloff",
401 "Decay rate of decay peak after TTL (in dB/sec)",
402 0.0, G_MAXDOUBLE, 10.0, G_PARAM_READWRITE));
404 gobject_class->set_property = gst_level_set_property;
405 gobject_class->get_property = gst_level_get_property;
407 gst_filter_signals[SIGNAL_LEVEL] =
408 g_signal_new ("level", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
409 G_STRUCT_OFFSET (GstLevelClass, level), NULL, NULL,
410 gstlevel_cclosure_marshal_VOID__DOUBLE_INT_DOUBLE_DOUBLE_DOUBLE,
412 G_TYPE_DOUBLE, G_TYPE_INT,
413 G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
417 gst_level_init (GstLevel *filter)
419 filter->sinkpad = gst_pad_new_from_template (gst_static_pad_template_get (&sink_template_factory), "sink");
420 gst_pad_set_link_function (filter->sinkpad, gst_level_link);
421 filter->srcpad = gst_pad_new_from_template (gst_static_pad_template_get (&src_template_factory), "src");
422 gst_pad_set_link_function (filter->srcpad, gst_level_link);
424 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
425 gst_pad_set_chain_function (filter->sinkpad, gst_level_chain);
426 filter->srcpad = gst_pad_new ("src", GST_PAD_SRC);
427 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
432 filter->RMS_dB = NULL;
436 filter->channels = 0;
438 filter->interval = 0.1;
439 filter->decay_peak_ttl = 0.4;
440 filter->decay_peak_falloff = 10.0; /* dB falloff (/sec) */
444 plugin_init (GstPlugin *plugin)
446 return gst_element_register (plugin, "level",
447 GST_RANK_NONE, GST_TYPE_LEVEL);
454 "Audio level plugin",