set window title to "Tuner"
[tunertool] / src / demo-tuner.c
1 /* GStreamer
2  * Copyright (C) 2006 Josep Torra <j.torra@telefonica.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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <string.h>
25 #include <math.h>
26 #include <gst/gst.h>
27 #include <gtk/gtk.h>
28
29 #define DEFAULT_AUDIOSRC "alsasrc"
30
31 GtkWidget *lblFrequency;
32
33 static void
34 on_window_destroy (GtkObject * object, gpointer user_data)
35 {
36   gtk_main_quit ();
37 }
38
39 /* update frequency label */
40 static void
41 update_frequency (gint frequency)
42 {
43   gchar *buffer;
44   gchar freq[5];
45
46   g_snprintf (freq, 5, "%d", frequency);
47   buffer = g_strconcat ("Frequency is ", freq, NULL);
48   gtk_label_set_text (GTK_LABEL (lblFrequency), buffer);
49   g_free (buffer);
50 }
51
52 /* receive spectral data from element message */
53 gboolean
54 message_handler (GstBus * bus, GstMessage * message, gpointer data)
55 {
56   if (message->type == GST_MESSAGE_ELEMENT) {
57     const GstStructure *s = gst_message_get_structure (message);
58     const gchar *name = gst_structure_get_name (s);
59
60     if (strcmp (name, "kissfft") == 0) {
61       gint frequency;
62
63       frequency = g_value_get_int (gst_structure_get_value (s, "frequency"));
64       update_frequency (frequency);
65     }
66   }
67   /* we handled the message we want, and ignored the ones we didn't want.
68    * so the core can unref the message for us */
69   return TRUE;
70 }
71
72 int
73 main (int argc, char *argv[])
74 {
75   GstElement *bin;
76   GstElement *src, *kissfft, *sink;
77   GstBus *bus;
78   GtkWidget *appwindow, *vbox;
79
80   gst_init (&argc, &argv);
81   gtk_init (&argc, &argv);
82
83   bin = gst_pipeline_new ("bin");
84
85   src = gst_element_factory_make (DEFAULT_AUDIOSRC, "src");
86
87   kissfft = gst_element_factory_make ("kissfft", "kissfft");
88   g_object_set (G_OBJECT (kissfft), "nfft", 16000, "message", TRUE, "minfreq",
89       220, "maxfreq", 1500, NULL);
90
91   sink = gst_element_factory_make ("fakesink", "sink");
92   g_object_set (G_OBJECT (sink), "silent", 1, NULL);
93
94   gst_bin_add_many (GST_BIN (bin), src, kissfft, sink, NULL);
95   if (!gst_element_link_many (src, kissfft, sink, NULL)) {
96     fprintf (stderr, "cant link elements\n");
97     exit (1);
98   }
99
100   bus = gst_element_get_bus (bin);
101   gst_bus_add_watch (bus, message_handler, NULL);
102   gst_object_unref (bus);
103
104   appwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
105   g_signal_connect (G_OBJECT (appwindow), "destroy",
106       G_CALLBACK (on_window_destroy), NULL);
107   vbox = gtk_vbox_new (FALSE, 6);
108
109   lblFrequency = gtk_label_new ("Frequency is ");
110   gtk_widget_set_size_request (lblFrequency, 300, -1);
111   gtk_container_add (GTK_CONTAINER (vbox), lblFrequency);
112
113   gtk_container_add (GTK_CONTAINER (appwindow), vbox);
114   gtk_widget_show_all (appwindow);
115
116   gst_element_set_state (bin, GST_STATE_PLAYING);
117   gtk_main ();
118   gst_element_set_state (bin, GST_STATE_NULL);
119
120   gst_object_unref (bin);
121
122   return 0;
123 }