2 * Copyright (C) 2000,2001,2002,2003,2005
3 * Thomas Vander Stichele <thomas at apestaart dot org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
29 message_handler (GstBus * bus, GstMessage * message, gpointer data)
32 if (message->type == GST_MESSAGE_ELEMENT) {
33 const GstStructure *s = gst_message_get_structure (message);
34 const gchar *name = gst_structure_get_name (s);
36 if (strcmp (name, "level") == 0) {
39 gdouble rms_dB, peak_dB, decay_dB;
41 const GValue *array_val;
43 GValueArray *rms_arr, *peak_arr, *decay_arr;
46 if (!gst_structure_get_clock_time (s, "endtime", &endtime))
47 g_warning ("Could not parse endtime");
49 /* the values are packed into GValueArrays with the value per channel */
50 array_val = gst_structure_get_value (s, "rms");
51 rms_arr = (GValueArray *) g_value_get_boxed (array_val);
53 array_val = gst_structure_get_value (s, "peak");
54 peak_arr = (GValueArray *) g_value_get_boxed (array_val);
56 array_val = gst_structure_get_value (s, "decay");
57 decay_arr = (GValueArray *) g_value_get_boxed (array_val);
59 /* we can get the number of channels as the length of any of the value
61 channels = rms_arr->n_values;
62 g_print ("endtime: %" GST_TIME_FORMAT ", channels: %d\n",
63 GST_TIME_ARGS (endtime), channels);
64 for (i = 0; i < channels; ++i) {
66 g_print ("channel %d\n", i);
67 value = g_value_array_get_nth (rms_arr, i);
68 rms_dB = g_value_get_double (value);
70 value = g_value_array_get_nth (peak_arr, i);
71 peak_dB = g_value_get_double (value);
73 value = g_value_array_get_nth (decay_arr, i);
74 decay_dB = g_value_get_double (value);
75 g_print (" RMS: %f dB, peak: %f dB, decay: %f dB\n",
76 rms_dB, peak_dB, decay_dB);
78 /* converting from dB to normal gives us a value between 0.0 and 1.0 */
79 rms = pow (10, rms_dB / 20);
80 g_print (" normalized rms value: %f\n", rms);
84 /* we handled the message we want, and ignored the ones we didn't want.
85 * so the core can unref the message for us */
90 main (int argc, char *argv[])
92 GstElement *audiotestsrc, *audioconvert, *level, *fakesink;
99 gst_init (&argc, &argv);
101 caps = gst_caps_from_string ("audio/x-raw,channels=2");
103 pipeline = gst_pipeline_new (NULL);
105 audiotestsrc = gst_element_factory_make ("audiotestsrc", NULL);
106 g_assert (audiotestsrc);
107 audioconvert = gst_element_factory_make ("audioconvert", NULL);
108 g_assert (audioconvert);
109 level = gst_element_factory_make ("level", NULL);
111 fakesink = gst_element_factory_make ("fakesink", NULL);
114 gst_bin_add_many (GST_BIN (pipeline), audiotestsrc, audioconvert, level,
116 if (!gst_element_link (audiotestsrc, audioconvert))
117 g_error ("Failed to link audiotestsrc and audioconvert");
118 if (!gst_element_link_filtered (audioconvert, level, caps))
119 g_error ("Failed to link audioconvert and level");
120 if (!gst_element_link (level, fakesink))
121 g_error ("Failed to link level and fakesink");
123 /* make sure we'll get messages */
124 g_object_set (G_OBJECT (level), "post-messages", TRUE, NULL);
125 /* run synced and not as fast as we can */
126 g_object_set (G_OBJECT (fakesink), "sync", TRUE, NULL);
128 bus = gst_element_get_bus (pipeline);
129 watch_id = gst_bus_add_watch (bus, message_handler, NULL);
131 gst_element_set_state (pipeline, GST_STATE_PLAYING);
133 /* we need to run a GLib main loop to get the messages */
134 loop = g_main_loop_new (NULL, FALSE);
135 g_main_loop_run (loop);
137 g_source_remove (watch_id);
138 g_main_loop_unref (loop);