fc6d5273eafd85b3e68c63716dbfaa004fbd4395
[framework/multimedia/gst-plugins-good0.10.git] / tests / icles / equalizer-test.c
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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
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_error_free (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
73 equalizer_set_band_value (GstElement * eq, guint band, gdouble val)
74 {
75   GstObject *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   gst_object_unref (child);
80   g_print ("Band %2d: %.2f\n", band, val);
81 }
82
83 static void
84 equalizer_set_all_band_values (GstElement * eq, guint num, gdouble val)
85 {
86   gint i;
87   GstObject *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     gst_object_unref (child);
93   }
94   g_print ("All bands: %.2f\n", val);
95 }
96
97 // fix above
98
99 static gboolean
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
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
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
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 #if !GLIB_CHECK_VERSION (2, 31, 0)
183   if (!g_thread_supported ())
184     g_thread_init (NULL);
185 #endif
186
187   /* command line option parsing */
188   ctx = g_option_context_new ("FILENAME");
189   g_option_context_add_group (ctx, gst_init_get_option_group ());
190   g_option_context_add_main_entries (ctx, test_goptions, NULL);
191
192   if (!g_option_context_parse (ctx, &argc, &argv, &opt_err)) {
193     g_error ("Error parsing command line options: %s", opt_err->message);
194     return -1;
195   }
196
197   GST_DEBUG_CATEGORY_INIT (equalizer_test_debug, "equalizertest", 0, "eqtest");
198
199   if (filenames == NULL || *filenames == NULL) {
200     g_printerr ("Please specify a file to play back\n");
201     return -1;
202   }
203
204   playbin = gst_element_factory_make ("playbin", "playbin");
205   if (playbin == NULL) {
206     g_error ("Couldn't create 'playbin' element");
207     return -1;
208   }
209
210   if (opt_audiosink_str) {
211     g_print ("Trying audiosink '%s' ...", opt_audiosink_str);
212     sink = gst_element_factory_make (opt_audiosink_str, "sink");
213     g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
214   } else {
215     sink = NULL;
216   }
217   if (sink == NULL) {
218     g_print ("Trying audiosink '%s' ...", "autoaudiosink");
219     sink = gst_element_factory_make ("autoaudiosink", "sink");
220     g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
221   }
222   if (sink == NULL) {
223     g_print ("Trying audiosink '%s' ...", "alsasink");
224     sink = gst_element_factory_make ("alsasink", "sink");
225     g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
226   }
227   if (sink == NULL) {
228     g_print ("Trying audiosink '%s' ...", "osssink");
229     sink = gst_element_factory_make ("osssink", "sink");
230     g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
231   }
232
233   g_assert (sink != NULL);
234
235   bin = gst_bin_new ("ausinkbin");
236   g_assert (bin != NULL);
237
238   eq = gst_element_factory_make ("equalizer-nbands", "equalizer");
239   g_assert (eq != NULL);
240
241   auconv = gst_element_factory_make ("audioconvert", "eqauconv");
242   g_assert (auconv != NULL);
243
244   gst_bin_add_many (GST_BIN (bin), eq, auconv, sink, NULL);
245
246   if (!gst_element_link (eq, auconv))
247     g_error ("Failed to link equalizer to audioconvert");
248
249   if (!gst_element_link (auconv, sink))
250     g_error ("Failed to link audioconvert to audio sink");
251
252   eq_sinkpad = gst_element_get_static_pad (eq, "sink");
253   g_assert (eq_sinkpad != NULL);
254
255   gst_element_add_pad (bin, gst_ghost_pad_new (NULL, eq_sinkpad));
256   gst_object_unref (eq_sinkpad);
257
258   g_object_set (playbin, "audio-sink", bin, NULL);
259
260   /* won't work: uri = gst_uri_construct ("file", filenames[0]); */
261   uri = g_strdup_printf ("file://%s", filenames[0]);
262   g_object_set (playbin, "uri", uri, NULL);
263   g_free (uri);
264
265   pipeline_bus = GST_ELEMENT_BUS (playbin);
266
267   ret = gst_element_set_state (playbin, GST_STATE_PLAYING);
268   if (ret == GST_STATE_CHANGE_FAILURE) {
269     g_printerr ("Failed to set playbin to PLAYING\n");
270     check_bus (1 * GST_SECOND);
271     return -1;
272   }
273
274   ret = gst_element_get_state (playbin, NULL, NULL, 5 * GST_SECOND);
275   if (ret == GST_STATE_CHANGE_ASYNC) {
276     g_printerr ("Failed to go to PLAYING in 5 seconds, bailing out\n");
277     return -1;
278   } else if (ret != GST_STATE_CHANGE_SUCCESS) {
279     g_printerr ("State change to PLAYING failed\n");
280     check_bus (1 * GST_SECOND);
281     return -1;
282   }
283
284   g_print ("Playing ...\n");
285   do_slider_fiddling (playbin, eq);
286
287   gst_element_set_state (playbin, GST_STATE_NULL);
288   gst_object_unref (playbin);
289
290   return 0;
291 }