summaryrefslogtreecommitdiffstats
path: root/tests/check/elements/rganalysis.c
blob: 143feec991828ffd38627baa64d038171aaa71d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
/* GStreamer ReplayGain analysis
 *
 * Copyright (C) 2006 Rene Stadler <mail@renestadler.de>
 *
 * rganalysis.c: Unit test for the rganalysis element
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

/* Some things to note about the RMS window length of the analysis algorithm and
 * thus the implementation used in the element: Processing divides input data
 * into 50ms windows at some point.  Some details about this that normally do
 * not matter:
 *
 *  1. At the end of a stream, the remainder of data that did not fill up the
 *     last 50ms window is simply discarded.
 *
 *  2. If the sample rate changes during a stream, the currently running window
 *     is discarded and the equal loudness filter gets reset as if a new stream
 *     started.
 *
 *  3. For the album gain, it is not entirely correct to think of obtaining it
 *     like "as if all the tracks are analyzed as one track".  There isn't a
 *     separate window being tracked for album processing, so at stream (track)
 *     end, the remaining unfilled window does not contribute to the album gain
 *     either.
 *
 *  4. If a waveform with a result gain G is concatenated to itself and the
 *     result processed as a track, the gain can be different from G if and only
 *     if the duration of the original waveform is not an integer multiple of
 *     50ms.  If the original waveform gets processed as a single track and then
 *     the same data again as a subsequent track, the album result gain will
 *     always match G (this is implied by 3.).
 *
 *  5. A stream shorter than 50ms cannot be analyzed.  At 8000 and 48000 Hz,
 *     this corresponds to 400 resp. 2400 frames.  If a stream is shorter than
 *     50ms, the element will not generate tags at EOS (only if an album
 *     finished, but only album tags are generated then).  This is not an
 *     erroneous condition, the element should behave normally.
 *
 * The limitations outlined in 1.-4. do not apply to the peak values.  Every
 * single sample is accounted for when looking for the peak.  Thus the album
 * peak is guaranteed to be the maximum value of all track peaks.
 *
 * In normal day-to-day use, these little facts are unlikely to be relevant, but
 * they have to be kept in mind for writing the tests here.
 */

#include <gst/check/gstcheck.h>

GList *buffers = NULL;

/* For ease of programming we use globals to keep refs for our floating src and
 * sink pads we create; otherwise we always have to do get_pad, get_peer, and
 * then remove references in every test function */
static GstPad *mysrcpad, *mysinkpad;

/* Mapping from supported sample rates to the correct result gain for the
 * following test waveform: 20 * 512 samples with a quarter-full amplitude of
 * toggling sign, changing every 48 samples and starting with the positive
 * value.
 *
 * Even if we would generate a wave describing a signal with the same frequency
 * at each sampling rate, the results would vary (slightly).  Hence the simple
 * generation method, since we cannot use a constant value as expected result
 * anyways.  For all sample rates, changing the sign every 48 frames gives a
 * sane frequency.  Buffers containing data that forms such a waveform is
 * created using the test_buffer_square_{float,int16}_{mono,stereo} functions
 * below.
 *
 * The results have been checked against what the metaflac and wavegain programs
 * generate for such a stream.  If you want to verify these, be sure that the
 * metaflac program does not produce incorrect results in your environment: I
 * found a strange bug in the (defacto) reference code for the analysis that
 * sometimes leads to incorrect RMS window lengths. */

struct rate_test
{
  guint sample_rate;
  gdouble gain;
};

static const struct rate_test supported_rates[] = {
  {8000, -0.91},
  {11025, -2.80},
  {12000, -3.13},
  {16000, -4.26},
  {22050, -5.64},
  {24000, -5.87},
  {32000, -6.03},
  {44100, -6.20},
  {48000, -6.14}
};

/* Lookup the correct gain adjustment result in above array. */

static gdouble
get_expected_gain (guint sample_rate)
{
  gint i;

  for (i = G_N_ELEMENTS (supported_rates); i--;)
    if (supported_rates[i].sample_rate == sample_rate)
      return supported_rates[i].gain;
  g_return_val_if_reached (0.0);
}

#define SILENCE_GAIN 64.82

#define REPLAY_GAIN_CAPS                                \
  "channels = (int) { 1, 2 }, "                         \
  "rate = (int) { 8000, 11025, 12000, 16000, 22050, "   \
  "24000, 32000, 44100, 48000 }"

#define RG_ANALYSIS_CAPS_TEMPLATE_STRING  \
  "audio/x-raw-float, "                   \
  "width = (int) 32, "                    \
  "endianness = (int) BYTE_ORDER, "       \
  REPLAY_GAIN_CAPS                        \
  "; "                                    \
  "audio/x-raw-int, "                     \
  "width = (int) 16, "                    \
  "depth = (int) [ 1, 16 ], "             \
  "signed = (boolean) true, "             \
  "endianness = (int) BYTE_ORDER, "       \
  REPLAY_GAIN_CAPS

static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (RG_ANALYSIS_CAPS_TEMPLATE_STRING)
    );
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (RG_ANALYSIS_CAPS_TEMPLATE_STRING)
    );

GstElement *
setup_rganalysis ()
{
  GstElement *analysis;
  GstBus *bus;

  GST_DEBUG ("setup_rganalysis");
  analysis = gst_check_setup_element ("rganalysis");
  mysrcpad = gst_check_setup_src_pad (analysis, &srctemplate, NULL);
  mysinkpad = gst_check_setup_sink_pad (analysis, &sinktemplate, NULL);
  gst_pad_set_active (mysrcpad, TRUE);
  gst_pad_set_active (mysinkpad, TRUE);

  bus = gst_bus_new ();
  gst_element_set_bus (analysis, bus);
  /* gst_element_set_bus does not steal a reference. */
  gst_object_unref (bus);

  return analysis;
}

void
cleanup_rganalysis (GstElement * element)
{
  GST_DEBUG ("cleanup_rganalysis");

  g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
  g_list_free (buffers);
  buffers = NULL;

  /* The bus owns references to the element: */
  gst_element_set_bus (element, NULL);

  gst_pad_set_active (mysrcpad, FALSE);
  gst_pad_set_active (mysinkpad, FALSE);
  gst_check_teardown_src_pad (element);
  gst_check_teardown_sink_pad (element);
  gst_check_teardown_element (element);
}

static void
set_playing_state (GstElement * element)
{
  fail_unless (gst_element_set_state (element,
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "Could not set state to PLAYING");
}

static void
send_eos_event (GstElement * element)
{
  GstBus *bus = gst_element_get_bus (element);
  GstPad *pad = gst_element_get_static_pad (element, "sink");
  GstEvent *event = gst_event_new_eos ();

  fail_unless (gst_pad_send_event (pad, event),
      "Cannot send EOS event: Not handled.");

  /* There is no sink element, so _we_ post the EOS message on the bus here.  Of
   * course we generate any EOS ourselves, but this allows us to poll for the
   * EOS message in poll_eos if we expect the element to _not_ generate a TAG
   * message.  That's better than waiting for a timeout to lapse. */
  fail_unless (gst_bus_post (bus, gst_message_new_eos (NULL)));

  gst_object_unref (bus);
  gst_object_unref (pad);
}

static void
send_tag_event (GstElement * element, GstTagList * tag_list)
{
  GstPad *pad = gst_element_get_static_pad (element, "sink");
  GstEvent *event = gst_event_new_tag (tag_list);

  fail_unless (gst_pad_send_event (pad, event),
      "Cannot send TAG event: Not handled.");

  gst_object_unref (pad);
}

static void
poll_eos (GstElement * element)
{
  GstBus *bus = gst_element_get_bus (element);
  GstMessage *message;

  message = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_TAG, GST_SECOND);
  fail_unless (message != NULL, "Could not poll for EOS message: Timed out");
  fail_unless (message->type == GST_MESSAGE_EOS,
      "Could not poll for eos message: got message of type %s instead",
      gst_message_type_get_name (message->type));

  gst_message_unref (message);
  gst_object_unref (bus);
}

/* This also polls for EOS since the TAG message comes right before the end of
 * streams. */

static GstTagList *
poll_tags (GstElement * element)
{
  GstBus *bus = gst_element_get_bus (element);
  GstTagList *tag_list;
  GstMessage *message;

  message = gst_bus_poll (bus, GST_MESSAGE_TAG, GST_SECOND);
  fail_unless (message != NULL, "Could not poll for TAG message: Timed out");

  gst_message_parse_tag (message, &tag_list);
  gst_message_unref (message);
  gst_object_unref (bus);

  poll_eos (element);

  return tag_list;
}

#define MATCH_PEAK(p1, p2) ((p1 < p2 + 1e-6) && (p2 < p1 + 1e-6))
#define MATCH_GAIN(g1, g2) ((g1 < g2 + 1e-13) && (g2 < g1 + 1e-13))

static void
fail_unless_track_gain (const GstTagList * tag_list, gdouble gain)
{
  gdouble result;

  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &result),
      "Tag list contains no track gain value");
  fail_unless (MATCH_GAIN (gain, result),
      "Track gain %+.2f does not match, expected %+.2f", result, gain);
}

static void
fail_unless_track_peak (const GstTagList * tag_list, gdouble peak)
{
  gdouble result;

  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &result),
      "Tag list contains no track peak value");
  fail_unless (MATCH_PEAK (peak, result),
      "Track peak %f does not match, expected %f", result, peak);
}

static void
fail_unless_album_gain (const GstTagList * tag_list, gdouble gain)
{
  gdouble result;

  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &result),
      "Tag list contains no album gain value");
  fail_unless (MATCH_GAIN (result, gain),
      "Album gain %+.2f does not match, expected %+.2f", result, gain);
}

static void
fail_unless_album_peak (const GstTagList * tag_list, gdouble peak)
{
  gdouble result;

  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &result),
      "Tag list contains no album peak value");
  fail_unless (MATCH_PEAK (peak, result),
      "Album peak %f does not match, expected %f", result, peak);
}

static void
fail_if_track_tags (const GstTagList * tag_list)
{
  gdouble result;

  fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &result),
      "Tag list contains track gain value (but should not)");
  fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &result),
      "Tag list contains track peak value (but should not)");
}

static void
fail_if_album_tags (const GstTagList * tag_list)
{
  gdouble result;

  fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &result),
      "Tag list contains album gain value (but should not)");
  fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &result),
      "Tag list contains album peak value (but should not)");
}

static void
fail_unless_num_tracks (GstElement * element, guint num_tracks)
{
  guint current;

  g_object_get (element, "num-tracks", &current, NULL);
  fail_unless (current == num_tracks,
      "num-tracks property has incorrect value %u, expected %u",
      current, num_tracks);
}

/* Functions that create buffers with constant sample values, for peak
 * tests. */

static GstBuffer *
test_buffer_const_float_mono (gint sample_rate, gsize n_frames, gfloat value)
{
  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat));
  gfloat *data = (gfloat *) GST_BUFFER_DATA (buf);
  GstCaps *caps;
  gint i;

  for (i = n_frames; i--;)
    *data++ = value;

  caps = gst_caps_new_simple ("audio/x-raw-float",
      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1,
      "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
  gst_buffer_set_caps (buf, caps);
  gst_caps_unref (caps);

  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);

  return buf;
}

static GstBuffer *
test_buffer_const_float_stereo (gint sample_rate, gsize n_frames,
    gfloat value_l, gfloat value_r)
{
  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat) * 2);
  gfloat *data = (gfloat *) GST_BUFFER_DATA (buf);
  GstCaps *caps;
  gint i;

  for (i = n_frames; i--;) {
    *data++ = value_l;
    *data++ = value_r;
  }

  caps = gst_caps_new_simple ("audio/x-raw-float",
      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2,
      "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
  gst_buffer_set_caps (buf, caps);
  gst_caps_unref (caps);

  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);

  return buf;
}

static GstBuffer *
test_buffer_const_int16_mono (gint sample_rate, gint depth, gsize n_frames,
    gint16 value)
{
  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16));
  gint16 *data = (gint16 *) GST_BUFFER_DATA (buf);
  GstCaps *caps;
  gint i;

  for (i = n_frames; i--;)
    *data++ = value;

  caps = gst_caps_new_simple ("audio/x-raw-int",
      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1,
      "endianness", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE,
      "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL);
  gst_buffer_set_caps (buf, caps);
  gst_caps_unref (caps);

  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);

  return buf;
}

static GstBuffer *
test_buffer_const_int16_stereo (gint sample_rate, gint depth, gsize n_frames,
    gint16 value_l, gint16 value_r)
{
  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16) * 2);
  gint16 *data = (gint16 *) GST_BUFFER_DATA (buf);
  GstCaps *caps;
  gint i;

  for (i = n_frames; i--;) {
    *data++ = value_l;
    *data++ = value_r;
  }

  caps = gst_caps_new_simple ("audio/x-raw-int",
      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2,
      "endianness", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE,
      "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL);
  gst_buffer_set_caps (buf, caps);
  gst_caps_unref (caps);

  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);

  return buf;
}

/* Functions that create data buffers containing square signal
 * waveforms. */

static GstBuffer *
test_buffer_square_float_mono (gint * accumulator, gint sample_rate,
    gsize n_frames, gfloat value)
{
  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat));
  gfloat *data = (gfloat *) GST_BUFFER_DATA (buf);
  GstCaps *caps;
  gint i;

  for (i = n_frames; i--;) {
    *accumulator += 1;
    *accumulator %= 96;

    if (*accumulator < 48)
      *data++ = value;
    else
      *data++ = -value;
  }

  caps = gst_caps_new_simple ("audio/x-raw-float",
      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1,
      "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
  gst_buffer_set_caps (buf, caps);
  gst_caps_unref (caps);

  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);

  return buf;
}

static GstBuffer *
test_buffer_square_float_stereo (gint * accumulator, gint sample_rate,
    gsize n_frames, gfloat value_l, gfloat value_r)
{
  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat) * 2);
  gfloat *data = (gfloat *) GST_BUFFER_DATA (buf);
  GstCaps *caps;
  gint i;

  for (i = n_frames; i--;) {
    *accumulator += 1;
    *accumulator %= 96;

    if (*accumulator < 48) {
      *data++ = value_l;
      *data++ = value_r;
    } else {
      *data++ = -value_l;
      *data++ = -value_r;
    }
  }

  caps = gst_caps_new_simple ("audio/x-raw-float",
      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2,
      "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
  gst_buffer_set_caps (buf, caps);
  gst_caps_unref (caps);

  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);

  return buf;
}

static GstBuffer *
test_buffer_square_int16_mono (gint * accumulator, gint sample_rate,
    gint depth, gsize n_frames, gint16 value)
{
  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16));
  gint16 *data = (gint16 *) GST_BUFFER_DATA (buf);
  GstCaps *caps;
  gint i;

  for (i = n_frames; i--;) {
    *accumulator += 1;
    *accumulator %= 96;

    if (*accumulator < 48)
      *data++ = value;
    else
      *data++ = -MAX (value, -32767);
  }

  caps = gst_caps_new_simple ("audio/x-raw-int",
      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1,
      "endianness", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE,
      "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL);
  gst_buffer_set_caps (buf, caps);
  gst_caps_unref (caps);

  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);

  return buf;
}

static GstBuffer *
test_buffer_square_int16_stereo (gint * accumulator, gint sample_rate,
    gint depth, gsize n_frames, gint16 value_l, gint16 value_r)
{
  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16) * 2);
  gint16 *data = (gint16 *) GST_BUFFER_DATA (buf);
  GstCaps *caps;
  gint i;

  for (i = n_frames; i--;) {
    *accumulator += 1;
    *accumulator %= 96;

    if (*accumulator < 48) {
      *data++ = value_l;
      *data++ = value_r;
    } else {
      *data++ = -MAX (value_l, -32767);
      *data++ = -MAX (value_r, -32767);
    }
  }

  caps = gst_caps_new_simple ("audio/x-raw-int",
      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2,
      "endianness", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE,
      "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL);
  gst_buffer_set_caps (buf, caps);
  gst_caps_unref (caps);

  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);

  return buf;
}

static void
push_buffer (GstBuffer * buf)
{
  /* gst_pad_push steals a reference. */
  fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
}

/*** Start of the tests. ***/

/* This test looks redundant, but early versions of the element
 * crashed when doing, well, nothing: */

GST_START_TEST (test_no_buffer)
{
  GstElement *element = setup_rganalysis ();

  set_playing_state (element);
  send_eos_event (element);
  poll_eos (element);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_no_buffer_album_1)
{
  GstElement *element = setup_rganalysis ();

  set_playing_state (element);

  /* Single track: */
  send_eos_event (element);
  poll_eos (element);

  /* First album: */
  g_object_set (element, "num-tracks", 3, NULL);

  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 2);

  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 1);

  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 0);

  /* Second album: */
  g_object_set (element, "num-tracks", 2, NULL);

  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 1);

  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 0);

  /* Single track: */
  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 0);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_no_buffer_album_2)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gint accumulator = 0;
  gint i;

  g_object_set (element, "num-tracks", 3, NULL);
  set_playing_state (element);

  /* No buffer for the first track. */

  send_eos_event (element);
  /* No tags should be posted, there was nothing to analyze: */
  poll_eos (element);
  fail_unless_num_tracks (element, 2);

  /* A test waveform with known gain result as second track: */

  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_mono (&accumulator, 44100, 512,
            0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, -6.20);
  /* Album is not finished yet: */
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 1);

  /* No buffer for the last track. */

  send_eos_event (element);

  tag_list = poll_tags (element);
  fail_unless_album_peak (tag_list, 0.25);
  fail_unless_album_gain (tag_list, -6.20);
  /* No track tags should be posted, as there was no data for it: */
  fail_if_track_tags (tag_list);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 0);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_empty_buffers)
{
  GstElement *element = setup_rganalysis ();

  set_playing_state (element);

  /* Single track: */
  push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0));
  send_eos_event (element);
  poll_eos (element);

  /* First album: */
  g_object_set (element, "num-tracks", 2, NULL);

  push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0));
  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 1);

  push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0));
  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 0);

  /* Second album, with a single track: */
  g_object_set (element, "num-tracks", 1, NULL);
  push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0));
  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 0);

  /* Single track: */
  push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0));
  send_eos_event (element);
  poll_eos (element);

  cleanup_rganalysis (element);
}

GST_END_TEST;

/* Tests for correctness of the peak values. */

/* Float peak test.  For stereo, one channel has the constant value of -1.369,
 * the other one 0.0.  This tests many things: The result peak value should
 * occur on any channel.  The peak is of course the absolute amplitude, so 1.369
 * should be the result.  This will also detect if the code uses the absolute
 * value during the comparison.  If it is buggy it will return 0.0 since 0.0 >
 * -1.369.  Furthermore, this makes sure that there is no problem with headroom
 * (exceeding 0dBFS).  In the wild you get float samples > 1.0 from stuff like
 * vorbis. */

GST_START_TEST (test_peak_float)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;

  set_playing_state (element);
  push_buffer (test_buffer_const_float_stereo (8000, 512, -1.369, 0.0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.369);
  gst_tag_list_free (tag_list);

  /* Swapped channels. */
  push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, -1.369));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.369);
  gst_tag_list_free (tag_list);

  /* Mono. */
  push_buffer (test_buffer_const_float_mono (8000, 512, -1.369));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.369);
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_peak_int16_16)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;

  set_playing_state (element);

  /* Half amplitude. */
  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 1 << 14, 0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);

  /* Swapped channels. */
  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, 1 << 14));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);

  /* Mono. */
  push_buffer (test_buffer_const_int16_mono (8000, 16, 512, 1 << 14));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);

  /* Half amplitude, negative variant. */
  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, -1 << 14, 0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);

  /* Swapped channels. */
  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, -1 << 14));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);

  /* Mono. */
  push_buffer (test_buffer_const_int16_mono (8000, 16, 512, -1 << 14));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);


  /* Now check for correct normalization of the peak value: Sample
   * values of this format range from -32768 to 32767.  So for the
   * highest positive amplitude we do not reach 1.0, only for
   * -32768! */

  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 32767, 0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 32767. / 32768.);
  gst_tag_list_free (tag_list);

  /* Swapped channels. */
  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, 32767));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 32767. / 32768.);
  gst_tag_list_free (tag_list);

  /* Mono. */
  push_buffer (test_buffer_const_int16_mono (8000, 16, 512, 32767));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 32767. / 32768.);
  gst_tag_list_free (tag_list);


  /* Negative variant, reaching 1.0. */
  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, -32768, 0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.0);
  gst_tag_list_free (tag_list);

  /* Swapped channels. */
  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, -32768));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.0);
  gst_tag_list_free (tag_list);

  /* Mono. */
  push_buffer (test_buffer_const_int16_mono (8000, 16, 512, -32768));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.0);
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

/* Same as the test before, but with 8 bits (packed into 16 bits). */

GST_START_TEST (test_peak_int16_8)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;

  set_playing_state (element);

  /* Half amplitude. */
  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 1 << 6, 0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);

  /* Swapped channels. */
  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, 1 << 6));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);

  /* Mono. */
  push_buffer (test_buffer_const_int16_mono (8000, 8, 512, 1 << 6));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);


  /* Half amplitude, negative variant. */
  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, -1 << 6, 0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);

  /* Swapped channels. */
  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, -1 << 6));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);

  /* Mono. */
  push_buffer (test_buffer_const_int16_mono (8000, 8, 512, -1 << 6));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);


  /* Almost full amplitude (maximum positive value). */
  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, (1 << 7) - 1, 0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.9921875);
  gst_tag_list_free (tag_list);

  /* Swapped channels. */
  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, (1 << 7) - 1));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.9921875);
  gst_tag_list_free (tag_list);

  /* Mono. */
  push_buffer (test_buffer_const_int16_mono (8000, 8, 512, (1 << 7) - 1));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.9921875);
  gst_tag_list_free (tag_list);


  /* Full amplitude (maximum negative value). */
  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, -1 << 7, 0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.0);
  gst_tag_list_free (tag_list);

  /* Swapped channels. */
  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, -1 << 7));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.0);
  gst_tag_list_free (tag_list);

  /* Mono. */
  push_buffer (test_buffer_const_int16_mono (8000, 8, 512, -1 << 7));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.0);
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_peak_album)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;

  g_object_set (element, "num-tracks", 2, NULL);
  set_playing_state (element);

  push_buffer (test_buffer_const_float_stereo (8000, 1024, 1.0, 0.0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.0);
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 1);

  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.0, 0.5));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  fail_unless_album_peak (tag_list, 1.0);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 0);

  /* Try a second album: */
  g_object_set (element, "num-tracks", 3, NULL);

  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.4, 0.4));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.4);
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 2);

  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.45, 0.45));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.45);
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 1);

  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.2, 0.2));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.2);
  fail_unless_album_peak (tag_list, 0.45);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 0);

  /* And now a single track, not in album mode (num-tracks is 0
   * now): */
  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.1, 0.1));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.1);
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

/* Switching from track to album mode. */

GST_START_TEST (test_peak_track_album)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;

  set_playing_state (element);

  push_buffer (test_buffer_const_float_mono (8000, 1024, 1.0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.0);
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);

  g_object_set (element, "num-tracks", 1, NULL);
  push_buffer (test_buffer_const_float_mono (8000, 1024, 0.5));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  fail_unless_album_peak (tag_list, 0.5);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 0);

  cleanup_rganalysis (element);
}

GST_END_TEST;

/* Disabling album processing before the end of the album.  Probably a rare edge
 * case and applications should not rely on this to work.  They need to send the
 * element to the READY state to clear up after an aborted album anyway since
 * they might need to process another album afterwards. */

GST_START_TEST (test_peak_album_abort_to_track)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;

  g_object_set (element, "num-tracks", 2, NULL);
  set_playing_state (element);

  push_buffer (test_buffer_const_float_stereo (8000, 1024, 1.0, 0.0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 1.0);
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 1);

  g_object_set (element, "num-tracks", 0, NULL);

  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.0, 0.5));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_gain_album)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gint accumulator;
  gint i;

  g_object_set (element, "num-tracks", 3, NULL);
  set_playing_state (element);

  /* The three tracks are constructed such that if any of these is in fact
   * ignored for the album gain, the album gain will differ. */

  accumulator = 0;
  for (i = 8; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.75, 0.75));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.75);
  fail_unless_track_gain (tag_list, -15.70);
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);

  accumulator = 0;
  for (i = 12; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.5, 0.5));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.5);
  fail_unless_track_gain (tag_list, -12.22);
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);

  accumulator = 0;
  for (i = 180; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.25, 0.25));
  send_eos_event (element);

  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, -6.20);
  fail_unless_album_peak (tag_list, 0.75);
  /* Strangely, wavegain reports -12.17 for the album, but the fixed
   * metaflac agrees to us.  Could be a 32767 vs. 32768 issue. */
  fail_unless_album_gain (tag_list, -12.18);
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

/* Checks ensuring that the "forced" property works as advertised. */

GST_START_TEST (test_forced)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gint accumulator = 0;
  gint i;

  g_object_set (element, "forced", FALSE, NULL);
  set_playing_state (element);

  tag_list = gst_tag_list_new ();
  /* Provided values are totally arbitrary. */
  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
      GST_TAG_TRACK_PEAK, 1.0, GST_TAG_TRACK_GAIN, 2.21, NULL);
  send_tag_event (element, tag_list);

  for (i = 20; i--;)
    push_buffer (test_buffer_const_float_stereo (44100, 512, 0.5, 0.5));
  send_eos_event (element);
  /* This fails if a tag message is generated: */
  poll_eos (element);

  /* Now back to a track without tags. */

  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.25, 0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (44100));
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

/* Sending track gain and peak in separate tag lists. */

GST_START_TEST (test_forced_separate)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gint accumulator = 0;
  gint i;

  g_object_set (element, "forced", FALSE, NULL);
  set_playing_state (element);

  tag_list = gst_tag_list_new ();
  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_GAIN, 2.21,
      NULL);
  send_tag_event (element, tag_list);

  tag_list = gst_tag_list_new ();
  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_PEAK, 1.0,
      NULL);
  send_tag_event (element, tag_list);

  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.5, 0.5));
  send_eos_event (element);
  /* This fails if a tag message is generated: */
  poll_eos (element);

  /* Now a track without tags. */

  accumulator = 0;
  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.25, 0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (44100));
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

/* A TAG event is sent _after_ data has already been processed.  In real
 * pipelines, this could happen if there is more than one rganalysis element (by
 * accident).  While it would have analyzed all the data prior to receiving the
 * event, I expect it to not post its results if not forced.  This test is
 * almost equivalent to test_forced. */

GST_START_TEST (test_forced_after_data)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gint accumulator = 0;
  gint i;

  g_object_set (element, "forced", FALSE, NULL);
  set_playing_state (element);

  for (i = 20; i--;)
    push_buffer (test_buffer_const_float_stereo (8000, 512, 0.5, 0.5));

  tag_list = gst_tag_list_new ();
  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
      GST_TAG_TRACK_PEAK, 1.0, GST_TAG_TRACK_GAIN, 2.21, NULL);
  send_tag_event (element, tag_list);

  send_eos_event (element);
  poll_eos (element);

  /* Now back to a normal track, this one has no tags: */
  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25,
            0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (8000));
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

/* Like test_forced, but *analyze* an album afterwards.  The two tests following
 * this one check the *skipping* of albums. */

GST_START_TEST (test_forced_album)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gint accumulator;
  gint i;

  g_object_set (element, "forced", FALSE, NULL);
  set_playing_state (element);

  tag_list = gst_tag_list_new ();
  /* Provided values are totally arbitrary. */
  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
      GST_TAG_TRACK_PEAK, 1.0, GST_TAG_TRACK_GAIN, 2.21, NULL);
  send_tag_event (element, tag_list);

  accumulator = 0;
  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.5, 0.5));
  send_eos_event (element);
  /* This fails if a tag message is generated: */
  poll_eos (element);

  /* Now an album without tags. */
  g_object_set (element, "num-tracks", 2, NULL);

  accumulator = 0;
  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.25, 0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (44100));
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 1);

  accumulator = 0;
  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.25, 0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (44100));
  fail_unless_album_peak (tag_list, 0.25);
  fail_unless_album_gain (tag_list, get_expected_gain (44100));
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 0);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_forced_album_skip)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gint accumulator = 0;
  gint i;

  g_object_set (element, "forced", FALSE, "num-tracks", 2, NULL);
  set_playing_state (element);

  tag_list = gst_tag_list_new ();
  /* Provided values are totally arbitrary. */
  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
      GST_TAG_TRACK_PEAK, 0.75, GST_TAG_TRACK_GAIN, 2.21,
      GST_TAG_ALBUM_PEAK, 0.80, GST_TAG_ALBUM_GAIN, -0.11, NULL);
  send_tag_event (element, tag_list);

  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25,
            0.25));
  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 1);

  /* This track has no tags, but needs to be skipped anyways since we
   * are in album processing mode. */
  for (i = 20; i--;)
    push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, 0.0));
  send_eos_event (element);
  poll_eos (element);
  fail_unless_num_tracks (element, 0);

  /* Normal track after the album.  Of course not to be skipped. */
  accumulator = 0;
  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25,
            0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (8000));
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_forced_album_no_skip)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gint accumulator = 0;
  gint i;

  g_object_set (element, "forced", FALSE, "num-tracks", 2, NULL);
  set_playing_state (element);

  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25,
            0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (8000));
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 1);

  /* The second track has indeed full tags, but although being not forced, this
   * one has to be processed because album processing is on. */
  tag_list = gst_tag_list_new ();
  /* Provided values are totally arbitrary. */
  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
      GST_TAG_TRACK_PEAK, 0.75, GST_TAG_TRACK_GAIN, 2.21,
      GST_TAG_ALBUM_PEAK, 0.80, GST_TAG_ALBUM_GAIN, -0.11, NULL);
  send_tag_event (element, tag_list);
  for (i = 20; i--;)
    push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, 0.0));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.0);
  fail_unless_track_gain (tag_list, SILENCE_GAIN);
  /* Second track was just silence so the album peak equals the first
   * track's peak. */
  fail_unless_album_peak (tag_list, 0.25);
  /* Statistical processing leads to the second track being
   * ignored for the gain (because it is so short): */
  fail_unless_album_gain (tag_list, get_expected_gain (8000));
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 0);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_forced_abort_album_no_skip)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gint accumulator = 0;
  gint i;

  g_object_set (element, "forced", FALSE, "num-tracks", 2, NULL);
  set_playing_state (element);

  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25,
            0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (8000));
  fail_if_album_tags (tag_list);
  gst_tag_list_free (tag_list);
  fail_unless_num_tracks (element, 1);

  /* Disabling album processing before end of album: */
  g_object_set (element, "num-tracks", 0, NULL);

  /* Processing a track that has to be skipped. */
  tag_list = gst_tag_list_new ();
  /* Provided values are totally arbitrary. */
  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
      GST_TAG_TRACK_PEAK, 0.75, GST_TAG_TRACK_GAIN, 2.21,
      GST_TAG_ALBUM_PEAK, 0.80, GST_TAG_ALBUM_GAIN, -0.11, NULL);
  send_tag_event (element, tag_list);
  for (i = 20; i--;)
    push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, 0.0));
  send_eos_event (element);
  poll_eos (element);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_reference_level)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gdouble ref_level;
  gint accumulator = 0;
  gint i;

  set_playing_state (element);

  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.25, 0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (44100));
  fail_if_album_tags (tag_list);
  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL,
          &ref_level) && MATCH_GAIN (ref_level, 89.),
      "Incorrect reference level tag");
  gst_tag_list_free (tag_list);

  g_object_set (element, "reference-level", 83., "num-tracks", 2, NULL);

  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.25, 0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (44100) - 6.);
  fail_if_album_tags (tag_list);
  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL,
          &ref_level) && MATCH_GAIN (ref_level, 83.),
      "Incorrect reference level tag");
  gst_tag_list_free (tag_list);

  accumulator = 0;
  for (i = 20; i--;)
    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512,
            0.25, 0.25));
  send_eos_event (element);
  tag_list = poll_tags (element);
  fail_unless_track_peak (tag_list, 0.25);
  fail_unless_track_gain (tag_list, get_expected_gain (44100) - 6.);
  fail_unless_album_peak (tag_list, 0.25);
  /* We provided the same waveform twice, with a reset separating
   * them.  Therefore, the album gain matches the track gain. */
  fail_unless_album_gain (tag_list, get_expected_gain (44100) - 6.);
  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL,
          &ref_level) && MATCH_GAIN (ref_level, 83.),
      "Incorrect reference level tag");
  gst_tag_list_free (tag_list);

  cleanup_rganalysis (element);
}

GST_END_TEST;

GST_START_TEST (test_all_formats)
{
  GstElement *element = setup_rganalysis ();
  GstTagList *tag_list;
  gint accumulator = 0;
  gint i, j;

  set_playing_state (element);
  for (i = G_N_ELEMENTS (supported_rates); i--;) {
    accumulator = 0;
    for (j = 0; j < 4; j++)
      push_buffer (test_buffer_square_float_stereo (&accumulator,
              supported_rates[i].sample_rate, 512, 0.25, 0.25));
    for (j = 0; j < 3; j++)
      push_buffer (test_buffer_square_float_mono (&accumulator,
              supported_rates[i].sample_rate, 512, 0.25));
    for (j = 0; j < 4; j++)
      push_buffer (test_buffer_square_int16_stereo (&accumulator,
              supported_rates[i].sample_rate, 16, 512, 1 << 13, 1 << 13));
    for (j = 0; j < 3; j++)
      push_buffer (test_buffer_square_int16_mono (&accumulator,
              supported_rates[i].sample_rate, 16, 512, 1 << 13));
    for (j = 0; j < 3; j++)
      push_buffer (test_buffer_square_int16_stereo (&accumulator,
              supported_rates[i].sample_rate, 8, 512, 1 << 5, 1 << 5));
    for (j = 0; j < 3; j++)
      push_buffer (test_buffer_square_int16_mono (&accumulator,
              supported_rates[i].sample_rate, 8, 512, 1 << 5));
    send_eos_event (element);
    tag_list = poll_tags (element);
    fail_unless_track_peak (tag_list, 0.25);
    fail_unless_track_gain (tag_list, supported_rates[i].gain);
    gst_tag_list_free (tag_list);
  }

  cleanup_rganalysis (element);
}

GST_END_TEST;

/* Checks ensuring all advertised supported sample rates are really
 * accepted, for integer and float, mono and stereo.  This also
 * verifies that the correct gain is computed for all formats (except
 * odd bit depths). */

#define MAKE_GAIN_TEST_FLOAT_MONO(sample_rate)                        \
  GST_START_TEST (test_gain_float_mono_##sample_rate)                 \
{                                                                     \
  GstElement *element = setup_rganalysis ();                          \
  GstTagList *tag_list;                                               \
  gint accumulator = 0;                                               \
  gint i;                                                             \
                                                                      \
  set_playing_state (element);                                        \
                                                                      \
  for (i = 0; i < 20; i++)                                            \
    push_buffer (test_buffer_square_float_mono (&accumulator,         \
            sample_rate, 512, 0.25));                                 \
  send_eos_event (element);                                           \
  tag_list = poll_tags (element);                                     \
  fail_unless_track_peak (tag_list, 0.25);                            \
  fail_unless_track_gain (tag_list,                                   \
      get_expected_gain (sample_rate));                               \
  gst_tag_list_free (tag_list);                                       \
                                                                      \
  cleanup_rganalysis (element);                                       \
}                                                                     \
                                                                      \
GST_END_TEST;

#define MAKE_GAIN_TEST_FLOAT_STEREO(sample_rate)                      \
  GST_START_TEST (test_gain_float_stereo_##sample_rate)               \
{                                                                     \
  GstElement *element = setup_rganalysis ();                          \
  GstTagList *tag_list;                                               \
  gint accumulator = 0;                                               \
  gint i;                                                             \
                                                                      \
  set_playing_state (element);                                        \
                                                                      \
  for (i = 0; i < 20; i++)                                            \
    push_buffer (test_buffer_square_float_stereo (&accumulator,       \
            sample_rate, 512, 0.25, 0.25));                           \
  send_eos_event (element);                                           \
  tag_list = poll_tags (element);                                     \
  fail_unless_track_peak (tag_list, 0.25);                            \
  fail_unless_track_gain (tag_list,                                   \
      get_expected_gain (sample_rate));                               \
  gst_tag_list_free (tag_list);                                       \
                                                                      \
  cleanup_rganalysis (element);                                       \
}                                                                     \
                                                                      \
GST_END_TEST;

#define MAKE_GAIN_TEST_INT16_MONO(sample_rate, depth)                 \
  GST_START_TEST (test_gain_int16_##depth##_mono_##sample_rate)       \
{                                                                     \
  GstElement *element = setup_rganalysis ();                          \
  GstTagList *tag_list;                                               \
  gint accumulator = 0;                                               \
  gint i;                                                             \
                                                                      \
  set_playing_state (element);                                        \
                                                                      \
  for (i = 0; i < 20; i++)                                            \
    push_buffer (test_buffer_square_int16_mono (&accumulator,         \
            sample_rate, depth, 512, 1 << (13 + depth - 16)));        \
                                                                      \
  send_eos_event (element);                                           \
  tag_list = poll_tags (element);                                     \
  fail_unless_track_peak (tag_list, 0.25);                            \
  fail_unless_track_gain (tag_list,                                   \
      get_expected_gain (sample_rate));                               \
  gst_tag_list_free (tag_list);                                       \
                                                                      \
  cleanup_rganalysis (element);                                       \
}                                                                     \
                                                                      \
GST_END_TEST;

#define MAKE_GAIN_TEST_INT16_STEREO(sample_rate, depth)               \
  GST_START_TEST (test_gain_int16_##depth##_stereo_##sample_rate)     \
{                                                                     \
  GstElement *element = setup_rganalysis ();                          \
  GstTagList *tag_list;                                               \
  gint accumulator = 0;                                               \
  gint i;                                                             \
                                                                      \
  set_playing_state (element);                                        \
                                                                      \
  for (i = 0; i < 20; i++)                                            \
    push_buffer (test_buffer_square_int16_stereo (&accumulator,       \
            sample_rate, depth, 512, 1 << (13 + depth - 16),          \
            1 << (13 + depth - 16)));                                 \
  send_eos_event (element);                                           \
  tag_list = poll_tags (element);                                     \
  fail_unless_track_peak (tag_list, 0.25);                            \
  fail_unless_track_gain (tag_list,                                   \
      get_expected_gain (sample_rate));                               \
  gst_tag_list_free (tag_list);                                       \
                                                                      \
  cleanup_rganalysis (element);                                       \
}                                                                     \
                                                                      \
GST_END_TEST;

MAKE_GAIN_TEST_FLOAT_MONO (8000);
MAKE_GAIN_TEST_FLOAT_MONO (11025);
MAKE_GAIN_TEST_FLOAT_MONO (12000);
MAKE_GAIN_TEST_FLOAT_MONO (16000);
MAKE_GAIN_TEST_FLOAT_MONO (22050);
MAKE_GAIN_TEST_FLOAT_MONO (24000);
MAKE_GAIN_TEST_FLOAT_MONO (32000);
MAKE_GAIN_TEST_FLOAT_MONO (44100);
MAKE_GAIN_TEST_FLOAT_MONO (48000);

MAKE_GAIN_TEST_FLOAT_STEREO (8000);
MAKE_GAIN_TEST_FLOAT_STEREO (11025);
MAKE_GAIN_TEST_FLOAT_STEREO (12000);
MAKE_GAIN_TEST_FLOAT_STEREO (16000);
MAKE_GAIN_TEST_FLOAT_STEREO (22050);
MAKE_GAIN_TEST_FLOAT_STEREO (24000);
MAKE_GAIN_TEST_FLOAT_STEREO (32000);
MAKE_GAIN_TEST_FLOAT_STEREO (44100);
MAKE_GAIN_TEST_FLOAT_STEREO (48000);

MAKE_GAIN_TEST_INT16_MONO (8000, 16);
MAKE_GAIN_TEST_INT16_MONO (11025, 16);
MAKE_GAIN_TEST_INT16_MONO (12000, 16);
MAKE_GAIN_TEST_INT16_MONO (16000, 16);
MAKE_GAIN_TEST_INT16_MONO (22050, 16);
MAKE_GAIN_TEST_INT16_MONO (24000, 16);
MAKE_GAIN_TEST_INT16_MONO (32000, 16);
MAKE_GAIN_TEST_INT16_MONO (44100, 16);
MAKE_GAIN_TEST_INT16_MONO (48000, 16);

MAKE_GAIN_TEST_INT16_STEREO (8000, 16);
MAKE_GAIN_TEST_INT16_STEREO (11025, 16);
MAKE_GAIN_TEST_INT16_STEREO (12000, 16);
MAKE_GAIN_TEST_INT16_STEREO (16000, 16);
MAKE_GAIN_TEST_INT16_STEREO (22050, 16);
MAKE_GAIN_TEST_INT16_STEREO (24000, 16);
MAKE_GAIN_TEST_INT16_STEREO (32000, 16);
MAKE_GAIN_TEST_INT16_STEREO (44100, 16);
MAKE_GAIN_TEST_INT16_STEREO (48000, 16);

MAKE_GAIN_TEST_INT16_MONO (8000, 8);
MAKE_GAIN_TEST_INT16_MONO (11025, 8);
MAKE_GAIN_TEST_INT16_MONO (12000, 8);
MAKE_GAIN_TEST_INT16_MONO (16000, 8);
MAKE_GAIN_TEST_INT16_MONO (22050, 8);
MAKE_GAIN_TEST_INT16_MONO (24000, 8);
MAKE_GAIN_TEST_INT16_MONO (32000, 8);
MAKE_GAIN_TEST_INT16_MONO (44100, 8);
MAKE_GAIN_TEST_INT16_MONO (48000, 8);

MAKE_GAIN_TEST_INT16_STEREO (8000, 8);
MAKE_GAIN_TEST_INT16_STEREO (11025, 8);
MAKE_GAIN_TEST_INT16_STEREO (12000, 8);
MAKE_GAIN_TEST_INT16_STEREO (16000, 8);
MAKE_GAIN_TEST_INT16_STEREO (22050, 8);
MAKE_GAIN_TEST_INT16_STEREO (24000, 8);
MAKE_GAIN_TEST_INT16_STEREO (32000, 8);
MAKE_GAIN_TEST_INT16_STEREO (44100, 8);
MAKE_GAIN_TEST_INT16_STEREO (48000, 8);

Suite *
rganalysis_suite (void)
{
  Suite *s = suite_create ("rganalysis");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);

  tcase_add_test (tc_chain, test_no_buffer);
  tcase_add_test (tc_chain, test_no_buffer_album_1);
  tcase_add_test (tc_chain, test_no_buffer_album_2);
  tcase_add_test (tc_chain, test_empty_buffers);

  tcase_add_test (tc_chain, test_peak_float);
  tcase_add_test (tc_chain, test_peak_int16_16);
  tcase_add_test (tc_chain, test_peak_int16_8);

  tcase_add_test (tc_chain, test_peak_album);
  tcase_add_test (tc_chain, test_peak_track_album);
  tcase_add_test (tc_chain, test_peak_album_abort_to_track);

  tcase_add_test (tc_chain, test_gain_album);

  tcase_add_test (tc_chain, test_forced);
  tcase_add_test (tc_chain, test_forced_separate);
  tcase_add_test (tc_chain, test_forced_after_data);
  tcase_add_test (tc_chain, test_forced_album);
  tcase_add_test (tc_chain, test_forced_album_skip);
  tcase_add_test (tc_chain, test_forced_album_no_skip);
  tcase_add_test (tc_chain, test_forced_abort_album_no_skip);

  tcase_add_test (tc_chain, test_reference_level);

  tcase_add_test (tc_chain, test_all_formats);

  tcase_add_test (tc_chain, test_gain_float_mono_8000);
  tcase_add_test (tc_chain, test_gain_float_mono_11025);
  tcase_add_test (tc_chain, test_gain_float_mono_12000);
  tcase_add_test (tc_chain, test_gain_float_mono_16000);
  tcase_add_test (tc_chain, test_gain_float_mono_22050);
  tcase_add_test (tc_chain, test_gain_float_mono_24000);
  tcase_add_test (tc_chain, test_gain_float_mono_32000);
  tcase_add_test (tc_chain, test_gain_float_mono_44100);
  tcase_add_test (tc_chain, test_gain_float_mono_48000);

  tcase_add_test (tc_chain, test_gain_float_stereo_8000);
  tcase_add_test (tc_chain, test_gain_float_stereo_11025);
  tcase_add_test (tc_chain, test_gain_float_stereo_12000);
  tcase_add_test (tc_chain, test_gain_float_stereo_16000);
  tcase_add_test (tc_chain, test_gain_float_stereo_22050);
  tcase_add_test (tc_chain, test_gain_float_stereo_24000);
  tcase_add_test (tc_chain, test_gain_float_stereo_32000);
  tcase_add_test (tc_chain, test_gain_float_stereo_44100);
  tcase_add_test (tc_chain, test_gain_float_stereo_48000);

  tcase_add_test (tc_chain, test_gain_int16_16_mono_8000);
  tcase_add_test (tc_chain, test_gain_int16_16_mono_11025);
  tcase_add_test (tc_chain, test_gain_int16_16_mono_12000);
  tcase_add_test (tc_chain, test_gain_int16_16_mono_16000);
  tcase_add_test (tc_chain, test_gain_int16_16_mono_22050);
  tcase_add_test (tc_chain, test_gain_int16_16_mono_24000);
  tcase_add_test (tc_chain, test_gain_int16_16_mono_32000);
  tcase_add_test (tc_chain, test_gain_int16_16_mono_44100);
  tcase_add_test (tc_chain, test_gain_int16_16_mono_48000);

  tcase_add_test (tc_chain, test_gain_int16_16_stereo_8000);
  tcase_add_test (tc_chain, test_gain_int16_16_stereo_11025);
  tcase_add_test (tc_chain, test_gain_int16_16_stereo_12000);
  tcase_add_test (tc_chain, test_gain_int16_16_stereo_16000);
  tcase_add_test (tc_chain, test_gain_int16_16_stereo_22050);
  tcase_add_test (tc_chain, test_gain_int16_16_stereo_24000);
  tcase_add_test (tc_chain, test_gain_int16_16_stereo_32000);
  tcase_add_test (tc_chain, test_gain_int16_16_stereo_44100);
  tcase_add_test (tc_chain, test_gain_int16_16_stereo_48000);

  tcase_add_test (tc_chain, test_gain_int16_8_mono_8000);
  tcase_add_test (tc_chain, test_gain_int16_8_mono_11025);
  tcase_add_test (tc_chain, test_gain_int16_8_mono_12000);
  tcase_add_test (tc_chain, test_gain_int16_8_mono_16000);
  tcase_add_test (tc_chain, test_gain_int16_8_mono_22050);
  tcase_add_test (tc_chain, test_gain_int16_8_mono_24000);
  tcase_add_test (tc_chain, test_gain_int16_8_mono_32000);
  tcase_add_test (tc_chain, test_gain_int16_8_mono_44100);
  tcase_add_test (tc_chain, test_gain_int16_8_mono_48000);

  tcase_add_test (tc_chain, test_gain_int16_8_stereo_8000);
  tcase_add_test (tc_chain, test_gain_int16_8_stereo_11025);
  tcase_add_test (tc_chain, test_gain_int16_8_stereo_12000);
  tcase_add_test (tc_chain, test_gain_int16_8_stereo_16000);
  tcase_add_test (tc_chain, test_gain_int16_8_stereo_22050);
  tcase_add_test (tc_chain, test_gain_int16_8_stereo_24000);
  tcase_add_test (tc_chain, test_gain_int16_8_stereo_32000);
  tcase_add_test (tc_chain, test_gain_int16_8_stereo_44100);
  tcase_add_test (tc_chain, test_gain_int16_8_stereo_48000);

  return s;
}

int
main (int argc, char **argv)
{
  gint nf;

  Suite *s = rganalysis_suite ();
  SRunner *sr = srunner_create (s);

  gst_check_init (&argc, &argv);

  srunner_run_all (sr, CK_ENV);
  nf = srunner_ntests_failed (sr);
  srunner_free (sr);

  return nf;
}