3 * Copyright (C) 2008 Sebastian Dröge <slomo@circular-chaos.org>
5 * equalizer.c: Unit test for the equalizer element
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include <gst/base/gstbasetransform.h>
25 #include <gst/check/gstcheck.h>
29 /* For ease of programming we use globals to keep refs for our floating
30 * src and sink pads we create; otherwise we always have to do get_pad,
31 * get_peer, and then remove references in every test function */
32 GstPad *mysrcpad, *mysinkpad;
34 #define EQUALIZER_CAPS_STRING \
35 "audio/x-raw-float, " \
36 "channels = (int) 1, " \
37 "rate = (int) 48000, " \
38 "endianness = (int) BYTE_ORDER, " \
41 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS ("audio/x-raw-float, "
45 "channels = (int) 1, "
46 "rate = (int) 48000, "
47 "endianness = (int) BYTE_ORDER, " "width = (int) 64 ")
49 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
52 GST_STATIC_CAPS ("audio/x-raw-float, "
53 "channels = (int) 1, "
54 "rate = (int) 48000, "
55 "endianness = (int) BYTE_ORDER, " "width = (int) 64 ")
59 setup_equalizer (void)
61 GstElement *equalizer;
63 GST_DEBUG ("setup_equalizer");
64 equalizer = gst_check_setup_element ("equalizer-nbands");
65 mysrcpad = gst_check_setup_src_pad (equalizer, &srctemplate, NULL);
66 mysinkpad = gst_check_setup_sink_pad (equalizer, &sinktemplate, NULL);
67 gst_pad_set_active (mysrcpad, TRUE);
68 gst_pad_set_active (mysinkpad, TRUE);
74 cleanup_equalizer (GstElement * equalizer)
76 GST_DEBUG ("cleanup_equalizer");
78 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
79 g_list_free (buffers);
82 gst_pad_set_active (mysrcpad, FALSE);
83 gst_pad_set_active (mysinkpad, FALSE);
84 gst_check_teardown_src_pad (equalizer);
85 gst_check_teardown_sink_pad (equalizer);
86 gst_check_teardown_element (equalizer);
89 GST_START_TEST (test_equalizer_5bands_passthrough)
91 GstElement *equalizer;
97 equalizer = setup_equalizer ();
98 g_object_set (G_OBJECT (equalizer), "num-bands", 5, NULL);
100 fail_unless_equals_int (gst_child_proxy_get_children_count (GST_CHILD_PROXY
103 fail_unless (gst_element_set_state (equalizer,
104 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
105 "could not set to playing");
107 inbuffer = gst_buffer_new_and_alloc (1024 * sizeof (gdouble));
108 in = (gdouble *) GST_BUFFER_DATA (inbuffer);
109 for (i = 0; i < 1024; i++)
110 in[i] = g_random_double_range (-1.0, 1.0);
112 caps = gst_caps_from_string (EQUALIZER_CAPS_STRING);
113 gst_buffer_set_caps (inbuffer, caps);
114 gst_caps_unref (caps);
115 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
117 /* pushing gives away my reference ... */
118 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
119 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
120 /* ... and puts a new buffer on the global list */
121 fail_unless (g_list_length (buffers) == 1);
123 res = (gdouble *) GST_BUFFER_DATA (GST_BUFFER (buffers->data));
125 for (i = 0; i < 1024; i++)
126 fail_unless_equals_float (in[i], res[i]);
129 cleanup_equalizer (equalizer);
134 GST_START_TEST (test_equalizer_5bands_minus_24)
136 GstElement *equalizer;
139 gdouble *in, *res, rms_in, rms_out;
142 equalizer = setup_equalizer ();
143 g_object_set (G_OBJECT (equalizer), "num-bands", 5, NULL);
145 fail_unless_equals_int (gst_child_proxy_get_children_count (GST_CHILD_PROXY
148 for (i = 0; i < 5; i++) {
150 gst_child_proxy_get_child_by_index (GST_CHILD_PROXY (equalizer), i);
151 fail_unless (band != NULL);
153 g_object_set (G_OBJECT (band), "gain", -24.0, NULL);
154 g_object_unref (G_OBJECT (band));
157 fail_unless (gst_element_set_state (equalizer,
158 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
159 "could not set to playing");
161 inbuffer = gst_buffer_new_and_alloc (1024 * sizeof (gdouble));
162 in = (gdouble *) GST_BUFFER_DATA (inbuffer);
163 for (i = 0; i < 1024; i++)
164 in[i] = g_random_double_range (-1.0, 1.0);
167 for (i = 0; i < 1024; i++)
168 rms_in += in[i] * in[i];
169 rms_in = sqrt (rms_in / 1024);
171 caps = gst_caps_from_string (EQUALIZER_CAPS_STRING);
172 gst_buffer_set_caps (inbuffer, caps);
173 gst_caps_unref (caps);
174 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
176 /* pushing gives away my reference ... */
177 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
178 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
179 /* ... and puts a new buffer on the global list */
180 fail_unless (g_list_length (buffers) == 1);
182 res = (gdouble *) GST_BUFFER_DATA (GST_BUFFER (buffers->data));
185 for (i = 0; i < 1024; i++)
186 rms_out += res[i] * res[i];
187 rms_out = sqrt (rms_out / 1024);
189 fail_unless (rms_in > rms_out);
192 cleanup_equalizer (equalizer);
197 GST_START_TEST (test_equalizer_5bands_plus_12)
199 GstElement *equalizer;
202 gdouble *in, *res, rms_in, rms_out;
205 equalizer = setup_equalizer ();
206 g_object_set (G_OBJECT (equalizer), "num-bands", 5, NULL);
208 fail_unless_equals_int (gst_child_proxy_get_children_count (GST_CHILD_PROXY
211 for (i = 0; i < 5; i++) {
213 gst_child_proxy_get_child_by_index (GST_CHILD_PROXY (equalizer), i);
214 fail_unless (band != NULL);
216 g_object_set (G_OBJECT (band), "gain", 12.0, NULL);
217 g_object_unref (G_OBJECT (band));
220 fail_unless (gst_element_set_state (equalizer,
221 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
222 "could not set to playing");
224 inbuffer = gst_buffer_new_and_alloc (1024 * sizeof (gdouble));
225 in = (gdouble *) GST_BUFFER_DATA (inbuffer);
226 for (i = 0; i < 1024; i++)
227 in[i] = g_random_double_range (-1.0, 1.0);
230 for (i = 0; i < 1024; i++)
231 rms_in += in[i] * in[i];
232 rms_in = sqrt (rms_in / 1024);
234 caps = gst_caps_from_string (EQUALIZER_CAPS_STRING);
235 gst_buffer_set_caps (inbuffer, caps);
236 gst_caps_unref (caps);
237 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
239 /* pushing gives away my reference ... */
240 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
241 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
242 /* ... and puts a new buffer on the global list */
243 fail_unless (g_list_length (buffers) == 1);
245 res = (gdouble *) GST_BUFFER_DATA (GST_BUFFER (buffers->data));
248 for (i = 0; i < 1024; i++)
249 rms_out += res[i] * res[i];
250 rms_out = sqrt (rms_out / 1024);
252 fail_unless (rms_in < rms_out);
255 cleanup_equalizer (equalizer);
260 GST_START_TEST (test_equalizer_band_number_changing)
262 GstElement *equalizer;
265 equalizer = setup_equalizer ();
267 g_object_set (G_OBJECT (equalizer), "num-bands", 5, NULL);
268 fail_unless_equals_int (gst_child_proxy_get_children_count (GST_CHILD_PROXY
271 for (i = 0; i < 5; i++) {
274 band = gst_child_proxy_get_child_by_index (GST_CHILD_PROXY (equalizer), i);
275 fail_unless (band != NULL);
276 gst_object_unref (band);
279 g_object_set (G_OBJECT (equalizer), "num-bands", 10, NULL);
280 fail_unless_equals_int (gst_child_proxy_get_children_count (GST_CHILD_PROXY
283 for (i = 0; i < 10; i++) {
286 band = gst_child_proxy_get_child_by_index (GST_CHILD_PROXY (equalizer), i);
287 fail_unless (band != NULL);
288 gst_object_unref (band);
292 cleanup_equalizer (equalizer);
298 equalizer_suite (void)
300 Suite *s = suite_create ("equalizer");
301 TCase *tc_chain = tcase_create ("general");
303 suite_add_tcase (s, tc_chain);
304 tcase_add_test (tc_chain, test_equalizer_5bands_passthrough);
305 tcase_add_test (tc_chain, test_equalizer_5bands_minus_24);
306 tcase_add_test (tc_chain, test_equalizer_5bands_plus_12);
307 tcase_add_test (tc_chain, test_equalizer_band_number_changing);
313 main (int argc, char **argv)
317 Suite *s = equalizer_suite ();
318 SRunner *sr = srunner_create (s);
320 gst_check_init (&argc, &argv);
322 srunner_run_all (sr, CK_NORMAL);
323 nf = srunner_ntests_failed (sr);