1 /* GStreamer
2 * Copyright (C) 2000,2001,2002,2003,2005
3 * Thomas Vander Stichele <thomas at apestaart dot org>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #include <string.h>
22 #include <math.h>
23
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
25
26 #include <gst/gst.h>
27
28 static gboolean
message_handler(GstBus * bus,GstMessage * message,gpointer data)29 message_handler (GstBus * bus, GstMessage * message, gpointer data)
30 {
31
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);
35
36 if (strcmp (name, "level") == 0) {
37 gint channels;
38 GstClockTime endtime;
39 gdouble rms_dB, peak_dB, decay_dB;
40 gdouble rms;
41 const GValue *array_val;
42 const GValue *value;
43 GValueArray *rms_arr, *peak_arr, *decay_arr;
44 gint i;
45
46 if (!gst_structure_get_clock_time (s, "endtime", &endtime))
47 g_warning ("Could not parse endtime");
48
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);
52
53 array_val = gst_structure_get_value (s, "peak");
54 peak_arr = (GValueArray *) g_value_get_boxed (array_val);
55
56 array_val = gst_structure_get_value (s, "decay");
57 decay_arr = (GValueArray *) g_value_get_boxed (array_val);
58
59 /* we can get the number of channels as the length of any of the value
60 * arrays */
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) {
65
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);
69
70 value = g_value_array_get_nth (peak_arr, i);
71 peak_dB = g_value_get_double (value);
72
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);
77
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);
81 }
82 }
83 }
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 */
86 return TRUE;
87 }
88
89 int
main(int argc,char * argv[])90 main (int argc, char *argv[])
91 {
92 GstElement *audiotestsrc, *audioconvert, *level, *fakesink;
93 GstElement *pipeline;
94 GstCaps *caps;
95 GstBus *bus;
96 guint watch_id;
97 GMainLoop *loop;
98
99 gst_init (&argc, &argv);
100
101 caps = gst_caps_from_string ("audio/x-raw,channels=2");
102
103 pipeline = gst_pipeline_new (NULL);
104 g_assert (pipeline);
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);
110 g_assert (level);
111 fakesink = gst_element_factory_make ("fakesink", NULL);
112 g_assert (fakesink);
113
114 gst_bin_add_many (GST_BIN (pipeline), audiotestsrc, audioconvert, level,
115 fakesink, NULL);
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");
122
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);
127
128 bus = gst_element_get_bus (pipeline);
129 watch_id = gst_bus_add_watch (bus, message_handler, NULL);
130
131 gst_element_set_state (pipeline, GST_STATE_PLAYING);
132
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);
136
137 g_source_remove (watch_id);
138 g_main_loop_unref (loop);
139 return 0;
140 }
141