27ecd01ac99796ed79e97bb4cbeb02ee0bd1e4c9
[platform/upstream/gst-plugins-good.git] / gst / level / plot.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * plot.c: output data points to be graphed with gnuplot
5  * Copyright (C) 2003
6  *           Thomas Vander Stichele <thomas at apestaart dot org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/gst.h>
29 #include <gtk/gtk.h>
30
31 gboolean got_channel[2] = { FALSE, FALSE}; /* to see if we got the signal for this one yet */
32 gint channels = 0 ;      /* guess at how many channels there are */
33 gdouble last_time = 0.0; /* time of last signal */
34 gdouble values[2][3];    /* array of levels from which to print */
35
36 static void
37 level_callback (GstElement *element, gdouble time, gint channel,
38                 gdouble rms, gdouble peak, gdouble decay)
39 {
40   int i = 0, j = 0;
41   gboolean got_all = FALSE;
42
43   if (channel  + 1> channels) channels = channel + 1;
44
45   /* reset got_channel if this is a new time point */
46   if (time > last_time)
47   {
48     for (i = 0; i < channels; ++i)  got_channel[i] = FALSE;
49     last_time = time;
50   }
51
52   /* store values */
53   got_channel[channel] = TRUE;
54   values[channel][0] = rms;
55   values[channel][1] = peak;
56   values[channel][2] = decay;
57
58   /* check if we have all channels, and output if we do */
59   /* FIXME: this fails on the first, no ? */
60   got_all = TRUE;
61   for (i = 0; i < channels; ++i)
62     if (!got_channel[i]) got_all = FALSE;
63   if (got_all)
64   {
65     g_print ("%f ", time);
66     for (i = 0; i < channels; ++i)
67       for (j = 0; j < 3; ++j)
68         g_print ("%f ", values[i][j]);
69     g_print ("\n");
70   }
71 }
72
73 static gboolean
74 idler (gpointer data)
75 {
76   GstElement *pipeline = GST_ELEMENT (data);
77   if (gst_bin_iterate (GST_BIN (pipeline)))
78     return TRUE;
79
80   gtk_main_quit ();
81   return FALSE;
82 }
83
84 int main
85 (int argc, char *argv[])
86 {
87
88   GstElement *pipeline = NULL;
89   GError *error = NULL;
90   GstElement *level;
91
92   gst_init (&argc, &argv);
93   gtk_init (&argc, &argv);
94
95   pipeline = gst_parse_launchv ((const gchar **) &argv[1], &error);
96   if (error)
97   {
98     g_print ("pipeline could not be constructed: %s\n", error->message);
99     g_print ("Please give a complete pipeline  with a 'level' element.\n");
100     g_print ("Example: sinesrc ! level ! osssink\n");
101     g_error_free (error);
102     return 1;
103   }
104
105   level = gst_bin_get_by_name (GST_BIN (pipeline), "level0");
106   if (level == NULL)
107   {
108     g_print ("Please give a pipeline with a 'level' element in it\n");
109     return 1;
110   }
111
112   g_object_set (level, "signal", TRUE, NULL);
113   g_signal_connect (level, "level", G_CALLBACK (level_callback), NULL);
114
115   
116   /* go to main loop */
117   gst_element_set_state (pipeline, GST_STATE_PLAYING);
118   g_idle_add (idler, pipeline);
119
120   gtk_main ();
121
122   return 0;
123 }
124