2 * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
3 * Copyright (C) 2008 Jan Schmidt <jan.schmidt@sun.com>
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 static guint spect_bands = 20;
27 #define AUDIOFREQ 32000
29 /* receive spectral data from element message */
31 message_handler (GstBus * bus, GstMessage * message, gpointer data)
33 if (message->type == GST_MESSAGE_ELEMENT) {
34 const GstStructure *s = gst_message_get_structure (message);
35 const gchar *name = gst_structure_get_name (s);
38 if (strcmp (name, "spectrum") == 0) {
39 const GValue *magnitudes;
41 const GValue *mag, *phase;
45 if (!gst_structure_get_clock_time (s, "endtime", &endtime))
46 endtime = GST_CLOCK_TIME_NONE;
48 g_print ("New spectrum message, endtime %" GST_TIME_FORMAT "\n",
49 GST_TIME_ARGS (endtime));
51 magnitudes = gst_structure_get_value (s, "magnitude");
52 phases = gst_structure_get_value (s, "phase");
54 for (i = 0; i < spect_bands; ++i) {
55 freq = (gdouble) ((AUDIOFREQ / 2) * i + AUDIOFREQ / 4) / spect_bands;
56 mag = gst_value_list_get_value (magnitudes, i);
57 phase = gst_value_list_get_value (phases, i);
59 if (mag != NULL && phase != NULL) {
60 g_print ("band %d (freq %g): magnitude %f dB phase %f\n", i, freq,
61 g_value_get_float (mag), g_value_get_float (phase));
71 main (int argc, char *argv[])
74 GstElement *src, *audioconvert, *spectrum, *sink;
79 gst_init (&argc, &argv);
81 bin = gst_pipeline_new ("bin");
83 src = gst_element_factory_make ("audiotestsrc", "src");
84 g_object_set (G_OBJECT (src), "wave", 0, "freq", 6000.0, NULL);
85 audioconvert = gst_element_factory_make ("audioconvert", NULL);
86 g_assert (audioconvert);
88 spectrum = gst_element_factory_make ("spectrum", "spectrum");
89 g_object_set (G_OBJECT (spectrum), "bands", spect_bands, "threshold", -80,
90 "message", TRUE, "message-phase", TRUE, NULL);
92 sink = gst_element_factory_make ("fakesink", "sink");
93 g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);
95 gst_bin_add_many (GST_BIN (bin), src, audioconvert, spectrum, sink, NULL);
97 caps = gst_caps_new_simple ("audio/x-raw-int",
98 "rate", G_TYPE_INT, AUDIOFREQ, NULL);
100 if (!gst_element_link (src, audioconvert) ||
101 !gst_element_link_filtered (audioconvert, spectrum, caps) ||
102 !gst_element_link (spectrum, sink)) {
103 fprintf (stderr, "can't link elements\n");
106 gst_caps_unref (caps);
108 bus = gst_element_get_bus (bin);
109 gst_bus_add_watch (bus, message_handler, NULL);
110 gst_object_unref (bus);
112 gst_element_set_state (bin, GST_STATE_PLAYING);
114 /* we need to run a GLib main loop to get the messages */
115 loop = g_main_loop_new (NULL, FALSE);
116 g_main_loop_run (loop);
118 gst_element_set_state (bin, GST_STATE_NULL);
120 gst_object_unref (bin);