1 /* GStreamer test for the equalizer element
2 * Copyright (C) 2007 Tim-Philipp Müller <tim centricular 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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /*
21 * Will tests the equalizer by fading all bands in and out one by one and
22 * finaly all together.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gst/gst.h>
30
31 #include <stdlib.h>
32 #include <math.h>
33
34 GST_DEBUG_CATEGORY_STATIC (equalizer_test_debug);
35 #define GST_CAT_DEFAULT equalizer_test_debug
36
37 static GstBus *pipeline_bus;
38
39 static gboolean
check_bus(GstClockTime max_wait_time)40 check_bus (GstClockTime max_wait_time)
41 {
42 GstMessage *msg;
43
44 msg = gst_bus_poll (pipeline_bus, GST_MESSAGE_ERROR | GST_MESSAGE_EOS,
45 max_wait_time);
46
47 if (msg == NULL)
48 return FALSE;
49
50 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
51 GError *err = NULL;
52 gchar *debug = NULL;
53
54 g_assert (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
55 gst_message_parse_error (msg, &err, &debug);
56 GST_ERROR ("ERROR: %s [%s]", err->message, debug);
57 g_print ("\n===========> ERROR: %s\n%s\n\n", err->message, debug);
58 g_clear_error (&err);
59 g_free (debug);
60 }
61
62 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS) {
63 g_print ("\n === EOS ===\n\n");
64 }
65
66 gst_message_unref (msg);
67 return TRUE;
68 }
69
70 // fix below
71
72 static void
equalizer_set_band_value(GstElement * eq,guint band,gdouble val)73 equalizer_set_band_value (GstElement * eq, guint band, gdouble val)
74 {
75 GObject *child;
76
77 child = gst_child_proxy_get_child_by_index (GST_CHILD_PROXY (eq), band);
78 g_object_set (child, "gain", val, NULL);
79 g_object_unref (child);
80 g_print ("Band %2d: %.2f\n", band, val);
81 }
82
83 static void
equalizer_set_all_band_values(GstElement * eq,guint num,gdouble val)84 equalizer_set_all_band_values (GstElement * eq, guint num, gdouble val)
85 {
86 gint i;
87 GObject *child;
88
89 for (i = 0; i < num; i++) {
90 child = gst_child_proxy_get_child_by_index (GST_CHILD_PROXY (eq), i);
91 g_object_set (child, "gain", val, NULL);
92 g_object_unref (child);
93 }
94 g_print ("All bands: %.2f\n", val);
95 }
96
97 // fix above
98
99 static gboolean
equalizer_set_band_value_and_wait(GstElement * eq,guint band,gdouble val)100 equalizer_set_band_value_and_wait (GstElement * eq, guint band, gdouble val)
101 {
102 equalizer_set_band_value (eq, band, val);
103 return check_bus (100 * GST_MSECOND);
104 }
105
106 static gboolean
equalizer_set_all_band_values_and_wait(GstElement * eq,guint num,gdouble val)107 equalizer_set_all_band_values_and_wait (GstElement * eq, guint num, gdouble val)
108 {
109 equalizer_set_all_band_values (eq, num, val);
110 return check_bus (100 * GST_MSECOND);
111 }
112
113 static void
do_slider_fiddling(GstElement * playbin,GstElement * eq)114 do_slider_fiddling (GstElement * playbin, GstElement * eq)
115 {
116 gboolean stop;
117 guint num_bands, i;
118 gdouble d, step = 0.5;
119
120 stop = FALSE;
121
122 g_object_get (eq, "num-bands", &num_bands, NULL);
123
124 g_print ("%u bands.\n", num_bands);
125
126 while (!stop) {
127 for (i = 0; !stop && i < num_bands; ++i) {
128 d = -24.0;
129 while (!stop && d <= 12.0) {
130 stop = equalizer_set_band_value_and_wait (eq, i, d);
131 d += step;
132 }
133 d = 12.0;
134 while (!stop && d >= -24.0) {
135 stop = equalizer_set_band_value_and_wait (eq, i, d);
136 d -= step;
137 }
138 d = -24.0;
139 while (!stop && d <= 12.0) {
140 stop = equalizer_set_band_value_and_wait (eq, i, d);
141 d += step;
142 }
143 }
144
145 d = 0.0;
146 while (!stop && d <= 12.0) {
147 stop = equalizer_set_all_band_values_and_wait (eq, num_bands, d);
148 d += step;
149 }
150 d = 12.0;
151 while (!stop && d >= -24.0) {
152 stop = equalizer_set_all_band_values_and_wait (eq, num_bands, d);
153 d -= step;
154 }
155 d = -24.0;
156 while (!stop && d <= 0.0) {
157 stop = equalizer_set_all_band_values_and_wait (eq, num_bands, d);
158 d += step;
159 }
160 }
161 }
162
163 int
main(int argc,char ** argv)164 main (int argc, char **argv)
165 {
166 gchar *opt_audiosink_str = NULL;
167 gchar **filenames = NULL;
168 const GOptionEntry test_goptions[] = {
169 {"audiosink", '\0', 0, G_OPTION_ARG_STRING, &opt_audiosink_str,
170 "audiosink to use (default: autoaudiosink)", NULL},
171 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
172 {NULL, '\0', 0, 0, NULL, NULL, NULL}
173 };
174 GOptionContext *ctx;
175 GError *opt_err = NULL;
176
177 GstStateChangeReturn ret;
178 GstElement *playbin, *sink, *bin, *eq, *auconv;
179 GstPad *eq_sinkpad;
180 gchar *uri;
181
182 /* command line option parsing */
183 ctx = g_option_context_new ("FILENAME");
184 g_option_context_add_group (ctx, gst_init_get_option_group ());
185 g_option_context_add_main_entries (ctx, test_goptions, NULL);
186
187 if (!g_option_context_parse (ctx, &argc, &argv, &opt_err)) {
188 g_error ("Error parsing command line options: %s", opt_err->message);
189 g_option_context_free (ctx);
190 g_clear_error (&opt_err);
191 return -1;
192 }
193 g_option_context_free (ctx);
194
195 GST_DEBUG_CATEGORY_INIT (equalizer_test_debug, "equalizertest", 0, "eqtest");
196
197 if (filenames == NULL || *filenames == NULL) {
198 g_printerr ("Please specify a file to play back\n");
199 return -1;
200 }
201
202 playbin = gst_element_factory_make ("playbin", "playbin");
203 if (playbin == NULL) {
204 g_error ("Couldn't create 'playbin' element");
205 return -1;
206 }
207
208 if (opt_audiosink_str) {
209 g_print ("Trying audiosink '%s' ...", opt_audiosink_str);
210 sink = gst_element_factory_make (opt_audiosink_str, "sink");
211 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
212 } else {
213 sink = NULL;
214 }
215 if (sink == NULL) {
216 g_print ("Trying audiosink '%s' ...", "autoaudiosink");
217 sink = gst_element_factory_make ("autoaudiosink", "sink");
218 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
219 }
220 if (sink == NULL) {
221 g_print ("Trying audiosink '%s' ...", "alsasink");
222 sink = gst_element_factory_make ("alsasink", "sink");
223 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
224 }
225 if (sink == NULL) {
226 g_print ("Trying audiosink '%s' ...", "osssink");
227 sink = gst_element_factory_make ("osssink", "sink");
228 g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
229 }
230
231 g_assert (sink != NULL);
232
233 bin = gst_bin_new ("ausinkbin");
234 g_assert (bin != NULL);
235
236 eq = gst_element_factory_make ("equalizer-nbands", "equalizer");
237 g_assert (eq != NULL);
238
239 auconv = gst_element_factory_make ("audioconvert", "eqauconv");
240 g_assert (auconv != NULL);
241
242 gst_bin_add_many (GST_BIN (bin), eq, auconv, sink, NULL);
243
244 if (!gst_element_link (eq, auconv))
245 g_error ("Failed to link equalizer to audioconvert");
246
247 if (!gst_element_link (auconv, sink))
248 g_error ("Failed to link audioconvert to audio sink");
249
250 eq_sinkpad = gst_element_get_static_pad (eq, "sink");
251 g_assert (eq_sinkpad != NULL);
252
253 gst_element_add_pad (bin, gst_ghost_pad_new (NULL, eq_sinkpad));
254 gst_object_unref (eq_sinkpad);
255
256 g_object_set (playbin, "audio-sink", bin, NULL);
257
258 /* won't work: uri = gst_uri_construct ("file", filenames[0]); */
259 uri = g_strdup_printf ("file://%s", filenames[0]);
260 g_object_set (playbin, "uri", uri, NULL);
261 g_free (uri);
262
263 pipeline_bus = GST_ELEMENT_BUS (playbin);
264
265 ret = gst_element_set_state (playbin, GST_STATE_PLAYING);
266 if (ret == GST_STATE_CHANGE_FAILURE) {
267 g_printerr ("Failed to set playbin to PLAYING\n");
268 check_bus (1 * GST_SECOND);
269 return -1;
270 }
271
272 ret = gst_element_get_state (playbin, NULL, NULL, 5 * GST_SECOND);
273 if (ret == GST_STATE_CHANGE_ASYNC) {
274 g_printerr ("Failed to go to PLAYING in 5 seconds, bailing out\n");
275 return -1;
276 } else if (ret != GST_STATE_CHANGE_SUCCESS) {
277 g_printerr ("State change to PLAYING failed\n");
278 check_bus (1 * GST_SECOND);
279 return -1;
280 }
281
282 g_print ("Playing ...\n");
283 do_slider_fiddling (playbin, eq);
284
285 gst_element_set_state (playbin, GST_STATE_NULL);
286 gst_object_unref (playbin);
287
288 return 0;
289 }
290