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.
19 /* TODO: add wave selection */
32 #ifndef DEFAULT_AUDIOSINK
33 #define DEFAULT_AUDIOSINK "autoaudiosink"
36 static guint spect_height = 64;
37 static guint spect_bands = 256;
38 static gfloat height_scale = 1.0;
40 static GtkWidget *drawingarea = NULL;
41 static GstClock *sync_clock = NULL;
44 on_window_destroy (GtkObject * object, gpointer user_data)
50 /* control audiotestsrc frequency */
52 on_frequency_changed (GtkRange * range, gpointer user_data)
54 GstElement *machine = GST_ELEMENT (user_data);
55 gdouble value = gtk_range_get_value (range);
57 g_object_set (machine, "freq", value, NULL);
61 on_configure_event (GtkWidget * widget, GdkEventConfigure * event,
64 GstElement *spectrum = GST_ELEMENT (user_data);
66 /*GST_INFO ("%d x %d", event->width, event->height); */
67 spect_height = event->height;
68 height_scale = event->height / 64.0;
69 spect_bands = event->width;
71 g_object_set (G_OBJECT (spectrum), "bands", spect_bands, NULL);
75 /* draw frequency spectrum as a bunch of bars */
77 draw_spectrum (gfloat * data)
80 GdkRectangle rect = { 0, 0, spect_bands, spect_height };
85 gdk_window_begin_paint_rect (drawingarea->window, &rect);
86 gdk_draw_rectangle (drawingarea->window, drawingarea->style->black_gc,
87 TRUE, 0, 0, spect_bands, spect_height);
88 for (i = 0; i < spect_bands; i++) {
89 gdk_draw_rectangle (drawingarea->window, drawingarea->style->white_gc,
90 TRUE, i, -data[i], 1, spect_height + data[i]);
92 gdk_window_end_paint (drawingarea->window);
95 /* process delayed message */
97 delayed_idle_spectrum_update (gpointer user_data)
99 draw_spectrum ((gfloat *) user_data);
105 delayed_spectrum_update (GstClock * sync_clock, GstClockTime time,
106 GstClockID id, gpointer user_data)
108 if (GST_CLOCK_TIME_IS_VALID (time))
109 g_idle_add (delayed_idle_spectrum_update, user_data);
115 /* receive spectral data from element message */
117 message_handler (GstBus * bus, GstMessage * message, gpointer data)
119 if (message->type == GST_MESSAGE_ELEMENT) {
120 const GstStructure *s = gst_message_get_structure (message);
121 const gchar *name = gst_structure_get_name (s);
123 if (strcmp (name, "spectrum") == 0) {
124 GstElement *spectrum = GST_ELEMENT (GST_MESSAGE_SRC (message));
125 GstClockTime timestamp, duration;
126 GstClockTime waittime = GST_CLOCK_TIME_NONE;
128 if (gst_structure_get_clock_time (s, "running-time", ×tamp) &&
129 gst_structure_get_clock_time (s, "duration", &duration)) {
130 /* wait for middle of buffer */
131 waittime = timestamp + duration / 2;
132 } else if (gst_structure_get_clock_time (s, "endtime", ×tamp)) {
133 waittime = timestamp;
135 if (GST_CLOCK_TIME_IS_VALID (waittime)) {
137 GstClockTime basetime = gst_element_get_base_time (spectrum);
138 gfloat *spect = g_new (gfloat, spect_bands);
143 list = gst_structure_get_value (s, "magnitude");
144 for (i = 0; i < spect_bands; ++i) {
145 value = gst_value_list_get_value (list, i);
146 spect[i] = height_scale * g_value_get_float (value);
150 gst_clock_new_single_shot_id (sync_clock, waittime + basetime);
151 gst_clock_id_wait_async (clock_id, delayed_spectrum_update,
153 gst_clock_id_unref (clock_id);
161 main (int argc, char *argv[])
164 GstElement *src, *spectrum, *audioconvert, *sink;
166 GtkWidget *appwindow, *vbox, *widget;
168 gst_init (&argc, &argv);
169 gtk_init (&argc, &argv);
171 bin = gst_pipeline_new ("bin");
173 src = gst_element_factory_make ("audiotestsrc", "src");
174 g_object_set (G_OBJECT (src), "wave", 0, NULL);
176 spectrum = gst_element_factory_make ("spectrum", "spectrum");
177 g_object_set (G_OBJECT (spectrum), "bands", spect_bands, "threshold", -80,
178 "message", TRUE, NULL);
180 audioconvert = gst_element_factory_make ("audioconvert", "audioconvert");
182 sink = gst_element_factory_make (DEFAULT_AUDIOSINK, "sink");
184 gst_bin_add_many (GST_BIN (bin), src, spectrum, audioconvert, sink, NULL);
185 if (!gst_element_link_many (src, spectrum, audioconvert, sink, NULL)) {
186 fprintf (stderr, "can't link elements\n");
190 bus = gst_element_get_bus (bin);
191 gst_bus_add_watch (bus, message_handler, NULL);
192 gst_object_unref (bus);
194 sync_clock = gst_pipeline_get_clock (GST_PIPELINE (bin));
196 appwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
197 g_signal_connect (G_OBJECT (appwindow), "destroy",
198 G_CALLBACK (on_window_destroy), NULL);
199 vbox = gtk_vbox_new (FALSE, 6);
201 widget = gtk_hscale_new_with_range (50.0, 20000.0, 10);
202 gtk_scale_set_draw_value (GTK_SCALE (widget), TRUE);
203 gtk_scale_set_value_pos (GTK_SCALE (widget), GTK_POS_TOP);
204 gtk_range_set_value (GTK_RANGE (widget), 440.0);
205 g_signal_connect (G_OBJECT (widget), "value-changed",
206 G_CALLBACK (on_frequency_changed), (gpointer) src);
207 gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
209 drawingarea = gtk_drawing_area_new ();
210 gtk_widget_set_size_request (drawingarea, spect_bands, spect_height);
211 g_signal_connect (G_OBJECT (drawingarea), "configure-event",
212 G_CALLBACK (on_configure_event), (gpointer) spectrum);
213 gtk_box_pack_start (GTK_BOX (vbox), drawingarea, TRUE, TRUE, 0);
215 gtk_container_add (GTK_CONTAINER (appwindow), vbox);
216 gtk_widget_show_all (appwindow);
218 gst_element_set_state (bin, GST_STATE_PLAYING);
220 gst_element_set_state (bin, GST_STATE_NULL);
222 gst_object_unref (sync_clock);
223 gst_object_unref (bin);