summaryrefslogtreecommitdiff
path: root/tests/icles/equalizer-test.c
blob: fc6d5273eafd85b3e68c63716dbfaa004fbd4395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* GStreamer test for the equalizer element
 * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Will tests the equalizer by fading all bands in and out one by one and
 * finaly all together.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <gst/gst.h>

#include <stdlib.h>
#include <math.h>

GST_DEBUG_CATEGORY_STATIC (equalizer_test_debug);
#define GST_CAT_DEFAULT equalizer_test_debug

static GstBus *pipeline_bus;

static gboolean
check_bus (GstClockTime max_wait_time)
{
  GstMessage *msg;

  msg = gst_bus_poll (pipeline_bus, GST_MESSAGE_ERROR | GST_MESSAGE_EOS,
      max_wait_time);

  if (msg == NULL)
    return FALSE;

  if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
    GError *err = NULL;
    gchar *debug = NULL;

    g_assert (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
    gst_message_parse_error (msg, &err, &debug);
    GST_ERROR ("ERROR: %s [%s]", err->message, debug);
    g_print ("\n===========> ERROR: %s\n%s\n\n", err->message, debug);
    g_error_free (err);
    g_free (debug);
  }

  if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS) {
    g_print ("\n === EOS ===\n\n");
  }

  gst_message_unref (msg);
  return TRUE;
}

// fix below

static void
equalizer_set_band_value (GstElement * eq, guint band, gdouble val)
{
  GstObject *child;

  child = gst_child_proxy_get_child_by_index (GST_CHILD_PROXY (eq), band);
  g_object_set (child, "gain", val, NULL);
  gst_object_unref (child);
  g_print ("Band %2d: %.2f\n", band, val);
}

static void
equalizer_set_all_band_values (GstElement * eq, guint num, gdouble val)
{
  gint i;
  GstObject *child;

  for (i = 0; i < num; i++) {
    child = gst_child_proxy_get_child_by_index (GST_CHILD_PROXY (eq), i);
    g_object_set (child, "gain", val, NULL);
    gst_object_unref (child);
  }
  g_print ("All bands: %.2f\n", val);
}

// fix above

static gboolean
equalizer_set_band_value_and_wait (GstElement * eq, guint band, gdouble val)
{
  equalizer_set_band_value (eq, band, val);
  return check_bus (100 * GST_MSECOND);
}

static gboolean
equalizer_set_all_band_values_and_wait (GstElement * eq, guint num, gdouble val)
{
  equalizer_set_all_band_values (eq, num, val);
  return check_bus (100 * GST_MSECOND);
}

static void
do_slider_fiddling (GstElement * playbin, GstElement * eq)
{
  gboolean stop;
  guint num_bands, i;
  gdouble d, step = 0.5;

  stop = FALSE;

  g_object_get (eq, "num-bands", &num_bands, NULL);

  g_print ("%u bands.\n", num_bands);

  while (!stop) {
    for (i = 0; !stop && i < num_bands; ++i) {
      d = -24.0;
      while (!stop && d <= 12.0) {
        stop = equalizer_set_band_value_and_wait (eq, i, d);
        d += step;
      }
      d = 12.0;
      while (!stop && d >= -24.0) {
        stop = equalizer_set_band_value_and_wait (eq, i, d);
        d -= step;
      }
      d = -24.0;
      while (!stop && d <= 12.0) {
        stop = equalizer_set_band_value_and_wait (eq, i, d);
        d += step;
      }
    }

    d = 0.0;
    while (!stop && d <= 12.0) {
      stop = equalizer_set_all_band_values_and_wait (eq, num_bands, d);
      d += step;
    }
    d = 12.0;
    while (!stop && d >= -24.0) {
      stop = equalizer_set_all_band_values_and_wait (eq, num_bands, d);
      d -= step;
    }
    d = -24.0;
    while (!stop && d <= 0.0) {
      stop = equalizer_set_all_band_values_and_wait (eq, num_bands, d);
      d += step;
    }
  }
}

int
main (int argc, char **argv)
{
  gchar *opt_audiosink_str = NULL;
  gchar **filenames = NULL;
  const GOptionEntry test_goptions[] = {
    {"audiosink", '\0', 0, G_OPTION_ARG_STRING, &opt_audiosink_str,
        "audiosink to use (default: autoaudiosink)", NULL},
    {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
    {NULL, '\0', 0, 0, NULL, NULL, NULL}
  };
  GOptionContext *ctx;
  GError *opt_err = NULL;

  GstStateChangeReturn ret;
  GstElement *playbin, *sink, *bin, *eq, *auconv;
  GstPad *eq_sinkpad;
  gchar *uri;

#if !GLIB_CHECK_VERSION (2, 31, 0)
  if (!g_thread_supported ())
    g_thread_init (NULL);
#endif

  /* command line option parsing */
  ctx = g_option_context_new ("FILENAME");
  g_option_context_add_group (ctx, gst_init_get_option_group ());
  g_option_context_add_main_entries (ctx, test_goptions, NULL);

  if (!g_option_context_parse (ctx, &argc, &argv, &opt_err)) {
    g_error ("Error parsing command line options: %s", opt_err->message);
    return -1;
  }

  GST_DEBUG_CATEGORY_INIT (equalizer_test_debug, "equalizertest", 0, "eqtest");

  if (filenames == NULL || *filenames == NULL) {
    g_printerr ("Please specify a file to play back\n");
    return -1;
  }

  playbin = gst_element_factory_make ("playbin", "playbin");
  if (playbin == NULL) {
    g_error ("Couldn't create 'playbin' element");
    return -1;
  }

  if (opt_audiosink_str) {
    g_print ("Trying audiosink '%s' ...", opt_audiosink_str);
    sink = gst_element_factory_make (opt_audiosink_str, "sink");
    g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
  } else {
    sink = NULL;
  }
  if (sink == NULL) {
    g_print ("Trying audiosink '%s' ...", "autoaudiosink");
    sink = gst_element_factory_make ("autoaudiosink", "sink");
    g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
  }
  if (sink == NULL) {
    g_print ("Trying audiosink '%s' ...", "alsasink");
    sink = gst_element_factory_make ("alsasink", "sink");
    g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
  }
  if (sink == NULL) {
    g_print ("Trying audiosink '%s' ...", "osssink");
    sink = gst_element_factory_make ("osssink", "sink");
    g_print ("%s\n", (sink) ? "ok" : "element couldn't be created");
  }

  g_assert (sink != NULL);

  bin = gst_bin_new ("ausinkbin");
  g_assert (bin != NULL);

  eq = gst_element_factory_make ("equalizer-nbands", "equalizer");
  g_assert (eq != NULL);

  auconv = gst_element_factory_make ("audioconvert", "eqauconv");
  g_assert (auconv != NULL);

  gst_bin_add_many (GST_BIN (bin), eq, auconv, sink, NULL);

  if (!gst_element_link (eq, auconv))
    g_error ("Failed to link equalizer to audioconvert");

  if (!gst_element_link (auconv, sink))
    g_error ("Failed to link audioconvert to audio sink");

  eq_sinkpad = gst_element_get_static_pad (eq, "sink");
  g_assert (eq_sinkpad != NULL);

  gst_element_add_pad (bin, gst_ghost_pad_new (NULL, eq_sinkpad));
  gst_object_unref (eq_sinkpad);

  g_object_set (playbin, "audio-sink", bin, NULL);

  /* won't work: uri = gst_uri_construct ("file", filenames[0]); */
  uri = g_strdup_printf ("file://%s", filenames[0]);
  g_object_set (playbin, "uri", uri, NULL);
  g_free (uri);

  pipeline_bus = GST_ELEMENT_BUS (playbin);

  ret = gst_element_set_state (playbin, GST_STATE_PLAYING);
  if (ret == GST_STATE_CHANGE_FAILURE) {
    g_printerr ("Failed to set playbin to PLAYING\n");
    check_bus (1 * GST_SECOND);
    return -1;
  }

  ret = gst_element_get_state (playbin, NULL, NULL, 5 * GST_SECOND);
  if (ret == GST_STATE_CHANGE_ASYNC) {
    g_printerr ("Failed to go to PLAYING in 5 seconds, bailing out\n");
    return -1;
  } else if (ret != GST_STATE_CHANGE_SUCCESS) {
    g_printerr ("State change to PLAYING failed\n");
    check_bus (1 * GST_SECOND);
    return -1;
  }

  g_print ("Playing ...\n");
  do_slider_fiddling (playbin, eq);

  gst_element_set_state (playbin, GST_STATE_NULL);
  gst_object_unref (playbin);

  return 0;
}