c7ae8462e3f794a8b73db6adf506c7ba218a879a
[platform/upstream/gstreamer.git] / tests / examples / spectrum / demo-osssrc.c
1 /* GStreamer
2  * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include <gst/gst.h>
28 #include <gtk/gtk.h>
29
30 #ifndef DEFAULT_AUDIOSRC
31 #define DEFAULT_AUDIOSRC "alsasrc"
32 #endif
33
34 static guint spect_height = 64;
35 static guint spect_bands = 256;
36 static gfloat height_scale = 1.0;
37
38 static GtkWidget *drawingarea = NULL;
39 static GstClock *sync_clock = NULL;
40
41 static void
42 on_window_destroy (GtkObject * object, gpointer user_data)
43 {
44   drawingarea = NULL;
45   gtk_main_quit ();
46 }
47
48 static gboolean
49 on_configure_event (GtkWidget * widget, GdkEventConfigure * event,
50     gpointer user_data)
51 {
52   GstElement *spectrum = GST_ELEMENT (user_data);
53
54   /*GST_INFO ("%d x %d", event->width, event->height); */
55   spect_height = event->height;
56   height_scale = event->height / 64.0;
57   spect_bands = event->width;
58
59   g_object_set (G_OBJECT (spectrum), "bands", spect_bands, NULL);
60   return FALSE;
61 }
62
63 /* draw frequency spectrum as a bunch of bars */
64 static void
65 draw_spectrum (gfloat * data)
66 {
67   gint i;
68   GdkRectangle rect = { 0, 0, spect_bands, spect_height };
69
70   if (!drawingarea)
71     return;
72
73   gdk_window_begin_paint_rect (drawingarea->window, &rect);
74   gdk_draw_rectangle (drawingarea->window, drawingarea->style->black_gc,
75       TRUE, 0, 0, spect_bands, spect_height);
76   for (i = 0; i < spect_bands; i++) {
77     gdk_draw_rectangle (drawingarea->window, drawingarea->style->white_gc,
78         TRUE, i, -data[i], 1, spect_height + data[i]);
79   }
80   gdk_window_end_paint (drawingarea->window);
81 }
82
83 /* process delayed message */
84 static gboolean
85 delayed_idle_spectrum_update (gpointer user_data)
86 {
87   draw_spectrum ((gfloat *) user_data);
88   g_free (user_data);
89   return (FALSE);
90 }
91
92 static gboolean
93 delayed_spectrum_update (GstClock * sync_clock, GstClockTime time,
94     GstClockID id, gpointer user_data)
95 {
96   if (GST_CLOCK_TIME_IS_VALID (time))
97     g_idle_add (delayed_idle_spectrum_update, user_data);
98   else
99     g_free (user_data);
100   return (TRUE);
101 }
102
103 /* receive spectral data from element message */
104 static gboolean
105 message_handler (GstBus * bus, GstMessage * message, gpointer data)
106 {
107   if (message->type == GST_MESSAGE_ELEMENT) {
108     const GstStructure *s = gst_message_get_structure (message);
109     const gchar *name = gst_structure_get_name (s);
110
111     if (strcmp (name, "spectrum") == 0) {
112       GstElement *spectrum = GST_ELEMENT (GST_MESSAGE_SRC (message));
113       GstClockTime timestamp, duration;
114       GstClockTime waittime = GST_CLOCK_TIME_NONE;
115
116       if (gst_structure_get_clock_time (s, "running-time", &timestamp) &&
117           gst_structure_get_clock_time (s, "duration", &duration)) {
118         /* wait for middle of buffer */
119         waittime = timestamp + duration / 2;
120       } else if (gst_structure_get_clock_time (s, "endtime", &timestamp)) {
121         waittime = timestamp;
122       }
123       if (GST_CLOCK_TIME_IS_VALID (waittime)) {
124         GstClockID clock_id;
125         GstClockTime basetime = gst_element_get_base_time (spectrum);
126         gfloat *spect = g_new (gfloat, spect_bands);
127         const GValue *list;
128         const GValue *value;
129         guint i;
130
131         list = gst_structure_get_value (s, "magnitude");
132         for (i = 0; i < spect_bands; ++i) {
133           value = gst_value_list_get_value (list, i);
134           spect[i] = height_scale * g_value_get_float (value);
135         }
136
137         clock_id =
138             gst_clock_new_single_shot_id (sync_clock, waittime + basetime);
139         gst_clock_id_wait_async (clock_id, delayed_spectrum_update,
140             (gpointer) spect);
141         gst_clock_id_unref (clock_id);
142       }
143     }
144   }
145   return TRUE;
146 }
147
148 int
149 main (int argc, char *argv[])
150 {
151   GstElement *bin;
152   GstElement *src, *spectrum, *sink;
153   GstBus *bus;
154   GtkWidget *appwindow;
155
156   gst_init (&argc, &argv);
157   gtk_init (&argc, &argv);
158
159   bin = gst_pipeline_new ("bin");
160
161   src = gst_element_factory_make (DEFAULT_AUDIOSRC, "src");
162
163   spectrum = gst_element_factory_make ("spectrum", "spectrum");
164   g_object_set (G_OBJECT (spectrum), "bands", spect_bands, "threshold", -80,
165       "message", TRUE, NULL);
166
167   sink = gst_element_factory_make ("fakesink", "sink");
168
169   gst_bin_add_many (GST_BIN (bin), src, spectrum, sink, NULL);
170   if (!gst_element_link_many (src, spectrum, sink, NULL)) {
171     fprintf (stderr, "can't link elements\n");
172     exit (1);
173   }
174
175   bus = gst_element_get_bus (bin);
176   gst_bus_add_watch (bus, message_handler, NULL);
177   gst_object_unref (bus);
178
179   sync_clock = gst_pipeline_get_clock (GST_PIPELINE (bin));
180
181   appwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
182   g_signal_connect (G_OBJECT (appwindow), "destroy",
183       G_CALLBACK (on_window_destroy), NULL);
184
185   drawingarea = gtk_drawing_area_new ();
186   gtk_widget_set_size_request (drawingarea, spect_bands, spect_height);
187   g_signal_connect (G_OBJECT (drawingarea), "configure-event",
188       G_CALLBACK (on_configure_event), (gpointer) spectrum);
189   gtk_container_add (GTK_CONTAINER (appwindow), drawingarea);
190   gtk_widget_show_all (appwindow);
191
192   gst_element_set_state (bin, GST_STATE_PLAYING);
193   gtk_main ();
194   gst_element_set_state (bin, GST_STATE_NULL);
195
196   gst_object_unref (sync_clock);
197   gst_object_unref (bin);
198
199   return 0;
200 }