build: fprintf, sprintf, sscanf need stdio.h
[platform/upstream/gstreamer.git] / tests / examples / spectrum / spectrum-example.c
1 /* GStreamer
2  * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
3  * Copyright (C) 2008 Jan Schmidt <jan.schmidt@sun.com>
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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <gst/gst.h>
25
26 static guint spect_bands = 20;
27
28 #define AUDIOFREQ 32000
29
30 /* receive spectral data from element message */
31 gboolean
32 message_handler (GstBus * bus, GstMessage * message, gpointer data)
33 {
34   if (message->type == GST_MESSAGE_ELEMENT) {
35     const GstStructure *s = gst_message_get_structure (message);
36     const gchar *name = gst_structure_get_name (s);
37     GstClockTime endtime;
38
39     if (strcmp (name, "spectrum") == 0) {
40       const GValue *magnitudes;
41       const GValue *phases;
42       const GValue *mag, *phase;
43       gdouble freq;
44       guint i;
45
46       if (!gst_structure_get_clock_time (s, "endtime", &endtime))
47         endtime = GST_CLOCK_TIME_NONE;
48
49       g_print ("New spectrum message, endtime %" GST_TIME_FORMAT "\n",
50           GST_TIME_ARGS (endtime));
51
52       magnitudes = gst_structure_get_value (s, "magnitude");
53       phases = gst_structure_get_value (s, "phase");
54
55       for (i = 0; i < spect_bands; ++i) {
56         freq = (gdouble) ((AUDIOFREQ / 2) * i + AUDIOFREQ / 4) / spect_bands;
57         mag = gst_value_list_get_value (magnitudes, i);
58         phase = gst_value_list_get_value (phases, i);
59
60         if (mag != NULL && phase != NULL) {
61           g_print ("band %d (freq %g): magnitude %f dB phase %f\n", i, freq,
62               g_value_get_float (mag), g_value_get_float (phase));
63         }
64       }
65       g_print ("\n");
66     }
67   }
68   return TRUE;
69 }
70
71 int
72 main (int argc, char *argv[])
73 {
74   GstElement *bin;
75   GstElement *src, *audioconvert, *spectrum, *sink;
76   GstBus *bus;
77   GstCaps *caps;
78   GMainLoop *loop;
79
80   gst_init (&argc, &argv);
81
82   bin = gst_pipeline_new ("bin");
83
84   src = gst_element_factory_make ("audiotestsrc", "src");
85   g_object_set (G_OBJECT (src), "wave", 0, "freq", 6000.0, NULL);
86   audioconvert = gst_element_factory_make ("audioconvert", NULL);
87   g_assert (audioconvert);
88
89   spectrum = gst_element_factory_make ("spectrum", "spectrum");
90   g_object_set (G_OBJECT (spectrum), "bands", spect_bands, "threshold", -80,
91       "message", TRUE, "message-phase", TRUE, NULL);
92
93   sink = gst_element_factory_make ("fakesink", "sink");
94   g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);
95
96   gst_bin_add_many (GST_BIN (bin), src, audioconvert, spectrum, sink, NULL);
97
98   caps = gst_caps_new_simple ("audio/x-raw-int",
99       "rate", G_TYPE_INT, AUDIOFREQ, NULL);
100
101   if (!gst_element_link (src, audioconvert) ||
102       !gst_element_link_filtered (audioconvert, spectrum, caps) ||
103       !gst_element_link (spectrum, sink)) {
104     fprintf (stderr, "can't link elements\n");
105     exit (1);
106   }
107   gst_caps_unref (caps);
108
109   bus = gst_element_get_bus (bin);
110   gst_bus_add_watch (bus, message_handler, NULL);
111   gst_object_unref (bus);
112
113   gst_element_set_state (bin, GST_STATE_PLAYING);
114
115   /* we need to run a GLib main loop to get the messages */
116   loop = g_main_loop_new (NULL, FALSE);
117   g_main_loop_run (loop);
118
119   gst_element_set_state (bin, GST_STATE_NULL);
120
121   gst_object_unref (bin);
122
123   return 0;
124 }