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 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 /* TODO: We use gdk_cairo_create() and others, which are deprecated */
25 #define GDK_DISABLE_DEPRECATION_WARNINGS
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <math.h>
31 #include <gst/gst.h>
32 #include <gtk/gtk.h>
33 
34 #ifndef DEFAULT_AUDIOSRC
35 #define DEFAULT_AUDIOSRC "alsasrc"
36 #endif
37 
38 static guint spect_height = 64;
39 static guint spect_bands = 256;
40 static gfloat height_scale = 1.0;
41 
42 static GtkWidget *drawingarea = NULL;
43 static GstClock *sync_clock = NULL;
44 
45 static void
on_window_destroy(GObject * object,gpointer user_data)46 on_window_destroy (GObject * object, gpointer user_data)
47 {
48   drawingarea = NULL;
49   gtk_main_quit ();
50 }
51 
52 static gboolean
on_configure_event(GtkWidget * widget,GdkEventConfigure * event,gpointer user_data)53 on_configure_event (GtkWidget * widget, GdkEventConfigure * event,
54     gpointer user_data)
55 {
56   GstElement *spectrum = GST_ELEMENT (user_data);
57 
58   /*GST_INFO ("%d x %d", event->width, event->height); */
59   spect_height = event->height;
60   height_scale = event->height / 64.0;
61   spect_bands = event->width;
62 
63   g_object_set (G_OBJECT (spectrum), "bands", spect_bands, NULL);
64   return FALSE;
65 }
66 
67 /* draw frequency spectrum as a bunch of bars */
68 static void
draw_spectrum(gfloat * data)69 draw_spectrum (gfloat * data)
70 {
71   gint i;
72   GdkRectangle rect = { 0, 0, spect_bands, spect_height };
73   cairo_t *cr;
74 
75   if (!drawingarea)
76     return;
77 
78   gdk_window_begin_paint_rect (gtk_widget_get_window (drawingarea), &rect);
79   cr = gdk_cairo_create (gtk_widget_get_window (drawingarea));
80 
81   cairo_set_source_rgb (cr, 0, 0, 0);
82   cairo_rectangle (cr, 0, 0, spect_bands, spect_height);
83   cairo_fill (cr);
84   cairo_set_source_rgb (cr, 1, 1, 1);
85   for (i = 0; i < spect_bands; i++) {
86     cairo_rectangle (cr, i, -data[i], 1, spect_height + data[i]);
87     cairo_fill (cr);
88   }
89   cairo_destroy (cr);
90 
91   gdk_window_end_paint (gtk_widget_get_window (drawingarea));
92 }
93 
94 /* process delayed message */
95 static gboolean
delayed_idle_spectrum_update(gpointer user_data)96 delayed_idle_spectrum_update (gpointer user_data)
97 {
98   draw_spectrum ((gfloat *) user_data);
99   g_free (user_data);
100   return (FALSE);
101 }
102 
103 static gboolean
delayed_spectrum_update(GstClock * sync_clock,GstClockTime time,GstClockID id,gpointer user_data)104 delayed_spectrum_update (GstClock * sync_clock, GstClockTime time,
105     GstClockID id, gpointer user_data)
106 {
107   if (GST_CLOCK_TIME_IS_VALID (time))
108     g_idle_add (delayed_idle_spectrum_update, user_data);
109   else
110     g_free (user_data);
111   return (TRUE);
112 }
113 
114 /* receive spectral data from element message */
115 static gboolean
message_handler(GstBus * bus,GstMessage * message,gpointer data)116 message_handler (GstBus * bus, GstMessage * message, gpointer data)
117 {
118   if (message->type == GST_MESSAGE_ELEMENT) {
119     const GstStructure *s = gst_message_get_structure (message);
120     const gchar *name = gst_structure_get_name (s);
121 
122     if (strcmp (name, "spectrum") == 0) {
123       GstElement *spectrum = GST_ELEMENT (GST_MESSAGE_SRC (message));
124       GstClockTime timestamp, duration;
125       GstClockTime waittime = GST_CLOCK_TIME_NONE;
126 
127       if (gst_structure_get_clock_time (s, "running-time", &timestamp) &&
128           gst_structure_get_clock_time (s, "duration", &duration)) {
129         /* wait for middle of buffer */
130         waittime = timestamp + duration / 2;
131       } else if (gst_structure_get_clock_time (s, "endtime", &timestamp)) {
132         waittime = timestamp;
133       }
134       if (GST_CLOCK_TIME_IS_VALID (waittime)) {
135         GstClockID clock_id;
136         GstClockTime basetime = gst_element_get_base_time (spectrum);
137         gfloat *spect = g_new (gfloat, spect_bands);
138         const GValue *list;
139         const GValue *value;
140         guint i;
141 
142         list = gst_structure_get_value (s, "magnitude");
143         for (i = 0; i < spect_bands; ++i) {
144           value = gst_value_list_get_value (list, i);
145           spect[i] = height_scale * g_value_get_float (value);
146         }
147 
148         clock_id =
149             gst_clock_new_single_shot_id (sync_clock, waittime + basetime);
150         gst_clock_id_wait_async (clock_id, delayed_spectrum_update,
151             (gpointer) spect, NULL);
152         gst_clock_id_unref (clock_id);
153       }
154     }
155   }
156   return TRUE;
157 }
158 
159 int
main(int argc,char * argv[])160 main (int argc, char *argv[])
161 {
162   GstElement *bin;
163   GstElement *src, *spectrum, *sink;
164   GstBus *bus;
165   GtkWidget *appwindow;
166 
167   gst_init (&argc, &argv);
168   gtk_init (&argc, &argv);
169 
170   bin = gst_pipeline_new ("bin");
171 
172   src = gst_element_factory_make (DEFAULT_AUDIOSRC, "src");
173 
174   spectrum = gst_element_factory_make ("spectrum", "spectrum");
175   g_object_set (G_OBJECT (spectrum), "bands", spect_bands, "threshold", -80,
176       "post-messages", TRUE, NULL);
177 
178   sink = gst_element_factory_make ("fakesink", "sink");
179 
180   gst_bin_add_many (GST_BIN (bin), src, spectrum, sink, NULL);
181   if (!gst_element_link_many (src, spectrum, sink, NULL)) {
182     fprintf (stderr, "can't link elements\n");
183     exit (1);
184   }
185 
186   bus = gst_element_get_bus (bin);
187   gst_bus_add_watch (bus, message_handler, NULL);
188   gst_object_unref (bus);
189 
190   sync_clock = gst_pipeline_get_clock (GST_PIPELINE (bin));
191 
192   appwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
193   g_signal_connect (G_OBJECT (appwindow), "destroy",
194       G_CALLBACK (on_window_destroy), NULL);
195 
196   drawingarea = gtk_drawing_area_new ();
197   gtk_widget_set_size_request (drawingarea, spect_bands, spect_height);
198   g_signal_connect (G_OBJECT (drawingarea), "configure-event",
199       G_CALLBACK (on_configure_event), (gpointer) spectrum);
200   gtk_container_add (GTK_CONTAINER (appwindow), drawingarea);
201   gtk_widget_show_all (appwindow);
202 
203   gst_element_set_state (bin, GST_STATE_PLAYING);
204   gtk_main ();
205   gst_element_set_state (bin, GST_STATE_NULL);
206 
207   gst_object_unref (sync_clock);
208   gst_object_unref (bin);
209 
210   return 0;
211 }
212