a5e13c418ece6a9ded088a993cb13b18beb98207
[platform/upstream/gstreamer.git] / tests / examples / audio / volume.c
1 /* GStreamer
2  *
3  * volume.c: sample application to change the volume of a pipeline
4  *
5  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
27  * with newer GTK versions (>= 3.3.0) */
28 #define GDK_DISABLE_DEPRECATION_WARNINGS
29
30 #include <math.h>
31
32 #include <gst/gst.h>
33 #include <gtk/gtk.h>
34
35 /* global pointer for the scale widget */
36 static GtkWidget *elapsed;
37 static GtkWidget *scale;
38
39 #ifndef M_LN10
40 #define M_LN10 (log(10.0))
41 #endif
42
43 static void
44 value_changed_callback (GtkWidget * widget, GstElement * volume)
45 {
46   gdouble value;
47   gdouble level;
48
49   value = gtk_range_get_value (GTK_RANGE (widget));
50   level = exp (value / 20.0 * M_LN10);
51   g_print ("Value: %f dB, level: %f\n", value, level);
52   g_object_set (volume, "volume", level, NULL);
53 }
54
55 static void
56 setup_gui (GstElement * volume)
57 {
58   GtkWidget *window;
59   GtkWidget *vbox;
60   GtkWidget *label, *hbox;
61
62   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
63   g_signal_connect (window, "destroy", gtk_main_quit, NULL);
64
65   vbox = gtk_vbox_new (TRUE, 0);
66   gtk_container_add (GTK_CONTAINER (window), vbox);
67
68   /* elapsed widget */
69   hbox = gtk_hbox_new (TRUE, 0);
70   label = gtk_label_new ("Elapsed");
71   elapsed = gtk_label_new ("0.000");
72   gtk_container_add (GTK_CONTAINER (hbox), label);
73   gtk_container_add (GTK_CONTAINER (hbox), elapsed);
74   gtk_container_add (GTK_CONTAINER (vbox), hbox);
75
76   /* volume */
77   hbox = gtk_hbox_new (TRUE, 0);
78   label = gtk_label_new ("volume");
79   gtk_container_add (GTK_CONTAINER (hbox), label);
80   scale = gtk_hscale_new_with_range (-90.0, 10.0, 0.2);
81   gtk_range_set_value (GTK_RANGE (scale), 0.0);
82   gtk_widget_set_size_request (scale, 100, -1);
83   gtk_container_add (GTK_CONTAINER (hbox), scale);
84   gtk_container_add (GTK_CONTAINER (vbox), hbox);
85   g_signal_connect (scale, "value-changed",
86       G_CALLBACK (value_changed_callback), volume);
87
88   gtk_widget_show_all (GTK_WIDGET (window));
89 }
90
91 static void
92 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
93 {
94   const GstStructure *s;
95
96   s = gst_message_get_structure (message);
97   g_print ("message from \"%s\" (%s): ",
98       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
99       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
100   if (s) {
101     gchar *sstr;
102
103     sstr = gst_structure_to_string (s);
104     g_print ("%s\n", sstr);
105     g_free (sstr);
106   } else {
107     g_print ("no message details\n");
108   }
109 }
110
111 static void
112 eos_message_received (GstBus * bus, GstMessage * message,
113     GstPipeline * pipeline)
114 {
115   message_received (bus, message, pipeline);
116   gtk_main_quit ();
117 }
118
119 int
120 main (int argc, char *argv[])
121 {
122
123   GstElement *pipeline = NULL;
124
125 #ifndef GST_DISABLE_PARSE
126   GError *error = NULL;
127 #endif
128   GstElement *volume;
129   GstBus *bus;
130
131 #ifdef GST_DISABLE_PARSE
132   g_print ("GStreamer was built without pipeline parsing capabilities.\n");
133   g_print
134       ("Please rebuild GStreamer with pipeline parsing capabilities activated to use this example.\n");
135   return 1;
136 #else
137   gst_init (&argc, &argv);
138   gtk_init (&argc, &argv);
139
140   pipeline = gst_parse_launchv ((const gchar **) &argv[1], &error);
141   if (error) {
142     g_print ("pipeline could not be constructed: %s\n", error->message);
143     g_print ("Please give a complete pipeline  with a 'volume' element.\n");
144     g_print ("Example: audiotestsrc ! volume ! %s\n", DEFAULT_AUDIOSINK);
145     g_error_free (error);
146     return 1;
147   }
148 #endif
149   volume = gst_bin_get_by_name (GST_BIN (pipeline), "volume0");
150   if (volume == NULL) {
151     g_print ("Please give a pipeline with a 'volume' element in it\n");
152     return 1;
153   }
154
155   /* setup message handling */
156   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
157   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
158   g_signal_connect (bus, "message::error", (GCallback) message_received,
159       pipeline);
160   g_signal_connect (bus, "message::warning", (GCallback) message_received,
161       pipeline);
162   g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
163       pipeline);
164
165   /* setup GUI */
166   setup_gui (volume);
167
168   /* go to main loop */
169   gst_element_set_state (pipeline, GST_STATE_PLAYING);
170   gtk_main ();
171   gst_element_set_state (pipeline, GST_STATE_NULL);
172   gst_object_unref (pipeline);
173
174   return 0;
175 }