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>
24 #include <gst/audio/audio.h>
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 \
37 "format = (string) "GST_AUDIO_NE (F32) ", " \
38 "channels = (int) [ 1, MAX ], " \
39 "rate = (int) [ 1, MAX ]"
41 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
46 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
52 /* gstcheck sets up a chain function that appends buffers to a global list.
53 * This is our equivalent of that for event handling. */
55 event_func (GstPad * pad, GstObject * parent, GstEvent * event)
57 events = g_list_append (events, event);
67 GST_DEBUG ("setup_rgvolume");
68 element = gst_check_setup_element ("rgvolume");
69 mysrcpad = gst_check_setup_src_pad (element, &srctemplate);
70 mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate);
72 /* Capture events, to test tag filtering behavior: */
73 gst_pad_set_event_function (mysinkpad, event_func);
75 gst_pad_set_active (mysrcpad, TRUE);
76 gst_pad_set_active (mysinkpad, TRUE);
82 cleanup_rgvolume (GstElement * element)
84 GST_DEBUG ("cleanup_rgvolume");
86 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
87 g_list_free (buffers);
90 g_list_foreach (events, (GFunc) gst_mini_object_unref, NULL);
94 gst_pad_set_active (mysrcpad, FALSE);
95 gst_pad_set_active (mysinkpad, FALSE);
96 gst_check_teardown_src_pad (element);
97 gst_check_teardown_sink_pad (element);
98 gst_check_teardown_element (element);
102 set_playing_state (GstElement * element)
104 fail_unless (gst_element_set_state (element,
105 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
106 "Could not set state to PLAYING");
110 set_null_state (GstElement * element)
112 fail_unless (gst_element_set_state (element,
113 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS,
114 "Could not set state to NULL");
118 send_eos_event (GstElement * element)
120 GstEvent *event = gst_event_new_eos ();
122 fail_unless (g_list_length (events) == 0);
123 fail_unless (gst_pad_push_event (mysrcpad, event),
124 "Pushing EOS event failed");
125 fail_unless (g_list_length (events) == 1);
126 fail_unless (events->data == event);
127 gst_mini_object_unref ((GstMiniObject *) events->data);
128 events = g_list_remove (events, event);
132 send_tag_event (GstElement * element, GstEvent * event)
134 g_return_val_if_fail (event->type == GST_EVENT_TAG, NULL);
136 fail_unless (g_list_length (events) == 0);
137 fail_unless (gst_pad_push_event (mysrcpad, event),
138 "Pushing tag event failed");
140 if (g_list_length (events) == 0) {
141 /* Event got filtered out. */
144 GstTagList *tag_list;
147 event = events->data;
148 events = g_list_remove (events, event);
150 fail_unless (event->type == GST_EVENT_TAG);
151 gst_event_parse_tag (event, &tag_list);
153 /* The element is supposed to filter out ReplayGain related tags. */
154 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &dummy),
155 "tag event still contains track gain tag");
156 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &dummy),
157 "tag event still contains track peak tag");
158 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &dummy),
159 "tag event still contains album gain tag");
160 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &dummy),
161 "tag event still contains album peak tag");
168 test_buffer_new (gfloat value)
175 buf = gst_buffer_new_and_alloc (8 * sizeof (gfloat));
176 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
177 for (i = 0; i < 8; i++)
179 gst_buffer_unmap (buf, data, -1);
181 caps = gst_caps_from_string ("audio/x-raw-float, "
182 "rate = 8000, channels = 1, endianness = BYTE_ORDER, width = 32");
183 gst_pad_set_caps (mysrcpad, 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;
209 gfloat input_sample, output_sample;
210 gdouble gain, prop_gain;
211 gboolean is_passthrough, expect_passthrough;
214 fail_unless (g_list_length (buffers) == 0);
217 input_buf = test_buffer_new (input_sample);
219 /* We keep an extra reference to detect passthrough mode. */
220 gst_buffer_ref (input_buf);
221 /* Pushing steals a reference. */
222 fail_unless (gst_pad_push (mysrcpad, input_buf) == GST_FLOW_OK);
223 gst_buffer_unref (input_buf);
225 /* The output buffer ends up on the global buffer list. */
226 fail_unless (g_list_length (buffers) == 1);
227 output_buf = buffers->data;
228 fail_if (output_buf == NULL);
230 buffers = g_list_remove (buffers, output_buf);
231 ASSERT_BUFFER_REFCOUNT (output_buf, "output_buf", 1);
233 fail_unless_equals_int (gst_buffer_get_size (output_buf),
234 8 * sizeof (gfloat));
236 data = gst_buffer_map (output_buf, NULL, NULL, GST_MAP_READ);
238 output_sample = *data;
239 fail_if (output_sample == 0.0, "First output sample is zero");
240 for (i = 1; i < 8; i++) {
241 fail_unless (output_sample == data[i], "Output samples not uniform");
243 gst_buffer_unmap (output_buf, data, -1);
245 gain = 20. * log10 (output_sample / input_sample);
246 fail_unless (MATCH_GAIN (gain, expected_gain),
247 "Applied gain is %.2f dB, expected %.2f dB", gain, expected_gain);
248 g_object_get (element, "result-gain", &prop_gain, NULL);
249 fail_unless (MATCH_GAIN (prop_gain, expected_gain),
250 "Result gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
252 is_passthrough = (output_buf == input_buf);
253 expect_passthrough = MATCH_GAIN (expected_gain, +0.00);
254 fail_unless (is_passthrough == expect_passthrough,
256 ? "Expected operation in passthrough mode"
257 : "Incorrect passthrough behaviour");
259 gst_buffer_unref (output_buf);
263 fail_unless_gain (GstElement * element, gdouble expected_gain)
265 fail_unless_target_gain (element, expected_gain);
266 fail_unless_result_gain (element, expected_gain);
269 /* Start of tests. */
271 GST_START_TEST (test_no_buffer)
273 GstElement *element = setup_rgvolume ();
275 set_playing_state (element);
276 set_null_state (element);
277 set_playing_state (element);
278 send_eos_event (element);
280 cleanup_rgvolume (element);
285 GST_START_TEST (test_events)
287 GstElement *element = setup_rgvolume ();
290 GstTagList *tag_list;
293 set_playing_state (element);
295 tag_list = gst_tag_list_new_empty ();
296 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
297 GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
298 GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
299 GST_TAG_ARTIST, "Foobar", NULL);
300 event = gst_event_new_tag (tag_list);
301 new_event = send_tag_event (element, event);
302 /* Expect the element to modify the writable event. */
303 fail_unless (event == new_event, "Writable tag event not reused");
304 gst_event_parse_tag (new_event, &tag_list);
305 fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
306 fail_unless (g_str_equal (artist, "Foobar"));
308 gst_event_unref (new_event);
310 /* Same as above, but with a non-writable event. */
312 tag_list = gst_tag_list_new_empty ();
313 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
314 GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
315 GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
316 GST_TAG_ARTIST, "Foobar", NULL);
317 event = gst_event_new_tag (tag_list);
318 /* Holding an extra ref makes the event unwritable: */
319 gst_event_ref (event);
320 new_event = send_tag_event (element, event);
321 fail_unless (event != new_event, "Unwritable tag event reused");
322 gst_event_parse_tag (new_event, &tag_list);
323 fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
324 fail_unless (g_str_equal (artist, "Foobar"));
326 gst_event_unref (event);
327 gst_event_unref (new_event);
329 cleanup_rgvolume (element);
334 GST_START_TEST (test_simple)
336 GstElement *element = setup_rgvolume ();
337 GstTagList *tag_list;
339 g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
340 "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
341 set_playing_state (element);
343 tag_list = gst_tag_list_new_empty ();
344 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
345 GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
346 GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
347 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
348 fail_unless_gain (element, -9.45); /* pre-amp + track gain */
349 send_eos_event (element);
351 g_object_set (element, "album-mode", TRUE, NULL);
353 tag_list = gst_tag_list_new_empty ();
354 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
355 GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
356 GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
357 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
358 fail_unless_gain (element, -3.91); /* pre-amp + album gain */
360 /* Switching back to track mode in the middle of a stream: */
361 g_object_set (element, "album-mode", FALSE, NULL);
362 fail_unless_gain (element, -9.45); /* pre-amp + track gain */
363 send_eos_event (element);
365 cleanup_rgvolume (element);
370 /* If there are no gain tags at all, the fallback gain is used. */
372 GST_START_TEST (test_fallback_gain)
374 GstElement *element = setup_rgvolume ();
375 GstTagList *tag_list;
377 /* First some track where fallback does _not_ apply. */
379 g_object_set (element, "album-mode", FALSE, "headroom", 10.00,
380 "pre-amp", -6.00, "fallback-gain", -3.00, NULL);
381 set_playing_state (element);
383 tag_list = gst_tag_list_new_empty ();
384 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
385 GST_TAG_TRACK_GAIN, +3.5, GST_TAG_TRACK_PEAK, 1.0,
386 GST_TAG_ALBUM_GAIN, -0.5, GST_TAG_ALBUM_PEAK, 1.0, NULL);
387 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
388 fail_unless_gain (element, -2.50); /* pre-amp + track gain */
389 send_eos_event (element);
391 /* Now a track completely missing tags. */
393 fail_unless_gain (element, -9.00); /* pre-amp + fallback-gain */
395 /* Changing the fallback gain in the middle of a stream, going to pass-through
397 g_object_set (element, "fallback-gain", +6.00, NULL);
398 fail_unless_gain (element, +0.00); /* pre-amp + fallback-gain */
399 send_eos_event (element);
401 /* Verify that result gain is set to +0.00 with pre-amp + fallback-gain >
402 * +0.00 and no headroom. */
404 g_object_set (element, "fallback-gain", +12.00, "headroom", +0.00, NULL);
405 fail_unless_target_gain (element, +6.00); /* pre-amp + fallback-gain */
406 fail_unless_result_gain (element, +0.00);
407 send_eos_event (element);
409 cleanup_rgvolume (element);
414 /* If album gain is to be preferred but not available, the track gain is to be
417 GST_START_TEST (test_fallback_track)
419 GstElement *element = setup_rgvolume ();
420 GstTagList *tag_list;
422 g_object_set (element, "album-mode", TRUE, "headroom", +0.00,
423 "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
424 set_playing_state (element);
426 tag_list = gst_tag_list_new_empty ();
427 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
428 GST_TAG_TRACK_GAIN, +2.11, GST_TAG_TRACK_PEAK, 1.0, NULL);
429 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
430 fail_unless_gain (element, -3.89); /* pre-amp + track gain */
432 send_eos_event (element);
434 cleanup_rgvolume (element);
439 /* If track gain is to be preferred but not available, the album gain is to be
442 GST_START_TEST (test_fallback_album)
444 GstElement *element = setup_rgvolume ();
445 GstTagList *tag_list;
447 g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
448 "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
449 set_playing_state (element);
451 tag_list = gst_tag_list_new_empty ();
452 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
453 GST_TAG_ALBUM_GAIN, +3.73, GST_TAG_ALBUM_PEAK, 1.0, NULL);
454 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
455 fail_unless_gain (element, -2.27); /* pre-amp + album gain */
457 send_eos_event (element);
459 cleanup_rgvolume (element);
464 GST_START_TEST (test_headroom)
466 GstElement *element = setup_rgvolume ();
467 GstTagList *tag_list;
469 g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
470 "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
471 set_playing_state (element);
473 tag_list = gst_tag_list_new_empty ();
474 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
475 GST_TAG_TRACK_GAIN, +3.50, GST_TAG_TRACK_PEAK, 1.0, NULL);
476 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
477 fail_unless_target_gain (element, +3.50); /* pre-amp + track gain */
478 fail_unless_result_gain (element, +0.00);
479 send_eos_event (element);
481 g_object_set (element, "headroom", +2.00, NULL);
482 tag_list = gst_tag_list_new_empty ();
483 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
484 GST_TAG_TRACK_GAIN, +9.18, GST_TAG_TRACK_PEAK, 0.687149, NULL);
485 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
486 fail_unless_target_gain (element, +9.18); /* pre-amp + track gain */
487 /* Result is 20. * log10 (1. / peak) + headroom. */
488 fail_unless_result_gain (element, 5.2589816238303335);
489 send_eos_event (element);
491 g_object_set (element, "album-mode", TRUE, NULL);
492 tag_list = gst_tag_list_new_empty ();
493 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
494 GST_TAG_ALBUM_GAIN, +5.50, GST_TAG_ALBUM_PEAK, 1.0, NULL);
495 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
496 fail_unless_target_gain (element, +5.50); /* pre-amp + album gain */
497 fail_unless_result_gain (element, +2.00); /* headroom */
498 send_eos_event (element);
500 cleanup_rgvolume (element);
505 GST_START_TEST (test_reference_level)
507 GstElement *element = setup_rgvolume ();
508 GstTagList *tag_list;
510 g_object_set (element,
512 "headroom", +0.00, "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
513 set_playing_state (element);
515 tag_list = gst_tag_list_new_empty ();
516 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
517 GST_TAG_TRACK_GAIN, 0.00, GST_TAG_TRACK_PEAK, 0.2,
518 GST_TAG_REFERENCE_LEVEL, 83., NULL);
519 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
520 /* Because our authorative reference is 89 dB, we bump it up by +6 dB. */
521 fail_unless_gain (element, +6.00); /* pre-amp + track gain */
522 send_eos_event (element);
524 g_object_set (element, "album-mode", TRUE, NULL);
526 /* Same as above, but with album gain. */
528 tag_list = gst_tag_list_new_empty ();
529 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
530 GST_TAG_TRACK_GAIN, 1.23, GST_TAG_TRACK_PEAK, 0.1,
531 GST_TAG_ALBUM_GAIN, 0.00, GST_TAG_ALBUM_PEAK, 0.2,
532 GST_TAG_REFERENCE_LEVEL, 83., NULL);
533 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
534 fail_unless_gain (element, +6.00); /* pre-amp + album gain */
536 cleanup_rgvolume (element);
542 rgvolume_suite (void)
544 Suite *s = suite_create ("rgvolume");
545 TCase *tc_chain = tcase_create ("general");
547 suite_add_tcase (s, tc_chain);
549 tcase_add_test (tc_chain, test_no_buffer);
550 tcase_add_test (tc_chain, test_events);
551 tcase_add_test (tc_chain, test_simple);
552 tcase_add_test (tc_chain, test_fallback_gain);
553 tcase_add_test (tc_chain, test_fallback_track);
554 tcase_add_test (tc_chain, test_fallback_album);
555 tcase_add_test (tc_chain, test_headroom);
556 tcase_add_test (tc_chain, test_reference_level);
562 main (int argc, char **argv)
566 Suite *s = rgvolume_suite ();
567 SRunner *sr = srunner_create (s);
569 gst_check_init (&argc, &argv);
571 srunner_run_all (sr, CK_ENV);
572 nf = srunner_ntests_failed (sr);