9c77ec462d5cd0ce8e058973a9d87e83ba74dbd5
[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 <string.h>
22 #include <stdlib.h>
23 #include <gst/gst.h>
24
25 static guint spect_bands = 20;
26
27 #define AUDIOFREQ 32000
28
29 /* receive spectral data from element message */
30 gboolean
31 message_handler (GstBus * bus, GstMessage * message, gpointer data)
32 {
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);
36     GstClockTime endtime;
37
38     if (strcmp (name, "spectrum") == 0) {
39       const GValue *magnitudes;
40       const GValue *phases;
41       const GValue *mag, *phase;
42       gdouble freq;
43       guint i;
44
45       if (!gst_structure_get_clock_time (s, "endtime", &endtime))
46         endtime = GST_CLOCK_TIME_NONE;
47
48       g_print ("New spectrum message, endtime %" GST_TIME_FORMAT "\n",
49           GST_TIME_ARGS (endtime));
50
51       magnitudes = gst_structure_get_value (s, "magnitude");
52       phases = gst_structure_get_value (s, "phase");
53
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);
58
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));
62         }
63       }
64       g_print ("\n");
65     }
66   }
67   return TRUE;
68 }
69
70 int
71 main (int argc, char *argv[])
72 {
73   GstElement *bin;
74   GstElement *src, *audioconvert, *spectrum, *sink;
75   GstBus *bus;
76   GstCaps *caps;
77   GMainLoop *loop;
78
79   gst_init (&argc, &argv);
80
81   bin = gst_pipeline_new ("bin");
82
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);
87
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);
91
92   sink = gst_element_factory_make ("fakesink", "sink");
93   g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);
94
95   gst_bin_add_many (GST_BIN (bin), src, audioconvert, spectrum, sink, NULL);
96
97   caps = gst_caps_new_simple ("audio/x-raw-int",
98       "rate", G_TYPE_INT, AUDIOFREQ, NULL);
99
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");
104     exit (1);
105   }
106   gst_caps_unref (caps);
107
108   bus = gst_element_get_bus (bin);
109   gst_bus_add_watch (bus, message_handler, NULL);
110   gst_object_unref (bus);
111
112   gst_element_set_state (bin, GST_STATE_PLAYING);
113
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);
117
118   gst_element_set_state (bin, GST_STATE_NULL);
119
120   gst_object_unref (bin);
121
122   return 0;
123 }