3 * audiomix.c: sample audio mixing application
5 * Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
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.
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.
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.
31 /* global items for the interaction */
32 static GtkWidget *scale;
33 static GObject *volumes[2];
34 static gint num_vol = 0;
38 value_changed_callback (GtkWidget * widget, gpointer * user_data)
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);
46 setup_gui (GstElement * volume, gchar * file_name1, gchar * file_name2)
48 GtkWidget *window, *layout, *label;
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);
55 layout = gtk_table_new (2, 3, FALSE);
56 gtk_table_set_col_spacings (GTK_TABLE (layout), 6);
57 gtk_container_add (GTK_CONTAINER (window), layout);
60 name = g_path_get_basename (file_name1);
61 if ((ext = strrchr (name, '.')))
63 label = gtk_label_new (name);
65 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
66 gtk_table_attach_defaults (GTK_TABLE (layout), label, 0, 1, 0, 1);
68 gtk_table_attach_defaults (GTK_TABLE (layout), gtk_label_new ("|"), 1, 2, 0,
71 name = g_path_get_basename (file_name2);
72 if ((ext = strrchr (name, '.')))
74 label = gtk_label_new (name);
76 gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
77 gtk_table_attach_defaults (GTK_TABLE (layout), label, 2, 3, 0, 1);
80 scale = gtk_hscale_new_with_range (0.0, 1.0, 1.0 / 200.0);
81 gtk_range_set_value (GTK_RANGE (scale), 0.0);
82 gtk_widget_set_size_request (scale, 200, -1);
83 gtk_table_attach_defaults (GTK_TABLE (layout), scale, 0, 3, 1, 2);
84 g_signal_connect (scale, "value-changed",
85 G_CALLBACK (value_changed_callback), volume);
87 gtk_widget_show_all (GTK_WIDGET (window));
91 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
93 const GstStructure *s;
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)));
102 sstr = gst_structure_to_string (s);
103 g_print ("%s\n", sstr);
106 g_print ("no message details\n");
111 eos_message_received (GstBus * bus, GstMessage * message,
112 GstPipeline * pipeline)
114 message_received (bus, message, pipeline);
119 dynamic_link (GstPadTemplate * templ, GstPad * newpad, gpointer user_data)
121 GstPad *target = GST_PAD (user_data);
123 gst_pad_link (newpad, target);
124 gst_object_unref (target);
128 make_mixer_channel (GstElement * pipeline, GstElement * mix, gchar * file_name)
130 GstElement *filesrc, *decodebin, *volume, *convert, *format;
133 /* prepare mixer channel */
134 filesrc = gst_element_factory_make ("filesrc", NULL);
135 decodebin = gst_element_factory_make ("decodebin2", 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,
141 gst_element_link (filesrc, decodebin);
142 gst_element_link_many (volume, convert, format, mix, NULL);
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);
148 caps = gst_caps_from_string ("audio/x-raw-int, "
149 "channels = (int) 2, "
150 "endianness = (int) BYTE_ORDER, "
151 "width = (int) 16, " "depth = (int) 16, " "signed = (boolean) true");
152 g_object_set (format, "caps", caps, NULL);
153 gst_caps_unref (caps);
155 /* remember volume element */
156 volumes[num_vol++] = (GObject *) volume;
158 /* handle dynamic pads */
159 g_signal_connect (G_OBJECT (decodebin), "pad-added",
160 G_CALLBACK (dynamic_link), gst_element_get_static_pad (volume, "sink"));
164 main (int argc, char *argv[])
166 GstElement *pipeline = NULL;
167 GstElement *mix, *convert, *sink;
171 g_print ("Usage: audiomix <file1> <file2>\n");
175 gst_init (&argc, &argv);
176 gtk_init (&argc, &argv);
178 /* prepare tail of pipeline */
179 pipeline = gst_pipeline_new ("audiomix");
180 mix = gst_element_factory_make ("adder", NULL);
181 convert = gst_element_factory_make ("audioconvert", NULL);
182 sink = gst_element_factory_make ("autoaudiosink", NULL);
183 gst_bin_add_many (GST_BIN (pipeline), mix, convert, sink, NULL);
184 gst_element_link_many (mix, convert, sink, NULL);
186 /* prepare mixer channel strips */
187 make_mixer_channel (pipeline, mix, argv[1]);
188 make_mixer_channel (pipeline, mix, argv[2]);
190 /* setup message handling */
191 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
192 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
193 g_signal_connect (bus, "message::error", (GCallback) message_received,
195 g_signal_connect (bus, "message::warning", (GCallback) message_received,
197 g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
201 setup_gui (pipeline, argv[1], argv[2]);
203 /* go to main loop */
204 gst_element_set_state (pipeline, GST_STATE_PLAYING);
206 gst_element_set_state (pipeline, GST_STATE_NULL);
207 gst_object_unref (pipeline);