Split regain play tests into separate script.
[flac.git] / test / test_replaygain.sh
1 #!/bin/sh -e
2
3 #  FLAC - Free Lossless Audio Codec
4 #  Copyright (C) 2002-2009  Josh Coalson
5 #  Copyright (C) 2011-2013  Xiph.Org Foundation
6 #
7 #  This file is part the FLAC project.  FLAC is comprised of several
8 #  components distributed under different licenses.  The codec libraries
9 #  are distributed under Xiph.Org's BSD-like license (see the file
10 #  COPYING.Xiph in this distribution).  All other programs, libraries, and
11 #  plugins are distributed under the GPL (see COPYING.GPL).  The documentation
12 #  is distributed under the Gnu FDL (see COPYING.FDL).  Each file in the
13 #  FLAC distribution contains at the top the terms under which it may be
14 #  distributed.
15 #
16 #  Since this particular file is relevant to all components of FLAC,
17 #  it may be distributed under the Xiph.Org license, which is the least
18 #  restrictive of those mentioned above.  See the file COPYING.Xiph in this
19 #  distribution.
20
21 die ()
22 {
23         echo $* 1>&2
24         exit 1
25 }
26
27 if [ x = x"$1" ] ; then
28         BUILD=debug
29 else
30         BUILD="$1"
31 fi
32
33 # change to 'false' to show all flac/metaflac output (useful for debugging)
34 if true ; then
35         SILENT='--silent'
36         TOTALLY_SILENT='--totally-silent'
37 else
38         SILENT=''
39         TOTALLY_SILENT=''
40 fi
41
42 LD_LIBRARY_PATH=`pwd`/../src/libFLAC/.libs:$LD_LIBRARY_PATH
43 LD_LIBRARY_PATH=`pwd`/../src/share/grabbag/.libs:$LD_LIBRARY_PATH
44 LD_LIBRARY_PATH=`pwd`/../src/share/getopt/.libs:$LD_LIBRARY_PATH
45 LD_LIBRARY_PATH=`pwd`/../src/share/replaygain_analysis/.libs:$LD_LIBRARY_PATH
46 LD_LIBRARY_PATH=`pwd`/../src/share/replaygain_synthesis/.libs:$LD_LIBRARY_PATH
47 LD_LIBRARY_PATH=`pwd`/../src/share/utf8/.libs:$LD_LIBRARY_PATH
48 LD_LIBRARY_PATH=`pwd`/../objs/$BUILD/lib:$LD_LIBRARY_PATH
49 export LD_LIBRARY_PATH
50 export MALLOC_CHECK_=3
51 export MALLOC_PERTURB_=$(($(date +%s) % 255 + 1))
52 PATH=`pwd`/../src/flac:$PATH
53 PATH=`pwd`/../src/metaflac:$PATH
54 PATH=`pwd`/../objs/$BUILD/bin:$PATH
55
56 if echo a | (grep -E '(a|b)') >/dev/null 2>&1
57         then EGREP='grep -E'
58         else EGREP='egrep'
59 fi
60
61 testdir="metaflac-test-files"
62 flacfile="replaygain.flac"
63
64 run_flac ()
65 {
66         if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
67                 echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 flac $*" >>test_metaflac.valgrind.log
68                 valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 flac --no-error-on-compression-fail $* 4>>test_metaflac.valgrind.log
69         else
70                 flac --no-error-on-compression-fail $*
71         fi
72 }
73
74 run_metaflac ()
75 {
76         if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
77                 echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 metaflac $*" >>test_metaflac.valgrind.log
78                 valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 metaflac $* 4>>test_metaflac.valgrind.log
79         else
80                 metaflac $*
81         fi
82 }
83
84 run_metaflac_silent ()
85 {
86         if [ -z "$SILENT" ] ; then
87                 run_metaflac $*
88         else
89                 if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
90                         echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 metaflac $*" >>test_metaflac.valgrind.log
91                         valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 metaflac $* 2>/dev/null 4>>test_metaflac.valgrind.log
92                 else
93                         metaflac $* 2>/dev/null
94                 fi
95         fi
96 }
97
98 check_flac ()
99 {
100         run_flac --silent --test $flacfile || die "ERROR in $flacfile" 1>&2
101 }
102
103 echo "Generating stream..."
104 bytes=80000
105 if dd if=/dev/zero ibs=1 count=$bytes | flac --force --verify -0 --input-size=$bytes --output-name=$flacfile --force-raw-format --endian=big --sign=signed --channels=1 --bps=8 --sample-rate=8000 - ; then
106         chmod +w $flacfile
107 else
108         die "ERROR during generation"
109 fi
110
111 check_flac
112
113 # Replay gain tests - Test the rates which have specific filter table entries
114 # and verify that harmonics can be processed correctly.
115
116 tonegenerator ()
117 {
118         # When using GAWK, use --lint=posix to identify non-POSIX awk usages.
119     awk -- '
120     BEGIN {
121             samplerate = '$1';
122
123             tone = 1000;
124             duration = 1;
125             bitspersample = 24;
126
127             samplemidpoint = 1;
128                         for (sps = 0 ; sps < bitspersample - 1 ; sps++) {
129                                 samplemidpoint *= 2;
130                         }
131
132             samplerange = samplemidpoint - 1;
133
134             pi = 4 * atan2(1,1);
135
136             for (ix = 0; ix < duration * samplerate; ++ix) {
137                     sample = sin(2 * pi * tone * ix / samplerate);
138                     sample *= samplerange;
139                     sample += samplemidpoint;
140                     sample = int(sample);
141                     for (bx = 0; bx < bitspersample/8; ++bx) {
142                             byte[bx] = sample % 256;
143                             sample /= 256;
144                     }
145                     while (bx--) {
146                             printf("%c", byte[bx]);
147                     }
148             }
149
150     }' /dev/null |
151     flac --silent --no-error-on-compression-fail \
152         --endian=big --channels=1 --bps=24 --sample-rate=$1 --sign=unsigned -
153 }
154
155 REPLAYGAIN_FREQ=
156 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ  8000/-12.73"
157 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 11025/-12.93"
158 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 11025/-12.93"
159 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 12000/-12.98"
160 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 16000/-13.27"
161 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 18900/-13.41"
162 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 22050/-13.77"
163 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 24000/-13.82"
164 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 28000/-14.06"
165 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 32000/-14.08"
166 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 36000/-14.12"
167 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 37800/-14.18"
168 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 44100/-14.17"
169 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 48000/-14.16:1:2:4"
170
171 set -e
172
173 for ACTION in $REPLAYGAIN_FREQ ; do
174   if [ -n "${ACTION##*:*}" ] ; then
175     HARMONICS=1
176   else
177     HARMONICS="${ACTION#*:}"
178   fi
179   FREQ="${ACTION%%/*}"
180   GAIN="${ACTION#*/}"
181   GAIN="${GAIN%%:*}"
182   while [ -n "$HARMONICS" ] ; do
183     MULTIPLE="${HARMONICS%%:*}"
184     if [ x"$MULTIPLE" = x"$HARMONICS" ] ; then
185       HARMONICS=
186     else
187       HARMONICS="${HARMONICS#*:}"
188     fi
189     RATE=$(($MULTIPLE * FREQ))
190     [ $MULTIPLE -eq 1 -o -n "${REPLAYGAIN_FREQ##* $RATE/*}" ] || break
191     echo -n "Testing FLAC replaygain $RATE ($FREQ x $MULTIPLE) ... "
192     tonegenerator $RATE > $flacfile
193     run_metaflac --add-replay-gain $flacfile
194     run_metaflac --list $flacfile | grep REPLAYGAIN.*GAIN= |
195     while read -r REPLAYGAIN ; do
196       MEASUREDGAIN="${REPLAYGAIN##*=}"
197       MEASUREDGAIN="${MEASUREDGAIN%% *}"
198       if [ x"$MEASUREDGAIN" != x"$GAIN" ] ; then
199         die "ERROR, Expected $GAIN db instead of $REPLAYGAIN"
200       fi
201     done
202     echo OK
203   done
204 done
205
206
207 rm -f $testdir/out.flac $testdir/out.meta
208
209 exit 0