1 /* GStreamer ReplayGain analysis
3 * Copyright (C) 2006 Rene Stadler <mail@renestadler.de>
5 * rganalysis.c: Unit test for the rganalysis 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 /* Some things to note about the RMS window length of the analysis algorithm and
24 * thus the implementation used in the element: Processing divides input data
25 * into 50ms windows at some point. Some details about this that normally do
28 * 1. At the end of a stream, the remainder of data that did not fill up the
29 * last 50ms window is simply discarded.
31 * 2. If the sample rate changes during a stream, the currently running window
32 * is discarded and the equal loudness filter gets reset as if a new stream
35 * 3. For the album gain, it is not entirely correct to think of obtaining it
36 * like "as if all the tracks are analyzed as one track". There isn't a
37 * separate window being tracked for album processing, so at stream (track)
38 * end, the remaining unfilled window does not contribute to the album gain
41 * 4. If a waveform with a result gain G is concatenated to itself and the
42 * result processed as a track, the gain can be different from G if and only
43 * if the duration of the original waveform is not an integer multiple of
44 * 50ms. If the original waveform gets processed as a single track and then
45 * the same data again as a subsequent track, the album result gain will
46 * always match G (this is implied by 3.).
48 * 5. A stream shorter than 50ms cannot be analyzed. At 8000 and 48000 Hz,
49 * this corresponds to 400 resp. 2400 frames. If a stream is shorter than
50 * 50ms, the element will not generate tags at EOS (only if an album
51 * finished, but only album tags are generated then). This is not an
52 * erroneous condition, the element should behave normally.
54 * The limitations outlined in 1.-4. do not apply to the peak values. Every
55 * single sample is accounted for when looking for the peak. Thus the album
56 * peak is guaranteed to be the maximum value of all track peaks.
58 * In normal day-to-day use, these little facts are unlikely to be relevant, but
59 * they have to be kept in mind for writing the tests here.
62 #include <gst/check/gstcheck.h>
64 /* For ease of programming we use globals to keep refs for our floating src and
65 * sink pads we create; otherwise we always have to do get_pad, get_peer, and
66 * then remove references in every test function */
67 static GstPad *mysrcpad, *mysinkpad;
69 /* Mapping from supported sample rates to the correct result gain for the
70 * following test waveform: 20 * 512 samples with a quarter-full amplitude of
71 * toggling sign, changing every 48 samples and starting with the positive
74 * Even if we would generate a wave describing a signal with the same frequency
75 * at each sampling rate, the results would vary (slightly). Hence the simple
76 * generation method, since we cannot use a constant value as expected result
77 * anyways. For all sample rates, changing the sign every 48 frames gives a
78 * sane frequency. Buffers containing data that forms such a waveform is
79 * created using the test_buffer_square_{float,int16}_{mono,stereo} functions
82 * The results have been checked against what the metaflac and wavegain programs
83 * generate for such a stream. If you want to verify these, be sure that the
84 * metaflac program does not produce incorrect results in your environment: I
85 * found a strange bug in the (defacto) reference code for the analysis that
86 * sometimes leads to incorrect RMS window lengths. */
94 static const struct rate_test supported_rates[] = {
106 /* Lookup the correct gain adjustment result in above array. */
109 get_expected_gain (guint sample_rate)
113 for (i = G_N_ELEMENTS (supported_rates); i--;)
114 if (supported_rates[i].sample_rate == sample_rate)
115 return supported_rates[i].gain;
116 g_return_val_if_reached (0.0);
119 #define SILENCE_GAIN 64.82
121 #define REPLAY_GAIN_CAPS \
122 "channels = (int) { 1, 2 }, " \
123 "rate = (int) { 8000, 11025, 12000, 16000, 22050, " \
124 "24000, 32000, 44100, 48000 }"
126 #define RG_ANALYSIS_CAPS_TEMPLATE_STRING \
127 "audio/x-raw-float, " \
128 "width = (int) 32, " \
129 "endianness = (int) BYTE_ORDER, " \
132 "audio/x-raw-int, " \
133 "width = (int) 16, " \
134 "depth = (int) [ 1, 16 ], " \
135 "signed = (boolean) true, " \
136 "endianness = (int) BYTE_ORDER, " \
139 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
142 GST_STATIC_CAPS (RG_ANALYSIS_CAPS_TEMPLATE_STRING)
144 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
147 GST_STATIC_CAPS (RG_ANALYSIS_CAPS_TEMPLATE_STRING)
151 setup_rganalysis (void)
153 GstElement *analysis;
156 GST_DEBUG ("setup_rganalysis");
157 analysis = gst_check_setup_element ("rganalysis");
158 mysrcpad = gst_check_setup_src_pad (analysis, &srctemplate, NULL);
159 mysinkpad = gst_check_setup_sink_pad (analysis, &sinktemplate, NULL);
160 gst_pad_set_active (mysrcpad, TRUE);
161 gst_pad_set_active (mysinkpad, TRUE);
163 bus = gst_bus_new ();
164 gst_element_set_bus (analysis, bus);
165 /* gst_element_set_bus does not steal a reference. */
166 gst_object_unref (bus);
172 cleanup_rganalysis (GstElement * element)
174 GST_DEBUG ("cleanup_rganalysis");
176 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
177 g_list_free (buffers);
180 /* The bus owns references to the element: */
181 gst_element_set_bus (element, NULL);
183 gst_pad_set_active (mysrcpad, FALSE);
184 gst_pad_set_active (mysinkpad, FALSE);
185 gst_check_teardown_src_pad (element);
186 gst_check_teardown_sink_pad (element);
187 gst_check_teardown_element (element);
191 set_playing_state (GstElement * element)
193 fail_unless (gst_element_set_state (element,
194 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
195 "Could not set state to PLAYING");
199 send_eos_event (GstElement * element)
201 GstBus *bus = gst_element_get_bus (element);
202 GstPad *pad = gst_element_get_static_pad (element, "sink");
203 GstEvent *event = gst_event_new_eos ();
205 fail_unless (gst_pad_send_event (pad, event),
206 "Cannot send EOS event: Not handled.");
208 /* There is no sink element, so _we_ post the EOS message on the bus here. Of
209 * course we generate any EOS ourselves, but this allows us to poll for the
210 * EOS message in poll_eos if we expect the element to _not_ generate a TAG
211 * message. That's better than waiting for a timeout to lapse. */
212 fail_unless (gst_bus_post (bus, gst_message_new_eos (NULL)));
214 gst_object_unref (bus);
215 gst_object_unref (pad);
219 send_tag_event (GstElement * element, GstTagList * tag_list)
221 GstPad *pad = gst_element_get_static_pad (element, "sink");
222 GstEvent *event = gst_event_new_tag (tag_list);
224 fail_unless (gst_pad_send_event (pad, event),
225 "Cannot send TAG event: Not handled.");
227 gst_object_unref (pad);
231 poll_eos (GstElement * element)
233 GstBus *bus = gst_element_get_bus (element);
236 message = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_TAG, GST_SECOND);
237 fail_unless (message != NULL, "Could not poll for EOS message: Timed out");
238 fail_unless (message->type == GST_MESSAGE_EOS,
239 "Could not poll for eos message: got message of type %s instead",
240 gst_message_type_get_name (message->type));
242 gst_message_unref (message);
243 gst_object_unref (bus);
246 /* This also polls for EOS since the TAG message comes right before the end of
250 poll_tags (GstElement * element)
252 GstBus *bus = gst_element_get_bus (element);
253 GstTagList *tag_list;
256 message = gst_bus_poll (bus, GST_MESSAGE_TAG, GST_SECOND);
257 fail_unless (message != NULL, "Could not poll for TAG message: Timed out");
259 gst_message_parse_tag (message, &tag_list);
260 gst_message_unref (message);
261 gst_object_unref (bus);
268 #define MATCH_PEAK(p1, p2) ((p1 < p2 + 1e-6) && (p2 < p1 + 1e-6))
269 #define MATCH_GAIN(g1, g2) ((g1 < g2 + 1e-13) && (g2 < g1 + 1e-13))
272 fail_unless_track_gain (const GstTagList * tag_list, gdouble gain)
276 fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &result),
277 "Tag list contains no track gain value");
278 fail_unless (MATCH_GAIN (gain, result),
279 "Track gain %+.2f does not match, expected %+.2f", result, gain);
283 fail_unless_track_peak (const GstTagList * tag_list, gdouble peak)
287 fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &result),
288 "Tag list contains no track peak value");
289 fail_unless (MATCH_PEAK (peak, result),
290 "Track peak %f does not match, expected %f", result, peak);
294 fail_unless_album_gain (const GstTagList * tag_list, gdouble gain)
298 fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &result),
299 "Tag list contains no album gain value");
300 fail_unless (MATCH_GAIN (result, gain),
301 "Album gain %+.2f does not match, expected %+.2f", result, gain);
305 fail_unless_album_peak (const GstTagList * tag_list, gdouble peak)
309 fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &result),
310 "Tag list contains no album peak value");
311 fail_unless (MATCH_PEAK (peak, result),
312 "Album peak %f does not match, expected %f", result, peak);
316 fail_if_track_tags (const GstTagList * tag_list)
320 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &result),
321 "Tag list contains track gain value (but should not)");
322 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &result),
323 "Tag list contains track peak value (but should not)");
327 fail_if_album_tags (const GstTagList * tag_list)
331 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &result),
332 "Tag list contains album gain value (but should not)");
333 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &result),
334 "Tag list contains album peak value (but should not)");
338 fail_unless_num_tracks (GstElement * element, guint num_tracks)
342 g_object_get (element, "num-tracks", ¤t, NULL);
343 fail_unless (current == num_tracks,
344 "num-tracks property has incorrect value %u, expected %u",
345 current, num_tracks);
348 /* Functions that create buffers with constant sample values, for peak
352 test_buffer_const_float_mono (gint sample_rate, gsize n_frames, gfloat value)
354 GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat));
355 gfloat *data = (gfloat *) GST_BUFFER_DATA (buf);
359 for (i = n_frames; i--;)
362 caps = gst_caps_new_simple ("audio/x-raw-float",
363 "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1,
364 "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
365 gst_buffer_set_caps (buf, caps);
366 gst_caps_unref (caps);
368 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
374 test_buffer_const_float_stereo (gint sample_rate, gsize n_frames,
375 gfloat value_l, gfloat value_r)
377 GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat) * 2);
378 gfloat *data = (gfloat *) GST_BUFFER_DATA (buf);
382 for (i = n_frames; i--;) {
387 caps = gst_caps_new_simple ("audio/x-raw-float",
388 "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2,
389 "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
390 gst_buffer_set_caps (buf, caps);
391 gst_caps_unref (caps);
393 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
399 test_buffer_const_int16_mono (gint sample_rate, gint depth, gsize n_frames,
402 GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16));
403 gint16 *data = (gint16 *) GST_BUFFER_DATA (buf);
407 for (i = n_frames; i--;)
410 caps = gst_caps_new_simple ("audio/x-raw-int",
411 "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1,
412 "endianness", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE,
413 "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL);
414 gst_buffer_set_caps (buf, caps);
415 gst_caps_unref (caps);
417 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
423 test_buffer_const_int16_stereo (gint sample_rate, gint depth, gsize n_frames,
424 gint16 value_l, gint16 value_r)
426 GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16) * 2);
427 gint16 *data = (gint16 *) GST_BUFFER_DATA (buf);
431 for (i = n_frames; i--;) {
436 caps = gst_caps_new_simple ("audio/x-raw-int",
437 "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2,
438 "endianness", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE,
439 "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL);
440 gst_buffer_set_caps (buf, caps);
441 gst_caps_unref (caps);
443 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
448 /* Functions that create data buffers containing square signal
452 test_buffer_square_float_mono (gint * accumulator, gint sample_rate,
453 gsize n_frames, gfloat value)
455 GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat));
456 gfloat *data = (gfloat *) GST_BUFFER_DATA (buf);
460 for (i = n_frames; i--;) {
464 if (*accumulator < 48)
470 caps = gst_caps_new_simple ("audio/x-raw-float",
471 "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1,
472 "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
473 gst_buffer_set_caps (buf, caps);
474 gst_caps_unref (caps);
476 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
482 test_buffer_square_float_stereo (gint * accumulator, gint sample_rate,
483 gsize n_frames, gfloat value_l, gfloat value_r)
485 GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat) * 2);
486 gfloat *data = (gfloat *) GST_BUFFER_DATA (buf);
490 for (i = n_frames; i--;) {
494 if (*accumulator < 48) {
503 caps = gst_caps_new_simple ("audio/x-raw-float",
504 "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2,
505 "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
506 gst_buffer_set_caps (buf, caps);
507 gst_caps_unref (caps);
509 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
515 test_buffer_square_int16_mono (gint * accumulator, gint sample_rate,
516 gint depth, gsize n_frames, gint16 value)
518 GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16));
519 gint16 *data = (gint16 *) GST_BUFFER_DATA (buf);
523 for (i = n_frames; i--;) {
527 if (*accumulator < 48)
530 *data++ = -MAX (value, -32767);
533 caps = gst_caps_new_simple ("audio/x-raw-int",
534 "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1,
535 "endianness", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE,
536 "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL);
537 gst_buffer_set_caps (buf, caps);
538 gst_caps_unref (caps);
540 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
546 test_buffer_square_int16_stereo (gint * accumulator, gint sample_rate,
547 gint depth, gsize n_frames, gint16 value_l, gint16 value_r)
549 GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16) * 2);
550 gint16 *data = (gint16 *) GST_BUFFER_DATA (buf);
554 for (i = n_frames; i--;) {
558 if (*accumulator < 48) {
562 *data++ = -MAX (value_l, -32767);
563 *data++ = -MAX (value_r, -32767);
567 caps = gst_caps_new_simple ("audio/x-raw-int",
568 "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2,
569 "endianness", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE,
570 "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL);
571 gst_buffer_set_caps (buf, caps);
572 gst_caps_unref (caps);
574 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
580 push_buffer (GstBuffer * buf)
582 /* gst_pad_push steals a reference. */
583 fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
584 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
587 /*** Start of the tests. ***/
589 /* This test looks redundant, but early versions of the element
590 * crashed when doing, well, nothing: */
592 GST_START_TEST (test_no_buffer)
594 GstElement *element = setup_rganalysis ();
596 set_playing_state (element);
597 send_eos_event (element);
600 cleanup_rganalysis (element);
605 GST_START_TEST (test_no_buffer_album_1)
607 GstElement *element = setup_rganalysis ();
609 set_playing_state (element);
612 send_eos_event (element);
616 g_object_set (element, "num-tracks", 3, NULL);
618 send_eos_event (element);
620 fail_unless_num_tracks (element, 2);
622 send_eos_event (element);
624 fail_unless_num_tracks (element, 1);
626 send_eos_event (element);
628 fail_unless_num_tracks (element, 0);
631 g_object_set (element, "num-tracks", 2, NULL);
633 send_eos_event (element);
635 fail_unless_num_tracks (element, 1);
637 send_eos_event (element);
639 fail_unless_num_tracks (element, 0);
642 send_eos_event (element);
644 fail_unless_num_tracks (element, 0);
646 cleanup_rganalysis (element);
651 GST_START_TEST (test_no_buffer_album_2)
653 GstElement *element = setup_rganalysis ();
654 GstTagList *tag_list;
655 gint accumulator = 0;
658 g_object_set (element, "num-tracks", 3, NULL);
659 set_playing_state (element);
661 /* No buffer for the first track. */
663 send_eos_event (element);
664 /* No tags should be posted, there was nothing to analyze: */
666 fail_unless_num_tracks (element, 2);
668 /* A test waveform with known gain result as second track: */
671 push_buffer (test_buffer_square_float_mono (&accumulator, 44100, 512,
673 send_eos_event (element);
674 tag_list = poll_tags (element);
675 fail_unless_track_peak (tag_list, 0.25);
676 fail_unless_track_gain (tag_list, -6.20);
677 /* Album is not finished yet: */
678 fail_if_album_tags (tag_list);
679 gst_tag_list_free (tag_list);
680 fail_unless_num_tracks (element, 1);
682 /* No buffer for the last track. */
684 send_eos_event (element);
686 tag_list = poll_tags (element);
687 fail_unless_album_peak (tag_list, 0.25);
688 fail_unless_album_gain (tag_list, -6.20);
689 /* No track tags should be posted, as there was no data for it: */
690 fail_if_track_tags (tag_list);
691 gst_tag_list_free (tag_list);
692 fail_unless_num_tracks (element, 0);
694 cleanup_rganalysis (element);
699 GST_START_TEST (test_empty_buffers)
701 GstElement *element = setup_rganalysis ();
703 set_playing_state (element);
706 push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0));
707 send_eos_event (element);
711 g_object_set (element, "num-tracks", 2, NULL);
713 push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0));
714 send_eos_event (element);
716 fail_unless_num_tracks (element, 1);
718 push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0));
719 send_eos_event (element);
721 fail_unless_num_tracks (element, 0);
723 /* Second album, with a single track: */
724 g_object_set (element, "num-tracks", 1, NULL);
725 push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0));
726 send_eos_event (element);
728 fail_unless_num_tracks (element, 0);
731 push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0));
732 send_eos_event (element);
735 cleanup_rganalysis (element);
740 /* Tests for correctness of the peak values. */
742 /* Float peak test. For stereo, one channel has the constant value of -1.369,
743 * the other one 0.0. This tests many things: The result peak value should
744 * occur on any channel. The peak is of course the absolute amplitude, so 1.369
745 * should be the result. This will also detect if the code uses the absolute
746 * value during the comparison. If it is buggy it will return 0.0 since 0.0 >
747 * -1.369. Furthermore, this makes sure that there is no problem with headroom
748 * (exceeding 0dBFS). In the wild you get float samples > 1.0 from stuff like
751 GST_START_TEST (test_peak_float)
753 GstElement *element = setup_rganalysis ();
754 GstTagList *tag_list;
756 set_playing_state (element);
757 push_buffer (test_buffer_const_float_stereo (8000, 512, -1.369, 0.0));
758 send_eos_event (element);
759 tag_list = poll_tags (element);
760 fail_unless_track_peak (tag_list, 1.369);
761 gst_tag_list_free (tag_list);
763 /* Swapped channels. */
764 push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, -1.369));
765 send_eos_event (element);
766 tag_list = poll_tags (element);
767 fail_unless_track_peak (tag_list, 1.369);
768 gst_tag_list_free (tag_list);
771 push_buffer (test_buffer_const_float_mono (8000, 512, -1.369));
772 send_eos_event (element);
773 tag_list = poll_tags (element);
774 fail_unless_track_peak (tag_list, 1.369);
775 gst_tag_list_free (tag_list);
777 cleanup_rganalysis (element);
782 GST_START_TEST (test_peak_int16_16)
784 GstElement *element = setup_rganalysis ();
785 GstTagList *tag_list;
787 set_playing_state (element);
789 /* Half amplitude. */
790 push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 1 << 14, 0));
791 send_eos_event (element);
792 tag_list = poll_tags (element);
793 fail_unless_track_peak (tag_list, 0.5);
794 gst_tag_list_free (tag_list);
796 /* Swapped channels. */
797 push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, 1 << 14));
798 send_eos_event (element);
799 tag_list = poll_tags (element);
800 fail_unless_track_peak (tag_list, 0.5);
801 gst_tag_list_free (tag_list);
804 push_buffer (test_buffer_const_int16_mono (8000, 16, 512, 1 << 14));
805 send_eos_event (element);
806 tag_list = poll_tags (element);
807 fail_unless_track_peak (tag_list, 0.5);
808 gst_tag_list_free (tag_list);
810 /* Half amplitude, negative variant. */
811 push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, -1 << 14, 0));
812 send_eos_event (element);
813 tag_list = poll_tags (element);
814 fail_unless_track_peak (tag_list, 0.5);
815 gst_tag_list_free (tag_list);
817 /* Swapped channels. */
818 push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, -1 << 14));
819 send_eos_event (element);
820 tag_list = poll_tags (element);
821 fail_unless_track_peak (tag_list, 0.5);
822 gst_tag_list_free (tag_list);
825 push_buffer (test_buffer_const_int16_mono (8000, 16, 512, -1 << 14));
826 send_eos_event (element);
827 tag_list = poll_tags (element);
828 fail_unless_track_peak (tag_list, 0.5);
829 gst_tag_list_free (tag_list);
832 /* Now check for correct normalization of the peak value: Sample
833 * values of this format range from -32768 to 32767. So for the
834 * highest positive amplitude we do not reach 1.0, only for
837 push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 32767, 0));
838 send_eos_event (element);
839 tag_list = poll_tags (element);
840 fail_unless_track_peak (tag_list, 32767. / 32768.);
841 gst_tag_list_free (tag_list);
843 /* Swapped channels. */
844 push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, 32767));
845 send_eos_event (element);
846 tag_list = poll_tags (element);
847 fail_unless_track_peak (tag_list, 32767. / 32768.);
848 gst_tag_list_free (tag_list);
851 push_buffer (test_buffer_const_int16_mono (8000, 16, 512, 32767));
852 send_eos_event (element);
853 tag_list = poll_tags (element);
854 fail_unless_track_peak (tag_list, 32767. / 32768.);
855 gst_tag_list_free (tag_list);
858 /* Negative variant, reaching 1.0. */
859 push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, -32768, 0));
860 send_eos_event (element);
861 tag_list = poll_tags (element);
862 fail_unless_track_peak (tag_list, 1.0);
863 gst_tag_list_free (tag_list);
865 /* Swapped channels. */
866 push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, -32768));
867 send_eos_event (element);
868 tag_list = poll_tags (element);
869 fail_unless_track_peak (tag_list, 1.0);
870 gst_tag_list_free (tag_list);
873 push_buffer (test_buffer_const_int16_mono (8000, 16, 512, -32768));
874 send_eos_event (element);
875 tag_list = poll_tags (element);
876 fail_unless_track_peak (tag_list, 1.0);
877 gst_tag_list_free (tag_list);
879 cleanup_rganalysis (element);
884 /* Same as the test before, but with 8 bits (packed into 16 bits). */
886 GST_START_TEST (test_peak_int16_8)
888 GstElement *element = setup_rganalysis ();
889 GstTagList *tag_list;
891 set_playing_state (element);
893 /* Half amplitude. */
894 push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 1 << 6, 0));
895 send_eos_event (element);
896 tag_list = poll_tags (element);
897 fail_unless_track_peak (tag_list, 0.5);
898 gst_tag_list_free (tag_list);
900 /* Swapped channels. */
901 push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, 1 << 6));
902 send_eos_event (element);
903 tag_list = poll_tags (element);
904 fail_unless_track_peak (tag_list, 0.5);
905 gst_tag_list_free (tag_list);
908 push_buffer (test_buffer_const_int16_mono (8000, 8, 512, 1 << 6));
909 send_eos_event (element);
910 tag_list = poll_tags (element);
911 fail_unless_track_peak (tag_list, 0.5);
912 gst_tag_list_free (tag_list);
915 /* Half amplitude, negative variant. */
916 push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, -1 << 6, 0));
917 send_eos_event (element);
918 tag_list = poll_tags (element);
919 fail_unless_track_peak (tag_list, 0.5);
920 gst_tag_list_free (tag_list);
922 /* Swapped channels. */
923 push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, -1 << 6));
924 send_eos_event (element);
925 tag_list = poll_tags (element);
926 fail_unless_track_peak (tag_list, 0.5);
927 gst_tag_list_free (tag_list);
930 push_buffer (test_buffer_const_int16_mono (8000, 8, 512, -1 << 6));
931 send_eos_event (element);
932 tag_list = poll_tags (element);
933 fail_unless_track_peak (tag_list, 0.5);
934 gst_tag_list_free (tag_list);
937 /* Almost full amplitude (maximum positive value). */
938 push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, (1 << 7) - 1, 0));
939 send_eos_event (element);
940 tag_list = poll_tags (element);
941 fail_unless_track_peak (tag_list, 0.9921875);
942 gst_tag_list_free (tag_list);
944 /* Swapped channels. */
945 push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, (1 << 7) - 1));
946 send_eos_event (element);
947 tag_list = poll_tags (element);
948 fail_unless_track_peak (tag_list, 0.9921875);
949 gst_tag_list_free (tag_list);
952 push_buffer (test_buffer_const_int16_mono (8000, 8, 512, (1 << 7) - 1));
953 send_eos_event (element);
954 tag_list = poll_tags (element);
955 fail_unless_track_peak (tag_list, 0.9921875);
956 gst_tag_list_free (tag_list);
959 /* Full amplitude (maximum negative value). */
960 push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, -1 << 7, 0));
961 send_eos_event (element);
962 tag_list = poll_tags (element);
963 fail_unless_track_peak (tag_list, 1.0);
964 gst_tag_list_free (tag_list);
966 /* Swapped channels. */
967 push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, -1 << 7));
968 send_eos_event (element);
969 tag_list = poll_tags (element);
970 fail_unless_track_peak (tag_list, 1.0);
971 gst_tag_list_free (tag_list);
974 push_buffer (test_buffer_const_int16_mono (8000, 8, 512, -1 << 7));
975 send_eos_event (element);
976 tag_list = poll_tags (element);
977 fail_unless_track_peak (tag_list, 1.0);
978 gst_tag_list_free (tag_list);
980 cleanup_rganalysis (element);
985 GST_START_TEST (test_peak_album)
987 GstElement *element = setup_rganalysis ();
988 GstTagList *tag_list;
990 g_object_set (element, "num-tracks", 2, NULL);
991 set_playing_state (element);
993 push_buffer (test_buffer_const_float_stereo (8000, 1024, 1.0, 0.0));
994 send_eos_event (element);
995 tag_list = poll_tags (element);
996 fail_unless_track_peak (tag_list, 1.0);
997 fail_if_album_tags (tag_list);
998 gst_tag_list_free (tag_list);
999 fail_unless_num_tracks (element, 1);
1001 push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.0, 0.5));
1002 send_eos_event (element);
1003 tag_list = poll_tags (element);
1004 fail_unless_track_peak (tag_list, 0.5);
1005 fail_unless_album_peak (tag_list, 1.0);
1006 gst_tag_list_free (tag_list);
1007 fail_unless_num_tracks (element, 0);
1009 /* Try a second album: */
1010 g_object_set (element, "num-tracks", 3, NULL);
1012 push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.4, 0.4));
1013 send_eos_event (element);
1014 tag_list = poll_tags (element);
1015 fail_unless_track_peak (tag_list, 0.4);
1016 fail_if_album_tags (tag_list);
1017 gst_tag_list_free (tag_list);
1018 fail_unless_num_tracks (element, 2);
1020 push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.45, 0.45));
1021 send_eos_event (element);
1022 tag_list = poll_tags (element);
1023 fail_unless_track_peak (tag_list, 0.45);
1024 fail_if_album_tags (tag_list);
1025 gst_tag_list_free (tag_list);
1026 fail_unless_num_tracks (element, 1);
1028 push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.2, 0.2));
1029 send_eos_event (element);
1030 tag_list = poll_tags (element);
1031 fail_unless_track_peak (tag_list, 0.2);
1032 fail_unless_album_peak (tag_list, 0.45);
1033 gst_tag_list_free (tag_list);
1034 fail_unless_num_tracks (element, 0);
1036 /* And now a single track, not in album mode (num-tracks is 0
1038 push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.1, 0.1));
1039 send_eos_event (element);
1040 tag_list = poll_tags (element);
1041 fail_unless_track_peak (tag_list, 0.1);
1042 fail_if_album_tags (tag_list);
1043 gst_tag_list_free (tag_list);
1045 cleanup_rganalysis (element);
1050 /* Switching from track to album mode. */
1052 GST_START_TEST (test_peak_track_album)
1054 GstElement *element = setup_rganalysis ();
1055 GstTagList *tag_list;
1057 set_playing_state (element);
1059 push_buffer (test_buffer_const_float_mono (8000, 1024, 1.0));
1060 send_eos_event (element);
1061 tag_list = poll_tags (element);
1062 fail_unless_track_peak (tag_list, 1.0);
1063 fail_if_album_tags (tag_list);
1064 gst_tag_list_free (tag_list);
1066 g_object_set (element, "num-tracks", 1, NULL);
1067 push_buffer (test_buffer_const_float_mono (8000, 1024, 0.5));
1068 send_eos_event (element);
1069 tag_list = poll_tags (element);
1070 fail_unless_track_peak (tag_list, 0.5);
1071 fail_unless_album_peak (tag_list, 0.5);
1072 gst_tag_list_free (tag_list);
1073 fail_unless_num_tracks (element, 0);
1075 cleanup_rganalysis (element);
1080 /* Disabling album processing before the end of the album. Probably a rare edge
1081 * case and applications should not rely on this to work. They need to send the
1082 * element to the READY state to clear up after an aborted album anyway since
1083 * they might need to process another album afterwards. */
1085 GST_START_TEST (test_peak_album_abort_to_track)
1087 GstElement *element = setup_rganalysis ();
1088 GstTagList *tag_list;
1090 g_object_set (element, "num-tracks", 2, NULL);
1091 set_playing_state (element);
1093 push_buffer (test_buffer_const_float_stereo (8000, 1024, 1.0, 0.0));
1094 send_eos_event (element);
1095 tag_list = poll_tags (element);
1096 fail_unless_track_peak (tag_list, 1.0);
1097 fail_if_album_tags (tag_list);
1098 gst_tag_list_free (tag_list);
1099 fail_unless_num_tracks (element, 1);
1101 g_object_set (element, "num-tracks", 0, NULL);
1103 push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.0, 0.5));
1104 send_eos_event (element);
1105 tag_list = poll_tags (element);
1106 fail_unless_track_peak (tag_list, 0.5);
1107 fail_if_album_tags (tag_list);
1108 gst_tag_list_free (tag_list);
1110 cleanup_rganalysis (element);
1115 GST_START_TEST (test_gain_album)
1117 GstElement *element = setup_rganalysis ();
1118 GstTagList *tag_list;
1122 g_object_set (element, "num-tracks", 3, NULL);
1123 set_playing_state (element);
1125 /* The three tracks are constructed such that if any of these is in fact
1126 * ignored for the album gain, the album gain will differ. */
1130 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1132 send_eos_event (element);
1133 tag_list = poll_tags (element);
1134 fail_unless_track_peak (tag_list, 0.75);
1135 fail_unless_track_gain (tag_list, -15.70);
1136 fail_if_album_tags (tag_list);
1137 gst_tag_list_free (tag_list);
1141 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1143 send_eos_event (element);
1144 tag_list = poll_tags (element);
1145 fail_unless_track_peak (tag_list, 0.5);
1146 fail_unless_track_gain (tag_list, -12.22);
1147 fail_if_album_tags (tag_list);
1148 gst_tag_list_free (tag_list);
1152 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1154 send_eos_event (element);
1156 tag_list = poll_tags (element);
1157 fail_unless_track_peak (tag_list, 0.25);
1158 fail_unless_track_gain (tag_list, -6.20);
1159 fail_unless_album_peak (tag_list, 0.75);
1160 /* Strangely, wavegain reports -12.17 for the album, but the fixed
1161 * metaflac agrees to us. Could be a 32767 vs. 32768 issue. */
1162 fail_unless_album_gain (tag_list, -12.18);
1163 gst_tag_list_free (tag_list);
1165 cleanup_rganalysis (element);
1170 /* Checks ensuring that the "forced" property works as advertised. */
1172 GST_START_TEST (test_forced)
1174 GstElement *element = setup_rganalysis ();
1175 GstTagList *tag_list;
1176 gint accumulator = 0;
1179 g_object_set (element, "forced", FALSE, NULL);
1180 set_playing_state (element);
1182 tag_list = gst_tag_list_new ();
1183 /* Provided values are totally arbitrary. */
1184 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
1185 GST_TAG_TRACK_PEAK, 1.0, GST_TAG_TRACK_GAIN, 2.21, NULL);
1186 send_tag_event (element, tag_list);
1189 push_buffer (test_buffer_const_float_stereo (44100, 512, 0.5, 0.5));
1190 send_eos_event (element);
1191 /* This fails if a tag message is generated: */
1194 /* Now back to a track without tags. */
1197 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1199 send_eos_event (element);
1200 tag_list = poll_tags (element);
1201 fail_unless_track_peak (tag_list, 0.25);
1202 fail_unless_track_gain (tag_list, get_expected_gain (44100));
1203 gst_tag_list_free (tag_list);
1205 cleanup_rganalysis (element);
1210 /* Sending track gain and peak in separate tag lists. */
1212 GST_START_TEST (test_forced_separate)
1214 GstElement *element = setup_rganalysis ();
1215 GstTagList *tag_list;
1216 gint accumulator = 0;
1219 g_object_set (element, "forced", FALSE, NULL);
1220 set_playing_state (element);
1222 tag_list = gst_tag_list_new ();
1223 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_GAIN, 2.21,
1225 send_tag_event (element, tag_list);
1227 tag_list = gst_tag_list_new ();
1228 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_PEAK, 1.0,
1230 send_tag_event (element, tag_list);
1233 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1235 send_eos_event (element);
1236 /* This fails if a tag message is generated: */
1239 /* Now a track without tags. */
1243 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1245 send_eos_event (element);
1246 tag_list = poll_tags (element);
1247 fail_unless_track_peak (tag_list, 0.25);
1248 fail_unless_track_gain (tag_list, get_expected_gain (44100));
1249 fail_if_album_tags (tag_list);
1250 gst_tag_list_free (tag_list);
1252 cleanup_rganalysis (element);
1257 /* A TAG event is sent _after_ data has already been processed. In real
1258 * pipelines, this could happen if there is more than one rganalysis element (by
1259 * accident). While it would have analyzed all the data prior to receiving the
1260 * event, I expect it to not post its results if not forced. This test is
1261 * almost equivalent to test_forced. */
1263 GST_START_TEST (test_forced_after_data)
1265 GstElement *element = setup_rganalysis ();
1266 GstTagList *tag_list;
1267 gint accumulator = 0;
1270 g_object_set (element, "forced", FALSE, NULL);
1271 set_playing_state (element);
1274 push_buffer (test_buffer_const_float_stereo (8000, 512, 0.5, 0.5));
1276 tag_list = gst_tag_list_new ();
1277 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
1278 GST_TAG_TRACK_PEAK, 1.0, GST_TAG_TRACK_GAIN, 2.21, NULL);
1279 send_tag_event (element, tag_list);
1281 send_eos_event (element);
1284 /* Now back to a normal track, this one has no tags: */
1286 push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25,
1288 send_eos_event (element);
1289 tag_list = poll_tags (element);
1290 fail_unless_track_peak (tag_list, 0.25);
1291 fail_unless_track_gain (tag_list, get_expected_gain (8000));
1292 gst_tag_list_free (tag_list);
1294 cleanup_rganalysis (element);
1299 /* Like test_forced, but *analyze* an album afterwards. The two tests following
1300 * this one check the *skipping* of albums. */
1302 GST_START_TEST (test_forced_album)
1304 GstElement *element = setup_rganalysis ();
1305 GstTagList *tag_list;
1309 g_object_set (element, "forced", FALSE, NULL);
1310 set_playing_state (element);
1312 tag_list = gst_tag_list_new ();
1313 /* Provided values are totally arbitrary. */
1314 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
1315 GST_TAG_TRACK_PEAK, 1.0, GST_TAG_TRACK_GAIN, 2.21, NULL);
1316 send_tag_event (element, tag_list);
1320 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1322 send_eos_event (element);
1323 /* This fails if a tag message is generated: */
1326 /* Now an album without tags. */
1327 g_object_set (element, "num-tracks", 2, NULL);
1331 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1333 send_eos_event (element);
1334 tag_list = poll_tags (element);
1335 fail_unless_track_peak (tag_list, 0.25);
1336 fail_unless_track_gain (tag_list, get_expected_gain (44100));
1337 fail_if_album_tags (tag_list);
1338 gst_tag_list_free (tag_list);
1339 fail_unless_num_tracks (element, 1);
1343 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1345 send_eos_event (element);
1346 tag_list = poll_tags (element);
1347 fail_unless_track_peak (tag_list, 0.25);
1348 fail_unless_track_gain (tag_list, get_expected_gain (44100));
1349 fail_unless_album_peak (tag_list, 0.25);
1350 fail_unless_album_gain (tag_list, get_expected_gain (44100));
1351 gst_tag_list_free (tag_list);
1352 fail_unless_num_tracks (element, 0);
1354 cleanup_rganalysis (element);
1359 GST_START_TEST (test_forced_album_skip)
1361 GstElement *element = setup_rganalysis ();
1362 GstTagList *tag_list;
1363 gint accumulator = 0;
1366 g_object_set (element, "forced", FALSE, "num-tracks", 2, NULL);
1367 set_playing_state (element);
1369 tag_list = gst_tag_list_new ();
1370 /* Provided values are totally arbitrary. */
1371 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
1372 GST_TAG_TRACK_PEAK, 0.75, GST_TAG_TRACK_GAIN, 2.21,
1373 GST_TAG_ALBUM_PEAK, 0.80, GST_TAG_ALBUM_GAIN, -0.11, NULL);
1374 send_tag_event (element, tag_list);
1377 push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25,
1379 send_eos_event (element);
1381 fail_unless_num_tracks (element, 1);
1383 /* This track has no tags, but needs to be skipped anyways since we
1384 * are in album processing mode. */
1386 push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, 0.0));
1387 send_eos_event (element);
1389 fail_unless_num_tracks (element, 0);
1391 /* Normal track after the album. Of course not to be skipped. */
1394 push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25,
1396 send_eos_event (element);
1397 tag_list = poll_tags (element);
1398 fail_unless_track_peak (tag_list, 0.25);
1399 fail_unless_track_gain (tag_list, get_expected_gain (8000));
1400 fail_if_album_tags (tag_list);
1401 gst_tag_list_free (tag_list);
1403 cleanup_rganalysis (element);
1408 GST_START_TEST (test_forced_album_no_skip)
1410 GstElement *element = setup_rganalysis ();
1411 GstTagList *tag_list;
1412 gint accumulator = 0;
1415 g_object_set (element, "forced", FALSE, "num-tracks", 2, NULL);
1416 set_playing_state (element);
1419 push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25,
1421 send_eos_event (element);
1422 tag_list = poll_tags (element);
1423 fail_unless_track_peak (tag_list, 0.25);
1424 fail_unless_track_gain (tag_list, get_expected_gain (8000));
1425 fail_if_album_tags (tag_list);
1426 gst_tag_list_free (tag_list);
1427 fail_unless_num_tracks (element, 1);
1429 /* The second track has indeed full tags, but although being not forced, this
1430 * one has to be processed because album processing is on. */
1431 tag_list = gst_tag_list_new ();
1432 /* Provided values are totally arbitrary. */
1433 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
1434 GST_TAG_TRACK_PEAK, 0.75, GST_TAG_TRACK_GAIN, 2.21,
1435 GST_TAG_ALBUM_PEAK, 0.80, GST_TAG_ALBUM_GAIN, -0.11, NULL);
1436 send_tag_event (element, tag_list);
1438 push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, 0.0));
1439 send_eos_event (element);
1440 tag_list = poll_tags (element);
1441 fail_unless_track_peak (tag_list, 0.0);
1442 fail_unless_track_gain (tag_list, SILENCE_GAIN);
1443 /* Second track was just silence so the album peak equals the first
1445 fail_unless_album_peak (tag_list, 0.25);
1446 /* Statistical processing leads to the second track being
1447 * ignored for the gain (because it is so short): */
1448 fail_unless_album_gain (tag_list, get_expected_gain (8000));
1449 gst_tag_list_free (tag_list);
1450 fail_unless_num_tracks (element, 0);
1452 cleanup_rganalysis (element);
1457 GST_START_TEST (test_forced_abort_album_no_skip)
1459 GstElement *element = setup_rganalysis ();
1460 GstTagList *tag_list;
1461 gint accumulator = 0;
1464 g_object_set (element, "forced", FALSE, "num-tracks", 2, NULL);
1465 set_playing_state (element);
1468 push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25,
1470 send_eos_event (element);
1471 tag_list = poll_tags (element);
1472 fail_unless_track_peak (tag_list, 0.25);
1473 fail_unless_track_gain (tag_list, get_expected_gain (8000));
1474 fail_if_album_tags (tag_list);
1475 gst_tag_list_free (tag_list);
1476 fail_unless_num_tracks (element, 1);
1478 /* Disabling album processing before end of album: */
1479 g_object_set (element, "num-tracks", 0, NULL);
1481 /* Processing a track that has to be skipped. */
1482 tag_list = gst_tag_list_new ();
1483 /* Provided values are totally arbitrary. */
1484 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
1485 GST_TAG_TRACK_PEAK, 0.75, GST_TAG_TRACK_GAIN, 2.21,
1486 GST_TAG_ALBUM_PEAK, 0.80, GST_TAG_ALBUM_GAIN, -0.11, NULL);
1487 send_tag_event (element, tag_list);
1489 push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, 0.0));
1490 send_eos_event (element);
1493 cleanup_rganalysis (element);
1498 GST_START_TEST (test_reference_level)
1500 GstElement *element = setup_rganalysis ();
1501 GstTagList *tag_list;
1503 gint accumulator = 0;
1506 set_playing_state (element);
1509 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1511 send_eos_event (element);
1512 tag_list = poll_tags (element);
1513 fail_unless_track_peak (tag_list, 0.25);
1514 fail_unless_track_gain (tag_list, get_expected_gain (44100));
1515 fail_if_album_tags (tag_list);
1516 fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL,
1517 &ref_level) && MATCH_GAIN (ref_level, 89.),
1518 "Incorrect reference level tag");
1519 gst_tag_list_free (tag_list);
1521 g_object_set (element, "reference-level", 83., "num-tracks", 2, NULL);
1524 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1526 send_eos_event (element);
1527 tag_list = poll_tags (element);
1528 fail_unless_track_peak (tag_list, 0.25);
1529 fail_unless_track_gain (tag_list, get_expected_gain (44100) - 6.);
1530 fail_if_album_tags (tag_list);
1531 fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL,
1532 &ref_level) && MATCH_GAIN (ref_level, 83.),
1533 "Incorrect reference level tag");
1534 gst_tag_list_free (tag_list);
1538 push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
1540 send_eos_event (element);
1541 tag_list = poll_tags (element);
1542 fail_unless_track_peak (tag_list, 0.25);
1543 fail_unless_track_gain (tag_list, get_expected_gain (44100) - 6.);
1544 fail_unless_album_peak (tag_list, 0.25);
1545 /* We provided the same waveform twice, with a reset separating
1546 * them. Therefore, the album gain matches the track gain. */
1547 fail_unless_album_gain (tag_list, get_expected_gain (44100) - 6.);
1548 fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL,
1549 &ref_level) && MATCH_GAIN (ref_level, 83.),
1550 "Incorrect reference level tag");
1551 gst_tag_list_free (tag_list);
1553 cleanup_rganalysis (element);
1558 GST_START_TEST (test_all_formats)
1560 GstElement *element = setup_rganalysis ();
1561 GstTagList *tag_list;
1562 gint accumulator = 0;
1565 set_playing_state (element);
1566 for (i = G_N_ELEMENTS (supported_rates); i--;) {
1568 for (j = 0; j < 4; j++)
1569 push_buffer (test_buffer_square_float_stereo (&accumulator,
1570 supported_rates[i].sample_rate, 512, 0.25, 0.25));
1571 for (j = 0; j < 3; j++)
1572 push_buffer (test_buffer_square_float_mono (&accumulator,
1573 supported_rates[i].sample_rate, 512, 0.25));
1574 for (j = 0; j < 4; j++)
1575 push_buffer (test_buffer_square_int16_stereo (&accumulator,
1576 supported_rates[i].sample_rate, 16, 512, 1 << 13, 1 << 13));
1577 for (j = 0; j < 3; j++)
1578 push_buffer (test_buffer_square_int16_mono (&accumulator,
1579 supported_rates[i].sample_rate, 16, 512, 1 << 13));
1580 for (j = 0; j < 3; j++)
1581 push_buffer (test_buffer_square_int16_stereo (&accumulator,
1582 supported_rates[i].sample_rate, 8, 512, 1 << 5, 1 << 5));
1583 for (j = 0; j < 3; j++)
1584 push_buffer (test_buffer_square_int16_mono (&accumulator,
1585 supported_rates[i].sample_rate, 8, 512, 1 << 5));
1586 send_eos_event (element);
1587 tag_list = poll_tags (element);
1588 fail_unless_track_peak (tag_list, 0.25);
1589 fail_unless_track_gain (tag_list, supported_rates[i].gain);
1590 gst_tag_list_free (tag_list);
1593 cleanup_rganalysis (element);
1598 /* Checks ensuring all advertised supported sample rates are really
1599 * accepted, for integer and float, mono and stereo. This also
1600 * verifies that the correct gain is computed for all formats (except
1601 * odd bit depths). */
1603 #define MAKE_GAIN_TEST_FLOAT_MONO(sample_rate) \
1604 GST_START_TEST (test_gain_float_mono_##sample_rate) \
1606 GstElement *element = setup_rganalysis (); \
1607 GstTagList *tag_list; \
1608 gint accumulator = 0; \
1611 set_playing_state (element); \
1613 for (i = 0; i < 20; i++) \
1614 push_buffer (test_buffer_square_float_mono (&accumulator, \
1615 sample_rate, 512, 0.25)); \
1616 send_eos_event (element); \
1617 tag_list = poll_tags (element); \
1618 fail_unless_track_peak (tag_list, 0.25); \
1619 fail_unless_track_gain (tag_list, \
1620 get_expected_gain (sample_rate)); \
1621 gst_tag_list_free (tag_list); \
1623 cleanup_rganalysis (element); \
1628 #define MAKE_GAIN_TEST_FLOAT_STEREO(sample_rate) \
1629 GST_START_TEST (test_gain_float_stereo_##sample_rate) \
1631 GstElement *element = setup_rganalysis (); \
1632 GstTagList *tag_list; \
1633 gint accumulator = 0; \
1636 set_playing_state (element); \
1638 for (i = 0; i < 20; i++) \
1639 push_buffer (test_buffer_square_float_stereo (&accumulator, \
1640 sample_rate, 512, 0.25, 0.25)); \
1641 send_eos_event (element); \
1642 tag_list = poll_tags (element); \
1643 fail_unless_track_peak (tag_list, 0.25); \
1644 fail_unless_track_gain (tag_list, \
1645 get_expected_gain (sample_rate)); \
1646 gst_tag_list_free (tag_list); \
1648 cleanup_rganalysis (element); \
1653 #define MAKE_GAIN_TEST_INT16_MONO(sample_rate, depth) \
1654 GST_START_TEST (test_gain_int16_##depth##_mono_##sample_rate) \
1656 GstElement *element = setup_rganalysis (); \
1657 GstTagList *tag_list; \
1658 gint accumulator = 0; \
1661 set_playing_state (element); \
1663 for (i = 0; i < 20; i++) \
1664 push_buffer (test_buffer_square_int16_mono (&accumulator, \
1665 sample_rate, depth, 512, 1 << (13 + depth - 16))); \
1667 send_eos_event (element); \
1668 tag_list = poll_tags (element); \
1669 fail_unless_track_peak (tag_list, 0.25); \
1670 fail_unless_track_gain (tag_list, \
1671 get_expected_gain (sample_rate)); \
1672 gst_tag_list_free (tag_list); \
1674 cleanup_rganalysis (element); \
1679 #define MAKE_GAIN_TEST_INT16_STEREO(sample_rate, depth) \
1680 GST_START_TEST (test_gain_int16_##depth##_stereo_##sample_rate) \
1682 GstElement *element = setup_rganalysis (); \
1683 GstTagList *tag_list; \
1684 gint accumulator = 0; \
1687 set_playing_state (element); \
1689 for (i = 0; i < 20; i++) \
1690 push_buffer (test_buffer_square_int16_stereo (&accumulator, \
1691 sample_rate, depth, 512, 1 << (13 + depth - 16), \
1692 1 << (13 + depth - 16))); \
1693 send_eos_event (element); \
1694 tag_list = poll_tags (element); \
1695 fail_unless_track_peak (tag_list, 0.25); \
1696 fail_unless_track_gain (tag_list, \
1697 get_expected_gain (sample_rate)); \
1698 gst_tag_list_free (tag_list); \
1700 cleanup_rganalysis (element); \
1705 MAKE_GAIN_TEST_FLOAT_MONO (8000);
1706 MAKE_GAIN_TEST_FLOAT_MONO (11025);
1707 MAKE_GAIN_TEST_FLOAT_MONO (12000);
1708 MAKE_GAIN_TEST_FLOAT_MONO (16000);
1709 MAKE_GAIN_TEST_FLOAT_MONO (22050);
1710 MAKE_GAIN_TEST_FLOAT_MONO (24000);
1711 MAKE_GAIN_TEST_FLOAT_MONO (32000);
1712 MAKE_GAIN_TEST_FLOAT_MONO (44100);
1713 MAKE_GAIN_TEST_FLOAT_MONO (48000);
1715 MAKE_GAIN_TEST_FLOAT_STEREO (8000);
1716 MAKE_GAIN_TEST_FLOAT_STEREO (11025);
1717 MAKE_GAIN_TEST_FLOAT_STEREO (12000);
1718 MAKE_GAIN_TEST_FLOAT_STEREO (16000);
1719 MAKE_GAIN_TEST_FLOAT_STEREO (22050);
1720 MAKE_GAIN_TEST_FLOAT_STEREO (24000);
1721 MAKE_GAIN_TEST_FLOAT_STEREO (32000);
1722 MAKE_GAIN_TEST_FLOAT_STEREO (44100);
1723 MAKE_GAIN_TEST_FLOAT_STEREO (48000);
1725 MAKE_GAIN_TEST_INT16_MONO (8000, 16);
1726 MAKE_GAIN_TEST_INT16_MONO (11025, 16);
1727 MAKE_GAIN_TEST_INT16_MONO (12000, 16);
1728 MAKE_GAIN_TEST_INT16_MONO (16000, 16);
1729 MAKE_GAIN_TEST_INT16_MONO (22050, 16);
1730 MAKE_GAIN_TEST_INT16_MONO (24000, 16);
1731 MAKE_GAIN_TEST_INT16_MONO (32000, 16);
1732 MAKE_GAIN_TEST_INT16_MONO (44100, 16);
1733 MAKE_GAIN_TEST_INT16_MONO (48000, 16);
1735 MAKE_GAIN_TEST_INT16_STEREO (8000, 16);
1736 MAKE_GAIN_TEST_INT16_STEREO (11025, 16);
1737 MAKE_GAIN_TEST_INT16_STEREO (12000, 16);
1738 MAKE_GAIN_TEST_INT16_STEREO (16000, 16);
1739 MAKE_GAIN_TEST_INT16_STEREO (22050, 16);
1740 MAKE_GAIN_TEST_INT16_STEREO (24000, 16);
1741 MAKE_GAIN_TEST_INT16_STEREO (32000, 16);
1742 MAKE_GAIN_TEST_INT16_STEREO (44100, 16);
1743 MAKE_GAIN_TEST_INT16_STEREO (48000, 16);
1745 MAKE_GAIN_TEST_INT16_MONO (8000, 8);
1746 MAKE_GAIN_TEST_INT16_MONO (11025, 8);
1747 MAKE_GAIN_TEST_INT16_MONO (12000, 8);
1748 MAKE_GAIN_TEST_INT16_MONO (16000, 8);
1749 MAKE_GAIN_TEST_INT16_MONO (22050, 8);
1750 MAKE_GAIN_TEST_INT16_MONO (24000, 8);
1751 MAKE_GAIN_TEST_INT16_MONO (32000, 8);
1752 MAKE_GAIN_TEST_INT16_MONO (44100, 8);
1753 MAKE_GAIN_TEST_INT16_MONO (48000, 8);
1755 MAKE_GAIN_TEST_INT16_STEREO (8000, 8);
1756 MAKE_GAIN_TEST_INT16_STEREO (11025, 8);
1757 MAKE_GAIN_TEST_INT16_STEREO (12000, 8);
1758 MAKE_GAIN_TEST_INT16_STEREO (16000, 8);
1759 MAKE_GAIN_TEST_INT16_STEREO (22050, 8);
1760 MAKE_GAIN_TEST_INT16_STEREO (24000, 8);
1761 MAKE_GAIN_TEST_INT16_STEREO (32000, 8);
1762 MAKE_GAIN_TEST_INT16_STEREO (44100, 8);
1763 MAKE_GAIN_TEST_INT16_STEREO (48000, 8);
1766 rganalysis_suite (void)
1768 Suite *s = suite_create ("rganalysis");
1769 TCase *tc_chain = tcase_create ("general");
1771 suite_add_tcase (s, tc_chain);
1773 tcase_add_test (tc_chain, test_no_buffer);
1774 tcase_add_test (tc_chain, test_no_buffer_album_1);
1775 tcase_add_test (tc_chain, test_no_buffer_album_2);
1776 tcase_add_test (tc_chain, test_empty_buffers);
1778 tcase_add_test (tc_chain, test_peak_float);
1779 tcase_add_test (tc_chain, test_peak_int16_16);
1780 tcase_add_test (tc_chain, test_peak_int16_8);
1782 tcase_add_test (tc_chain, test_peak_album);
1783 tcase_add_test (tc_chain, test_peak_track_album);
1784 tcase_add_test (tc_chain, test_peak_album_abort_to_track);
1786 tcase_add_test (tc_chain, test_gain_album);
1788 tcase_add_test (tc_chain, test_forced);
1789 tcase_add_test (tc_chain, test_forced_separate);
1790 tcase_add_test (tc_chain, test_forced_after_data);
1791 tcase_add_test (tc_chain, test_forced_album);
1792 tcase_add_test (tc_chain, test_forced_album_skip);
1793 tcase_add_test (tc_chain, test_forced_album_no_skip);
1794 tcase_add_test (tc_chain, test_forced_abort_album_no_skip);
1796 tcase_add_test (tc_chain, test_reference_level);
1798 tcase_add_test (tc_chain, test_all_formats);
1800 tcase_add_test (tc_chain, test_gain_float_mono_8000);
1801 tcase_add_test (tc_chain, test_gain_float_mono_11025);
1802 tcase_add_test (tc_chain, test_gain_float_mono_12000);
1803 tcase_add_test (tc_chain, test_gain_float_mono_16000);
1804 tcase_add_test (tc_chain, test_gain_float_mono_22050);
1805 tcase_add_test (tc_chain, test_gain_float_mono_24000);
1806 tcase_add_test (tc_chain, test_gain_float_mono_32000);
1807 tcase_add_test (tc_chain, test_gain_float_mono_44100);
1808 tcase_add_test (tc_chain, test_gain_float_mono_48000);
1810 tcase_add_test (tc_chain, test_gain_float_stereo_8000);
1811 tcase_add_test (tc_chain, test_gain_float_stereo_11025);
1812 tcase_add_test (tc_chain, test_gain_float_stereo_12000);
1813 tcase_add_test (tc_chain, test_gain_float_stereo_16000);
1814 tcase_add_test (tc_chain, test_gain_float_stereo_22050);
1815 tcase_add_test (tc_chain, test_gain_float_stereo_24000);
1816 tcase_add_test (tc_chain, test_gain_float_stereo_32000);
1817 tcase_add_test (tc_chain, test_gain_float_stereo_44100);
1818 tcase_add_test (tc_chain, test_gain_float_stereo_48000);
1820 tcase_add_test (tc_chain, test_gain_int16_16_mono_8000);
1821 tcase_add_test (tc_chain, test_gain_int16_16_mono_11025);
1822 tcase_add_test (tc_chain, test_gain_int16_16_mono_12000);
1823 tcase_add_test (tc_chain, test_gain_int16_16_mono_16000);
1824 tcase_add_test (tc_chain, test_gain_int16_16_mono_22050);
1825 tcase_add_test (tc_chain, test_gain_int16_16_mono_24000);
1826 tcase_add_test (tc_chain, test_gain_int16_16_mono_32000);
1827 tcase_add_test (tc_chain, test_gain_int16_16_mono_44100);
1828 tcase_add_test (tc_chain, test_gain_int16_16_mono_48000);
1830 tcase_add_test (tc_chain, test_gain_int16_16_stereo_8000);
1831 tcase_add_test (tc_chain, test_gain_int16_16_stereo_11025);
1832 tcase_add_test (tc_chain, test_gain_int16_16_stereo_12000);
1833 tcase_add_test (tc_chain, test_gain_int16_16_stereo_16000);
1834 tcase_add_test (tc_chain, test_gain_int16_16_stereo_22050);
1835 tcase_add_test (tc_chain, test_gain_int16_16_stereo_24000);
1836 tcase_add_test (tc_chain, test_gain_int16_16_stereo_32000);
1837 tcase_add_test (tc_chain, test_gain_int16_16_stereo_44100);
1838 tcase_add_test (tc_chain, test_gain_int16_16_stereo_48000);
1840 tcase_add_test (tc_chain, test_gain_int16_8_mono_8000);
1841 tcase_add_test (tc_chain, test_gain_int16_8_mono_11025);
1842 tcase_add_test (tc_chain, test_gain_int16_8_mono_12000);
1843 tcase_add_test (tc_chain, test_gain_int16_8_mono_16000);
1844 tcase_add_test (tc_chain, test_gain_int16_8_mono_22050);
1845 tcase_add_test (tc_chain, test_gain_int16_8_mono_24000);
1846 tcase_add_test (tc_chain, test_gain_int16_8_mono_32000);
1847 tcase_add_test (tc_chain, test_gain_int16_8_mono_44100);
1848 tcase_add_test (tc_chain, test_gain_int16_8_mono_48000);
1850 tcase_add_test (tc_chain, test_gain_int16_8_stereo_8000);
1851 tcase_add_test (tc_chain, test_gain_int16_8_stereo_11025);
1852 tcase_add_test (tc_chain, test_gain_int16_8_stereo_12000);
1853 tcase_add_test (tc_chain, test_gain_int16_8_stereo_16000);
1854 tcase_add_test (tc_chain, test_gain_int16_8_stereo_22050);
1855 tcase_add_test (tc_chain, test_gain_int16_8_stereo_24000);
1856 tcase_add_test (tc_chain, test_gain_int16_8_stereo_32000);
1857 tcase_add_test (tc_chain, test_gain_int16_8_stereo_44100);
1858 tcase_add_test (tc_chain, test_gain_int16_8_stereo_48000);
1864 main (int argc, char **argv)
1868 Suite *s = rganalysis_suite ();
1869 SRunner *sr = srunner_create (s);
1871 gst_check_init (&argc, &argv);
1873 srunner_run_all (sr, CK_ENV);
1874 nf = srunner_ntests_failed (sr);