2 * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
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.
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.
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.
30 #ifndef DEFAULT_AUDIOSRC
31 #define DEFAULT_AUDIOSRC "alsasrc"
34 static guint spect_height = 64;
35 static guint spect_bands = 256;
36 static gfloat height_scale = 1.0;
38 static GtkWidget *drawingarea = NULL;
39 static GstClock *sync_clock = NULL;
42 on_window_destroy (GtkObject * object, gpointer user_data)
49 on_configure_event (GtkWidget * widget, GdkEventConfigure * event,
52 GstElement *spectrum = GST_ELEMENT (user_data);
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;
59 g_object_set (G_OBJECT (spectrum), "bands", spect_bands, NULL);
63 /* draw frequency spectrum as a bunch of bars */
65 draw_spectrum (gfloat * data)
68 GdkRectangle rect = { 0, 0, spect_bands, spect_height };
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]);
80 gdk_window_end_paint (drawingarea->window);
83 /* process delayed message */
85 delayed_idle_spectrum_update (gpointer user_data)
87 draw_spectrum ((gfloat *) user_data);
93 delayed_spectrum_update (GstClock * sync_clock, GstClockTime time,
94 GstClockID id, gpointer user_data)
96 if (GST_CLOCK_TIME_IS_VALID (time))
97 g_idle_add (delayed_idle_spectrum_update, user_data);
103 /* receive spectral data from element message */
105 message_handler (GstBus * bus, GstMessage * message, gpointer data)
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);
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;
116 if (gst_structure_get_clock_time (s, "running-time", ×tamp) &&
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", ×tamp)) {
121 waittime = timestamp;
123 if (GST_CLOCK_TIME_IS_VALID (waittime)) {
125 GstClockTime basetime = gst_element_get_base_time (spectrum);
126 gfloat *spect = g_new (gfloat, spect_bands);
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);
138 gst_clock_new_single_shot_id (sync_clock, waittime + basetime);
139 gst_clock_id_wait_async (clock_id, delayed_spectrum_update,
141 gst_clock_id_unref (clock_id);
149 main (int argc, char *argv[])
152 GstElement *src, *spectrum, *sink;
154 GtkWidget *appwindow;
156 gst_init (&argc, &argv);
157 gtk_init (&argc, &argv);
159 bin = gst_pipeline_new ("bin");
161 src = gst_element_factory_make (DEFAULT_AUDIOSRC, "src");
163 spectrum = gst_element_factory_make ("spectrum", "spectrum");
164 g_object_set (G_OBJECT (spectrum), "bands", spect_bands, "threshold", -80,
165 "message", TRUE, NULL);
167 sink = gst_element_factory_make ("fakesink", "sink");
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");
175 bus = gst_element_get_bus (bin);
176 gst_bus_add_watch (bus, message_handler, NULL);
177 gst_object_unref (bus);
179 sync_clock = gst_pipeline_get_clock (GST_PIPELINE (bin));
181 appwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
182 g_signal_connect (G_OBJECT (appwindow), "destroy",
183 G_CALLBACK (on_window_destroy), NULL);
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);
192 gst_element_set_state (bin, GST_STATE_PLAYING);
194 gst_element_set_state (bin, GST_STATE_NULL);
196 gst_object_unref (sync_clock);
197 gst_object_unref (bin);