2005-12-30 15:28:44 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2000,2001,2002,2003,2005
|
|
|
|
* Thomas Vander Stichele <thomas at apestaart dot org>
|
|
|
|
*
|
|
|
|
* 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.
|
2005-12-30 15:28:44 +00:00
|
|
|
*/
|
|
|
|
|
2005-09-14 20:51:47 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2012-10-30 11:10:49 +00:00
|
|
|
#define GLIB_DISABLE_DEPRECATION_WARNINGS
|
|
|
|
|
2005-09-14 20:51:47 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2010-03-17 17:23:00 +00:00
|
|
|
static gboolean
|
2005-09-14 20:51:47 +00:00
|
|
|
message_handler (GstBus * bus, GstMessage * message, gpointer data)
|
|
|
|
{
|
|
|
|
|
2005-10-19 20:48:24 +00:00
|
|
|
if (message->type == GST_MESSAGE_ELEMENT) {
|
2005-09-14 20:51:47 +00:00
|
|
|
const GstStructure *s = gst_message_get_structure (message);
|
|
|
|
const gchar *name = gst_structure_get_name (s);
|
|
|
|
|
|
|
|
if (strcmp (name, "level") == 0) {
|
|
|
|
gint channels;
|
2005-09-23 18:15:51 +00:00
|
|
|
GstClockTime endtime;
|
|
|
|
gdouble rms_dB, peak_dB, decay_dB;
|
2005-09-14 20:51:47 +00:00
|
|
|
gdouble rms;
|
2012-10-30 11:10:49 +00:00
|
|
|
const GValue *array_val;
|
2005-09-14 20:51:47 +00:00
|
|
|
const GValue *value;
|
2013-02-05 07:26:14 +00:00
|
|
|
GValueArray *rms_arr, *peak_arr, *decay_arr;
|
2005-09-23 18:15:51 +00:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (!gst_structure_get_clock_time (s, "endtime", &endtime))
|
2005-09-14 20:51:47 +00:00
|
|
|
g_warning ("Could not parse endtime");
|
2013-02-05 07:26:14 +00:00
|
|
|
|
|
|
|
/* the values are packed into GValueArrays with the value per channel */
|
2012-10-30 11:10:49 +00:00
|
|
|
array_val = gst_structure_get_value (s, "rms");
|
2013-02-05 07:26:14 +00:00
|
|
|
rms_arr = (GValueArray *) g_value_get_boxed (array_val);
|
|
|
|
|
|
|
|
array_val = gst_structure_get_value (s, "peak");
|
|
|
|
peak_arr = (GValueArray *) g_value_get_boxed (array_val);
|
2005-09-14 20:51:47 +00:00
|
|
|
|
2013-02-05 07:26:14 +00:00
|
|
|
array_val = gst_structure_get_value (s, "decay");
|
|
|
|
decay_arr = (GValueArray *) g_value_get_boxed (array_val);
|
|
|
|
|
|
|
|
/* we can get the number of channels as the length of any of the value
|
|
|
|
* arrays */
|
|
|
|
channels = rms_arr->n_values;
|
2005-09-23 18:15:51 +00:00
|
|
|
g_print ("endtime: %" GST_TIME_FORMAT ", channels: %d\n",
|
|
|
|
GST_TIME_ARGS (endtime), channels);
|
|
|
|
for (i = 0; i < channels; ++i) {
|
2012-10-30 11:10:49 +00:00
|
|
|
|
2005-09-23 18:15:51 +00:00
|
|
|
g_print ("channel %d\n", i);
|
2013-02-05 07:26:14 +00:00
|
|
|
value = g_value_array_get_nth (rms_arr, i);
|
2005-09-23 18:15:51 +00:00
|
|
|
rms_dB = g_value_get_double (value);
|
2013-02-05 07:26:14 +00:00
|
|
|
|
|
|
|
value = g_value_array_get_nth (peak_arr, i);
|
2005-09-23 18:15:51 +00:00
|
|
|
peak_dB = g_value_get_double (value);
|
2013-02-05 07:26:14 +00:00
|
|
|
|
|
|
|
value = g_value_array_get_nth (decay_arr, i);
|
2005-09-23 18:15:51 +00:00
|
|
|
decay_dB = g_value_get_double (value);
|
|
|
|
g_print (" RMS: %f dB, peak: %f dB, decay: %f dB\n",
|
|
|
|
rms_dB, peak_dB, decay_dB);
|
|
|
|
|
|
|
|
/* converting from dB to normal gives us a value between 0.0 and 1.0 */
|
|
|
|
rms = pow (10, rms_dB / 20);
|
|
|
|
g_print (" normalized rms value: %f\n", rms);
|
|
|
|
}
|
2005-09-14 20:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* we handled the message we want, and ignored the ones we didn't want.
|
|
|
|
* so the core can unref the message for us */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2005-12-11 19:25:41 +00:00
|
|
|
GstElement *audiotestsrc, *audioconvert, *level, *fakesink;
|
2005-09-14 20:51:47 +00:00
|
|
|
GstElement *pipeline;
|
2005-09-23 18:15:51 +00:00
|
|
|
GstCaps *caps;
|
2005-09-14 20:51:47 +00:00
|
|
|
GstBus *bus;
|
2011-04-15 13:11:35 +00:00
|
|
|
guint watch_id;
|
2005-09-14 20:51:47 +00:00
|
|
|
GMainLoop *loop;
|
|
|
|
|
|
|
|
gst_init (&argc, &argv);
|
|
|
|
|
2011-08-19 12:01:45 +00:00
|
|
|
caps = gst_caps_from_string ("audio/x-raw,channels=2");
|
2005-09-23 18:15:51 +00:00
|
|
|
|
2005-09-14 20:51:47 +00:00
|
|
|
pipeline = gst_pipeline_new (NULL);
|
|
|
|
g_assert (pipeline);
|
2005-12-11 19:25:41 +00:00
|
|
|
audiotestsrc = gst_element_factory_make ("audiotestsrc", NULL);
|
|
|
|
g_assert (audiotestsrc);
|
2005-09-23 18:15:51 +00:00
|
|
|
audioconvert = gst_element_factory_make ("audioconvert", NULL);
|
|
|
|
g_assert (audioconvert);
|
2005-09-14 20:51:47 +00:00
|
|
|
level = gst_element_factory_make ("level", NULL);
|
|
|
|
g_assert (level);
|
|
|
|
fakesink = gst_element_factory_make ("fakesink", NULL);
|
|
|
|
g_assert (fakesink);
|
|
|
|
|
2005-12-11 19:25:41 +00:00
|
|
|
gst_bin_add_many (GST_BIN (pipeline), audiotestsrc, audioconvert, level,
|
2005-09-23 18:15:51 +00:00
|
|
|
fakesink, NULL);
|
2011-04-16 16:57:32 +00:00
|
|
|
if (!gst_element_link (audiotestsrc, audioconvert))
|
|
|
|
g_error ("Failed to link audiotestsrc and audioconvert");
|
|
|
|
if (!gst_element_link_filtered (audioconvert, level, caps))
|
|
|
|
g_error ("Failed to link audioconvert and level");
|
|
|
|
if (!gst_element_link (level, fakesink))
|
|
|
|
g_error ("Failed to link level and fakesink");
|
2005-09-14 20:51:47 +00:00
|
|
|
|
|
|
|
/* make sure we'll get messages */
|
2013-02-28 07:44:18 +00:00
|
|
|
g_object_set (G_OBJECT (level), "post-messages", TRUE, NULL);
|
2008-11-26 21:19:47 +00:00
|
|
|
/* run synced and not as fast as we can */
|
|
|
|
g_object_set (G_OBJECT (fakesink), "sync", TRUE, NULL);
|
2005-09-14 20:51:47 +00:00
|
|
|
|
|
|
|
bus = gst_element_get_bus (pipeline);
|
2005-09-29 13:08:41 +00:00
|
|
|
watch_id = gst_bus_add_watch (bus, message_handler, NULL);
|
2005-09-14 20:51:47 +00:00
|
|
|
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_PLAYING);
|
|
|
|
|
|
|
|
/* we need to run a GLib main loop to get the messages */
|
|
|
|
loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
g_main_loop_run (loop);
|
|
|
|
|
2011-04-15 13:11:35 +00:00
|
|
|
g_source_remove (watch_id);
|
|
|
|
g_main_loop_unref (loop);
|
2005-09-14 20:51:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|