* src/gstpitch.c:
[tunertool] / src / demo-audiotest.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_AUDIOSINK "alsasink"
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 /* control audiotestsrc frequency */
40 static void
41 on_frequency_changed (GtkRange * range, gpointer user_data)
42 {
43   GstElement *machine = GST_ELEMENT (user_data);
44   gdouble value = gtk_range_get_value (range);
45
46   g_object_set (machine, "freq", value, NULL);
47 }
48
49 /* control audiotestsrc frequency */
50 static void
51 update_frequency (gint frequency)
52 {
53   gchar *buffer;
54   gchar freq[5];
55
56   g_snprintf (freq, 5, "%d", frequency);
57   buffer = g_strconcat ("Frequency is ", freq, NULL);
58   gtk_label_set_text (GTK_LABEL (lblFrequency), buffer);
59   g_free (buffer);
60 }
61
62 /* receive spectral data from element message */
63 gboolean
64 message_handler (GstBus * bus, GstMessage * message, gpointer data)
65 {
66   if (message->type == GST_MESSAGE_ELEMENT) {
67     const GstStructure *s = gst_message_get_structure (message);
68     const gchar *name = gst_structure_get_name (s);
69
70     if (strcmp (name, "kissfft") == 0) {
71       gint frequency;
72
73       frequency = g_value_get_int (gst_structure_get_value (s, "frequency"));
74       update_frequency (frequency);
75     }
76   }
77   /* we handled the message we want, and ignored the ones we didn't want.
78    * so the core can unref the message for us */
79   return TRUE;
80 }
81
82 int
83 main (int argc, char *argv[])
84 {
85   GstElement *bin;
86   GstElement *src, *kissfft, *sink;
87   GstBus *bus;
88   GtkWidget *appwindow, *vbox, *widget;
89
90   gst_init (&argc, &argv);
91   gtk_init (&argc, &argv);
92
93   bin = gst_pipeline_new ("bin");
94
95   src = gst_element_factory_make ("audiotestsrc", "src");
96
97   kissfft = gst_element_factory_make ("kissfft", "kissfft");
98   g_object_set (G_OBJECT (kissfft), "nfft", 1024, "message", TRUE, "minfreq",
99       220, "maxfreq", 1500, NULL);
100
101   sink = gst_element_factory_make (DEFAULT_AUDIOSINK, "sink");
102
103   gst_bin_add_many (GST_BIN (bin), src, kissfft, sink, NULL);
104   if (!gst_element_link_many (src, kissfft, sink, NULL)) {
105     fprintf (stderr, "cant link elements\n");
106     exit (1);
107   }
108
109   bus = gst_element_get_bus (bin);
110   gst_bus_add_watch (bus, message_handler, NULL);
111   gst_object_unref (bus);
112
113   appwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
114   g_signal_connect (G_OBJECT (appwindow), "destroy",
115       G_CALLBACK (on_window_destroy), NULL);
116   vbox = gtk_vbox_new (FALSE, 6);
117
118   widget = gtk_hscale_new_with_range (50.0, 2000.0, 10);
119   gtk_widget_set_size_request (widget, 300, -1);
120   gtk_scale_set_draw_value (GTK_SCALE (widget), TRUE);
121   gtk_scale_set_value_pos (GTK_SCALE (widget), GTK_POS_TOP);
122   gtk_range_set_value (GTK_RANGE (widget), 440.0);
123   g_signal_connect (G_OBJECT (widget), "value-changed",
124       G_CALLBACK (on_frequency_changed), (gpointer) src);
125   gtk_container_add (GTK_CONTAINER (vbox), widget);
126
127   lblFrequency = gtk_label_new ("Frequency is ");
128   gtk_container_add (GTK_CONTAINER (vbox), lblFrequency);
129
130   gtk_container_add (GTK_CONTAINER (appwindow), vbox);
131   gtk_widget_show_all (appwindow);
132
133   gst_element_set_state (bin, GST_STATE_PLAYING);
134   gtk_main ();
135   gst_element_set_state (bin, GST_STATE_NULL);
136
137   gst_object_unref (bin);
138
139   return 0;
140 }