1 /* GStreamer ReplayGain volume adjustment
3 * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
5 * rgvolume.c: Unit test for the rgvolume 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
23 #include <gst/check/gstcheck.h>
27 GList *buffers = NULL;
30 /* For ease of programming we use globals to keep refs for our floating src and
31 * sink pads we create; otherwise we always have to do get_pad, get_peer, and
32 * then remove references in every test function */
33 static GstPad *mysrcpad, *mysinkpad;
35 #define RG_VOLUME_CAPS_TEMPLATE_STRING \
36 "audio/x-raw-float, " \
37 "width = (int) 32, " \
38 "endianness = (int) BYTE_ORDER, " \
39 "channels = (int) [ 1, MAX ], " \
40 "rate = (int) [ 1, MAX ]"
42 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
47 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
50 GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
53 /* gstcheck sets up a chain function that appends buffers to a global list.
54 * This is our equivalent of that for event handling. */
56 event_func (GstPad * pad, GstEvent * event)
58 events = g_list_append (events, event);
68 GST_DEBUG ("setup_rgvolume");
69 element = gst_check_setup_element ("rgvolume");
70 mysrcpad = gst_check_setup_src_pad (element, &srctemplate, NULL);
71 mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate, NULL);
73 /* Capture events, to test tag filtering behavior: */
74 gst_pad_set_event_function (mysinkpad, event_func);
76 gst_pad_set_active (mysrcpad, TRUE);
77 gst_pad_set_active (mysinkpad, TRUE);
83 cleanup_rgvolume (GstElement * element)
85 GST_DEBUG ("cleanup_rgvolume");
87 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
88 g_list_free (buffers);
91 g_list_foreach (events, (GFunc) gst_mini_object_unref, NULL);
95 gst_pad_set_active (mysrcpad, FALSE);
96 gst_pad_set_active (mysinkpad, FALSE);
97 gst_check_teardown_src_pad (element);
98 gst_check_teardown_sink_pad (element);
99 gst_check_teardown_element (element);
103 set_playing_state (GstElement * element)
105 fail_unless (gst_element_set_state (element,
106 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
107 "Could not set state to PLAYING");
111 set_null_state (GstElement * element)
113 fail_unless (gst_element_set_state (element,
114 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS,
115 "Could not set state to NULL");
119 send_eos_event (GstElement * element)
121 GstEvent *event = gst_event_new_eos ();
123 fail_unless (g_list_length (events) == 0);
124 fail_unless (gst_pad_push_event (mysrcpad, event),
125 "Pushing EOS event failed");
126 fail_unless (g_list_length (events) == 1);
127 fail_unless (events->data == event);
128 gst_mini_object_unref ((GstMiniObject *) events->data);
129 events = g_list_remove (events, event);
133 send_tag_event (GstElement * element, GstEvent * event)
135 g_return_val_if_fail (event->type == GST_EVENT_TAG, NULL);
137 fail_unless (g_list_length (events) == 0);
138 fail_unless (gst_pad_push_event (mysrcpad, event),
139 "Pushing tag event failed");
141 if (g_list_length (events) == 0) {
142 /* Event got filtered out. */
145 GstTagList *tag_list;
148 event = events->data;
149 events = g_list_remove (events, event);
151 fail_unless (event->type == GST_EVENT_TAG);
152 gst_event_parse_tag (event, &tag_list);
154 /* The element is supposed to filter out ReplayGain related tags. */
155 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &dummy),
156 "tag event still contains track gain tag");
157 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &dummy),
158 "tag event still contains track peak tag");
159 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &dummy),
160 "tag event still contains album gain tag");
161 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &dummy),
162 "tag event still contains album peak tag");
169 test_buffer_new (gfloat value)
176 buf = gst_buffer_new_and_alloc (8 * sizeof (gfloat));
177 data = (gfloat *) GST_BUFFER_DATA (buf);
178 for (i = 0; i < 8; i++)
181 caps = gst_caps_from_string ("audio/x-raw-float, "
182 "rate = 8000, channels = 1, endianess = BYTE_ORDER, width = 32");
183 gst_buffer_set_caps (buf, caps);
184 gst_caps_unref (caps);
186 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
191 #define MATCH_GAIN(g1, g2) ((g1 < g2 + 1e-6) && (g2 < g1 + 1e-6))
194 fail_unless_target_gain (GstElement * element, gdouble expected_gain)
198 g_object_get (element, "target-gain", &prop_gain, NULL);
200 fail_unless (MATCH_GAIN (prop_gain, expected_gain),
201 "Target gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
205 fail_unless_result_gain (GstElement * element, gdouble expected_gain)
207 GstBuffer *input_buf, *output_buf;
208 gfloat input_sample, output_sample;
209 gdouble gain, prop_gain;
210 gboolean is_passthrough, expect_passthrough;
213 fail_unless (g_list_length (buffers) == 0);
216 input_buf = test_buffer_new (input_sample);
218 /* We keep an extra reference to detect passthrough mode. */
219 gst_buffer_ref (input_buf);
220 /* Pushing steals a reference. */
221 fail_unless (gst_pad_push (mysrcpad, input_buf) == GST_FLOW_OK);
222 gst_buffer_unref (input_buf);
224 /* The output buffer ends up on the global buffer list. */
225 fail_unless (g_list_length (buffers) == 1);
226 output_buf = buffers->data;
227 fail_if (output_buf == NULL);
229 buffers = g_list_remove (buffers, output_buf);
230 ASSERT_BUFFER_REFCOUNT (output_buf, "output_buf", 1);
231 fail_unless_equals_int (GST_BUFFER_SIZE (output_buf), 8 * sizeof (gfloat));
233 output_sample = *((gfloat *) GST_BUFFER_DATA (output_buf));
235 fail_if (output_sample == 0.0, "First output sample is zero");
236 for (i = 1; i < 8; i++) {
237 gfloat output = ((gfloat *) GST_BUFFER_DATA (output_buf))[i];
239 fail_unless (output_sample == output, "Output samples not uniform");
242 gain = 20. * log10 (output_sample / input_sample);
243 fail_unless (MATCH_GAIN (gain, expected_gain),
244 "Applied gain is %.2f dB, expected %.2f dB", gain, expected_gain);
245 g_object_get (element, "result-gain", &prop_gain, NULL);
246 fail_unless (MATCH_GAIN (prop_gain, expected_gain),
247 "Result gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
249 is_passthrough = (output_buf == input_buf);
250 expect_passthrough = MATCH_GAIN (expected_gain, +0.00);
251 fail_unless (is_passthrough == expect_passthrough,
253 ? "Expected operation in passthrough mode"
254 : "Incorrect passthrough behaviour");
256 gst_buffer_unref (output_buf);
260 fail_unless_gain (GstElement * element, gdouble expected_gain)
262 fail_unless_target_gain (element, expected_gain);
263 fail_unless_result_gain (element, expected_gain);
266 /* Start of tests. */
268 GST_START_TEST (test_no_buffer)
270 GstElement *element = setup_rgvolume ();
272 set_playing_state (element);
273 set_null_state (element);
274 set_playing_state (element);
275 send_eos_event (element);
277 cleanup_rgvolume (element);
282 GST_START_TEST (test_events)
284 GstElement *element = setup_rgvolume ();
287 GstTagList *tag_list;
290 set_playing_state (element);
292 tag_list = gst_tag_list_new ();
293 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
294 GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
295 GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
296 GST_TAG_ARTIST, "Foobar", NULL);
297 event = gst_event_new_tag (tag_list);
298 new_event = send_tag_event (element, event);
299 /* Expect the element to modify the writable event. */
300 fail_unless (event == new_event, "Writable tag event not reused");
301 gst_event_parse_tag (new_event, &tag_list);
302 fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
303 fail_unless (g_str_equal (artist, "Foobar"));
305 gst_event_unref (new_event);
307 /* Same as above, but with a non-writable event. */
309 tag_list = gst_tag_list_new ();
310 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
311 GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
312 GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
313 GST_TAG_ARTIST, "Foobar", NULL);
314 event = gst_event_new_tag (tag_list);
315 /* Holding an extra ref makes the event unwritable: */
316 gst_event_ref (event);
317 new_event = send_tag_event (element, event);
318 fail_unless (event != new_event, "Unwritable tag event reused");
319 gst_event_parse_tag (new_event, &tag_list);
320 fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
321 fail_unless (g_str_equal (artist, "Foobar"));
323 gst_event_unref (event);
324 gst_event_unref (new_event);
326 cleanup_rgvolume (element);
331 GST_START_TEST (test_simple)
333 GstElement *element = setup_rgvolume ();
334 GstTagList *tag_list;
336 g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
337 "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
338 set_playing_state (element);
340 tag_list = gst_tag_list_new ();
341 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
342 GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
343 GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
344 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
345 fail_unless_gain (element, -9.45); /* pre-amp + track gain */
346 send_eos_event (element);
348 g_object_set (element, "album-mode", TRUE, NULL);
350 tag_list = gst_tag_list_new ();
351 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
352 GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
353 GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
354 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
355 fail_unless_gain (element, -3.91); /* pre-amp + album gain */
357 /* Switching back to track mode in the middle of a stream: */
358 g_object_set (element, "album-mode", FALSE, NULL);
359 fail_unless_gain (element, -9.45); /* pre-amp + track gain */
360 send_eos_event (element);
362 cleanup_rgvolume (element);
367 /* If there are no gain tags at all, the fallback gain is used. */
369 GST_START_TEST (test_fallback_gain)
371 GstElement *element = setup_rgvolume ();
372 GstTagList *tag_list;
374 /* First some track where fallback does _not_ apply. */
376 g_object_set (element, "album-mode", FALSE, "headroom", 10.00,
377 "pre-amp", -6.00, "fallback-gain", -3.00, NULL);
378 set_playing_state (element);
380 tag_list = gst_tag_list_new ();
381 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
382 GST_TAG_TRACK_GAIN, +3.5, GST_TAG_TRACK_PEAK, 1.0,
383 GST_TAG_ALBUM_GAIN, -0.5, GST_TAG_ALBUM_PEAK, 1.0, NULL);
384 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
385 fail_unless_gain (element, -2.50); /* pre-amp + track gain */
386 send_eos_event (element);
388 /* Now a track completely missing tags. */
390 fail_unless_gain (element, -9.00); /* pre-amp + fallback-gain */
392 /* Changing the fallback gain in the middle of a stream, going to pass-through
394 g_object_set (element, "fallback-gain", +6.00, NULL);
395 fail_unless_gain (element, +0.00); /* pre-amp + fallback-gain */
396 send_eos_event (element);
398 /* Verify that result gain is set to +0.00 with pre-amp + fallback-gain >
399 * +0.00 and no headroom. */
401 g_object_set (element, "fallback-gain", +12.00, "headroom", +0.00, NULL);
402 fail_unless_target_gain (element, +6.00); /* pre-amp + fallback-gain */
403 fail_unless_result_gain (element, +0.00);
404 send_eos_event (element);
406 cleanup_rgvolume (element);
411 /* If album gain is to be preferred but not available, the track gain is to be
414 GST_START_TEST (test_fallback_track)
416 GstElement *element = setup_rgvolume ();
417 GstTagList *tag_list;
419 g_object_set (element, "album-mode", TRUE, "headroom", +0.00,
420 "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
421 set_playing_state (element);
423 tag_list = gst_tag_list_new ();
424 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
425 GST_TAG_TRACK_GAIN, +2.11, GST_TAG_TRACK_PEAK, 1.0, NULL);
426 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
427 fail_unless_gain (element, -3.89); /* pre-amp + track gain */
429 send_eos_event (element);
431 cleanup_rgvolume (element);
436 /* If track gain is to be preferred but not available, the album gain is to be
439 GST_START_TEST (test_fallback_album)
441 GstElement *element = setup_rgvolume ();
442 GstTagList *tag_list;
444 g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
445 "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
446 set_playing_state (element);
448 tag_list = gst_tag_list_new ();
449 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
450 GST_TAG_ALBUM_GAIN, +3.73, GST_TAG_ALBUM_PEAK, 1.0, NULL);
451 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
452 fail_unless_gain (element, -2.27); /* pre-amp + album gain */
454 send_eos_event (element);
456 cleanup_rgvolume (element);
461 GST_START_TEST (test_headroom)
463 GstElement *element = setup_rgvolume ();
464 GstTagList *tag_list;
466 g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
467 "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
468 set_playing_state (element);
470 tag_list = gst_tag_list_new ();
471 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
472 GST_TAG_TRACK_GAIN, +3.50, GST_TAG_TRACK_PEAK, 1.0, NULL);
473 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
474 fail_unless_target_gain (element, +3.50); /* pre-amp + track gain */
475 fail_unless_result_gain (element, +0.00);
476 send_eos_event (element);
478 g_object_set (element, "headroom", +2.00, NULL);
479 tag_list = gst_tag_list_new ();
480 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
481 GST_TAG_TRACK_GAIN, +9.18, GST_TAG_TRACK_PEAK, 0.687149, NULL);
482 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
483 fail_unless_target_gain (element, +9.18); /* pre-amp + track gain */
484 /* Result is 20. * log10 (1. / peak) + headroom. */
485 fail_unless_result_gain (element, 5.2589816238303335);
486 send_eos_event (element);
488 g_object_set (element, "album-mode", TRUE, NULL);
489 tag_list = gst_tag_list_new ();
490 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
491 GST_TAG_ALBUM_GAIN, +5.50, GST_TAG_ALBUM_PEAK, 1.0, NULL);
492 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
493 fail_unless_target_gain (element, +5.50); /* pre-amp + album gain */
494 fail_unless_result_gain (element, +2.00); /* headroom */
495 send_eos_event (element);
497 cleanup_rgvolume (element);
502 GST_START_TEST (test_reference_level)
504 GstElement *element = setup_rgvolume ();
505 GstTagList *tag_list;
507 g_object_set (element,
509 "headroom", +0.00, "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
510 set_playing_state (element);
512 tag_list = gst_tag_list_new ();
513 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
514 GST_TAG_TRACK_GAIN, 0.00, GST_TAG_TRACK_PEAK, 0.2,
515 GST_TAG_REFERENCE_LEVEL, 83., NULL);
516 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
517 /* Because our authorative reference is 89 dB, we bump it up by +6 dB. */
518 fail_unless_gain (element, +6.00); /* pre-amp + track gain */
519 send_eos_event (element);
521 g_object_set (element, "album-mode", TRUE, NULL);
523 /* Same as above, but with album gain. */
525 tag_list = gst_tag_list_new ();
526 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
527 GST_TAG_TRACK_GAIN, 1.23, GST_TAG_TRACK_PEAK, 0.1,
528 GST_TAG_ALBUM_GAIN, 0.00, GST_TAG_ALBUM_PEAK, 0.2,
529 GST_TAG_REFERENCE_LEVEL, 83., NULL);
530 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
531 fail_unless_gain (element, +6.00); /* pre-amp + album gain */
533 cleanup_rgvolume (element);
539 rgvolume_suite (void)
541 Suite *s = suite_create ("rgvolume");
542 TCase *tc_chain = tcase_create ("general");
544 suite_add_tcase (s, tc_chain);
546 tcase_add_test (tc_chain, test_no_buffer);
547 tcase_add_test (tc_chain, test_events);
548 tcase_add_test (tc_chain, test_simple);
549 tcase_add_test (tc_chain, test_fallback_gain);
550 tcase_add_test (tc_chain, test_fallback_track);
551 tcase_add_test (tc_chain, test_fallback_album);
552 tcase_add_test (tc_chain, test_headroom);
553 tcase_add_test (tc_chain, test_reference_level);
559 main (int argc, char **argv)
563 Suite *s = rgvolume_suite ();
564 SRunner *sr = srunner_create (s);
566 gst_check_init (&argc, &argv);
568 srunner_run_all (sr, CK_ENV);
569 nf = srunner_ntests_failed (sr);