• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * audiomix.c: sample audio mixing application
4  *
5  * Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <string.h>
28 #include <gst/gst.h>
29 #include <gtk/gtk.h>
30 
31 /* global items for the interaction */
32 static GtkWidget *scale;
33 static GObject *volumes[2];
34 static gint num_vol = 0;
35 
36 
37 static void
value_changed_callback(GtkWidget * widget,gpointer * user_data)38 value_changed_callback (GtkWidget * widget, gpointer * user_data)
39 {
40   gdouble value = gtk_range_get_value (GTK_RANGE (widget));
41   g_object_set (volumes[0], "volume", 1.0 - value, NULL);
42   g_object_set (volumes[1], "volume", value, NULL);
43 }
44 
45 static void
setup_gui(GstElement * volume,gchar * file_name1,gchar * file_name2)46 setup_gui (GstElement * volume, gchar * file_name1, gchar * file_name2)
47 {
48   GtkWidget *window, *layout, *label;
49   gchar *name, *ext;
50 
51   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
52   gtk_window_set_title (GTK_WINDOW (window), "audiomix");
53   g_signal_connect (window, "destroy", gtk_main_quit, NULL);
54 
55   layout = gtk_grid_new ();
56   g_object_set (G_OBJECT (layout), "column-spacing", 6, NULL);
57   gtk_container_add (GTK_CONTAINER (window), layout);
58 
59   /* channel labels */
60   name = g_path_get_basename (file_name1);
61   if ((ext = strrchr (name, '.')))
62     *ext = '\0';
63   label = gtk_label_new (name);
64   g_free (name);
65   gtk_grid_attach (GTK_GRID (layout), label, 0, 0, 1, 1);
66 
67   gtk_grid_attach (GTK_GRID (layout), gtk_label_new ("|"), 1, 0, 1, 1);
68 
69   name = g_path_get_basename (file_name2);
70   if ((ext = strrchr (name, '.')))
71     *ext = '\0';
72   label = gtk_label_new (name);
73   g_free (name);
74   gtk_grid_attach (GTK_GRID (layout), label, 2, 0, 1, 1);
75 
76   /* mix slider */
77   scale =
78       gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0.0, 1.0,
79       1.0 / 200.0);
80   gtk_range_set_value (GTK_RANGE (scale), 0.0);
81   gtk_widget_set_size_request (scale, 200, -1);
82   gtk_widget_set_hexpand (scale, TRUE);
83   gtk_grid_attach (GTK_GRID (layout), scale, 0, 1, 3, 1);
84   g_signal_connect (scale, "value-changed",
85       G_CALLBACK (value_changed_callback), volume);
86 
87   gtk_widget_show_all (GTK_WIDGET (window));
88 }
89 
90 static void
message_received(GstBus * bus,GstMessage * message,GstPipeline * pipeline)91 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
92 {
93   const GstStructure *s;
94 
95   s = gst_message_get_structure (message);
96   g_print ("message from \"%s\" (%s): ",
97       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
98       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
99   if (s) {
100     gchar *sstr;
101 
102     sstr = gst_structure_to_string (s);
103     g_print ("%s\n", sstr);
104     g_free (sstr);
105   } else {
106     g_print ("no message details\n");
107   }
108 }
109 
110 static void
eos_message_received(GstBus * bus,GstMessage * message,GstPipeline * pipeline)111 eos_message_received (GstBus * bus, GstMessage * message,
112     GstPipeline * pipeline)
113 {
114   message_received (bus, message, pipeline);
115   gtk_main_quit ();
116 }
117 
118 static void
dynamic_link(GstPadTemplate * templ,GstPad * newpad,gpointer user_data)119 dynamic_link (GstPadTemplate * templ, GstPad * newpad, gpointer user_data)
120 {
121   GstPad *target = GST_PAD (user_data);
122 
123   gst_pad_link (newpad, target);
124   gst_object_unref (target);
125 }
126 
127 static void
make_mixer_channel(GstElement * pipeline,GstElement * mix,gchar * file_name)128 make_mixer_channel (GstElement * pipeline, GstElement * mix, gchar * file_name)
129 {
130   GstElement *filesrc, *decodebin, *volume, *convert, *format;
131   GstCaps *caps;
132 
133   /* prepare mixer channel */
134   filesrc = gst_element_factory_make ("filesrc", NULL);
135   decodebin = gst_element_factory_make ("decodebin", NULL);
136   volume = gst_element_factory_make ("volume", NULL);
137   convert = gst_element_factory_make ("audioconvert", NULL);
138   format = gst_element_factory_make ("capsfilter", NULL);
139   gst_bin_add_many (GST_BIN (pipeline), filesrc, decodebin, volume, convert,
140       format, NULL);
141   gst_element_link (filesrc, decodebin);
142   gst_element_link_many (volume, convert, format, mix, NULL);
143 
144   /* configure elements */
145   g_object_set (filesrc, "location", file_name, NULL);
146   g_object_set (volume, "volume", (num_vol == 0) ? 1.0 : 0.0, NULL);
147 
148   caps = gst_caps_from_string ("audio/x-raw, "
149       "format = (string) S16LE, " "channels = (int) 2");
150   g_object_set (format, "caps", caps, NULL);
151   gst_caps_unref (caps);
152 
153   /* remember volume element */
154   volumes[num_vol++] = (GObject *) volume;
155 
156   /* handle dynamic pads */
157   g_signal_connect (G_OBJECT (decodebin), "pad-added",
158       G_CALLBACK (dynamic_link), gst_element_get_static_pad (volume, "sink"));
159 }
160 
161 int
main(int argc,char * argv[])162 main (int argc, char *argv[])
163 {
164   GstElement *pipeline = NULL;
165   GstElement *mix, *convert, *sink;
166   GstBus *bus;
167 
168   if (argc < 3) {
169     g_print ("Usage: audiomix <file1> <file2>\n");
170     return 1;
171   }
172 
173   gst_init (&argc, &argv);
174   gtk_init (&argc, &argv);
175 
176   /* prepare tail of pipeline */
177   pipeline = gst_pipeline_new ("audiomix");
178   mix = gst_element_factory_make ("adder", NULL);
179   convert = gst_element_factory_make ("audioconvert", NULL);
180   sink = gst_element_factory_make ("autoaudiosink", NULL);
181   gst_bin_add_many (GST_BIN (pipeline), mix, convert, sink, NULL);
182   gst_element_link_many (mix, convert, sink, NULL);
183 
184   /* prepare mixer channel strips */
185   make_mixer_channel (pipeline, mix, argv[1]);
186   make_mixer_channel (pipeline, mix, argv[2]);
187 
188   /* setup message handling */
189   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
190   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
191   g_signal_connect (bus, "message::error", (GCallback) message_received,
192       pipeline);
193   g_signal_connect (bus, "message::warning", (GCallback) message_received,
194       pipeline);
195   g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
196       pipeline);
197 
198   /* setup GUI */
199   setup_gui (pipeline, argv[1], argv[2]);
200 
201   /* go to main loop */
202   gst_element_set_state (pipeline, GST_STATE_PLAYING);
203   gtk_main ();
204   gst_element_set_state (pipeline, GST_STATE_NULL);
205   gst_object_unref (pipeline);
206   gst_object_unref (bus);
207 
208   return 0;
209 }
210