1 /* GStreamer
2 * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
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
20 /* TODO: We use gdk_cairo_create() and others, which are deprecated */
21 #define GDK_DISABLE_DEPRECATION_WARNINGS
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26 #include <gst/gst.h>
27 #include <gtk/gtk.h>
28
29 #define NBANDS 10
30
31 static GtkWidget *drawingarea = NULL;
32 static guint spect_height = 128;
33 static guint spect_bands = 256;
34 static gfloat height_scale = 2.0;
35
36 static void
on_window_destroy(GObject * object,gpointer user_data)37 on_window_destroy (GObject * object, gpointer user_data)
38 {
39 drawingarea = NULL;
40 gtk_main_quit ();
41 }
42
43 static gboolean
on_configure_event(GtkWidget * widget,GdkEventConfigure * event,gpointer user_data)44 on_configure_event (GtkWidget * widget, GdkEventConfigure * event,
45 gpointer user_data)
46 {
47 GstElement *spectrum = GST_ELEMENT (user_data);
48
49 /*GST_INFO ("%d x %d", event->width, event->height); */
50 spect_height = event->height;
51 height_scale = event->height / 64.0;
52 spect_bands = event->width;
53
54 g_object_set (G_OBJECT (spectrum), "bands", spect_bands, NULL);
55 return FALSE;
56 }
57
58 /* control gains */
59 static void
on_gain_changed(GtkRange * range,gpointer user_data)60 on_gain_changed (GtkRange * range, gpointer user_data)
61 {
62 GstObject *band = GST_OBJECT (user_data);
63 gdouble value = gtk_range_get_value (range);
64
65 g_object_set (band, "gain", value, NULL);
66 }
67
68 /* control bandwidths */
69 static void
on_bandwidth_changed(GtkRange * range,gpointer user_data)70 on_bandwidth_changed (GtkRange * range, gpointer user_data)
71 {
72 GstObject *band = GST_OBJECT (user_data);
73 gdouble value = gtk_range_get_value (range);
74
75 g_object_set (band, "bandwidth", value, NULL);
76 }
77
78 /* control frequency */
79 static void
on_freq_changed(GtkRange * range,gpointer user_data)80 on_freq_changed (GtkRange * range, gpointer user_data)
81 {
82 GstObject *band = GST_OBJECT (user_data);
83 gdouble value = gtk_range_get_value (range);
84
85 /* hbox */
86 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (range));
87
88 /* frame */
89 GtkWidget *parent_parent = gtk_widget_get_parent (parent);
90 gchar *label = g_strdup_printf ("%d Hz", (int) (value + 0.5));
91
92 gtk_frame_set_label (GTK_FRAME (parent_parent), label);
93 g_free (label);
94
95 g_object_set (band, "freq", value, NULL);
96 }
97
98 /* draw frequency spectrum as a bunch of bars */
99 static void
draw_spectrum(gfloat * data)100 draw_spectrum (gfloat * data)
101 {
102 gint i;
103 GdkRectangle rect = { 0, 0, spect_bands, spect_height };
104 cairo_t *cr;
105
106 if (!drawingarea)
107 return;
108
109 gdk_window_begin_paint_rect (gtk_widget_get_window (drawingarea), &rect);
110 cr = gdk_cairo_create (gtk_widget_get_window (drawingarea));
111
112 cairo_set_source_rgb (cr, 0, 0, 0);
113 cairo_rectangle (cr, 0, 0, spect_bands, spect_height);
114 cairo_fill (cr);
115 for (i = 0; i < spect_bands; i++) {
116 cairo_set_source_rgb (cr, 1, 1, 1);
117 cairo_rectangle (cr, i, -data[i], 1, spect_height + data[i]);
118 cairo_fill (cr);
119 }
120 cairo_destroy (cr);
121
122 gdk_window_end_paint (gtk_widget_get_window (drawingarea));
123 }
124
125 static void
dynamic_link(GstPadTemplate * templ,GstPad * newpad,gpointer user_data)126 dynamic_link (GstPadTemplate * templ, GstPad * newpad, gpointer user_data)
127 {
128 GstPad *target = GST_PAD (user_data);
129
130 gst_pad_link (newpad, target);
131 gst_object_unref (target);
132 }
133
134 /* receive spectral data from element message */
135 static gboolean
message_handler(GstBus * bus,GstMessage * message,gpointer data)136 message_handler (GstBus * bus, GstMessage * message, gpointer data)
137 {
138 if (message->type == GST_MESSAGE_ELEMENT) {
139 const GstStructure *s = gst_message_get_structure (message);
140 const gchar *name = gst_structure_get_name (s);
141
142 if (strcmp (name, "spectrum") == 0) {
143 gfloat *spect = g_new (gfloat, spect_bands);
144 const GValue *list;
145 const GValue *value;
146 guint i;
147
148 list = gst_structure_get_value (s, "magnitude");
149 for (i = 0; i < spect_bands; ++i) {
150 value = gst_value_list_get_value (list, i);
151 spect[i] = height_scale * g_value_get_float (value);
152 }
153 draw_spectrum (spect);
154 g_free (spect);
155 }
156 }
157 return TRUE;
158 }
159
160 int
main(int argc,char * argv[])161 main (int argc, char *argv[])
162 {
163 GstElement *bin;
164 GstElement *decodebin, *decconvert;
165 GstElement *capsfilter, *equalizer, *spectrum, *sinkconvert, *sink;
166 GstCaps *caps;
167 GstBus *bus;
168 GtkWidget *appwindow, *vbox, *hbox, *scale;
169 int i, num_bands = NBANDS;
170
171 GOptionEntry options[] = {
172 {"bands", 'b', 0, G_OPTION_ARG_INT, &num_bands,
173 "Number of bands", NULL},
174 {NULL}
175 };
176 GOptionContext *ctx;
177 GError *err = NULL;
178
179 ctx = g_option_context_new ("- demo of audio equalizer");
180 g_option_context_add_main_entries (ctx, options, NULL);
181 g_option_context_add_group (ctx, gst_init_get_option_group ());
182 g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
183
184 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
185 g_print ("Error initializing: %s\n", err->message);
186 g_option_context_free (ctx);
187 g_clear_error (&err);
188 exit (1);
189 }
190 g_option_context_free (ctx);
191
192 if (argc < 2) {
193 g_print ("Usage: %s <uri to play>\n", argv[0]);
194 g_print (" For optional arguments: --help\n");
195 exit (-1);
196 }
197
198 gst_init (&argc, &argv);
199 gtk_init (&argc, &argv);
200
201 bin = gst_pipeline_new ("bin");
202
203 /* Uri decoding */
204 decodebin = gst_element_factory_make ("uridecodebin", "decoder");
205 g_object_set (G_OBJECT (decodebin), "uri", argv[1], NULL);
206
207 /* Force float32 samples */
208 decconvert = gst_element_factory_make ("audioconvert", "decconvert");
209 capsfilter = gst_element_factory_make ("capsfilter", "capsfilter");
210 caps =
211 gst_caps_new_simple ("audio/x-raw", "format", G_TYPE_STRING, "F32LE",
212 NULL);
213 g_object_set (capsfilter, "caps", caps, NULL);
214
215 equalizer = gst_element_factory_make ("equalizer-nbands", "equalizer");
216 g_object_set (G_OBJECT (equalizer), "num-bands", num_bands, NULL);
217
218 spectrum = gst_element_factory_make ("spectrum", "spectrum");
219 g_object_set (G_OBJECT (spectrum), "bands", spect_bands, "threshold", -80,
220 "post-messages", TRUE, "interval", 500 * GST_MSECOND, NULL);
221
222 sinkconvert = gst_element_factory_make ("audioconvert", "sinkconvert");
223
224 sink = gst_element_factory_make ("autoaudiosink", "sink");
225
226 gst_bin_add_many (GST_BIN (bin), decodebin, decconvert, capsfilter, equalizer,
227 spectrum, sinkconvert, sink, NULL);
228 if (!gst_element_link_many (decconvert, capsfilter, equalizer, spectrum,
229 sinkconvert, sink, NULL)) {
230 fprintf (stderr, "can't link elements\n");
231 exit (1);
232 }
233
234 /* Handle dynamic pads */
235 g_signal_connect (G_OBJECT (decodebin), "pad-added",
236 G_CALLBACK (dynamic_link), gst_element_get_static_pad (decconvert,
237 "sink"));
238
239 bus = gst_element_get_bus (bin);
240 gst_bus_add_watch (bus, message_handler, NULL);
241 gst_object_unref (bus);
242
243 appwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
244 gtk_window_set_title (GTK_WINDOW (appwindow), "Equalizer Demo");
245 g_signal_connect (G_OBJECT (appwindow), "destroy",
246 G_CALLBACK (on_window_destroy), NULL);
247 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
248
249 drawingarea = gtk_drawing_area_new ();
250 gtk_widget_set_size_request (drawingarea, spect_bands, spect_height);
251 g_signal_connect (G_OBJECT (drawingarea), "configure-event",
252 G_CALLBACK (on_configure_event), (gpointer) spectrum);
253 gtk_box_pack_start (GTK_BOX (vbox), drawingarea, TRUE, TRUE, 0);
254
255 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 20);
256
257 for (i = 0; i < num_bands; i++) {
258 GObject *band;
259 gdouble freq;
260 gdouble bw;
261 gdouble gain;
262 gchar *label;
263 GtkWidget *frame, *scales_hbox;
264
265 band = gst_child_proxy_get_child_by_index (GST_CHILD_PROXY (equalizer), i);
266 g_assert (band != NULL);
267 g_object_get (band, "freq", &freq, NULL);
268 g_object_get (band, "bandwidth", &bw, NULL);
269 g_object_get (band, "gain", &gain, NULL);
270
271 label = g_strdup_printf ("%d Hz", (int) (freq + 0.5));
272 frame = gtk_frame_new (label);
273 g_free (label);
274
275 scales_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
276
277 /* Create gain scale */
278 scale = gtk_scale_new_with_range (GTK_ORIENTATION_VERTICAL,
279 -24.0, 12.0, 0.5);
280 gtk_scale_set_draw_value (GTK_SCALE (scale), TRUE);
281 gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
282 gtk_range_set_value (GTK_RANGE (scale), gain);
283 gtk_widget_set_size_request (scale, 35, 150);
284 g_signal_connect (G_OBJECT (scale), "value-changed",
285 G_CALLBACK (on_gain_changed), (gpointer) band);
286 gtk_box_pack_start (GTK_BOX (scales_hbox), scale, FALSE, FALSE, 0);
287
288 /* Create bandwidth scale */
289 scale = gtk_scale_new_with_range (GTK_ORIENTATION_VERTICAL,
290 0.0, 20000.0, 5.0);
291 gtk_scale_set_draw_value (GTK_SCALE (scale), TRUE);
292 gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
293 gtk_range_set_value (GTK_RANGE (scale), bw);
294 gtk_widget_set_size_request (scale, 45, 150);
295 g_signal_connect (G_OBJECT (scale), "value-changed",
296 G_CALLBACK (on_bandwidth_changed), (gpointer) band);
297 gtk_box_pack_start (GTK_BOX (scales_hbox), scale, TRUE, TRUE, 0);
298
299 /* Create frequency scale */
300 scale = gtk_scale_new_with_range (GTK_ORIENTATION_VERTICAL,
301 20.0, 20000.0, 5.0);
302 gtk_scale_set_draw_value (GTK_SCALE (scale), TRUE);
303 gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
304 gtk_range_set_value (GTK_RANGE (scale), freq);
305 gtk_widget_set_size_request (scale, 45, 150);
306 g_signal_connect (G_OBJECT (scale), "value-changed",
307 G_CALLBACK (on_freq_changed), (gpointer) band);
308 gtk_box_pack_start (GTK_BOX (scales_hbox), scale, TRUE, TRUE, 0);
309
310 gtk_container_add (GTK_CONTAINER (frame), scales_hbox);
311
312 gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
313 }
314
315 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
316
317 gtk_container_add (GTK_CONTAINER (appwindow), vbox);
318 gtk_widget_show_all (appwindow);
319
320 gst_element_set_state (bin, GST_STATE_PLAYING);
321 gtk_main ();
322 gst_element_set_state (bin, GST_STATE_NULL);
323
324 gst_object_unref (bin);
325
326 return 0;
327 }
328