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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 /* TODO: add wave selection */
20 /* fast vs. slow mode - see update_spectrum_bands()
21 * we are still cheating below, we only draw the first spect_bands and ignore a few :/
22 *
23 * xtime gst-launch-0.10 -q audiotestsrc num-buffers=10000 ! spectrum bands=1441 ! fakesink
24 * 2.29u 0.02s 2.14r 25504kB gst-launch-0.10 -q audiotestsrc num-buffers=10000 ! spectrum bands=1441 ! fakesink
25 * 2.20u 0.05s 2.10r 25664kB gst-launch-0.10 -q audiotestsrc num-buffers=10000 ! spectrum bands=1441 ! fakesink
26 * 2.23u 0.04s 2.10r 25664kB gst-launch-0.10 -q audiotestsrc num-buffers=10000 ! spectrum bands=1441 ! fakesink
27 *
28 * xtime gst-launch-0.10 -q audiotestsrc num-buffers=10000 ! spectrum bands=1440 ! fakesink
29 * 25.01u 0.08s 25.00r 25552kB gst-launch-0.10 -q audiotestsrc num-buffers=10000 ! spectrum bands=1440 ! fakesink
30 * 24.96u 0.03s 24.88r 25568kB gst-launch-0.10 -q audiotestsrc num-buffers=10000 ! spectrum bands=1440 ! fakesink
31 * 25.11u 0.03s 25.03r 25536kB gst-launch-0.10 -q audiotestsrc num-buffers=10000 ! spectrum bands=1440 ! fakesink
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 /* TODO: We use gdk_cairo_create() and others, which are deprecated */
39 #define GDK_DISABLE_DEPRECATION_WARNINGS
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <math.h>
45 #include <gst/gst.h>
46 #include <gst/fft/gstfft.h>
47 #include <gtk/gtk.h>
48
49 #ifndef DEFAULT_AUDIOSINK
50 #define DEFAULT_AUDIOSINK "autoaudiosink"
51 #endif
52
53 static guint spect_height = 64;
54 static guint spect_bands = 256;
55 static gfloat height_scale = 1.0;
56 static gboolean fast = FALSE;
57
58 static GtkWidget *drawingarea = NULL;
59 static GtkWidget *bands_used = NULL;
60 static GstClock *sync_clock = NULL;
61
62 static void
on_window_destroy(GObject * object,gpointer user_data)63 on_window_destroy (GObject * object, gpointer user_data)
64 {
65 drawingarea = NULL;
66 gtk_main_quit ();
67 }
68
69 /* control audiotestsrc frequency */
70 static void
on_frequency_changed(GtkRange * range,gpointer user_data)71 on_frequency_changed (GtkRange * range, gpointer user_data)
72 {
73 GstElement *machine = GST_ELEMENT (user_data);
74 gdouble value = gtk_range_get_value (range);
75
76 g_object_set (machine, "freq", value, NULL);
77 }
78
79 static void
update_spectrum_bands(GstElement * spectrum)80 update_spectrum_bands (GstElement * spectrum)
81 {
82 guint bands = spect_bands;
83 gchar str[50];
84
85 if (fast)
86 bands = ((gst_fft_next_fast_length (2 * bands - 2) + 2) / 2);
87
88 sprintf (str, "using %u bands", bands);
89
90 g_object_set (bands_used, "label", str, NULL);
91 g_object_set (G_OBJECT (spectrum), "bands", bands, NULL);
92 }
93
94 static gboolean
on_configure_event(GtkWidget * widget,GdkEventConfigure * event,gpointer user_data)95 on_configure_event (GtkWidget * widget, GdkEventConfigure * event,
96 gpointer user_data)
97 {
98 /*GST_INFO ("%d x %d", event->width, event->height); */
99 spect_height = event->height;
100 height_scale = event->height / 64.0;
101 spect_bands = event->width;
102
103 update_spectrum_bands (GST_ELEMENT (user_data));
104 return FALSE;
105 }
106
107 static void
on_fast_slow_mode_changed(GtkToggleButton * togglebutton,gpointer user_data)108 on_fast_slow_mode_changed (GtkToggleButton * togglebutton, gpointer user_data)
109 {
110 fast = gtk_toggle_button_get_active (togglebutton);
111 update_spectrum_bands (GST_ELEMENT (user_data));
112 }
113
114 /* draw frequency spectrum as a bunch of bars */
115 static void
draw_spectrum(gfloat * data)116 draw_spectrum (gfloat * data)
117 {
118 gint i;
119 GdkRectangle rect = { 0, 0, spect_bands, spect_height };
120 cairo_t *cr;
121
122 if (!drawingarea)
123 return;
124
125 gdk_window_begin_paint_rect (gtk_widget_get_window (drawingarea), &rect);
126 cr = gdk_cairo_create (gtk_widget_get_window (drawingarea));
127
128 cairo_set_source_rgb (cr, 0, 0, 0);
129 cairo_rectangle (cr, 0, 0, spect_bands, spect_height);
130 cairo_fill (cr);
131 cairo_set_source_rgb (cr, 1, 1, 1);
132 for (i = 0; i < spect_bands; i++) {
133 cairo_rectangle (cr, i, -data[i], 1, spect_height + data[i]);
134 cairo_fill (cr);
135 }
136 cairo_destroy (cr);
137
138 gdk_window_end_paint (gtk_widget_get_window (drawingarea));
139 }
140
141 /* process delayed message */
142 static gboolean
delayed_idle_spectrum_update(gpointer user_data)143 delayed_idle_spectrum_update (gpointer user_data)
144 {
145 draw_spectrum ((gfloat *) user_data);
146 g_free (user_data);
147 return (FALSE);
148 }
149
150 static gboolean
delayed_spectrum_update(GstClock * sync_clock,GstClockTime time,GstClockID id,gpointer user_data)151 delayed_spectrum_update (GstClock * sync_clock, GstClockTime time,
152 GstClockID id, gpointer user_data)
153 {
154 if (GST_CLOCK_TIME_IS_VALID (time))
155 g_idle_add (delayed_idle_spectrum_update, user_data);
156 else
157 g_free (user_data);
158 return (TRUE);
159 }
160
161 /* receive spectral data from element message */
162 static gboolean
message_handler(GstBus * bus,GstMessage * message,gpointer data)163 message_handler (GstBus * bus, GstMessage * message, gpointer data)
164 {
165 if (message->type == GST_MESSAGE_ELEMENT) {
166 const GstStructure *s = gst_message_get_structure (message);
167 const gchar *name = gst_structure_get_name (s);
168
169 if (strcmp (name, "spectrum") == 0) {
170 GstElement *spectrum = GST_ELEMENT (GST_MESSAGE_SRC (message));
171 GstClockTime timestamp, duration;
172 GstClockTime waittime = GST_CLOCK_TIME_NONE;
173
174 if (gst_structure_get_clock_time (s, "running-time", ×tamp) &&
175 gst_structure_get_clock_time (s, "duration", &duration)) {
176 /* wait for middle of buffer */
177 waittime = timestamp + duration / 2;
178 } else if (gst_structure_get_clock_time (s, "endtime", ×tamp)) {
179 waittime = timestamp;
180 }
181 if (GST_CLOCK_TIME_IS_VALID (waittime)) {
182 GstClockID clock_id;
183 GstClockTime basetime = gst_element_get_base_time (spectrum);
184 gfloat *spect = g_new (gfloat, spect_bands);
185 const GValue *list;
186 const GValue *value;
187 guint i;
188
189 list = gst_structure_get_value (s, "magnitude");
190 for (i = 0; i < spect_bands; ++i) {
191 value = gst_value_list_get_value (list, i);
192 spect[i] = height_scale * g_value_get_float (value);
193 }
194
195 clock_id =
196 gst_clock_new_single_shot_id (sync_clock, waittime + basetime);
197 gst_clock_id_wait_async (clock_id, delayed_spectrum_update,
198 (gpointer) spect, NULL);
199 gst_clock_id_unref (clock_id);
200 }
201 }
202 }
203 return TRUE;
204 }
205
206 int
main(int argc,char * argv[])207 main (int argc, char *argv[])
208 {
209 GstElement *bin;
210 GstElement *src, *spectrum, *audioconvert, *sink;
211 GstBus *bus;
212 GtkWidget *appwindow, *vbox, *hbox, *widget;
213
214 gst_init (&argc, &argv);
215 gtk_init (&argc, &argv);
216
217 bin = gst_pipeline_new ("bin");
218
219 src = gst_element_factory_make ("audiotestsrc", "src");
220 g_object_set (G_OBJECT (src), "wave", 0, NULL);
221
222 spectrum = gst_element_factory_make ("spectrum", "spectrum");
223 g_object_set (G_OBJECT (spectrum), "bands", spect_bands, "threshold", -80,
224 "post-messages", TRUE, NULL);
225
226 audioconvert = gst_element_factory_make ("audioconvert", "audioconvert");
227
228 sink = gst_element_factory_make (DEFAULT_AUDIOSINK, "sink");
229
230 gst_bin_add_many (GST_BIN (bin), src, spectrum, audioconvert, sink, NULL);
231 if (!gst_element_link_many (src, spectrum, audioconvert, sink, NULL)) {
232 fprintf (stderr, "can't link elements\n");
233 exit (1);
234 }
235
236 bus = gst_element_get_bus (bin);
237 gst_bus_add_watch (bus, message_handler, NULL);
238 gst_object_unref (bus);
239
240 sync_clock = gst_pipeline_get_clock (GST_PIPELINE (bin));
241
242 appwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
243 g_signal_connect (G_OBJECT (appwindow), "destroy",
244 G_CALLBACK (on_window_destroy), NULL);
245 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
246
247 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
248 widget = gtk_check_button_new_with_label ("Fast");
249 g_signal_connect (G_OBJECT (widget), "toggled",
250 G_CALLBACK (on_fast_slow_mode_changed), (gpointer) spectrum);
251 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
252 bands_used = gtk_label_new ("");
253 gtk_box_pack_start (GTK_BOX (hbox), bands_used, FALSE, FALSE, 0);
254
255 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
256
257 widget = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
258 50.0, 20000.0, 10);
259 gtk_scale_set_draw_value (GTK_SCALE (widget), TRUE);
260 gtk_scale_set_value_pos (GTK_SCALE (widget), GTK_POS_TOP);
261 gtk_range_set_value (GTK_RANGE (widget), 440.0);
262 g_signal_connect (G_OBJECT (widget), "value-changed",
263 G_CALLBACK (on_frequency_changed), (gpointer) src);
264 gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
265
266 drawingarea = gtk_drawing_area_new ();
267 gtk_widget_set_size_request (drawingarea, spect_bands, spect_height);
268 g_signal_connect (G_OBJECT (drawingarea), "configure-event",
269 G_CALLBACK (on_configure_event), (gpointer) spectrum);
270 gtk_box_pack_start (GTK_BOX (vbox), drawingarea, TRUE, TRUE, 0);
271
272 gtk_container_add (GTK_CONTAINER (appwindow), vbox);
273 gtk_widget_show_all (appwindow);
274
275 gst_element_set_state (bin, GST_STATE_PLAYING);
276 gtk_main ();
277 gst_element_set_state (bin, GST_STATE_NULL);
278
279 gst_object_unref (sync_clock);
280 gst_object_unref (bin);
281
282 return 0;
283 }
284